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=-21.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,MENTIONS_GIT_HOSTING, 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 9216CC47080 for ; Mon, 31 May 2021 22:00:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0E40761287 for ; Mon, 31 May 2021 22:00:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0E40761287 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 958656B0036; Mon, 31 May 2021 18:00:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E12A6B006C; Mon, 31 May 2021 18:00:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 70C2E8D0001; Mon, 31 May 2021 18:00:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0210.hostedemail.com [216.40.44.210]) by kanga.kvack.org (Postfix) with ESMTP id 354726B0036 for ; Mon, 31 May 2021 18:00:26 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id C0ECF8249980 for ; Mon, 31 May 2021 22:00:25 +0000 (UTC) X-FDA: 78202895610.16.515C37A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 2BE2B6007BC5 for ; Mon, 31 May 2021 22:00:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622498424; 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=t46RHNDEmY4o58Po7nKS5eF2WaQvdWusm20+8SaEYgo=; b=CM/hYrqh9Eikhw23Stbu1xLi1fSr57Yqg49FNmA4HdG9kmu00r4bz32Oy0VJsA/u8BBNMc RPQbNzEv8S+DTpJyBPT9Wl1YUkMyILUv22JKM1D8wEWp/iqLp8QUAMaxs9pwwcLBgg0dlp yWU+ZarXwC+nM8nQHRtehULn3y/GnN8= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-309-0oCqoTD4P7O02b9gjU7odg-1; Mon, 31 May 2021 18:00:20 -0400 X-MC-Unique: 0oCqoTD4P7O02b9gjU7odg-1 Received: by mail-qt1-f198.google.com with SMTP id o10-20020a05622a138ab029023ee9c05f1eso968868qtk.21 for ; Mon, 31 May 2021 15:00:20 -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=t46RHNDEmY4o58Po7nKS5eF2WaQvdWusm20+8SaEYgo=; b=AFBTTDHoEF3HdTbm0osiYcIsDgFzMzFbtj7X3d4JkP6X4oShu4YZE08DHbH6DjpJN8 QEOoXe4nc1yARPdbcwBKoOJ3V48Az2B4u4evuu6t/6/izPT5UNX2m8FsZYk+W5JjilQQ M8CHus1KzogABU714m2yfkQaSn8vjF3hYpf9+5XhLR2BiO38EeYHajurRqpKtRQ5qm0V u9kZ1ahkQE6jczPY6Ll+LPdLr41KBh3XQLOmNwnsTgCnQDbqSJ/VHDxQ/cbYYXGES0pv 8IwndzzNgahl3crNBqt5Ml25M/tcQ/CHiBIHn+wd/4YmE6V26Wo1BMDvCNAeKT6N2JPF WB7g== X-Gm-Message-State: AOAM532yyhX92bHQUZHoy7Lb+aD5p7Yx6Dcoo0OSpCVAZcsoZJi46oiA 1nF0ldzdXDMEu6X3/O91AN9YrN+xW6ctT4WYCyQA+oy9i7uI6rdnBEE0cfB+rxPsPXz4gApY+Xk bWDM8v86omj4= X-Received: by 2002:ae9:e40f:: with SMTP id q15mr11813165qkc.125.1622498420150; Mon, 31 May 2021 15:00:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyLVeDVTd9L3v7WYgooRvFnXw9Eyz45oCpCSrlqvoOWh5k3mD44yrgsbdTkFPeCgNIj1hYyQg== X-Received: by 2002:ae9:e40f:: with SMTP id q15mr11813134qkc.125.1622498419831; Mon, 31 May 2021 15:00:19 -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 g5sm9824290qtm.2.2021.05.31.15.00.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 May 2021 15:00:19 -0700 (PDT) Date: Mon, 31 May 2021 18:00:18 -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: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="CM/hYrqh"; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf25.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=peterx@redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 2BE2B6007BC5 X-Stat-Signature: 3ijaoh8h5p1difii318173tjupjb97cc X-HE-Tag: 1622498413-207846 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, May 28, 2021 at 08:32:25AM -0400, Peter Xu wrote: > 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 Sorry I may need to retrieve the ACK here... TL;DR: we'd need to skip the optimization for safety when MM_CP_PROT_NUMA set The thing is that there's report showing that numa could have an issue with the other patch in Andrea's COR branch (which is a nicer one covering huge pages) if without prot_numa check there: https://git.kernel.org/pub/scm/linux/kernel/git/andrea/aa.git/commit/?id=7471fd37fc2b8780fb4a3ab9a5ee9d87752e246a Now it's still not clear whether the same issue could happen with this patch as it's using page_count() rather than page_mapcount() due to the different handling of the two branches, however to be safe and before we figure that thing out, imho it's better this patch also skips NUMA path completely. -- Peter Xu