Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1105346rwn; Thu, 8 Sep 2022 13:34:13 -0700 (PDT) X-Google-Smtp-Source: AA6agR6fAfxALID4LVAbfAU1KIcwNa/sv2OV6rmEo4iuqlznA2o+D7BB8pz9PkWg6nzdu5RJ1ewE X-Received: by 2002:a19:ad47:0:b0:494:846e:bf0a with SMTP id s7-20020a19ad47000000b00494846ebf0amr3275574lfd.576.1662669252942; Thu, 08 Sep 2022 13:34:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662669252; cv=none; d=google.com; s=arc-20160816; b=AVa8MXhrT7PrH7R86IJcrqdtp8klPt8rTmv9AIYpCel2PAR4PqpvTMGRxZ+fMrP1Jl MhVkuHzcwY5c4JCJ3QvYazdHrAbCa6VnqOl76wrP+hLHN5XLbGuQjiLQ44SZqvp9srk2 79EkRHRymEQwmYyM0/MSG+AbHeJbv8iIpZlVmnouP8R2Q/SqZDp7KbBZSJOPo40oZ1Eg L2bVuwi6WJsdlGf+U9+rR/hCG/I2NMBmjAIllROsUoxgv9OTmXDoVPtTgBs6HWKzTSE5 Cl60HCCC+6DFc9F1EYhbvvabxpTnfEf4TxHfSnIphHGBm2scmE01lIWIciitFzUl8y2Q 5hiQ== 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=sAPX+ZC38kQtr2QFRpu/URBXzXPjKhVIvzN8o4lUIkw=; b=Rjsdhronq74bhJc5pZUouuZb2OgYK98ZdVP3mDuyrVqOhDhdvqlVgmGloht6YdjsTD Jp+UYizTCZbBcX/F8r/7hbxKZvC/rY1++iX6o7P9lj/sHKJI0SszCCVTGj/8UC4BCvk9 w4d0NGMVPE86Qj0UmZXU4HMRKAxSxd2spQ/OvAFRBu+2ngO9VMsJjNzEwjecusQkXw2s U8eetyaaYfM86oV0blynNz484gSOmD6zEJpUXyN+Cm6u+4qCOIavWoMQgVqx2nV86HSF VbNQXs0hEjrwEPSEU/2TEMWhqPgMQ8tQNHrMGxtfwbANqwHkX+vY47Xuxp45MSAE9Il1 xCJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dSeIQS9E; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d3-20020a2e3603000000b0025ecb4f750bsi8248380lja.341.2022.09.08.13.33.44; Thu, 08 Sep 2022 13:34:12 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=dSeIQS9E; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229631AbiIHUVV (ORCPT + 99 others); Thu, 8 Sep 2022 16:21:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229488AbiIHUVT (ORCPT ); Thu, 8 Sep 2022 16:21:19 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8315AEC74A; Thu, 8 Sep 2022 13:21:18 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 07D56B82258; Thu, 8 Sep 2022 20:21:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AE503C433C1; Thu, 8 Sep 2022 20:21:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1662668475; bh=6pLu81MViNZ3vOfUBzSq3rkw+GwHKE8Ga0clDe9AKfw=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=dSeIQS9ErZc40hJTb/ceFFxw+TGRCEkSQ9mkV5D98K5gITwdVUxh9TqSZM9UQiwhJ eWZ7y3zDBex5COdbvvVpyjiJ6SMUhX3NyarURYHA1FvXOGoU/UikpELaoiCeFUY64y gE38cbAopvou/oZWj+MlzpKIc9R9J6rfxv5xmz4tqoUhMxb6MAbj+T9n4kQVFqeSDy e5zp+sYnQtRIammvTPmY8Ivhve8XbJ7cjJ+8qJapSx0KgPrNbd883WLAcoTCcCvypx Osgrq6dUvSB5zHzwsuTvT9XywDm3GGaMB78mXI4VoE4Szj9Nyi8Yh8I1oeXIvqcUD9 Cpv2zfHpr8gfg== Received: by mail-oa1-f49.google.com with SMTP id 586e51a60fabf-11e9a7135easo47450154fac.6; Thu, 08 Sep 2022 13:21:15 -0700 (PDT) X-Gm-Message-State: ACgBeo3BPNob3zVGNfqvhlmj1BLniWeLBg1gO4qgiEoIVx+xYefLmWEm IVBzv9jI/Qn6fI7Knags7Iec25CuEpZk9mbiC5I= X-Received: by 2002:aca:3016:0:b0:345:9d47:5e11 with SMTP id w22-20020aca3016000000b003459d475e11mr2161800oiw.31.1662668474858; Thu, 08 Sep 2022 13:21:14 -0700 (PDT) MIME-Version: 1.0 References: <20220907155746.1750329-1-punit.agrawal@bytedance.com> <877d2ecffy.fsf_-_@stealth> In-Reply-To: <877d2ecffy.fsf_-_@stealth> From: Song Liu Date: Thu, 8 Sep 2022 13:21:04 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: Re: [PATCH v2] bpf: Simplify code by using for_each_cpu_wrap() To: Punit Agrawal Cc: Alexei Starovoitov , bpf , open list , zhoufeng.zf@bytedance.com, Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Yonghong Song , John Fastabend , KP Singh , Jiri Olsa Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Thu, Sep 8, 2022 at 3:45 AM Punit Agrawal wrote: > > Hi Song, > > Thanks for taking a look. > > Song Liu writes: > > > On Wed, Sep 7, 2022 at 8:58 AM Punit Agrawal > > wrote: > >> > >> In the percpu freelist code, it is a common pattern to iterate over > >> the possible CPUs mask starting with the current CPU. The pattern is > >> implemented using a hand rolled while loop with the loop variable > >> increment being open-coded. > >> > >> Simplify the code by using for_each_cpu_wrap() helper to iterate over > >> the possible cpus starting with the current CPU. As a result, some of > >> the special-casing in the loop also gets simplified. > >> > >> No functional change intended. > >> > >> Signed-off-by: Punit Agrawal > >> --- > >> v1 -> v2: > >> * Fixed the incorrect transformation changing semantics of __pcpu_freelist_push_nmi() > >> > >> Previous version - > >> v1: https://lore.kernel.org/all/20220817130807.68279-1-punit.agrawal@bytedance.com/ > >> > >> kernel/bpf/percpu_freelist.c | 48 ++++++++++++------------------------ > >> 1 file changed, 16 insertions(+), 32 deletions(-) > >> > >> diff --git a/kernel/bpf/percpu_freelist.c b/kernel/bpf/percpu_freelist.c > >> index 00b874c8e889..b6e7f5c5b9ab 100644 > >> --- a/kernel/bpf/percpu_freelist.c > >> +++ b/kernel/bpf/percpu_freelist.c > >> @@ -58,23 +58,21 @@ static inline void ___pcpu_freelist_push_nmi(struct pcpu_freelist *s, > >> { > >> int cpu, orig_cpu; > >> > >> - orig_cpu = cpu = raw_smp_processor_id(); > >> + orig_cpu = raw_smp_processor_id(); > >> while (1) { > >> - struct pcpu_freelist_head *head; > >> + for_each_cpu_wrap(cpu, cpu_possible_mask, orig_cpu) { > >> + struct pcpu_freelist_head *head; > >> > >> - head = per_cpu_ptr(s->freelist, cpu); > >> - if (raw_spin_trylock(&head->lock)) { > >> - pcpu_freelist_push_node(head, node); > >> - raw_spin_unlock(&head->lock); > >> - return; > >> + head = per_cpu_ptr(s->freelist, cpu); > >> + if (raw_spin_trylock(&head->lock)) { > >> + pcpu_freelist_push_node(head, node); > >> + raw_spin_unlock(&head->lock); > >> + return; > >> + } > >> } > >> - cpu = cpumask_next(cpu, cpu_possible_mask); > >> - if (cpu >= nr_cpu_ids) > >> - cpu = 0; > > > > I personally don't like nested loops here. Maybe we can keep > > the original while loop and use cpumask_next_wrap()? > > Out of curiosity, is there a reason to avoid nesting here? The nested > loop avoids the "cpu == orig_cpu" unnecessary check every iteration. for_each_cpu_wrap is a more complex loop, so we are using some checks either way. OTOH, the nesting is not too deep (two loops then one if), so I guess current version is fine. Acked-by: Song Liu > > As suggested, it's possible to use cpumask_next_wrap() like below - > > diff --git a/kernel/bpf/percpu_freelist.c b/kernel/bpf/percpu_freelist.c > index 00b874c8e889..19e8eab70c40 100644 > --- a/kernel/bpf/percpu_freelist.c > +++ b/kernel/bpf/percpu_freelist.c > @@ -68,9 +68,7 @@ static inline void ___pcpu_freelist_push_nmi(struct pcpu_freelist *s, > raw_spin_unlock(&head->lock); > return; > } > - cpu = cpumask_next(cpu, cpu_possible_mask); > - if (cpu >= nr_cpu_ids) > - cpu = 0; > + cpu = cpumask_next_wrap(cpu, cpu_possible_mask, orig_cpu, false); > > /* cannot lock any per cpu lock, try extralist */ > if (cpu == orig_cpu && > > > I can send an updated patch if this is preferred. > > Thanks, > Punit