From nobody Wed Dec 17 12:48:30 2025 Received: from mail-ua1-f73.google.com (mail-ua1-f73.google.com [209.85.222.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E29D91B0F25 for ; Tue, 10 Dec 2024 21:31:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733866264; cv=none; b=u0U07kkSF5Dks1C38SkdRgdzx07Oxj3EuTms+ZAiN9Rl3XBMq1PFOcCDbtxcDXPZ31jdrCIfVo4xyB0bdBHBkcMAW4aJJU3asvKIInJlBnEqZKiUe8C8ktVTzJ5nWIqfFwq3Yd0T5S0g2GwxM0BQbV74yn/1BIOf+WhNFQPf7fE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733866264; c=relaxed/simple; bh=cLn5rbfFYFPA2ZQZv+GbmAK03oo9d1PmaRYCNciDab8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LZDHHUPeBXaYQxb6I0MvKTfMFti2wfWNVhgEVBgXyLXoD0MwI60r94LiC6+d2XPghNygdcb8Gg5yT0n7GLMx0hqtNbtVl8Eeyv/2X/nNj8g9ATJMZy1NalBNUoRsjXXFlQTI3WyCKzz5hA9U+7MEztHZC4+f3JnkSNB25egbvoI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--bgeffon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oXLUlW8B; arc=none smtp.client-ip=209.85.222.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--bgeffon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oXLUlW8B" Received: by mail-ua1-f73.google.com with SMTP id a1e0cc1a2514c-85217018d93so815505241.1 for ; Tue, 10 Dec 2024 13:31:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733866261; x=1734471061; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dgSSzFrkXg1CYSEsBlnHDnKWydapEER+JZ1i3v6REhA=; b=oXLUlW8B+H98TPdSMAdDLUGi5iaTEXcN/wQJfDs8Jm9rBnXJJ9gH3bqKxeoPKYLreY A0CjGIAwv745qU879Uhhgz7sZK0UPt56AkrYAMozR1rdQQ7y6loGN5lIettJHEGac+Ka E7nNiED6ayHEzeSWAiXPLnkZUdnIowypo2CdBD2t2CISJu0wpBHhsrXDG4Dgsu6oU2zY gHr6iiUmowGxy6PllhFwV2v5sKtxaixozc8epaCQ/0S3bpETr0YGH95N/Y8HOyHAohAP njUd2z8aFX2i2usQjhlPS9CU+H0Z0dk71a/KDwyS1Wdpm05Amzx3wS1bozunOJsvykjF x8QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733866261; x=1734471061; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dgSSzFrkXg1CYSEsBlnHDnKWydapEER+JZ1i3v6REhA=; b=GxzbJ73lVjQse/H57i6LXm4jfNLPagA+AvmHy25jIxgzDS625EkjHilYGDLTmAfHoA HL3SowblsQX+K8YsaoLp7Mj5PDMY6VzAq4DHRAT6a/v7xjaxF376Jab37F/aJppYpx3I vAr0irzZBCBhH/gNJGHV1qgHj0LrAdy3Vb7r1IZOQVmPKUPEiPNEQnQffQa7v+EIMXwq AHLMFGO285VD/MpjLBk/VO75+2l+QGEYkQUqamAAwfoe1oZr5N45zpgnaDJELmbbJtd1 D0PcN1jQYF6N2oz7Z0TrCZpk8ZoK3WZVJEBe9WBaUlJDFbIgcJtsLzdiXKf9iBZGuVOb /U0g== X-Forwarded-Encrypted: i=1; AJvYcCX4Blw7qCg8GzK3FYwi1gA/61l+sMuz3yZRGa0PH5MTZ1nCDF8sl0uvbG2z8l0g08CqipkdtNQ/D0LLqPc=@vger.kernel.org X-Gm-Message-State: AOJu0Yy0OfPV7idaIed1gLv7jrrqsnRjAJcQm/J8iXeaIvwRsmfM3GXY XTSVDT57ztJNeZs0xlgcH+j9SrfISg210yDxACOGCVNP6pWfBgujYTBVjLqbdgv/6+FFPejs6O6 Z060nhA== X-Google-Smtp-Source: AGHT+IEKDfK3mB1Oud35BwI5L0PRyQfm0U+xeis8VT50pizsN9eNp+xwqUl3Ojn1lQWMegDIcvf/M2NcwiQC X-Received: from vsvd7.prod.google.com ([2002:a05:6102:1487:b0:4b1:11cd:b284]) (user=bgeffon job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:1629:b0:4af:4ae0:2320 with SMTP id ada2fe7eead31-4b1291bba89mr1461879137.26.1733866260721; Tue, 10 Dec 2024 13:31:00 -0800 (PST) Date: Tue, 10 Dec 2024 16:30:46 -0500 In-Reply-To: <20241210213050.2839638-1-bgeffon@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241210213050.2839638-1-bgeffon@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241210213050.2839638-2-bgeffon@google.com> Subject: [RFC PATCH 1/5] mm: mremap: Fix new_addr being used as a hint with MREMAP_DONTUNMAP From: Brian Geffon To: Andrew Morton Cc: Lorenzo Stoakes , Jann Horn , Vlastimil Babka , "Liam R. Howlett" , linux-mm@kvack.org, Marco Vanotti , linux-kernel@vger.kernel.org, Brian Geffon Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Two non-mutually exclusive paths can land in mremap_to, MREMAP_FIXED and MREMAP_DONTUNMAP which are called from mremap(). In the case of MREMAP_FIXED we must validate the new_addr to ensure that the new address is valid. In the case of MREMAP_DONTUNMAP without MREMAP_FIXED a new address is specified as a hint, just like it would be in the case of mmap. In this second case we don't need to perform any checks because get_unmapped_area() will align new_addr, just like it would in the case of mmap. This patch only fixes the behavior that was inadvertently added with MREMAP_DONTUNMAP. v2: - Addressed comment from Marco Vanotti to consolidate these checks into existing MREMAP_FIXED blocks. Signed-off-by: Brian Geffon Reported-by: Marco Vanotti Reviewed-By: Marco Vanotti --- mm/mremap.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/mm/mremap.c b/mm/mremap.c index 60473413836b..62aec72bbe42 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -912,16 +912,6 @@ static unsigned long mremap_to(unsigned long addr, uns= igned long old_len, unsigned long ret; unsigned long map_flags =3D 0; =20 - if (offset_in_page(new_addr)) - return -EINVAL; - - if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len) - return -EINVAL; - - /* Ensure the old/new locations do not overlap */ - if (addr + old_len > new_addr && new_addr + new_len > addr) - return -EINVAL; - /* * move_vma() need us to stay 4 maps below the threshold, otherwise * it will bail out at the very beginning. @@ -940,6 +930,25 @@ static unsigned long mremap_to(unsigned long addr, uns= igned long old_len, return -ENOMEM; =20 if (flags & MREMAP_FIXED) { + /* + * Two non-mutually exclusive paths can land in mremap_to, MREMAP_FIXED + * and MREMAP_DONTUNMAP which are called from mremap(). In the case of + * MREMAP_FIXED we must validate the new_addr to ensure that the new + * address is valid. In the case of MREMAP_DONTUNMAP without MREMAP_FIXED + * a new address is specified as a hint, just like it would be in the + * case of mmap. In this second case we don't need to perform any checks + * because get_unmapped_area() will align new_addr, just like it would in + * the case of mmap. + */ + if (offset_in_page(new_addr)) + return -EINVAL; + + if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len) + return -EINVAL; + + /* Ensure the old/new locations do not overlap */ + if (addr + old_len > new_addr && new_addr + new_len > addr) + return -EINVAL; /* * In mremap_to(). * VMA is moved to dst address, and munmap dst first. --=20 2.47.0.338.g60cca15819-goog From nobody Wed Dec 17 12:48:30 2025 Received: from mail-qt1-f202.google.com (mail-qt1-f202.google.com [209.85.160.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD1771B0F3E for ; Tue, 10 Dec 2024 21:31:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733866264; cv=none; b=M947MCsAxnHuoNcGsiviNqNej95bpGzRaCWRP0tgOSp7Eg4YUaybabPNha+TacODLZnFBI6mTB2jaWAQkuml5TZVGgRGG/Ex9ZTMDpkrq9CQKc9+q8nAzMp/C3KNrttSbz5QRGwY6K0W3JFSWVj0wuuQKyDDr3B4AhM600IK4/Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733866264; c=relaxed/simple; bh=IOZAPs5Tx3+mzCRFJUlx/4J6iv0fhE1GwZLu+JUI+d8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hDjrlndu9JiBUUf1SGv5cKJ8hii0MOm0GGKCdcwz3Zapr8RWmCTwsum+Q9lvZp1nbDpI8N+3BwJ8pXHzlIvpSg+8hBvsqPmCDo8PtduYuQIpOYIuXR4wlYrPYyC0IA7P192Ocvn7/6ABPRPXycqWjNWTiVeSPxPe61aiPg9CDd0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--bgeffon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=yR1thKEB; arc=none smtp.client-ip=209.85.160.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--bgeffon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yR1thKEB" Received: by mail-qt1-f202.google.com with SMTP id d75a77b69052e-4675c482d6cso50522811cf.2 for ; Tue, 10 Dec 2024 13:31:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733866262; x=1734471062; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=G32JxFMabBUDQi+52by10jYMucvOr3O4hJ3hDDPHLcQ=; b=yR1thKEBGPljTAoRQ76gAJbwfqRmBVktKtuZG8qFNyxW2En1vEdfbyItXTpKJvhOF6 QTMG0dQnObtI6ziv3xCwWmfcZoSfjpER7JmoFJ5UCZZnUbJc/jR9ch/kO2mMoBaDCirF ZwHMAncTL/jw73WQB0k11QLDkmWNHtaBVpbm3vfPT0HavL352lz7gnj2HXBUA5hft6I0 7PUgb72+WKwvvHy34tCO3mEwUOA/grlG+SU8zYyAkZf+j3zbJodHqsZ7B/Fw9gycd0FO E+GO42dawTxgYybd8teReEkvV5jdcHKQDhFQUjwfSf+HJIW2yVLf2VfZKIW3TmTVaPJp Z3lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733866262; x=1734471062; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=G32JxFMabBUDQi+52by10jYMucvOr3O4hJ3hDDPHLcQ=; b=GwdLpEyjOwlaofgjcaaRpNbYmGMoMeVP5GTO0Gh00R+rk1CFWWDC9p2t1a90v6fppA 4HA92iFJTMsMU4GDB506HhC2sEbTxna8MKjqdcxSK23fPdiUBvNkGylNugOPJdUErdrK VDc9MXdJm5r5I0UEeeUnBP01X+YkZ1mLeguanWblBhORD4sPorxwdLHoiGBT6mRaOhjW /aCQBbQnf4T0Sr4VK4O5PXc8cfjt3IwbmaospO9V7f/PL1BROzwfgX13VHOOZ99JujIk gPjjfyqA4GqEa84xCV9gBoY6zXibvQWqaAPG+s0w2khxLvBoR6s/AFWRgMq9iMuAp4zV T+Mg== X-Forwarded-Encrypted: i=1; AJvYcCVMrSm8IE3zwSqH14bIJYbFRF8ohCZEKxqEgGu/+OfvUx3jjXqz2BEie847mRMj9JIvgknFc7vnJ+gGXdA=@vger.kernel.org X-Gm-Message-State: AOJu0YzG8OsjexvAI5H29AB9upO6wn76v5/kxeASKu3/1MfAPdc/d15W CrUzNvrF2wyErpVP4nIJX8xp9yyv6Cyx/YtCqi+6RyZAjEhfMLiSkJbkHn/OsffAdFZQqGF/0Bd DP5NF9A== X-Google-Smtp-Source: AGHT+IEpCtrL8H1FFUbminr4KvNlPmZTmCfZCaR3pkbbEKhlEMHUlCB45RdOMbWWkvCARQTAtmkhm4ng8J34 X-Received: from qtbbn6.prod.google.com ([2002:a05:622a:1dc6:b0:467:7fcc:751e]) (user=bgeffon job=prod-delivery.src-stubby-dispatcher) by 2002:ac8:7f93:0:b0:466:b1fb:3028 with SMTP id d75a77b69052e-467892e9a98mr7952001cf.4.1733866261754; Tue, 10 Dec 2024 13:31:01 -0800 (PST) Date: Tue, 10 Dec 2024 16:30:47 -0500 In-Reply-To: <20241210213050.2839638-1-bgeffon@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241210213050.2839638-1-bgeffon@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241210213050.2839638-3-bgeffon@google.com> Subject: [RFC PATCH 2/5] mm: mremap: Use round_hint_to_min() for new_addr hints From: Brian Geffon To: Andrew Morton Cc: Lorenzo Stoakes , Jann Horn , Vlastimil Babka , "Liam R. Howlett" , linux-mm@kvack.org, Marco Vanotti , linux-kernel@vger.kernel.org, Brian Geffon Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" MREMAP_DONTUNMAP allows for a get_unmapped_area() hint when used without MREMAP_FIXED. This was behavior was introduced inadvertently and should be rounded to the mmap_min_address like in mmap(2). Signed-off-by: Brian Geffon --- include/linux/mm_inline.h | 14 ++++++++++++++ mm/mmap.c | 13 ------------- mm/mremap.c | 3 +++ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 1b6a917fffa4..863143ec5bb0 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -9,6 +9,7 @@ #include #include #include +#include =20 /** * folio_is_file_lru - Should the folio be on a file LRU or anon LRU? @@ -613,4 +614,17 @@ static inline bool vma_has_recency(struct vm_area_stru= ct *vma) return true; } =20 +/* + * If a hint addr is less than mmap_min_addr change hint to be as + * low as possible but still greater than mmap_min_addr + */ +static inline unsigned long round_hint_to_min(unsigned long hint) +{ + hint &=3D PAGE_MASK; + if (((void *)hint !=3D NULL) && + (hint < mmap_min_addr)) + return PAGE_ALIGN(mmap_min_addr); + return hint; +} + #endif diff --git a/mm/mmap.c b/mm/mmap.c index d32b7e701058..04952ac21d58 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -215,19 +215,6 @@ SYSCALL_DEFINE1(brk, unsigned long, brk) return origbrk; } =20 -/* - * If a hint addr is less than mmap_min_addr change hint to be as - * low as possible but still greater than mmap_min_addr - */ -static inline unsigned long round_hint_to_min(unsigned long hint) -{ - hint &=3D PAGE_MASK; - if (((void *)hint !=3D NULL) && - (hint < mmap_min_addr)) - return PAGE_ALIGN(mmap_min_addr); - return hint; -} - bool mlock_future_ok(struct mm_struct *mm, unsigned long flags, unsigned long bytes) { diff --git a/mm/mremap.c b/mm/mremap.c index 62aec72bbe42..fdc1b0f1b38e 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -1109,6 +1109,9 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned= long, old_len, goto out; } =20 + if (!(flags & MREMAP_FIXED)) + new_addr =3D round_hint_to_min(new_addr); + if (flags & (MREMAP_FIXED | MREMAP_DONTUNMAP)) { ret =3D mremap_to(addr, old_len, new_addr, new_len, &locked, flags, &uf, &uf_unmap_early, --=20 2.47.0.338.g60cca15819-goog From nobody Wed Dec 17 12:48:30 2025 Received: from mail-qk1-f202.google.com (mail-qk1-f202.google.com [209.85.222.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C0D322FAC2 for ; Tue, 10 Dec 2024 21:31:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733866265; cv=none; b=iL2NQBEkIAouiFb/liiahWlkTVKKSD51LUP0m83uxgl4h5xQjYXikCxVsVjVawuOUtU4P4MhDU99IGqXdz0pq7+z9K0ho62obZcrFKwgyyzJVMFrHVT4f5SydPPN3SSgDcjOQg6l02Uf4IU0/0fgjZph5njmr9TbhW5xK5TIZQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733866265; c=relaxed/simple; bh=lgT59yOQQAh+32S2AgovLwSLHWGX99+a0idf7dOno7A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ML7EtsjnLCPw4R2IOmzUUcG4VyGm/nGN3KEoX8/dVED+WaUohxSNkcWj0/qm3kmMhTHJ7IMctufEsoaZAteaXoqb+Gqla+lFqD6HIiDTxwcnWRMwSAPsWbGENWFhwvZlkLn/z+7DnnvJjHqSFwu6AdcLAevTGxpbS00pZQO5xRM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--bgeffon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4ylsvyit; arc=none smtp.client-ip=209.85.222.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--bgeffon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4ylsvyit" Received: by mail-qk1-f202.google.com with SMTP id af79cd13be357-7b6e1b037d5so141593685a.0 for ; Tue, 10 Dec 2024 13:31:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733866262; x=1734471062; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qLtQZxHfyZ3UG9LKxPNXk6qON+2equfYqBxEH3X/7vQ=; b=4ylsvyityhFbZ+2POrQR798MdpQdWKe2vmUt9mmeOfpswcv60egk0uLA2xBxZdKmUk uOwObb0FW5iISxBp/mgW1acn8bQO8FwkA0j2siy4lZIepuQjDirnz9wzeyTIwIagE1CD flUt8apEySaXbqE6Ka2cnSBrY9bYMTlAQ43R3nfcsxO5lUyGX7T4JyhJES2raj9jTnuh vIB51D3bemybZzaY/69RsrLYId5+XRxJPi4YUy/cJa1tN8zOLNgNrNuaHqshWYiDdRJE AX8nB9Kuw1OReKQ6GDbt00QsTtMxqE1c1r13p03S4JpNu/62CudAyRpM7ARPiDTgVu/D z6Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733866262; x=1734471062; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qLtQZxHfyZ3UG9LKxPNXk6qON+2equfYqBxEH3X/7vQ=; b=a/wBFplnpRQ00rylcrRX31hCbANVRgc7kywxL4lc7CTrRyx+tb9TeuTZFzRlkOUzY5 DT8DRlOuow4+QbYi40eYEZBJ+oRG1daiC97SRtawHLOCq0MnveLIy1ElKuVZ0uJ9C+z7 pzjxCJZNUEExy/UcypmLOs2WbnHoNb42sA0wylWtzUNNaIjyeXIZl/Tx7p9oycVQZxOH 18/Ld1ORsl+NK0Z9z9z82ihGArR8fsZY+Rd37s3lLAH8JcApIUGAu4KyqWnQQP+RL5kC jB8WUpPV7LcKn+YrriqZpU+aHd0ZULZYLRLdjs03jGYQsIDmvOATMiTbW2X5wBgm1SL1 ZKHw== X-Forwarded-Encrypted: i=1; AJvYcCXkZ/Z9Cn7nY3to2CYxhzkaZdGN6oZxFxDczgoS2wHnEnWs166nIegrgoOzMIlKL+eZPVRO0PgNU3NdQLw=@vger.kernel.org X-Gm-Message-State: AOJu0YzWhCvbq5teYVCKJEy44zYSjc6btWWWxfqQpIj7C0Wg7CjIBMzy mlVILTNXrmRzKC6uXEA84/FEbUMnaWM6pD7ml8VELg2uciG5vZEL5pVEj4MWBlc9LhFKYiPYelQ gycy2wQ== X-Google-Smtp-Source: AGHT+IH3NMWyVkk/B0e5FDWJt6ft3j8uGcZlVg76bwog9+Es+PKknkRKBnzr77X+s6jFQDjeJUd3QE5HwBLx X-Received: from qkkl16.prod.google.com ([2002:a37:f510:0:b0:7b6:cfce:56c5]) (user=bgeffon job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:6181:b0:7b6:6c33:994e with SMTP id af79cd13be357-7b6eb433b8amr127896485a.6.1733866262599; Tue, 10 Dec 2024 13:31:02 -0800 (PST) Date: Tue, 10 Dec 2024 16:30:48 -0500 In-Reply-To: <20241210213050.2839638-1-bgeffon@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241210213050.2839638-1-bgeffon@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241210213050.2839638-4-bgeffon@google.com> Subject: [RFC PATCH 3/5] mm: mremap: Allow new_addr to be specified as a hint From: Brian Geffon To: Andrew Morton Cc: Lorenzo Stoakes , Jann Horn , Vlastimil Babka , "Liam R. Howlett" , linux-mm@kvack.org, Marco Vanotti , linux-kernel@vger.kernel.org, Brian Geffon Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When using MREMAP_MAYMOVE previously the new_addr was ignored unless the user specified MREMAP_FIXED. This change will allow it to be used as a hint in that situation similar to how mmap(2) behaves. get_unmapped_area() will handle page aligning the new address hint. Signed-off-by: Brian Geffon --- mm/mremap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/mremap.c b/mm/mremap.c index fdc1b0f1b38e..1d2522fba0ef 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -1205,7 +1205,7 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned= long, old_len, if (vma->vm_flags & VM_MAYSHARE) map_flags |=3D MAP_SHARED; =20 - new_addr =3D get_unmapped_area(vma->vm_file, 0, new_len, + new_addr =3D get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT), map_flags); --=20 2.47.0.338.g60cca15819-goog From nobody Wed Dec 17 12:48:30 2025 Received: from mail-qt1-f202.google.com (mail-qt1-f202.google.com [209.85.160.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B6CB23237E for ; Tue, 10 Dec 2024 21:31:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733866265; cv=none; b=CNnF3trlOwQGvwPPCc5gzH8d37ICr3jhsz53iNVVML5KSC+04JLpmZcfEo/o0XzXkZ7QTBSJwfGIJJAHF2iOaLU+oYmS7p4NgeUFuBOIk2zuPDnzG/+o4/fWGwKGik2jDRHgej6Gg/TZe/2nbwj0zLGR3YXjDyK7/rUF3exVO2U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733866265; c=relaxed/simple; bh=7BtEdFWcYLngsq2PQODOD5BiLZOIKMRwRSUO9nBBYBg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=E2r1wu2lFiGHM25xP0NIyntmyTdxirYjZOcL80FJZHfDF28Kus1lnKuKj05Z8jZvJe94cFjzJLu1MntBCRcdcFcjE779co7qr1KQ7M/z/a0pDwgiaMmV06oPaP1JSdDA2LxlH5TDQULeH8pka1WQj1OhHbj2iiohR0RspgViOiI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--bgeffon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bRCn6R6u; arc=none smtp.client-ip=209.85.160.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--bgeffon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bRCn6R6u" Received: by mail-qt1-f202.google.com with SMTP id d75a77b69052e-467518d8bdaso80023251cf.2 for ; Tue, 10 Dec 2024 13:31:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733866263; x=1734471063; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=8zF6cyo4V+yV1OddWsDU7llWFhEXM57i4QvxWba9u5g=; b=bRCn6R6u52WUg6LH0+J3obvVxCFUwN94ruyntEcF1gDkU5mD1jv1g/ReJbUzXuZlU6 HwD0QM79MojHI8TkJlYlT/gItF8nvnpGdVPb/KKPXUdilblmRJJASv9hX0rlrXXzbxlH QyOxwNa/cV5fd8o++YK/0mGN+qnV5Xe780uu9F9QoLw5/k8QmBh/KsaiOTkdofxO0rOC YHTpBPcn5zigw/oAHoRP37NX0uNMu4qrADEb70B6IGq6qASldJ8hcE7XiBxj72owEBxZ 7BR1V/3EOTJF2Xz7ZX+P3HpC6A6GDQu5Vo8MdoLjnhh90WsAhYxeuseagSjlIk2pI/bt MPsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733866263; x=1734471063; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8zF6cyo4V+yV1OddWsDU7llWFhEXM57i4QvxWba9u5g=; b=XkPp1sNlA1bourdTr21vs6Uj7jGzZ+owfOy3eaMvk+lEc7MMbRO4EB2peIs0xNB2zl Cd/wgVgxxeq8xlKafjpylocBnpNnPCss7dXbzTyTeSWDPABr7kFr1MG09DKbX51LiNmh iHUmSA5aHjaqr8eDAMFsM5GUlUihGv96H+Nd9L1zrcu3PviMhgSi0I2fQZHzeeVS/I/D YqLCSJ/4b9wO09fRh6KSYUaK1HC605faRNrxP28cL20St7IdT86h3EEPbhwriz5feoe4 xu4xP0K5Lxxk98nOnbIz7sEO67/RFFphUzD5CI4J6R7cG9mIkC75MpA5pKOmXyc/1DcG 9WSQ== X-Forwarded-Encrypted: i=1; AJvYcCVSgNZcitSkM6KduHJ+pd18o0gRC2yl+1fxXcyCUfYJp1s3gBXTC/aFDWzMB0QuUm98QNm+zPe4lx5/MbM=@vger.kernel.org X-Gm-Message-State: AOJu0Yw60WnZ6+JmZwCnx+D/MfURAl5AjNgmI/DVJKy7kDp9FWa39xlG 4nmT8HSeAj+LDckQxh10jQob5B4qzZ/h/OwHhopC59uELtI8+sYIwXzEjuKYh9/uA5ILBAOt7zT 2TDqRKA== X-Google-Smtp-Source: AGHT+IHZ1924mCsfRh+0nxIV+XSxlFuKLxmvXW19hSqpR+438olx78Wtep6vn7HuCeERSohDHX/QpTjD62ds X-Received: from qtbcc14.prod.google.com ([2002:a05:622a:410e:b0:467:518e:d31b]) (user=bgeffon job=prod-delivery.src-stubby-dispatcher) by 2002:ac8:58cd:0:b0:466:a3bf:41a7 with SMTP id d75a77b69052e-467893c594amr7220151cf.51.1733866263282; Tue, 10 Dec 2024 13:31:03 -0800 (PST) Date: Tue, 10 Dec 2024 16:30:49 -0500 In-Reply-To: <20241210213050.2839638-1-bgeffon@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241210213050.2839638-1-bgeffon@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241210213050.2839638-5-bgeffon@google.com> Subject: [RFC PATCH 4/5] selftests: mm: Add a new MREMAP_DONTUNMAP self test From: Brian Geffon To: Andrew Morton Cc: Lorenzo Stoakes , Jann Horn , Vlastimil Babka , "Liam R. Howlett" , linux-mm@kvack.org, Marco Vanotti , linux-kernel@vger.kernel.org, Brian Geffon Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a new selftest which validates that a new_addr as a hint behaves in the same way as mmap. Signed-off-by: Brian Geffon --- tools/testing/selftests/mm/mremap_dontunmap.c | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mremap_dontunmap.c b/tools/testing/= selftests/mm/mremap_dontunmap.c index 1d75084b9ca5..ccce97c68019 100644 --- a/tools/testing/selftests/mm/mremap_dontunmap.c +++ b/tools/testing/selftests/mm/mremap_dontunmap.c @@ -224,6 +224,44 @@ static void mremap_dontunmap_simple_fixed() ksft_test_result_pass("%s\n", __func__); } =20 +// This test validates MREMAP_DONTUNMAP using a newaddr hint without +// MREMAP_FIXED. +static void mremap_dontunmap_simple_newaddr_hint() +{ + unsigned long num_pages =3D 5; + + // This dest hint is intentionally not aligned. + void *new_addr_hint =3D (void*)0x999900010; + + void *source_mapping =3D + mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + BUG_ON(source_mapping =3D=3D MAP_FAILED, "mmap"); + memset(source_mapping, 'a', num_pages * page_size); + + void *remapped_mapping =3D + mremap(source_mapping, num_pages * page_size, num_pages * page_size, + MREMAP_DONTUNMAP | MREMAP_MAYMOVE, + new_addr_hint); + BUG_ON(remapped_mapping =3D=3D MAP_FAILED, "mremap"); + + // And the source mapping will have had its ptes dropped. + BUG_ON(check_region_contains_byte + (source_mapping, num_pages * page_size, 0) !=3D 0, + "source should have no ptes"); + + // And the remapped area will be filled with 'a's. + BUG_ON(check_region_contains_byte + (remapped_mapping, num_pages * page_size, 'a') !=3D 0, + "dest should have remapped content"); + + BUG_ON(munmap(source_mapping, num_pages * page_size) =3D=3D -1, + "unable to unmap source mapping"); + BUG_ON(munmap(remapped_mapping, num_pages * page_size) =3D=3D -1, + "unable to unmap source mapping"); + ksft_test_result_pass("%s\n", __func__); +} + // This test validates that we can MREMAP_DONTUNMAP for a portion of an // existing mapping. static void mremap_dontunmap_partial_mapping() @@ -348,7 +386,7 @@ int main(void) ksft_finished(); } =20 - ksft_set_plan(5); + ksft_set_plan(6); =20 // Keep a page sized buffer around for when we need it. page_buffer =3D @@ -359,6 +397,7 @@ int main(void) mremap_dontunmap_simple(); mremap_dontunmap_simple_shmem(); mremap_dontunmap_simple_fixed(); + mremap_dontunmap_simple_newaddr_hint(); mremap_dontunmap_partial_mapping(); mremap_dontunmap_partial_mapping_overwrite(); =20 --=20 2.47.0.338.g60cca15819-goog From nobody Wed Dec 17 12:48:30 2025 Received: from mail-qt1-f201.google.com (mail-qt1-f201.google.com [209.85.160.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1878D23238E for ; Tue, 10 Dec 2024 21:31:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733866269; cv=none; b=ZSlt2EugJrqCN43pxYjk0fb+gqIcZAaL1BD5GNLmgJgM/vNCAZ0BwX0Hi8hgAmaHwYHy5E0c5siMNqP22wEplK1s+m7yLpbesW3bVBELKzC0vucLY5K4reyYTWqpgGagBQEmp7dRUDb1SnCtSTUVODnkuU53pYteYC/6qbyLinc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733866269; c=relaxed/simple; bh=Fo+jcVx3pAYgTTMoKfson94C1ceVwwTfSgpbPciuP1U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qh+mVYHQzgVoP8nvMZ74L/v55dbPgju1d5X+g2lV68rMeWVP35kIAru59jm8b0P7RciRbg27QuKthTnvAIVa4FjbS9U9UwZ/Uva9sdtzRGa7ThX6SA83sYKDjYq2cqJY2bQ1t8DTM1lmpj7rczk560MDvGlnKjpUnmFHy6dMDqw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--bgeffon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=zKrZ7EJj; arc=none smtp.client-ip=209.85.160.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--bgeffon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zKrZ7EJj" Received: by mail-qt1-f201.google.com with SMTP id d75a77b69052e-46775c891d2so50402971cf.2 for ; Tue, 10 Dec 2024 13:31:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733866264; x=1734471064; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=p+Ew5mGOm8qX6hJiqcK96oIfktQ2zB7aJWIvrQrH8Us=; b=zKrZ7EJjCHzcD2CWQD9GlJWDU5BJqFwcd0CbBENW0tchiXxlhuuoJpTo2mQQ+TMFPd RTiMBUNU4+e98Xswxy4TLPvQXIKAAjf/9KZ8qNLIyBCA/tGfLs/oLvaXqI+dlYoLrnS4 xtJ1pBwq8AqjsVMjmVxqpkNK6bCNclIMp6tZuX8uF3G1d7K5sSGEGVxJjbIwvN5RBNG9 37x2cdIF0CpWRQGl07wzHRxZ2IdH/DD8HQdsvu2omRa0JILuAhiQHrtheJLd5PzrWNhk vzS5ALET417MeLqJp7AZffXOxW/GtmrZblziyqUz4WOlwSXDmuCF2e7+ho+6gK4yfiyG Rbpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733866264; x=1734471064; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=p+Ew5mGOm8qX6hJiqcK96oIfktQ2zB7aJWIvrQrH8Us=; b=V6brck1ydwqKjPwieRSeUJmo1hMPTqiLSMeU5kXgDrntcCWdtneRd/uuTgtiNUYG9M KRuvcL+Gf8BKq+gl+eGcAqgGDvSQwAMODAR6OpsQKSd4kMmalxAqRM7r0Ygc8cLMVevq U7qpK7UhRXN3hQBb4JLNpYJvrqM9ZFyLr1K9BaJs4jzLPRC84A8HazVseUfxcpRDvIdt 8TXaG9vxWIV9rfCDuQUymcnFo86JaPWPUHyPmILtou1Ol/PDaw9pIwkNI9RlVpqIM3/S N+4f5yz1F/EiTPqUPm8iELiUMEjLEW+rP2rE4YOiVnD37yUGAZVSYDuiQZOOfCDQSrfb dLfQ== X-Forwarded-Encrypted: i=1; AJvYcCVNMNSbGQ3UXO2LPMaaLLl+NevhSqSbGdKQpHVE+yYwoPIF3ZeUGMWxchs3DtPSUQ0ujFgLkDQxRu5Dlcg=@vger.kernel.org X-Gm-Message-State: AOJu0Yx9Ey4f3dB5Fbd2IUjJUOA/oumXrqZi+9qAGZ452753F80e4aD7 ISHmCdem/kmvPqyYxvvRlP341z5lLt/mO/xTlkh0teUMfL4psXnW3uDDB1rywl2YVswrSLzIMEI XBEQzyA== X-Google-Smtp-Source: AGHT+IHcROXBNbjH71/lURsLY2Z1G5CKon5Ljv9fq9orhUweiNnkkP9Uhr+rnjfYEZKZsBEOScmUDcRF+8Fv X-Received: from qtbcg3.prod.google.com ([2002:a05:622a:4083:b0:466:9f81:8c8c]) (user=bgeffon job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:1389:b0:467:7fbf:d115 with SMTP id d75a77b69052e-467892a43f3mr8418101cf.12.1733866263990; Tue, 10 Dec 2024 13:31:03 -0800 (PST) Date: Tue, 10 Dec 2024 16:30:50 -0500 In-Reply-To: <20241210213050.2839638-1-bgeffon@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241210213050.2839638-1-bgeffon@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241210213050.2839638-6-bgeffon@google.com> Subject: [RFC PATCH 5/5] selftests: mm: Add selftest for new_addr hint with MREMAP_MAYMOVE. From: Brian Geffon To: Andrew Morton Cc: Lorenzo Stoakes , Jann Horn , Vlastimil Babka , "Liam R. Howlett" , linux-mm@kvack.org, Marco Vanotti , linux-kernel@vger.kernel.org, Brian Geffon Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This selftest tries to validate that a hint will be used with MREMAP_MAYMOVE. Given it's just a hint we try to structure the test in a way where it will succeed. To ensure we can validate the behavior we create a single mapping and split it by unmapping the middle and we'll use that as a hint for MREMAP_MAYMOVE without using MREMAP_FIXED. Signed-off-by: Brian Geffon --- tools/testing/selftests/mm/mremap_test.c | 113 ++++++++++++++++++++++- 1 file changed, 112 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selft= ests/mm/mremap_test.c index 5a3a9bcba640..42ed869e2e01 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -270,6 +271,115 @@ static void mremap_expand_merge(FILE *maps_fp, unsign= ed long page_size) ksft_test_result_fail("%s\n", test_name); } =20 +/* + * This test validates that mremap(2) with MREMAP_MAYMOVE uses the new + * address as a hint. + */ +static void mremap_maymove_hint(FILE *maps_fp, unsigned long page_size) +{ + char *test_name =3D "mremap MAY_MOVE with hint"; + void *mapping_a, *mapping_b, *mapping_c, *remapped, *hint; + intptr_t base_map_addr =3D 0x8FF00000; + +#if !defined(__i386__) && !defined(__x86_64__) + /* + * This test is written with knowledge about the architecture specific be= havior of + * get_unmapped_area(). For that reason this specific test is only applic= able to x86. + */ + ksft_test_result_skip("%s\n", test_name); + return; +#endif + + /* + * To validate the behavior we'll use the following layout: + * + * | mapping a | | mapping b | mapping c | + * | 1 page | 10 pages unmapped | 2 pages mapped | 1 page mapped | + * + * To guarantee we can get this layout we'll do a single mmap and then + * munmap and mprotect accordingly, this will prevent the test from being + * flaky. + * + * We'll attempt to resize mapping b to 3 pages using MAYMOVE, because + * mapping c is beyond it it'll have to be moved. We will use mapping a + * as the hint to validate it lands just beyond it. The final result: + * + * | mapping a | | mapping b | | = mapping c | + * | 1 page | 2 pages unmapped | 3 pages mapped | 7 pages unampped | 1= page mapped | + * + */ + mapping_a =3D mmap((void*)base_map_addr, 14 * page_size, PROT_READ | PROT= _WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (mapping_a =3D=3D MAP_FAILED) { + ksft_print_msg("mmap failed: %s\n", strerror(errno)); + goto out_fail; + } + + mapping_b =3D (void*)((intptr_t)mapping_a + 11*page_size); + mapping_c =3D (void*)((intptr_t)mapping_a + 13*page_size); + + /* unmap the 10 pages after mapping a */ + munmap((void*)((intptr_t)(mapping_a) + page_size), 10*page_size); + + /* make mapping a and c PROT_NONE to complete the vma splitting */ + mprotect(mapping_a, page_size, PROT_NONE); + mprotect(mapping_c, page_size, PROT_NONE); + + /* + * Validate the split: mapping a, b, and c are mapped with a gap after 'a= '. + */ + if (!is_range_mapped(maps_fp, (unsigned long)mapping_a, + (unsigned long)(mapping_a + page_size))) { + ksft_print_msg("mapping 'a' was not mapped at %p\n", mapping_a); + goto out_fail; + } + + if (is_range_mapped(maps_fp, (unsigned long)mapping_a + page_size, + (unsigned long)mapping_a + 10*page_size)) { + ksft_print_msg("unmapped area after mapping 'a' not found\n"); + goto out_fail; + } + + if (!is_range_mapped(maps_fp, (unsigned long)mapping_b, + (unsigned long)(mapping_b + 2*page_size))) { + ksft_print_msg("mapping 'b' was not mapped at %p\n", mapping_b); + goto out_fail; + } + + if (!is_range_mapped(maps_fp, (unsigned long)mapping_c, + (unsigned long)(mapping_c + page_size))) { + ksft_print_msg("mapping 'c' was not mapped at %p\n", mapping_c); + goto out_fail; + } + + /* + * Now try to mremap mapping 'b' using a hint, it will be increased in si= ze + * so that the VMA must be moved. Bypass the glibc wrapper of mremap(2) b= ecuase + * it will attempt to 0 the hint unless MREMAP_DONTUNMAP is set, for refe= rence: + * https://sourceware.org/git/?p=3Dglibc.git;a=3Dcommitdiff;h=3D6c40cb0e9= f893d49dc7caee580a055de53562206;hp=3D54252394c25ddf0062e288d4a6ab7a885f8ae0= 09 + */ + hint =3D (void*)((intptr_t)mapping_a + 3*page_size); + remapped =3D (void*)syscall(SYS_mremap, mapping_b, 2*page_size, 3*page_si= ze, MREMAP_MAYMOVE, hint); + if ((intptr_t)remapped !=3D (intptr_t)hint) { + if (remapped =3D=3D MAP_FAILED) + ksft_print_msg("remap of 'b' failed %s\n", strerror(errno)); + else + ksft_print_msg("mapping 'b' was unexpectedly remapped from %p to %p. ex= pected: %p\n", + mapping_b, remapped, hint); + goto out_fail; + } + + munmap(mapping_a, page_size); + munmap(remapped, 3 * page_size); + munmap(mapping_c, page_size); + ksft_test_result_pass("%s\n", test_name); + return; + +out_fail: + ksft_test_result_fail("%s\n", test_name); +} + /* * Similar to mremap_expand_merge() except instead of removing the middle = page, * we remove the last then attempt to remap offset from the second page. T= his @@ -720,7 +830,7 @@ int main(int argc, char **argv) char *rand_addr; size_t rand_size; int num_expand_tests =3D 2; - int num_misc_tests =3D 2; + int num_misc_tests =3D 3; struct test test_cases[MAX_TEST] =3D {}; struct test perf_test_cases[MAX_PERF_TEST]; int page_size; @@ -842,6 +952,7 @@ int main(int argc, char **argv) =20 mremap_expand_merge(maps_fp, page_size); mremap_expand_merge_offset(maps_fp, page_size); + mremap_maymove_hint(maps_fp, page_size); =20 fclose(maps_fp); =20 --=20 2.47.0.338.g60cca15819-goog