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 X-Spam-Level: X-Spam-Status: No, score=-16.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2E1B0C11F65 for ; Wed, 30 Jun 2021 16:42:20 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 91D7061456 for ; Wed, 30 Jun 2021 16:42:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 91D7061456 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E8A1A8D01BA; Wed, 30 Jun 2021 12:42:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E3A3D8D01A2; Wed, 30 Jun 2021 12:42:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C65A38D01BA; Wed, 30 Jun 2021 12:42:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0054.hostedemail.com [216.40.44.54]) by kanga.kvack.org (Postfix) with ESMTP id 9D8598D01A2 for ; Wed, 30 Jun 2021 12:42:18 -0400 (EDT) Received: from smtpin37.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 63CB882E78B5 for ; Wed, 30 Jun 2021 16:42:18 +0000 (UTC) X-FDA: 78310957956.37.366A7BC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf27.hostedemail.com (Postfix) with ESMTP id D9D32700009B for ; Wed, 30 Jun 2021 16:42:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625071337; 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=+rVcCQ34SdtSSdRNC+YcxDbDrKdvz9errhFrqqjURMc=; b=invWalp8qdajxs2F03z8ZtJSy1HGQVQaVhYEkabYytsSNxflv+HNd56CDjDLb+b3ChYkgz 9miAM5zcdzDChUy5NpYb+R9VIMHUC4GaTf8sg7ol0ohL7Ztoo26r95OjowsYbYvJv6M1d9 3TQeIHuyxmom9mf4UUtdCldXtu+0Nc4= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-264-uYv5YETQM3OM7HzfXTdy7g-1; Wed, 30 Jun 2021 12:42:15 -0400 X-MC-Unique: uYv5YETQM3OM7HzfXTdy7g-1 Received: by mail-qk1-f198.google.com with SMTP id e13-20020a37e50d0000b02903ad5730c883so2068208qkg.22 for ; Wed, 30 Jun 2021 09:42:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=OiRC9BHMkJ2ZNbqNUXk2pt3f/O9/0fqo5vSlAuOz4M0=; b=Urf7+245kATvgzI+zoIQvnWEpgmSv3yxdMsCZy2BqbxKNZUUpR3xkBxNi2rsGeOcwL gK9jHWwNF27MeoEkfGxkx6ANjjOSSbyPmLFcogWgx4vkDWFYzVqs8GzgQOGn9i79ID9+ 15xQaTBrmaR0mkc41bqPibZUjr/vsjdPIFQzV2PmWvkxSuGnW3/PtULHZF/IAs8qXBYW r/E5K9AOc7DxpJW8mclavQTpb9P+dUFCLGOYWIfBCIB+1r6EryUOVj1X4cuq4QIPazuU 80wJHhaeDKMGJY/D2gZxJ+RdHJZg5ld9C7Fy/QBwf1KkIqFHZTATr1rFLI3qhNd/X/Vu DJnw== X-Gm-Message-State: AOAM5323jm0WEh9HGggW1JuCMV/jXWshq2MMa1270TuP1Qs0w7gzfkM8 u8EFgNdGq3Oufri8bnx76F3BAiHLsEB3KrSYDfl21Qs7cHbfXI7m7krWNLrkE3rfWjQJwY3dvLL Hk9UhmUpIGqQ= X-Received: by 2002:a05:620a:158c:: with SMTP id d12mr37295577qkk.42.1625071335121; Wed, 30 Jun 2021 09:42:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxDUr2MbbPQiUMcyscroHP0d5MO1KINriCL0gsYMHAWki3m1ggZtTBGGu0HBd0gImstEVx/kQ== X-Received: by 2002:a05:620a:158c:: with SMTP id d12mr37295546qkk.42.1625071334818; Wed, 30 Jun 2021 09:42:14 -0700 (PDT) Received: from t490s (bras-base-toroon474qw-grc-65-184-144-111-238.dsl.bell.ca. [184.144.111.238]) by smtp.gmail.com with ESMTPSA id j15sm5365994qki.23.2021.06.30.09.42.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 09:42:13 -0700 (PDT) Date: Wed, 30 Jun 2021 12:42:12 -0400 From: Peter Xu To: Linus Torvalds Cc: Andrew Morton , Andrea Arcangeli , Evgeniy Stepanov , kostyak@google.com, Linux-MM , mm-commits@vger.kernel.org, Peter Collingbourne Subject: Re: [patch 128/192] mm: improve mprotect(R|W) efficiency on pages referenced once Message-ID: References: <20210628193256.008961950a714730751c1423@linux-foundation.org> <20210629023959.4ZAFiI8oZ%akpm@linux-foundation.org> MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="12N2WSkLxi7kcQDE" Content-Disposition: inline Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=invWalp8; spf=none (imf27.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: mnnh9nob3e3sfrktbjjuxx5k1gh3f7g7 X-Rspamd-Queue-Id: D9D32700009B X-Rspamd-Server: rspam06 X-HE-Tag: 1625071337-514995 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: --12N2WSkLxi7kcQDE Content-Type: text/plain; charset=utf-8 Content-Disposition: inline On Tue, Jun 29, 2021 at 07:25:42PM -0700, Linus Torvalds wrote: > On Tue, Jun 29, 2021 at 6:39 PM Peter Xu wrote: > > > > And since MM_CP_DIRTY_ACCT implies "VM_WRITE|VM_SHARED" all set, above should > > be a slightly faster version of below: > > That's way too subtle, particularly since the MM_CP_DIRTY_ACCT logic > comes from another file entirely. > > I don't think it's even faster, considering that presumably the > anonymous mapping case is the common one, and that's the one that > needs all the extra tests, it's likely better to _not_ test that very > subtle flag at all, and just doing the straightforward and obvious > tests that are understandable _locally_. > > So I claim that it's > > (a) not an optimization at all > > (b) completely locally unintuitive and unreadable > > > Again, I think in all cases some more comment should be good indeed.. > > I really want more than a comment. I want that MM_CP_DIRTY_ACCT bit > testing gone. My understanding is that MM_CP_DIRTY_ACCT contains all check results from vma_wants_writenotify(), so if we drop it we'd need to have something like that to be checked within change_pte_range(), which is again slower (I have totally no idea how slow to check vma->vm_flags & VM_WRITE, but moving the whole vma_wants_writenotify here is definitely even slower). > > The only point where it makes sense to check MM_CP_DIRTY_ACCT is > within the context of "is the page already dirty". > > So I think the logic should be something along the lines of > > - first: > > if (!(vma->vm_flags & VM_WRITE)) > return false; > > because that logic is set in stone, and true regardless of anything > else. If the vma isn't writable, we're not going to set the write bit. > End of story. > > - then, check the vma_is_anonumous() case: > > if (vma_is_anonymous(vma)) > return page_count(pte_page(pte)) == 1; > > because if it's a writable mapping, and anonymous, then we can > mark it writable if we're the exclusive owners of that page. Shouldn't we still at least checks [soft-]dirty bits and uffd-wp bits to make sure it's either not dirty tracked or uffd wr-protected? Say, IMHO it's possible that soft-dirty tracking enabled on this anonymous vma range, then we still depend on the write bit removed to set the soft-dirty later in the fault handler. > > - and THEN we can handle the "ok, shared mapping, now let's start > thinking about dirty accounting" cases. > > Make it obvious and correct. This is not a sequence where you should > try to (incorrectly) optimize away individual instructions. Yes I still fully agree it's very un-obvious. So far the best thing I can come up with is something like below (patch attached too but not yet tested). I moved VM_WRITE out so hopefully it'll be very clear; then I also rearranged the checks so the final outcome looks like below: static bool may_avoid_write_fault(pte_t pte, struct vm_area_struct *vma, unsigned long cp_flags) { /* * It is unclear whether this optimization can be done safely for NUMA * pages. */ if (cp_flags & MM_CP_PROT_NUMA) return false; /* * Never apply write bit if VM_WRITE not set. Note that this is * actually checked for VM_SHARED when MM_CP_DIRTY_ACCT is set, so * logically we only need to check it for !MM_CP_DIRTY_ACCT, but just * make it even more obvious. */ if (!(vma->vm_flags & VM_WRITE)) return false; /* * Don't do this optimization for clean pages as we need to be notified * of the transition from clean to dirty. */ if (!pte_dirty(pte)) return false; /* Same for softdirty. */ if (!pte_soft_dirty(pte) && (vma->vm_flags & VM_SOFTDIRTY)) return false; /* * For userfaultfd the user program needs to monitor write faults so we * can't do this optimization. */ if (pte_uffd_wp(pte)) return false; /* * MM_CP_DIRTY_ACCT indicates that we can always make the page writable * regardless of the number of references. Time to set the write bit. */ if (cp_flags & MM_CP_DIRTY_ACCT) return true; /* * Othewise it means !MM_CP_DIRTY_ACCT. We can only apply write bit * early if it's anonymous page and we exclusively own it. */ if (vma_is_anonymous(vma) && (page_count(pte_page(pte)) == 1)) return true; /* Don't play any trick */ return false; } The logic should be the same as before, it's just that we'll do an extra check on VM_WRITE for MM_CP_DIRTY_ACCT but assuming it's ok. Another side note is that I still think the VM_SOFTDIRTY check is wrong in may_avoid_write_fault() and even in the old code (I mentioned it previously when reviewing the patch), as !VM_SOFTDIRTY should mean soft dirty tracking enabled while VM_SOFTDIRTY means disabled. So I wonder whether it should be: - if (!pte_soft_dirty(pte) && (vma->vm_flags & VM_SOFTDIRTY)) + if (!pte_soft_dirty(pte) && !(vma->vm_flags & VM_SOFTDIRTY)) However I didn't touch it up there as it may need more justifications (I feel it's okay in the old code, as vma_wants_writenotify actually checks it too and in the right way; however after the anonymous fast path it seems to prone to error if it's anonymous; I'll check later). Thanks, -- Peter Xu --12N2WSkLxi7kcQDE Content-Type: text/plain; charset=utf-8 Content-Disposition: attachment; filename="0001-mm-mprotect-Optimize-layout-of-may_avoid_write_fault.patch" >From 4fb32ad7c949d5ec6b6ea364d3388b50bf674c9c Mon Sep 17 00:00:00 2001 From: Peter Xu Date: Wed, 30 Jun 2021 12:20:12 -0400 Subject: [PATCH] mm/mprotect: Optimize layout of may_avoid_write_fault() Firstly move VM_WRITE check to be outside of !MM_CP_DIRTY_ACCT chunk, so as to make it clear that we won't accidentally set the write bit to !VM_WRITE vmas. The old logic is hard to read in that it was written in reversed logic. Put things backward by moving the soft-dirty and uffd-wp checks earlier. Make the NUMA check even earlier than those as it's a cheap check and straightforward. Make the only "return true" case to be either the MM_CP_DIRTY_ACCT (which stands for the VM_SHARED cases when write bit can be applied), or the special anonymous page when we exclusively own it. Signed-off-by: Peter Xu --- mm/mprotect.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index 4cb240fd9936..3977bfd55f62 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -40,17 +40,20 @@ static bool may_avoid_write_fault(pte_t pte, struct vm_area_struct *vma, unsigned long cp_flags) { /* - * The dirty accountable bit indicates that we can always make the page - * writable regardless of the number of references. + * It is unclear whether this optimization can be done safely for NUMA + * pages. */ - if (!(cp_flags & MM_CP_DIRTY_ACCT)) { - /* Otherwise, we must have exclusive access to the page. */ - if (!(vma_is_anonymous(vma) && (vma->vm_flags & VM_WRITE))) - return false; + if (cp_flags & MM_CP_PROT_NUMA) + return false; - if (page_count(pte_page(pte)) != 1) - return false; - } + /* + * Never apply write bit if VM_WRITE not set. Note that this is + * actually checked for VM_SHARED when MM_CP_DIRTY_ACCT is set, so + * logically we only need to check it for !MM_CP_DIRTY_ACCT, but just + * make it even more obvious. + */ + if (!(vma->vm_flags & VM_WRITE)) + return false; /* * Don't do this optimization for clean pages as we need to be notified @@ -71,13 +74,21 @@ static bool may_avoid_write_fault(pte_t pte, struct vm_area_struct *vma, return false; /* - * It is unclear whether this optimization can be done safely for NUMA - * pages. + * MM_CP_DIRTY_ACCT indicates that we can always make the page writable + * regardless of the number of references. Time to set the write bit. */ - if (cp_flags & MM_CP_PROT_NUMA) - return false; + if (cp_flags & MM_CP_DIRTY_ACCT) + return true; + + /* + * Othewise it means !MM_CP_DIRTY_ACCT. We can only apply write bit + * early if it's anonymous page and we exclusively own it. + */ + if (vma_is_anonymous(vma) && (page_count(pte_page(pte)) == 1)) + return true; - return true; + /* Don't play any trick */ + return false; } static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd, -- 2.31.1 --12N2WSkLxi7kcQDE--