Received: by 2002:ab2:715a:0:b0:1fd:c064:50c with SMTP id l26csp9184lqm; Mon, 10 Jun 2024 10:58:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXpV2AbeODxE3bb53gNMHezL0J4mKdF76CtKES/wOHLDbIMcVuYX0MFmdWZTM3n9vasIqxAxZMazXnvQpkUMAZj36sQ+mC7mh50o8rNFA== X-Google-Smtp-Source: AGHT+IH76c8M3zCxNdx9IUR3AJCW2qCz9YcR3Ia6uXVtTAaIjPFd10SocbKwx86RDtFAWbecmBXV X-Received: by 2002:a17:906:fa05:b0:a6f:fbc:b3ef with SMTP id a640c23a62f3a-a6f0fbcb455mr505651166b.59.1718042336352; Mon, 10 Jun 2024 10:58:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718042336; cv=pass; d=google.com; s=arc-20160816; b=vxPfcN9YwIBn0qHPum41w8+V6XgdqiK1FGcjT7ZB+7kalGZVKPylNLm02qBi1nfncL 5Bd74UD8xfQHTYjA6tMUcge2fqhPI7bpJG4jCqXQBFS2hyu0ufjmVeDP0bhTZrj36x9J emFbZo/WZQl+xf2upoDpoGigN0w3bVXJDPPP19na7gZMZJ+NbgwveF9tsWSH216156zk FKK1yqwyYEmq26a3wqfq7uJaPmSm3rvzY2fQDq0euQmiM2TySwLUvLnLpnxphZeS/mMC mg9XVi+XXGhP3NPRMLw7FKgcglFFrcbywjtJhJ1rhJjd3LC5bf+02Cps+oFfrjhK7fE2 Hc/Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=bQX4a1uB13mXlhl/FxYuXCoDhG2XSW0BFXyW6YKkaDg=; fh=SCssJFybW88jo7MRVvVSuDiy7luHKfVK6jyYHvREFAg=; b=EB5uLPE0rdPtLzDVc0EcewDPUB53c28n60ZOeMrtC+U3hEyn7WF7VWZKExQAjRGWm0 8WM86dokDOzDSP254ce4NNCX4H/wTasWAvJ4Cjdlh3t8DwQ04tp6PxgrXbeorhhrjS2Q ahjLxyDSqknC8HDyOH+6crbHx+kh3lfMu5FTOJKXV+1+/pIh1tOZkf13FlhfnFcEakiq fBKzk9EY7yqLJFyIiyGkWj3YmY0FasvlfOYYVvdb/+zwebBypiIU/985ZGnNckuk4bno +hLeHik54sIl42TG7FHBCHGCKD0VUNLkp2mJKz9S1KIqvAa13oscaFIkUM8zI2eNb19r GIwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=B6uYFWB3; arc=pass (i=1 spf=pass spfdomain=linuxfoundation.org dkim=pass dkdomain=linux-foundation.org); spf=pass (google.com: domain of linux-kernel+bounces-208689-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208689-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a6df16c06d5si367244466b.791.2024.06.10.10.58.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 10:58:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-208689-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=B6uYFWB3; arc=pass (i=1 spf=pass spfdomain=linuxfoundation.org dkim=pass dkdomain=linux-foundation.org); spf=pass (google.com: domain of linux-kernel+bounces-208689-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208689-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id D81251F22D71 for ; Mon, 10 Jun 2024 17:58:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1C28914B084; Mon, 10 Jun 2024 17:58:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="B6uYFWB3" Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0CB914900C for ; Mon, 10 Jun 2024 17:58:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718042320; cv=none; b=ddRdpLjq+SIEAnI5dqz2LlZXvgxHTKEYTjp9v9h5UuC2CCALFTarG3Fo/kGcgSVDKAv8k0udW9p5DGRjqiEr23oq/nQp3e8wIjgY7yEu63roIR5SB5Qwebja3thCu5w98jWMJCdjWAWvbjMnC+gDs2b0u4GX2MWOjM6Xe56VOuk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718042320; c=relaxed/simple; bh=hO6nnDmuhkUErx75fBkprBGAIkEHIr9E1JJw8oC4y1A=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=Xe/N9W81xl5GR54L6Rwosu2ElfG55Tf2jzWiAm97RIPvCwIyVrGoN+OSBAQ6ZivbZiDQbzrgsXoVoz5H8V0ai5Rkx7Bd7QdqdzIyrlzxx8zIZd9Uk8mLil2OtoIYugAoXaZjngtcxZCOgW3NuRzNSyYyvWfKfbFCGcQdW2iFa0Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org; spf=pass smtp.mailfrom=linuxfoundation.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=B6uYFWB3; arc=none smtp.client-ip=209.85.208.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linuxfoundation.org Received: by mail-lj1-f174.google.com with SMTP id 38308e7fff4ca-2ebe0a81dc8so2429331fa.2 for ; Mon, 10 Jun 2024 10:58:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; t=1718042316; x=1718647116; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=bQX4a1uB13mXlhl/FxYuXCoDhG2XSW0BFXyW6YKkaDg=; b=B6uYFWB392WDEcYr0HbznBnOc65vMkmbWesae73GUt4mybcRD4siviUw6vNx4qdp1R IxO08I+AFOHO51Zu/sdVI03lQhK7O2CB7ePLaD6iMyem/FGvoTN6VssUJiNTEocj9+tw ifuTpVtCHbuZhlMkS4/teuJHTrgbfvkhhJxLo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718042316; x=1718647116; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bQX4a1uB13mXlhl/FxYuXCoDhG2XSW0BFXyW6YKkaDg=; b=BcxexQHjkj/c6GCQFFssZcmvwH+AiLBE4JIJNLTVG5XlEKNfXItR4nw6WiwiCpZRo5 R+Uxu6k8NOc5q/SDfpwOyAu9cQnz8LkFJoMlcJB7XvuLqzxAAIzfAsVTXDiuJBIslldg wpItIAaX2YScel4VsGGkLyEKDq/nIFuZ36/xSGyNPLp8ZaouJFBnmBrzCp2k7Yy+F50I ZDnhmaSwDfpQYNvSEAg61DFA5GQBTwiCoM5KmF3AAFC7N8gVKN4xCU53qC57TysSHBkM lslt6dbeinOQQ0zPU0KZs0BXLejt+O9y5+suxPpQSqfIxlciJxaOGqtnm2YtW9w2cD49 cNQQ== X-Forwarded-Encrypted: i=1; AJvYcCVBTYuItpaNVJvQAzaEzvx6xoy7X2OpnQKKcNu2waSezrXAQebJL7myEfwonP+NPlFF7La3IFrHdwipfTw94TS8BBfoHMjXzzPj2Zfz X-Gm-Message-State: AOJu0Ywwk4GXwOjyAAnRts6wOePgbPasyOW8bBV3V7BTK7pUCtcF3di4 NBCn8urUKbKc7LoddjGm7HDLa0jTnBSgO/V8XOoEOUDAtlc/1RNtyrQ8ewlkVj/71bWA6aKEdfj 7bnA= X-Received: by 2002:a2e:3502:0:b0:2eb:d696:4b80 with SMTP id 38308e7fff4ca-2ebd6965d7bmr51041131fa.52.1718042315791; Mon, 10 Jun 2024 10:58:35 -0700 (PDT) Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com. [209.85.218.41]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57aae234204sm7781388a12.87.2024.06.10.10.58.34 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 Jun 2024 10:58:34 -0700 (PDT) Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a6ef8e62935so22102866b.3 for ; Mon, 10 Jun 2024 10:58:34 -0700 (PDT) X-Forwarded-Encrypted: i=1; AJvYcCU9hMnkO37W1gsY0SF1ty1+BzFdIqgDRvVitlQ5+5p7pm/BwEgi0k7Or1+GsVYU/hYV4ZOw0E/6+mfGWvWmdcZiGqNOpDoNyAJJbXnz X-Received: by 2002:a17:906:11ca:b0:a6e:139b:996d with SMTP id a640c23a62f3a-a6e139b9d75mr689185666b.32.1718042314389; Mon, 10 Jun 2024 10:58:34 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240608193504.429644-2-torvalds@linux-foundation.org> <29a8ceb4-a699-433b-8a11-be6b3c9fd045@rasmusvillemoes.dk> In-Reply-To: <29a8ceb4-a699-433b-8a11-be6b3c9fd045@rasmusvillemoes.dk> From: Linus Torvalds Date: Mon, 10 Jun 2024 10:58:17 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] x86: add 'runtime constant' infrastructure To: Rasmus Villemoes Cc: Peter Anvin , Ingo Molnar , Borislav Petkov , Thomas Gleixner , Josh Poimboeuf , Linux Kernel Mailing List , "the arch/x86 maintainers" , linux-arch Content-Type: text/plain; charset="UTF-8" On Mon, 10 Jun 2024 at 02:50, Rasmus Villemoes wrote: > > Well, I think it lacks a little. I made it so that an arch didn't need > to implement support for all runtime_const_*() helpers Honestly, normally the number of helpers would max out at probably three or four. The only helpers you need is for the different constant sizes and the placement in the instructions. For example, on x86-64, you almost certainly end up with just three fixup functions (byte, 32-bit and 64-bit) and then *maybe* a couple of "operation" helpers. Honestly, the only op helpers that are likely to exist are add/mask/shift. But I didn't add macros for creating these ops, because I don't actually believe very many exist in the first place. And yes, x86-64 ends up being fairly simple, because it doesn't have multiple different ways of encoding immediates in different places of the instruction. But even that isn't going to make for very many operations. In fact, one of the whole design points here was KISS - Keep It Simple Stupid. Very much *not* trying to handle all operations. This is not something that will ever be like the atomic ops, where we have a plethora of them. To use a runtime constant, the code has to be really *really* critical. Honestly, I have only ever seen one such function. __d_lookup_rcu() really has shown up for over a decace as one of the hottest kernel functions on real and relevant loads. For example, in your RAI patches six years ago, you did the dentry cache, and you did the inode_cachep thing. And honestly, while I've never seen the inode_cachep in a profile. If you ever look up the value of inode_cachep, it's because you're doing to allocate or free an inode, and the memory load is the *least* of your problems. > name because yes, much better than rai_), but could implement just > runtime_const_load() and not the _shift_right thing, Not a single relevant architecture would find that AT ALL useful. If you have a constant shift, that constant will be encoded in the shift instruction. Only completely broken and pointless architectures would ever have to use a special "load constant into register, then shift by register". I'm sure such horrors exist - hardware designers sometimes have some really odd hang-ups - but none of the relevant architectures do that. > Otherwise, if we want to add some runtime_const_mask32() thing to > support hash tables where we use that model, one would have to hook up > support on all architectures at once. See above: when we're talking about a couple of operations, I think trying to abstract it is actually *wrong*. It makes the code less readable, not more. > Don't you need a cc clobber here? "cc" clobbers aren't actually needed on x86. All inline asms are assumed to clobber cc It's not wrong to add them, but we generally don't. > And for both, I think asm_inline is appropriate Yup. Will do. > I know it's a bit more typing, but the section names should be > "runtime_const_shift" etc., not merely "runtime_shift". I actually had that initially. It wasn't the "more typing". It was "harder to read" because everything became so long and cumbersome. The noise in the names basically ended up just overwhelming all the RealCode(tm). > > + RUNTIME_CONST(shift, d_hash_shift) > > + RUNTIME_CONST(ptr, dentry_hashtable) > > + > > Hm, doesn't this belong in the common linker script? I actually went back and forth on this and had it both ways. I ended up worrying that different architectures would want to have these things in particular locations, closer to the text section etc. IOW, I ended up putting it in the architecture-specific part because that's where the altinstructions sections were, and it fit that pattern. It could possibly go into the INIT_DATA_SECTION thing in the generic macros, but it has to go *outside* the section that is called ".init.data" because the section name has to match. See why I didn't do that? > I mean, if arm64 were to implement support for this, they'd also have to add this > boilerplate to their vmlinux.lds.S? See https://lore.kernel.org/all/CAHk-=wiPSnaCczHp3Jy=kFjfqJa7MTQg6jht_FwZCxOnpsi4Vw@mail.gmail.com/ doing arm64 was always the plan - ti was doing perf profiles on arm64 that showed this nasty pattern to me once again (and honestly, showed it much more: I have 14% of all time in __d_lookup_rcu() because while this machine has lots of cores, it doesn't have lots of cache, so it's all very nasty). Notice how small that patch is. Yes, it adds two lines to the arm64 vmlinux.lds.S file. But everything else is literally just the required "this is how you modify the instructions". There is no extra fat there. > Please keep the #includes together at the top of the file. Yes, my original plan was to do #ifdef CONFIG_RUNTIME_CONST #include .. do the const version of d_hash() .. #else .. do the non-const one .. #endif but the asm-generic approach meant that I didn't even need to make it a CONFIG variable. > > +static inline struct hlist_bl_head *d_hash(unsigned long hashlen) > > Could you spend some words on this signature change? Why should this now > (on 64BIT) take the full hash_len as argument, only to let the compiler > (with the (u32) cast) or cpu (in the x86_64 case, via the %k modifier if > I'm reading things right) only use the lower 32 bits? I'll separate it out, but it boils down to both x86 and arm64 doing 32-bit shifts that clear the upper 32 bits. So you do *not* want a separate instruction just to turn a "unsigned long" into a "u32". And while arm64 can take a 32-bit input register and output a 64-bit output, on x86-64 the input and output registers are the same, and you cannot tell the compiler that the upper 32 bits don't matter on input to the asm. End result: you actually want to keep the input to the constant shift as the native register size. But I'll separate out the d_hash() interface change from the actual runtime constant part. Linus