From nobody Fri Sep 12 07:29:48 2025 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 5814BEE4993 for ; Sat, 19 Aug 2023 14:13:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233205AbjHSONz (ORCPT ); Sat, 19 Aug 2023 10:13:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229923AbjHSONx (ORCPT ); Sat, 19 Aug 2023 10:13:53 -0400 Received: from mail-yw1-x1129.google.com (mail-yw1-x1129.google.com [IPv6:2607:f8b0:4864:20::1129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B226D93E1 for ; Sat, 19 Aug 2023 07:12:44 -0700 (PDT) Received: by mail-yw1-x1129.google.com with SMTP id 00721157ae682-589c7801d1cso20132857b3.0 for ; Sat, 19 Aug 2023 07:12:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692454363; x=1693059163; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uf9CTvZgeFnZIxznv1sHeC3yripV+bDt6DFW7z/9P+A=; b=gc/1EWDc23XJKVaSsGUTWb76KcCm7bL450mDTtcfp5orDsZ0x+Im2b89o82pkGYPWI 2kKh97M0Pq7E6YI2JQ/rY98jLUI6q+y6Lfk5DxI3rTh/kXImFMu6dvwiSxbbwTFx4DWs BmPmqnq0ztWSsuWtLDuqVl8xesUfwyR/COfFhlO+wFNYUEJbVGZOGxxCyhaqLsZpUhHt psuAlS3iNnxR5tqGB6vLwR8cAkieCaqf8MJCvd4Kd5NQMPMeZf1RaksZoemImF2e5Sps Xq8VtTGwn6+KwuC2/RzFwbmFFxUBOeaU4SX5xY1EWJUuY/hz2lGPWJifS4G6MR1+kdNA ApSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692454363; x=1693059163; 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:message-id:reply-to; bh=uf9CTvZgeFnZIxznv1sHeC3yripV+bDt6DFW7z/9P+A=; b=EN75cyIsRsyhdhQbCLdxDDKx8OAPKxupUC5PT03cT/RgT3D2NS1jjhTlddYxbwJItW nK4xXBNYDF1GtcZWbJWkv/tPhapDhRwHlBOi4e4CivaP9qmjiiBEAC793VYfoKXQH3Ut TeZqoaGnecA311HF0ACWKim4dLR3TrQL7dofbX3k0av44hAads0SkY/pzgkD+wRQsbHS K6NAW3mwTTt0BRhLbhwqaYnCsgi4GwhS0MATMhiQc09NMMG+d2w+sMhTbtTtS3h48h1g h8eMFO4Zf2xIDCN7UR/t0Bmdo0J2KOXL9fZEuZhxCgueTcDSIjsAySletzzX2ggDrWth 9Yfw== X-Gm-Message-State: AOJu0Yye1/ZCo6/DS+3BXq2b9Q/eME1JslZM90Q9vSTfunKSeKWBOEka cFfPRaDhEfRD8UP23UE+X88RWOFsEo4= X-Google-Smtp-Source: AGHT+IEoff5wDnqvsze/t8OiCL4lM8ZHmxWRyutUm7aAYNfsUsVv7+2AJf68cb+zqoIwdne80+wp6A== X-Received: by 2002:a0d:eb8f:0:b0:589:c2fe:2afb with SMTP id u137-20020a0deb8f000000b00589c2fe2afbmr2161158ywe.30.1692454363496; Sat, 19 Aug 2023 07:12:43 -0700 (PDT) Received: from localhost ([32.218.242.113]) by smtp.gmail.com with ESMTPSA id g20-20020a0ce4d4000000b0063d245c00b5sm1482645qvm.0.2023.08.19.07.12.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Aug 2023 07:12:43 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Yury Norov , Ingo Molnar , Peter Zijlstra , Andrew Morton , Ben Segall , Daniel Bristot de Oliveira , Dietmar Eggemann , Jacob Keller , Jakub Kicinski , Juri Lelli , Mel Gorman , Steven Rostedt , Tariq Toukan , Valentin Schneider , Vincent Guittot , shiju.jose@huawei.com, jonathan.cameron@huawei.com, prime.zeng@huawei.com, linuxarm@huawei.com, yangyicong@hisilicon.com, Andy Shevchenko , Rasmus Villemoes Subject: [PATCH v2 1/6] numa: generalize numa_map_to_online_node() Date: Sat, 19 Aug 2023 07:12:33 -0700 Message-Id: <20230819141239.287290-2-yury.norov@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230819141239.287290-1-yury.norov@gmail.com> References: <20230819141239.287290-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" The function in fact searches the nearest node for a given one, based on a N_ONLINE state. This is a common pattern to search for a nearest node. This patch converts numa_map_to_online_node() to numa_nearest_node() so that others won't need to opencode the logic. Signed-off-by: Yury Norov --- include/linux/numa.h | 7 +++++-- mm/mempolicy.c | 18 +++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/include/linux/numa.h b/include/linux/numa.h index 59df211d051f..fb30a42f0700 100644 --- a/include/linux/numa.h +++ b/include/linux/numa.h @@ -25,7 +25,7 @@ #include =20 /* Generic implementation available */ -int numa_map_to_online_node(int node); +int numa_nearest_node(int node, unsigned int state); =20 #ifndef memory_add_physaddr_to_nid static inline int memory_add_physaddr_to_nid(u64 start) @@ -44,10 +44,11 @@ static inline int phys_to_target_node(u64 start) } #endif #else /* !CONFIG_NUMA */ -static inline int numa_map_to_online_node(int node) +static inline int numa_nearest_node(int node, unsigned int state) { return NUMA_NO_NODE; } + static inline int memory_add_physaddr_to_nid(u64 start) { return 0; @@ -58,6 +59,8 @@ static inline int phys_to_target_node(u64 start) } #endif =20 +#define numa_map_to_online_node(node) numa_nearest_node(node, N_ONLINE) + #ifdef CONFIG_HAVE_ARCH_NODE_DEV_GROUP extern const struct attribute_group arch_node_dev_group; #endif diff --git a/mm/mempolicy.c b/mm/mempolicy.c index c53f8beeb507..0fc9a3b1d765 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -131,22 +131,26 @@ static struct mempolicy default_policy =3D { static struct mempolicy preferred_node_policy[MAX_NUMNODES]; =20 /** - * numa_map_to_online_node - Find closest online node + * numa_nearest_node - Find nearest node by state * @node: Node id to start the search + * @state: State to filter the search * - * Lookup the next closest node by distance if @nid is not online. + * Lookup the closest node by distance if @nid is not in state. * - * Return: this @node if it is online, otherwise the closest node by dista= nce + * Return: this @node if it is in state, otherwise the closest node by dis= tance */ -int numa_map_to_online_node(int node) +int numa_nearest_node(int node, unsigned int state) { int min_dist =3D INT_MAX, dist, n, min_node; =20 - if (node =3D=3D NUMA_NO_NODE || node_online(node)) + if (state >=3D NR_NODE_STATES) + return -EINVAL; + + if (node =3D=3D NUMA_NO_NODE || node_state(node, state)) return node; =20 min_node =3D node; - for_each_online_node(n) { + for_each_node_state(n, state) { dist =3D node_distance(node, n); if (dist < min_dist) { min_dist =3D dist; @@ -156,7 +160,7 @@ int numa_map_to_online_node(int node) =20 return min_node; } -EXPORT_SYMBOL_GPL(numa_map_to_online_node); +EXPORT_SYMBOL_GPL(numa_nearest_node); =20 struct mempolicy *get_task_policy(struct task_struct *p) { --=20 2.39.2 From nobody Fri Sep 12 07:29:48 2025 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 13C1EEE4993 for ; Sat, 19 Aug 2023 14:15:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233242AbjHSOO7 (ORCPT ); Sat, 19 Aug 2023 10:14:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229923AbjHSOO6 (ORCPT ); Sat, 19 Aug 2023 10:14:58 -0400 Received: from mail-qv1-xf2f.google.com (mail-qv1-xf2f.google.com [IPv6:2607:f8b0:4864:20::f2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F294693FD for ; Sat, 19 Aug 2023 07:12:45 -0700 (PDT) Received: by mail-qv1-xf2f.google.com with SMTP id 6a1803df08f44-6432a429f61so10831726d6.2 for ; Sat, 19 Aug 2023 07:12:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692454365; x=1693059165; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QFVxXxStyebQd2+5mwYh1o0joRC2SclF2vbhFRruGlE=; b=ZfnvpAJgMbfQ3CZw7R7tBTIb0/CjcaNSgcbF2GnwC1IHBGukLBv2YpA5Xks+Msu2TA uMhZWngetDzl4ElT+MpmnGvhNpcECn0iQeDQaf7+z5ohObKr5KveEms6lREnz0pkluIU lbhrThEQrUqq6005xo225pWxTJLTL7a5laff9rT5866P9fbRYXvRztLy4FgY5FGg/NNs 0+QPT5LsThOBOcRtvSSNj3gmKBcKzSr9A+7gOARHUk+/0caenQwXXwwIjYV4cA1ZWQsh TniAp6hta2j/wAcjBcjfdlP6BU1uXdT2cSDxOe10NQwQoEvi2oJ38UOEwKOB6im0XK+m //JQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692454365; x=1693059165; 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:message-id:reply-to; bh=QFVxXxStyebQd2+5mwYh1o0joRC2SclF2vbhFRruGlE=; b=B/8X/oartafkNc1/i1sJd2hSHGci3rW2irAC+uSuOo547A5XyW44XkqfSDcolGr1TU XmKGR1glbfyllj3Gd6RQV+Q5dGoXvH7nXFH7OKdAG0WO/Jhr1CkbGU6G77sjCeG48NBl b8yEnfHVbNjYt+TH/86Tbq027DiK0ICybg4JrwSQt341V2upv59GwWJBbAh5t8qQwPqa 2GMAX+Zn649eH97Ia5s6qlqEGBu8bTz8dH9mO/Nol8L6ElJ7+E4KVxwC6sUKknJwrYsf ZUDnkEvRMZI1cnwdr7uWWd6OvlE9EuDyd6qJY+A2aqzA89E+WwzNQHPVb6FJ+LLGvhbT Exnw== X-Gm-Message-State: AOJu0YyEy1fy+Oe8ok/VCq7TwEN7xj8g6keTHBgAD6D4zdGXo/NLBSyb EMu+0bsGkG2SerNTmg4lsFwLqU72huQ= X-Google-Smtp-Source: AGHT+IFUX8eTyssulPQya93DY8TjvT45WssAgMpP4ifh6tDP9KAQSGhQl+ktG/q4W6s+7sBb8lbdUw== X-Received: by 2002:a0c:8e83:0:b0:640:5beb:d89d with SMTP id x3-20020a0c8e83000000b006405bebd89dmr2415714qvb.40.1692454364731; Sat, 19 Aug 2023 07:12:44 -0700 (PDT) Received: from localhost ([32.218.242.113]) by smtp.gmail.com with ESMTPSA id h16-20020a0cab10000000b0064906cfe430sm1473236qvb.135.2023.08.19.07.12.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Aug 2023 07:12:44 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Yury Norov , Ingo Molnar , Peter Zijlstra , Andrew Morton , Ben Segall , Daniel Bristot de Oliveira , Dietmar Eggemann , Jacob Keller , Jakub Kicinski , Juri Lelli , Mel Gorman , Steven Rostedt , Tariq Toukan , Valentin Schneider , Vincent Guittot , shiju.jose@huawei.com, jonathan.cameron@huawei.com, prime.zeng@huawei.com, linuxarm@huawei.com, yangyicong@hisilicon.com, Andy Shevchenko , Rasmus Villemoes Subject: [PATCH v2 2/6] sched/fair: fix opencoded numa_nearest_node() Date: Sat, 19 Aug 2023 07:12:34 -0700 Message-Id: <20230819141239.287290-3-yury.norov@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230819141239.287290-1-yury.norov@gmail.com> References: <20230819141239.287290-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" task_numa_placement() searches for a nearest node to migrate by calling for_each_node_state(). Now that we have numa_nearest_node(), switch to using it. Signed-off-by: Yury Norov --- kernel/sched/fair.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index b3e25be58e2b..e7b7cf87937b 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -2645,19 +2645,7 @@ static void task_numa_placement(struct task_struct *= p) } =20 /* Cannot migrate task to CPU-less node */ - if (max_nid !=3D NUMA_NO_NODE && !node_state(max_nid, N_CPU)) { - int near_nid =3D max_nid; - int distance, near_distance =3D INT_MAX; - - for_each_node_state(nid, N_CPU) { - distance =3D node_distance(max_nid, nid); - if (distance < near_distance) { - near_nid =3D nid; - near_distance =3D distance; - } - } - max_nid =3D near_nid; - } + max_nid =3D numa_nearest_node(max_nid, N_CPU); =20 if (ng) { numa_group_count_active_nodes(ng); --=20 2.39.2 From nobody Fri Sep 12 07:29:48 2025 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 AA5B1EE49A8 for ; Sat, 19 Aug 2023 14:15:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233287AbjHSOPB (ORCPT ); Sat, 19 Aug 2023 10:15:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233218AbjHSOO6 (ORCPT ); Sat, 19 Aug 2023 10:14:58 -0400 Received: from mail-oi1-x236.google.com (mail-oi1-x236.google.com [IPv6:2607:f8b0:4864:20::236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06AF19ECB for ; Sat, 19 Aug 2023 07:12:47 -0700 (PDT) Received: by mail-oi1-x236.google.com with SMTP id 5614622812f47-3a7d4030621so1171270b6e.3 for ; Sat, 19 Aug 2023 07:12:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692454366; x=1693059166; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=igV7SREietQ24odTG+SOK9M1ry44nQsh1uk2kgh31uI=; b=YHzUfnhp2ULvFItZJ3o9nXuTnirno2N6SDjG259/ov8RQGn1YKf8iAafsV/TZmzM9G +JnpUr2gmWptKe9x+JSvawiBnbrHhC0ttUD2okQ/G1fkWvTO+q+8WWnRri6umDq+AeyL uI0r086HHFLAzxmjNMoczkq3+6neOsKWrv7/U+R0BktdT2UnHnqO2MbZ02/Sv0a3y64w uVF4L/O5aitTTPQbj/ImVhoNslyY90UeKiLJpC1hbba+hqARYZbe+jJxR1t91g/UGIRk cj0t8on3AEkxlEMjodaPmpu584rPPq3QYwFMKZXzjDyKDOj1sI+dorfQbPfPplhlCTXI V2Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692454366; x=1693059166; 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:message-id:reply-to; bh=igV7SREietQ24odTG+SOK9M1ry44nQsh1uk2kgh31uI=; b=R3I6FrJoOiR1df06L/eq+2jpjyvA1NOfjqaIh5poa07OXgmZy9MGqTSjlCyCxd1rJ8 hp2RgYT7MOJfFxmV5ztutwQ0I8O0MnS6T5YkdIzGT4C56b21BKEz+AF8e0e9bjiba/jr Ks7uRfEodk+1XE1CafpaE11+i8tR8X+S1jkl7dwDY6ZLb8VRyrm7sFZrU/R7U7+E5ses 4vbvv5y0J4ghYoGe/BSLN/rlqfhI1D77abKXRdGjqPH7d9n1Xa2ToJ3wbii2Sn0rpnW2 TCDtxO14Dj90bkzlQrbKwiJmkX//xdsLE4d4AHN8Bilc+xI5YuWMm6YkMMr8ZF+F2SD0 t8mQ== X-Gm-Message-State: AOJu0YzMIpNV55dV29m4DHXZMtVJVbnU58W8xGqgRFpXwOdhz8hnsHcC PDjbTfB50fDegVmlBFZdpgEgIuuLH4o= X-Google-Smtp-Source: AGHT+IHSqqEDImaL7LnYyBklQgJU18/Wzpv/z+7QriCsXPpQMlpRzLKW65D8myoE3i10msbA9KqjvA== X-Received: by 2002:a05:6358:e486:b0:135:499b:a68c with SMTP id by6-20020a056358e48600b00135499ba68cmr488750rwb.8.1692454365835; Sat, 19 Aug 2023 07:12:45 -0700 (PDT) Received: from localhost ([32.218.242.113]) by smtp.gmail.com with ESMTPSA id r11-20020a0ccc0b000000b00631fea4d5bcsm1466439qvk.95.2023.08.19.07.12.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Aug 2023 07:12:45 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Yury Norov , Ingo Molnar , Peter Zijlstra , Andrew Morton , Ben Segall , Daniel Bristot de Oliveira , Dietmar Eggemann , Jacob Keller , Jakub Kicinski , Juri Lelli , Mel Gorman , Steven Rostedt , Tariq Toukan , Valentin Schneider , Vincent Guittot , shiju.jose@huawei.com, jonathan.cameron@huawei.com, prime.zeng@huawei.com, linuxarm@huawei.com, yangyicong@hisilicon.com, Andy Shevchenko , Rasmus Villemoes , Guenter Roeck Subject: [PATCH v2 3/6] sched: fix sched_numa_find_nth_cpu() in CPU-less case Date: Sat, 19 Aug 2023 07:12:35 -0700 Message-Id: <20230819141239.287290-4-yury.norov@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230819141239.287290-1-yury.norov@gmail.com> References: <20230819141239.287290-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" When the node provided by user is CPU-less, corresponding record in sched_domains_numa_masks is not set. Trying to dereference it in the following code leads to kernel crash. To avoid it, start searching from the nearest node with CPUs. Fixes: cd7f55359c90 ("sched: add sched_numa_find_nth_cpu()") Reported-by: Yicong Yang Closes: https://lore.kernel.org/lkml/CAAH8bW8C5humYnfpW3y5ypwx0E-09A3QxFE1J= FzR66v+mO4XfA@mail.gmail.com/T/ Reported-by: Guenter Roeck Closes: https://lore.kernel.org/lkml/ZMHSNQfv39HN068m@yury-ThinkPad/T/#mf64= 31cb0b7f6f05193c41adeee444bc95bf2b1c4 Signed-off-by: Yury Norov Reviewed-by: Yicong Yang --- kernel/sched/topology.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c index d3a3b2646ec4..c6e89afa0d65 100644 --- a/kernel/sched/topology.c +++ b/kernel/sched/topology.c @@ -2113,12 +2113,16 @@ static int hop_cmp(const void *a, const void *b) */ int sched_numa_find_nth_cpu(const struct cpumask *cpus, int cpu, int node) { - struct __cmp_key k =3D { .cpus =3D cpus, .node =3D node, .cpu =3D cpu }; + struct __cmp_key k =3D { .cpus =3D cpus, .cpu =3D cpu }; struct cpumask ***hop_masks; int hop, ret =3D nr_cpu_ids; =20 rcu_read_lock(); =20 + /* CPU-less node entries are uninitialized in sched_domains_numa_masks */ + node =3D numa_nearest_node(node, N_CPU); + k.node =3D node; + k.masks =3D rcu_dereference(sched_domains_numa_masks); if (!k.masks) goto unlock; --=20 2.39.2 From nobody Fri Sep 12 07:29:48 2025 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 99B4DEE4993 for ; Sat, 19 Aug 2023 14:15:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233305AbjHSOPD (ORCPT ); Sat, 19 Aug 2023 10:15:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233224AbjHSOO6 (ORCPT ); Sat, 19 Aug 2023 10:14:58 -0400 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B5489ED9 for ; Sat, 19 Aug 2023 07:12:48 -0700 (PDT) Received: by mail-qv1-xf29.google.com with SMTP id 6a1803df08f44-64cca551ae2so3733466d6.0 for ; Sat, 19 Aug 2023 07:12:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692454367; x=1693059167; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SmmjHmVn6ls0sfUQEL2MtTTuL3JIkk0EADWsp1ssJ1I=; b=DG6joiC2Lh32MiV3Hhqxx3JWfQDfDz/+8b0EdnMI1S+h3jh7LlNSk0ZQqNJgRg62Z7 3rlaxuUKCDxday8et0CVelK2WFG3zjQNY2LFZOzuTLwxfBhJqvEfVigb/4QPBMbQcxjU qjqzPnj0LYYPPADAvFTkSkVDZfLtCn1hLjx1jfvoUGGiDN9oLQeTTHtTTmJTcCitifem CKA0GfI3ugT45tKAdlgqZEDA18Rftgam1AdPVf8K6HYQufQ6i7Il64g3uPRSBc+0rc0k wfwnK3RBh06a2KwGVvE7GvgcZQYFOyPzUvnjFYV6Kv+K90LdXhq+3wO3Px4qqG69USp8 cr3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692454367; x=1693059167; 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:message-id:reply-to; bh=SmmjHmVn6ls0sfUQEL2MtTTuL3JIkk0EADWsp1ssJ1I=; b=l4LyclCCgJkt4inB2ma7FG5D0sIR/tZEqO5L7XmabR4v9JWvNNSmrn2j1cnm7T14ZJ avRLZXxxdp9N2WpTCgQe+Sj23pbCoAWoIPpYNNfRllYHDviu5SJvBdoE/LFGpG3HkaPm WSN8K/U8N1IPphy0rhhsLwSx/+79ShkOP8jHX835iF4I4uw5rly4HrobkQJj4t0GQ/+Q qIKaam4MUBihQP2R28STOukhPd/4FEMPoqlv4KBamPfX4Cyf4xlb1dra5L/Fh7TB4dXf kNUCKoIbec1l2pDbBDdGUiJv0EETlrNZvsiaaT5u6bwijkJHrWIIPXadEHJF3BQnCUWP 7LAg== X-Gm-Message-State: AOJu0YxEqfPHBePHlEYktLVkGMd9gfzmdvMAL5K98DgXIU1BSdyNVKph j3csWtPAhXUME2JF0zkCU7yrOIaoJa0= X-Google-Smtp-Source: AGHT+IEfRsyy5LserSpARnbs4AMrhHIcl356NFzEZZyieATaLVkGrmSM4un/e4KNH9OAVV4li0xoAg== X-Received: by 2002:a0c:e413:0:b0:64b:997f:5a7e with SMTP id o19-20020a0ce413000000b0064b997f5a7emr2602799qvl.8.1692454366974; Sat, 19 Aug 2023 07:12:46 -0700 (PDT) Received: from localhost ([32.218.242.113]) by smtp.gmail.com with ESMTPSA id g22-20020a0caad6000000b006262de12a8csm1464891qvb.65.2023.08.19.07.12.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Aug 2023 07:12:46 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Yury Norov , Ingo Molnar , Peter Zijlstra , Andrew Morton , Ben Segall , Daniel Bristot de Oliveira , Dietmar Eggemann , Jacob Keller , Jakub Kicinski , Juri Lelli , Mel Gorman , Steven Rostedt , Tariq Toukan , Valentin Schneider , Vincent Guittot , shiju.jose@huawei.com, jonathan.cameron@huawei.com, prime.zeng@huawei.com, linuxarm@huawei.com, yangyicong@hisilicon.com, Andy Shevchenko , Rasmus Villemoes Subject: [PATCH v2 4/6] sched: fix sched_numa_find_nth_cpu() in non-NUMA case Date: Sat, 19 Aug 2023 07:12:36 -0700 Message-Id: <20230819141239.287290-5-yury.norov@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230819141239.287290-1-yury.norov@gmail.com> References: <20230819141239.287290-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" When CONFIG_NUMA is enabled, sched_numa_find_nth_cpu() searches for a CPU in sched_domains_numa_masks. The masks includes only online CPUs, so effectively offline CPUs are skipped. When CONFIG_NUMA is disabled, the fallback function should be consistent. Fixes: cd7f55359c90 ("sched: add sched_numa_find_nth_cpu()") Signed-off-by: Yury Norov --- include/linux/topology.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/topology.h b/include/linux/topology.h index fea32377f7c7..52f5850730b3 100644 --- a/include/linux/topology.h +++ b/include/linux/topology.h @@ -251,7 +251,7 @@ extern const struct cpumask *sched_numa_hop_mask(unsign= ed int node, unsigned int #else static __always_inline int sched_numa_find_nth_cpu(const struct cpumask *c= pus, int cpu, int node) { - return cpumask_nth(cpu, cpus); + return cpumask_nth_and(cpu, cpus, cpu_online_mask); } =20 static inline const struct cpumask * --=20 2.39.2 From nobody Fri Sep 12 07:29:48 2025 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 B5EA2EE49A8 for ; Sat, 19 Aug 2023 14:15:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232994AbjHSOPF (ORCPT ); Sat, 19 Aug 2023 10:15:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233236AbjHSOO7 (ORCPT ); Sat, 19 Aug 2023 10:14:59 -0400 Received: from mail-qk1-x736.google.com (mail-qk1-x736.google.com [IPv6:2607:f8b0:4864:20::736]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39CAF9EF7 for ; Sat, 19 Aug 2023 07:12:49 -0700 (PDT) Received: by mail-qk1-x736.google.com with SMTP id af79cd13be357-76d83954c43so126372485a.2 for ; Sat, 19 Aug 2023 07:12:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692454368; x=1693059168; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Crn5/+8WdRhCgV6i5ssfAVZRKeJzdqL7IL6ctYKnl6U=; b=U7AgRT8Ejxxl/VEdRwqDG1ENqN4QGxU6q+qxz0PKZpU9PSYvv4hGm/LQQLGxOrW1gJ CWLHmh7bBtOZQEIirnGLdBTRwYf5ThOF0JN0i91aMcJOkDKLZxhdvJ3WZr20h2BmTR/b icRcsHm5yrdnjCN64NRgjkhAgWD8MzAJiawtCyEU8rZpGuPTFn7dVM43FYHmSDsEpWgH WeYgmoVQ6tUx1hhTGyjdZmYTtK6mjtcolc7TSb9NigZLMaPqqeQrDZdjkyl/ECDxq5VC CZEAbglb9Y0vPOZDMQZOKdkUtKdyAS3YZNc/qBVNVgenMfI4FRZzgHQ1iiynIlneqMrG HyuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692454368; x=1693059168; 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:message-id:reply-to; bh=Crn5/+8WdRhCgV6i5ssfAVZRKeJzdqL7IL6ctYKnl6U=; b=YfJ4XkCd0ppkGgVjUFuBoVHJNF6Xml5rRjNf2ykfkNMzb49B3HpgpGsSi+MMjv5Yus 9uK/r/TgfjKhrsO26+rOJl12jr/VJtTTDCY9ZnDZCny4QIWsla8kr4wY11E2rTJ+f9Aa BWRA1XTWouN964bY96dT0cZGzqbrXrWiIU8bjk8dbZWueTjAdgaJNILRQ2VimQmmPxZY Hg0VVp8bn2AYhMaeGO4sAUSnGri859KeNPgkS0aGWYtKdE/8CqSQdjmcPULq9EXK6wDO Ldb6N+qgxdaINy46+iuaLnweeidKhWWiOkNxqi9zlpzT3/lnqRD7InXi841DYcihpHLR cLzg== X-Gm-Message-State: AOJu0YyN/UnNyNwWi3D92zD5lgJ59MZeaB/C46A50ZcYAgI3Z4a0rU72 FckZ/jYgfk0wLzPIpSe9nLbl0dRlCWM= X-Google-Smtp-Source: AGHT+IETTvZTEFyiJf8ZsCBw1Z942T/yFUfrCc+lCrhAif1gu2FCfd/M7wsnPaQD+/POCrDPVTsgpw== X-Received: by 2002:a05:620a:2987:b0:76c:e764:5081 with SMTP id r7-20020a05620a298700b0076ce7645081mr2825410qkp.37.1692454367995; Sat, 19 Aug 2023 07:12:47 -0700 (PDT) Received: from localhost ([32.218.242.113]) by smtp.gmail.com with ESMTPSA id 22-20020a05620a079600b0076ce061f44dsm1204827qka.25.2023.08.19.07.12.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Aug 2023 07:12:47 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Yury Norov , Ingo Molnar , Peter Zijlstra , Andrew Morton , Ben Segall , Daniel Bristot de Oliveira , Dietmar Eggemann , Jacob Keller , Jakub Kicinski , Juri Lelli , Mel Gorman , Steven Rostedt , Tariq Toukan , Valentin Schneider , Vincent Guittot , shiju.jose@huawei.com, jonathan.cameron@huawei.com, prime.zeng@huawei.com, linuxarm@huawei.com, yangyicong@hisilicon.com, Andy Shevchenko , Rasmus Villemoes Subject: [PATCH v2 5/6] sched: handle NUMA_NO_NODE in sched_numa_find_nth_cpu() Date: Sat, 19 Aug 2023 07:12:37 -0700 Message-Id: <20230819141239.287290-6-yury.norov@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230819141239.287290-1-yury.norov@gmail.com> References: <20230819141239.287290-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" sched_numa_find_nth_cpu() doesn't handle NUMA_NO_NODE properly, and may crash kernel if passed with it. On the other hand, the only user of sched_numa_find_nth_cpu() has to check NUMA_NO_NODE case explicitly. It would be easier for users if this logic will get moved into sched_numa_find_nth_cpu(). Signed-off-by: Yury Norov --- kernel/sched/topology.c | 3 +++ lib/cpumask.c | 4 +--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c index c6e89afa0d65..bc6802700103 100644 --- a/kernel/sched/topology.c +++ b/kernel/sched/topology.c @@ -2117,6 +2117,9 @@ int sched_numa_find_nth_cpu(const struct cpumask *cpu= s, int cpu, int node) struct cpumask ***hop_masks; int hop, ret =3D nr_cpu_ids; =20 + if (node =3D=3D NUMA_NO_NODE) + return cpumask_nth_and(cpu, cpus, cpu_online_mask); + rcu_read_lock(); =20 /* CPU-less node entries are uninitialized in sched_domains_numa_masks */ diff --git a/lib/cpumask.c b/lib/cpumask.c index 19277c6d551f..e77ee9d46f71 100644 --- a/lib/cpumask.c +++ b/lib/cpumask.c @@ -147,9 +147,7 @@ unsigned int cpumask_local_spread(unsigned int i, int n= ode) /* Wrap: we always want a cpu. */ i %=3D num_online_cpus(); =20 - cpu =3D (node =3D=3D NUMA_NO_NODE) ? - cpumask_nth(i, cpu_online_mask) : - sched_numa_find_nth_cpu(cpu_online_mask, i, node); + cpu =3D sched_numa_find_nth_cpu(cpu_online_mask, i, node); =20 WARN_ON(cpu >=3D nr_cpu_ids); return cpu; --=20 2.39.2 From nobody Fri Sep 12 07:29:48 2025 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 6430CEE4993 for ; Sat, 19 Aug 2023 14:15:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233316AbjHSOPG (ORCPT ); Sat, 19 Aug 2023 10:15:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233237AbjHSOO7 (ORCPT ); Sat, 19 Aug 2023 10:14:59 -0400 Received: from mail-qv1-xf35.google.com (mail-qv1-xf35.google.com [IPv6:2607:f8b0:4864:20::f35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69D71A265 for ; Sat, 19 Aug 2023 07:12:50 -0700 (PDT) Received: by mail-qv1-xf35.google.com with SMTP id 6a1803df08f44-649a653479bso10152236d6.0 for ; Sat, 19 Aug 2023 07:12:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692454369; x=1693059169; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kSjHpIarSFWJNPdWsAvPv/NwMFUjoCdaJnar30zRHJU=; b=YPTYau8iMQLnPV+8MSH/8fja/dsf3ZcEtp5kyGNDrnFQqPSXA5t6VdW4PpzV8wj9De 9w4eBuHp337uqHsSO8utHM8/RVexJMd4Nq1n4C8TDizf3M2jE01bhejhVqpv67N90w3m /8hJWdkhJO1gY/EjNZkWkBPTBTnXoF4wuIv9yOUkUWDdwRQyE9douqL/HsdzxxILocF1 iOyufwrqJiVlOWBYHXCP40osxOUD/3qinlpd6jk6RJgTZk4jgn6odD4ol+tnkFtuw5k6 LxPvVHNtQxQz8fCwlXbUSbbzBIOIa6DuKbo9gMZIF6Uov1UISZHXIyeaEbDQRmznnsAk dP0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692454369; x=1693059169; 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:message-id:reply-to; bh=kSjHpIarSFWJNPdWsAvPv/NwMFUjoCdaJnar30zRHJU=; b=HCKKg5tAm8OSJ8XKQBxteYqTKLRTgOQ3Jy/ps5T9hmXi8sZJTMi6thxV5NfBMTvf19 LHbpGLoKbkIY49No5frIbzHCBOJP6g48nLdiwHHppST2LTq0XVSoa5PSZS0T2ho3W3VJ F3D91tl1AmrEDfB53ux4aZSetftJuB0mXb1MujtkpNsroCzZCqdf5tu/7GKp3Py5Ts9G J5rDrB5su2W80LwaekfIF8YGfatj8kVyGhMwO7xW7aV4rHVGEkWl51OG+uLL7qz5upW3 C4og1cBZG0nFRS5y7W1mdvBjzTpDbTGaZU27ZMyJ5LkofAF03sOWCta3UEfNfGv9TVic epLA== X-Gm-Message-State: AOJu0YxbPEUmm+EBCDn8D4AKGMKMJkWMfrQmvD1CfOeZVAufwq7HmZvC LYFQBhjqQCbNdRxhvYpuuKSHYcpxx9Q= X-Google-Smtp-Source: AGHT+IFlP2jDt8uZ7TktMhzGEL+WSo8Hr7UkTWitsqazph963ocRgXITG4K/KKqigpjjb7d7gIYaJQ== X-Received: by 2002:a0c:f34d:0:b0:647:1764:7e1a with SMTP id e13-20020a0cf34d000000b0064717647e1amr1891960qvm.27.1692454369104; Sat, 19 Aug 2023 07:12:49 -0700 (PDT) Received: from localhost ([32.218.242.113]) by smtp.gmail.com with ESMTPSA id x8-20020a0ce0c8000000b0062def68f75csm68439qvk.124.2023.08.19.07.12.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Aug 2023 07:12:48 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Yury Norov , Ingo Molnar , Peter Zijlstra , Andrew Morton , Ben Segall , Daniel Bristot de Oliveira , Dietmar Eggemann , Jacob Keller , Jakub Kicinski , Juri Lelli , Mel Gorman , Steven Rostedt , Tariq Toukan , Valentin Schneider , Vincent Guittot , shiju.jose@huawei.com, jonathan.cameron@huawei.com, prime.zeng@huawei.com, linuxarm@huawei.com, yangyicong@hisilicon.com, Andy Shevchenko , Rasmus Villemoes Subject: [PATCH v2 6/6] sched: fix sched_numa_find_nth_cpu() comment Date: Sat, 19 Aug 2023 07:12:38 -0700 Message-Id: <20230819141239.287290-7-yury.norov@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230819141239.287290-1-yury.norov@gmail.com> References: <20230819141239.287290-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" Reword sched_numa_find_nth_cpu() comment and make it kernel-doc compatible. Signed-off-by: Yury Norov --- kernel/sched/topology.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c index bc6802700103..789b281d2380 100644 --- a/kernel/sched/topology.c +++ b/kernel/sched/topology.c @@ -2103,13 +2103,15 @@ static int hop_cmp(const void *a, const void *b) return -1; } =20 -/* - * sched_numa_find_nth_cpu() - given the NUMA topology, find the Nth next = cpu - * closest to @cpu from @cpumask. - * cpumask: cpumask to find a cpu from - * cpu: Nth cpu to find - * - * returns: cpu, or nr_cpu_ids when nothing found. +/** + * sched_numa_find_nth_cpu() - given the NUMA topology, find the Nth close= st CPU + * from @cpus to @cpu, taking into account dis= tance + * from a given @node. + * @cpus: cpumask to find a cpu from + * @cpu: CPU to start searching + * @node: NUMA node to order CPUs by distance + * + * Return: cpu, or nr_cpu_ids when nothing found. */ int sched_numa_find_nth_cpu(const struct cpumask *cpus, int cpu, int node) { --=20 2.39.2