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 B4B76C11F69 for ; Wed, 30 Jun 2021 00:12:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4493661D81 for ; Wed, 30 Jun 2021 00:12:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4493661D81 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 4D92A8D0161; Tue, 29 Jun 2021 20:12:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4889A8D0160; Tue, 29 Jun 2021 20:12:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2DB7A8D0161; Tue, 29 Jun 2021 20:12:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0191.hostedemail.com [216.40.44.191]) by kanga.kvack.org (Postfix) with ESMTP id 013FB8D0160 for ; Tue, 29 Jun 2021 20:12:17 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id C100418024AFB for ; Wed, 30 Jun 2021 00:12:17 +0000 (UTC) X-FDA: 78308463114.06.F0EB34C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 3E97A300009C for ; Wed, 30 Jun 2021 00:12:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625011936; 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=ZLJF9xDpvbchJ65JpLXFXZqwOW+LXIZ7K0GDx0getA0=; b=E0VX+qPc11XH5zv/MtXMif8DVIljOZ42f1C4pqWsYYrL5Tye+otbVoCjAeVaQYBlOR7ub0 T2/sL282h7aI+yLFpyjfzQKlgzD6qkfBKIQlmnHcv11XPeRHcxTTwexll5oRWEwJ75FW4B oV+qwn8TSZWmRBX9i1Sc5oQGubtq3rQ= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-3-ickVkcg1Pemy1vc-QB-7hA-1; Tue, 29 Jun 2021 20:12:15 -0400 X-MC-Unique: ickVkcg1Pemy1vc-QB-7hA-1 Received: by mail-qt1-f197.google.com with SMTP id c17-20020ac87dd10000b0290250fd339409so112000qte.6 for ; Tue, 29 Jun 2021 17:12:15 -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=ZLJF9xDpvbchJ65JpLXFXZqwOW+LXIZ7K0GDx0getA0=; b=QoSjfffKiFnVzfK2k+sKF5hc70ieRNqs4ll8qo6Q9yFIEENta3mwvqaLsdUZtjqSXa oV90kr9KBrXG9AZ4cxnBquxhkSVM3NafI2DziMJnuk6ZqR7qUezJyXqQT5mJwsK21pqy 6lh3QiiZyH3hPpEpqaMsgtLq4DRLtZWex+6BimC/g4glqNF76CqQHgu7V42tdpLNzkGO Ns+HHxpzuAWYtg5Zot+D9WR7cjeUipzhyoPhz2fFOi5covclg0zqvvNgfdMgON4dgqCh 1XP+Cc6R/Mibi8EZwRiEe/PpMDs0Vx+jtxmrVLIAd9BHZ3VoytcDzShxiN9DSommhH7b 2/pA== X-Gm-Message-State: AOAM532GtYyUonUOQtvhKwGet8iePYx5PpRPrXWFziPoCaHunw1D3NTa KO1SccaSDQXawyBrXUF8Nj3Og3rI8Fy3hLIUUMPUk3MvvJdhX+pU3RfNUTMM7JLwfBq7DHtr0tB 01nJAv4+i+5I= X-Received: by 2002:a0c:f682:: with SMTP id p2mr3415787qvn.17.1625011934670; Tue, 29 Jun 2021 17:12:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3Xw02u5+7/ZI8Ut6KPgtn1Xl4oTAPeAdeQy3o+QKzOzszcVMi3eGNJtTSkXmDaMpifPEpwQ== X-Received: by 2002:a0c:f682:: with SMTP id p2mr3415768qvn.17.1625011934502; Tue, 29 Jun 2021 17:12:14 -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 k8sm11370228qkj.46.2021.06.29.17.12.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Jun 2021 17:12:13 -0700 (PDT) Date: Tue, 29 Jun 2021 20:12:12 -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: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=E0VX+qPc; spf=none (imf03.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: rspam03 X-Rspamd-Queue-Id: 3E97A300009C X-Stat-Signature: g4xqbo9fctijk69gozuj8gww9sg5j84k X-HE-Tag: 1625011937-970992 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 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. 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