From patchwork Thu Jan 23 04:23:22 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: 13947932 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 D4B19C02182 for ; Thu, 23 Jan 2025 04:28:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 698646B0096; Wed, 22 Jan 2025 23:28:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 622BE6B0098; Wed, 22 Jan 2025 23:28:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C2DD6B0099; Wed, 22 Jan 2025 23:28:40 -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 2CD2F6B0096 for ; Wed, 22 Jan 2025 23:28:40 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DC14D140B38 for ; Thu, 23 Jan 2025 04:28:39 +0000 (UTC) X-FDA: 83037435558.20.3C5E056 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf24.hostedemail.com (Postfix) with ESMTP id 50EC8180003 for ; Thu, 23 Jan 2025 04:28:38 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; spf=pass (imf24.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=1737606518; 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=Jc2TQoSL3j7v6SJaeBe+lsW7ecYjzyMkuVMBWMOAINM=; b=BXl3pTjxizY8eXot+nXQg84mmC58NZsLJt6HuDP9pAkMBJgD4t0a+SLSRX2uiz9PY+r4hf UYd2djrb21sOlSjDYVvGELvefwN7+cCYdhrp7OXbnbBbpOuXTj2vsDrTvQzZ/DjeGdU2Yp zo+efibts/ZoVK6BezKo/bTB3Zo63HI= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; spf=pass (imf24.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=1737606518; a=rsa-sha256; cv=none; b=3gywqNr3MVBXmGgCtV6srS8qaHVXNztKjmEIU2VESIbKzPXLwGZ9+JZh9LFJnQJM3zoble OC8C2uc+hS8O19zYahX9r63v5uun5OMjEV/5eryIyK6Sl9imhZG47HijXR5D25+zqsjLX4 xytN4WheOokvrVrNK9gqhgJuaWxzTtc= 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 1taols-000000005uH-2fkG; Wed, 22 Jan 2025 23:24:48 -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 v7 03/12] x86/mm: consolidate full flush threshold decision Date: Wed, 22 Jan 2025 23:23:22 -0500 Message-ID: <20250123042447.2259648-4-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 50EC8180003 X-Stat-Signature: pgek5hf1d1h5syoijmc1wm1y9j78w18w X-HE-Tag: 1737606518-11401 X-HE-Meta: U2FsdGVkX1/UpGTxgKd5fC2bzprq+inWXoPbYlpf5tXUoJxtkTn02W0MReFL9PY7dLvjoixnwW9flnsza273WlA4msrfGHKdnfdKase4w6DwL2xmWI9fdvE7dIhoiUp7CYOueDY06LcD4veERQdAzopr89lCeva8v3f8N7Re0FqoSmWvAYj+QSkR3UI5GpVsOI9KbSS/ptH9YoIk1V0Pk3zb2RSBbfRBwyDNG9fTqiwrx/33rsFo2AqWCFAiMAE75e9Hu2KjVDU8QV9JQQYZj8ow7Lh9X/zNFacfZSG3eAVY5LSbfobv2SSxqlgA5PGFD5LCfUK7MjhCvf1ms7N9vD2KItSNpCv9iHUTYPtGD5ONyGjXposU3ojW/wpDm4iWrio2U8glV8K6TSavMARwcCZjhgvLdHojMTN9uIfbk0wa7BhdNEY2O/qUhbgKeDF3AFkSGKC5LO1mKlCluGm5E9RiBn26so0OPsrmVZKS0zPadPT0130GO59T+m2ElaJlt/vlgTor4i9S+TGw6Ef/Pn/OKdcCZu3BIq14I9LaWHqmN9nZg+n2C4AtbBeuCZIo4VCKcI6po9s23Cuw3QdtLLEd2hqXngEY8xgmq7nXNakucvDhp97M/lJapnmHKQ2mWKrqgRKaZ0bVYquzQtV69pHNVbiylpcE+kmqxl0XJ8urLMl1PF0+ksic4NHuPvEz7ywNAdJ+R9Oz1krxsOa5ROf1+OtBP3m3eDfprWYpiJiJH8GF5kPyaCFUisEQhoqg0Rxuh2bno2Q3NrGqjGD2QXgE5JBscKYnFfK/V0Mdpixz+orEDs2gDY4hOucjKjyi+dTAuxYBS8crJSSAc/35Qw4qRpt6TpT2QQhitP8KUkSumKkcmZcyF/ywJ5Q2NOL79Sj0/mTwf3SeuBZXv3ZUPA1sMqUc1+U2Y106U87o69o/5unt7KQJDbX5yIDdeAwy43NAheY+yXbYkuBISzH KhQQs9Sm 1DJM0CBxzDZg3KXO7BDJZw1P8fQwGzhXf58LH77uC730H/fGYw4oVNJO2VwJZwb8NZatVNaTtlokly5Psf7akByuP8gY2bPC+pOE6mmQzqfDEvWJ236bxXz1eP3vI2kTmwoOvUaZiCBEH78CipC5V8iZEp/u00iR7hE0sBhvAWwvP84AtGI7wrzUl5FyBsOfjPzlrb94SjWQV3RgpXGytlYXKnDhG8Kr2Z+L/NaVADRGSFEhQ5M/ShIyLGiPL+0XPUA50AywN7t1VYDDBs2h+xfC4y4f0T6AFuAHBFNrb612TWrLZriE4v3ZhVFwzOyEPn1AtDmx1f6CKO5SeGUbYd6Qd+8UtcfnDzN/raR/CFVVAWIjNKEa5i0kGb1VGxsmxZdgQ3rVr6FK9lA9rvNxW6NB4Yw== 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..4c2feb7259b1 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 (info->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