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 600A1C433EF for ; Fri, 22 Jul 2022 13:51:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 733536B0072; Fri, 22 Jul 2022 09:51:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E1B16B0073; Fri, 22 Jul 2022 09:51:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 582C36B0074; Fri, 22 Jul 2022 09:51:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 469736B0072 for ; Fri, 22 Jul 2022 09:51:48 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 08671140CB2 for ; Fri, 22 Jul 2022 13:51:48 +0000 (UTC) X-FDA: 79714873896.09.AB82798 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 9957A80063 for ; Fri, 22 Jul 2022 13:51:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658497907; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=NA/icI4SGPUsAzJtXWUBBRXNCfBWprjzwJ+ZmvXATQQ=; b=WwD+IIqb3GiMawyAWfUysEVY3JXPmbktSRGI9uojhFJprI93YbMjMIfnC69QXJLxOh7Di4 W3oEnZnziYKPLysZ/70sRqTUu3yFk+E8Trf9AOH7Wjt+j5dPmch06DEbyCC3UovUfMgqKM /lCsAgw8HCG+gDPOyL6WcOBMloW7TEU= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-63-RRaWIPy1NSqYDSbOqPZLmg-1; Fri, 22 Jul 2022 09:51:46 -0400 X-MC-Unique: RRaWIPy1NSqYDSbOqPZLmg-1 Received: by mail-qv1-f71.google.com with SMTP id f8-20020a056214076800b0047421aaa4c0so751012qvz.5 for ; Fri, 22 Jul 2022 06:51:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=NA/icI4SGPUsAzJtXWUBBRXNCfBWprjzwJ+ZmvXATQQ=; b=cWdoDMnd+V7Cd41tmz72tHa3wT4WIyiuCNPP70/SjJR31vlP/BeaIan+d/lDAzfkat DalH5xYwwgRwp3Q08fMDmDmVRQOnG6J7SbkAhUAEqY9p6JwHfe+KFiRVBYjd0CjGCYuu J38VDFVOV4Z6qM2AgKtqdtQsoFBOAsIp6Qs8rUpVhjRsWG4TQ7DMNZzxI+5Y8oztGMtd nec3Id/2cRF+jhwn6Pfb2lEeZULmJJ9iuOJyv6g4V+oJdTdYzaHfcCjRv79Bpd3f7phi h2hc+0nx88hVY0PoRw1NyYaLvQ+ja6+iUiMAuvmgkWG9P3jpMuegIkPvu0cUJCYBOsa6 YYMA== X-Gm-Message-State: AJIora9QTc2nSdbS3wsl3zCOKKmmHILBinfVqywNvSsyI335lwswoWpC dTYGNggE7PsLcKfIackdr/R4eNSjXJad9HX/JT0B8ATGtCCbTNqJVi/omYW3qSOLevlwpY5BPXW voJbJmbd2uPM= X-Received: by 2002:a05:622a:1308:b0:31e:f8fd:5366 with SMTP id v8-20020a05622a130800b0031ef8fd5366mr95626qtk.387.1658497905287; Fri, 22 Jul 2022 06:51:45 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uQNMEVj5gQNfhym5zCEbYFG/gOOUlew9W3SgLJHiWDxgOLaddfFp36LG4rHCSPtgn2qmC4Bw== X-Received: by 2002:a05:622a:1308:b0:31e:f8fd:5366 with SMTP id v8-20020a05622a130800b0031ef8fd5366mr95613qtk.387.1658497905025; Fri, 22 Jul 2022 06:51:45 -0700 (PDT) Received: from xz-m1.local (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id fw8-20020a05622a4a8800b0031eb643c0f5sm3007707qtb.94.2022.07.22.06.51.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 06:51:44 -0700 (PDT) Date: Fri, 22 Jul 2022 09:51:43 -0400 From: Peter Xu To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Nadav Amit , Andrew Morton , Andrea Arcangeli Subject: Re: [PATCH v3 1/3] mm/mprotect: Fix soft-dirty check in can_change_pte_writable() Message-ID: References: <20220721183338.27871-1-peterx@redhat.com> <20220721183338.27871-2-peterx@redhat.com> MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658497907; a=rsa-sha256; cv=none; b=BwagrAXA1iRgBk4G/aq6Z+2D9FByXlWUzHbyL5GeM6Q0Hu16A57/7Oie7UadksudoIaW3m eL1Hoq0soPsiMBmeu7OHbSbog+Ko+VGEpHbnIMYBQcvHa3luWvq/0LuN/6GRLWh+8cXBH7 jZCtsVK+4CgZ9Dy2ak38NRsUGvic1o4= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WwD+IIqb; spf=none (imf02.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658497907; 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=NA/icI4SGPUsAzJtXWUBBRXNCfBWprjzwJ+ZmvXATQQ=; b=DFBEUejcnDbQ0Ab5VglprI608FOSxDBEiHQKxQYCshETyUFIIcSxXQ+bYOXBH/BFz5+4Ng DRSDSTK2+cVsygXjtFeJvmwZnCzERq0oyL6Yxgkqt8E9t/kg7SKdNwZUKRE/DvoJ0BQ5lA se5ljfNggZs/PKHWBwhgmNndBjMLElY= Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WwD+IIqb; spf=none (imf02.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 9957A80063 X-Stat-Signature: kxf848d6a6g7rd7n7sa3k6fsnr1nuwbw X-Rspam-User: X-HE-Tag: 1658497907-686146 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 Fri, Jul 22, 2022 at 09:08:59AM +0200, David Hildenbrand wrote: > On 21.07.22 20:33, Peter Xu wrote: > > The check wanted to make sure when soft-dirty tracking is enabled we won't > > grant write bit by accident, as a page fault is needed for dirty tracking. > > The intention is correct but we didn't check it right because VM_SOFTDIRTY > > set actually means soft-dirty tracking disabled. Fix it. > > > > There's another thing tricky about soft-dirty is that, we can't check the > > vma flag !(vma_flags & VM_SOFTDIRTY) directly but only check it after we > > checked CONFIG_MEM_SOFT_DIRTY because otherwise VM_SOFTDIRTY will be > > defined as zero, and !(vma_flags & VM_SOFTDIRTY) will constantly return > > true. To avoid misuse, introduce a helper for checking whether vma has > > soft-dirty tracking enabled. > > > > > [...] > > > > > Here we attach a Fixes to commit 64fe24a3e05e only for easy tracking, as > > this patch won't apply to a tree before that point. However the commit > > wasn't the source of problem, it's just that then anonymous memory will > > also suffer from this problem with mprotect(). > > I'd remove that paragraph and also add > > Fixes: 64e455079e1b ("mm: softdirty: enable write notifications on VMAs after VM_SOFTDIRTY cleared") > > That introduced this wrong check for pagecache pages AFAIKS. > > We don't care if the patch applies before 64fe24a3e05e, if someone wants to > backport the fix, they can just adjust it accordingly. IMO besides marking the culprit commit, Fixes can also provide input to stable trees to see whether we should try pick some patch up. What I wanted to express here is we don't need to try pick this patch up before kernel that doesn't have 64fe24a3e05e because it won't apply. I can attach both Fixes with the hope that it'll help in both cases if you're fine with it, with slight explanations. > > > > > Fixes: 64fe24a3e05e ("mm/mprotect: try avoiding write faults for exclusive anonymous pages when changing protection") > > Signed-off-by: Peter Xu > > --- > > mm/internal.h | 18 ++++++++++++++++++ > > mm/mmap.c | 2 +- > > mm/mprotect.c | 2 +- > > 3 files changed, 20 insertions(+), 2 deletions(-) > > > > diff --git a/mm/internal.h b/mm/internal.h > > index 15e8cb118832..e2d442e3c0b2 100644 > > --- a/mm/internal.h > > +++ b/mm/internal.h > > @@ -860,4 +860,22 @@ struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags); > > > > DECLARE_PER_CPU(struct per_cpu_nodestat, boot_nodestats); > > > > +static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma) > > +{ > > + /* > > + * NOTE: we must check this before VM_SOFTDIRTY on soft-dirty > > + * enablements, because when without soft-dirty being compiled in, > > + * VM_SOFTDIRTY is defined as 0x0, then !(vm_flags & VM_SOFTDIRTY) > > + * will be constantly true. > > + */ > > + if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) > > + return false; > > + > > + /* > > + * Soft-dirty is kind of special: its tracking is enabled when the > > + * vma flags not set. > > + */ > > + return !(vma->vm_flags & VM_SOFTDIRTY); > > +} > > That will come in handy in other patches I'm cooking. > > > + > > #endif /* __MM_INTERNAL_H */ > > diff --git a/mm/mmap.c b/mm/mmap.c > > index 125e8903c93c..93f9913409ea 100644 > > --- a/mm/mmap.c > > +++ b/mm/mmap.c > > @@ -1518,7 +1518,7 @@ int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) > > return 0; > > > > /* Do we need to track softdirty? */ > > - if (IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) && !(vm_flags & VM_SOFTDIRTY)) > > + if (vma_soft_dirty_enabled(vma)) > > return 1; > > > > /* Specialty mapping? */ > > diff --git a/mm/mprotect.c b/mm/mprotect.c > > index 0420c3ed936c..c403e84129d4 100644 > > --- a/mm/mprotect.c > > +++ b/mm/mprotect.c > > @@ -49,7 +49,7 @@ static inline bool can_change_pte_writable(struct vm_area_struct *vma, > > return false; > > > > /* Do we need write faults for softdirty tracking? */ > > - if ((vma->vm_flags & VM_SOFTDIRTY) && !pte_soft_dirty(pte)) > > + if (vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte)) > > return false; > > > > /* Do we need write faults for uffd-wp tracking? */ > > > Reviewed-by: David Hildenbrand Thanks, -- Peter Xu