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=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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 4BBD0C4707F for ; Fri, 28 May 2021 01:35:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C582B613B6 for ; Fri, 28 May 2021 01:35:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C582B613B6 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B23916B006C; Thu, 27 May 2021 21:35:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ACED76B006E; Thu, 27 May 2021 21:35:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 616898D0001; Thu, 27 May 2021 21:35:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0100.hostedemail.com [216.40.44.100]) by kanga.kvack.org (Postfix) with ESMTP id 1C8F06B006C for ; Thu, 27 May 2021 21:35:55 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id AD6C4841F for ; Fri, 28 May 2021 01:35:54 +0000 (UTC) X-FDA: 78188923428.24.75B562A Received: from mail-il1-f170.google.com (mail-il1-f170.google.com [209.85.166.170]) by imf27.hostedemail.com (Postfix) with ESMTP id 03FCF801C7C5 for ; Fri, 28 May 2021 01:35:45 +0000 (UTC) Received: by mail-il1-f170.google.com with SMTP id h15so2007487ilr.2 for ; Thu, 27 May 2021 18:35:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Lpakr0cnC0MQEKU3spqeJsUDCLdaebDPcSQylJFkC8M=; b=vtXewbQfZNREJrre1A5TG7WEpfz/BY2ySMh9PTB7qV6gVZJOB/FELZtDSE/qtt62C+ zrIxALjuTSYjjZhINbt4n3JBDtro91sXS+pS5HkleSdolhWHPujlNmvQUn/jdSYzeMZB xJIPYhfaPrjD4hoBPu2GAkwLki3THDq3tB0zW5NIVvHs3Xo/wTAMw+GdF3soQSDquHvr 3x1WgQZ8sX6QbtIS7W030+YpbtjI/2L4F697lQOkcedMaesMfRvnE1COcMhmjHQe3B4Q nJFuizGFZ2a4iS9MLmjpTOm5gz0ZbidGRBO9eKSZr6kKvMXk89KCG374S4sOoV1SPM6J FPIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Lpakr0cnC0MQEKU3spqeJsUDCLdaebDPcSQylJFkC8M=; b=LoncuUOZDC4Ua4mGfKSKaJNbVOBe74BQaLZephluySiSyWE0LqALyNO/kpM/7lrKo7 Lq8fn8M7fC1bIMLH+A3z+LaZpDaK3ZVOnS7GazdevID4PUkveJ2laLlwFyqFaMS5wGAs 8hL3/d3jQvYmWmpP2TBuVPEPOzqufVpvmTVyWMZFkZRjBR04nLCeQuMze8qZrT/99Dhd tQKdpq2o4nAnIG1qQ16IqA7Kzcl9/QdAmQUHN5Sp2dqWM8x1/BT+p6kUxETTP9pQP8+x kyCRu6oaURxDdLpHOmnTmiPWfoHtEcAoPbN+PMYn7PQ+AFIoAsXNPVzMFt7vic/wndqC thUw== X-Gm-Message-State: AOAM530pWzJ3o7idzIIp0nwqoLpf5TlSt70d0rWUwHITd0JSfDZubwqY jEcp8qO0oGuFV461nXDXQP/Ak1fw1mkW2YCCmmknMg== X-Google-Smtp-Source: ABdhPJzXZFgBkzgKIaAiLefL4CuxMqsyMf/GE4FVoclYvRXQdGin034gvtd8zfLNDxT0ytNxovUx1a+JZCB8Q7/JBNo= X-Received: by 2002:a05:6e02:ea5:: with SMTP id u5mr4014162ilj.28.1622165753617; Thu, 27 May 2021 18:35:53 -0700 (PDT) MIME-Version: 1.0 References: <20210527190453.1259020-1-pcc@google.com> In-Reply-To: From: Peter Collingbourne Date: Thu, 27 May 2021 18:35:42 -0700 Message-ID: Subject: Re: [PATCH v4] mm: improve mprotect(R|W) efficiency on pages referenced once To: Peter Xu Cc: Andrew Morton , Kostya Kortchinsky , Evgenii Stepanov , Andrea Arcangeli , Linux Memory Management List Content-Type: text/plain; charset="UTF-8" Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=vtXewbQf; spf=pass (imf27.hostedemail.com: domain of pcc@google.com designates 209.85.166.170 as permitted sender) smtp.mailfrom=pcc@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 03FCF801C7C5 X-Stat-Signature: kcckin9sum936kkrapu3zhyi8j6ou1p8 X-HE-Tag: 1622165745-198901 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 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. > 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. Peter