Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp97032rwb; Wed, 9 Nov 2022 20:43:24 -0800 (PST) X-Google-Smtp-Source: AMsMyM4kEVStNAj/crWuCfgW57iO6caAgJIN27lIZitdrbTAXvB2Rr8/F7CHUMAvwtb26VZ9JYAJ X-Received: by 2002:a05:6a00:1ac6:b0:56d:919b:f4a1 with SMTP id f6-20020a056a001ac600b0056d919bf4a1mr52241683pfv.50.1668055404143; Wed, 09 Nov 2022 20:43:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668055404; cv=none; d=google.com; s=arc-20160816; b=JMi/+wDDZs4cJPbMYIhOVWzAb60i3nOzyYHqWNWnlFvKNRJDNni9A/5JbA+y58DrVM RSpXHlXCxlmyC0CGr269F89Y4C1NckQdQH1eIjyPW+yaXxWmuIuuXzBNPfB2yUbzQ2wN 6eIBQUlVbL5oMVG0PuOY8YjFtR0MWxQqNiXwb39DtH1YF+J81y6peI5dGUKGS/cD3Ptq qfzLFFbdcJ/0+295ZgREHp/VjL+/bPXBBeGAC1QBuch4vArD7yiNhwWcGXcPlEdnZ5rP YVAfjLPWdcCun3fv0nX7SFVSP7VHgHyZYP32mAOV8wK4FU+SztLLo9OOi3Ac89DpvnsB vXmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=broEoXdt1RORQBiw4E0W6sQ7sc1LDhrBYXkzX8MV7dU=; b=K0Q1hAiiYhyZkscLvTplubCD5j7xSpPrmGINwfrI1YoT5dP15ALRhPu+h1Ia8N/RNq P0Z5hgwPQkwQEoHmaiCtTVcXc1gi5nf3uVIqYMY0uUH/Yi7A4/02YeYOZSwm6HoUofXu tFLED9v8wLscjTTBmc0Hhp85JVpSSrcw/0SNCZ8YQCEwFCWEo0WMFX56G8aMpoxckVOH NkX7D+//RU7P/XZBAsdn7FDuBUzhVOpnTcu37bTPuNLnGU4etHbcBzre7zIAZRLXmJwK SybOpSvtWjkQQ3hau+/rpqyVP8nR9BKMGOiXwMxHQLyjRYZb5thkAnASe0FXYagRoCl5 +f0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=MhhL7Ahc; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u14-20020a056a00124e00b0056262d97880si21221966pfi.186.2022.11.09.20.43.11; Wed, 09 Nov 2022 20:43: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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=MhhL7Ahc; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232544AbiKJEGM (ORCPT + 92 others); Wed, 9 Nov 2022 23:06:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230093AbiKJEGI (ORCPT ); Wed, 9 Nov 2022 23:06:08 -0500 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 365F720F48; Wed, 9 Nov 2022 20:06:03 -0800 (PST) Received: by mail-ej1-x633.google.com with SMTP id t25so1944796ejb.8; Wed, 09 Nov 2022 20:06:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=broEoXdt1RORQBiw4E0W6sQ7sc1LDhrBYXkzX8MV7dU=; b=MhhL7AhcIH5JSSYX4/uG5eIFGmzaru4Q93jwmJ0PVtE+tpK7Dukwu19vxR6LMOu53T D77ijPWUyEuM5d9Y+CopZDWewNcBL3S8WXQFy5QHyGPmMktYW8epiJEtLmYh4axrOIBk tb21BOBMnQxcg6kCnJi1xUJiAdPoVu2QcIzl7/DXVvdYH2BDSdtAU6H25wTHEqN67pfT /xOoIslkTda50mnaWQcrG5kiVri/UT9ZNf5uecZ/rdgLKQfMWSanVHltdjVyIG1cprX0 4r5LfSR+izL9SD8xG+0Np0WqgqNf64H8nFtfUXDPoklF4z1VRV/WBUMyiwqLrY1r6GMK dPEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=broEoXdt1RORQBiw4E0W6sQ7sc1LDhrBYXkzX8MV7dU=; b=HMGwY7+lWwAbcQhQ8thBFXZt68UrxdUS9WTux4XC+4jIsIc1jJy/2ulCgDmIiuZleb W/Bn5MdHi3PYx+jZIPtLFMwXUk1P11uOFH2apSvboUBGa2Z2KQvJigMalarlPzkXr+nO YM+cFr204364AchkZAFpnx4emPNFk/cUWx8tQ+MnjMwqbpgn121lg+tntaJb3IctzlKK tRvzkdUdPrl58FWG11/YAmvFDmIUuzTf8iVeZrN13lFOfdjKkL2EdUcpHlUhay3+Jhm6 aL39PJWQiYmRv81P6v+3rWbVG8W3AFuurPQWCstqzspKz5R2V5MuiNj1EtGdLIL/p1/s ZcEQ== X-Gm-Message-State: ACrzQf2lVtudLzE3ylqkscM6zbO35TgzV5SisWNtEbT+RIHa+5ifujv4 peaUlxtDixz93G2LD1OxTN0ILPPqWzZBlXsgIXI= X-Received: by 2002:a17:906:11d6:b0:7ad:fd3e:2a01 with SMTP id o22-20020a17090611d600b007adfd3e2a01mr39368145eja.545.1668053161605; Wed, 09 Nov 2022 20:06:01 -0800 (PST) MIME-Version: 1.0 References: <20221110035039.54859-1-xukuohai@huawei.com> In-Reply-To: <20221110035039.54859-1-xukuohai@huawei.com> From: Andrii Nakryiko Date: Wed, 9 Nov 2022 20:05:49 -0800 Message-ID: Subject: Re: [PATCH bpf-next v3] bpf: Initialize same number of free nodes for each pcpu_freelist To: Xu Kuohai 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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 Wed, Nov 9, 2022 at 7:33 PM Xu Kuohai wrote: > > 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 > --- > v3: Simplify code as suggested by Andrii > v2: Update commit message and add Yonghong's ack > --- > kernel/bpf/percpu_freelist.c | 27 ++++++++++++++------------- > 1 file changed, 14 insertions(+), 13 deletions(-) > > diff --git a/kernel/bpf/percpu_freelist.c b/kernel/bpf/percpu_freelist.c > index b6e7f5c5b9ab..bd60070c079f 100644 > --- a/kernel/bpf/percpu_freelist.c > +++ b/kernel/bpf/percpu_freelist.c > @@ -100,22 +100,23 @@ 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, cpu_idx, 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(); > + > + cpu_idx = 0; > > 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 = min(n + (cpu_idx < m ? 1 : 0), nr_elems); why the min() here? > + for (i = 0; i < j; i++) { > + head = per_cpu_ptr(s->freelist, cpu); > + /* No locking required as this is not visible yet. */ > + pcpu_freelist_push_node(head, buf); > + buf += elem_size; > + } > + nr_elems -= j; > + cpu_idx++; > } > } > > -- > 2.30.2 >