From patchwork Tue Dec 10 21:30:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Geffon X-Patchwork-Id: 13902094 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D79A9E77180 for ; Tue, 10 Dec 2024 21:31:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 453278D0013; Tue, 10 Dec 2024 16:31:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 38E668D000B; Tue, 10 Dec 2024 16:31:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 22F318D0013; Tue, 10 Dec 2024 16:31:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id F345F8D000B for ; Tue, 10 Dec 2024 16:31:03 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A47CC42855 for ; Tue, 10 Dec 2024 21:31:03 +0000 (UTC) X-FDA: 82880344596.14.A71B1AD Received: from mail-vs1-f74.google.com (mail-vs1-f74.google.com [209.85.217.74]) by imf26.hostedemail.com (Postfix) with ESMTP id E55FC14001A for ; Tue, 10 Dec 2024 21:30:44 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=yRhB+hnP; spf=pass (imf26.hostedemail.com: domain of 3FLNYZwcKCB86B9AAJIBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--bgeffon.bounces.google.com designates 209.85.217.74 as permitted sender) smtp.mailfrom=3FLNYZwcKCB86B9AAJIBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--bgeffon.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733866244; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dgSSzFrkXg1CYSEsBlnHDnKWydapEER+JZ1i3v6REhA=; b=ghF7p29ibs1zNavzNLk3E6akfkCpYuI8ofIHBNFtFgtYRqie5Y9bjFSBtgLRSzkJbMspko 370EEiy0XeDM92LOjW1n0vbfMbqgkwvKGxBbbr+mJ8eMBWfsf4BNCkBxsew5beBrGOP8pT TH8LYohSSbvFhRGcVMHyDn47S8muS8U= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=yRhB+hnP; spf=pass (imf26.hostedemail.com: domain of 3FLNYZwcKCB86B9AAJIBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--bgeffon.bounces.google.com designates 209.85.217.74 as permitted sender) smtp.mailfrom=3FLNYZwcKCB86B9AAJIBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--bgeffon.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733866244; a=rsa-sha256; cv=none; b=qblcVAdj1VI0C8YqSdNbv47fRQ9tThxTZfAtvvLlkLtOqlfoDg1aQyWvNSsvN3PDGv7UKk X01miiIsExZxslw+knRgoIec1c0wzgDXdDSlKN5iEBMFgDqyCktewA0RLqOumGan0N/0CA X8p4pBGFdCRsBto1g1k4gPXRWc89Ek4= Received: by mail-vs1-f74.google.com with SMTP id ada2fe7eead31-4affbf5361eso523803137.0 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=kvack.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=yRhB+hnPd3lg8Pxg0/km9FycHZW3GTMjIt5F+APLX1xwqm10fXfGf5jlPuNsapP6Oc 3SAmmXxBYTj+Bxqtnr0ZpWU19TR5GKAEcgcVTBS1z4S5KK5v8pBvLGIIxG2c5bDlr3q/ FdlxtuCrJv3Ihi0THLu0edQYI5SX8CY6ePtJxYGlTAjDXnnjOfEknYq3PCpdgvkXSbqs RRS8CDVzp+fh0lsvqmbE7AKfPOgnTePNYaClh1oyim0OyTI8jRU4Vmg86+tsBn+UeGdT 5ymY6kAisIHyHvCU7ORA+HHRv84gTDEW8DDNx6LqSS/IfzMgY8tRy+trX8InwX1NUNDY FEKg== 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=j8VAjglfUH60eACRd6CuAK1NzIpc2z8eO7rXUTrKT+38w+q2RtFDE3Txgeqv3/uIE7 VWR2nevX5FIV8oRF7kNS1WTH+XTQ/xOui7u8GHeESOccehNp7PqkDajLLdBZTYNm28Oy LsMe3y7DBZdMBuQ1gWf8zgPGfUFDM974C2GKGdC2MJn/t4NjhSdLgfzGsGOTk55bJMYp QTGZYYU8C8ekVjUGOtbAn61qj5K4A2fEQ88SEdkmCMAooLzaYyFQEufARLFqSxi0e2hf PiliICmGbnn4PuP+oVyitcoLkdXXx6YMJswq0xNYrj2PawE997WZLK2KTdlB9d97r1db ccOA== X-Forwarded-Encrypted: i=1; AJvYcCUIQ6g3OGpU4Vd1CFaUXGXRwD9i0vzs5cW/ZSvocYIJstxQK23MbVIzWNVjo8M1pUe5Bax+WkviWQ==@kvack.org X-Gm-Message-State: AOJu0YxLAlb+KtGR7QM2+dFzMvDCMFrwzD02/OcOepxZEhqs7Z/nD2fy nqlmgnouqKvFsJNkM5bYmEa/LDe6nmhQmLqLn8KLZnvne6zkMVz4kr1Z525BJnV3+uWKu3xtJLR 3Le0Mdw== 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> 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 X-Rspamd-Queue-Id: E55FC14001A X-Rspamd-Server: rspam12 X-Stat-Signature: rb8gphnqbq43hxuxf5ei8yhtzbmzir1w X-Rspam-User: X-HE-Tag: 1733866244-928342 X-HE-Meta: U2FsdGVkX1/C5Zgs4sXAvNUqaiGVqGsg3WoTdIRLR4Lj0O0h3CIXArmEk5LCF8/WWtSPQ/VmA88bevNELhleYO3jTDdWNECJzzwy+z29WT8/3vUpEr/2ZD73/SYU4UXDLmQ2dGaTN81PiddccAqBouLit9jGMrP3m26TWqhBBEiJdYMUChBXqyndNug/LXxU4D1PlTcD4+zaGE9ok/4S9+I+y+fHLDFulz4gTPg91DQjR2UNjoyIy8jDRmjdFsgsPSQMp0Xz5NxlZaA6GoCkQUHrkC9iSoK1G0Oma/oAyzrXpVmrA3Oll5Z8/JXvX6+KnMIdD3952CIga9/xG0r7UHh+NOZslxbmmMktOt5ggHuoaEL22MsXJeXJTwb1nVDUJRUakqp62cxXdpTSMC3DR1pVSalLqk+6d0yKoG8rfBC/4gLLZtjeGljiUKYlLS444fBJ2Po/7AUA7wA5JL47i6JRlPUpmgGX07zkxsffTRJtM1C5UulydtOvgBKp4RHbWzarEolE6ybdVnNNxEo1NJXPapfqMbpP63PSwaMEIqjZZHEICKxfaaGeLzEyJ8qxGt0J8JKPPoVqvhdtI+aNpO8vHCZ46J257doQyu47CHy2M8uCtRzDD9Kf8jsqVwjs5n8Vuyghua9jER+Pdh+bph+7xvUeiF8tf66ufbiQbLMjPlqmRoNgkKe0FNZVV84cjdnxxtCo9JKDK7LTZFInR4UkH2DDgZqaPgOXoD0yzmCLrHWRWQo7nibiCAKX4tltv7j0vvfKhEXy87YygKIhlO1HLX1OFYmN1O3fu5zevSgTogz2PyhDKh3CVagBgYgoPLnL1WuXxKd5jynZ3Mob4NZu0YOkXunJm+NjgCB655qgGAXjgUQBrdVFTsIo24A/4um3sKi9lu8w6YNE56QpLy6r9yt9jkkj9wtfpFoKLdoMQzW2/s596Dh7zWqbn7UARng8lhDPAN6YPx8DFeh C3XqOdOf PRK+4gVFIA5UpIp3ghUdpiybOEHnHXWeJU2UKH+zPqNaXeDo9ZxXuGBTS4eFLGwGsnnR2wxPQpVo+gY8kQ5USAxUXcUVPBXhbs9EM3Tx5xIJhA28fwZNAps/d05R52jed8LOg/ZNpTYUvWqSm4ulHk98rBL+c/4pj9Mrgkowsac99NpOa1lPL/4hcY+a6w9vYXek4zDmM380Y70Imu+muD5yx1ktlXWWMhmKwDTqE8Z7s2qIfDQXMUlgH0ayAHnOAlzC+0bm6CPUtGnSdJO8jTg9arF1K2muH8dl1vv5N2SyxaFllnr2ASlM4CSlo/Nm6PehS4N8kghW+5DH+N+UJAXuDIksaRSDhSprD4Wg9jZZZGuZ8MoaRZoGwi8QtbLNdJvz0VLpb/CJCsNzq25UD2YRMSu2qhMqY3do7OQvhkUZ5sH6FhGF7lz5NMwJorEDQlaKuxScmoiMIM3rgsTcOjmp+lWY44gxiQuFZ4nVtmbASwFo3VCzayqcNxmHzxwCPXenmNL9XS0B5JBUfKU+yECwdyJgpMhPPdOVwA/YOzjhXaLcZimhi1AsiiVU98tUaIjjAIdjnBJZHSZkqnk/cZMpgK3HwINDEQX8F X-Bogosity: Ham, tests=bogofilter, spamicity=0.280704, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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, unsigned long old_len, unsigned long ret; unsigned long map_flags = 0; - 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, unsigned long old_len, return -ENOMEM; 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. From patchwork Tue Dec 10 21:30:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Geffon X-Patchwork-Id: 13902095 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2573FE7717F for ; Tue, 10 Dec 2024 21:31:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 515618D0014; Tue, 10 Dec 2024 16:31:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A0568D000B; Tue, 10 Dec 2024 16:31:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F2988D0014; Tue, 10 Dec 2024 16:31:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 0BE678D000B for ; Tue, 10 Dec 2024 16:31:05 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A7B21120EF1 for ; Tue, 10 Dec 2024 21:31:04 +0000 (UTC) X-FDA: 82880344302.15.B47D6C4 Received: from mail-qt1-f201.google.com (mail-qt1-f201.google.com [209.85.160.201]) by imf26.hostedemail.com (Postfix) with ESMTP id A5CE4140007 for ; Tue, 10 Dec 2024 21:30:45 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="BXS5c/pG"; spf=pass (imf26.hostedemail.com: domain of 3FbNYZwcKCCA7CABBKJCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--bgeffon.bounces.google.com designates 209.85.160.201 as permitted sender) smtp.mailfrom=3FbNYZwcKCCA7CABBKJCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--bgeffon.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733866247; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=G32JxFMabBUDQi+52by10jYMucvOr3O4hJ3hDDPHLcQ=; b=fcy6LCuyg7gwa+XtjXRMHHWA3uoPGxsid5Q32w5lvCr4XM94MkCrXP73m+fhrI9fX63ST5 Oyf37sR95lb+2A3z/mXhbbvCRMrtXcpgkr9pjp3U0byhJ7Y4ZCL9FCJjSl+gUFBvysuoTl Cor/pP+87KlQV2ykuHJOUQfCdI3Ytss= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733866247; a=rsa-sha256; cv=none; b=Zzy2V//TQOgM8QQbpXvV/TxfdV+54ep5kjhQJGH3hfP5m/by9aKGUf4xf41zn7gl1xIHkP vlTS0zxLX3oQPY/YyM1J+blZkWVxxAwtWg68ZeU1HpPUSI6nC6ywn12Pz2Esnv+diBV/Ec Y+CXJyQo/nKW/VH3A8EJ/lhofWher5k= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="BXS5c/pG"; spf=pass (imf26.hostedemail.com: domain of 3FbNYZwcKCCA7CABBKJCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--bgeffon.bounces.google.com designates 209.85.160.201 as permitted sender) smtp.mailfrom=3FbNYZwcKCCA7CABBKJCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--bgeffon.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-qt1-f201.google.com with SMTP id d75a77b69052e-46752894cb6so70963081cf.1 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=kvack.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=BXS5c/pGRyy2sFfBBQBywhWRnBEvDpHmQaSuWyxyU69R2s9rN+d8Yqh33ZuVh9fSKT ZoS+pJB+AbvAjMOWSvX2YSY5DKEJRsNwtYe939jVFwVL69sQ5OupSyOso2VrweAZr0yw dHawsqPr7cITSdMiLOSEhzu3a9J36QDmnm5fdWXFo8hUy34PR+oT944sUa4UiWJ/Jgzh SBO9HEQH0+cnKzVvOxStAabvkAeiAI4se3qIlAb28/+cijAQK84xTUfUoLjreE7mkiYs ObWJp8+j6OtnKEkI/TJU2CvI8kyOjF01h+bApZAzc/tXLTaf//84yA1Uv/7oz9HVPimo Xh6A== 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=ma8ArjCwPx+usT5jiH+6myhLWXltvBNlRZJd5DHgnsun8xpIcrFMyGy2niyjsP00nB KtbP5qIuhfpwRjA0BMB7OzePIXN1pXfzUW2ZhsdC9z5R0dJQ6c4j+IAiO0Ma9Vxq3iTc 9c7uSosxX5xvsItB4IJiChpVjVU5iF+EW9ChK1L48h5AOHbq573aYm4ldCiEjGnqRuwS GlA9dvpzxUfce21drZXL3LNGfXrKJZlII/jasKqGVtfmUbi/bRaIxrCwjzjvFL30NKrx XURGGpsjk9BG4hgRSRVuP5RFkP1goyTRkXrzR8iXCwax2YCX8LpnNcQCTf3T97vi3dwL akDQ== X-Forwarded-Encrypted: i=1; AJvYcCXwJfGTidr3mmtmWllhRcg5smxD6WBUEGaF9w8KvGmWQzKMt/mObssBxU9EPDzbJrMEYHFWowXtow==@kvack.org X-Gm-Message-State: AOJu0YyDLHVMRnTXncsphvsz7kffRw3OkY3myC6RE8hCn9BpAILV6YUk TmTXkv7WZqWS8E+FDGz2wN2iA9gRN0dWEZtoRag82S3BWurKXgDqd7wi/WT+CwbCnxD1ZYBkLR6 BOuDu2w== 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> 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 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: A5CE4140007 X-Stat-Signature: 8jgzd8bbn48crwq57crtei9rthfmh89q X-Rspam-User: X-HE-Tag: 1733866245-762033 X-HE-Meta: U2FsdGVkX18Aqn++MlUG3Ec0SZom5tbDPmzG72bKINTfzTxOVorDRP/1yggynPDeXTdu6fc9Ffb96PS+eHugIgIRYuHCTh9UEEOS4fcCx55Y9bRB84LlFwezVMv0ktpEuqBn8yxEdi9l3FwCDUoHnC74VuTCM1L1FsN/BbjazVKSLxvPGrlNcN7HVun6DrqxmbZ2ocncY1BPiOuuLXdNtBxGEICPDNYRyFPVOuBaWPK/DTM3McP61Hq1vpVWu82Zlmrcfat4TTaQUAVpR7KTW8oJR9R5dOsQ8uK6SDDqG99ZXadl2eZpqXshKizJa4udXu3B09z8vDgJtZCdlijsSw6X1jEreXwizLKrQyC/sOTm2RwaD8i75vNSuNZkfOA+EReqm6v1BU3dzX5BJBrLqQUeS31dpK9ckJiEfJIu+bsMpRS0hqBUAW4MzJZLjGivzzgrHt4XKrKqYbzDWkQ6a7UpNQPUYaghFPKa4wiJXYCI8FsXMvwU1CjtRy64u9t/IHjLEbplnKHIO5gg4p31e6AjA8ZMNm7aSZOF//D9k3AlJoP/reD7Fq/j1KQ1/SzbNEWY3ChdEsdDQ48DCFvOJFqAjmeK9ZEcuWoOyVcF3g7+im2LY9A5K6ix8xgktkXSotTb7hvwnVRnQGFz7xEOkqrgc/5S9p6uew28+QJokZf3S/cH5DqfXldkiIU3RLe9LSTBzxB+WnDRaK6ODbao011YifT1m5dPUlGitqPiah05uqioKvvhQjQtrofGtccbfzx21eWp5FDCDWg/IedVkDqjdvKUvCle+lkEFLLhkr1Ijn6HKt0W3PaN4suNRW91RZBsgohaZkSsd0Xl7lo4H4I7BuWZDudsnxSIWAV78cVDVhQT2o+DBzTMTRlJeuNewg1AngKkyzAIlnm4Li5t1h3RKFj3z77K+MHrnu5OpoKbE0NrnEGxkPorYub6Nh7EFoxNmZ4TiHf5bsrZXWy vS93o9hQ N0YLv8Kmkfos/i4FKTk7w4RSkrTtQ8Xlq7wu0d7j0tRUPxqMhjbxzRmyLJP8M9vlu4VphiKfaTBkWt7+YnVCuFsyfks7odsP8/nTIrQ2+sqivl+qev6Gi/c+PD5beVPkgH1mSpRjyynLIXVW4nedhDt6QHTqwYgW9lGEXPTk1pvmllfOCOLxTJ5gypm+lo6w+5dUoxMPiRFgKpchHkA/rZW8rFZGfkxpinlbq43K/5mtbQqM+iIL+F+0TspvGHfv97WDk3G9WCwsE/IWz4HnXfMvO4J3vZIw7R6cNV6wzvQRctuqXq/lpMA5xzNaaVZbrTpiuJZgm0OT1kII3kJguA/ifpeOJZ9ASaT2TsgCJndz85aqNznNJjXP8KE9yrKoSubSEp5vaTOY/nCPj7fizIoMcLpSvLqCxhSnBnVbhJGPCyPVX2g5pVMyhx+Sk+wL50UcR5UzjSrq7Ts+rW/NznA3NfPyPyK7XcQ/8rf/kd9FkYy116b0BYZKXrtfaVOsp8b2NFKKKIYDYOnqx0e4Vf7gg3XZ7Aq2w3wpBxDxfyIGsRHYDiyxXjpB/X7pndp/Q7RN2vhoKr+YVCgPhJCqfYJiERPSQ9b1OVrwl X-Bogosity: Ham, tests=bogofilter, spamicity=0.009608, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 /** * 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_struct *vma) return true; } +/* + * 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 &= PAGE_MASK; + if (((void *)hint != 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; } -/* - * 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 &= PAGE_MASK; - if (((void *)hint != 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; } + if (!(flags & MREMAP_FIXED)) + new_addr = round_hint_to_min(new_addr); + if (flags & (MREMAP_FIXED | MREMAP_DONTUNMAP)) { ret = mremap_to(addr, old_len, new_addr, new_len, &locked, flags, &uf, &uf_unmap_early, From patchwork Tue Dec 10 21:30:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Geffon X-Patchwork-Id: 13902096 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0D45E7717F for ; Tue, 10 Dec 2024 21:31:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED9968D0015; Tue, 10 Dec 2024 16:31:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E63498D000B; Tue, 10 Dec 2024 16:31:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C94018D0015; Tue, 10 Dec 2024 16:31:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9668D8D000B for ; Tue, 10 Dec 2024 16:31:05 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4E34BC0E9F for ; Tue, 10 Dec 2024 21:31:05 +0000 (UTC) X-FDA: 82880343882.03.2B965F1 Received: from mail-qk1-f202.google.com (mail-qk1-f202.google.com [209.85.222.202]) by imf07.hostedemail.com (Postfix) with ESMTP id 1632840007 for ; Tue, 10 Dec 2024 21:30:39 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="jmLYT/oA"; spf=pass (imf07.hostedemail.com: domain of 3FrNYZwcKCCE8DBCCLKDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--bgeffon.bounces.google.com designates 209.85.222.202 as permitted sender) smtp.mailfrom=3FrNYZwcKCCE8DBCCLKDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--bgeffon.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733866239; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=qLtQZxHfyZ3UG9LKxPNXk6qON+2equfYqBxEH3X/7vQ=; b=yMFHMHc9f04zfeJr3HOIdGgiA5hdedwXLx/01715DLF2yiQIfEdt27g+LcM3Ndv6z4dc+c ywOXZyTezdRdERq+zhlH9ePsL/FTBIj8PX2wpWgJag3rc1eYoUZGdYXpPTf6XE4t15jA1L CYmzwokHZJfBk0pJuld3YXHD6vQPuAY= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="jmLYT/oA"; spf=pass (imf07.hostedemail.com: domain of 3FrNYZwcKCCE8DBCCLKDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--bgeffon.bounces.google.com designates 209.85.222.202 as permitted sender) smtp.mailfrom=3FrNYZwcKCCE8DBCCLKDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--bgeffon.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733866239; a=rsa-sha256; cv=none; b=iXbGPjNa419npd0qoSOsv38Jig+JNtDjUbZ0O7jhjt/2IklvOdR3mt/DgLxRP49LCJ3a9x 8fo6MXwuFGqZNziqnlV/SBcPgQ0s/aK+YhKLPcvT01/1zVKNh6sv7DNRCD7BpkFMDHyiYY KYLVmYQUTijm866mu8id5966F7FIuRQ= Received: by mail-qk1-f202.google.com with SMTP id af79cd13be357-7b6e178d4f8so140873685a.3 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=kvack.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=jmLYT/oAeiNDk6WSHKhKTTeo4yeShsY01UvV1c8+lEyS7dWJhNwQUfP2ICniiT+vXi 5CWORhNdOPU3BJK9TLkZr3IO8pRq3VTF5PaLo6cSfxnu3gIOLCfkvJYSRwoxC7tduBDh BAoRIxBX6gdRLd3l5uGOp1SCyj3XXFHq2fla+dLpirXgtlp5TCwa7W2cnX3kJfCuE3AQ oG5gys4MbzH/Mj02S4NDKwSrIJglaW3X19ewNEGNs1QITebNhkdc2kWJ70sq9sCTiZRm 5wJvW8S3q8E/oABmORBQiKuVrCpFJvwW8KLgPgxzNRqtAgRvYrxgIVQGIp1Mzen6Yxrj tH6A== 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=hF4tydTfX4+3DIZL1EYlXKWz4T+D4Foc36qnNrTeHFBH4GN6t0UqY0gnpUTeDwE6Nn Uy0Mb+AHN+6QKk5mV3HTvq7I7RlyD0/2XSwIAE5NPYOIzxnMdWLAFXJa05PX7MEVxqHb iTUajLqP6oFpckzS7NF9t6bqDKgVRaePWKPJaqIJ0pZ/XxPoM4kkyOZnq9LTQIzNUqH4 O2/O9PJxCHwnmALByDRazCiw7hsu6VCQ3bAEHJp8DXvCzLzMmQyxAcp6UBOZVeVohzMZ /kk53t5CwnsOxaV7bZ5ZeQEKN4+MC38ZbYl5AVy3HhGttFkFF8HPNSqTZZ/81gxcBkVf J5yQ== X-Forwarded-Encrypted: i=1; AJvYcCUOkGmNXM0Z6WgEkKvfONc5WL/ktOHAZcSf0xn+WK+TyEX8ILXcqA6m/AAZtDX12vbkgB3P2etyjw==@kvack.org X-Gm-Message-State: AOJu0YwwBjzA3CUeaGXKU3S9mVFDEZePiCMbrsSfpL8mcFEnWdhCbKVv KDiggqR+btyqlXMv2P15GFShLtnSYeShinusGw3Mgwk26soBHcGWG9LUDNI6/DmuHRLJKBGdE9b KrnQ+9A== 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> 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 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 1632840007 X-Rspam-User: X-Stat-Signature: itsd9ru1rukamngrq6nbksu6kw1fnntp X-HE-Tag: 1733866239-559231 X-HE-Meta: U2FsdGVkX1/nkQBDvwjYqjV2fuFIOv75rIGKvpI09XOZebP1YUvYHu1M7ZWqBHq4NeSErQarcItGQAbPnqxnOOSPJSQ1OIppSL4fdwmoy3yoE7EVGau0tEY103IhdMRG2GmL20FKxr9j8pRn4Qa1AY65lCWeadfiAmlpaFHbdYIvMxNWfM89up7HTb2x3SWLCkfU2Igx7/lLwEy90qJM39EaZ4yf/RzPHI64LY5G9vchXCwgTmP39z5WloN1uKuJepU0Mhsq6aFSKI+F2ktJ9JXzeiZ9uzrvmX+xikbHCbrt9JA2JVofOxpfDWMVcyvVcBo8qFrRlJ0yczcF93SqgpFsc4xnavAQ0+65z1rs5wV2qW6RxMbFzg022Iw2QiuGyEd1qpyMpAwUbMhl3WGr6tWm/QE6UbJAw8WpZcFqu1e7+Xe6gFCjd7wxSCaReHi6IEKE4OWsPx6S8p6NIu+AQgOuYIlpNkgTZ+c8fvvdOYnD/Pdnc0sCtHXFZpNAkLvHx8msB0S37vkmQGXepnQitnOOdb8U5CE805/Q2GT3g6sUsa8vPDgwfP+7Dw/6vSqIPsL37HEWvtSCnxW0EhRyJIpEnFesjwaKq26u+3TGoinO5ZAM7u+pefKnIzr5LlJlAoj8xg7dZs9Zyljqkr0prE1H0tee+zjruykgbE1qB3YaybP4e7bnY6B5BWxuwWDk0+FtG428iGEgPdVI3o96XrvGvLk9N24IpKtA1tOjzIJp9Auy5OkYFgieUMwZ9js2NKaHaa1UUpbOn27BjXwWgPSFhjo7Gdvau0IUnXc/Qurjb6lh3vWS40GwC/KblSLg1QCcUAUiGaMkRWbFLnGCzTKB8UPDx3HeF1F6eSxAuidKs/D2VvMYEwJZelwc4tR5/nxH9IZ+jteUtJj6uKttPrYREtp2rkIBHMj4DHrzSffszOa14eY0SncVWI3HTjBmQk/W7ayRVelf5OUWHpA Gt284VO6 6OkGumaNsTXxFYBN7cg1zIip7BfYZhRSV/FRfjUrF3aLQY6azmF4+rGOWKZjb9G2lD1vN3ajFCY9uQpSnymWsYocs+wrJIoel1znOZyNTfWtf64J124Uk6hbZXj7Tzrc2ouK46Dt6Gcwrf/Ms4r5QGEjAUmNQANhVnsUEpoCzhLyizux4PMynTkWFMXn8Km4fXqSYZX6ThkZa5ujh0onSvztUmA2mPdGI8Sx68rIkQNIII7nSpyaQ51ECGliNqSgkN+ri0YTofwvO6b+/pIn1qh9eUwvGYtdupfR2oJ4dkRfTj+wCtHtix05JTVMvwAtHB6GYD+NcRExZmgTnDe3+hmmR74fpCsPNjLC7RuJF9H6MzzLDrzBdRzVhCgvF811ws7vwZbKPAqOGWTR8Ldsz8ZCJXKYqebnbMbiJ1F7XvnUKFLXGCy+eQ3UaCi44GjTQhRE9O+WWySuYuobqZFei3vBzGnD9eEB9VKYcQ378JGiUZ9s6+v939rpFTjhTBiyI2mdLkDF9zCJI9u3AwDhpfDvWeNvdG78yzMDK3egMuWLVZ86wB+u+vacRQghTACIb7B/LtPmb+5IUOh5ZQi33f8yvrVEIz4yeHgQm X-Bogosity: Ham, tests=bogofilter, spamicity=0.255066, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 |= MAP_SHARED; - new_addr = get_unmapped_area(vma->vm_file, 0, new_len, + new_addr = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT), map_flags); From patchwork Tue Dec 10 21:30:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Geffon X-Patchwork-Id: 13902097 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BE3EBE7717F for ; Tue, 10 Dec 2024 21:31:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9F9518D0016; Tue, 10 Dec 2024 16:31:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 981C18D000B; Tue, 10 Dec 2024 16:31:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 786D18D0016; Tue, 10 Dec 2024 16:31:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5516A8D000B for ; Tue, 10 Dec 2024 16:31:06 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id F0F61140B45 for ; Tue, 10 Dec 2024 21:31:05 +0000 (UTC) X-FDA: 82880344260.12.EE54E31 Received: from mail-qt1-f201.google.com (mail-qt1-f201.google.com [209.85.160.201]) by imf08.hostedemail.com (Postfix) with ESMTP id 4FB9F160006 for ; Tue, 10 Dec 2024 21:30:49 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=O3ZaiP7A; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 3F7NYZwcKCCI9ECDDMLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--bgeffon.bounces.google.com designates 209.85.160.201 as permitted sender) smtp.mailfrom=3F7NYZwcKCCI9ECDDMLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--bgeffon.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733866254; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8zF6cyo4V+yV1OddWsDU7llWFhEXM57i4QvxWba9u5g=; b=AHT5xvY7w9zpRg3haw0I55yKDkK8+F/SSOT5JL37NS3LUQZ8MIoc0aE1xYjhZHWhWyyoXR DDSQCVnRCSZP3P5HlUv/gqfs11HidNBWDB6kLPGdjgRjLda8ci4lhk1ZhV4aAzWMEpNDCX tsPrdhMrnf8sN/4G+eX9L0bJwY635zA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733866254; a=rsa-sha256; cv=none; b=hCn+gdEJL1q3iVL4gIgfNAd9ymPtoOmnnYOuprta2hBMUWFObrdzq5FRJ67ztmutFoFzo4 NeTB7PBbP2fCwta7PfPDPXDlDU59oGV2Jg9KgB12bj3RtHhHYLfVHT7j9i+Q2n+fjwMleP armrtrlVvQPasXmWd28NZa0kbH5LZFc= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=O3ZaiP7A; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 3F7NYZwcKCCI9ECDDMLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--bgeffon.bounces.google.com designates 209.85.160.201 as permitted sender) smtp.mailfrom=3F7NYZwcKCCI9ECDDMLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--bgeffon.bounces.google.com Received: by mail-qt1-f201.google.com with SMTP id d75a77b69052e-4677666bb00so30268331cf.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=1733866263; x=1734471063; darn=kvack.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=O3ZaiP7AmxooQTf1d3jmy3QWK+xFonbofUuIb1JHDF6FxTL24tP0MxuOevro4tu3+/ JUmJwX9kzTTjT1SVv/jioo0sMGpi9AUirCWopDTtQf61jBk/jfTU6h1rWIAK0D0zX5ww /6YPvcaj/cup5xoezx1ov4VOWA72Jf3D+eMNh9cl5ZY8YrjvWNXYjwYusQ+2mKgM4UIV oUUho8UynajPsx1wJRWBR0binAPfh+Ap+hMZLH5fopp2llSUVGMHziaEyhO5j1QK9f8x YwHRCRfZIUp/xHfQnFF9m0Tce8s0H1J+GOYRWjsH0ek8VktgYpwgukliLTaOzKHfCp+8 4P2A== 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=bFxJqGv+I6H384S57mD9bmvUafHkKftiglm6uFBBZ13C+z33HC6GnJ1eX8ccuaj55A QyeI4rvKPWqZ9QRKmjyncIyCX/7XKZQ/U7ngOx5KLXVrEsRxM2+TUq2AQ8QdR87EDa30 +1Oci2+GuxcyboE6vAcrXdUnFJLKL/TO3oLb/YzpzA0pVbhikrmpJsUHf12bgIHX3POr Lz5lHH7J3Gl8GYSyN8Jk6tlZn3V/VdKRrSgus/IFiFIlC84FMlU3ONuVmP/iRJdoESbO fJL8Pf5BDlgLmwM7ZXAsiL6uWL07NHKBpIoOPSB7Y+q8+tFVSFF74jR4gw77EsC9gkjk VuRw== X-Forwarded-Encrypted: i=1; AJvYcCVvSpqrKvIaWJ7b9/5tf9PcqPQ+IPZQjVq5fGaLvQfYv7FFHkS4n5gtec5n+S5MSp0lAYCjwAN5TA==@kvack.org X-Gm-Message-State: AOJu0YzIQwtlmFArylxR/pBe48TLaZgxtLqs3Z0IajSg0b3r9NEgnNwF 5inzzXst+zdaABdv3Q390dVtw3y3os/di7vgmVrA6ypZGUwfxJEJ9Z52zU7UzG9kTbcGdjAVoPI nD3nH1g== 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> 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 X-Stat-Signature: b69u4md86z76kcenkooeah43oyaky4hp X-Rspamd-Queue-Id: 4FB9F160006 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1733866249-813911 X-HE-Meta: U2FsdGVkX1+3V+p/quPtv+kFKSpDI7DGZZkBaOgQtHENE1tyX7bjAkvT96CMTqEkZmduxQslVfmayXw/OpS4GIEhVfjX6/I4CQTj/Abx9MvFBT6GJLsUTAddnOzc7f3rXKHaPOmjPqXqOOnZ0HMkDuH1hwHz4hNW6D16Ydqra8s7pBZkwVBC1cvBNa+R0l3ZTwI7GLNN5MEgSL+e3zFOIwGee6AcdQP4B11NNv3rR1RMdA6MV3cMWd2BFIPMPCpgOv+DoPflLvTXzzmC89unZle7mgSgPV8NUwdXUbvKuXYBqG9ef8YUnyvwhFQuzklNgUEvwsfFf1qgQhZkNNYETb/pgZHaTdCjyQZUQxxEcjAiz/iMUHWdjgNDzdhLrZZkxquDWwDW2vfwIMy1RPQkSXLv8kFq8EFFlnRC5hh+c4znQP9lqAFLrURmnbZVKA/DWe3m95WWZ4pe3J4+LoA638wH88mjKtK7FcDfd/NBDfXtVRtLktTSxllaTzcJJXu5wY2I7hEuhq56Df3Gw1rWtugonGA1oXaycCIv6rKv4pd4OYGu9nFuOunvm9wzcDxDe1SZ/rr0mhJn77HjbI0KKM85W5wsTIMm4dE8ZuKuK05a8VM/9W/JsSEMFobksuGjb/351M/GS1RoZyUGVXIhsuqXYgp4vdtN0ce+KFpVOYn05Kg7lqRQCVeb//gnfmSIqWk+nw/rbWl8osH7p/GiVWZ8A/8BhWPKDYbok5pKgY54Ppk0UqA3FDJtoJzYhkoh/ffak8sZllTsj7OAQC6C/AJg8Zbkj571gopNHZgHpaXa2pxWb2okk4TYXR6utqAuO7msUK/5lvZb6msLI9ES3A8bLGgMR5UpOMRXm7OeYa3z7upGb3VAqoFmQ3eqhmkytKExZ6j+smXg+9LOFmHHqZoAUVmm/YcKDgL5X3lTYjJQhk+TqFjUjSNed73H/ErpnBFFOW81T9bTWlXefJx SiamfnGN oUzgoqeraUGI/z8iGdnkoTi2o3yArA9Ktr67/3Gir1Uo+ckIYISrqd/qwqFDqLXjK4zsXdrtuL4Qe/XGMNzy0jNU+JISQYG1Llj68mv4mOLtye1yP+8NBOlejZEH0qh+XYYlc2g4Uf7LmMyhPqWttul6vMLVX/AfJrabFp+mwKcn+DaqD+W6Gyi8jMY0aYuKB5dC+6rk3IHVh22LAYY+4fUXKpg9+tFUkWVQCMAym5uIgIasrtf1j7tP6CikhVbEe9mdoN49OWX1T/bSg8qMTNrngZoqufT/hazK7QqwCpULdtaWS2KExoJZQigI/OYwBdB6J916foofWuzvxHKrKQBQ7Mr8Fb0/UiPpumpM16gsU8L5nKJ/ml7KgoMkOR/LqqFRX4O3lXS51YeijWD2VzMgf1bQzXujpt8j063QxcNlF5ojncGSlvm/Pltxmo8qkRbSiRnT8okZeFfI1gkpD3pFrSUyuwadCi1T72HbuWP2CGOs5UdOuLYloxscqgfbrdPKTZXs8aBjYFWsC9u+VFfmI+tmK2zoXEMo98JMkiefu6Afezv9tlYbiVC6k4EslxgMyGeklRBRIPVSb6b7kFY9asQGkFd9oj7cP X-Bogosity: Ham, tests=bogofilter, spamicity=0.216112, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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__); } +// This test validates MREMAP_DONTUNMAP using a newaddr hint without +// MREMAP_FIXED. +static void mremap_dontunmap_simple_newaddr_hint() +{ + unsigned long num_pages = 5; + + // This dest hint is intentionally not aligned. + void *new_addr_hint = (void*)0x999900010; + + void *source_mapping = + mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + BUG_ON(source_mapping == MAP_FAILED, "mmap"); + memset(source_mapping, 'a', num_pages * page_size); + + void *remapped_mapping = + mremap(source_mapping, num_pages * page_size, num_pages * page_size, + MREMAP_DONTUNMAP | MREMAP_MAYMOVE, + new_addr_hint); + BUG_ON(remapped_mapping == 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) != 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') != 0, + "dest should have remapped content"); + + BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, + "unable to unmap source mapping"); + BUG_ON(munmap(remapped_mapping, num_pages * page_size) == -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(); } - ksft_set_plan(5); + ksft_set_plan(6); // Keep a page sized buffer around for when we need it. page_buffer = @@ -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(); From patchwork Tue Dec 10 21:30:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Geffon X-Patchwork-Id: 13902098 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 320A0E77180 for ; Tue, 10 Dec 2024 21:31:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8C0898D0017; Tue, 10 Dec 2024 16:31:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 84B8A8D000B; Tue, 10 Dec 2024 16:31:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6006C8D0017; Tue, 10 Dec 2024 16:31:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 3C7A08D000B for ; Tue, 10 Dec 2024 16:31:07 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E106E42868 for ; Tue, 10 Dec 2024 21:31:06 +0000 (UTC) X-FDA: 82880344008.27.0D12574 Received: from mail-qt1-f201.google.com (mail-qt1-f201.google.com [209.85.160.201]) by imf10.hostedemail.com (Postfix) with ESMTP id 2D8F1C000C for ; Tue, 10 Dec 2024 21:30:55 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mH4u4kST; spf=pass (imf10.hostedemail.com: domain of 3F7NYZwcKCCI9ECDDMLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--bgeffon.bounces.google.com designates 209.85.160.201 as permitted sender) smtp.mailfrom=3F7NYZwcKCCI9ECDDMLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--bgeffon.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733866254; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=p+Ew5mGOm8qX6hJiqcK96oIfktQ2zB7aJWIvrQrH8Us=; b=AbWe/9YMTQzB8eNAi31eMXY5snKJAAWes4vLRCtR6VmUXDdJuigdq2dvg1nwjzTRWKBzTq QWABkbw+rXsdWUzuYdgCBWG7KstqEQgxAVTS+zz6f/yR9U6J8PDXcAAig3jQYesh9Farps 48j3QlzOUF7lPp5raYDaRd8DbyXsLkg= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mH4u4kST; spf=pass (imf10.hostedemail.com: domain of 3F7NYZwcKCCI9ECDDMLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--bgeffon.bounces.google.com designates 209.85.160.201 as permitted sender) smtp.mailfrom=3F7NYZwcKCCI9ECDDMLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--bgeffon.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733866254; a=rsa-sha256; cv=none; b=jI1c/ApvHAV6cwzg4eFXNk681BHS8ZCHdfktE319dikqB0Drbk+ZeB7w5bK0PYM+77gwNs P9Es81Ic9eDnjgOC2P7uSb6GJuDtApWikoOVPT573Y7IfHibhA0rV3L9P/uBUsB8olFGFe XH9wtqxbBh03ePvYJHQSZXp6Hb9lHUI= Received: by mail-qt1-f201.google.com with SMTP id d75a77b69052e-466bc740022so64238151cf.0 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=kvack.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=mH4u4kSTCo4kFXFlMuvSrq5Oz+cESU6Tp9Czn5l4S6h9IyTbfv8II7JXuaPh3FZjHE f7XCDNYZJHNiPRDX8/dF9aNo7zysjhUeO/QcY/RGx/vSKeNh+M3ZHnszXEQZkevCGrYd twAXucw8TBdrtCX24YBPYPLElhqMipFaVkACiGoHPgs8amcjXLRULPWBOLJtA2QeObQX d8/aUuwFKhAmaNA7LBT4fqz9FTN7rAe1yVlrQp/ErbqwYU7N46Gml5yjyXpqKkVa0y+z mZYL35o6Jpx6aTx6JrWdDBvaD56ff0NCRoNJ4sY2Y75CfQPQVQXWERiHzdHF7fAeRjVD srEQ== 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=PIEE7o8i8ZtQKAOS0kRrbsaLzAAhXFm6KRV0xB+FszlwXAbsM1fnkJLk0cBE3L9bde xjODR5dvh29gKNz/QaO/O7WkE4wP7fp8pobvNsrG9hIPaG93BnXni05Fq21MxOqnSF4g fIJ+AVYYV4ljkecEzYeE/mwUOwP+4OezGH3YOme87EOxsvo1I4B1mf1f1Uh01lz9buxz 6ayCWCn+o/xzGkf6gKrQ5qxLRAX2DqvJCWi5bCMTICm3IAkgdNYRj8CXKDBygKkYRhc1 3UV+xPFjRCA3Qx0ii4gy+Lr1WQu10ke1Q+RzJwIVmNhyW06EfLMvVPoM1jS3S4ohS00n +pUg== X-Forwarded-Encrypted: i=1; AJvYcCVBN7w9fmOVkqmimfQqUipW0bViUpYWLcwWOmEVBpu6ofgSaPln1n34K4de1on1teNr4FKnGPem/g==@kvack.org X-Gm-Message-State: AOJu0YzWuRBk0Gl+sH2vhCyvlNU01nNs9Uoi0N16AiVod/Pid+K2pX0n 97qkLYh6xiiuwZmIHpqi8kc6CFP6Cug2lmzg+Kkx4EDBUK3VNoE6y3UEDv917FGdwK3MBdHHTBT BHto4LQ== 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> 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 X-Rspamd-Queue-Id: 2D8F1C000C X-Stat-Signature: 484y6s1sid9a5c4bp9nua4sj4whg1at8 X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1733866254-398174 X-HE-Meta: U2FsdGVkX19t9OY3nBe2Sz5ZGD5VC/p7N0w8hlKTcUVhIIqe7zJC6cOsD2AdElCKQbSaSgvko1wuR37s3Be0yhDlytT2CSEHBupouEKKSpekMUFYJ6CrFajLOFKoWNjkzO5gZ380Fokv4hbJiEBSLyit6+ogt4uccrCR+PXC9sbjAutSqcW+CzuAq9bpSl92JaikQCEChruIh/gS1mwD0UZS1Uo+oYXn1GoXXF0EbUH0oMqGTty4jlbZcqG/SqllCILE+SCx7OstwqwAdpTzDsrTvcof81Fr2MPO7P6bXjJ7nc9/fRohAU5SZFlq6Wnz5YEc3xglWSdjoHZzH9oM/xm4HJ6ud2Q4xgDjKV6o0TfiWIcV2+IJLcrsP4e/XtcHDRHRRiWB1bzAZrP3JFUp2f2nEc5WXZroN/14dXY2xxORVcD1T+ob7IxacKyzFjcM51eCISDuCnk+eVqPbe4TsHunwJg/J+U8L0tzpWmS35FryWeA68xzLLYlN2xveK9yv307sUV/ieV/VRvOAZOimT36BzFFnwqvU4LAFZoKuy06kOzXA7cvUerhKJXkKHwJetr6P8jOl5roy15rE6jboa1PNE8cjyFsVW69/6kEgBEasTY4zxLBg3HQREYG9hN3SYTBd93/Y0oEkAfbrL6b3U/TzfkqdWs1gfaMFR69fxMY6pz75dY2qcPxYruCAoP6i7uMyu6B5Ep7qw74m/9c5AHyMHuI1dYpd9+PWlrqeuczrmmDZcN7MBWhQ3Ht9ZcXIqSvJ07QBF9hdpGQKQCJYuyumN0hCMSH1NcWUwWMQ1vR2QcpzAwEk3lSvwg4rt89tPU1eS5O0S51yxA4OMxWLh9d8FG6n64Kc3xAmDp9yk15JZz77AUCQjoCIqH05X9feQej5MEfpra3zL+TUB6hO4GVVVmDtI7YBxTn19/1+WjAfuN85AeWB7jPd18CHuyaZcmhux2+EQG273J32He 0tyeIm1b RVglXl8YmL0cgFGh40PmJtTIyMyuEVT3vUZ5+pwzOJc10c3EHLhR5STs+kkzcLRfwA5E0KOHEjtEpP78viM2a+ml8hmps5l/PIVAARgR8rR2Im/oMcvibBJiCLytxXR7gNRJQn6SRJAWaf8CkC/gsv3ojfe1GeTA0Vz0BROee6Ej4Nm+f8Niy31bHOkji+hmWX/vbkDurIeRXvQNFoeoH//RHmTwZ4vIrlNVcejh9Wd721ZgE/Lfw8vtSgENYj7kQFPPP8H0baK3tPjG8JcPwm/3A4uTn7m59r+B5bV6PjYT9BRFQpuLv+Yc6CmfXEc9E5LSJDSfuMo8lOEr2iwdPuBwYRokq6yJrfwOQcB78av5SkmsE22QENK2SkfhfGJ95ESTSkr+iokkdweputxFtlu9vCe+JqIBhEJEeWf6v0Lr2WuDAuAC/2vVVJG1HMDdD67hyyYDV+FhyB6QkvAcyVcYmPXasO8l2JzPUjNWPwocPS44Wv6QoA05SCCR3KajPPSXFIT8BVOyeDaUVhPW5sVuZs2DI84Na1Aa9ubnurKSSM+FTGHVea2Tk4B+T0QqX8BB/A0R/VN54kKPJlaMdUExaEgVPlH5lqcIhVRJqjEFB42QFfSo7Tpby/ZN5bk2a3ad6z1T0tIqyQzZFWxAC4ETaMw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.020596, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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/selftests/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, unsigned long page_size) ksft_test_result_fail("%s\n", test_name); } +/* + * 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 = "mremap MAY_MOVE with hint"; + void *mapping_a, *mapping_b, *mapping_c, *remapped, *hint; + intptr_t base_map_addr = 0x8FF00000; + +#if !defined(__i386__) && !defined(__x86_64__) + /* + * This test is written with knowledge about the architecture specific behavior of + * get_unmapped_area(). For that reason this specific test is only applicable 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 = mmap((void*)base_map_addr, 14 * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (mapping_a == MAP_FAILED) { + ksft_print_msg("mmap failed: %s\n", strerror(errno)); + goto out_fail; + } + + mapping_b = (void*)((intptr_t)mapping_a + 11*page_size); + mapping_c = (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 size + * so that the VMA must be moved. Bypass the glibc wrapper of mremap(2) becuase + * it will attempt to 0 the hint unless MREMAP_DONTUNMAP is set, for reference: + * https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=6c40cb0e9f893d49dc7caee580a055de53562206;hp=54252394c25ddf0062e288d4a6ab7a885f8ae009 + */ + hint = (void*)((intptr_t)mapping_a + 3*page_size); + remapped = (void*)syscall(SYS_mremap, mapping_b, 2*page_size, 3*page_size, MREMAP_MAYMOVE, hint); + if ((intptr_t)remapped != (intptr_t)hint) { + if (remapped == 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. expected: %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. This @@ -720,7 +830,7 @@ int main(int argc, char **argv) char *rand_addr; size_t rand_size; int num_expand_tests = 2; - int num_misc_tests = 2; + int num_misc_tests = 3; struct test test_cases[MAX_TEST] = {}; struct test perf_test_cases[MAX_PERF_TEST]; int page_size; @@ -842,6 +952,7 @@ int main(int argc, char **argv) mremap_expand_merge(maps_fp, page_size); mremap_expand_merge_offset(maps_fp, page_size); + mremap_maymove_hint(maps_fp, page_size); fclose(maps_fp);