From patchwork Mon Nov 11 01:23:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Yang X-Patchwork-Id: 13870131 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 3FDA8D12D5C for ; Mon, 11 Nov 2024 01:24:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9FF446B007B; Sun, 10 Nov 2024 20:24:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9887D6B0082; Sun, 10 Nov 2024 20:24:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8021C6B0083; Sun, 10 Nov 2024 20:24:09 -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 612E26B007B for ; Sun, 10 Nov 2024 20:24:09 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0999441550 for ; Mon, 11 Nov 2024 01:24:09 +0000 (UTC) X-FDA: 82772067378.18.1F59016 Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) by imf27.hostedemail.com (Postfix) with ESMTP id 882D54000A for ; Mon, 11 Nov 2024 01:23:26 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FD0+sF9a; spf=pass (imf27.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.208.52 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731288016; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references:dkim-signature; bh=RD8CuXH0sDsPA4cZucuLgP8G7WCBgwpKF9OHtKeps3A=; b=VHvI5uTuMw9WTIkKh/eDQoT0nMb3A0tPBkoFEeLS+TvHep2mjChA1A1zU5PprKI7Y7L58R F9FYyIQh6hAIk29+jCatZ1vKNGN0C0Vm24rtkanp6b1O7+xDinP3cIz2zrLKE4y/YChVS+ aatw9HgCDg4ZHHcUNRE+HCSceUyNkPQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FD0+sF9a; spf=pass (imf27.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.208.52 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731288016; a=rsa-sha256; cv=none; b=cbH2R2CnNYLH40DwXMU1Y2I3bNyCF29pAJQZjlnErjfD8/7QFGGogVQTRzMhdPblh92gdO OH9vs/X2G7dNHct14Kv4vPqxXQxpfjebdwJr0AtMVAZmrsSqahp0Kyr3QGulgUQrWHFjyS Pl58boFLpf2kYnLbhMsqvTfgykkml8o= Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-5c9404c0d50so4929031a12.3 for ; Sun, 10 Nov 2024 17:24:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731288246; x=1731893046; darn=kvack.org; h=message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RD8CuXH0sDsPA4cZucuLgP8G7WCBgwpKF9OHtKeps3A=; b=FD0+sF9amxezJWBFTO9cOgKdPGTE5ZiEcbq5hvR2y98WREaY52ukVh57Py19NMeohr zITnj4sl+JgCHgvOxkiWaDx4KpF7y63B9ZI2ZtF98Ja24Ds9Ay9Nyp6OgMIAac79evgf EOR8MvcVdWBwi/QlBx1cPqXlHFpXH7ew01Xd7KXzoyCwOvXZtINi7my3Mhiyx3jHg8X4 MnYMU4U+sHGgeuA/eRoHvieFHpf5zFiw0/Ptn9jx2FAFf2oH4nGEoUCCZK/ucrTO1M5b NdH9KiWxFYe5MUsaqgJwIGSQiJD+nRtewZAyNGtjy2RCSFJHguhF1PlgJYbmwwYm3Bwr z1NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731288246; x=1731893046; h=message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RD8CuXH0sDsPA4cZucuLgP8G7WCBgwpKF9OHtKeps3A=; b=jUMnm5baXsE8kU7sLHLmWFRfW8bi5Ii6OTNKo9PvBF5zcMn9Hi+rNcQDwvyZB2LeIS kAWbrSV4L4h27mK9Tfzrv5ZkJuYUTuceBaiZ4iCNw9vGNWdEULilcDDtqgVXk6IgCRU/ 5SbunQ/tar9YrOnebAQrBtOu88ENNEc99E0PLf7npvquMrfY9apId49Mx4XNO5MLOM81 BqxPXi6o1SdE2anxyQysVm1T7+ariQJjol3B8/Tl53pX0TLnjVtKdMT2uA6Qx+4Y7uqK KhX10HVS6EnIppChiUvo0A/IedfAwEYwYgvnC3osbp/Ax0c4z27y0h4IQpzHgJ+3kj8J DqhA== X-Gm-Message-State: AOJu0YzTD8whfNhn9aCMncR67057d0vA+kFE3JnnufBUF5hqgmECHokm QbZ01hvRfc/c5tdrHMMNba2uwTz2/JElVyfNGFJqX8tMJ+WQkSB+ X-Google-Smtp-Source: AGHT+IGBKGSFxFi0WRPEqVx7jQED6b56o0sjO59a6NQ3doOH8NwVyg4Owq/Bdzehysir8i2UlqOuUQ== X-Received: by 2002:a17:907:3fa2:b0:a9e:4b88:e03b with SMTP id a640c23a62f3a-a9eefce928emr1110550366b.0.1731288245369; Sun, 10 Nov 2024 17:24:05 -0800 (PST) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a9ee0dc578bsm540401166b.104.2024.11.10.17.24.02 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 10 Nov 2024 17:24:03 -0800 (PST) From: Wei Yang To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, Wei Yang , "Liam R . Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH] mm/vma: only set vmg->next when removal is necessary Date: Mon, 11 Nov 2024 01:23:40 +0000 Message-Id: <20241111012340.28906-1-richard.weiyang@gmail.com> X-Mailer: git-send-email 2.11.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 882D54000A X-Stat-Signature: a4rbp1j843sd9rtdt59uqw965fz5ogm7 X-Rspam-User: X-HE-Tag: 1731288206-335222 X-HE-Meta: U2FsdGVkX18EHHsWZkaxQl2jk1G87/rUqVlUQwe3XxI0SK75PC2XfygHaMccN+T2iEJB+YAey/KEn1yDuYwOUZCcwVduWMnDBCNBrqWrsiTvtRiwG8UeSruMbilPeeoQDo4+e4nxQFgGBiUVwZuSgJC3aIHRcF3WhIjJbIsrZwHecdL74Z50D4MVMLBj8o7RKpM1SEf+qHQZjXx/4oUCBTydNA43LwsCTgwKTTvhshIyupmvttkYTVju9f2V/2AovfpjwD+4YtH7+6b5Jw8nKUcNC2UkeNzcsRR0C0tySWnN2nHaw3dwJ/zWS5Pza38V4slN8CSv91baJ6j+mP1dWp7v2O3HX4xebtLR0wC7uZQ6QS9aJbwY/g18lmzw21fSRCDupeft6+yC59ghoXOduHNwF+fuZl0PpRi9MmnXoLfck/IeuC38kIy/f4vY59jYwlje7O8iEKA6kCEZzzJ2gwlZwEjGZgddmUkB5TovYBcjI2Yl6Kx+P3OS8xmoQuV3GIsXOasrn8NQYc4RJ9Y09GO8k2pow5Bors5iGNGzykPqTIOopUREra+VUNzj/0PDKBppS8JGCEdn7wigKOxYi/aeDiu5fNIa8a/7Cq/OerSWLdqfarcz0gg+II0fc35LS5jYrH6uF0t0o8DPXRNE390z4HKx6j8LglC3TAM8aNXfc3QTYI1uWfufsTyFxFQulHbu2tD5AKnDQnWWjobChRnsOLKWSqDYPx5Z3uIh6IiIgajoyOr9ABKDjG5YpvG01vqgDGq4hdiXKo02W7Gyqe0VikH1jpLHkr3XFSC+pVknfN9BtQChz00u29cfW9oR77HAS+Ml7o92RWsYfKD+6lhvnH5cGfQRkQwZaYzv89M2wjBiswfQm8/KW4m0YDWN1urUVgiSmPuCCc/E+epgvmq1baGGDzEXX52aclMlIjZYBEODAVojEbxvuKcGMs75xlcmo/d7IkazMfQTvXv Qm4wvFTu /BDGoTyF5W9xkP12cM+nzlEWialAQZAcxBWRbuAD3l5kSn9xIlUh+tv5MhVBZ1/BLcdZ1CBvmadBn5rcFHUQT/KbkSF314XoTjTzvwu11Lsz/HE+1wnFtcZmJrtLgQ+oNTqwFK7r8FQTA25uiiAXIpNKyjYfqgEMC/jP04U9I5XU3D2IZhAAvll7Hyq+QGrfB/9IQLmfecqz/+fT6TitQkSg/f52AfSTqxrMHdvgwCZTqEaP28Qvz2NF86DGnMISBgvmt5bNzvpKhZYVsHwQaUXBocVJwwpehCEPvu3jJ+tqOTKoQLkpbtPEUPHn194M7WIJVAfxc2sqMIHIPSMdtPNTO5/pcCZ0qrBDvyKU4BAhs/cxZsBDh9CXFS6amDCl1ehyTow3Z2VqYSJ1UoJMyOBlKNrK2tjcpEsRrqkMzhE3wgUr7phzqafrjSNUDNWVfKbCnkgLU0x1i9vkQkFPAK10qI6iHDCrK3OjQq0EWfUDvN2A= 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: vma_expand() is called by relocate_vma_down() and vma_merge_new_range() with only vma_merge_new_range() has a chance to remove 'next'. By leveraging the knowledge in vma_merge_new_range(), only set vmg->next when removal is necessary, we can simplify the logic in vma_expand(). Originally we have an assumption that VMG state could be safely reused after a merge. This assumption is removed after commit 5a689bac0bbc ("mm: remove unnecessary reset state logic on merge new VMA"). So we are safe to clear it. Signed-off-by: Wei Yang CC: Liam R. Howlett CC: Lorenzo Stoakes CC: Vlastimil Babka CC: Jann Horn --- mm/vma.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mm/vma.c b/mm/vma.c index 8a454a7bbc80..85d82bc1eaed 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -984,6 +984,7 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg) can_merge_left = can_vma_merge_left(vmg); can_merge_right = !just_expand && can_vma_merge_right(vmg, can_merge_left); + vmg->next = NULL; /* If we can merge with the next VMA, adjust vmg accordingly. */ if (can_merge_right) { vmg->end = next->vm_end; @@ -1001,8 +1002,12 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg) * are not permitted to do so, reduce the operation to merging * prev and vma. */ - if (can_merge_right && !can_merge_remove_vma(next)) - vmg->end = end; + if (can_merge_right) { + if (!can_merge_remove_vma(next)) + vmg->end = end; + else + vmg->next = next; + } /* In expand-only case we are already positioned at prev. */ if (!just_expand) { @@ -1030,9 +1035,9 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg) * @vmg: Describes a VMA expansion operation. * * Expand @vma to vmg->start and vmg->end. Can expand off the start and end. - * Will expand over vmg->next if it's different from vmg->vma and vmg->end == - * vmg->next->vm_end. Checking if the vmg->vma can expand and merge with - * vmg->next needs to be handled by the caller. + * Will expand over vmg->next if it's set. + * Checking if the vmg->vma can expand and merge with vmg->next needs to be + * handled by the caller. * * Returns: 0 on success. * @@ -1043,17 +1048,15 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg) int vma_expand(struct vma_merge_struct *vmg) { struct vm_area_struct *anon_dup = NULL; - bool remove_next = false; struct vm_area_struct *vma = vmg->vma; struct vm_area_struct *next = vmg->next; mmap_assert_write_locked(vmg->mm); vma_start_write(vma); - if (next && (vma != next) && (vmg->end == next->vm_end)) { + if (next) { int ret; - remove_next = true; /* This should already have been checked by this point. */ VM_WARN_ON(!can_merge_remove_vma(next)); vma_start_write(next); @@ -1062,13 +1065,10 @@ int vma_expand(struct vma_merge_struct *vmg) return ret; } - /* Not merging but overwriting any part of next is not handled. */ - VM_WARN_ON(next && !remove_next && - next != vma && vmg->end > next->vm_start); /* Only handles expanding */ VM_WARN_ON(vma->vm_start < vmg->start || vma->vm_end > vmg->end); - if (commit_merge(vmg, NULL, remove_next ? next : NULL, NULL, 0, true)) + if (commit_merge(vmg, NULL, next, NULL, 0, true)) goto nomem; return 0;