From patchwork Fri Aug 16 11:13:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bert Karwatzki X-Patchwork-Id: 13766008 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 79951C3DA4A for ; Fri, 16 Aug 2024 11:14:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3AED26B016E; Fri, 16 Aug 2024 07:14:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 122F06B016D; Fri, 16 Aug 2024 07:14:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D517A6B016F; Fri, 16 Aug 2024 07:14:41 -0400 (EDT) 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 A3EF76B016E for ; Fri, 16 Aug 2024 07:14:41 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 58630161AE3 for ; Fri, 16 Aug 2024 11:14:41 +0000 (UTC) X-FDA: 82457850762.18.99326C5 Received: from mout.web.de (mout.web.de [212.227.17.12]) by imf04.hostedemail.com (Postfix) with ESMTP id 49DBC40011 for ; Fri, 16 Aug 2024 11:14:39 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b="TuCpX/N8"; spf=pass (imf04.hostedemail.com: domain of spasswolf@web.de designates 212.227.17.12 as permitted sender) smtp.mailfrom=spasswolf@web.de; dmarc=pass (policy=quarantine) header.from=web.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723806842; a=rsa-sha256; cv=none; b=bfx4LZ1IE4LeuYVPqiPYCz880KSKTT4HTF4gGvPlpOivsNxBozng2qBRhhbwuKnfYcW7bI ka6OexBlNJSSslWoqxnUbOp+sFl+kJubI/HeIQV+wxpoYtiIBtA+8DZNc/vGqUvd7gA3e0 vd68ZQtVPkS5o/z2HmhhYmey2G+5r50= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b="TuCpX/N8"; spf=pass (imf04.hostedemail.com: domain of spasswolf@web.de designates 212.227.17.12 as permitted sender) smtp.mailfrom=spasswolf@web.de; dmarc=pass (policy=quarantine) header.from=web.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723806842; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KqLpCASAg0v6fyS+T7RSQ7Led3ug2Wk1EwA5uf3qlqs=; b=Yh4qjLoYZFy0nSN6nLegxpnmK7wlie5q5P05tCAw54X7tXr0FDeH7TzeID0YZSr0pAQCeP y1YMlZ0U8Dtb/Vc48S4YscCBpWz5AV9oUVCt5PJTMkIo3j9yXERHiXdNp4eV5YqZlX1eMy sbq/spJ9MtcogNABfricdnMYszvybhs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1723806872; x=1724411672; i=spasswolf@web.de; bh=KqLpCASAg0v6fyS+T7RSQ7Led3ug2Wk1EwA5uf3qlqs=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-ID:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=TuCpX/N8xsW0eKxPxeG6dJ6Y77L1OkDJFkJcUm8KItesKAZyR52AGlQEUMq5q6ys WXn7S2kGA6f8cFjk55BAjLFmHLJL1fBjRb6FMBUFDKPCOWqoGWP9ydRgzmx6dE1wG XiPtV/FFY7ha0urhLfQB3DoWxo3X9xvkb+jKLaCEaAq6iRHSRM9OuH3AIs6DdCsvX caCa6KOjN9N/VQhKMQ9ob7Qbnyx6LaiTb9bJJAtYvObM+9Z2d8hA4Rn8j63ZvFNOF W1lVB6TV5pTxL4rdwuBpzrMH6ccQzjwx982zxV+mkHPsSPtNEuRJo0Vl6DdquKywV 0U02jpPCuD23d3SXXQ== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from localhost.localdomain ([84.119.92.193]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1N4NDQ-1sD2R73bQU-00xraG; Fri, 16 Aug 2024 13:14:31 +0200 From: Bert Karwatzki To: "Liam R . Howlett" Cc: Bert Karwatzki , Suren Baghdasaryan , Vlastimil Babka , Lorenzo Stoakes , Matthew Wilcox , sidhartha.kumar@oracle.com, "Paul E . McKenney" , Jiri Olsa , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Kees Cook , Jeff Xu , "Liam R . Howlett" , Lorenzo Stoakes Subject: [PATCH v5.1 11/19] mm/mmap: Reposition vma iterator in mmap_region() Date: Fri, 16 Aug 2024 13:13:52 +0200 Message-ID: <20240816111405.11793-12-spasswolf@web.de> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240816111405.11793-1-spasswolf@web.de> References: <20240816111405.11793-1-spasswolf@web.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:zz36G1dDzlpCZzGc4BqVfsIpgRKxbbe/K3KeQh0rlbU0Mr8YLuw KRmPeUju2SCl0xjMdC8oTSGzJLI4AyuQGa8nWVnr0TzHc7rQilOpyIbb7eeWbZiCfZPOlzY C0TXbu6GIHsqcTL3ZWoDPfsZLK1d9HLVwKlEFTn+qBGbvfFUdDGUTLHUVM11TQ5bvnqPaML my6q7cvd82jmcerhF5BMQ== UI-OutboundReport: notjunk:1;M01:P0:fpY6WE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hOJU4jAgOsPp2T49uk2nom8NLuiE13TdZggq2/YkGvWW2qSuaW5EUun29+X9AXTE5q8vjJxVoru0Nwdm3WjzLbw8rkAtk09yHTSUtX7GlWDtW/EpwMBW4UnMsffp6BJ7/b3UQcywNuoMbrl6l7+D0k/aB7hIuL X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Instead of moving (or leaving) the vma iterator pointing at the previous vma, leave it pointing at the insert location. Pointing the vma iterator at the insert location allows for a cleaner walk of the vma tree for MAP_FIXED and the no expansion cases. The vma_prev() call in the case of merging the previous vma is equivalent to vma_iter_prev_range(), since the vma iterator will be pointing to the location just before the previous vma. Signed-off-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes --- mm/mmap.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) -- 2.45.2 diff --git a/mm/mmap.c b/mm/mmap.c index 16fd14b243f9..6720b55b47ed 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1404,21 +1404,22 @@ unsigned long mmap_region(struct file *file, unsigned long addr, mas_init(&mas_detach, &mt_detach, /* addr = */ 0); /* Prepare to unmap any existing mapping in the area */ if (vms_gather_munmap_vmas(&vms, &mas_detach)) - return -ENOMEM; + goto gather_failed; /* Remove any existing mappings from the vma tree */ if (vma_iter_clear_gfp(&vmi, addr, end, GFP_KERNEL)) - return -ENOMEM; + goto clear_tree_failed; /* Unmap any existing mapping in the area */ vms_complete_munmap_vmas(&vms, &mas_detach); next = vms.next; prev = vms.prev; - vma_prev(&vmi); vma = NULL; } else { next = vma_next(&vmi); prev = vma_prev(&vmi); + if (prev) + vma_iter_next_range(&vmi); } /* @@ -1431,11 +1432,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vm_flags |= VM_ACCOUNT; } - if (vm_flags & VM_SPECIAL) { - if (prev) - vma_iter_next_range(&vmi); + if (vm_flags & VM_SPECIAL) goto cannot_expand; - } /* Attempt to expand an old mapping */ /* Check next */ @@ -1456,19 +1454,21 @@ unsigned long mmap_region(struct file *file, unsigned long addr, merge_start = prev->vm_start; vma = prev; vm_pgoff = prev->vm_pgoff; - } else if (prev) { - vma_iter_next_range(&vmi); + vma_prev(&vmi); /* Equivalent to going to the previous range */ } - /* Actually expand, if possible */ - if (vma && - !vma_expand(&vmi, vma, merge_start, merge_end, vm_pgoff, next)) { - khugepaged_enter_vma(vma, vm_flags); - goto expanded; + if (vma) { + /* Actually expand, if possible */ + if (!vma_expand(&vmi, vma, merge_start, merge_end, vm_pgoff, next)) { + khugepaged_enter_vma(vma, vm_flags); + goto expanded; + } + + /* If the expand fails, then reposition the vma iterator */ + if (unlikely(vma == prev)) + vma_iter_set(&vmi, addr); } - if (vma == prev) - vma_iter_set(&vmi, addr); cannot_expand: /* @@ -1629,6 +1629,12 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vm_unacct_memory(charged); validate_mm(mm); return error; + +clear_tree_failed: + abort_munmap_vmas(&mas_detach); +gather_failed: + validate_mm(mm); + return -ENOMEM; } static int __vm_munmap(unsigned long start, size_t len, bool unlock)