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=-11.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, 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 84393C4707F for ; Fri, 28 May 2021 01:21:20 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 20879613BF for ; Fri, 28 May 2021 01:21:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 20879613BF 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 9EEBD6B006C; Thu, 27 May 2021 21:21:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D7576B006E; Thu, 27 May 2021 21:21:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 865976B0070; Thu, 27 May 2021 21:21:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0208.hostedemail.com [216.40.44.208]) by kanga.kvack.org (Postfix) with ESMTP id 55A316B006C for ; Thu, 27 May 2021 21:21:19 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id F0313180ACEE4 for ; Fri, 28 May 2021 01:21:18 +0000 (UTC) X-FDA: 78188886636.23.C0E1E50 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf14.hostedemail.com (Postfix) with ESMTP id 93880C007764 for ; Fri, 28 May 2021 01:21:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622164877; 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=629zFpv+rql8aLj0Lt+8Ebex96JZbnUkhXAM86vkWt8=; b=BtCSIQmv5kngk5hKxvei2o+kBQQywLWJE2CKuBcg1p8gNW5kzt2Ea+IO+rTEW6lQyD/hg3 41ibzMDmy3UZSwfjY8Z4/GDUbebyf+ghDsug0HV6OUtuBUvBngkdAeN2Vx8EOVzXXsiRpP 4EBH8ha8B5uwVaKWVSV1xBs9ybM63ss= 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-421-cnx3u3YTNiiW1wctHmGM0A-1; Thu, 27 May 2021 21:21:16 -0400 X-MC-Unique: cnx3u3YTNiiW1wctHmGM0A-1 Received: by mail-qk1-f198.google.com with SMTP id b203-20020a3767d40000b02903a6207bfef5so1880526qkc.1 for ; Thu, 27 May 2021 18:21:16 -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=629zFpv+rql8aLj0Lt+8Ebex96JZbnUkhXAM86vkWt8=; b=QOPnLxcLwbMczjtkdDI0rWEFfHz56lUfx3mpk97/hOinapr4iry51SivnFyBRuRWLE 7FCJPr2UUcYGs0/cwfbaNFHnY88fYtrnUHJgk8G5X+u2IWQVhkEYzy0i8PiYmcZCvkRc kPA0jsSBm8NQPar/QG9qkunlc+AnpuDMLIYlIPFUNV0pAPJjAvE4LJqyN46pNe4GTq98 sbRZslGFpEpnJcfzLGhivTZpFi7hpHABudY0WhNXyJWER6Zn4peRodL9WeRB47MXcSLR Ao0dGsYn1OxLE0s0p/AXFAdNSrkVqpj5Yfp8NRoA3yZdbbQmLrC+PrIpwecNApLbPGMg pNUg== X-Gm-Message-State: AOAM531r9jgS0IX6lMsjatCfLR/G7sdcOtvHf/d/qbjC8EKTIsWLSnmX lLr7K/01NahHK1JEIGX9Mx+GJ9MOSs3vBkObueR4+mG9i3/npFUfkquhQBfJIqYuPdApG3+sAAf IevSRorrHrOE= X-Received: by 2002:ac8:5bc2:: with SMTP id b2mr1307580qtb.220.1622164875312; Thu, 27 May 2021 18:21:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyirVDNkQZSmI8Uc8kAFMMmwLtvvhh+4nmmTch3HboDx5Rx/Vl770Fy4VJFRYbdgx9HNEnizA== X-Received: by 2002:ac8:5bc2:: with SMTP id b2mr1307563qtb.220.1622164874956; Thu, 27 May 2021 18:21:14 -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 196sm2547514qkk.130.2021.05.27.18.21.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 May 2021 18:21:14 -0700 (PDT) Date: Thu, 27 May 2021 21:21:13 -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: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BtCSIQmv; spf=none (imf14.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 93880C007764 X-Stat-Signature: 51r7f3z93sif1e6hqsdqqbxq6e41gba1 X-HE-Tag: 1622164869-460471 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 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? 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? -- Peter Xu