Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp424933rwn; Thu, 8 Sep 2022 03:54:34 -0700 (PDT) X-Google-Smtp-Source: AA6agR5Yz/L5PnquE4ALlJujgZMB+7qU1ATZlIZshDWNnK8yq/ZoJLRWMcNazaE1nxud+Vbu4xx8 X-Received: by 2002:a17:90b:1c8a:b0:1fe:1536:5617 with SMTP id oo10-20020a17090b1c8a00b001fe15365617mr3516266pjb.246.1662634474480; Thu, 08 Sep 2022 03:54:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662634474; cv=none; d=google.com; s=arc-20160816; b=og0LPYBOqUXbOUuKSc7h8lrm7Pj0NNNebVAyZ651BZ2Phys2x/dD768ErbtQ5Fl85z WC0XHRdBnJaU5unDE3kz8UuotdJBeYEIKrkwLfHNFJduhhsdEnreH+kNXhHWXLJuLjpZ aaQ48T9ytnVjAstaPtkIyxfjJgvbyR0rQz3E8npXVWFmerMasTrDGGworm4SJTBm461c G/DdKLOl4gZAWR/xKv7nyWuez4Ddn7he9AHAcdmmUwiR1mKpqXXCZhJh0e133IV4iFvI y2ARqYeqxSPUSvKYP1Cbmgu3/gjDpzFgoBR1zdO+7wp/31BmxhNgW29tdp4i3FGKWeJU SaSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:message-id:in-reply-to :date:references:subject:cc:to:from:dkim-signature; bh=SxXiDCpv5ePqARD9ofSsBk9oYeXNDztMJRIdlqicQ3s=; b=LVU2NrnzLD6yxV1DQizu9bFnBxntTj+hwTe4QPEAvOSIoZTga6djjAbmUqh6N5n+ED iaM/JsaEzcjePw19wSu8p21N3eQ25bSMhwUYtgb8khczf7u6C6kB5BplW96OWdbFsBEX 1bCfvuq2BtWDrvWOQ0tvgXe+Y+7dTrMQJM2tDZPFKdd6R2a36etqs+aadYLe5TQ3VJQa vHl7VcBCQndmVOsnwmRwgOguOUbQ6fGPRvn9NXcoqXashWiwKtkBgSEcbiM5bakC55X1 i42xV9SPeAHqpfx1AAwJUHMsloUyXwSUbrsjlrLnUlucKJqPLXEXQREo6iaUjdfYAAiC AdnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=Ic5bgE3q; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a184-20020a6390c1000000b004386cd2cc65si432811pge.300.2022.09.08.03.54.21; Thu, 08 Sep 2022 03:54:34 -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=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=Ic5bgE3q; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230411AbiIHKp7 (ORCPT + 99 others); Thu, 8 Sep 2022 06:45:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231292AbiIHKpn (ORCPT ); Thu, 8 Sep 2022 06:45:43 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A9417B299 for ; Thu, 8 Sep 2022 03:45:41 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id c2-20020a1c3502000000b003b2973dafb7so1371433wma.2 for ; Thu, 08 Sep 2022 03:45:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=mime-version:user-agent:message-id:in-reply-to:date:references :subject:cc:to:from:from:to:cc:subject:date; bh=SxXiDCpv5ePqARD9ofSsBk9oYeXNDztMJRIdlqicQ3s=; b=Ic5bgE3qmdoInb3nyWjrnLqplQa6JS/sUvuCBa8GbFL4dYzZq/OL04Aco7SKYxso76 5MmQi/s9waL9MULGvdwKV/9mkAltXgmwf3QwESiwpGxDBWgplt6rwEqNNgJBf1bEYnK7 kqTpS+chO4JvrQnZcnjVfdZ0IVc6z6J/EuCIrpSDauTP6cB4XKOMOi5DbHgQ9hBnhv7t dCJvfhxdddGvqpMm/65MJjSPkEDLDXr2i97AJXMihpPdgxiMRITtCRZ6JFvVP8O7BE8P /epaRbWs6elt9fziAUc0+WtVGdhCIkTObXTHnblHWDxWr4g7aWjzGfExywezz3expnmC 2Sbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:message-id:in-reply-to:date:references :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date; bh=SxXiDCpv5ePqARD9ofSsBk9oYeXNDztMJRIdlqicQ3s=; b=FulfrGQGjQgHZkO6JLelhyEhZJZP2B92xGDQ5g7rXIkb+RQkTRCTOdw4EPd6mn1ZPo u6ceuUw712vxoiYf08WqC92r29egLvjEE2cEJFRMfUg8IfwtPWLV2BAZ2DzAB4cStQAD ym9nZHnaRTfcbs04ahhWSYfjIIYUFZUCc28il1mE7forn4Ccr5SqsrchmkVzbuIPXsAi 7NG0WnApkOJkmMl+3CLaeb29FexAzzynk8ZimFFGk507jItri1H1bO+cmxIZq0/H5sco RQ8MjLPovoBOPLeTpqy2qMov9Cx0FiswN/Ec2QrFir0u1j9XYKsBMEBmUqnMlnM4k4kh usXg== X-Gm-Message-State: ACgBeo35SkTssuzcoFJ8X1moQi2mM+h8cWTp7lLXHL4jB63XO6o6SSYX 6NfPXTRhhOc5WXca8i7YUA2Y5A== X-Received: by 2002:a05:600c:4e52:b0:3a6:d89:4d1b with SMTP id e18-20020a05600c4e5200b003a60d894d1bmr1891759wmq.150.1662633939770; Thu, 08 Sep 2022 03:45:39 -0700 (PDT) Received: from localhost ([95.148.15.66]) by smtp.gmail.com with ESMTPSA id k39-20020a05600c1ca700b003a5f3de6fddsm2611839wms.25.2022.09.08.03.45.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 03:45:38 -0700 (PDT) From: Punit Agrawal To: Song Liu Cc: Punit Agrawal , Alexei Starovoitov , bpf , open list , zhoufeng.zf@bytedance.com, Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Yonghong Song , John Fastabend , KP Singh , Jiri Olsa Subject: Re: Re: [PATCH v2] bpf: Simplify code by using for_each_cpu_wrap() References: <20220907155746.1750329-1-punit.agrawal@bytedance.com> Date: Thu, 08 Sep 2022 11:45:37 +0100 In-Reply-To: (Song Liu's message of "Wed, 7 Sep 2022 17:55:23 -0700") Message-ID: <877d2ecffy.fsf_-_@stealth> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 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. 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