Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp3357355rwb; Mon, 5 Sep 2022 10:11:19 -0700 (PDT) X-Google-Smtp-Source: AA6agR6FZmL0ZYeZ14woDbJO7irQZsVe+/J2frb59JUesLOfJ6SUSnpuv7zQk5sa3gbIvwxIhQSp X-Received: by 2002:a05:6402:3507:b0:448:b672:55ee with SMTP id b7-20020a056402350700b00448b67255eemr27622320edd.107.1662397879174; Mon, 05 Sep 2022 10:11:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662397879; cv=none; d=google.com; s=arc-20160816; b=k58ZKRieBDoySlSzlB5jV93HjYDaYHTNQJxNrWonWz9RtLsuBAWiNY+vfgAQZ+qHsG 7fJm3ZUL27DPyJ2sxZ+yYyYtHCejjHdKQPsjl7uip8uMBayNER6x7jbriim1F2h1v+dT yHTi3GcsjjEsN8cDj/gt0WhjLBFsgUoTh5tg+f/LGuR/aAbwTkdbRekfvCCwiGgzle0D spoqSUtuZcAHhIVNv9X75UEiTkYqJnBol1WXdhIfor4U5J0sqBWdZztEcMClXpLEtvyx CUwv/7/1iuATGzTWRLoTzbNvOaMdJ1pMAei7YXL7LBFqlYaBNtW/WefJCWrvujkbJpZb jQOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=pvPW0cjV7c+Ybyz5Fia/K22ovnKJbGa0LNbZZE3i9A4=; b=Wilc8oV65/UZg4IVJwrpaHh+x13XZzHC2GaT6RUgXMP+lMlQS0QdNSl2C/0gh3a8d7 KI5wol1cf9geg2dJ3vZoUa9ncDr38FOaWJE8Qca+dcbpzTAjx5w2Qp2s858kcHutsQ1P mjCX/tnevmrHLr7lTuQyYR2eaM4kpviprZstIEqyqMqvkdhTMO8A3BY0/vFi4GnrjNeX 0G5MYbJ7WilPYG4odYzQR5vfECzcUieLC2i0JWOVPcgndl0sZmJ/CxgHmX6tE1jmAkmI BLh3vgXk9PU2KVc7kU27kbKQUq8LhuBiLh4S5Ip5qSrk+ZCWlD1FqPGx7o8NYkQNAmjA mtVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iZGfukyp; 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 fd5-20020a056402388500b0044dc1faa85dsi3937766edb.253.2022.09.05.10.10.54; Mon, 05 Sep 2022 10:11:19 -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=iZGfukyp; 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 S237313AbiIEQob (ORCPT + 99 others); Mon, 5 Sep 2022 12:44:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55506 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236740AbiIEQo0 (ORCPT ); Mon, 5 Sep 2022 12:44:26 -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 9BCC2C5E for ; Mon, 5 Sep 2022 09:44:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1662396259; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=pvPW0cjV7c+Ybyz5Fia/K22ovnKJbGa0LNbZZE3i9A4=; b=iZGfukypy6jv6hlEJTYxa45ndVDnMz3vOT42GshaZQOlgLeWUx6SCFnpIf+OhX9yUWON2C +/okNueZ+NZYclOcmMlXQ6NS6Z1Aqixhz6Oe2pG+xwGnX04XVhRPEnvuwVc443eGpnTux1 vPTNzBWvDzMg2amxhmdvGIPXrWC5hTw= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-661-Ij-29KJ6OEKa0NgnfCyXWQ-1; Mon, 05 Sep 2022 12:44:18 -0400 X-MC-Unique: Ij-29KJ6OEKa0NgnfCyXWQ-1 Received: by mail-wr1-f72.google.com with SMTP id r23-20020adfb1d7000000b002286358a916so609200wra.3 for ; Mon, 05 Sep 2022 09:44:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date; bh=pvPW0cjV7c+Ybyz5Fia/K22ovnKJbGa0LNbZZE3i9A4=; b=4jUyffeCvx65s3w3IvEWVehtulOE8gX0pp37AyfsD1H4f3Ipm1oDtUuG1w15fCUg7k 3U1vxbVchMZT9M+CadhGwsl51mlILILshxTjnQTelwokXG8gJEGRMxRsUcfCdWzD9uwo ZnyhMnHGre1MdgQNe7ulE5ekeIenCuAQaUIFqxg27Xu1kxZADrlY7/0GrF+KEDekmiat XHoRvuTsCUj4ggoyDAO8+0JamfGoqVu6WTd2/+8DQ6haJ7InH8kiKJ7wOE5qK9WixTHI pqCpW0QqkgVQ93XYupHoI7wsPKTsJkTJ63H32vFiVAvfvYSRa/savv4++59zrCdfG7wd AUlw== X-Gm-Message-State: ACgBeo0i0NKCASEzrAHWMOqavM8izVah1zj+ZQCcS/fWc9bdb6WLtFi7 2ELGpvcQbJ3zR1nZyqnuMAQQEmh9+cMbgHhfoUU4kKWfh5HMDt2vwMC9l8wrJSqZMKRDtzRm+5H ipX+m3Ie+mLS+aUtM612qwjjB X-Received: by 2002:a05:600c:22ca:b0:3a5:c30d:ca9f with SMTP id 10-20020a05600c22ca00b003a5c30dca9fmr11310673wmg.25.1662396257249; Mon, 05 Sep 2022 09:44:17 -0700 (PDT) X-Received: by 2002:a05:600c:22ca:b0:3a5:c30d:ca9f with SMTP id 10-20020a05600c22ca00b003a5c30dca9fmr11310648wmg.25.1662396257014; Mon, 05 Sep 2022 09:44:17 -0700 (PDT) Received: from vschneid.remote.csb ([185.11.37.247]) by smtp.gmail.com with ESMTPSA id l10-20020a1ced0a000000b003a8436e2a94sm11092786wmh.16.2022.09.05.09.44.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Sep 2022 09:44:16 -0700 (PDT) From: Valentin Schneider To: Tariq Toukan , 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: Re: [PATCH v3 8/9] sched/topology: Introduce for_each_numa_hop_cpu() In-Reply-To: References: <20220825181210.284283-1-vschneid@redhat.com> <20220825181210.284283-9-vschneid@redhat.com> Date: Mon, 05 Sep 2022 17:44:15 +0100 Message-ID: MIME-Version: 1.0 Content-Type: text/plain 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,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 On 05/09/22 12:46, Tariq Toukan wrote: > On 8/25/2022 9:12 PM, Valentin Schneider wrote: >> 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) >> > > Hiding two nested loops together in one for_each_* macro leads to > unexpected behavior for the standard usage of 'break/continue'. > > for_each_numa_hop_cpu(cpu, node) { > if (condition) > break; <== will terminate the inner loop only, but it's > invisible to the human developer/reviewer. > } > > These bugs will not be easy to spot in code review. > Yeah, it's not ideal, but I *did* attach a comment warning about it. Spreading this pattern for sure isn't great, but there *is* a precedent with for_each_process_thread(). >> #endif /* _LINUX_TOPOLOGY_H */