From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0A6CC38145 for ; Thu, 8 Sep 2022 10:46:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231311AbiIHKqA (ORCPT ); Thu, 8 Sep 2022 06:46:00 -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-Google-Smtp-Source: AA6agR7PHj3999h/guCh5epJRzTGtcyBBxFZmcyyWtX9b7PNdQCFkLkPn89R+suWBlEtQDuigYYdgg== 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 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