Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp707697ybx; Wed, 30 Oct 2019 23:06:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqxK21G1iKeawCzjKSkW+0CmogsQ96gqdVp7pUYIK//Sfn3kQuMB1t01JhdAhfePqjB7B1YI X-Received: by 2002:a05:6402:2d4:: with SMTP id b20mr4104182edx.103.1572502016646; Wed, 30 Oct 2019 23:06:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572502016; cv=none; d=google.com; s=arc-20160816; b=Xw8IzkXbW70sl+yzD39qftv6D8KzUMCQ17Wev1jo8lhIAxxwRlWgHkFuJUQ5fMOFq2 J6WjxeCBMuEjtvA43AUu6qqHwG6LtADxLv8lvoCy86jBny8J/W0Ru5SzRy72LT/FF4w8 nkecILpw93lLvnfeA5oaqHVUqF2q1+Sl+7svxyfaSX7q/Kx9xAsIBqQZk+P/v9WrfzA0 mkRDO7lIc6YehoPB4ewE76c59rE2VS0U+l+zNzh01KM6Vb7Y8TSdJnZ4shC6SpvHqZ8c k/cq6lgFcEMlwn+XKnADV7hZcpWL2aNv5Md21DIxSoKk8fTEP8FVQYG9Alp6HkDxIV2H Cy1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=THEjmAn5kUzfLHsp0EBinTIJvoepXCtXtF7zTiNtNfI=; b=AC14Fk+yD3UxUC0l5AbQb9FkBonz1JOD+kNRg1T8kngkIeTARID8pqaAWfw9lSipKB zuhwopfQHw/DyETD4UPGtqTsMHKX5iMtPK58Q1OO5qiPKXPuK1pEf0VHSaAQMXsTz0pO ChSH01P1F+Tnr1POJtyprm8lhz1PlsTsNXGNOE+YIeOY8d0h9699kwsPpHfPw1S15dGI AgyqR7I8stNYoAXFIkIFH8kF2hS9nyE98LEsm5vFSm0y4mNsV0usAgSk+CVaXx7Jh4FT Ulbwm5RyZRwH7sY1SLnJj1OAZU/cEZos8jlnoa5pTy3AhY7ypeJwLt7SUHzvLaKrqKEg QKMQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q25si2846613ejb.340.2019.10.30.23.06.31; Wed, 30 Oct 2019 23:06:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726646AbfJaGDd (ORCPT + 99 others); Thu, 31 Oct 2019 02:03:33 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:54876 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726370AbfJaGDd (ORCPT ); Thu, 31 Oct 2019 02:03:33 -0400 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 2116CB2439A51576775E; Thu, 31 Oct 2019 14:03:31 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.439.0; Thu, 31 Oct 2019 14:03:20 +0800 From: Shaokun Zhang To: CC: yuqi jin , Andrew Morton , Mike Rapoport , Paul Burton , Michal Hocko , Michael Ellerman , Anshuman Khandual , Shaokun Zhang Subject: [PATCH] lib: optimize cpumask_local_spread() Date: Thu, 31 Oct 2019 14:03:33 +0800 Message-ID: <1572501813-2125-1-git-send-email-zhangshaokun@hisilicon.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.69.192.56] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: yuqi jin In the multi-processor and NUMA system, A device may have many numa nodes belonging to multiple cpus. When we get a local numa, it is better to find the node closest to the local numa node to return instead of going to the online cpu immediately. For example, In Huawei Kunpeng 920 system, there are 4 NUMA node(0 -3) in the 2-socket system(0 - 1). If the I/O device is in socket1 and the local NUMA node is 2, we shall choose the non-local node3 in the same socket when cpu core in NUMA node2 is less that I/O requirements. If we directly pick one cpu core from all online ones, it may be in the another socket and it is not friendly for performance. Cc: Andrew Morton Cc: Mike Rapoport Cc: Paul Burton Cc: Michal Hocko Cc: Michael Ellerman Cc: Anshuman Khandual Signed-off-by: yuqi jin Signed-off-by: Shaokun Zhang --- Changes from RFC: Address Michal Hocko's comment: Use GFP_ATOMIC instead of GFP_KERNEL lib/cpumask.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/lib/cpumask.c b/lib/cpumask.c index 0cb672eb107c..c92177b0e095 100644 --- a/lib/cpumask.c +++ b/lib/cpumask.c @@ -192,6 +192,33 @@ void __init free_bootmem_cpumask_var(cpumask_var_t mask) } #endif +static void calc_node_distance(int *node_dist, int node) +{ + int i; + + for (i = 0; i < nr_node_ids; i++) + node_dist[i] = node_distance(node, i); +} + +static int find_nearest_node(int *node_dist, bool *used_flag) +{ + int i, min_dist = node_dist[0], node_id = -1; + + for (i = 0; i < nr_node_ids; i++) + if (used_flag[i] == 0) { + min_dist = node_dist[i]; + node_id = i; + break; + } + for (i = 0; i < nr_node_ids; i++) + if (node_dist[i] < min_dist && used_flag[i] == 0) { + min_dist = node_dist[i]; + node_id = i; + } + + return node_id; +} + /** * cpumask_local_spread - select the i'th cpu with local numa cpu's first * @i: index number @@ -205,7 +232,8 @@ void __init free_bootmem_cpumask_var(cpumask_var_t mask) */ unsigned int cpumask_local_spread(unsigned int i, int node) { - int cpu; + int cpu, j, id, *node_dist; + bool *used_flag; /* Wrap: we always want a cpu. */ i %= num_online_cpus(); @@ -215,19 +243,45 @@ unsigned int cpumask_local_spread(unsigned int i, int node) if (i-- == 0) return cpu; } else { - /* NUMA first. */ - for_each_cpu_and(cpu, cpumask_of_node(node), cpu_online_mask) - if (i-- == 0) - return cpu; + node_dist = kmalloc_array(nr_node_ids, sizeof(int), GFP_ATOMIC); + if (!node_dist) + for_each_cpu(cpu, cpu_online_mask) + if (i-- == 0) + return cpu; - for_each_cpu(cpu, cpu_online_mask) { - /* Skip NUMA nodes, done above. */ - if (cpumask_test_cpu(cpu, cpumask_of_node(node))) - continue; + used_flag = kmalloc_array(nr_node_ids, sizeof(bool), GFP_ATOMIC); + if (!used_flag) + for_each_cpu(cpu, cpu_online_mask) + if (i-- == 0) { + kfree(node_dist); + return cpu; + } + memset(used_flag, 0, nr_node_ids * sizeof(bool)); - if (i-- == 0) - return cpu; + calc_node_distance(node_dist, node); + for (j = 0; j < nr_node_ids; j++) { + id = find_nearest_node(node_dist, used_flag); + if (id < 0) + break; + for_each_cpu_and(cpu, + cpumask_of_node(id), cpu_online_mask) + if (i-- == 0) { + kfree(node_dist); + kfree(used_flag); + return cpu; + } + used_flag[id] = 1; } + + for_each_cpu(cpu, cpu_online_mask) + if (i-- == 0) { + kfree(node_dist); + kfree(used_flag); + return cpu; + } + + kfree(node_dist); + kfree(used_flag); } BUG(); } -- 2.7.4