From patchwork Sun Jan 12 15:53:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13936443 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE66DE7719E for ; Sun, 12 Jan 2025 15:55:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F3226B009A; Sun, 12 Jan 2025 10:55:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EA0A86B008C; Sun, 12 Jan 2025 10:55:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B807E6B008C; Sun, 12 Jan 2025 10:55:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7E7276B009C for ; Sun, 12 Jan 2025 10:55:42 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 254B7120A8C for ; Sun, 12 Jan 2025 15:55:42 +0000 (UTC) X-FDA: 82999250124.12.4DCE30A Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf23.hostedemail.com (Postfix) with ESMTP id 8C7D1140002 for ; Sun, 12 Jan 2025 15:55:40 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; spf=pass (imf23.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736697340; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7/4rya3KGHNDVRGeMHkz9fXaJq1zq5GdZFEclvX/xmk=; b=EJQ3KU3zgXlrEHVwM8vg1/o8lPIWG74SurZvxBluQ1e0Mz7AZwS4+8fHcUdif9HxtnMmZS u9FuxG1UfNqQFQLBGDNH3nV2cVq2eCAIjkXwY+foxQaF5drmf7PQqlS4/sxLx5JyeUMZ4C 7dxn4/vFqD6U5KfaS2D5M3LeJMhHIww= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736697340; a=rsa-sha256; cv=none; b=vI14XZPZRJYSiFgqNn9lFy5vyPC6Iv0ssDe6xxPFhLh9H/fxMnMGqzn5mNFkUwO+KYaJOI bElYS7XsX5gZax/DmlA8COz28t6vSR2CHgaLIX7OOOsT2LB2f0WKRTBGvlMwYuNjnmoWbK hq9dzomyAqujd6D6frHwd26onbUFOFA= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; spf=pass (imf23.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none Received: from fangorn.home.surriel.com ([10.0.13.7]) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.97.1) (envelope-from ) id 1tX0Ii-0000000010W-3fnO; Sun, 12 Jan 2025 10:54:56 -0500 From: Rik van Riel To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, bp@alien8.de, peterz@infradead.org, dave.hansen@linux.intel.com, zhengqi.arch@bytedance.com, nadav.amit@gmail.com, thomas.lendacky@amd.com, kernel-team@meta.com, linux-mm@kvack.org, akpm@linux-foundation.org, jannh@google.com, Rik van Riel Subject: [PATCH v4 12/12] x86/mm: only invalidate final translations with INVLPGB Date: Sun, 12 Jan 2025 10:53:56 -0500 Message-ID: <20250112155453.1104139-13-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250112155453.1104139-1-riel@surriel.com> References: <20250112155453.1104139-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 8C7D1140002 X-Stat-Signature: upwc5y4tywef5nxuwz9udipd5fa35tx8 X-Rspam-User: X-HE-Tag: 1736697340-107169 X-HE-Meta: U2FsdGVkX1+zCnBTIqdGiQtZTGspuh7+6VZSXKH4W4L6GEq6fqtoFixRaGlbJQmCH+VpRa5DnNGm0KTK489Ap0wE0KEvJANbMGbq0O1UH2k+ZqSDZGKoP0FBfIyQ6luqU+fjl8H1GdZc79Hl0KS/KkXF+yr4h/jWmJr1fUek7S9dGopcCrBY3llFRCosmUPRhAzaj6iJXUyCsYSclF88PpdNNs+O/KuTBPkaJ2OLA08nmZZTYHjc3HJngg6Den4wFgyu/gQypzA903XTWXo9EQGFnDst3yMmT1tnYZY7MDWYPVfNfKALiPvG66/1wpvXyK6vzEWCMjrSYXW4eM0pikmbCqCHbKonH+SGnEjhWO0R6pV4uaaX/wkzCLl/vf0grlb/8l3UZEgBXAt82viVDldKNKaGE5YrluGtmsKd6JLA753n0l4sOB8CcPVMqDtz5S1cGdHjgra553vmPfBZKRN97swnLoowPE9mbCfVf8MuHUF0k5jnvV9UMFKF413f18RKVtfA8BTc+tNY8mcjpsv4ykimIAa7WL4mJKiddDeQPTVocRwpKcWGkzww7RBtoQKF8a0QbeP9wnbJKCFuHgVTNCWZVZ19gXuwYRF2EMInVl3qlFzI/wuT59N6EcX9XWXfZxn8oNI/Mw244GhSdmvdXb27fTT2vThSEwwwTHCaS3uG1z7DMQpsS0lKHJi7+5P/N3gm3pOqhRJeQl+B+wH9qQKnUfpHWiaY+BpW7lrkEl6Gy02MwEyqyOC+z5beHssblxEFoICLo17g1qTHs3fPUzs+sk6E9JsHHb6FIXloabPipcUtfmtYP+TWd8LA6LWM4Bxw74atx8ZlkIywgCvmXuB+ciKJKcsLusY8h94B8JSt+tNOw+0D7vZH/k/11pHsSZS8mn1yVtzLx9O7G8TiIqOKnFRgC30/UhQRQeZzsSQ49hSDjbvMuOaE6QE0Lfvl5nJZdkUvYBvjLZR ULWg8eYt kGs955zJkaVrmWQ317qMTz4VTtvAKxgZutTPERmYyR9Lc3WTYMt8MwVnaXlXrKe8VAuqJVO8zgB/Sse5315AcFT4B5OR9NvjjwARa6Gue/kwCIGK4FmtxdH0uX91FrlzDXyWqEGbCtvwqQNM7BCftsQMFe5nLgAgT9BhmXBPpPlXrCF1WAYrIFN0psapJ6LdCl56OFtPZKWhwLqfSQihUDE1g21K75H571wv/i+vCIuqyE0UkyyvH+IagDo8iJYZF63suK6Lc+3IxabPjecxu7M8bulqZO2ePgKkxhQaEBGPNe9KcWuWXjIRDnHmwRONV8loawVL7H8rmOtIcFMDXOgY0dH261QkMRCRu2RzodfTpVKCqFEelN76Cb3zyxeDkT1GqA6Trz0HvNjw= 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: List-Subscribe: List-Unsubscribe: Use the INVLPGB_FINAL_ONLY flag when invalidating mappings with INVPLGB. This way only leaf mappings get removed from the TLB, leaving intermediate translations cached. On the (rare) occasions where we free page tables we do a full flush, ensuring intermediate translations get flushed from the TLB. Signed-off-by: Rik van Riel --- arch/x86/include/asm/invlpgb.h | 10 ++++++++-- arch/x86/mm/tlb.c | 8 ++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/invlpgb.h b/arch/x86/include/asm/invlpgb.h index d62e3733a1ab..4fa48d063b76 100644 --- a/arch/x86/include/asm/invlpgb.h +++ b/arch/x86/include/asm/invlpgb.h @@ -61,9 +61,15 @@ static inline void invlpgb_flush_user(unsigned long pcid, static inline void invlpgb_flush_user_nr_nosync(unsigned long pcid, unsigned long addr, - int nr, bool pmd_stride) + int nr, bool pmd_stride, + bool freed_tables) { - __invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA); + unsigned long flags = INVLPGB_PCID | INVLPGB_VA; + + if (!freed_tables) + flags |= INVLPGB_FINAL_ONLY; + + __invlpgb(0, pcid, addr, nr - 1, pmd_stride, flags); } /* Flush all mappings for a given PCID, not including globals. */ diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 532911fbb12a..0254e9ebaf15 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -538,10 +538,10 @@ static void broadcast_tlb_flush(struct flush_tlb_info *info) nr = min(maxnr, (info->end - addr) >> info->stride_shift); nr = max(nr, 1); - invlpgb_flush_user_nr_nosync(kern_pcid(asid), addr, nr, pmd); + invlpgb_flush_user_nr_nosync(kern_pcid(asid), addr, nr, pmd, info->freed_tables); /* Do any CPUs supporting INVLPGB need PTI? */ if (static_cpu_has(X86_FEATURE_PTI)) - invlpgb_flush_user_nr_nosync(user_pcid(asid), addr, nr, pmd); + invlpgb_flush_user_nr_nosync(user_pcid(asid), addr, nr, pmd, info->freed_tables); addr += nr << info->stride_shift; } while (addr < info->end); @@ -1699,10 +1699,10 @@ void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, batch->used_invlpgb = true; migrate_disable(); } - invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false); + invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false, false); /* Do any CPUs supporting INVLPGB need PTI? */ if (static_cpu_has(X86_FEATURE_PTI)) - invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false); + invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false, false); } else { inc_mm_tlb_gen(mm); cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm));