From mboxrd@z Thu Jan 1 00:00:00 1970 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 03153C71153 for ; Mon, 28 Aug 2023 20:26:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D838280027; Mon, 28 Aug 2023 16:26:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 162C98E001E; Mon, 28 Aug 2023 16:26:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F1DC8280027; Mon, 28 Aug 2023 16:26:44 -0400 (EDT) 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 DCCE18E001E for ; Mon, 28 Aug 2023 16:26:44 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A23CD403A3 for ; Mon, 28 Aug 2023 20:26:44 +0000 (UTC) X-FDA: 81174646728.09.B71FAB2 Received: from mail-il1-f178.google.com (mail-il1-f178.google.com [209.85.166.178]) by imf08.hostedemail.com (Postfix) with ESMTP id D357516000E for ; Mon, 28 Aug 2023 20:26:42 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=XoFUyree; dmarc=none; spf=pass (imf08.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.166.178 as permitted sender) smtp.mailfrom=joel@joelfernandes.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1693254402; 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=GdS5U3zAZC1OpDhYKXHHzRAYVsQHuMtwD7yrhm4gv74=; b=2wts/dZfZeFrHp7HycOnln8W+hqNv1uTQyk6DmvCWJXtIZD4TgBg3lWDsVWNTYga2xLrOx GRHFtTQRv/xgqtopRWQ6E6PQ3O58BJ4wgcvMFIe+TbpEC9QA+gDdtTcbxAMWVVNpre9KpE BE7eywLOkrHePt4i/s3jRGS89jwQz6c= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=XoFUyree; dmarc=none; spf=pass (imf08.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.166.178 as permitted sender) smtp.mailfrom=joel@joelfernandes.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693254402; a=rsa-sha256; cv=none; b=AgtfW/WLOajrIyQJV6V/IhLe4qFVGndJtWx3Pbove4IKHNb7ZheKVb20qMa+SvQQ7dy0pR kcU3apUNYyOfA9CB9UH6PslycHMUqRRj2FJ84x+c9XvbpD8hFVE278vdHxjwqUx8FqLjEy 9yBtjIoZ2PtRKxw4RMu5d+XEt6tV3To= Received: by mail-il1-f178.google.com with SMTP id e9e14a558f8ab-34cb35dfb00so13690075ab.2 for ; Mon, 28 Aug 2023 13:26:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1693254402; x=1693859202; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=GdS5U3zAZC1OpDhYKXHHzRAYVsQHuMtwD7yrhm4gv74=; b=XoFUyree7jGvewOhLkWctFz9UUfqABU6WjfNyh4y2OGVkrmFTJWqCE6wgWP7Ri68HE /7HD8pklyNEEO/PNMSQ1Lf3CRRy4GFIQz7O6mmGJUqMi3vpDb113Z9FICN++FMJZ2aKi 3TV99MoyxR8BLjZ2inOpXoPqZimLx3pjfwCL0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693254402; x=1693859202; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=GdS5U3zAZC1OpDhYKXHHzRAYVsQHuMtwD7yrhm4gv74=; b=AsH2RQ0iq4k0n9m06WY+UIlm0ZU9o1MonRKNUG6jxoJWeWsuc7Nq1k+zyxDgGFWhg5 C7ry8IGbjXnHABVFjDYYcKMQInnWWsbv/vqCUWxacMFoC/JRQLXl+Qg4GwF2A51BvDgr JwiOH6FMoqp0rcDNXOAS3H7pvDZiD493YAYQg3fuyYCyYAV+kyvHtuEiITVKZUi+qR7q LEFX6p0jemmlVWks1THrxerl2WGEVTi1rl1bJzaW38EyvxLz95P6fKiX1D1cnrRYKhdN 8AFeomVjhywsfU6kSD8qVuWO+vjW431i1U/dpzvE5yaiY3lR17sQsRiCACgo5R2NnD8A uc3g== X-Gm-Message-State: AOJu0YyZ8bZfOVbgg6Lup43qc/a0zfsgQoJv2zTvO6iUd2T46HUnsamw 7W9U5RCrQG/U8wdeSpfK1IRARw== X-Google-Smtp-Source: AGHT+IGRLCtFYkksB6W0I4qT0QxymU8BO3DUqCwvOEFrU8Ok9D94zjEg8TO2rLD8je6BGtvYtCaOKQ== X-Received: by 2002:a92:d7c7:0:b0:34a:c618:b904 with SMTP id g7-20020a92d7c7000000b0034ac618b904mr18628179ilq.22.1693254400046; Mon, 28 Aug 2023 13:26:40 -0700 (PDT) Received: from localhost (156.190.123.34.bc.googleusercontent.com. [34.123.190.156]) by smtp.gmail.com with ESMTPSA id f11-20020a056638118b00b0042b3042ccd8sm2666957jas.13.2023.08.28.13.26.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Aug 2023 13:26:39 -0700 (PDT) Date: Mon, 28 Aug 2023 20:26:38 +0000 From: Joel Fernandes To: Lorenzo Stoakes Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Linus Torvalds , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan , Kalesh Singh , Lokesh Gidra Subject: Re: [PATCH v5 2/7] mm/mremap: Allow moves within the same VMA Message-ID: <20230828202638.GA1646335@google.com> References: <20230822015501.791637-1-joel@joelfernandes.org> <20230822015501.791637-3-joel@joelfernandes.org> <46196ba1-c54d-4c1d-954f-a0006602af99@lucifer.local> <20230828183240.GA1621761@google.com> <8891681e-532c-4d7b-bc28-b4ad3e017331@lucifer.local> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8891681e-532c-4d7b-bc28-b4ad3e017331@lucifer.local> X-Rspamd-Queue-Id: D357516000E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: j4c4h4dywxtdpr5xjuhc5bw4wk87rc9x X-HE-Tag: 1693254402-540356 X-HE-Meta: U2FsdGVkX19uvuYY46BB2KoSsRfwZ+xmpxe9RtgqL1+aMzerdzcfgC+sqQQMQcQ72yDrynlOk0R56FgOJuooZPMaoiXHyjjdYiU2Y1eZGydluhIzkAg+DLPMthsIDvGzHR3fx+1JPTx5Au3/l38e968DkFe5qYUuXj/MSlRGseG121rqo+9fJyfFLR4qW3uGLgDkDNWKik6XqA4AgsP0WY8BbRu4AJweoCW0E/ClkRwQci1n2toYWtZ51A644e9VmB+jsSslfRNK9bI9kbFOJw1Hn0ZeOuLbfqp+uDen3yW2/ZdU2enQl14oIImSF8kjfW4QrNQhhOeZ4DzEFRPCSZP2Z+EYIWOpuPUsRGCYuuOL3z3h9qNQ4RzTOYxdPZaLIHqSvoM1+qln7JXoT6g7auKc+MpoXJdXWEnnmTATNPgsIt8OU82XCCv/VxDXwkTDVyFc/Awil0bUH+1mszMTFaq7v0XHVHQZT/ka2wcanYqsmJJwKVIYSvyubLIOubz5PaEZtfVEPVDjkRf9h4dFPcv0U+yP/9dowWUdQwcVble8B3HF3uhsz8pl521zjLxBscny1qmtklOsbqZOckLh3IaD+v7wk/GhY2jYxM5OZPTgn3tXKk2CfjFKRXsOsMFyDuh7L/LJjgd9GfR95CDKAoseIzF4Hcmi5+7gXAY2NztmkwE8O9QC+S2xa5HOPQj00RpqKIUrg+UdAWtkJgTVyYvIKRQ7x8CFZUsijj1WHd8RoXH1LT/MgHm8jyGYLebaNPXOv4hM4OXGMgPGq0nS1mJguPWg39J/1xix0T+Lnoft4c+RMqouaqQg/qTcKE4gzJ8dbO79h7PpkGAo2gdlS5Jh0CqAYRarcwRayvdvUCqOKZY8mGOZNVGu78+gNs8q42Bu9q4rKvyFBPTa3w1sXv847a66k2RwpQ+j/ZuBGgFRQ+Jc03Fo+OUc9xV/dW4MlvbiBoJ6FEaIxyYWv6b ijZzNqSu nTZ1LiNyH/n7priUxH76ZmxfJD8QJz80Vd+g+Oh3O8gFlz3zUEQCrCAe9RqKMozEj7fsTPTga+zmLw6/6Z3TfeuabD/pAF6yynwAfbK1KdOjz57aK+bqJRStvQ0uxceIDM1/0ARQ/9+UVlgXhdw3mkXw/L/WU+8E6XaIVPhsIqCZVqW0omb1HsQ+SuSF8d8Iy2DNJUYNRRIZiXWVkdznE/dS2w4RrEPRs0yjHUhfJCzaFw8rjbJyfgi30KW7v2qPwKceNI+9HrNjiHGoG+BZ/TL779c7LCi4RF1aPjVIImvHIF2AWw3fCEcaDMGQzmj265rOl 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: On Mon, Aug 28, 2023 at 08:00:18PM +0100, Lorenzo Stoakes wrote: > On Mon, Aug 28, 2023 at 06:32:40PM +0000, Joel Fernandes wrote: > > On Sun, Aug 27, 2023 at 10:21:14AM +0100, Lorenzo Stoakes wrote: > > [..] > > > > > > > > /* > > > > * Flags used by change_protection(). For now we make it a bitmap so > > > > diff --git a/mm/mremap.c b/mm/mremap.c > > > > index 035fbf542a8f..06baa13bd2c8 100644 > > > > --- a/mm/mremap.c > > > > +++ b/mm/mremap.c > > > > @@ -490,12 +490,13 @@ static bool move_pgt_entry(enum pgt_entry entry, struct vm_area_struct *vma, > > > > } > > > > > > > > /* > > > > - * A helper to check if a previous mapping exists. Required for > > > > - * move_page_tables() and realign_addr() to determine if a previous mapping > > > > - * exists before we can do realignment optimizations. > > > > + * A helper to check if aligning down is OK. The aligned address should fall > > > > + * on *no mapping*. For the stack moving down, that's a special move within > > > > + * the VMA that is created to span the source and destination of the move, > > > > + * so we make an exception for it. > > > > */ > > > > static bool can_align_down(struct vm_area_struct *vma, unsigned long addr_to_align, > > > > - unsigned long mask) > > > > + unsigned long mask, bool for_stack) > > > > { > > > > unsigned long addr_masked = addr_to_align & mask; > > > > > > > > @@ -504,7 +505,7 @@ static bool can_align_down(struct vm_area_struct *vma, unsigned long addr_to_ali > > > > * of the corresponding VMA, we can't align down or we will destroy part > > > > * of the current mapping. > > > > */ > > > > - if (vma->vm_start != addr_to_align) > > > > + if (!for_stack && vma->vm_start != addr_to_align) > > > > return false; > > > > > > I'm a little confused by this exception, is it very specifically for the > > > shift_arg_pages() case where can assume we are safe to just discard the > > > lower portion of the stack? > > > > > > Wouldn't the find_vma_intersection() line below fail in this case? I may be > > > missing something here :) > > > > I think you are right. In v4, this was not an issue as we did this: > > > > > > + if (!for_stack && vma->vm_start != addr_to_align) > > + return false; > > + > > + cur = find_vma_prev(vma->vm_mm, vma->vm_start, &prev); > > + if (WARN_ON_ONCE(cur != vma)) > > + return false; > > > > Which essentially means this patch is a NOOP in v5 for the stack case. > > > > > So what we really want is the VMA previous to @vma and whether than subsumes > > the masked address. > > > > Should I just change it back to the v4 version then as above for both patch 1 > > and 2 and carry your review tags? > > You will not be surprised to hear that I'd rather not :) I think if we did > revert to that approach it'd need rework anyway, so I'd ask for a respin w/o > tag if we were to go down that road. > > HOWEVER let's first clarify what we want to check. > > My understand (please correct me if mistaken) is that there are two > acceptable cases:- > > 1. !for_stack > > addr_masked addr_to_align > | | > v v > . |-----| > . <-must be empty-> | vma | > . |-----| > > 2. for_stack > > addr_masked addr_to_align > | | > v v > |----.-------------------.-----| > | . vma . | > |----.-------------------.-----| > > Meaning that there are only two cases that we should care about:- > > 1. !for_stack: addr_to_align == vma->vm_start and no other VMA exists > between this and addr_masked > > 2. for_stack: addr_masked is in the same VMA as addr_to_align. > > In this case, the check can surely be:- > > return find_vma_intersection(vma->vm_mm, addr_masked, addr_to_align) == > (for_stack ? vma : NULL); > > (maybe would be less ugly to actually assign the intersection value to a > local var and check that) For completness: Lorenzo made some valid points on IRC and we'll do this patch (2/7) like this for v6 after sufficient testing. static bool can_align_down(struct vm_area_struct *vma, unsigned long addr_to_align, unsigned long mask, bool for_stack) { unsigned long addr_masked = addr_to_align & mask; /* * If @addr_to_align of either source or destination is not the beginning * of the corresponding VMA, we can't align down or we will destroy part * of the current mapping for cases other than the stack. */ if (!for_stack && vma->vm_start != addr_to_align) return false; /* In the stack case we explicitly permit in-VMA alignment. */ if (for_stack && addr_masked >= vma->vm_start) return true; /* * Make sure the realignment doesn't cause the address to fall on an * existing mapping. */ return find_vma_intersection(vma->vm_mm, addr_masked, vma->vm_start) == NULL; } Thanks Lorenzo for the suggestion! > > > > This is also hard to test as it requires triggering the execve stack move > > case. Though it is not a bug (as it is essentially a NOOP), it still would be > > nice to test it. This is complicated by also the fact that mremap(2) itself > > does not allow overlapping moves. I could try to hardcode the unfavorable > > situation as I have done in the past to force that mremap warning. > > I find this exception a bit confusing, why are we so adamant on performing > the optimisation in this case when it makes the code uglier and is rather > hard to understand? Does it really matter that much? Let me know if you still felt it made the code uglier, but it looks like just one more if() condition. And who knows may be in the future we want to do such overlapping moves for other cases? ;) > I wonder whether it wouldn't be better to just drop that (unless you really > felt strongly about it) for the patch set and then perhaps address it in a > follow up? > This may entirely be a product of my simply not entirely understanding this > case so do forgive the probing, I just want to make sure we handle it > correctly! It was just to avoid that false-positive warning where we can align down the stack move to avoid warnings about zero'd PMDs. We could certainly do it in this series or as a follow-up but since we came up with the above snippet, I will keep it in this series for now and hopefully you are ok with that. thanks, - Joel