From patchwork Sun Jan 12 15:53:47 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: 13936439 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 373E4E77188 for ; Sun, 12 Jan 2025 15:55:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5DD726B008A; Sun, 12 Jan 2025 10:55:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 58CB06B0092; 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 42DA46B0099; Sun, 12 Jan 2025 10:55:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 21EED6B008A for ; Sun, 12 Jan 2025 10:55:42 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9451746831 for ; Sun, 12 Jan 2025 15:55:41 +0000 (UTC) X-FDA: 82999250082.05.7C149D6 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf09.hostedemail.com (Postfix) with ESMTP id 02222140005 for ; Sun, 12 Jan 2025 15:55:39 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf09.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com 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=R5TeZQASt5BDinMVhrgO8a6VlnYyWJ30WOz14hCXwwQ=; b=1qB+B11b1fCyur6PWGa2vUmZd3nw+2MTZWdeZL+8PNYcOFMUg3l3WVU7F9F5cTsHTvWMDW DmPaNqtVj66I22Hq5aaS94d2G0ivEtEY3xsmA+TKYBXHYs4GU+VOIuTig+Xb+dCcYBnN6F G95SX3hC2d1gQREoVs/rtyo+BowfbE0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736697340; a=rsa-sha256; cv=none; b=fx03Dd9lHVAPtlG49moC2d8559KkmfGtg598COz8xrQOxHMnbBAqmxIenJDZLntIeUZ6z7 96Rh7L/UAB4zCJ7SW0SSS42u18VDlA3KzpJBJJzbwLh8GpPrPgitEbiwviZZe4wOZV86Q/ 9k8AAUGr+qwCIwy8CoaVH02Eo1vLcrI= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf09.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com 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-2pBh; 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 , Dave Hansen Subject: [PATCH v4 03/12] x86/mm: consolidate full flush threshold decision Date: Sun, 12 Jan 2025 10:53:47 -0500 Message-ID: <20250112155453.1104139-4-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-Stat-Signature: 64xxn63xr99poto3aza49twg668e7h4f X-Rspamd-Queue-Id: 02222140005 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1736697339-632582 X-HE-Meta: U2FsdGVkX19baBQkQ6kCuTCwD+G83izhLss53jRsn278XP/wwxhfLs7gqX69frBWfGWjOpfRZZkxmScJbwPO0Ie4odrV+Gas25g6x20a5s89fnk13RluMXPGjCwkUijEeGc75stMhG9Jw84oJgHyrGgyG2kuMhpg1oCyoH2i2aHR3M/lTDzXtAgls+P9KORD10/VNCX2HOZLOgpcKOSBBGW98gIP5bp9fuBB44d8mjJdYQPXWD4+THCbeRhoMPwvt53bNG6XHa4LV7BHiZ6M5CN1nbxF6P/MXxS1jQpJGN9CQmeWp4VtEBzf9K5Odqr8pFjkMSa/oSnxvwWBKJopgnGZgxIPnuZ9Wo5B5/TZKDoWej1ZdVzoqC9F7eaecQGNcJt/scSAb/fM/JEvIeLZd7oIGvAvds1Om4GjxTWr+Ixu2M4kKTe13zBTVXz8ngeoNcnAFTQnmStZpxzT2bqjj/3yh3rPva+ztBANq8BTVLImvBdgPR8ivCtuVEDwFBnbdrylnNzyuGDJQAlV9i2J3M3R6rRGeytogok19roWhyYP7dS9qv/szwAHBa+ZlKE47KzSBAjWbkKeT7JJy/JqEEvb2X2R/kL8cA3OeU7/4vRXLo3ysKxq0dofwwU0Ck6aUvlMxgVN7GbjABDgvvLmgfA+FQvA/mYPzz/RC/aqZkHK05VL77kjGYqSftfgnL8U6w89u7+a1Uri5kxS28PPhyWHKsAXTLJyamxxMibRS6tvJlFN50BNJZ/6Fa5p8ads5jwuWY8IZrFlI4hmsO1tdfEGouBHGcY0f3etO6IuOLG9H0E06LNI59iBQGfQ3L/eNQ5dqrUxdS5UNC0PDMzF8fV6wEypmSm7SUXE4oEFgUKcLqgwRjQNMG3C0PVU0DFWMhrvUr6Rew2Ddd+nbcs37i9fC5NId3dNN35afuuQE+zvVcsniJ6WFjsKoBI/RpQnV3cqa/u8Qdo1i+pIxvm qZ7TvIK7 INVt2CRZFgoWtpriuDabdeDgH/MPjz2dODLW8AaY4/XSvkfcA/J2+FwYXFin5Rl313FO+dbtIeinnrgsZLlhlC94cO935EJtaKHRvXRfcvkmZBTGkH55ndLQjK3XlY0wMHwbiKg2bPZ9EnqVNWEIVdSV+ujDGCls+CliiUJWZGeqpCpEzu31aQniFl4wXY1UBqZXdLRBkmIpPmlCSWKoA98DaO/45ZWFKelb3/VzH3hIW03+LvFDZdma+kLUfCCu+6Iyrkp5sgVbesu0OC/1TgvCdCEFNvEESmslpLTjv1dxl2M6f4EjVfT+Gft+0m0J/GC8UCXxTxDfl/+dHcxwYXdpGHbE+CaKSaSvyKmeR7uJGi5NBXsVmJDfMelIkTkqU5tE7Ev/68MMpmKHP4fk6rw2zRw== 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: Reduce code duplication by consolidating the decision point for whether to do individual invalidations or a full flush inside get_flush_tlb_info. Signed-off-by: Rik van Riel Suggested-by: Dave Hansen --- arch/x86/mm/tlb.c | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 6cf881a942bb..2b339f55f839 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1009,6 +1009,15 @@ static struct flush_tlb_info *get_flush_tlb_info(struct mm_struct *mm, info->initiating_cpu = smp_processor_id(); info->trim_cpumask = 0; + /* + * If the number of flushes is so large that a full flush + * would be faster, do a full flush. + */ + if ((end - start) >> stride_shift > tlb_single_page_flush_ceiling) { + info->start = 0; + info->end = TLB_FLUSH_ALL; + } + return info; } @@ -1026,17 +1035,8 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, bool freed_tables) { struct flush_tlb_info *info; + int cpu = get_cpu(); u64 new_tlb_gen; - int cpu; - - cpu = get_cpu(); - - /* Should we flush just the requested range? */ - if ((end == TLB_FLUSH_ALL) || - ((end - start) >> stride_shift) > tlb_single_page_flush_ceiling) { - start = 0; - end = TLB_FLUSH_ALL; - } /* This is also a barrier that synchronizes with switch_mm(). */ new_tlb_gen = inc_mm_tlb_gen(mm); @@ -1089,22 +1089,18 @@ static void do_kernel_range_flush(void *info) void flush_tlb_kernel_range(unsigned long start, unsigned long end) { - /* Balance as user space task's flush, a bit conservative */ - if (end == TLB_FLUSH_ALL || - (end - start) > tlb_single_page_flush_ceiling << PAGE_SHIFT) { - on_each_cpu(do_flush_tlb_all, NULL, 1); - } else { - struct flush_tlb_info *info; + struct flush_tlb_info *info; + guard(preempt)(); - preempt_disable(); - info = get_flush_tlb_info(NULL, start, end, 0, false, - TLB_GENERATION_INVALID); + info = get_flush_tlb_info(NULL, start, end, PAGE_SHIFT, false, + TLB_GENERATION_INVALID); + if (end == TLB_FLUSH_ALL) + on_each_cpu(do_flush_tlb_all, NULL, 1); + else on_each_cpu(do_kernel_range_flush, info, 1); - put_flush_tlb_info(); - preempt_enable(); - } + put_flush_tlb_info(); } /* @@ -1276,7 +1272,7 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) int cpu = get_cpu(); - info = get_flush_tlb_info(NULL, 0, TLB_FLUSH_ALL, 0, false, + info = get_flush_tlb_info(NULL, 0, TLB_FLUSH_ALL, PAGE_SHIFT, false, TLB_GENERATION_INVALID); /* * flush_tlb_multi() is not optimized for the common case in which only