From patchwork Mon Jan 20 02:40:11 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: 13944704 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 B1C93C02187 for ; Mon, 20 Jan 2025 02:42:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1884F6B0088; Sun, 19 Jan 2025 21:42:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C8185280007; Sun, 19 Jan 2025 21:42:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C78D6B0093; Sun, 19 Jan 2025 21:42:30 -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 0000A6B008A for ; Sun, 19 Jan 2025 21:42:29 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B0C8DC2650 for ; Mon, 20 Jan 2025 02:42:29 +0000 (UTC) X-FDA: 83026281618.04.39BAA89 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf08.hostedemail.com (Postfix) with ESMTP id 34120160002 for ; Mon, 20 Jan 2025 02:42:27 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf08.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737340948; a=rsa-sha256; cv=none; b=TrWgU6WVZ6CMSanrIpxqKL34g8Er3mB1YkRd1TyHjbaJff10sLzcLCUdoeOYKFhS7D/qR5 RWorjeexcBL9i2cEiWr2okyM08j6vf7ixb/cOjnFCj+O3RCLTPKf1oAbsISWFvG61tctf+ jBAIhN3+bMa6RVTwdKO6Kla9734ZRQM= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf08.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=1737340948; 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=AZRfUxz70VjnkIJaLe2OCdU+ZICtTqCUnZJDXWZXOLAv+wPm3OFZ+qzungqmkeOyH8XzBq pWM0XmlEAVdBELsWE3YqVD8h4npFFzrBncZY2nOq/8h7O1hCuguE1fyUvzn1bqpymyOF9A cNscAF8qE+/Aloyw68uTwTkvwDSClBI= 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 1tZhis-000000002w5-0qpL; Sun, 19 Jan 2025 21:41:06 -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 v6 03/12] x86/mm: consolidate full flush threshold decision Date: Sun, 19 Jan 2025 21:40:11 -0500 Message-ID: <20250120024104.1924753-4-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250120024104.1924753-1-riel@surriel.com> References: <20250120024104.1924753-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 34120160002 X-Stat-Signature: e8hhruetb936nfh1eafgihy8icmusnm8 X-Rspam-User: X-HE-Tag: 1737340947-386713 X-HE-Meta: U2FsdGVkX1/HGPOBavUETtYY5S0Y0AMet/H1ur8seOB18RTNyMALoLpzOW0OA7kwJmBM4nnEg9Cb/on8NhPlc2twSrmwnZnwC1nOhzzwIuzDML1KJkSt9nVt2W6AWgtMHWsC7OJ9UPgxAOuAYc6JgnBwFxYSmVoKeHAUKS7n3E/Jv4vfjyI9wNvLbg3P3Uttv93J5klERTr2WEovgkRA44kT/rRyNIc4GBwxV+3A0E+jm8fUTNZ//vZebivAF2XXza5Q+Dg4WRN5ninIoh2dQpCn7r0+zVapI6uCBQQlrxPG5wQn0Jjb9UCo0ysdPgqZnDXcvJQyWAxzTmmQrADLU8P2UA4TYY/t1aTtff9WdXw4r4cjp8IVnQcS7ADFAFQFg0F7bf9lEI6S9N7DeXKPkQbN7fxIaAuDBQ/LwY79DO92mwPeoZPjniE2m27nocU9LwNwB7xMDHelp3+PoEwmicmqiGvONwTpBZI1ooUKlBdxk+A/etn8aHa37b71/WKca97xFYb8SM/RlQHWpY9TUs/6hWZGfUnRIrNXnEAYTql2OJnFXnOzEyboMhZHy8fAsJqkBb8+d3aLa5CERuIEc1cs3UBgTfKsXaM4+uSyLdY/oVqvf69QhfEm7SrZ8SLoulGZ63mcEmLqj9YbvlKQlXd0tXialDOWs/kLlGrOzW6ejaE5q4whoRhEkNHaOAWkSAXR+5u3FTCHr7b1hTaWeGHf25YCtKhj2MVVhp8qfoUrh1qNIs8xMTZkyqjGnbgQsM5dd3JZSrzqvgP5yK4BJo9Q+xzBfsKA1VXA9ey9SwrPQMx5isxb2m3v+oPSCxP8P+Vo/Iv+Oco9RsaSs+sfgoy8NTuBdM3sVYpGWaAI911S13xugUISI/QfjKgrHcssp/2oGWr4hBjvudcqXwU8IaTqJbzUxn1nEisvYmwSyI4F5OV8VOd+u7lc37rmkeoeUZ7LxZzYaCeLUsJ/gne HdgJdPks fwemmcyptugpwJxHbwtN/quVfmMbp5JhjvZAlBaZ+iGyxjEvwdil2GNmeVxH+1x3S88ZML/kLNb4R/GV3oHf7ZiuuWLaaaB5magJZxdZwXJ5cCegMkkd5vg9wUuYfLV9sVhfxnpqLPS9go+iWUWbNQJLUFMkbaq701qcJ4Z877Q3gpl49g+jZm6MdlhLScY7LO9Jpt4whquERIoso3gvxj81BivdBf7A38TSbmwmsBmusnOOgBaJpxPoNhclp6lm2WcVbggfhUdxGgyJ6jgDFczRL2NeKqKASv2zX9UER1ljU4TvJyvGe7wlVH2cEOiQB7Bq/39c19f+yFfj6tcJp1f7TODYzdzM4dr70o/+KppGzQIDcHRbFbqXjQyjf46QR97OL7SXDAMILkkw2vNmlgn7feQ== 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