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 061BFC43334 for ; Fri, 22 Jul 2022 07:09:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F2BB86B0072; Fri, 22 Jul 2022 03:09:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EDB828E0001; Fri, 22 Jul 2022 03:09:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA2596B0074; Fri, 22 Jul 2022 03:09:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CA98D6B0072 for ; Fri, 22 Jul 2022 03:09:13 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AAD58160B3E for ; Fri, 22 Jul 2022 07:09:13 +0000 (UTC) X-FDA: 79713859386.12.C56E118 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf26.hostedemail.com (Postfix) with ESMTP id A3F18140086 for ; Fri, 22 Jul 2022 07:09:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658473752; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=W03YUh23JrP2otyQElwrVQuoO95BxhmDO5t8VZsxiZg=; b=bLk3yPBvvKLgd6AGQIW4dU/Y0UQGn4BR69eYK218orvvNbOn4gh6paDzZ57wCH+pdZaoJC FplLVAqYdkNTtoyMnMiwkfN7SS2gVOkr44ipt8qFWiph/zrW/LDHtrPGTQwMteGFrECEeG 1749bT8AOnclaIMAKrRwxoy6FmtYSPQ= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-185-tWOmq9V9O-m8FzZH3vqHWQ-1; Fri, 22 Jul 2022 03:09:10 -0400 X-MC-Unique: tWOmq9V9O-m8FzZH3vqHWQ-1 Received: by mail-wm1-f69.google.com with SMTP id bh18-20020a05600c3d1200b003a32044cc9fso2089353wmb.6 for ; Fri, 22 Jul 2022 00:09:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:cc:references:from:organization:subject :in-reply-to:content-transfer-encoding; bh=W03YUh23JrP2otyQElwrVQuoO95BxhmDO5t8VZsxiZg=; b=le72LgVZYetZZzRuH21hCtM0wZkypDikvlUGCc2KLV37rnQOXT0Wi7GIH2Dpt6/3/U OH5Ue/wly5e09TqzTu5oHoULOKO3X+zwUd46DCKDsVamxfut1r6/HMR24CHkTOg4nNlL iB64uaHmJvN9N/Wdx0LvPPqbh+bhENRZhNln88uIrzpWzfS9NBI85N/czR9D16DWs4Mj nKHHcJ0Rmvv5wotjyHISZckJvl+w+1l25fbzKwcFQciqlZN4FCoHYFy8sfSr/ZUb1Ri9 La2XjNLAMA6w9XetmvY8Zs8zG8F4thk4dDaUrOTxLBubrdZW3tM0DTdNVvsFQY5AZk/Q tQFA== X-Gm-Message-State: AJIora97EtXdP40IeWR9HrVW8C/jvWu/MCmLcba9V/BVt4/Hoqf4BIIV RqHC21rR/xzeqYSXRaE8lkEsjnJQClMgq0iWksl0tDeJeI/8quK/nci3wceNS4WvU9y65urDMQc f8eopXIjXOBQ= X-Received: by 2002:a05:600c:4fc5:b0:3a3:2d73:c147 with SMTP id o5-20020a05600c4fc500b003a32d73c147mr8316865wmq.197.1658473749585; Fri, 22 Jul 2022 00:09:09 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vNPXifmqewhxBZfVs3JuSQs7OOt8RXLQG57YuQglvjvnMqQDHZomYGAjWysqcCNy2Su6Xigg== X-Received: by 2002:a05:600c:4fc5:b0:3a3:2d73:c147 with SMTP id o5-20020a05600c4fc500b003a32d73c147mr8316827wmq.197.1658473749022; Fri, 22 Jul 2022 00:09:09 -0700 (PDT) Received: from ?IPV6:2003:cb:c707:cb00:cd6b:7964:cc7d:b0e6? (p200300cbc707cb00cd6b7964cc7db0e6.dip0.t-ipconnect.de. [2003:cb:c707:cb00:cd6b:7964:cc7d:b0e6]) by smtp.gmail.com with ESMTPSA id q5-20020a1ce905000000b003a0323463absm4039820wmc.45.2022.07.22.00.09.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 22 Jul 2022 00:09:08 -0700 (PDT) Message-ID: Date: Fri, 22 Jul 2022 09:08:59 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 To: Peter Xu , linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Nadav Amit , Andrew Morton , Andrea Arcangeli References: <20220721183338.27871-1-peterx@redhat.com> <20220721183338.27871-2-peterx@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v3 1/3] mm/mprotect: Fix soft-dirty check in can_change_pte_writable() In-Reply-To: <20220721183338.27871-2-peterx@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658473753; a=rsa-sha256; cv=none; b=gRHvhdWOufjxoW5Pl2yh/CEtqHx4of4Nzh0mzj/BUDx6tPDaKZ4iXiqC67PFHmERjhHW4w 4znrlMq2ecdTw4chiDThHNm5NrKA5eDkjRpKervl+I7SE1Df3IhX29t/eeSNt0nqAMnrAm GD5jVId5v+U9z1NSQqt011yJmViZeXA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bLk3yPBv; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf26.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658473753; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=W03YUh23JrP2otyQElwrVQuoO95BxhmDO5t8VZsxiZg=; b=QMM6OryjWlB413CKO6k2mbWey7fdW9m752K2BE5bLdV1DlbhH6zFCsce/t7cLOOtFFD4r0 z6CFCCcXGfJP5Pl0BKq+UAiy6VUuXYNJKlRz1tKfLdtABNMc8z35Pye00EcVpGhaibb1/M +/HenmW/vaaoGIh6lvynUcl51jwGOSQ= X-Rspam-User: Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bLk3yPBv; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf26.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com X-Rspamd-Server: rspam01 X-Stat-Signature: 9mr4h9cp16gaadbscyes7x88exp64syo X-Rspamd-Queue-Id: A3F18140086 X-HE-Tag: 1658473752-999409 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 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. > > 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, David / dhildenb