From nobody Fri Apr 3 05:18:58 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF09EECAAD3 for ; Sun, 18 Sep 2022 03:08:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229615AbiIRDIG (ORCPT ); Sat, 17 Sep 2022 23:08:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229558AbiIRDH0 (ORCPT ); Sat, 17 Sep 2022 23:07:26 -0400 Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A383F27159 for ; Sat, 17 Sep 2022 20:07:25 -0700 (PDT) Received: by mail-qk1-x735.google.com with SMTP id q11so15400302qkc.12 for ; Sat, 17 Sep 2022 20:07:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=SBOziEHkuH3w7/25l1oW2hkJ2TfdUUpVoIxYlWXHHvA=; b=Eips4MEH0hXW7hGRgRT+q3D1fUYpQ4rU/J2zk97cwSqUd4+CX8dXiYWZZjqiUwE7RP omcBLor5ov3jJk63zBPOzt7rJFsC6A3uux6r9H2ub+40Ia3YUvoHomcgKI21Oz4CxWHZ gJy3zVrH1+CMKYYwwUWYZbcosKS1hNVSEF2JIz6FkqEoWqCj20uNb9EAJhEuVayKAkmb Zao49cv2ZJUVWWLlnJbeXTQk4s7m8TXrcecbM4m5T5mM6WYL0aZGaNbzT0gUrfCAff0v AJWmACo4uIgAz0mhr1kCzWIOSRHSf4xudMBR43hA2/iphGUqL4zwqlRIAERYeM6WO48/ ErBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=SBOziEHkuH3w7/25l1oW2hkJ2TfdUUpVoIxYlWXHHvA=; b=1LXOcyf0C/haRP5xhEoaRtHQJwVrXbYiJnuPysVyJ92D1UpIdDPz0h0BAX3LyvejFB jvNqrOHN0La/DxhE7OavOEeyVdTgULBxMrWPQ3bFffWSbczRqFduru1bUBcO8eGeML8P quIG/vztCwnihUmPv0Y8Md3+Hk/NKT0wwv5h4c9A/MYWsP98yQ2UoNq0LzbtUd237UhR +zH97cZD94XwZcBIX+KvY7aInOQnb4DU/qBV/BUMieeCkOJI3bSKFdrzD+Hh2ppAoGBz m4PgT072m1gWnPfTe4e61RzjfFOX8wN4sJBWIEfy+HXN7JVLTqwOLiRk645o1dfmSQe0 qjvA== X-Gm-Message-State: ACrzQf2KAxU8l9vI6LKnKgbjq/kESIyNrFBZ4JvsD3E6r7tru5zJ3yDI 9jQy4sj6d/uBx3XHuRP/C5TGyw4Hw0A= X-Google-Smtp-Source: AMsMyM6hCNQmI2r9Ld2t4MZN5RE5pkAfvZvt4nHfjynB+SEZVXifdBE9DznHwiaavnj6QBb52cyepA== X-Received: by 2002:a05:620a:f8e:b0:6cc:1c3a:4f5d with SMTP id b14-20020a05620a0f8e00b006cc1c3a4f5dmr8833133qkn.425.1663470444652; Sat, 17 Sep 2022 20:07:24 -0700 (PDT) Received: from localhost ([2601:4c1:c100:2270:a495:2224:867e:566a]) by smtp.gmail.com with ESMTPSA id de20-20020a05620a371400b006bb49cfe147sm9319549qkb.84.2022.09.17.20.07.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Sep 2022 20:07:24 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, Alexander Lobakin , Andy Shevchenko , Arnd Bergmann , David Gow , Eric Dumazet , Isabella Basso , Kees Cook , Keith Busch , Kumar Kartikeya Dwivedi , Marco Elver , Mark Rutland , Rasmus Villemoes , Steven Rostedt , =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , Valentin Schneider Cc: Yury Norov Subject: [PATCH v3 6/6] cpumask: add cpumask_nth_{,and,andnot} Date: Sat, 17 Sep 2022 20:07:16 -0700 Message-Id: <20220918030716.1252285-7-yury.norov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220918030716.1252285-1-yury.norov@gmail.com> References: <20220918030716.1252285-1-yury.norov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add cpumask_nth_{,and,andnot} as wrappers around corresponding find functions, and use it in cpumask_local_spread(). Signed-off-by: Yury Norov --- include/linux/cpumask.h | 44 +++++++++++++++++++++++++++++++++++++++++ lib/cpumask.c | 28 ++++++++++++-------------- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index 31bf08c786e3..621b59820561 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -337,6 +337,50 @@ unsigned int cpumask_any_but(const struct cpumask *mas= k, unsigned int cpu) return i; } =20 +/** + * cpumask_nth - get the first cpu in a cpumask + * @srcp: the cpumask pointer + * @cpu: the N'th cpu to find, starting from 0 + * + * Returns >=3D nr_cpu_ids if such cpu doesn't exist. + */ +static inline unsigned int cpumask_nth(unsigned int cpu, const struct cpum= ask *srcp) +{ + return find_nth_bit(cpumask_bits(srcp), nr_cpumask_bits, cpumask_check(cp= u)); +} + +/** + * cpumask_nth_and - get the first cpu in 2 cpumasks + * @srcp1: the cpumask pointer + * @srcp2: the cpumask pointer + * @cpu: the N'th cpu to find, starting from 0 + * + * Returns >=3D nr_cpu_ids if such cpu doesn't exist. + */ +static inline +unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1, + const struct cpumask *srcp2) +{ + return find_nth_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), + nr_cpumask_bits, cpumask_check(cpu)); +} + +/** + * cpumask_nth_andnot - get the first cpu set in 1st cpumask, and clear in= 2nd. + * @srcp1: the cpumask pointer + * @srcp2: the cpumask pointer + * @cpu: the N'th cpu to find, starting from 0 + * + * Returns >=3D nr_cpu_ids if such cpu doesn't exist. + */ +static inline +unsigned int cpumask_nth_andnot(unsigned int cpu, const struct cpumask *sr= cp1, + const struct cpumask *srcp2) +{ + return find_nth_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), + nr_cpumask_bits, cpumask_check(cpu)); +} + #define CPU_BITS_NONE \ { \ [0 ... BITS_TO_LONGS(NR_CPUS)-1] =3D 0UL \ diff --git a/lib/cpumask.c b/lib/cpumask.c index f0ae119be8c4..2c4a63b6f03f 100644 --- a/lib/cpumask.c +++ b/lib/cpumask.c @@ -128,23 +128,21 @@ unsigned int cpumask_local_spread(unsigned int i, int= node) i %=3D num_online_cpus(); =20 if (node =3D=3D NUMA_NO_NODE) { - for_each_cpu(cpu, cpu_online_mask) - if (i-- =3D=3D 0) - return cpu; + cpu =3D cpumask_nth(i, cpu_online_mask); + if (cpu < nr_cpu_ids) + return cpu; } else { /* NUMA first. */ - for_each_cpu_and(cpu, cpumask_of_node(node), cpu_online_mask) - if (i-- =3D=3D 0) - return cpu; - - for_each_cpu(cpu, cpu_online_mask) { - /* Skip NUMA nodes, done above. */ - if (cpumask_test_cpu(cpu, cpumask_of_node(node))) - continue; - - if (i-- =3D=3D 0) - return cpu; - } + cpu =3D cpumask_nth_and(i, cpu_online_mask, cpumask_of_node(node)); + if (cpu < nr_cpu_ids) + return cpu; + + i -=3D cpumask_weight_and(cpu_online_mask, cpumask_of_node(node)); + + /* Skip NUMA nodes, done above. */ + cpu =3D cpumask_nth_andnot(i, cpu_online_mask, cpumask_of_node(node)); + if (cpu < nr_cpu_ids) + return cpu; } BUG(); } --=20 2.34.1