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=-5.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS 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 9B58FC11F67 for ; Tue, 29 Jun 2021 17:50:33 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1572061DD1 for ; Tue, 29 Jun 2021 17:50:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1572061DD1 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2CCA38D015C; Tue, 29 Jun 2021 13:50:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 27CC18D00F0; Tue, 29 Jun 2021 13:50:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 11DC68D015C; Tue, 29 Jun 2021 13:50:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0253.hostedemail.com [216.40.44.253]) by kanga.kvack.org (Postfix) with ESMTP id E425F8D00F0 for ; Tue, 29 Jun 2021 13:50:31 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id C9E5D8249980 for ; Tue, 29 Jun 2021 17:50:31 +0000 (UTC) X-FDA: 78307501062.06.4A8E36F Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) by imf17.hostedemail.com (Postfix) with ESMTP id 76432F000090 for ; Tue, 29 Jun 2021 17:50:31 +0000 (UTC) Received: by mail-lf1-f44.google.com with SMTP id d16so40873040lfn.3 for ; Tue, 29 Jun 2021 10:50:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=nD2K9Pxsj0l0b+r7ypwFgEO7F+ajCZLB9GSClDHGIeM=; b=Uyx5+j8RItEl6KLB44CZsNqYyz2PhNSsPvCfMzZvprRy/E5nYisXzXC5dvyWNgVW9S ZZQSymVq/fe7l1ouggcOO4WkfRm5fYqi+1doWRfBPEMg8qVg0eBxTJiF67QYJ8F0I2Cp b8H0HqcANjqJrBdKKJ2joAHMLIUnJcu4/rZSI= 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=nD2K9Pxsj0l0b+r7ypwFgEO7F+ajCZLB9GSClDHGIeM=; b=FTuOu/jaVxnlOzPr+KC3NC/qdUPa5e2A32Qf9J+g/Q7NPhFASEBxxSpEm6h4MM8eLC 9nj92mSl2hrvxjvGaqulRYBMluamgPEcnFO7qXTEvfcVQusYuaVMMbx2PQsHEPGAFPI9 S8ylHcZ6X4W0cs3tIJC+/wPuL/LPTD9xeSX3U4jFjDoj1UXnpBUIddqHSMLl3pFsTK1n KtmiIqP9f23tvzgZv7tbx8ZAG9s6FpL5TtqzfAMtKlXL9dnH2Jelx7EiN8XtKH1wLnRy Tit5zJW1LaxbNdvPH4riwTvurR2Fsnw2dOUB6JN29PMIsOkqoODUHmNYcv67JPdl81u7 UF0A== X-Gm-Message-State: AOAM53341vMBvcM3JlzLcqQcPYlbur2trUHMsh0FQucvdjrKuL9YkiGm 9oTCarq1AKhQeTogpvrS/NaKaGivNCqYWByM/1o= X-Google-Smtp-Source: ABdhPJwFAzhIBZ9MY54Ug+bUkXsN31wFjHKJ16vXq58Htq10PKjxUYYTz48XSmZbaNLm1sviE+1oaA== X-Received: by 2002:a19:4805:: with SMTP id v5mr24413273lfa.338.1624989029150; Tue, 29 Jun 2021 10:50:29 -0700 (PDT) Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com. [209.85.208.177]) by smtp.gmail.com with ESMTPSA id i17sm1439092lfu.255.2021.06.29.10.50.28 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 29 Jun 2021 10:50:28 -0700 (PDT) Received: by mail-lj1-f177.google.com with SMTP id w11so11556036ljh.0 for ; Tue, 29 Jun 2021 10:50:28 -0700 (PDT) X-Received: by 2002:a2e:a276:: with SMTP id k22mr4682986ljm.465.1624989028284; Tue, 29 Jun 2021 10:50:28 -0700 (PDT) MIME-Version: 1.0 References: <20210628193256.008961950a714730751c1423@linux-foundation.org> <20210629023959.4ZAFiI8oZ%akpm@linux-foundation.org> In-Reply-To: <20210629023959.4ZAFiI8oZ%akpm@linux-foundation.org> From: Linus Torvalds Date: Tue, 29 Jun 2021 10:50:12 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [patch 128/192] mm: improve mprotect(R|W) efficiency on pages referenced once To: Andrew Morton Cc: Andrea Arcangeli , Evgeniy Stepanov , kostyak@google.com, Linux-MM , mm-commits@vger.kernel.org, Peter Collingbourne , Peter Xu Content-Type: text/plain; charset="UTF-8" Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=google header.b=Uyx5+j8R; spf=pass (imf17.hostedemail.com: domain of torvalds@linuxfoundation.org designates 209.85.167.44 as permitted sender) smtp.mailfrom=torvalds@linuxfoundation.org; dmarc=none X-Stat-Signature: cajcbiw474tn81m5ausqjqppwkmnowem X-Rspamd-Queue-Id: 76432F000090 X-Rspamd-Server: rspam06 X-HE-Tag: 1624989031-180428 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 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. 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. 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. So please make me get the warm and fuzzies about this code. Because as-is, it just looks scary. Linus