Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4996549rdb; Tue, 12 Dec 2023 16:07:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IGckXhAIUWSnGYbEiZAsM+nSY90Gwt5oCoTv1KXeSLApJ2PNKobxboChOCJUymDLOaxxRF/ X-Received: by 2002:a05:6a20:158b:b0:18f:c6f0:62c5 with SMTP id h11-20020a056a20158b00b0018fc6f062c5mr4260114pzj.96.1702426062007; Tue, 12 Dec 2023 16:07:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426061; cv=none; d=google.com; s=arc-20160816; b=0HbcE6xW0TIFLO8bpuJ+V8nAAthH2lyIDcsGBjAvyvf8rLGaYUniBnxYeytxfz43aT 1HSSjY0a/el8d8Pgem57s38k+ltkkZ/kNJ14idLOi7+0wuFtA+82Dm/JO0rpElFRSDze 2H/KXPuCET6Fb29KUOvqqDeXRXZKOktp1+44habFNISY49jSQ6rEVixejqqUBt7m3DYh AMEkv56T9bPiKHO78zAfrMmYu0PALf0tIKjxUYfAE5a3447kTcGridpKUj3nrlLyuGpK 2XzpzbxrwOydStu3jLCGFyvXaqYPIKWMtWF5vDbNHfT61qt9TE0DJpPpyw0J4joAboFc 5x8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=LJ4KbsvY2/ywK+sqBP6N+RhYq5r1N/+Z719Qf3le3yQ=; fh=KhqnTocrEvz7RHBjuOgxYl/THIwZiFJnmB5udX32Fs8=; b=IIRBjr3PnvP5SypssoOjc6L52BOoAUfUpckU2Q9iuLi309bQUG8JGFznCvWdXo74GM 95NifhJytwkUiKjlllrKDCblP4aTKLNG4kGImcUkV4AZqWbo6ZhYPx9P0bXrUsGWZ4Wz C/IZqRXjPw+xoOXagy9mp6bZfIzD5ogNkyNwjLGhJ5yNv/nY6hTZ+dEWjwWkmSUkqeFi xtcmSEFTfVEcJL4rwgNMfPLqhxJ8T8bbPlhZBr3VeAFzjiR+kO3/x1RUbHqcArHJwO/A j//fMG4XqORlvrmFhMEbeFZoN2Vwv6sC1KOhyAUkuBohjPwShsbz2w8oHXOXoL7KLhYR ZiRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VvNyeZ3w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id z17-20020a170903019100b001d0ab464f3bsi8696318plg.345.2023.12.12.16.07.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:07:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VvNyeZ3w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 9179580AFBB9; Tue, 12 Dec 2023 16:07:39 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377822AbjLMAHO (ORCPT + 99 others); Tue, 12 Dec 2023 19:07:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377801AbjLMAHF (ORCPT ); Tue, 12 Dec 2023 19:07:05 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53C25BE for ; Tue, 12 Dec 2023 16:07:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702426020; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=LJ4KbsvY2/ywK+sqBP6N+RhYq5r1N/+Z719Qf3le3yQ=; b=VvNyeZ3wwcxGnSL54XP4kZ/sU5SEDbfBlZrFVmhrUClKMdM08GS+qOFyOXjYqGLZJNeb5F IiFdGdm53IWfnFlSEKNK/rOwpExJZ5RjJHUmQvfsW2yg2rrrAuUxNwuSFWJBTBdM43NrDA DApf13WBH9tKopU9aIHn05dRrKU17L8= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-400-AP-7cYdjOrKENm1rXQUcrQ-1; Tue, 12 Dec 2023 19:06:58 -0500 X-MC-Unique: AP-7cYdjOrKENm1rXQUcrQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 198371C05EA4; Wed, 13 Dec 2023 00:06:58 +0000 (UTC) Received: from fedora (unknown [10.72.120.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AACE5492BE6; Wed, 13 Dec 2023 00:06:53 +0000 (UTC) Date: Wed, 13 Dec 2023 08:06:48 +0800 From: Ming Lei To: Yury Norov Cc: Andrew Morton , Thomas Gleixner , linux-kernel@vger.kernel.org, Andy Shevchenko , Rasmus Villemoes , ming.lei@redhat.com Subject: Re: [PATCH v3 2/7] lib/group_cpus: optimize inner loop in grp_spread_init_one() Message-ID: References: <20231212042108.682072-1-yury.norov@gmail.com> <20231212042108.682072-3-yury.norov@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,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 morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:07:39 -0800 (PST) On Tue, Dec 12, 2023 at 09:04:19AM -0800, Yury Norov wrote: > On Tue, Dec 12, 2023 at 05:46:53PM +0800, Ming Lei wrote: > > On Mon, Dec 11, 2023 at 08:21:02PM -0800, Yury Norov wrote: > > > The loop starts from the beginning every time we switch to the next > > > sibling mask. This is the Schlemiel the Painter's style of coding > > > because we know for sure that nmsk is clear up to current CPU, and we > > > can just continue from the next CPU. > > > > > > Also, we can do it nicer if leverage the dedicated for_each() iterator, > > > and simplify the logic of clearing a bit in nmsk. > > > > > > Signed-off-by: Yury Norov > > > --- > > > lib/group_cpus.c | 13 ++++++------- > > > 1 file changed, 6 insertions(+), 7 deletions(-) > > > > > > diff --git a/lib/group_cpus.c b/lib/group_cpus.c > > > index ee272c4cefcc..10dead3ab0e0 100644 > > > --- a/lib/group_cpus.c > > > +++ b/lib/group_cpus.c > > > @@ -30,14 +30,13 @@ static void grp_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk, > > > > > > /* If the cpu has siblings, use them first */ > > > siblmsk = topology_sibling_cpumask(cpu); > > > - for (sibl = -1; cpus_per_grp > 0; ) { > > > - sibl = cpumask_next(sibl, siblmsk); > > > - if (sibl >= nr_cpu_ids) > > > - break; > > > - if (!cpumask_test_and_clear_cpu(sibl, nmsk)) > > > - continue; > > > + sibl = cpu + 1; > > > > It doesn't have to 'cpu + 1', see below comment. > > > > > + > > > + for_each_cpu_and_from(sibl, siblmsk, nmsk) { > > > + cpumask_clear_cpu(sibl, nmsk); > > > cpumask_set_cpu(sibl, irqmsk); > > > - cpus_per_grp--; > > > + if (cpus_per_grp-- == 0) > > > > if (--cpus_per_grp == 0) > > That's right, I'll send a new version this weekend. > > > > + return; > > > > I think for_each_cpu_and() should work just fine, cause cpu has been cleared > > from nmsk, so the change can be something like, then patch 1 isn't > > necessary. > > It works just fine except that it's O(N^2), where O(N) is easily > achievable. Again, it's not about performance, it's about coding > habits. Both for_each_cpu_and() and for_each_cpu_and_from() are O(N), aren't they? Given both two are based on find_next_and_bit(). for_each_cpu_and() is simpler and more readable, and more importantly, we can save one single-user public helper. > > > for_each_cpu_and(sibl, siblmsk, nmsk) { > > cpumask_clear_cpu(sibl, nmsk); > > cpumask_set_cpu(sibl, irqmsk); > > if (--cpus_per_grp == 0) > > return; > > } Thanks, Ming