From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (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 9833435F17D for ; Fri, 6 Mar 2026 22:51:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772837520; cv=none; b=CW5JuD73wumxQI3CJ13vH/7SF5hVILhCWf7X1D8zTMDMB4f4v7NUMLpy1NUVCvNksZVBskLbofPS7WyNe44jS3e9eHFafN03Dqgej3jkiNAOtGw9fU/5qvFWnlj3ZiLrhNsAhqGa1WYAtN+mYFMHjdgqEsHAqQUGhOjus6rWNxw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772837520; c=relaxed/simple; bh=2wHXZAdPDeWXiT3dF/iJP5pNHB+KvzI+EJ5gqZu10Ik=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eLwcDC5MXgnpkrrEJ9nXF+PWc0RPbPXlxFcj5P1TQTl3CIkLt+jH+MAd6taQ+SkgQnOreD+tv4K52NKLvFcg50pEfkpOWXNS+965UmsqQ4UFb34AvknNzep4dE/yFYyj3rk4mVA4NxdH94/ekg+A9Vz5oMow8HUdKK3mS6cxzXw= 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=OZVOSZ6Z; arc=none smtp.client-ip=209.85.221.44 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="OZVOSZ6Z" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-439b7c2788dso4405547f8f.1 for ; Fri, 06 Mar 2026 14:51:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772837517; x=1773442317; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FFLGRcb+7+DWSwwR0Q53uNLMV1PZ5s9OjseZXq2vX6g=; b=OZVOSZ6Z6IXQ6ytdwnP8vQoSGYRB5P27wyY7MBO1Lm2ttwSqIfYrwOaczxb1X7GjsW t2RzL54/GC8KMMR49DPRoX1fwbSJfkdLfq4aihlOeZcm1iVLsV2fwexRTiJ3pkVqWUWT 4RUDwUYOFxSBYEix/fkH9ZZNgt1lL9DMcC498mDsBfIjlZ8DK43gUtKtuKHET8H3rUA6 hmOsmHX5I56+a8U2SHwc3uTQ3kY9fTkk7hvbl4GXj2hXalaLaJQziOVEd8gFwOS3GgxV 5LG/CFlQOck3TXqzTho2Iz5YmNqEfR+V30sBazCEeRc8TfqdlQpq/UY6qj3dMhTqi5BK g7HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772837517; x=1773442317; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=FFLGRcb+7+DWSwwR0Q53uNLMV1PZ5s9OjseZXq2vX6g=; b=LutnCftfoujWLb1MrsdUJ2DV4DFKrzW1iJH9WslxM6UKi/Ka0BMzfzxsN0l1qRn2bd glXXpR7bY4LKQcm/rCGIvO1ilr7wzAhh1qvQtd/WuIzLZDc+WgH8wj4lA5ILrpP3IY2j FP9chvVlhDBo15A2w3BNH2XHa+c6Ifr1NXt7vJEilGYDk5yxhTKrPnuIRyFP1ONmzXyh 1uijl55Gr3zZosAwDESuJE/2nzMczIHQBMbmtnMbhQc92ogd6OkjNM/Wrt58Lg5GETvx tRWbqJfDlQj7QpwFz21gV+Gk1pbCGSAvJhhZ1UVjJ4k1ImDvKiv9ZY4EixNKayvEivEb z/1Q== X-Forwarded-Encrypted: i=1; AJvYcCV5CBj+8LhbHWTAyUPP7tjg52HAOSKU42QL58UjAkH6wgSnfkocY0OIK8KgbC/VpY13HTCMLF087GyuoE4=@vger.kernel.org X-Gm-Message-State: AOJu0Yzp0kp2ghOpp4CuPbaQt7RbNmt/L/N7PuZ8t43OqZOZ3ue7VUWU Se7tkVHMK/6svEYUSLXObSXFW5sygS3LtOz4sMOnI0NFrbYHV1GrQ2U7 X-Gm-Gg: ATEYQzwnV9N27Khi1Z3KmXAfXJEkQ61ClTet0URJJVD12n3i+RP2mhVaiLYyKnYh0c4 kj0WrKqLXEpIlJO7yCKoARUv+xaIJL30ksd8C/lt5aBfBtSbY0MT0cwoFbcdvBNvjK2ujZ8X6zS H/EhyiNe8bmLrl+WyZZQZI8ooaJlh3r3ICDXbSUWniZTR55Oi5KBJnc0kI+QqQDtUCEFt8f8IKa uh5tBT7wgDEXEehrcVDPtNzw3H8bdywWZAJhyCFU6UpRuzP7FnfjxXds7bTBvkSj3ZG4nyA8t0g i29Llq+FBC7m3KkPEnqxQeaLrETMys7+8J+tfxUX3h1S0Okye02ixX+NCtYjsRuRGtyt35il1Jb tfQBsffKuFdT6fTbt/b4WAKeIFtiFDsTmWIcGPMjxzDrYMYz0nuEOoMoWqowSt5YhiN4Nr+1H3C bjfYftAQBLb5/4+3dP9W6WZVIqGZVIqy2giMsEGqva1niXsrB/ag9aml/Sc1Qm+gYpqeeDWTpAv ovzLf/UIz5J X-Received: by 2002:a05:6000:230f:b0:439:d755:a894 with SMTP id ffacd0b85a97d-439da348b29mr6708277f8f.3.1772837516703; Fri, 06 Mar 2026 14:51:56 -0800 (PST) Received: from snowdrop.snailnet.com (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-439dae57c05sm6160845f8f.39.2026.03.06.14.51.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Mar 2026 14:51:56 -0800 (PST) From: david.laight.linux@gmail.com To: Waiman Long , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng , linux-kernel@vger.kernel.org, Linus Torvalds , Yafang Shao , Steven Rostedt Cc: David Laight Subject: [PATCH v3 next 4/5] Optimise decode_cpu() and per_cpu_ptr() Date: Fri, 6 Mar 2026 22:51:49 +0000 Message-Id: <20260306225150.93178-5-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260306225150.93178-1-david.laight.linux@gmail.com> References: <20260306225150.93178-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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; -- 2.39.5