Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp350992pxb; Mon, 25 Oct 2021 09:29:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyrUhQ1ydvxFh8SUlaZUlVOfVy0Grw9Vyo9Wgk2/DcMClHrEfEj8tmix1nSy3iAHV6lAKug X-Received: by 2002:a05:6402:4315:: with SMTP id m21mr5686184edc.304.1635179376015; Mon, 25 Oct 2021 09:29:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635179376; cv=none; d=google.com; s=arc-20160816; b=VSlcCvTsKTFAiYGOaGlqOIXOt8Zjph4JzUaTu9BW1Qd/mCxzX62jNfbl+BPUaaun09 b/hAxHF34xehRGOoq53DqOn1fF9S/m3/pTHxR5PkiMx5wowtiC+d3HU3t1B/kZY6o+Bs bVXN6zzn/iFTdoWxmRAHs6G9KhoMzuoYsNi5FfHgWtqcxsU/WHJVXbM6irJafSbrdIV/ 1rY4dBHbpheYC8bmy3AacW9mD1SPi9sTSHmU5tr8sfZDLUSs5nshgirHSLnEijF7KaR3 KE/zwtd+l1wCqNYb5kR0A1PxZou5GHwxT/E5fI/H36dO3wp8TJFQFhh+3Ug04z7Dw7SS wO7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=oI5ZgoBumkMPsdg0dKXSGfk+ipx5MuCcFI/OUoaJ4JA=; b=mwmaxPT3QwJ4kl3dkUR5xDUpotZSz3R91KPWoE2sK02+nLoEwcoV0U9Idl/tjaS0+A tKNtYx2H3krvF+dxSVTJGf4VqVpRuv/oIe4F407bstjOP/n8bOMlmUufgQ7dwSIvEFZR RzEe2HSn3IPhaBt7aVkxyMfAWkzO+pgAn93bkPu3AJcBTTxlha9JTgeR3ISxjq0OGQB2 jMnNGaTTrLKNPak8qzVZkCjYbACR5B0+tPdp7sc5IPlQ1Oopk/ZYkHnwSIgO4e6G95lM bQ2zDMNRGkjydrnaFGzMlfPBhtQcyS0e54TyVL2nKlFK8VHlbbpo89kcViHOT+AoEmTa BZ9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="fh+pd/l5"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jg5si919852ejc.71.2021.10.25.09.29.10; Mon, 25 Oct 2021 09:29:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="fh+pd/l5"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233908AbhJYQ3f (ORCPT + 99 others); Mon, 25 Oct 2021 12:29:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231258AbhJYQ3e (ORCPT ); Mon, 25 Oct 2021 12:29:34 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADD8FC061745 for ; Mon, 25 Oct 2021 09:27:12 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id h193so11461593pgc.1 for ; Mon, 25 Oct 2021 09:27:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=oI5ZgoBumkMPsdg0dKXSGfk+ipx5MuCcFI/OUoaJ4JA=; b=fh+pd/l59PDrHZYxexVQVs2VA67U04FFXQfsvO0JKI44kT9n928Ye1TdCuXKUDN4/8 cb3v3S6mIB3PVPk539SCpNxl2wtWfyGs4mPuhrW1FpdWYa0df+ovqHV8sfAzdy6JR/v5 mzRjhrUgjZKq5T1Vq7IIMwPqDJsjAlH6/omogDLPFwXDR+dOeS5/fUw1Zql6Rksxs28q lAq6ZfsD1+o9OaMYpRnZk5/rM+hQdyfVKJxZ3f0QrIRJJVe24lhnteDDqHA6odfMd+n4 jaL0p5g4Q1HkttnmcsiFu5u84ytOsidpsZc75GQgX9gW4EHoUk7x6iue63HUfUwPaMxR 3+Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=oI5ZgoBumkMPsdg0dKXSGfk+ipx5MuCcFI/OUoaJ4JA=; b=JGoQ7ge2403YzD+whqKigxx3JtY8pkVMigwLNISGYqOn35YBGKvbAP82pDg/tJOL0r XLpejy3H0F4l1U7Mm2j7YEAkCDGhWIznMrSgLxTKDv1fYu5m1x1KNzPFf/S8Ig05HuY8 OE+wEiOxkgI7jLYVZjrxSfGawtgtkGzQJfubvy5VRYwpvL9clAt4Uqp3i4P2CU3t39O/ jFqGBCbSNN8xCAd/da0Zly9jw9krzYFlPfUnjxMl+BH2bM2F0Mh4q8X3KZxLjYPxopwG rRww9GmrC9l4BQnWuuSvHOn6o9e5tKAXlJ22M9KjNEF++X/rPHgnBwqU8PmkC2/m9N0+ 8fTg== X-Gm-Message-State: AOAM532Y9JvHpqIyBSUG9iWgwDZOshPcEClCm+dIxNoFfCDDqOaSltmR lrVI7TuhzcMt/fb5KL/cFIs= X-Received: by 2002:a62:648a:0:b0:47b:e1a5:483a with SMTP id y132-20020a62648a000000b0047be1a5483amr12888524pfb.52.1635179231292; Mon, 25 Oct 2021 09:27:11 -0700 (PDT) Received: from smtpclient.apple (c-24-6-216-183.hsd1.ca.comcast.net. [24.6.216.183]) by smtp.gmail.com with ESMTPSA id u19sm5481523pfk.201.2021.10.25.09.27.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Oct 2021 09:27:10 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: [PATCH v2 5/5] mm/mprotect: do not flush on permission promotion From: Nadav Amit In-Reply-To: Date: Mon, 25 Oct 2021 09:27:08 -0700 Cc: Linux-MM , LKML , Andrea Arcangeli , Andrew Morton , Andy Lutomirski , Dave Hansen , Peter Xu , Thomas Gleixner , Will Deacon , Yu Zhao , Nick Piggin , x86@kernel.org Content-Transfer-Encoding: quoted-printable Message-Id: <94412B55-0E16-4BC1-982F-37C082A0BABD@gmail.com> References: <20211021122112.592634-1-namit@vmware.com> <20211021122112.592634-6-namit@vmware.com> To: Peter Zijlstra X-Mailer: Apple Mail (2.3654.120.0.1.13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On Oct 25, 2021, at 4:12 AM, Peter Zijlstra = wrote: >=20 > On Thu, Oct 21, 2021 at 05:21:12AM -0700, Nadav Amit wrote: >> +/* >> + * pte_may_need_flush() checks whether permissions were demoted and = require a >> + * flush. It should only be used for userspace PTEs. >> + */ >> +static inline bool pte_may_need_flush(pte_t oldpte, pte_t newpte) >> +{ >> + /* new is non-present: need only if old is present */ >> + if (!pte_present(newpte)) >> + return pte_present(oldpte); >> + >> + /* old is not present: no need for flush */ >> + if (!pte_present(oldpte)) >> + return false; >=20 > Would it not be clearer to write the above like: >=20 > /* !PRESENT -> * ; no need for flush */ > if (!pte_present(oldpte)) > return false; >=20 > /* PRESENT -> !PRESENT ; needs flush */ > if (!pte_present(newpte)) > return true; >=20 > ? I will change the comment to yours. Thanks. >=20 >=20 >> diff --git a/mm/mprotect.c b/mm/mprotect.c >> index 0f5c87af5c60..6179c82ea72d 100644 >> --- a/mm/mprotect.c >> +++ b/mm/mprotect.c >> @@ -141,7 +141,8 @@ static unsigned long change_pte_range(struct = mmu_gather *tlb, >> ptent =3D pte_mkwrite(ptent); >> } >> ptep_modify_prot_commit(vma, addr, pte, oldpte, = ptent); >> - tlb_flush_pte_range(tlb, addr, PAGE_SIZE); >> + if (pte_may_need_flush(oldpte, ptent)) >> + tlb_flush_pte_range(tlb, addr, = PAGE_SIZE); >> pages++; >> } else if (is_swap_pte(oldpte)) { >> swp_entry_t entry =3D pte_to_swp_entry(oldpte); >=20 > One question on naming, "may_need" sounds a bit washy to me, either it > does or it does not. I suppose you're trying to convey the fact that = we > ought to err towards too many TLBi rather than too few, but that's > always true. >=20 > That is, would "needs" not be a better name? The =E2=80=9Cmay=E2=80=9D is indeed intended to be clear that the = function can error towards too many TLB flushes (of any kind). For instance, in a change from (!dirty|write)->(!write), no flush is needed in theory. I was too chicken to add it, at least for now. I can change the name and indicate in the comment instead though.=