From patchwork Thu Jan 16 02:30:26 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: 13941160 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 829FBC02180 for ; Thu, 16 Jan 2025 02:32:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D4EB280004; Wed, 15 Jan 2025 21:32:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8096D280002; Wed, 15 Jan 2025 21:32:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 51BEC6B0089; Wed, 15 Jan 2025 21:32:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1514A6B008C for ; Wed, 15 Jan 2025 21:32:48 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 629CAAEF78 for ; Thu, 16 Jan 2025 02:32:47 +0000 (UTC) X-FDA: 83011741974.28.C97D250 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf26.hostedemail.com (Postfix) with ESMTP id CC3B714000B for ; Thu, 16 Jan 2025 02:32:45 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; spf=pass (imf26.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=1736994765; 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=cUJMLaEKqo6CTpsFhcH1hbpSqbEySeP7dICZgiGMxI4=; b=6T6C6gcWYtoRBGfUvpoPEnzIh4yVvbcvBaeiSXpcdazmpbr6/cT/qqvnJuvGwHINAFuDBY K3sO7VTMGZHwrS3CQBCg2UydsUEhGOlVj0VxnwNC/5zUWO1t70i5siVP9Ki3DPfR2vQWbM yWc3Idwes6+gGZ/RJSICpbzPxAi1lT8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; spf=pass (imf26.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736994765; a=rsa-sha256; cv=none; b=GUoZUDl/L2Y7cm2Zs7jo0M07VO3xbF/sSn6suAX/I6nlbnjLxCVmlABI4nJmP5myogrhA0 +rOynFIZtcwiMN2onWVvC1YV76TEci9GOL0AcAPeKgAjzHMjKPP3yjRSxjnQKMVnKobYAO G5FOdV4kU7pCoM2KEEpxH73Izd8xP7Y= 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 1tYFfO-000000003nd-3AF4; Wed, 15 Jan 2025 21:31:30 -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, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel , Dave Hansen Subject: [PATCH v5 03/12] x86/mm: consolidate full flush threshold decision Date: Wed, 15 Jan 2025 21:30:26 -0500 Message-ID: <20250116023127.1531583-4-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250116023127.1531583-1-riel@surriel.com> References: <20250116023127.1531583-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: CC3B714000B X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: a6c8n6tyf4jfmnihosme1w1xyagkouq4 X-HE-Tag: 1736994765-597922 X-HE-Meta: U2FsdGVkX1+pBq+OG+gL3Sm9/O76UAI8it0/wwsoJlk9SHwFdhKBmSRyvs3itmg/gFcWdFHJogOH53YupQ2F8GRhPwnFuQpx8cw4QdtuYwQBzOWZrcyGSWx116MI5CYvEJwWSGYvzyuvCbISZ2h9FPu9HOM4cb0DuyjqssuN/iINWdPguxvyY2wqLuRDxFyKAcZsJVAG7ZMboMDra/WhxIVGmLosYor1bhkjwm0CGYdQCO40k+xhsCt5cmMjzJrZmiw9VFV8+86pUCuUfiZmnl5bUv6sLKwYvGBjIa49UHGSix1ZxUWUxqT4KQTy3ZN1U1Mlp+Io7yFNoqJNxYluNwxzbqK4LbdhorZa6jvqas1DZVPFBBFiiwYnBY6MLxHdE6FI4ymX38KRDmG9Hrlw3HlTfX+kadksMS1orGjhLa217jd6LiWrT7LUnG0AdacU5XhH9guqO6uoHHzK0vZJiJTkl6BCfIdANIaoRnTvOwgWc7CTezomoU8AhWZcIy3mvf6uq6zzCG7VhUH8o0zM1SlplTj9A38oabwmJ0TEjQPhFaJYhfhVsbEHgMTatxR+JLSLrg1y6Cc2BK8foZfgfWIt9HwunYTbH/QmstG//WPbv89639vYLFL0K5GhyLUT6UDXpTETjHbTAcuXtddvF0Q8UWZdtTV5cEtvkts+TDAPqdMXZVUMDofuTwAayeL5s0IpGZjoVN3FaqJQl5elUX7hL5ver2g0Lo1C5ziosOOqQxXH8mvJayRXMQaow5VnuTqA+4IJcQZfx7YG8GQCxejvfJgcVdY1mWLHmhLu2+EOItQHUVAM332imRej+PUuDAb4X4ZgBQlds6wZm91yUTS5jZv1Zq7rxBBNS4hc9aEaylS3Zpz+K/iM5EoCfvncedjUNGVbQ/6SkDvpUC/F6JHkUlo0YQArWCWcjuShnWtqsqTnTYxnAJeOglNTGlpNxg9cDHCqg7KKkda+cfM /viZVgs7 9rdAANTOAHT/1ENhpYOuAKzEnydTF0ljeyeimYBdLaC3gjyi1jZFTaUkJPOLstwaGvMf38SzYo/dSBrwdm3fJPrq20NKm07+mqol+rW7YiN0woHOGl+rC2CEovMuRMo2iWDPwvHi+/rADT/gs9CrnMOWOAz5SsCF77M8FV0oiHP5PdynEIiobk29jib/f0hBnuOk0lHezTmAV3JuIbFo6xfBE6391BsOtFfX+6CsdAcbmW9K2yoPLXkxWy5qWFVzlWK6itvNfcCZ57BCCx+bGUhUfr9kIiF6bhI7i+gu2ZhjviuTdMC3oNjWUdpzC3UQSSU/GuU4sll/4yWrAhPoXfe5Aj3b+fgn7LFHa5fMXDrmzFZbklDKgOvUwYbgxPVQcDSgte9BqbS53NJI7XrpEwwUSbw== 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 | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 6cf881a942bb..2f38cf95dee3 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,19 @@ 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; - preempt_disable(); - info = get_flush_tlb_info(NULL, start, end, 0, false, - TLB_GENERATION_INVALID); + guard(preempt)(); + + 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 +1273,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