From mboxrd@z Thu Jan 1 00:00:00 1970 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 D3378C021B2 for ; Tue, 25 Feb 2025 20:23:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 66B3B280008; Tue, 25 Feb 2025 15:23:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 61B62280007; Tue, 25 Feb 2025 15:23:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E2DB280008; Tue, 25 Feb 2025 15:23:23 -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 324BB280007 for ; Tue, 25 Feb 2025 15:23:23 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id AAD40C011C for ; Tue, 25 Feb 2025 20:23:22 +0000 (UTC) X-FDA: 83159591844.05.A6740D6 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf13.hostedemail.com (Postfix) with ESMTP id E74B02000D for ; Tue, 25 Feb 2025 20:23:20 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; spf=pass (imf13.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=1740515001; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EM6I6rR7+Q3brOi/+rk7wtdm2cDuoeq9a46aTNe/UCI=; b=O2686nZrDS0crDKxIigYq9YdNANmjUcoDp1gRm+sT/FiucqTGAWm2iE9ogbFWYMXW0S59G PKBQJdAPXNP67+1Op9NFsX6P7zyEr38d3Vbdp4l8NxZvDHcxGOZV8RjUN8b4jwVSh+rR0p L4s3b2CCaLMK5mvBzKfMwrKvEWYMLjA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=pass (imf13.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=1740515001; a=rsa-sha256; cv=none; b=FdENoZm2N6UWMBREIdfP7UVcyk99mjb4bkAVJ8SuVUm7EGLrrAMUPU3eRzNtxi87OxcZNf Tcp4N37YOhyP6wJMpAnHWGP/2/iN4Jf6kxOMxRs59ZfZjHviDff5kZLhwY8fHflr46YA4J Hpxp6XgEdBAPMo4N1TlRApkaXY46DeU= 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 1tn1Rm-000000007sa-2iZv; Tue, 25 Feb 2025 15:22:30 -0500 Message-ID: Subject: Re: [PATCH v13 07/14] x86/mm: global ASID allocation helper functions From: Rik van Riel To: Borislav Petkov Cc: x86@kernel.org, linux-kernel@vger.kernel.org, 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, jackmanb@google.com, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Manali.Shukla@amd.com, mingo@kernel.org Date: Tue, 25 Feb 2025 15:22:30 -0500 In-Reply-To: <20250224141601.GIZ7x_IXs-wla5BRsd@fat_crate.local> References: <20250223194943.3518952-1-riel@surriel.com> <20250223194943.3518952-8-riel@surriel.com> <20250224141601.GIZ7x_IXs-wla5BRsd@fat_crate.local> Autocrypt: addr=riel@surriel.com; prefer-encrypt=mutual; keydata=mQENBFIt3aUBCADCK0LicyCYyMa0E1lodCDUBf6G+6C5UXKG1jEYwQu49cc/gUBTTk33A eo2hjn4JinVaPF3zfZprnKMEGGv4dHvEOCPWiNhlz5RtqH3SKJllq2dpeMS9RqbMvDA36rlJIIo47 Z/nl6IA8MDhSqyqdnTY8z7LnQHqq16jAqwo7Ll9qALXz4yG1ZdSCmo80VPetBZZPw7WMjo+1hByv/ lvdFnLfiQ52tayuuC1r9x2qZ/SYWd2M4p/f5CLmvG9UcnkbYFsKWz8bwOBWKg1PQcaYHLx06sHGdY dIDaeVvkIfMFwAprSo5EFU+aes2VB2ZjugOTbkkW2aPSWTRsBhPHhV6dABEBAAG0HlJpayB2YW4gU mllbCA8cmllbEByZWRoYXQuY29tPokBHwQwAQIACQUCW5LcVgIdIAAKCRDOed6ShMTeg05SB/986o gEgdq4byrtaBQKFg5LWfd8e+h+QzLOg/T8mSS3dJzFXe5JBOfvYg7Bj47xXi9I5sM+I9Lu9+1XVb/ r2rGJrU1DwA09TnmyFtK76bgMF0sBEh1ECILYNQTEIemzNFwOWLZZlEhZFRJsZyX+mtEp/WQIygHV WjwuP69VJw+fPQvLOGn4j8W9QXuvhha7u1QJ7mYx4dLGHrZlHdwDsqpvWsW+3rsIqs1BBe5/Itz9o 6y9gLNtQzwmSDioV8KhF85VmYInslhv5tUtMEppfdTLyX4SUKh8ftNIVmH9mXyRCZclSoa6IMd635 Jq1Pj2/Lp64tOzSvN5Y9zaiCc5FucXtB9SaWsgdmFuIFJpZWwgPHJpZWxAc3VycmllbC5jb20+iQE +BBMBAgAoBQJSLd2lAhsjBQkSzAMABgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAAKCRDOed6ShMTe g4PpB/0ZivKYFt0LaB22ssWUrBoeNWCP1NY/lkq2QbPhR3agLB7ZXI97PF2z/5QD9Fuy/FD/jddPx KRTvFCtHcEzTOcFjBmf52uqgt3U40H9GM++0IM0yHusd9EzlaWsbp09vsAV2DwdqS69x9RPbvE/Ne fO5subhocH76okcF/aQiQ+oj2j6LJZGBJBVigOHg+4zyzdDgKM+jp0bvDI51KQ4XfxV593OhvkS3z 3FPx0CE7l62WhWrieHyBblqvkTYgJ6dq4bsYpqxxGJOkQ47WpEUx6onH+rImWmPJbSYGhwBzTo0Mm G1Nb1qGPG+mTrSmJjDRxrwf1zjmYqQreWVSFEt26tBpSaWsgdmFuIFJpZWwgPHJpZWxAZmIuY29tP okBPgQTAQIAKAUCW5LbiAIbIwUJEswDAAYLCQgHAwIGFQgCCQoLBBYCAwECHgECF4AACgkQznneko TE3oOUEQgAsrGxjTC1bGtZyuvyQPcXclap11Ogib6rQywGYu6/Mnkbd6hbyY3wpdyQii/cas2S44N cQj8HkGv91JLVE24/Wt0gITPCH3rLVJJDGQxprHTVDs1t1RAbsbp0XTksZPCNWDGYIBo2aHDwErhI omYQ0Xluo1WBtH/UmHgirHvclsou1Ks9jyTxiPyUKRfae7GNOFiX99+ZlB27P3t8CjtSO831Ij0Ip QrfooZ21YVlUKw0Wy6Ll8EyefyrEYSh8KTm8dQj4O7xxvdg865TLeLpho5PwDRF+/mR3qi8CdGbkE c4pYZQO8UDXUN4S+pe0aTeTqlYw8rRHWF9TnvtpcNzZw== Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.54.3 (3.54.3-1.fc41) MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: E74B02000D X-Stat-Signature: 9wzti7r4sdr6yao9t86hczjgrd9je3dx X-HE-Tag: 1740515000-892429 X-HE-Meta: U2FsdGVkX19lynPKesV5ndd6XzTLAQgKjdHojs5oaR5XThqictC2Qj6HBYOBsx4zIgC+7S9uyfCslCYGVlkcsaOrzKnd5roJamptvZCMQuh+zKjVmM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nMVdEsx/udC4EZ5UeUVufBHsO5g9VVtg9Io8FK46QYlDHLncb4+cpD+ksLR3wd5b8xtuN21p7SWlWtd7B4K/tpdCFYyAKNA3lsc9FE8CmL9TAFGJJCodD9RrMVoH5Lsb8Jqo9jfUOh1MkspcIwiDSc98lBspRL/A4M5lZxwEXCPBBQjf 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: On Mon, 2025-02-24 at 15:16 +0100, Borislav Petkov wrote: > On Sun, Feb 23, 2025 at 02:48:57PM -0500, Rik van Riel wrote: > >=20 > > =C2=A0arch/x86/include/asm/mmu.h=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0 = 11 +++ > > =C2=A0arch/x86/include/asm/tlbflush.h |=C2=A0 43 ++++++++++ > > =C2=A0arch/x86/mm/tlb.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 144 > > +++++++++++++++++++++++++++++++- > > =C2=A03 files changed, 195 insertions(+), 3 deletions(-) >=20 > arch/x86/mm/tlb.c:378:6: warning: no previous prototype for > =E2=80=98destroy_context_free_global_asid=E2=80=99 [-Wmissing-prototypes] > =C2=A0 378 | void destroy_context_free_global_asid(struct mm_struct *mm) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ^~~~~~~~~~= ~~~~~~~~~~~~~~~~~~~~~~ > arch/x86/mm/tlb.c:355:13: warning: =E2=80=98use_global_asid=E2=80=99 defi= ned but not > used [-Wunused-function] > =C2=A0 355 | static void use_global_asid(struct mm_struct *mm) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ^~~~~~~~~~~~~~~ > arch/x86/mm/tlb.c:327:13: warning: =E2=80=98mm_active_cpus_exceeds=E2=80= =99 defined > but not used [-Wunused-function] > =C2=A0 327 | static bool mm_active_cpus_exceeds(struct mm_struct *mm, int > threshold) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ^~~~~~~~~~~~~~~~~~~~~~ >=20 > If those functions are going to remain global they better get a > proper prefix > like "tlb_". >=20 I've renamed the global function to=20 tlb_destroy_context_free_global_asid > And add the functions with their first use - no need to pre-add them. That's what I originally had. Dave requested I split out the patch into multiple ones. That means either introducing helper functions in a separate patch, or coming up with one version of the code in one patch, and then replacing that code in the next, resulting in a bunch of extra code to review. https://lore.kernel.org/linux-kernel/b067a9fc-ff5f-4baa-a1ff-3fa749ae4d44@i= ntel.com/ >=20 > >=20 > > +static inline void assign_mm_global_asid(struct mm_struct *mm, u16 > > asid) >=20 > mm_assign_global_asid() Done. > > +/* > > + * Global ASIDs are allocated for multi-threaded processes that > > are > > + * active on multiple CPUs simultaneously, giving each of those > > + * processes the same PCIDs on every CPU, for use with hardware- > > assisted >=20 > "the same PCID" or "the same PCIDs"? >=20 > Does a multithreaded process get one or more than one PCIDs? I'd > expect only > one ofc. It's only one. Fixed the commment. >=20 > > + asid =3D find_next_zero_bit(global_asid_used, > > MAX_ASID_AVAILABLE, last_global_asid); > > + > > + if (asid >=3D MAX_ASID_AVAILABLE) { >=20 > if (asid >=3D MAX_ASID_AVAILABLE && !global_asid_available) Done. > > +/* > > + * Check whether a process is currently active on more than > > "threshold" CPUs. >=20 > @threshold - then it is clear that you mean the function argument. >=20 Done. Thank you. > > + * This is a cheap estimation on whether or not it may make sense > > to assign > > + * a global ASID to this process, and use broadcast TLB > > invalidation. > > + */ > > +static bool mm_active_cpus_exceeds(struct mm_struct *mm, int > > threshold) > > +{ > > + int count =3D 0; > > + int cpu; >=20 > Is this function supposed to hold some sort of a lock? I don't think we care too much about total accuracy here. Not holding up other CPUs is probably more important. >=20 > > + > > + /* This quick check should eliminate most single threaded > > programs. */ > > + if (cpumask_weight(mm_cpumask(mm)) <=3D threshold) > > + return false; > > + > > + /* Slower check to make sure. */ > > + for_each_cpu(cpu, mm_cpumask(mm)) { >=20 > Needs CPU hotplug protection? I don't see CPU hotplug protection in most other loops that iterate over CPUs and do nothing but read some per-CPU data. What are we trying to protect against? What kind of protection do we need? >=20 > > + /* The last global ASID was consumed while waiting for the > > lock. */ > > + if (!global_asid_available) { > > + VM_WARN_ONCE(1, "Ran out of global ASIDs\n"); >=20 > And? Why do we want to warn here? We need to reset global ASIDs > anyway. This warning prints if we run out of global ASIDs, but have more processes in the system that want one. This basically helps us figure out whether or not we should bother implementing more aggressive ASID re-use (like ARM and RISCV have), which might require us to figure out how to make that re-use more scalable than it is today. >=20 > > + return; > > + } > > + > > + asid =3D allocate_global_asid(); > > + if (!asid) > > + return; > > + > > + assign_mm_global_asid(mm, asid); > > +} > > + > > +void destroy_context_free_global_asid(struct mm_struct *mm) >=20 > That name is a mouthful. mm_free_global_asid() is just fine. >=20 Done. --=20 All Rights Reversed.