From nobody Tue Dec 23 14:23:15 2025 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 63C87230D38 for ; Mon, 13 Jan 2025 06:18:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736749126; cv=none; b=jDEvwwvqukmhfVWuYru4H8vA1w3XE9HS+HY4S1ik+0BYZwq1+KTP5ZH+IEfYgnxvXutgx7j0J6ck8gPEunfTeDsg1x90j2ePYh8fKPniJz7sLogBQdI3P2L6MScKFUrlT2EpvKrnLHzN9hBVkgXRccOGY/naGNhS0Clli8+fIbc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736749126; c=relaxed/simple; bh=w+MfzC9uvzJs+cJCE5M185FdgBh5lgH7Noptfq30pMI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=cm09FpHWPonAvCZpOv9arRHX5Id2Up71AbrtxHH+UHyehLyidFtlD7CXWJQSxM/zJ+rUuMbQOBTuSnC0wFi3peWrvnAJYYdyiiUhgUYpBop4KiPR0EgnbTIH10thMKAP6tVMVeWnPJi1i4qQuSUAV028LDhqXx4TxvQTV/aPhB4= 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=jz/at0eB; arc=none smtp.client-ip=209.85.214.171 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="jz/at0eB" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-21675fd60feso89530115ad.2 for ; Sun, 12 Jan 2025 22:18:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736749124; x=1737353924; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=49hlNwlkHZcy0sqq1GaQHAbEvgTQVK41dyKUEJyhdEc=; b=jz/at0eBMHDca8kINH696i7VnKm2Ry6dYiapralSho2PerLMtwIhI2j7cXHnnrrGiM x2U/sbXtZwlqXXHanN0kGDGllOF1K8SroMT5/IDouZX9jfz7ZzZli4PQLoJq2OvvYgs6 5XmIJkKD2c2YS3lcybcOUoGKiyzJPP1uKQiqPSsDH9Y/P28qDOy6Oatf4tZ0jsSxcMhI oB17uuMPldVWeMQsBv1TnkhYtNzGe8RCkI/xAl30YZOtQD3PYRz82zUi6yv0um/LKbXt SAnqvz6UWjlLjj5MEXHag1Qvh6ses3gwIUc8QFAeASjm8U7z79BLm1dFi08S9Z9+KhiE yMww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736749124; x=1737353924; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=49hlNwlkHZcy0sqq1GaQHAbEvgTQVK41dyKUEJyhdEc=; b=B+vC0Jr9HghAtTBfiPUeagg6PdpDIJXLXqqOnOzH3cV8REI0IAh0azrI9U2BGJ8uT9 lpDJHdJmJt+k58X1Jh61H+FLLcUr8JJqw6vkToIuYXp4vMmSO15JdbqRcUuga4cQGnnV jg5T+dq5W9aUBRC/tN0fsUD+u2P/6aoZAqJ7ZBIY9mbqcga+YsCeoXp8g38BUnocl+z5 z7NvvmbSG/qbpfAYU6RWWAqyvnBIeSnveuQ5klBblWTbueIZwY6lOIRp4akHm9F1JBdG ni5m2zHILssAVnxnco6hTqxAo1a4oFLRqgUUrCmSC9FPOCuy5fvwzL6HE8g4GfXtDl0l Erfw== X-Forwarded-Encrypted: i=1; AJvYcCVRm261yac9kzMR8bTKkd0XIquY7R32NM4IowugzH8sy2Jwrf81Ck1E6dlubABCh4OSeWLBg7Gj2CQzb7U=@vger.kernel.org X-Gm-Message-State: AOJu0Yxm6NbEwjwmvuMjn6pu/8jmPra8fiFzb4kTmAkExitaW6i4NpEA M8mkvsc/R9fcJzw2EJXNE9N4K71a0mpuKIUP4x5TdCHTVJIGlFf812LBAQyQ X-Gm-Gg: ASbGncuNLQAGXWCOv7MJpawoQdQPdhzWdwLbhdLtcNefK2EZgjR3PooM+p7hi8K1a8g VXxSWli2Goje2NbGThi2QLqIEAw7y/DI3zqRBiJ0bS81z0FhtrDc/LLOFrz88CweawOAvQakdLC o23bufQb9k4lb+PQZVXCurZEqJ0RE3SNaZsG3gtty05Tvr442OUqvhnwMsrd3UiqLpXM0VigSvB uSbIrXtyzwEdTZUWgoVC131BwUv9+DhacLkpnDNVZWfpTQhlN+/0J53+8ai7p6z83ZtGlU5qe1c X-Google-Smtp-Source: AGHT+IHVc+HFjXLUYo/jeeg6rpbAv/Xo4XC6WWc/PF/Otr/Y8dXsDe+qkRa1aysMgCnLzlDd4BTv2Q== X-Received: by 2002:a17:902:d4c4:b0:216:4016:5aea with SMTP id d9443c01a7336-21a83f71202mr296215195ad.29.1736749124409; Sun, 12 Jan 2025 22:18:44 -0800 (PST) Received: from vaxr-BM6660-BM6360.. ([2001:288:7001:2703:ed7e:d523:ad1d:dd48]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f152c6asm46311395ad.104.2025.01.12.22.18.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 22:18:43 -0800 (PST) From: I Hsin Cheng To: yury.norov@gmail.com Cc: linux@rasmusvillemoes.dk, jserv@ccns.ncku.edu.tw, linux-kernel@vger.kernel.org, I Hsin Cheng Subject: [RFC PATCH] cpumask: Implement "random" version of cpumask_any_but() Date: Mon, 13 Jan 2025 14:18:39 +0800 Message-ID: <20250113061839.22131-1-richard120310@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Original implementation of "cpumask_any_but()" isn't actually random as the comment claims itself to be. It's behavior is in fact to select the first cpu in "mask" which isn't equal to "cpu". Re-implement the function so we can choose a random cpu by randomly select the value of "n" and choose the nth cpu in "mask" Experiments[1] are done below to verify it generate more random result than orginal implementation which tends to select the same cpu over and over again. Signed-off-by: I Hsin Cheng --- The test is done on x86_64 architecture with 6.8.0-48-generic kernel version on Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz [1]: Test script: int init_module(void) { const struct cpumask *cur_mask =3D cpu_online_mask; unsigned int cpu =3D 5, result; int times =3D 50; pr_info("Old cpumask_any_but(): "); for (int i =3D 0; i < times; i++) { result =3D cpumask_any_but(cur_mask, cpu); pr_cont("%u ", result); } pr_info("\n"); pr_info("New cpumask_any_but(): "); for (int i =3D 0; i < times; i++) { result =3D cpumask_any_but_v2(cur_mask, cpu); pr_cont("%u ", result); } pr_info("\n"); return 0; } Experiment result showned as below display in dmesg: [ 8036.558152] Old cpumask_any_but(): 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0= 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [ 8036.558193] New cpumask_any_but(): 7 1 1 2 2 2 3 4 0 2 7 4 6 3 3 2 2 4 2= 7 6 6 6 4 6 6 6 4 4 7 6 2 2 6 7 6 6 3 0 6 2 1 0 4 4 6 4 6 6 3 --- include/linux/cpumask.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index 9278a50d5..336297960 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -401,12 +401,18 @@ unsigned int __pure cpumask_next_wrap(int n, const st= ruct cpumask *mask, int sta static __always_inline unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu) { - unsigned int i; + unsigned int i, n, weight; =20 cpumask_check(cpu); - for_each_cpu(i, mask) - if (i !=3D cpu) - break; + weight =3D cpumask_weight(mask); + n =3D get_random_u32() % weight; + + /* If we accidentally pick "n" equal to "cpu", + * then simply choose "n + 1"th cpu. + */ + if (n =3D=3D cpu) + n =3D (n + 1) % weight; + i =3D cpumask_nth(n, mask); return i; } =20 --=20 2.43.0