Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp962647rwe; Thu, 25 Aug 2022 12:26:23 -0700 (PDT) X-Google-Smtp-Source: AA6agR7wlaZgDPZYl9ShEZcwy5koWpe9VFRaEOLC/cOleMPsoaTcbsL2EohftxizwJ5PnHw1Ezt8 X-Received: by 2002:a17:907:b14:b0:73d:bbf2:793 with SMTP id h20-20020a1709070b1400b0073dbbf20793mr3323757ejl.199.1661455583628; Thu, 25 Aug 2022 12:26:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661455583; cv=none; d=google.com; s=arc-20160816; b=nsClHbm8BpVCQ90zPESz1IWtw0mBSjc3X1GGqS7IVpQgIesj2Ta+wuQOZ8KfpJGD7O JxPPTtluKRj02BeTUWX74Kn8HsKeqIvR6hjkHzLamgsJUZmvxDBLooISzvgz0gVo1q9a KUGhQyv/oEaz6vqOzwJb5A3QKnhD75LMlIN9r1tdAZMA1ZyR/Q+uLvfqZ4wFhxTm5kYu RCIlGeRWupR97TwDuwaqkdSmED4HJSkbgRMluzFbknL1xU9obq7p5Q3ZEWGkRhnBq6F6 ReF9GmY1v8UEqQU2gsS/iO7h3VRzBgz/yzvfyvGfrmrh5SMZ/E+tyh/QXw83x88zr3E1 wMUA== 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=Ygl4bsiGppbXjaR+QFxwzenbyEWvTTquS8G3MYyuF5k=; b=CPpy+i9DgJ+t8IcjxFcOx/dyLiZCeFcmEjll9HUSp/a1KPufrP4vCYm3YFBIdWDbxF iuQmtnFweUJuJe7I/LLZrPB3D33SrccPMcIFTXnUagRKV2ZCCAHq4W/BQJVP6FCeBacK albi2VnlpvGIsArJcRIyXlP8QWnolSzQmsVBB/xQMi2AL9eSUGaHRg36ScykqY8KSb1b 2U/DeODyWMzwRvZ3U7SZSHVxF+zFx51ecLIjy1CVJH2b+cMaKOHKX40ICcMea2hm//Gb gGMhk3ckiJV8y8FHgmOxfXn0E7yHOQIltWdR5Bgd2Hz7w7hLBrx5H2+cQGPlqHZf1VWr 8xtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gjZO7aIV; 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 4-20020a170906328400b0073d6f5dde1asi7996ejw.498.2022.08.25.12.25.55; Thu, 25 Aug 2022 12:26:23 -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=gjZO7aIV; 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 S243482AbiHYSNm (ORCPT + 99 others); Thu, 25 Aug 2022 14:13:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243069AbiHYSNJ (ORCPT ); Thu, 25 Aug 2022 14:13:09 -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 89573BD29C for ; Thu, 25 Aug 2022 11:12:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661451172; 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=gjZO7aIVARYnOyfIL8a9EN9KO2fTaxR3kUnPE2HeIvu546LCw4dBbTAyxdQkkNVyXCg4QR saVshhGoFmq3B5YOnXw4WPU1Qdv5aOlY/w31F/V/0voikw5khIkxgttemJBv+6ne83qlCk /EXxI75PcPUilpK0u/sgRjRpUg3Sfvg= 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-668-OaXISu9QPw6zBye0Q4G_Ww-1; Thu, 25 Aug 2022 14:12:51 -0400 X-MC-Unique: OaXISu9QPw6zBye0Q4G_Ww-1 Received: by mail-wm1-f70.google.com with SMTP id j22-20020a05600c485600b003a5e4420552so2766236wmo.8 for ; Thu, 25 Aug 2022 11:12:51 -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=Z66IxMRyGQTuaUhIcTDlHeZlHMDZdSic9/RMRc+HAUoeCyS9c5ilKNr2RAD3S+rNYe JrYrVHGsF2qekbYxleJYG/839tIaGJAu2G0VZN4vQihLhNpu1TlsLc3KfmkOnStVjRD3 +lny77F22fI0kyHx66Z9C07UFtBvzI4v75tb0mrW7Kk0L9vcMZr9EK2tE+tliQIB67dC wGDlXzLMpMOZvmgCvNwbwZ7pF5/Rbtt7k/ID/sZNFvg/9I9rGyYgv1uBwh5TEZZqJP1X 4aRhfdkex3fgn0HDBW0s2ZzppVyLFxWZElucFJ+fb3N/z/KjNDk0UHaDs0lIqT5/k53F bFSQ== X-Gm-Message-State: ACgBeo0Iys8VOhk2F5rzlqlu04RBVNRrcPMuHb4KgVMZ8uKBB6dLVohZ 6z55m1abqaFedG+2D/gZ42y8kiQcGL9BJhYQmvgW0xUDDpFgaNsQQcxGFG5QrgRGPPkk5cVhqVP crqDwilP3Qt601XPyFzOYtXps X-Received: by 2002:adf:e10c:0:b0:225:3168:c261 with SMTP id t12-20020adfe10c000000b002253168c261mr2960618wrz.159.1661451170438; Thu, 25 Aug 2022 11:12:50 -0700 (PDT) X-Received: by 2002:adf:e10c:0:b0:225:3168:c261 with SMTP id t12-20020adfe10c000000b002253168c261mr2960588wrz.159.1661451170194; Thu, 25 Aug 2022 11:12:50 -0700 (PDT) Received: from vschneid.remote.csb ([185.11.37.247]) by smtp.gmail.com with ESMTPSA id w1-20020a05600018c100b00225250f2d1bsm20371622wrq.94.2022.08.25.11.12.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Aug 2022 11:12:49 -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 v3 8/9] sched/topology: Introduce for_each_numa_hop_cpu() Date: Thu, 25 Aug 2022 19:12:09 +0100 Message-Id: <20220825181210.284283-9-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220825181210.284283-1-vschneid@redhat.com> References: <20220825181210.284283-1-vschneid@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 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 */ -- 2.31.1