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 3A6BDD116FB for ; Sun, 27 Oct 2024 12:15:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB2816B0085; Sun, 27 Oct 2024 08:15:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A63506B0088; Sun, 27 Oct 2024 08:15:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 92A4F6B0092; Sun, 27 Oct 2024 08:15:21 -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 760136B0085 for ; Sun, 27 Oct 2024 08:15:21 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E2F5DC0AD3 for ; Sun, 27 Oct 2024 12:14:57 +0000 (UTC) X-FDA: 82719276612.12.F00189D Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) by imf20.hostedemail.com (Postfix) with ESMTP id D6D341C001F for ; Sun, 27 Oct 2024 12:14:51 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=URSRw6hL; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.221.52 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730031203; a=rsa-sha256; cv=none; b=5TTdrt2fyquZMZqLwewzNSLagvCkm5ClNHsQBG6Fd8hzMVARVfXm9ZJk511qWc9qNQmhXM c5dAscLdcjVRAywnilJVXm18gyA6KUiR9labQi4aX1wQt8W4HNO6Sd/lStopDdtm/SaHoC hAEpibrOhzovFH7b/hXoZMDVrLC675Q= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=URSRw6hL; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.221.52 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730031203; h=from:from:sender:reply-to: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=I0uNGU0qPhSwFtf1/0x7VLcfxN709g96Ep+w7HMo4V4=; b=SP6037Oh3P21FJ+4Ur4l4wsHQakm9POT1kqRQqoR4glqwS4iIV46toS3buPtOer6t9hUSp RPjdbXdecOMMzJUMmsXjDVaNGG0nd5RyRHZqApNSbc12/Mr/F4GPvroNjAiZZcMnDTnQvi hTR1cxLaFOvr6ZlJJt6Ak674DIjhMZY= Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-37d58377339so3016420f8f.1 for ; Sun, 27 Oct 2024 05:15:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730031317; x=1730636117; darn=kvack.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=I0uNGU0qPhSwFtf1/0x7VLcfxN709g96Ep+w7HMo4V4=; b=URSRw6hLcMYF8snFVBHQV2C0SgnykKv8VNa2JCfLv3naqlbownHk5iDQGE7rDIVjR4 xI5nXUP3DVLcHm9uw+X4dhcWtwFHa94Weygadi/S5StYw6eLwpmG54PikN/twHwlXt/j CS/On4ij+xLY4qx2AENc/n8IzAvMsJAjqagdtq6kg/NQ4vNyUoTpELpHf4oLAZI8hK3A nhXH31swjRxgQhnqe/ZcXHWPXh1SMn7pHS9ipDik3JBuLUjvRKkXkdkU+kDlvevx9T0O OMRq2WZ9ONGzXbdWRxayN+Eqf/scb3Gt3SfYoF5K4Jpu9F4niPgBuv/XtjkcdxZH4vmp rtow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730031317; x=1730636117; h=user-agent:in-reply-to:content-disposition:mime-version:references :reply-to:message-id:subject:cc:to:from:date:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=I0uNGU0qPhSwFtf1/0x7VLcfxN709g96Ep+w7HMo4V4=; b=tcexrmYxFTVO87aATthBps4duxTyopPdMkMmxH/bYcspNgTNTKlJL4sSuW/1kNX/zI RdgkHlAEsHU9veU5nxEu3AL7sUJm8ebdlNyR39YWOMJW+Z/QvJn3L8ld1nt6oMCjW/l2 d6fnvBuEbY7neXmWJ+5NzAne/2SBAHoD5lo6hXfkwWsj3u8P8NX9gU2nDqKkVM5wUH4j u5C68oshy9/0YcWSgzQVunyFMNrx3xoePMvDb+KPV/DNH4CkMsy5rMd6QpcOwMTP6zKf aUhZzAZaWhYcH0jKLvLPLrZnxeN6Ze0YZBP11WFbrWqi3Ind+RnhhgN5C3M2qQJ/gOVz DquQ== X-Forwarded-Encrypted: i=1; AJvYcCV6FC6nRzL5huENPbbWioYx5fxjV194uoN0R0d84fMCP+12zEGXu3i31Kw846itHTUREtH/EgiLwg==@kvack.org X-Gm-Message-State: AOJu0YzbgjpX9WFehGvLRUOzrgXWqkTL2cRck2HsRs9OFph8ykcgWWor MmnJJStVAIzQxw8OuhAX3SZn3VybnAE6P8MtZDowk0RRPKucd6FP X-Google-Smtp-Source: AGHT+IHon6H/zOVRVEoZJbjHEm0AODvp1FKVSw7WNdp4ip/ixmlGZG4OJZsHPvCnNUsFVI4XbZxsmw== X-Received: by 2002:a5d:56d2:0:b0:37e:f4a1:2b58 with SMTP id ffacd0b85a97d-38061172aa9mr4967028f8f.16.1730031317304; Sun, 27 Oct 2024 05:15:17 -0700 (PDT) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a9b3a0887d8sm269165566b.206.2024.10.27.05.15.16 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 27 Oct 2024 05:15:16 -0700 (PDT) Date: Sun, 27 Oct 2024 12:15:15 +0000 From: Wei Yang To: Lorenzo Stoakes Cc: Wei Yang , akpm@linux-foundation.org, linux-mm@kvack.org, Liam Howlett , Vlastimil Babka Subject: Re: [PATCH] mm/mlock: set the correct prev on failure Message-ID: <20241027121515.iu2g4lfbimasru5l@master> Reply-To: Wei Yang References: <20241027025629.14715-1-richard.weiyang@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) X-Rspamd-Queue-Id: D6D341C001F X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: qw8x4rdsc3tearx31h8bk7q4remae99m X-HE-Tag: 1730031291-562726 X-HE-Meta: U2FsdGVkX1+Z5RiNautZyN6VLVECyAHzgfxTQd6nhxYHRhZk1JEAq/4vGmSb9wsDuNdpz8rgxq1xnAwqUoMCouGYVtu9OB7QKX7cTvXwggIziN/5aXwENFr+hm43ZXI0HagrTvuRfx1F3lLiAH5fCmfzeiVrJGPf3H55Xg0WSAYuKsoCpkLYjXFSfasT0lQcbyQcYABlV17rFAiedAV4h1qfczxW8RU41i38OOH3XG2qGhX1MUHHT+oK+MFKaCj2/iOf1Bgmf8V1uGYLAzHrX2Sw7PRXSizs87KqGuVi2Z4hT0iZS4uWEBZSNGtyAsDQWTkdWKyf1xmb2uJy5QqSnkh9ZPSey9r/NcbgUpZpHhn+axxaJtyO1lJsgy4q77uT6f5KEFuci/9c/egRc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ZlEuzF1hvDatWJgOJugGVs//Ww7CIlGPOipqhgNYXpgs0/41EM4QJAnvC5N5RLBtIPgDyzJh2T6ALyredFgNuNYdQO6TmKLXkvy/fNN4bqrDjzO9CrJVauDGxy04w6n0IcLyqsQ6d3N7O6nSwuz0Dbidbh12Wd1LWa9tW3iabC73UNGcu6hagjoY5C6yBDSaHReTvHHW7Otkoba4PP3j+gbXMkQ 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: On Sun, Oct 27, 2024 at 11:41:13AM +0000, Lorenzo Stoakes wrote: >+ Vlastimil, Liam, Jann as this is VMA-related. > >We really need to bring all VMA-ish files under the VMA MAINTAINERS >block... will maybe address that once things around that file... calm down >a bit. > >But please cc all of us on anything that even vaguely relates to VMAs, >thanks! > Sure, will add them in later change. >On Sun, Oct 27, 2024 at 02:56:29AM +0000, Wei Yang wrote: >> After commit 94d7d9233951 ("mm: abstract the vma_merge()/split_vma() >> pattern for mprotect() et al."), if vma_modify_flags() return error, the >> vma is set to an error code. This will lead to an invalid prev be >> returned. > >This is a great spot, but this commit message is missing critical >details. This is only meaningful for apply_mlockall_flags() which is both >ignoring errors AND assuming mlock_fixup(), even on error, is correctly >updating the prev state. Which is imo wrong. > Yes. >So I'd _add_ a bit more information here like: > >Generally this shouldn't matter as the caller should treat an error as >indicating state is now invalidated, however unfortunately >apply_mlockall_flags() does not check for errors and assumes that >mlock_fixup() correctly maintains prev even if an error were to occur. > >This patch fixes that assumption. > >We'll also need to backport this, so a: > >Fixes: 94d7d9233951 ("mm: abstract the vma_merge()/split_vma() pattern for mprotect() et al.") >Cc: > >Needs to be added, and make the next revision [PATCH hotfix 6.12 v2] to >make it clear this needs to go to 6.12. > Will follow this. >> >> Signed-off-by: Wei Yang >> CC: Lorenzo Stoakes >> --- >> mm/mlock.c | 7 ++++--- >> 1 file changed, 4 insertions(+), 3 deletions(-) >> >> diff --git a/mm/mlock.c b/mm/mlock.c >> index e3e3dc2b2956..8c3f9cf8f960 100644 >> --- a/mm/mlock.c >> +++ b/mm/mlock.c >> @@ -478,11 +478,12 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, >> /* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ >> goto out; >> >> - vma = vma_modify_flags(vmi, *prev, vma, start, end, newflags); >> - if (IS_ERR(vma)) { >> - ret = PTR_ERR(vma); >> + *prev = vma_modify_flags(vmi, *prev, vma, start, end, newflags); >> + if (IS_ERR(*prev)) { >> + ret = PTR_ERR(*prev); >> goto out; >> } >> + vma = *prev; > >Yeah sorry I hate this, it was icky before and it's kinda disgusting to >assign *prev then *prev to vma, then, if success, vma to *prev - yeah >that's super confusing :) > >I mean a better alternative if you were to do this approach would be to >have a new vma local but I don't actually think that's the correct >approach. > >Really the caller _must_ deal with errors, and not assume any state is >valid after an error occurs. > >So I think the fix should be in apply_mlockall_flags() instead like: > > ... > > for_each_vma(vmi, vma) { > ... > int error; > > ... > > error = mlock_fixup(&vmi, vma, &prev, vma->vm_start, vma->vm_end, > newflags); > /* Ignore errors, but prev needs fixing up. */ > if (error) > prev = vma; > > ... > } > >This is also a smaller delta for backporting. > I have to say this one look better. Thanks > >> >> /* >> * Keep track of amount of locked VM. >> -- >> 2.34.1 >> >> > >I'm happy for you to resubmit like this and take full credit by the way! :) >assuming you agree with this approach. > >This is also reminding me that I need to refactor all this crap, the whole >passing prev around and looping like that is horrible. Also the outer loop >should be maintaining prev, not the inner one. > >This is going on my TODO list! -- Wei Yang Help you, Help me