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=-6.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no 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 7A797C11F67 for ; Wed, 30 Jun 2021 01:39:37 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E495B61D43 for ; Wed, 30 Jun 2021 01:39:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E495B61D43 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 04B158D0164; Tue, 29 Jun 2021 21:39:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F3D4B8D0160; Tue, 29 Jun 2021 21:39:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB62E8D0164; Tue, 29 Jun 2021 21:39:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0046.hostedemail.com [216.40.44.46]) by kanga.kvack.org (Postfix) with ESMTP id B31738D0160 for ; Tue, 29 Jun 2021 21:39:34 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 851EA8249980 for ; Wed, 30 Jun 2021 01:39:34 +0000 (UTC) X-FDA: 78308683068.26.F0B7DEA Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf10.hostedemail.com (Postfix) with ESMTP id 25AA86001BB2 for ; Wed, 30 Jun 2021 01:39:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625017173; 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=SfRRElH/OVRFFfreHJ0prj1+bL496qRlGzHYuzFCC2U=; b=io2XFa1BbsW6Mu6HfoaeK5tGZg0yOD6I8Va9fR93+5doGclDxjju+ZhmPUVWDApKiuSKU1 DbCwfXmWt5I8CImvCEqItHCHY0cmEaKuPIw0f0iO/yFiD/y1DNaKrFCwGv8KGOTivS9/jc xuZGpFkpYiCUVTLDhWaNpJvN5NmgZpQ= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-192-c0vrgBlpMO-GzbEBTUTxDg-1; Tue, 29 Jun 2021 21:39:32 -0400 X-MC-Unique: c0vrgBlpMO-GzbEBTUTxDg-1 Received: by mail-qv1-f70.google.com with SMTP id kj25-20020a0562145299b02902883c3c1b4bso276177qvb.18 for ; Tue, 29 Jun 2021 18:39:32 -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=SfRRElH/OVRFFfreHJ0prj1+bL496qRlGzHYuzFCC2U=; b=kjtxnnj/X0kh8enTgG30hGujykdiHXorFc+29XY5HVkUIbV3FjfxBMHjEclXzKuU3K oyNTNjydBO60jcAWgQANP/tC9uHnhrmJ4QrtjwSIQn9c7IQmlBOVKSNHOAMnLOT2vC6J OydH+xikDfHzH2SFZ9UfFYkOFvpxq2ix+Ir0cgFmaY6hrmruxe5s7dB7reZSblpB4+2O a4cGM+/RTqhPeHSk0YlLT3rDeMdGda7r4TpiXitnNfkjn7qBZ7oo8eBzi7Co1vXYqrqa 4atf5BHwWE7KNo6mt28ZHUCiy1wTJkhanjrPLemBVVpbJ3csYll86V4k50w5mZt6+7Yn GM4A== X-Gm-Message-State: AOAM533LToK7Yj5E4DAN2cdJWlcVKFuzwXC3fQjjWPdiG/BIH1zRj8jN +9gQeQJh/vC8twxB0B6y9qSBHPsEQT1j7LPOZjNanWmPzh26n43s91pn1DJ0ICf1rWYB3I4RJCz 9wGAf1yn8BfM= X-Received: by 2002:a37:b1c3:: with SMTP id a186mr34548480qkf.17.1625017171689; Tue, 29 Jun 2021 18:39:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzFin9uavRfX45sZNHUPPhepSFPS8/dlGO5ahrhOtBjd4gNJkcSj3hOxZZPP+HpCyFGhu4fcg== X-Received: by 2002:a37:b1c3:: with SMTP id a186mr34548465qkf.17.1625017171411; Tue, 29 Jun 2021 18:39:31 -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 e6sm8002347qkg.12.2021.06.29.18.39.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Jun 2021 18:39:30 -0700 (PDT) Date: Tue, 29 Jun 2021 21:39:29 -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: text/plain; charset=utf-8 Content-Disposition: inline Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=io2XFa1B; spf=none (imf10.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: wehbjgaut445hxybcjmraiyans37597s X-Rspamd-Queue-Id: 25AA86001BB2 X-Rspamd-Server: rspam06 X-HE-Tag: 1625017173-29311 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 Tue, Jun 29, 2021 at 08:12:12PM -0400, Peter Xu wrote: > On Tue, Jun 29, 2021 at 10:50:12AM -0700, Linus Torvalds wrote: > > On Mon, Jun 28, 2021 at 7:40 PM Andrew Morton wrote: > > > > > > > > > - /* Avoid taking write faults for known dirty pages */ > > > - if (dirty_accountable && pte_dirty(ptent) && > > > - (pte_soft_dirty(ptent) || > > > - !(vma->vm_flags & VM_SOFTDIRTY))) { > > > + if (may_avoid_write_fault(ptent, vma, cp_flags)) > > > ptent = pte_mkwrite(ptent); > > > - } > > > > Hmm. I don't think this is correct. > > > > As fat as I can tell, may_avoid_write_fault() doesn't even check if > > the vma is writable! > > > > Am I misreading it? Because I think you just made even a shared mmap > > with "mprotect(PROT_READ)" turn the pte's writable. > > > > Which is a "slight" security issue. > > > > Maybe the new code is fine, and I'm missing something. The old code > > looks strange too, which makes me think that the MM_CP_DIRTY_ACCT test > > ends up saving us and depend on VM_WRITE. But it's very much not > > obvious. > > vma_wants_writenotify() checks first VM_WRITE|VM_SHARED, otherwise > MM_CP_DIRTY_ACCT will not be set. While for anonymous vmas the newly > introduced may_avoid_write_fault() checks VM_WRITE explicitly. Sorry, this statement is unclear. It's not about anonymous or not, it's just that a hidden check against VM_WRITE is there already.. Say, below chunk of the patch: 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 (page_count(pte_page(pte)) != 1) return false; } Should be the same as: if (!(cp_flags & MM_CP_DIRTY_ACCT)) { if (!vma_is_anonymous(vma)) return false; if (!(vma->vm_flags & VM_WRITE)) return false; if (page_count(pte_page(pte)) != 1) return false; } And since MM_CP_DIRTY_ACCT implies "VM_WRITE|VM_SHARED" all set, above should be a slightly faster version of below: /* This just never trigger if MM_CP_DIRTY_ACCT set */ if (!(vma->vm_flags & VM_WRITE)) return false; if (!(cp_flags & MM_CP_DIRTY_ACCT)) { if (!vma_is_anonymous(vma)) return false; if (page_count(pte_page(pte)) != 1) return false; } It's just that we avoid checking "vma->vm_flags & VM_WRITE" when MM_CP_DIRTY_ACCT set. Again, I think in all cases some more comment should be good indeed.. > > Agreed even if it's checked it's not straightforward. Maybe it'll be a bonus > to have a comment above may_avoid_write_fault() about it in a follow up. > > > > > And even if I _am_ missing something, I really would like a very > > obvious and direct test for "this vma is writable", ie maybe a > > > > if (!(vma->vm_flags & VM_WRITE)) > > return false; > > > > at the very top of the function. > > Yes looks okay too; I think using MM_CP_DIRTY_ACCT flag has a slight advantage > in that it checks VM_WRITE only once before calling change_protection(), rather > than doing the check for every pte even if we know it'll have the same result. > However it indeed hides the facts deeper.. > > > > > And no, "pte_dirty()" is not a reason to make something writable, it > > might have started out as a writable mapping, and we dirtied the page, > > and we made it read-only. The page stays dirty, but it shouldn't > > become writable just because of that. > > I think the dirty bit checks are only to make sure we don't need those extra > write faults. It should definitely be based on the fact that VM_WRITE being > set already. > > Thanks, > > -- > Peter Xu -- Peter Xu