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.1 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 451EFC47087 for ; Fri, 28 May 2021 12:32:33 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C0F25611C2 for ; Fri, 28 May 2021 12:32:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C0F25611C2 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 3C5136B006C; Fri, 28 May 2021 08:32:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 374906B006E; Fri, 28 May 2021 08:32:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CA056B0070; Fri, 28 May 2021 08:32:32 -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 DCB536B006C for ; Fri, 28 May 2021 08:32:31 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 818D0B7A6 for ; Fri, 28 May 2021 12:32:31 +0000 (UTC) X-FDA: 78190578102.14.326C3E5 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf29.hostedemail.com (Postfix) with ESMTP id D7D023964 for ; Fri, 28 May 2021 12:32:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622205150; 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=YSBxCNcRFOaXQ/Ztw2KfbXj0IYRHagcWjSy8nAU680s=; b=H6GcWX9xrjY0BM/wUSMzSEvwAQzLi3zIJ3nmUEPv944d56Mk8YZPp64adfDH8Po8CYnUKm 1vrdWnqCvgSTol7hWDTfTg8CvmzqDb1fgBqbldJQ7564QQ3512x7QM+ko6/01RefucyfZh 6B0d4ZrpHpDGy5RIHQ54sU1kngaX3dc= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-514-Hg8z3sPuPEOf7-XNktXIwg-1; Fri, 28 May 2021 08:32:29 -0400 X-MC-Unique: Hg8z3sPuPEOf7-XNktXIwg-1 Received: by mail-qv1-f72.google.com with SMTP id a29-20020a0ca99d0000b02901ec0ad2c871so2601431qvb.0 for ; Fri, 28 May 2021 05:32:29 -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=YSBxCNcRFOaXQ/Ztw2KfbXj0IYRHagcWjSy8nAU680s=; b=PPXhwB4MmXw4UBtvAl7CIyNp4vGPg9WvLBWAnw/gE+2nqEEpby1pwOj6F5rqmcvG5A 5s+NjSP5cS80EEI3MeAW0AfGgZeBUC8t/1EXPmmZRRQVlurrofpDsfN74IsiUlcH1ZNE BD3mNeZzt1VNxizTv4Ep8TiPR7r1Bbmj2/3BeL55kA9j7Ox76UJV1iheMfKGt4CRCfwI wHdaiJkou/0Rg+Txwdl4RIHs5WfNe0kNWrMqrr/mDygaZ5m49Qt0xju3u8wLva4+zj9f Xb71atOn35f1L5NzuHrOUsUyz2fqTwafxy6eRyFtKcVSfP7BZEV4X22ptJ9JPBBm7z1V 1XfQ== X-Gm-Message-State: AOAM533WIC62lrXEr3lfHxl+cQkiWX7wI+53HnaQx7y8Js2y+ulzfYL6 hFWaDxvPV2HVqzzPXLfFYsO3KGMkSAvG6WAKeHgdUuA8GZOj/82PJvouwnAlA8fo8L3ZnbWiYZI h/Ih/BEQXMUE= X-Received: by 2002:ac8:570a:: with SMTP id 10mr3204136qtw.360.1622205148684; Fri, 28 May 2021 05:32:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwzWr6fFh7PKApKX8bwPePTsDGYs9qIzqYlx8Ox0fWr6WDcfXS/UABQZCR96TrzixTDsEsx1A== X-Received: by 2002:ac8:570a:: with SMTP id 10mr3204116qtw.360.1622205148333; Fri, 28 May 2021 05:32:28 -0700 (PDT) Received: from t490s (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id p3sm3205347qti.31.2021.05.28.05.32.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:32:27 -0700 (PDT) Date: Fri, 28 May 2021 08:32:25 -0400 From: Peter Xu To: Peter Collingbourne Cc: Andrew Morton , Kostya Kortchinsky , Evgenii Stepanov , Andrea Arcangeli , Linux Memory Management List Subject: Re: [PATCH v4] mm: improve mprotect(R|W) efficiency on pages referenced once Message-ID: References: <20210527190453.1259020-1-pcc@google.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 Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=H6GcWX9x; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf29.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com X-Stat-Signature: rwqksxx8nfnwif1x6gw5a8d71e1of1ih X-Rspamd-Queue-Id: D7D023964 X-Rspamd-Server: rspam02 X-HE-Tag: 1622205139-109858 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 Thu, May 27, 2021 at 06:35:42PM -0700, Peter Collingbourne wrote: > On Thu, May 27, 2021 at 6:21 PM Peter Xu wrote: > > > > On Thu, May 27, 2021 at 04:37:11PM -0700, Peter Collingbourne wrote: > > > On Thu, May 27, 2021 at 2:41 PM Peter Xu wrote: > > > > > > > > Peter, > > > > > > > > On Thu, May 27, 2021 at 12:04:53PM -0700, Peter Collingbourne wrote: > > > > > > > > [...] > > > > > > > > > +static bool may_avoid_write_fault(pte_t pte, struct vm_area_struct *vma, > > > > > + unsigned long cp_flags) > > > > > +{ > > > > > + if (!(cp_flags & MM_CP_DIRTY_ACCT)) { > > > > > + if (!(vma_is_anonymous(vma) && (vma->vm_flags & VM_WRITE))) > > > > > + return false; > > > > > + > > > > > + if (page_count(pte_page(pte)) != 1) > > > > > + return false; > > > > > + } > > > > > > > > Can we make MM_CP_DIRTY_ACCT still in charge? IIUC that won't affect your use > > > > case, something like: > > > > > > > > /* Never apply trick if MM_CP_DIRTY_ACCT not set */ > > > > if (!(cp_flags & MM_CP_DIRTY_ACCT)) > > > > return false; > > > > > > > > The thing is that's really what MM_CP_DIRTY_ACCT is about, imho (as its name > > > > shows). Say, we should start to count on the dirty bit for applying the write > > > > bit only if that flag set. With above, I think we can drop the pte_uffd_wp() > > > > check below because uffd_wp never applies MM_CP_DIRTY_ACCT when do > > > > change_protection(). > > > > > > I don't think that would work. The anonymous pages that we're > > > interesting in optimizing are private writable pages, for which > > > vma_wants_writenotify(vma, vma->vm_page_prot) would return false (and > > > thus MM_CP_DIRTY_ACCT would not be set, and thus your code would > > > disable the optimization), because of this code at the top of > > > vma_wants_writenotify: > > > > > > /* If it was private or non-writable, the write bit is already clear */ > > > if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) > > > return 0; > > > > > > IIUC, dirty accountable is about whether we can always apply the > > > optimization no matter what the ref count is, so it isn't suitable for > > > situations where we need to check the ref count. > > > > Ah I see.. Though it still looks weird e.g. the first check could have been > > done before calling change_protection()? > > > > diff --git a/mm/mprotect.c b/mm/mprotect.c > > index 96f4df023439..9270140afbbd 100644 > > --- a/mm/mprotect.c > > +++ b/mm/mprotect.c > > @@ -548,7 +548,8 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev, > > * held in write mode. > > */ > > vma->vm_flags = newflags; > > - dirty_accountable = vma_wants_writenotify(vma, vma->vm_page_prot); > > + dirty_accountable = vma_wants_writenotify(vma, vma->vm_page_prot) || > > + (vma_is_anonymous(vma) && (vma->vm_flags & VM_WRITE)); > > vma_set_page_prot(vma); > > > > change_protection(vma, start, end, vma->vm_page_prot, > > > > Would something like this make the check even faster? > > That still doesn't seem like it would work either. I think we need > three kinds of behavior (glossing over a bunch of details): > > - always make RW for certain shared pages (this is the original dirty > accountable behavior) > - don't make RW except for page_count==1 for certain private pages > - don't optimize at all in other cases > > A single bit isn't enough to cover all of these possibilities. Yes I guess you're right. > > > Meanwhile when I'm looking at the rest I found I cannot understand the other > > check in this patch regarding soft dirty: > > > > + if (!pte_soft_dirty(pte) && (vma->vm_flags & VM_SOFTDIRTY)) > > + return false; > > > > I'm wondering why it's not: > > > > + if (!pte_soft_dirty(pte) && !(vma->vm_flags & VM_SOFTDIRTY)) > > + return false; > > > > Then I look back and it's indeed what it does before, starting from commit > > 64e455079e1b ("mm: softdirty: enable write notifications on VMAs after > > VM_SOFTDIRTY cleared", 2014-10-14): > > > > if (dirty_accountable && pte_dirty(ptent) && > > (pte_soft_dirty(ptent) || > > !(vma->vm_flags & VM_SOFTDIRTY))) > > > > However I don't get it... Shouldn't this be "if soft dirty set, or soft dirty > > tracking not enabled, then we can grant the write bit"? The thing is afaiu > > VM_SOFTDIRTY works in the reversed way that soft dirty enabled only if it's > > cleared. Hmm... Am I the only one thinks it wrong? > > No strong opinions here, I'm just preserving the original logic. Yeah no reason to block your patch. I'll think about it. It's just that the 1st !MM_CP_DIRTY_ACCT check looks very hard to follow, however indeed I don't have any better idea to rewrite it. Then the patch looks good to me: Reviewed-by: Peter Xu Thanks, -- Peter Xu