Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2099134rwb; Fri, 11 Nov 2022 05:09:58 -0800 (PST) X-Google-Smtp-Source: AA0mqf6I2Ss5o/6QeEEZLtAztBvY5mn0ARIrMn4P8a/IpXC9S/uMPAtbV+/5FK8UupogrUleQUHc X-Received: by 2002:a05:6402:1cc4:b0:462:df63:5ec5 with SMTP id ds4-20020a0564021cc400b00462df635ec5mr1410377edb.147.1668172197991; Fri, 11 Nov 2022 05:09:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668172197; cv=none; d=google.com; s=arc-20160816; b=SVdAFR20ujTXd/hDZ2VC08puZz4g1V/Ku767qn810b10bjwuq3kNnvu0mJnbbMvh5i nX9yRrO5cwuhXYG1s8v2vpqDA5bnAQkRsQz4FIJgH1vZzXQYIgREPVVTrCCLgvuH91fL 3hs8t07b9KJf1q+NJXmCgWHfeyrG+U35vOk/glZSuYKzqRkQIDTgK959zdQaz3iNpbPR haRLV5ajvVjFG/IbMOnX6DPWTBpj08yDyMApouCW7IjMX8cP//AGR3YYUoV5fQ0dI4V7 uV9mX4KMYK/3M5rcxeyxaVlM3A6lWj3n1AFSLY/h1MiqPPNzh9buEx92yxk5vq74z/nB dpuQ== 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=BdYPafUvj3P9IU2MKntvFsM6/fqLP3qYTHkc1/qjlC0=; b=eBO88/bWTHdiPHPin4TGMaeCGUxdN2Aweo6AikxojO9LLaWiZ21ASkxWOeqhV6QUfL S7ZrrvdcFN7Qm/5HgUR4Ll3vCYkoj2pyIRB0B0xa/sm79W6b/8YmMUaa1ytLOfuWb6Q0 FSLbqfop2PP578QnSeMk+JYB1G8lQcRWR764v5fiAfQsyV4i1xXJ54Wn0ddpqqoNwSt8 nJgBRxDFcKgyQiINDOdlzCb908Aoni5LtDYwQUncOdGm/TyWeotMkNlanx7Ttf02AS2G Ghbpj0E1PIitJ7WUFM7OOP65he9sZVMlGQaP4lHvFrCXoMnUMUguyPzH8pzvviWULY15 knRA== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x12-20020a05640226cc00b004631f24f2d1si1189294edd.610.2022.11.11.05.09.32; Fri, 11 Nov 2022 05:09:57 -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 S233918AbiKKNHH (ORCPT + 91 others); Fri, 11 Nov 2022 08:07:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233950AbiKKNGg (ORCPT ); Fri, 11 Nov 2022 08:06:36 -0500 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BC5087B2B; Fri, 11 Nov 2022 05:06:05 -0800 (PST) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4N7zTy3sdhz4f3lX0; Fri, 11 Nov 2022 21:05:58 +0800 (CST) Received: from k01.huawei.com (unknown [10.67.174.197]) by APP2 (Coremail) with SMTP id Syh0CgA3m7a4SG5jnj9jAQ--.40398S2; Fri, 11 Nov 2022 21:06:01 +0800 (CST) From: Xu Kuohai To: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , wuqiang Subject: [PATCH bpf-next v5] bpf: Initialize same number of free nodes for each pcpu_freelist Date: Fri, 11 Nov 2022 08:22:58 -0500 Message-Id: <20221111132258.762033-1-xukuohai@huaweicloud.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: Syh0CgA3m7a4SG5jnj9jAQ--.40398S2 X-Coremail-Antispam: 1UD129KBjvJXoW7uFyxArWrAw15ZF1xtF18Krg_yoW8ZFWDpr Z3Jw1Yqw1vqrs5uws5Ka1fu34ftw4DGw17Way5Kr1rZry5Ja4vqr10yF4SqFWrWr1Ivr4F yrs09FZ8Aa4UuFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUk2b4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I 0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40E x7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x 0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1l42xK82IY c2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s 026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF 0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0x vE42xK8VAvwI8IcIk0rVWrJr0_WFyUJwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E 87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUrR6zUUUUU 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_NONE 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 From: Xu Kuohai pcpu_freelist_populate() initializes nr_elems / num_possible_cpus() + 1 free nodes for some CPUs, and then possibly one CPU with fewer nodes, followed by remaining cpus with 0 nodes. For example, when nr_elems == 256 and num_possible_cpus() == 32, 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, then allocates the remaining free nodes by one for each CPU until no free nodes left. Signed-off-by: Xu Kuohai Acked-by: Yonghong Song --- v5: Move "head = per_cpu_ptr ..." out of the i-loop as suggesetd by wuqiang v4: Remove unneeded min() v3: Simplify code as suggested by Andrii v2: Update commit message and add Yonghong's ack --- kernel/bpf/percpu_freelist.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/kernel/bpf/percpu_freelist.c b/kernel/bpf/percpu_freelist.c index b6e7f5c5b9ab..de336e3dffee 100644 --- a/kernel/bpf/percpu_freelist.c +++ b/kernel/bpf/percpu_freelist.c @@ -100,22 +100,19 @@ 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; + unsigned int cpu_idx = 0, cpu, i, j, n, m; - pcpu_entries = nr_elems / num_possible_cpus() + 1; - i = 0; + n = nr_elems / num_possible_cpus(); + m = nr_elems % num_possible_cpus(); for_each_possible_cpu(cpu) { -again: head = per_cpu_ptr(s->freelist, cpu); - /* No locking required as this is not visible yet. */ - pcpu_freelist_push_node(head, buf); - i++; - buf += elem_size; - if (i == nr_elems) - break; - if (i % pcpu_entries) - goto again; + j = n + (cpu_idx++ < m ? 1 : 0); + for (i = 0; i < j; i++) { + /* No locking required as this is not visible yet. */ + pcpu_freelist_push_node(head, buf); + buf += elem_size; + } } } -- 2.30.2