From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 43B6B303A26 for ; Fri, 6 Mar 2026 23:02:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772838124; cv=none; b=GpRnsYitK1x2W6LvV3DkejlcGlRMYtBzr15CsS9zTLWnEf2z4dzIcjqtmfZ3XEO+yveVBZLd6Ya/7tSXRmX6AG2xFXmyiLx+Lg0lxFCOkdceWhC8m+8EVbdTtnV9tbMRhEFXOydyRQFcIUZEferAeCYyyFQgMnQC3fzdpiodyco= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772838124; c=relaxed/simple; bh=feArA+KlIOSS8V4zQttxEYJoG3jlDGnPqZ4psSts/vQ=; h=Date:From:To:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ko1vi28huLjgsNSjnvI8LH7BZRLcjocJeTFIHoUaAzrKUujUt4rFvPB94s4lLrSM/22hxj/FWyQSqc4pPHE+VPNwyPR6E6zH4o4ap18hFd/e6fTySx4LPXH1HHaAX+mr/xos4lJtvh10cerINF7kjf/1Ihti+HzZ6fY+P06/Eng= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ZiwBzeTa; arc=none smtp.client-ip=209.85.221.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZiwBzeTa" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-439c56e822eso4004989f8f.2 for ; Fri, 06 Mar 2026 15:02:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772838121; x=1773442921; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:to:from:date:from:to:cc:subject:date:message-id :reply-to; bh=sgWqMlgENHCJE0WtuXsbxfaWXYPnD4k6Gb6xEbtCqlA=; b=ZiwBzeTacyqH3Vils8BmjX1x16+wLMmm7OhcOpuNDeM18mt964xBNtJhEH/AP+OfNz PsHpZq64yClEjiC4LHmVY9+d8+2gJuK/lGQzaRd6HFVVT4GyPBMMsGSCQVHPBBp6EhR/ BQm/rgfvLSQ/GaBo2Dq4t9+C//mq/VuTMx8nJIuEtVdC9sXxKAFOWroEbmsa8yTEDABh T/aeKM18g73xS+nwPLQVSaKtQO2mfdOHtkpDLnG/+Fot09EEdvaKo8fZZ07DA7D0awVm HeFBDsC8hWoMgW/iNmR+vtFvuFgJ4cypG61o3Vs0nYZLFqJDgdNZcTHQluZOHety3HJu A4Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772838121; x=1773442921; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:to:from:date:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=sgWqMlgENHCJE0WtuXsbxfaWXYPnD4k6Gb6xEbtCqlA=; b=JyLGU0QY7vqXV75CLVJ2yWb51nilZ7wvuhuxeunLMk4Fp/WrdQXMsNOXGTGK1IpTBH orQ2N0ia/BCS5r7dlYfCaGpY3aOKc6IYsBbuF9k4vH+rGsUVeRcJGJ3GT+QXdTlzCRs+ qS3im5j56YJLSr3BE/5w32sDDUBOl0HQu35ErEJHyuItwB05Sa/ACfTkwfLFPGkpNhBn OS+Emo9KFuu+5Bcpt/ffM4ykxSLzIGl3eWpki5X/PcXDqb2/mr62DvJRGdBn2kopyyKE ngzYL7i3PH7mBFMUauwQj1VDG1lTOZZVn5De6LwAwbyAY68/m4ajTgpkt85p2ck20jUz PXow== X-Forwarded-Encrypted: i=1; AJvYcCVwZGJQXtHVodU/31x5nB+dj+wNfjkdQ7V1iZqAQoovQBTI3uhxpPCbWDx/Tz/9OHHmjmDA5DjFcYk/6Og=@vger.kernel.org X-Gm-Message-State: AOJu0YwwLPEzO+gdN11hpDmPKvTGtSJV7RrVmdPLFFDbQn2Tihd8xy+2 0b+5XIgfE22bI2MXKZPOurLgqq2f2O+gU7sHRAbWDQCyZr39u1K4J3Xm X-Gm-Gg: ATEYQzzXtvKXwv7yJ5lryvoG5GSJgMCCooVloVNuBavCDnTgYUvSE/jZwitbO2a0MAS tOYV9A6SdaINeXcpnGz9tQLN/PWZ/hB8fHOAbva0fkyc0+EmwolA3MKqAP30gjQZrl7xXdDv/ux FIFh+OBy1HNVFi0eLEcoyBfvHQPcbm72h52SeVtnc3TjLNzY/eEzT3ayRIcc7SqxhqJTCiojQvx Z2rZj2Seu1vqlrm0w1qhvawVnhVLFotZvFYczFhmZsdrfmMDjhrnzzesXYDIKFrN4EJnoUuw5Se qIewETQFu9oWieD8qjuFTGSHN9TEDaEstLuxVCS4IQ81HfpIQ3j1gMws+joLOmAJnZ2gjAIUG46 hvHmO+WHveScKs9kMBA6OmQUhgtYdoUnSzbOhr0bLhCDaW54YDpOOVg6OG4CfwdmiBin8DhWoQu yUnUvD0GpDluK8SlpM9b3g5N/BM0coH/wHmOkf5LxyfOJtO2e8Beg00mMXwv+j1cLm X-Received: by 2002:a05:6000:609:b0:439:c81f:b96d with SMTP id ffacd0b85a97d-439da89d4d3mr7136754f8f.51.1772838121299; Fri, 06 Mar 2026 15:02:01 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-439dad8d840sm8006760f8f.8.2026.03.06.15.02.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Mar 2026 15:02:01 -0800 (PST) Date: Fri, 6 Mar 2026 23:01:59 +0000 From: David Laight To: Waiman Long , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng , linux-kernel@vger.kernel.org, Linus Torvalds , Yafang Shao , Steven Rostedt Subject: Re: [PATCH v3 next 4/5] Optimise decode_cpu() and per_cpu_ptr() Message-ID: <20260306230159.7399d786@pumpkin> In-Reply-To: <20260306225150.93178-5-david.laight.linux@gmail.com> References: <20260306225150.93178-1-david.laight.linux@gmail.com> <20260306225150.93178-5-david.laight.linux@gmail.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 6 Mar 2026 22:51:49 +0000 david.laight.linux@gmail.com wrote: Apologies to Yafang for mistyping his address... > From: David Laight > > Changing the 'cpu number' variables to 'unsigned int' generates > slightly better code (and the values can never be negative). > > More specifically gcc knows that decrementing the 'encoded' value > zeros the high 32bits (on sane 64bit architectures) so that it doesn't > need to zero/sign extend the value to index __per_cpu_offset[]. > > Not massive but saves two instructions. > > Signed-off-by: David Laight > --- > > Proposed by Linus. > Part of a discussion from v1 about whether removing the offset would help. > > kernel/locking/osq_lock.c | 18 +++++++++--------- > 1 file changed, 9 insertions(+), 9 deletions(-) > > diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c > index 5dd7e08d4fda..0619691e2756 100644 > --- a/kernel/locking/osq_lock.c > +++ b/kernel/locking/osq_lock.c > @@ -15,7 +15,7 @@ > struct optimistic_spin_node { > struct optimistic_spin_node *next; > int locked; /* 1 if lock acquired */ > - int prev_cpu; /* encoded CPU # + 1 value */ > + unsigned int prev_cpu; /* encoded CPU # + 1 value */ > }; > > static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node); > @@ -24,19 +24,19 @@ static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node); > * We use the value 0 to represent "no CPU", thus the encoded value > * will be the CPU number incremented by 1. > */ > -static inline int encode_cpu(int cpu_nr) > +static inline unsigned int encode_cpu(unsigned int cpu_nr) > { > return cpu_nr + 1; > } > > -static inline int prev_cpu_nr(struct optimistic_spin_node *node) > +static inline unsigned int prev_cpu_nr(struct optimistic_spin_node *node) > { > return READ_ONCE(node->prev_cpu) - 1; > } > > -static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val) > +static inline struct optimistic_spin_node *decode_cpu(unsigned int encoded_cpu_val) > { > - int cpu_nr = encoded_cpu_val - 1; > + unsigned int cpu_nr = encoded_cpu_val - 1; > > return per_cpu_ptr(&osq_node, cpu_nr); > } > @@ -53,9 +53,9 @@ static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val) > static inline struct optimistic_spin_node * > osq_wait_next(struct optimistic_spin_queue *lock, > struct optimistic_spin_node *node, > - int old_cpu) > + unsigned int old_cpu) > { > - int curr = encode_cpu(smp_processor_id()); > + unsigned int curr = encode_cpu(smp_processor_id()); > > for (;;) { > if (atomic_read(&lock->tail) == curr && > @@ -94,8 +94,8 @@ bool osq_lock(struct optimistic_spin_queue *lock) > { > struct optimistic_spin_node *node = this_cpu_ptr(&osq_node); > struct optimistic_spin_node *prev, *next; > - int curr = encode_cpu(smp_processor_id()); > - int prev_cpu; > + unsigned int curr = encode_cpu(smp_processor_id()); > + unsigned int prev_cpu; > > node->next = NULL; >