Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp2413633rdb; Mon, 25 Dec 2023 09:38:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IEolUaRmC9YEpoWJalZHp6TVIsG5WxcwCuLBhM+35McpDbVES8EH1opAdqa3pSVUPN8pnNC X-Received: by 2002:a05:600c:4509:b0:40d:516b:b51c with SMTP id t9-20020a05600c450900b0040d516bb51cmr2001125wmo.78.1703525905995; Mon, 25 Dec 2023 09:38:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703525905; cv=none; d=google.com; s=arc-20160816; b=peW6RxmfANmXoXThHAYgqcR6FkTIq9XEbG0UbL+84zhMScdmpY+zPtoDiEfPfYMpTy ELLCGt6K3WAmCSIHvSjyUbJs5klS0O5+qBHY5b1YiYf8siqsXFowayiSpzSa8n4ca4tP +uuYsy8REa7Bba9/nUMOVRyBMe3b+qPzIF5dB4PFpTO/okcZEa5mZYsBAxOIWkErYLIM pzDq/jmm8odEvieF2Nfc8LXKsLOtpcvIB3G1Wj1HWV4N9aDT4bPlTGGV8BRAJg/q3/75 ZDLBKU20UgwkeCegA4fLd1lcOtdKQHEeMyo5ShEVR04lMTsVW2tCQjiDv5CtUJcvW02/ On8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=+qVYfNb+ln88k2U8sDJhGRbrHOCr6KMYYSe1kdUIoOQ=; fh=P8WKVbhiWBy10Q9giupoRy5TLybsQirmOwtBEYAPRCY=; b=sieduTMXdYQZTov26ACWbwy8Xg0SQVfOsj7Pp+B3aIu/7kkkHQpe7f23Z65fEcYcXF QlU0wWmF/N0fkeXxs0+mCnl3j4PIrmBpUJADw6fkQNr6P4CNZmTs7KTNa6Dzy9PQ0sDq qTwzSHN9LsVqLM3AyHCrIJZ6DvPGloo5T7bbnxGHY0Z+yvfwW04b1XikpG/kB3MObIKo Qsbs0/NFI46CQ3TaJALIwlWLO3paZ26aQ9aUcmmHmnU56A9eGd2Pw0J9YWSsB5UAOq7B clZM/6M8q0UZGhu5O/VFHHsF7QAQlmnUDtwV9O0F4o/4AQEhAJB02sd86bI35SozoEo4 TR2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=MgXmlNGu; spf=pass (google.com: domain of linux-kernel+bounces-11129-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-11129-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id d7-20020a170906040700b00a24a321b843si4556168eja.670.2023.12.25.09.38.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Dec 2023 09:38:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-11129-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=MgXmlNGu; spf=pass (google.com: domain of linux-kernel+bounces-11129-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-11129-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id BE2F31F22107 for ; Mon, 25 Dec 2023 17:38:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5E40F52F88; Mon, 25 Dec 2023 17:38:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MgXmlNGu" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3EB0351C36 for ; Mon, 25 Dec 2023 17:38:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yb1-f177.google.com with SMTP id 3f1490d57ef6-dbe165485d4so56123276.0 for ; Mon, 25 Dec 2023 09:38:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703525897; x=1704130697; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=+qVYfNb+ln88k2U8sDJhGRbrHOCr6KMYYSe1kdUIoOQ=; b=MgXmlNGuIHbsGFKwqLbGEkup41RdZXo9/2VTEWfVn3UwvgxEi7O932ltRypM2oDWSy /k0OJF/sIWHs1xFIgoL8byuIwJVMzzZm2RjSdbGtE2iHkoV7wego6KEiDfr8OCooRRJJ cb8Vxv6y61ZtLfHtYhsyYsggoCoW8l/UjDRgoEg4iVImVI//tuIlgm0lh0+jth5gRvDI mLi0liiW7pgDhH1UKBmvcXEgG1A/8sAlAyPtYCTz2hB16uDKy2jMpK6Hn4udIIdwyTmX rDhgaq+gCWFXCNtzJcr/jXhiU963m6R1DWhA4VZUlAb42eJELRLS1viNAk2izPyJQxIw JArQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703525897; x=1704130697; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=+qVYfNb+ln88k2U8sDJhGRbrHOCr6KMYYSe1kdUIoOQ=; b=MyUz+sAR/4qSIcaJV61LryUmUrXO33AumunBCSQ/SaBCEEiZ5vUOUs0kL6fmdvrqlb 8fLywrgXrm9YmKxfogN+ss80f2YiFzZnSv95INASzHcotcIPla7WvP0j2MokhLFdhitN V/i7LrveOhEdZZ6sCfQVzRssSa8Z2EyTE4hEa85vQzkovylDz9jpECtfc4+ybVSUqoXY CE0objH0AUj4j8mDEDfLxjFJs5WXsY3OgfYMoxLPBJ0hWV4qZ7I1umeYzd33NLergCxk vsYB8ICvHlXtPC3o7gNxW9D8KQkImU2FiHikmdplO1BHEBAbq+Fuf+4vuk+IRxn2clgK lFlQ== X-Gm-Message-State: AOJu0YwvqMlCOFF4kc+BIjYNdTXpi/J0HOSU2LGgjJujmLEvK4tH/+KP Pz0tNTrTc45f+pnDMVfB08Q7V7nJMCE= X-Received: by 2002:a05:690c:290b:b0:5e9:fa4a:fd56 with SMTP id eg11-20020a05690c290b00b005e9fa4afd56mr2133759ywb.13.1703525897109; Mon, 25 Dec 2023 09:38:17 -0800 (PST) Received: from localhost ([2601:344:8301:57f0:3e97:6886:45bb:11c4]) by smtp.gmail.com with ESMTPSA id gz1-20020a05690c470100b005eae538dd1asm2640058ywb.27.2023.12.25.09.38.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Dec 2023 09:38:16 -0800 (PST) Date: Mon, 25 Dec 2023 09:38:15 -0800 From: Yury Norov To: Ming Lei Cc: Andrew Morton , Thomas Gleixner , linux-kernel@vger.kernel.org, Andy Shevchenko , Rasmus Villemoes 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Wed, Dec 13, 2023 at 08:06:48AM +0800, Ming Lei wrote: > 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 the same Schlemiel the Painter's code, as the plain for() was. > 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