Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp4467rwb; Wed, 9 Nov 2022 18:59:19 -0800 (PST) X-Google-Smtp-Source: AMsMyM4l1VaJPqixuuFyqlXvio8kh/+SNe/QvbHVRBvGSMa2BwLCLKNMBGq388J+eO6cKrKMoJlu X-Received: by 2002:a05:6402:13d6:b0:461:b1b9:bed0 with SMTP id a22-20020a05640213d600b00461b1b9bed0mr1508048edx.122.1668049159315; Wed, 09 Nov 2022 18:59:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668049159; cv=none; d=google.com; s=arc-20160816; b=Q6b9LfVzJzGHArwrJR7jwUMbg+VYImvrA1ncKDwHQ+GLkJJviIATRMAx3vHHYDlMG9 aK/P4Gsn2AgF68YoihIc2daoX0pEoDpihpox2Ib9M+pvMYQIHfh8PTMPTr/7nvxgNdHL rML+jR3wOivbXoQR5L6tGweu8L1oIS1koPUazDhI1wKsQLdvnVZqYZSo0bHYH22K8gOB mMCZwXk/6RoZ1Eng4BxutR+CJEd8Eb96aZrRqS4Lt+eabOkZHdDragbLAZhtUKWXuCNm bS6PUGeJYbZsU3P9xoGMOy4WarsrfC1kKo0wp/U18unNqnMyL+msAfrNockrOFwO5XcE lLnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=GrpSwKogUcostQSoHeEWjEIhESDeYJhcvCqZPi0QRz4=; b=Fy6OPT0u2dDZpJelAa3IKqxlALpln7dSSYd9TWIHDFmawhnP2m+lH6FIgjsgQfxmzg x7wKqF6dwKDEf3Q4nXeNlX0PGmzaTtn09i/9ZyvPg28JhqdrKG7rCmcUzMCMqa6e2bzm WxFHB6dkYUQOldcIpec0Mo3lfCE9nd21vdrcChyM9RcKy9HsDxrhN9ALVKffOA7LyYAv NuZCApWxldMA3veVhATT6RsVVvUAx/2xEA9VODse+947Eu80y2VOgxaYhe17qCVuM3Hk urhrsYSYadOBDGpohMHYnUSGDdQz73rwB5AMwCQxbNlaVH696jpsL43/sOOthGDrembq +Erg== 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 jx15-20020a170907760f00b007adbc807ca8si13918594ejc.137.2022.11.09.18.58.51; Wed, 09 Nov 2022 18:59:19 -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 S232317AbiKJCj1 (ORCPT + 93 others); Wed, 9 Nov 2022 21:39:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232187AbiKJCjZ (ORCPT ); Wed, 9 Nov 2022 21:39:25 -0500 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1255713EAD; Wed, 9 Nov 2022 18:39:24 -0800 (PST) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4N75dL0p1bz4f3mWP; Thu, 10 Nov 2022 10:39:18 +0800 (CST) Received: from [10.67.111.192] (unknown [10.67.111.192]) by APP2 (Coremail) with SMTP id Syh0CgCHnLdYZGxj3g0UAQ--.21205S2; Thu, 10 Nov 2022 10:39:21 +0800 (CST) Message-ID: <9caf3639-97f7-367f-a4a2-1c31b271d620@huaweicloud.com> Date: Thu, 10 Nov 2022 10:39:20 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCH bpf-next v2] bpf: Initialize same number of free nodes for each pcpu_freelist Content-Language: en-US To: Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa References: <20221108142207.4079521-1-xukuohai@huaweicloud.com> From: Xu Kuohai In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-CM-TRANSID: Syh0CgCHnLdYZGxj3g0UAQ--.21205S2 X-Coremail-Antispam: 1UD129KBjvJXoWxWryUAry8Xw4rWFWxuFy7Awb_yoW5CrWrpF 4fJ3WUtwn5Xr98uw4rW3W7WrW3twsrGr17Wa98KF15Ary3tFyvqr1vkFs0qrWxWr17Zr1F yr4q9FZrZa4UXFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvIb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x 0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG 6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFV Cjc4AY6r1j6r4UM4x0Y48IcVAKI48JM4IIrI8v6xkF7I0E8cxan2IY04v7Mxk0xIA0c2IE e2xFo4CEbIxvr21l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxV Aqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q 6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6x kF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWrZr1j6s0DMIIF0xvEx4A2jsIE 14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf 9x07UWE__UUUUU= X-CM-SenderInfo: 50xn30hkdlqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, 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: X-Mailing-List: linux-kernel@vger.kernel.org On 11/10/2022 7:56 AM, Andrii Nakryiko wrote: > On Tue, Nov 8, 2022 at 6:05 AM Xu Kuohai wrote: >> >> 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, 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, 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 >> --- >> v2: Update commit message and add Yonghong's ack >> --- >> 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; >> } > > this loop's logic is quite hard to follow, if we are fixing it, can we > simplify it maybe? something like: > > int cpu, cpu_idx, i, j, n, m; > > n = nr_elems / num_possible_cpus(); > m = nr_elems % num_possible_cpus(); > > for_each_possible_cpu(cpu) { > i = n + (cpu_idx < m ? 1 : 0); > for (j = 0; j < i; j++) { > head = per_cpu_ptr(s->freelist, cpu); > pcpu_freelist_push_node(head, buf); > buf += elem_size; > } > cpu_idx++; > } > > > no gotos, no extra ifs: for each cpu we determine correct number of > elements to allocate, then just allocate them in a straightforward > loop > that's great, will update to: int cpu, cpu_idx, i, j, n, m; n = nr_elems / num_possible_cpus(); m = nr_elems % num_possible_cpus(); for_each_possible_cpu(cpu) { j = min(n + (cpu_idx < m ? 1 : 0), nr_elems); for (i = 0; i < j; i++) { head = per_cpu_ptr(s->freelist, cpu); pcpu_freelist_push_node(head, buf); buf += elem_size; } nr_elems -= j; cpu_idx++; } >> } >> -- >> 2.30.2 >>