Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1194978rwb; Fri, 23 Sep 2022 09:18:15 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5Rld0V96hlMt6MxgVANvNiYGwkcYTPhesrTVPxbqKsMSjlNfR5Ps9+NOfyAiVh1Dd20WSh X-Received: by 2002:a65:58cc:0:b0:43c:2fc6:e7d7 with SMTP id e12-20020a6558cc000000b0043c2fc6e7d7mr5410017pgu.478.1663949894609; Fri, 23 Sep 2022 09:18:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663949894; cv=none; d=google.com; s=arc-20160816; b=nkQ2KF7ql+RCxq+pyzLIlX9QKY7yla+nVr3J1EkFdFWn2bkRnp2uIm8UFEaSC9+01o G3cfUiqTKje+IQ8Wf3ITbx6wrdoopPYE22KE6ymJcVzJoIHxhwCM5NZP7nGEaE67BNKy G40D7HeCEYJod6T3e6DMU2sdEM5BPdUOkUr86WJ0XYw7qOq11Z24+x5WOwOiMlHEThBS ECEUmXNFCMnb7nKL6WzKB2hBX3kokc57tJ/XiNc7Dwcq+/TrqCHrJb/DHYHj7sfEVNzx Zwhq5nBFrsHHEPL1cyRSyxVJJ2G2+7ZQWoR+2Ox9MQ6ozAfX+HV8iG7xpnX76pTyj2JN Ej8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+vdqLCntBqaxf8FADpHkJLlOi77ldZqexu1XSAK/G7U=; b=Vn87P/nd94QggglEcLvYR36sGPpL4VyN9z1WCwc8//IuDRH+RyKSz4ijzSfREkBcUC pnso6mpBUfE7cK+u3uyFVFDkzLJapp6nRt5KjR2nn7Bzn07F1ZgFCmeIeKHofzBisZTU Z9hQY+la3fbLhy7TnGqWO09+D+bjR1oYekQAp/VVgnT11UpQK6iBf6Bg2yTv3wJ1KVUS oK+n7hPba9EGbMMEcto0udsJoguk4hZI2+a3ahKGdvMa6eUMeUBKwxQsXHbBvSTgAaKk CVNRXsFQ/PjhNHeYd8YVwX90v0i9xF9a9ER+qMz88h3Tf/SoQW+qwYEqaXujJx3xE1s6 O3hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Nqix3/gq"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x2-20020a656aa2000000b00434369f0878si10607469pgu.436.2022.09.23.09.17.59; Fri, 23 Sep 2022 09:18:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Nqix3/gq"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232817AbiIWP4i (ORCPT + 99 others); Fri, 23 Sep 2022 11:56:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229787AbiIWP4O (ORCPT ); Fri, 23 Sep 2022 11:56:14 -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 B67DF147692 for ; Fri, 23 Sep 2022 08:56:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663948569; 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=+vdqLCntBqaxf8FADpHkJLlOi77ldZqexu1XSAK/G7U=; b=Nqix3/gqX6Den5SbB7mbrnYbYd3jhwGksXezS92QMiXcRDBxCPBbqqjt5lRphWeTEPfxW6 HFB6ysQrKP0FT683sIxzYzlNWwtNS1g63pIJ9aroroY5PsHHR2j4/rd5Oegcc+/gqAR/uZ qpVE4+Scmk6fyGtP3mGIzLEbK83V1K8= 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-301-C7FGtdC7PNaG4ALFi6y13A-1; Fri, 23 Sep 2022 11:56:07 -0400 X-MC-Unique: C7FGtdC7PNaG4ALFi6y13A-1 Received: by mail-wm1-f69.google.com with SMTP id p36-20020a05600c1da400b003b4faefa2b9so298544wms.6 for ; Fri, 23 Sep 2022 08:56:07 -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 :subject:date; bh=+vdqLCntBqaxf8FADpHkJLlOi77ldZqexu1XSAK/G7U=; b=suzgXS4ofNoJSW1FBBz6zV2c+3l9t4WBgwYvfLV65cNyTautQ+1e0uIeN+e6gXrZVi czYYkpuaYji0QQfUaSHUDFRyuvB9NFdyGYVP4dO0Y5Ydc8TiPluOylKn05e4h43W7UcL EdD0BGQRCOuThMhEX+7nFSE9RWsiBZT2hEPT0WFixfru6TZTYqcxMA+c/6KovQMWjFpd nNlmtO8NjumtzFA3PrxqP/z5tSxbsQQxmy3IO1yIGxlQgKpbRGp7MPQ/Ko6Vql5/tdlM iypSDxogcu7SRPlOqdNSztfpniMfwBP0NLR8vblp+dO4PNIkr9C2YgXZybv5Ls9POzoj 8i9w== X-Gm-Message-State: ACrzQf2fXzZ7y2byOnckBaFZt0ggCFvB6v3YTlhBBwjHZHyt2LSMTc5p 15nHe52Yr+LNIDADrmoN3hB7SVRpOqX5Dc9Z064w6F6owgMlnFb9OQmWJtvObVXiDTJ27JWn08P j3Hf3sXdHUiz+BaK6PRz7w1ie X-Received: by 2002:a7b:c457:0:b0:3b4:689d:b408 with SMTP id l23-20020a7bc457000000b003b4689db408mr13584462wmi.22.1663948566651; Fri, 23 Sep 2022 08:56:06 -0700 (PDT) X-Received: by 2002:a7b:c457:0:b0:3b4:689d:b408 with SMTP id l23-20020a7bc457000000b003b4689db408mr13584449wmi.22.1663948566427; Fri, 23 Sep 2022 08:56:06 -0700 (PDT) Received: from vschneid.remote.csb ([185.11.37.247]) by smtp.gmail.com with ESMTPSA id i7-20020a5d5587000000b0021badf3cb26sm9055429wrv.63.2022.09.23.08.56.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Sep 2022 08:56:05 -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 , Heiko Carstens , Tony Luck , Jonathan Cameron , Gal Pressman , Tariq Toukan , Jesse Brandeburg Subject: [PATCH v4 6/7] sched/topology: Introduce for_each_numa_hop_cpu() Date: Fri, 23 Sep 2022 16:55:41 +0100 Message-Id: <20220923155542.1212814-5-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220923132527.1001870-1-vschneid@redhat.com> References: <20220923132527.1001870-1-vschneid@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 3e91ae6d0ad5..7aa7e6a4c739 100644 --- a/include/linux/topology.h +++ b/include/linux/topology.h @@ -257,5 +257,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 */ -- 2.31.1