From patchwork Wed Aug 17 17:58:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 12946288 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 7E864C25B08 for ; Wed, 17 Aug 2022 17:58:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240940AbiHQR6e (ORCPT ); Wed, 17 Aug 2022 13:58:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238249AbiHQR6b (ORCPT ); Wed, 17 Aug 2022 13:58:31 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 635BC2559B for ; Wed, 17 Aug 2022 10:58:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660759106; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FeC1hIbpeF331EYBQF/yk4Lw5Xn8tjgBYGOWKQGB6dY=; b=N7EOlLBJCQIdpP2oA3ioT9PbHXsQ5AdGbUuGo/3iUR8x7pro1pAvgkJxoJb8VuO486QLsw 3YdMb1SicTE4DVap7q2zbcydnksA6nM5kvSL3znRQ4p7nXq3pviR+kQ+BnrKFZfjYyJDvG DLWPclhouNHWzaOANn3B5l1OQZWfM1Q= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-642-SbeU30hIOrqJZcnM7qyhCg-1; Wed, 17 Aug 2022 13:58:25 -0400 X-MC-Unique: SbeU30hIOrqJZcnM7qyhCg-1 Received: by mail-wm1-f69.google.com with SMTP id f18-20020a05600c4e9200b003a5f81299caso3479617wmq.7 for ; Wed, 17 Aug 2022 10:58:25 -0700 (PDT) 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; bh=FeC1hIbpeF331EYBQF/yk4Lw5Xn8tjgBYGOWKQGB6dY=; b=uEDISWf6R2ap7yd+FRvMZ6nW2EfJ52QEH57iG0y687skJhOBOUjhnIAMwe1qBlbCPZ r6lbEO0AawgxcgzS2k26hDAGjJofeYvSPu/0clnu0cEkPceasvon41CzWH17FtQNAH/G zmQeAURjCKu6GLT/XtRnD8kfDc/shUYHgE8p0s15dmov1L63r9mwB6cLJxSoiMkhAWQD DfD7z9DsoErQxZsGg/UuKnFz/2UtqRi/vp4caWvK53zB32D+jwFdCg/Qas7T+BwfuTba 8wSBEpiOmz1al3n3C/pna/Ds6QHL0dKaTEwPXkx0T7D50wTMHB2eOGenFU4FYWQ+QQbK n2Ww== X-Gm-Message-State: ACgBeo0UXjjuF5aFCCWmg6tUxpJlZHn7+HNJCFxf5gOh3Hi1wjsUhM6+ TNIa8Z0NqC99MfujLI25p/29eZ3Nf+jjJfJd+dKj7vgen8vAUb8Z9ZwP5yjj8XUxqO+HBhUDuqT 2GbutZocZ1xWZjLTwwnRuxQ== X-Received: by 2002:a05:600c:4ece:b0:3a6:28:bc59 with SMTP id g14-20020a05600c4ece00b003a60028bc59mr2841861wmq.154.1660759104262; Wed, 17 Aug 2022 10:58:24 -0700 (PDT) X-Google-Smtp-Source: AA6agR510plnN0HC3OB3lvW09uzTiD8Mi1hIvWoOCu1vCuuNhK8JvlAqdM5qXlzOkhVL+R6kqbyikQ== X-Received: by 2002:a05:600c:4ece:b0:3a6:28:bc59 with SMTP id g14-20020a05600c4ece00b003a60028bc59mr2841852wmq.154.1660759104030; Wed, 17 Aug 2022 10:58:24 -0700 (PDT) Received: from vschneid.remote.csb ([185.11.37.247]) by smtp.gmail.com with ESMTPSA id u8-20020a05600c19c800b003a541d893desm2809009wmq.38.2022.08.17.10.58.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Aug 2022 10:58:23 -0700 (PDT) From: Valentin Schneider To: netdev@vger.kernel.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Saeed Mahameed , Leon Romanovsky , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Yury Norov , Andy Shevchenko , Rasmus Villemoes , Ingo Molnar , Peter Zijlstra , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Mel Gorman , Greg Kroah-Hartman , Barry Song , Heiko Carstens , Tony Luck , Jonathan Cameron , Gal Pressman , Tariq Toukan Subject: [PATCH v2 1/5] bitops: Introduce find_next_andnot_bit() Date: Wed, 17 Aug 2022 18:58:08 +0100 Message-Id: <20220817175812.671843-2-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220817175812.671843-1-vschneid@redhat.com> References: <20220817175812.671843-1-vschneid@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org In preparation of introducing for_each_cpu_andnot(), add a variant of find_next_bit() that negate the bits in @addr2 when ANDing them with the bits in @addr1. Note that the _find_next_bit() @invert argument now gets split into two: @invert1 for words in @addr1, @invert2 for words in @addr2. The only current users of _find_next_bit() with @invert set are: o find_next_zero_bit() o find_next_zero_bit_le() and neither of these pass an @addr2, so the conversion is straightforward. Signed-off-by: Valentin Schneider --- include/linux/find.h | 44 ++++++++++++++++++++++++++++++++++++++------ lib/find_bit.c | 23 ++++++++++++----------- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/include/linux/find.h b/include/linux/find.h index 424ef67d4a42..920597de4e62 100644 --- a/include/linux/find.h +++ b/include/linux/find.h @@ -10,7 +10,8 @@ extern unsigned long _find_next_bit(const unsigned long *addr1, const unsigned long *addr2, unsigned long nbits, - unsigned long start, unsigned long invert, unsigned long le); + unsigned long start, unsigned long invert1, unsigned long invert2, + unsigned long le); extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size); extern unsigned long _find_first_and_bit(const unsigned long *addr1, const unsigned long *addr2, unsigned long size); @@ -41,7 +42,7 @@ unsigned long find_next_bit(const unsigned long *addr, unsigned long size, return val ? __ffs(val) : size; } - return _find_next_bit(addr, NULL, size, offset, 0UL, 0); + return _find_next_bit(addr, NULL, size, offset, 0UL, 0UL, 0); } #endif @@ -71,7 +72,38 @@ unsigned long find_next_and_bit(const unsigned long *addr1, return val ? __ffs(val) : size; } - return _find_next_bit(addr1, addr2, size, offset, 0UL, 0); + return _find_next_bit(addr1, addr2, size, offset, 0UL, 0UL, 0); +} +#endif + +#ifndef find_next_andnot_bit +/** + * find_next_andnot_bit - find the next set bit in one memory region + * but not in the other + * @addr1: The first address to base the search on + * @addr2: The second address to base the search on + * @size: The bitmap size in bits + * @offset: The bitnumber to start searching at + * + * Returns the bit number for the next set bit + * If no bits are set, returns @size. + */ +static inline +unsigned long find_next_andnot_bit(const unsigned long *addr1, + const unsigned long *addr2, unsigned long size, + unsigned long offset) +{ + if (small_const_nbits(size)) { + unsigned long val; + + if (unlikely(offset >= size)) + return size; + + val = *addr1 & ~*addr2 & GENMASK(size - 1, offset); + return val ? __ffs(val) : size; + } + + return _find_next_bit(addr1, addr2, size, offset, 0UL, ~0UL, 0); } #endif @@ -99,7 +131,7 @@ unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, return val == ~0UL ? size : ffz(val); } - return _find_next_bit(addr, NULL, size, offset, ~0UL, 0); + return _find_next_bit(addr, NULL, size, offset, ~0UL, 0UL, 0); } #endif @@ -247,7 +279,7 @@ unsigned long find_next_zero_bit_le(const void *addr, unsigned return val == ~0UL ? size : ffz(val); } - return _find_next_bit(addr, NULL, size, offset, ~0UL, 1); + return _find_next_bit(addr, NULL, size, offset, ~0UL, 0UL, 1); } #endif @@ -266,7 +298,7 @@ unsigned long find_next_bit_le(const void *addr, unsigned return val ? __ffs(val) : size; } - return _find_next_bit(addr, NULL, size, offset, 0UL, 1); + return _find_next_bit(addr, NULL, size, offset, 0UL, 0UL, 1); } #endif diff --git a/lib/find_bit.c b/lib/find_bit.c index 1b8e4b2a9cba..c46b66d7d2b4 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -21,27 +21,29 @@ #if !defined(find_next_bit) || !defined(find_next_zero_bit) || \ !defined(find_next_bit_le) || !defined(find_next_zero_bit_le) || \ - !defined(find_next_and_bit) + !defined(find_next_and_bit) || !defined(find_next_andnot_bit) /* * This is a common helper function for find_next_bit, find_next_zero_bit, and * find_next_and_bit. The differences are: - * - The "invert" argument, which is XORed with each fetched word before - * searching it for one bits. * - The optional "addr2", which is anded with "addr1" if present. + * - The "invert" arguments, which are XORed with each fetched word (invert1 + * for words in addr1, invert2 for those in addr2) before searching it for + * one bits. */ unsigned long _find_next_bit(const unsigned long *addr1, - const unsigned long *addr2, unsigned long nbits, - unsigned long start, unsigned long invert, unsigned long le) + const unsigned long *addr2, + unsigned long nbits, unsigned long start, + unsigned long invert1, unsigned long invert2, + unsigned long le) { unsigned long tmp, mask; if (unlikely(start >= nbits)) return nbits; - tmp = addr1[start / BITS_PER_LONG]; + tmp = addr1[start / BITS_PER_LONG] ^ invert1; if (addr2) - tmp &= addr2[start / BITS_PER_LONG]; - tmp ^= invert; + tmp &= addr2[start / BITS_PER_LONG] ^ invert2; /* Handle 1st word. */ mask = BITMAP_FIRST_WORD_MASK(start); @@ -57,10 +59,9 @@ unsigned long _find_next_bit(const unsigned long *addr1, if (start >= nbits) return nbits; - tmp = addr1[start / BITS_PER_LONG]; + tmp = addr1[start / BITS_PER_LONG] ^ invert1; if (addr2) - tmp &= addr2[start / BITS_PER_LONG]; - tmp ^= invert; + tmp &= addr2[start / BITS_PER_LONG] ^ invert2; } if (le) From patchwork Wed Aug 17 17:58:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 12946290 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 00733C3F6B0 for ; Wed, 17 Aug 2022 17:58:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241037AbiHQR6u (ORCPT ); Wed, 17 Aug 2022 13:58:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241065AbiHQR6g (ORCPT ); Wed, 17 Aug 2022 13:58:36 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9E2124966 for ; Wed, 17 Aug 2022 10:58:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660759112; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dgGyeroL7nQdNp4tnXnxYdnkM2nPtV/937q52s3gCa4=; b=T8rdQR51FBlo9TRCT4f0q2suxB3UwBnDTU3zCuTXnP+mhNY0AbVxFvMeFyDJgxjEyJ8pzD ZDGS9IRByra4uraOvDSf4JWLzmsOO/FU8mro4g0UC6NyzQUaLDfh35vM6Y0Kt54IoUs18M z2BKjyEylHh5CUwwa7DSbo4LFWOtAw0= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-47-mW8Adc0VO52kfSpSr6ElDw-1; Wed, 17 Aug 2022 13:58:27 -0400 X-MC-Unique: mW8Adc0VO52kfSpSr6ElDw-1 Received: by mail-wm1-f70.google.com with SMTP id ay27-20020a05600c1e1b00b003a5bff0df8dso998721wmb.0 for ; Wed, 17 Aug 2022 10:58:27 -0700 (PDT) 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; bh=dgGyeroL7nQdNp4tnXnxYdnkM2nPtV/937q52s3gCa4=; b=uc4HXlCXYhElH22tsYReaLFfcBMPVMx7VdygEGEep7HvMs30PJqSYdcS75JgDSpCYM iccrBNS2Rke0pGHhUqZ3RFQan7M6fLl6GFTgqfmV95R11sFRrRfqcIzuwB/vqDxc2a2B P5UgI6q1yZIS+0meiIuR5qGfhqYW4NsKFWTwly74QXegjmzXeD/Wgw5V6glCHNj3CM49 +3tkD5h8nX5l4BuaA2qWRmnkAJ2SngIqUcvKMhYRRkNu8BzQc/kkbmYr5LH9fYKqjcyA GvipEGrV+HFKFsq/D3I+E/x8lfr+zI/aq/kSTu0PBalEM03uCFbsq4KpcoPK7a9mrowy U71Q== X-Gm-Message-State: ACgBeo1zYspYEZl3cWdnqEfoLnHegVeUWnsNODpa1k4wRT5r6hwLtfz8 W6P9htVPgNeawvDhaPIwNoMTQjo7OPDmfBZjwVhxI7y0oU1LqwgjdodZVU2JHE4pYn9b6E2K1II BF7nme5KMsF5g1slpFDS1aA== X-Received: by 2002:a1c:7c18:0:b0:3a5:aaae:d203 with SMTP id x24-20020a1c7c18000000b003a5aaaed203mr2944202wmc.2.1660759106079; Wed, 17 Aug 2022 10:58:26 -0700 (PDT) X-Google-Smtp-Source: AA6agR6BuvGaPt3Y8Uq9xyqFkwCoOJ0UWHYzIzHfPTHj9ADsoXRbGLYtizHhA+vtJ3TFF0VSU9wZjQ== X-Received: by 2002:a1c:7c18:0:b0:3a5:aaae:d203 with SMTP id x24-20020a1c7c18000000b003a5aaaed203mr2944172wmc.2.1660759105855; Wed, 17 Aug 2022 10:58:25 -0700 (PDT) Received: from vschneid.remote.csb ([185.11.37.247]) by smtp.gmail.com with ESMTPSA id u8-20020a05600c19c800b003a541d893desm2809009wmq.38.2022.08.17.10.58.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Aug 2022 10:58:25 -0700 (PDT) From: Valentin Schneider To: netdev@vger.kernel.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Saeed Mahameed , Leon Romanovsky , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Yury Norov , Andy Shevchenko , Rasmus Villemoes , Ingo Molnar , Peter Zijlstra , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Mel Gorman , Greg Kroah-Hartman , Barry Song , Heiko Carstens , Tony Luck , Jonathan Cameron , Gal Pressman , Tariq Toukan Subject: [PATCH v2 2/5] cpumask: Introduce for_each_cpu_andnot() Date: Wed, 17 Aug 2022 18:58:09 +0100 Message-Id: <20220817175812.671843-3-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220817175812.671843-1-vschneid@redhat.com> References: <20220817175812.671843-1-vschneid@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org for_each_cpu_and() is very convenient as it saves having to allocate a temporary cpumask to store the result of cpumask_and(). The same issue applies to cpumask_andnot() which doesn't actually need temporary storage for iteration purposes. Following what has been done for for_each_cpu_and(), introduce for_each_cpu_andnot(). Signed-off-by: Valentin Schneider --- include/linux/cpumask.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index 0d435d0edbcb..295b137717bb 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -237,6 +237,25 @@ unsigned int cpumask_next_and(int n, const struct cpumask *src1p, nr_cpumask_bits, n + 1); } +/** + * cpumask_next_andnot - get the next cpu in *src1p & ~*src2p + * @n: the cpu prior to the place to search (ie. return will be > @n) + * @src1p: the first cpumask pointer + * @src2p: the second cpumask pointer + * + * Returns >= nr_cpu_ids if no further cpus set in *src1p & ~*src2p + */ +static inline +unsigned int cpumask_next_andnot(int n, const struct cpumask *src1p, + const struct cpumask *src2p) +{ + /* -1 is a legal arg here. */ + if (n != -1) + cpumask_check(n); + return find_next_andnot_bit(cpumask_bits(src1p), cpumask_bits(src2p), + nr_cpumask_bits, n + 1); +} + /** * for_each_cpu - iterate over every cpu in a mask * @cpu: the (optionally unsigned) integer iterator @@ -297,6 +316,25 @@ unsigned int __pure cpumask_next_wrap(int n, const struct cpumask *mask, int sta (cpu) = cpumask_next_and((cpu), (mask1), (mask2)), \ (cpu) < nr_cpu_ids;) +/** + * for_each_cpu_andnot - iterate over every cpu in one mask but not in the other + * @cpu: the (optionally unsigned) integer iterator + * @mask1: the first cpumask pointer + * @mask2: the second cpumask pointer + * + * This saves a temporary CPU mask in many places. It is equivalent to: + * struct cpumask tmp; + * cpumask_andnot(&tmp, &mask1, &mask2); + * for_each_cpu(cpu, &tmp) + * ... + * + * After the loop, cpu is >= nr_cpu_ids. + */ +#define for_each_cpu_andnot(cpu, mask1, mask2) \ + for ((cpu) = -1; \ + (cpu) = cpumask_next_andnot((cpu), (mask1), (mask2)), \ + (cpu) < nr_cpu_ids;) + /** * cpumask_any_but - return a "random" in a cpumask, but not this one. * @mask: the cpumask to search From patchwork Thu Aug 18 16:45:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 12947455 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 A640AC3F6B0 for ; Thu, 18 Aug 2022 16:45:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344965AbiHRQpq (ORCPT ); Thu, 18 Aug 2022 12:45:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36538 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344949AbiHRQpg (ORCPT ); Thu, 18 Aug 2022 12:45:36 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B98B4B99FC for ; Thu, 18 Aug 2022 09:45:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660841134; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qZr7gdCnQUFeKt5ugQdZLofQqTfC3GDipKWYJIctC0o=; b=bC3Ak9Wth7r0KqCXyA6QdLS0cdsUN66l2i2359vuRcM3SsT3EkKBqXxyQaTsP9JPYsPX82 b12a7PCWs9tfvpK53PJqDhW327PyMD21m1jdn8m68Tg03KudcjTu138b7WPSgoP0P0O8mY 5or1Vhh1P3riWAKilDtexhfDXwFLiuY= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-393-9gnavOOyO2SIWmK5r3fIUA-1; Thu, 18 Aug 2022 12:45:33 -0400 X-MC-Unique: 9gnavOOyO2SIWmK5r3fIUA-1 Received: by mail-wm1-f72.google.com with SMTP id q16-20020a1cf310000000b003a626026ed1so746004wmq.4 for ; Thu, 18 Aug 2022 09:45:33 -0700 (PDT) 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; bh=qZr7gdCnQUFeKt5ugQdZLofQqTfC3GDipKWYJIctC0o=; b=mXUHjZcglsBO/UipLCkRzaJsbrJ3+mLZPiQbNY9vEacL2Qz1AKt8Fhb8EkAODJY/iG Wo/Z0sap7QnrOavhKQ7HoTmLnyR+jG7POtTRJ0ffcxgh07i9jS2d2QON5JV4bCOGOAe2 Intvr/TN9wiLHCLZ8v8rCDiYYuV1yD/8e01jT1fkEVe957PT/DREtgUnlmS4PlDSlSFi bMZorYx3O60Eu3IBXQ+6j4fQLH9WLZX7YJFdeuQzjVn9jjHO1sLTEuoRCrWR8cjo836H hgUs5PAJ6JEc6/AKP9P720edH/yxFcqZkO2+vMAJNPyR4ry8/B2TvRiyA/TEXhFRiKMT rAaw== X-Gm-Message-State: ACgBeo1SfI+PLtzVEclOsaqcF2OYh1DbOyh6VwHWUiJi0B7riqTSL574 etw1vSmFVIk8Bjpk4ITsZvsUxkK7gHnFz02KdvShHTGDHwqgQ43VZfKFXdUuwird4+35iZkw2R3 kZb10M64LV7VigvRNjBSj6Q== X-Received: by 2002:a5d:5408:0:b0:220:63d5:d9f3 with SMTP id g8-20020a5d5408000000b0022063d5d9f3mr2077536wrv.249.1660841132684; Thu, 18 Aug 2022 09:45:32 -0700 (PDT) X-Google-Smtp-Source: AA6agR6gxwdIXrKECZW5GHwGnYxvYL4c65/BszpFIuTjU6CiGZ3DNUbfNM6q7fEaB9mFStW46VURNA== X-Received: by 2002:a5d:5408:0:b0:220:63d5:d9f3 with SMTP id g8-20020a5d5408000000b0022063d5d9f3mr2077533wrv.249.1660841132508; Thu, 18 Aug 2022 09:45:32 -0700 (PDT) Received: from vschneid.remote.csb ([185.11.37.247]) by smtp.gmail.com with ESMTPSA id d7-20020a05600c3ac700b003a5ad7f6de2sm2465458wms.15.2022.08.18.09.45.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Aug 2022 09:45:32 -0700 (PDT) From: Valentin Schneider To: netdev@vger.kernel.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Saeed Mahameed , Leon Romanovsky , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Yury Norov , Andy Shevchenko , Rasmus Villemoes , Ingo Molnar , Peter Zijlstra , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Mel Gorman , Greg Kroah-Hartman , Barry Song , Heiko Carstens , Tony Luck , Jonathan Cameron , Gal Pressman , Tariq Toukan Subject: [PATCH v2 3/5] sched/topology: Introduce sched_numa_hop_mask() Date: Thu, 18 Aug 2022 17:45:20 +0100 Message-Id: <20220818164522.1087673-1-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220817175812.671843-1-vschneid@redhat.com> References: <20220817175812.671843-1-vschneid@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Tariq has pointed out that drivers allocating IRQ vectors would benefit from having smarter NUMA-awareness - cpumask_local_spread() only knows about the local node and everything outside is in the same bucket. sched_domains_numa_masks is pretty much what we want to hand out (a cpumask of CPUs reachable within a given distance budget), introduce sched_numa_hop_mask() to export those cpumasks. Link: http://lore.kernel.org/r/20220728191203.4055-1-tariqt@nvidia.com Signed-off-by: Valentin Schneider --- include/linux/topology.h | 9 +++++++++ kernel/sched/topology.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/linux/topology.h b/include/linux/topology.h index 4564faafd0e1..13b82b83e547 100644 --- a/include/linux/topology.h +++ b/include/linux/topology.h @@ -245,5 +245,14 @@ static inline const struct cpumask *cpu_cpu_mask(int cpu) return cpumask_of_node(cpu_to_node(cpu)); } +#ifdef CONFIG_NUMA +extern const struct cpumask *sched_numa_hop_mask(int node, int hops); +#else +static inline const struct cpumask *sched_numa_hop_mask(int node, int hops) +{ + return ERR_PTR(-EOPNOTSUPP); +} +#endif /* CONFIG_NUMA */ + #endif /* _LINUX_TOPOLOGY_H */ diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c index 8739c2a5a54e..f0236a0ae65c 100644 --- a/kernel/sched/topology.c +++ b/kernel/sched/topology.c @@ -2067,6 +2067,34 @@ int sched_numa_find_closest(const struct cpumask *cpus, int cpu) return found; } +/** + * sched_numa_hop_mask() - Get the cpumask of CPUs at most @hops hops away. + * @node: The node to count hops from. + * @hops: Include CPUs up to that many hops away. 0 means local node. + * + * Requires rcu_lock to be held. Returned cpumask is only valid within that + * read-side section, copy it if required beyond that. + * + * Note that not all hops are equal in size; see sched_init_numa() for how + * distances and masks are handled. + * + * Also note that this is a reflection of sched_domains_numa_masks, which may change + * during the lifetime of the system (offline nodes are taken out of the masks). + */ +const struct cpumask *sched_numa_hop_mask(int node, int hops) +{ + struct cpumask ***masks = rcu_dereference(sched_domains_numa_masks); + + if (node >= nr_node_ids || hops >= sched_domains_numa_levels) + return ERR_PTR(-EINVAL); + + if (!masks) + return NULL; + + return masks[hops][node]; +} +EXPORT_SYMBOL_GPL(sched_numa_hop_mask); + #endif /* CONFIG_NUMA */ static int __sdt_alloc(const struct cpumask *cpu_map) From patchwork Thu Aug 18 16:45:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 12947454 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 BA437C32772 for ; Thu, 18 Aug 2022 16:45:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344930AbiHRQpp (ORCPT ); Thu, 18 Aug 2022 12:45:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344965AbiHRQpj (ORCPT ); Thu, 18 Aug 2022 12:45:39 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B90B0BA9E7 for ; Thu, 18 Aug 2022 09:45:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660841137; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ygl4bsiGppbXjaR+QFxwzenbyEWvTTquS8G3MYyuF5k=; b=KHJ91UGZyodTKLUqmrJt3u6AjctN+Gh/QVXyKc+DzO6nv/uz1vwXgHaHuIoBbIVJSwCBB8 x770RsdHInn8SCMNvEM/7pVZESB3wz/Bc8BJeJaTMHZbw8OJ2242ldZuIQOBrhTST+owgI M7hdiGxYRqlFVpprwj0tZrSdPC+h/I8= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-486-J7YHRaiXOyqQj6qEXXj3hA-1; Thu, 18 Aug 2022 12:45:36 -0400 X-MC-Unique: J7YHRaiXOyqQj6qEXXj3hA-1 Received: by mail-wm1-f69.google.com with SMTP id f7-20020a1c6a07000000b003a60ede816cso832172wmc.0 for ; Thu, 18 Aug 2022 09:45:36 -0700 (PDT) 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; bh=Ygl4bsiGppbXjaR+QFxwzenbyEWvTTquS8G3MYyuF5k=; b=G0xYLotlPc9aQc6Lr8bJ4R4IdaVRIWv36uNKykJtJJ49FB/MCECdgz8u/XhJ6b4bVh g/5XTJyNYocMa2+T8li4Aht/F8T2BsUFll4tddLDZHGGPBsg0ann+6t58SHtjT9HC1U5 Poki1KApoXOlBsqEcWU/1STw3M4ILKC9rzEjqbZQxqwpBpjapKz1coe65pPE6/Suqe+Y I6UsHFbeCVjHbSw7Nd9WZnU1AASGctgBduJoSaS1/gChMv4kqRf67Xwwfis/O8Ech5c6 SM2Jlxea/ascLOLCSgIi/iLlPo6jmTMp4n8aOGul6oj+E/xPkcthL50qgpbXH1tIKPrV BUpQ== X-Gm-Message-State: ACgBeo3B/CclL/kQHHgW/TyiQSUTkJJSDZb7AWsu3UJdZdE0ENUwp9L0 9f2sxkHnn5fEoAyvFYbkUcllz08R3jUv/SqV93MUnSV0OwlZdVqubAYHtLTtvp4FPOo3DE2M2OE eVXojA9zLiQ/LaWLp4xcURw== X-Received: by 2002:a5d:63cb:0:b0:21e:b81d:8b0d with SMTP id c11-20020a5d63cb000000b0021eb81d8b0dmr2101101wrw.526.1660841135369; Thu, 18 Aug 2022 09:45:35 -0700 (PDT) X-Google-Smtp-Source: AA6agR7i0NVijGHDSaLCCQYtUON7K5y9O7pv4DOZf3cy/HYjnD2P0jXlg2O08mjKG+P/oEyOridfcQ== X-Received: by 2002:a5d:63cb:0:b0:21e:b81d:8b0d with SMTP id c11-20020a5d63cb000000b0021eb81d8b0dmr2101084wrw.526.1660841135139; Thu, 18 Aug 2022 09:45:35 -0700 (PDT) Received: from vschneid.remote.csb ([185.11.37.247]) by smtp.gmail.com with ESMTPSA id d7-20020a05600c3ac700b003a5ad7f6de2sm2465458wms.15.2022.08.18.09.45.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Aug 2022 09:45:34 -0700 (PDT) From: Valentin Schneider To: netdev@vger.kernel.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Saeed Mahameed , Leon Romanovsky , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Yury Norov , Andy Shevchenko , Rasmus Villemoes , Ingo Molnar , Peter Zijlstra , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Mel Gorman , Greg Kroah-Hartman , Barry Song , Heiko Carstens , Tony Luck , Jonathan Cameron , Gal Pressman , Tariq Toukan Subject: [PATCH v2 4/5] sched/topology: Introduce for_each_numa_hop_cpu() Date: Thu, 18 Aug 2022 17:45:21 +0100 Message-Id: <20220818164522.1087673-2-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220818164522.1087673-1-vschneid@redhat.com> References: <20220817175812.671843-1-vschneid@redhat.com> <20220818164522.1087673-1-vschneid@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org The recently introduced sched_numa_hop_mask() exposes cpumasks of CPUs reachable within a given distance budget, but this means each successive cpumask is a superset of the previous one. Code wanting to allocate one item per CPU (e.g. IRQs) at increasing distances would thus need to allocate a temporary cpumask to note which CPUs have already been visited. This can be prevented by leveraging for_each_cpu_andnot() - package all that logic into one ugl^D fancy macro. Signed-off-by: Valentin Schneider --- include/linux/topology.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/include/linux/topology.h b/include/linux/topology.h index 13b82b83e547..6c671dc3252c 100644 --- a/include/linux/topology.h +++ b/include/linux/topology.h @@ -254,5 +254,42 @@ static inline const struct cpumask *sched_numa_hop_mask(int node, int hops) } #endif /* CONFIG_NUMA */ +/** + * for_each_numa_hop_cpu - iterate over CPUs by increasing NUMA distance, + * starting from a given node. + * @cpu: the iteration variable. + * @node: the NUMA node to start the search from. + * + * Requires rcu_lock to be held. + * Careful: this is a double loop, 'break' won't work as expected. + * + * + * Implementation notes: + * + * Providing it is valid, the mask returned by + * sched_numa_hop_mask(node, hops+1) + * is a superset of the one returned by + * sched_numa_hop_mask(node, hops) + * which may not be that useful for drivers that try to spread things out and + * want to visit a CPU not more than once. + * + * To accommodate for that, we use for_each_cpu_andnot() to iterate over the cpus + * of sched_numa_hop_mask(node, hops+1) with the CPUs of + * sched_numa_hop_mask(node, hops) removed, IOW we only iterate over CPUs + * a given distance away (rather than *up to* a given distance). + * + * hops=0 forces us to play silly games: we pass cpu_none_mask to + * for_each_cpu_andnot(), which turns it into for_each_cpu(). + */ +#define for_each_numa_hop_cpu(cpu, node) \ + for (struct { const struct cpumask *curr, *prev; int hops; } __v = \ + { sched_numa_hop_mask(node, 0), NULL, 0 }; \ + !IS_ERR_OR_NULL(__v.curr); \ + __v.hops++, \ + __v.prev = __v.curr, \ + __v.curr = sched_numa_hop_mask(node, __v.hops)) \ + for_each_cpu_andnot(cpu, \ + __v.curr, \ + __v.hops ? __v.prev : cpu_none_mask) #endif /* _LINUX_TOPOLOGY_H */ From patchwork Thu Aug 18 16:45:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 12947453 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 042FCC00140 for ; Thu, 18 Aug 2022 16:45:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344502AbiHRQpo (ORCPT ); Thu, 18 Aug 2022 12:45:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344973AbiHRQpl (ORCPT ); Thu, 18 Aug 2022 12:45:41 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EE6DBA9E7 for ; Thu, 18 Aug 2022 09:45:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660841139; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ywTMuS9huvM7zHWOxR6GjSmCdhb3SJi0oS2399XlZwU=; b=D79UzXcHA7YAvj+O6XmGbxReWxL3xzxKhKp4R1Otbgv3Vk0hzPNBMuqN31CGYRJP7yuA9O zACkadpB8NGMmU2zR7QR0IApc5W0tZbfu3v6BDDLCUWTp3UGei2Qd8UTT6lkE1pcgti59t XKspMVZbLvwgjoOvQnlIZWpgNZ86klM= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-150-zdfFZprLPleARRD0QqZtoA-1; Thu, 18 Aug 2022 12:45:38 -0400 X-MC-Unique: zdfFZprLPleARRD0QqZtoA-1 Received: by mail-wm1-f71.google.com with SMTP id r5-20020a1c4405000000b003a5fa79008bso91473wma.5 for ; Thu, 18 Aug 2022 09:45:38 -0700 (PDT) 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; bh=ywTMuS9huvM7zHWOxR6GjSmCdhb3SJi0oS2399XlZwU=; b=bnfYz6EJoeocEtK6imonYgE0e39B9jfOwDE8k1EDJwvHGhUtt+7CGqlkGA/9duK/aO 6b5urpc1CCNfsJYMwLiCgahndFhGUqvDaAUt9W/RAPsHhJmnXC55M1G16r9MrUZ5q/Fk EoZYAYf+jTWroySdgApqDpHuHhjCLeu/6S0RzLtuSKH7uTR0EoBDzN3njeG/rH2JFrOJ z5XnuBsgQmpuReTidRGG8gADGJvHwuFlj/ghYOxZp8IzM5g5pdcoQD25dcDlLPihs/kq sbj//mkHA6i+PRerl44FtMvk5BWebq42KaY/ocU7S3n6pVTjI8tOuaeVsYh1SJCO6E9z FraQ== X-Gm-Message-State: ACgBeo02Yv4RyVszcY8vAp8dyuS35wysDgsAZdVT8Xt1JNxrXWniRM1b 3ljPE9D42we32U/vzberzN18VEJ1Wx+LlmC7h9NVoJuuMGcdU/ml15WzQxP+7k66yAgq8E66HDh YqnWr8g+HKCQIvDyMHjQiuA== X-Received: by 2002:a05:6000:1f08:b0:21f:bbe:252c with SMTP id bv8-20020a0560001f0800b0021f0bbe252cmr2104347wrb.340.1660841137316; Thu, 18 Aug 2022 09:45:37 -0700 (PDT) X-Google-Smtp-Source: AA6agR5V5LqD6L/uZK5MQwaiVfgT2IX7c0E6MzpdjvY5DMfJ6PdmT5prIm5y5OBYNyJ1oo+5n3JYqw== X-Received: by 2002:a05:6000:1f08:b0:21f:bbe:252c with SMTP id bv8-20020a0560001f0800b0021f0bbe252cmr2104328wrb.340.1660841137167; Thu, 18 Aug 2022 09:45:37 -0700 (PDT) Received: from vschneid.remote.csb ([185.11.37.247]) by smtp.gmail.com with ESMTPSA id d7-20020a05600c3ac700b003a5ad7f6de2sm2465458wms.15.2022.08.18.09.45.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Aug 2022 09:45:36 -0700 (PDT) From: Valentin Schneider To: netdev@vger.kernel.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Saeed Mahameed , Leon Romanovsky , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Yury Norov , Andy Shevchenko , Rasmus Villemoes , Ingo Molnar , Peter Zijlstra , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Mel Gorman , Greg Kroah-Hartman , Barry Song , Heiko Carstens , Tony Luck , Jonathan Cameron , Gal Pressman , Tariq Toukan Subject: [PATCH v2 5/5] SHOWCASE: net/mlx5e: Leverage for_each_numa_hop_cpu() Date: Thu, 18 Aug 2022 17:45:22 +0100 Message-Id: <20220818164522.1087673-3-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220818164522.1087673-1-vschneid@redhat.com> References: <20220817175812.671843-1-vschneid@redhat.com> <20220818164522.1087673-1-vschneid@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Not-signed-off-by: Valentin Schneider --- drivers/net/ethernet/mellanox/mlx5/core/eq.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/mellanox/mlx5/core/eq.c b/drivers/net/ethernet/mellanox/mlx5/core/eq.c index 229728c80233..0a5432903edd 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/eq.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/eq.c @@ -812,6 +812,7 @@ static int comp_irqs_request(struct mlx5_core_dev *dev) int ncomp_eqs = table->num_comp_eqs; u16 *cpus; int ret; + int cpu; int i; ncomp_eqs = table->num_comp_eqs; @@ -830,8 +831,15 @@ static int comp_irqs_request(struct mlx5_core_dev *dev) ret = -ENOMEM; goto free_irqs; } - for (i = 0; i < ncomp_eqs; i++) - cpus[i] = cpumask_local_spread(i, dev->priv.numa_node); + + rcu_read_lock(); + for_each_numa_hop_cpus(cpu, dev->priv.numa_node) { + cpus[i] = cpu; + if (++i == ncomp_eqs) + goto spread_done; + } +spread_done: + rcu_read_unlock(); ret = mlx5_irqs_request_vectors(dev, cpus, ncomp_eqs, table->comp_irqs); kfree(cpus); if (ret < 0)