Message ID | 20221107085030.3901608-1-xukuohai@huaweicloud.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1919719wru; Mon, 7 Nov 2022 00:44:24 -0800 (PST) X-Google-Smtp-Source: AMsMyM68Hh/LXBARminnQFA1iOJToW0DpGKGm4LHWIA3+NboEsez+PUIntPNAr3Qhx+g/A4uKIqM X-Received: by 2002:a17:906:5a56:b0:7ad:c391:42ee with SMTP id my22-20020a1709065a5600b007adc39142eemr42425060ejc.127.1667810664462; Mon, 07 Nov 2022 00:44:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667810664; cv=none; d=google.com; s=arc-20160816; b=YRHe1rkIq07Wlgn7yVo/gSHymrAKj0IBFNp+FHmam1nvVdrGaMo0yqdPB2WnoLGr70 yyWCnsZHUH6WgN/i4YX74e5UuoWkFbbFpuFH9/p5JNqW+PoP+Hib5f1gDEtoZBSube/a 8+3jypOE8qTVe96CUWhjQK1U7kE1OqmbnEEBck7moM/d+e+shgZNCPfsy+0IMHvWPOxQ ruezfi2bUwB3FPrKTuy2PLYbINmvh3xHFgNMkAkUjxcT9hscjg06DKcaNBB/bYioGGO0 6xWg90MZF22Ixc7SNAFrPW/cuBJULAnX2x4eLZgg/prMR7Cih8Z7gJIr9jvjri8phYOv 4htg== 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 :message-id:date:subject:cc:to:from; bh=j1DJUn3SnKrp09ym98vQ/M7CTrXDwwnEt2V4+q0xtg8=; b=oPDyBtDBlxDkeBuk1f40MFF7nndMto0mu/QF47dG5XkSFjtsYN2gBu6YtarjoKYwzq a2YhwGDxVbZCjsvBDPKF8ybtYQbI4S7lSrFqUzEZtNS+2lE7NtxTkNnIo90fPtOyDQ73 E6PCWMoqdM6BGlu054BhRQzh6Tf5Xetc0ff3HHK7Z5bRghxEJ3qodj7RaDxgKuwTVCHH ygaUP3MYDHhD/MkPW5JMYQO0JFGLV0QTjPDj57ugk76NT4Y6vYp0TNna1hN038bf+7Zm Rr5od2dmu7IXOJHbeGdcdGn6rV2NR91h0HrT84DjrT3PqKcmgX5H6u7qTP27hDOj6fm6 cb9A== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c26-20020a056402159a00b00464718ca52bsi7108752edv.174.2022.11.07.00.44.00; Mon, 07 Nov 2022 00:44:24 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231292AbiKGIfW (ORCPT <rfc822;hjfbswb@gmail.com> + 99 others); Mon, 7 Nov 2022 03:35:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231264AbiKGIfU (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 7 Nov 2022 03:35:20 -0500 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7FD0140D0; Mon, 7 Nov 2022 00:35:18 -0800 (PST) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4N5PgN3cppz4f4G0R; Mon, 7 Nov 2022 16:35:12 +0800 (CST) Received: from k01.huawei.com (unknown [10.67.174.197]) by APP2 (Coremail) with SMTP id Syh0CgBHXrlCw2hjV_t7AA--.38382S2; Mon, 07 Nov 2022 16:35:15 +0800 (CST) From: Xu Kuohai <xukuohai@huaweicloud.com> To: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Alexei Starovoitov <ast@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, Andrii Nakryiko <andrii@kernel.org>, Martin KaFai Lau <martin.lau@linux.dev>, Song Liu <song@kernel.org>, Yonghong Song <yhs@fb.com>, John Fastabend <john.fastabend@gmail.com>, KP Singh <kpsingh@kernel.org>, Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>, Jiri Olsa <jolsa@kernel.org> Subject: [PATCH bpf-next] bpf: Initialize same number of free nodes for each pcpu_freelist Date: Mon, 7 Nov 2022 03:50:30 -0500 Message-Id: <20221107085030.3901608-1-xukuohai@huaweicloud.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: Syh0CgBHXrlCw2hjV_t7AA--.38382S2 X-Coremail-Antispam: 1UD129KBjvJXoW7uFyxArWrJr4fXw4UXFyrJFb_yoW8AFWxpF 4fAw1jqw1vgrn5Wws5G3WrW343t3ykGw13Ga98Kr1Yvry3GFyqvr1vyF4aqFWagF4Ivr4F kFsI9FZFyayUXFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkFb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I 0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40E x7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x 0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1l42xK82IY c2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s 026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF 0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0x vE42xK8VAvwI8IcIk0rVWrZr1j6s0DMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2 jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07UWE__UUUUU= X-CM-SenderInfo: 50xn30hkdlqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748826235617592891?= X-GMAIL-MSGID: =?utf-8?q?1748826235617592891?= |
Series |
[bpf-next] bpf: Initialize same number of free nodes for each pcpu_freelist
|
|
Commit Message
Xu Kuohai
Nov. 7, 2022, 8:50 a.m. UTC
From: Xu Kuohai <xukuohai@huawei.com> pcpu_freelist_populate() initializes nr_elems / num_possible_cpus() + 1 free nodes for each CPU except the last initialized CPU, always making the last CPU get fewer free nodes. For example, when nr_elems == 256 and num_possible_cpus() == 32, if CPU 0 is the current cpu, CPU 0~27 each gets 9 free nodes, CPU 28 gets 4 free nodes, CPU 29~31 get 0 free nodes, while in fact each CPU should get 8 nodes equally. This patch initializes nr_elems / num_possible_cpus() free nodes for each CPU firstly, and then allocates the remaining free nodes by one for each CPU until no free nodes left. Signed-off-by: Xu Kuohai <xukuohai@huawei.com> --- kernel/bpf/percpu_freelist.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
Comments
On 11/7/22 12:50 AM, Xu Kuohai wrote: > From: Xu Kuohai <xukuohai@huawei.com> > > pcpu_freelist_populate() initializes nr_elems / num_possible_cpus() + 1 > free nodes for each CPU except the last initialized CPU, always making > the last CPU get fewer free nodes. For example, when nr_elems == 256 ... free nodes for some cpus, and then possibly one cpu with fewer nodes, followed by remaining cpus with 0 nodes. > and num_possible_cpus() == 32, if CPU 0 is the current cpu, CPU 0~27 > each gets 9 free nodes, CPU 28 gets 4 free nodes, CPU 29~31 get 0 free > nodes, while in fact each CPU should get 8 nodes equally. > > This patch initializes nr_elems / num_possible_cpus() free nodes for each > CPU firstly, and then allocates the remaining free nodes by one for each > CPU until no free nodes left. > > Signed-off-by: Xu Kuohai <xukuohai@huawei.com> LGTM. Did you observe any performance issues? Acked-by: Yonghong Song <yhs@fb.com> > --- > kernel/bpf/percpu_freelist.c | 9 ++++++--- > 1 file changed, 6 insertions(+), 3 deletions(-) > > diff --git a/kernel/bpf/percpu_freelist.c b/kernel/bpf/percpu_freelist.c > index b6e7f5c5b9ab..89e84f7381cc 100644 > --- a/kernel/bpf/percpu_freelist.c > +++ b/kernel/bpf/percpu_freelist.c > @@ -100,12 +100,15 @@ void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size, > u32 nr_elems) > { > struct pcpu_freelist_head *head; > - int i, cpu, pcpu_entries; > + int i, cpu, pcpu_entries, remain_entries; > + > + pcpu_entries = nr_elems / num_possible_cpus(); > + remain_entries = nr_elems % num_possible_cpus(); > > - pcpu_entries = nr_elems / num_possible_cpus() + 1; > i = 0; > > for_each_possible_cpu(cpu) { > + int j = i + pcpu_entries + (remain_entries-- > 0 ? 1 : 0); > again: > head = per_cpu_ptr(s->freelist, cpu); > /* No locking required as this is not visible yet. */ > @@ -114,7 +117,7 @@ void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size, > buf += elem_size; > if (i == nr_elems) > break; > - if (i % pcpu_entries) > + if (i < j) > goto again; > } > }
On 11/8/2022 12:40 AM, Yonghong Song wrote: > > > On 11/7/22 12:50 AM, Xu Kuohai wrote: >> From: Xu Kuohai <xukuohai@huawei.com> >> >> pcpu_freelist_populate() initializes nr_elems / num_possible_cpus() + 1 >> free nodes for each CPU except the last initialized CPU, always making >> the last CPU get fewer free nodes. For example, when nr_elems == 256 > > ... free nodes for some cpus, and then possibly one cpu with fewer nodes, followed by remaining cpus with 0 nodes. > Will update the commit message to describe it more accurately, thanks. >> and num_possible_cpus() == 32, if CPU 0 is the current cpu, CPU 0~27 >> each gets 9 free nodes, CPU 28 gets 4 free nodes, CPU 29~31 get 0 free >> nodes, while in fact each CPU should get 8 nodes equally. >> >> This patch initializes nr_elems / num_possible_cpus() free nodes for each >> CPU firstly, and then allocates the remaining free nodes by one for each >> CPU until no free nodes left. >> >> Signed-off-by: Xu Kuohai <xukuohai@huawei.com> > > LGTM. Did you observe any performance issues? > No. I ran map_perf_test and did not observe any performance issues. I think it's because the test cases are repeated in loops, so the pcpu_freelists become stable and balanced after the first few loops. > Acked-by: Yonghong Song <yhs@fb.com> > >> --- >> kernel/bpf/percpu_freelist.c | 9 ++++++--- >> 1 file changed, 6 insertions(+), 3 deletions(-) >> >> diff --git a/kernel/bpf/percpu_freelist.c b/kernel/bpf/percpu_freelist.c >> index b6e7f5c5b9ab..89e84f7381cc 100644 >> --- a/kernel/bpf/percpu_freelist.c >> +++ b/kernel/bpf/percpu_freelist.c >> @@ -100,12 +100,15 @@ void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size, >> u32 nr_elems) >> { >> struct pcpu_freelist_head *head; >> - int i, cpu, pcpu_entries; >> + int i, cpu, pcpu_entries, remain_entries; >> + >> + pcpu_entries = nr_elems / num_possible_cpus(); >> + remain_entries = nr_elems % num_possible_cpus(); >> - pcpu_entries = nr_elems / num_possible_cpus() + 1; >> i = 0; >> for_each_possible_cpu(cpu) { >> + int j = i + pcpu_entries + (remain_entries-- > 0 ? 1 : 0); >> again: >> head = per_cpu_ptr(s->freelist, cpu); >> /* No locking required as this is not visible yet. */ >> @@ -114,7 +117,7 @@ void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size, >> buf += elem_size; >> if (i == nr_elems) >> break; >> - if (i % pcpu_entries) >> + if (i < j) >> goto again; >> } >> }
diff --git a/kernel/bpf/percpu_freelist.c b/kernel/bpf/percpu_freelist.c index b6e7f5c5b9ab..89e84f7381cc 100644 --- a/kernel/bpf/percpu_freelist.c +++ b/kernel/bpf/percpu_freelist.c @@ -100,12 +100,15 @@ void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size, u32 nr_elems) { struct pcpu_freelist_head *head; - int i, cpu, pcpu_entries; + int i, cpu, pcpu_entries, remain_entries; + + pcpu_entries = nr_elems / num_possible_cpus(); + remain_entries = nr_elems % num_possible_cpus(); - pcpu_entries = nr_elems / num_possible_cpus() + 1; i = 0; for_each_possible_cpu(cpu) { + int j = i + pcpu_entries + (remain_entries-- > 0 ? 1 : 0); again: head = per_cpu_ptr(s->freelist, cpu); /* No locking required as this is not visible yet. */ @@ -114,7 +117,7 @@ void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size, buf += elem_size; if (i == nr_elems) break; - if (i % pcpu_entries) + if (i < j) goto again; } }