From nobody Wed Dec 17 06:28:14 2025 Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) (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 9E998ECD for ; Sat, 20 Jan 2024 02:50:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719060; cv=none; b=TyLJ5aEXP0TRGPTykgxG0xaecSdYEOB92v3ZuDQ37O6kPVz0Oo1LweJxbytiCdzoLkaphKef6njDOQxYqTwdKLiq8iFO4v/vzD5l9F96cnXElLjdIoXcEUK9306RSKaQs4ehxkqv9sPDLmYK9kqPeVFe24t1eE5+d2AV4DWC2aU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719060; c=relaxed/simple; bh=SVwOa745y/p7doJwcbG2VSsSKIR/sIktsJ8KB1vHWPM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Z2owwEFmf3ocAnKkzA3b4eLl2qqxfWT9EXsPko9RAJvIqzaHbfKOkl3pg+HBRLBbjCBHAqipVFiyZRfvbnlWE+0irM0tMwb7A1Wd9Wq3foTgAVaHbnQZr91LraWZut0slrZ9Cq93cFrPw+8591g27cyuaNAZlWZznun2TbWKAgQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cZBtEHOe; arc=none smtp.client-ip=209.85.128.176 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cZBtEHOe" Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-5ebca94cf74so13947847b3.0 for ; Fri, 19 Jan 2024 18:50:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705719057; x=1706323857; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=t+ncE/Ke8W/9eSicUFGfjAMk7XfHt6TzmaC+XB67O8c=; b=cZBtEHOeuUjLoazd/HsZGtCtBfAy7fB5HFRlygF6yNS7WO46pqoen4IkeqB4b/5VV/ QmH/Wm0FQmSqcawq9G4riJbsPGm73YjmYfyQpELavf1+Sp5WvGZEBGtnoJKsbkflAcFm Er4J+38+3JUcQNpRSW3E/Z8htroQouIssP9+Kob9hz7RAHjpeke7RJBxlplcZpISHyML 2C91epABEvwST5Co9Azz5lsQ7Eq+WL3SG4+M7W3s0bWkZNyPEaeheFQoP/vKwlnvuXCz 5hZyb0yREa7+caL8kihNmYtbVD36jDB4HyuDUE+P12NB6NqrYNbCsj/c/vVpRPKz1abk x5OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705719057; x=1706323857; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=t+ncE/Ke8W/9eSicUFGfjAMk7XfHt6TzmaC+XB67O8c=; b=ZZRp2/WT6YOnN0To871NwYbkeM4yUoCGudlHKvVXkJ0Xd6j1w52/rSjV4MdbVXq1v2 x6jCVDrE3DMZDG2idoQdg5/6aFneQ9FK8aAvSqUMgqRtZXvJmoCCO7CKGTwc3GAv1kXN 187GOzjAQHjryPQgbn1y6DIeCm1+0aw9Q0vGZfFaIOTySNfbXNJMBUTBlaQtJMS+rhEs i86Ihc4d5OqWrgHvNQA3SLCE+D2PRtLacRX18vi5t8NI47HcjfSY3iY1OrPFIC4yeeze CoWIG/r3hDI7sFzSRL9COn0gTzY4xzltVGW9SWXSil132wRn686KabP4MHV6tkOOKEHI pwuw== X-Gm-Message-State: AOJu0Yx/KyjZ6AhURcef6L8JR8gCTaUkVKQfsOZhGvJwxdqK3Pz4RXfr zYGIyr2hb6qeIBSvU3c5KKzi66tId4asc758GP3WELMMkSf6y0LX X-Google-Smtp-Source: AGHT+IEBemjBN9YRLuTBhZtFCGVJr5mUhG1n1vsbhUWd+Ve5XrfNo27NIhoncvmYKmUNx9+bet0s7g== X-Received: by 2002:a81:7bc5:0:b0:5ff:9b17:8cd7 with SMTP id w188-20020a817bc5000000b005ff9b178cd7mr735066ywc.71.1705719057453; Fri, 19 Jan 2024 18:50:57 -0800 (PST) Received: from localhost ([2601:344:8301:57f0:2288:782e:a717:678d]) by smtp.gmail.com with ESMTPSA id s4-20020a0dd004000000b005fb0d1afc7csm7925684ywd.120.2024.01.19.18.50.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 18:50:56 -0800 (PST) From: Yury Norov To: Andrew Morton , Thomas Gleixner , Ming Lei , linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Breno Leitao , Nathan Chancellor , Rasmus Villemoes , Zi Yan Subject: [PATCH 1/9] cpumask: introduce for_each_cpu_and_from() Date: Fri, 19 Jan 2024 18:50:45 -0800 Message-Id: <20240120025053.684838-2-yury.norov@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240120025053.684838-1-yury.norov@gmail.com> References: <20240120025053.684838-1-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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Similarly to for_each_cpu_and(), introduce a for_each_cpu_and_from(), which is handy when it's needed to traverse 2 cpumasks or bitmaps, starting from a given position. Signed-off-by: Yury Norov --- include/linux/cpumask.h | 11 +++++++++++ include/linux/find.h | 3 +++ 2 files changed, 14 insertions(+) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index cfb545841a2c..73ff2e0ef090 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -332,6 +332,17 @@ unsigned int __pure cpumask_next_wrap(int n, const str= uct cpumask *mask, int sta #define for_each_cpu_and(cpu, mask1, mask2) \ for_each_and_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpu= mask_bits) =20 +/** + * for_each_cpu_and_from - iterate over every cpu in both masks starting f= rom a given cpu + * @cpu: the (optionally unsigned) integer iterator + * @mask1: the first cpumask pointer + * @mask2: the second cpumask pointer + * + * After the loop, cpu is >=3D nr_cpu_ids. + */ +#define for_each_cpu_and_from(cpu, mask1, mask2) \ + for_each_and_bit_from(cpu, cpumask_bits(mask1), cpumask_bits(mask2), smal= l_cpumask_bits) + /** * for_each_cpu_andnot - iterate over every cpu present in one mask, exclu= ding * those present in another. diff --git a/include/linux/find.h b/include/linux/find.h index 5e4f39ef2e72..dfd3d51ff590 100644 --- a/include/linux/find.h +++ b/include/linux/find.h @@ -563,6 +563,9 @@ unsigned long find_next_bit_le(const void *addr, unsign= ed (bit) =3D find_next_and_bit((addr1), (addr2), (size), (bit)), (bit) = < (size);\ (bit)++) =20 +#define for_each_and_bit_from(bit, addr1, addr2, size) \ + for (; (bit) =3D find_next_and_bit((addr1), (addr2), (size), (bit)), (bit= ) < (size); (bit)++) + #define for_each_andnot_bit(bit, addr1, addr2, size) \ for ((bit) =3D 0; \ (bit) =3D find_next_andnot_bit((addr1), (addr2), (size), (bit)), (bi= t) < (size);\ --=20 2.40.1 From nobody Wed Dec 17 06:28:14 2025 Received: from mail-yb1-f182.google.com (mail-yb1-f182.google.com [209.85.219.182]) (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 5FA021375 for ; Sat, 20 Jan 2024 02:51:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719061; cv=none; b=kDYzvkVHG4QYGzD07lIWRuIuIjKxamX2ULDOHwn7ugs57O7/52nnIzGzBTfLrzFY+IweBSzKNgm2tQYhtwoauDvE4Elknr0jpcuflznWpGuVPm4zy5pKDbzhwWFA4p+t24eisld6Gj7jRCHgFQxQZi2A5eQdaUiplh407NizjGA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719061; c=relaxed/simple; bh=FVVUMvujZhZjmYkcGH7AjE35UQGcTY7BYcUBQUAOERE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=C83gSIWzNX36YU+MfAEcuV/DH1vGbs86IQmEuObG8k+CnKQg0bFxB8iQhSdY4+Yu8pE4o2PitGj0zjxtwKUHwZLkStgw8khwXQZvNSOCEeYZCF8L1eWR6Ht4kib3w1rCvUdDQczClM51WxfCe7uQ6wBSt36QHuVgOwTh62Ql8CM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=H996nqN/; arc=none smtp.client-ip=209.85.219.182 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="H996nqN/" Received: by mail-yb1-f182.google.com with SMTP id 3f1490d57ef6-dc2501ed348so1046679276.0 for ; Fri, 19 Jan 2024 18:51:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705719059; x=1706323859; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BHRAsDAn8C4kdn9sH3kl576WyN+RLpoPnrMOrraWnqU=; b=H996nqN/BwKi5OJBP+83SlUaIecrSP5MoV2qn66jZq3TYwFyIt++IUoXtp45Md1ZFX W85P/cTsFsQK/GZQ4kt4ukseMdouGpqUhYSEqoxaj99LR5Pl2fQNHLz3y9c06lVYyYYe 1uBMs/f2BG5yzkJBu7Dr+3p5hJvN3m17ZyhjXLru4Fk+6fLt16NT8phwbyUlZKM6clqy lbjVFAiad9IuadFnjqFvzuV9JhcOggX+VuwJpfyrKv3MHI3H942UThbVyZ0xnGh6SKr9 bpiVRGVOsO3eGQBDq449aQXAt/tZrBmwwR2P3YQGgdr0sk0R4U8BGNP+hIMf/9pnesU+ nmnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705719059; x=1706323859; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BHRAsDAn8C4kdn9sH3kl576WyN+RLpoPnrMOrraWnqU=; b=UL/7VdS+lkY3r+av9eovVyN0UqTKgEIp94zbWsV6F9Pa4POzH3A+awRshoiqSF+m1W MMvoXvpV50E48BXPqOqTcl+3wfhmHIINmKoT4rcFH0Ml+rAF06kwDLLYo+mLwZGxo85o J8+O6eqBQBl0KCHyBThLY7HLKxU4ldKcEDZ1fW1hrMSg7FsaLBCG2z1PSwZOlVeOOEQe yIEK3Yi2ZgAzNvPChK7yyBCv7+mQlqU1j4/SOswxy5EQ98lmpDtLaXLuhsTfZ2kx0rEf 4O3ui55eFtTq5OqMQfZqY3b96vlx+hQBKsvCXar1MzQOnH111oQiDlTIimAu/Za9RNXo gXaw== X-Gm-Message-State: AOJu0YzwwmzRFhK1YnMF7wCURy7HrzABcFLpv+N4gqQH9jnb8HNVVb8C XF1L8bUK1d/Q44FZxcHJI/k53cS5I7NMlc2EtfG3OH06Y7Un1q7t X-Google-Smtp-Source: AGHT+IF0xskwrMmWrtjLD2ozmMbhaGVUw1PuxJ1m4BaGD7SOiNKnNBc/j4r35lnX/kDTDCHgA1D/Wg== X-Received: by 2002:a25:db87:0:b0:dbf:1913:388d with SMTP id g129-20020a25db87000000b00dbf1913388dmr1444200ybf.26.1705719059194; Fri, 19 Jan 2024 18:50:59 -0800 (PST) Received: from localhost ([2601:344:8301:57f0:2288:782e:a717:678d]) by smtp.gmail.com with ESMTPSA id v1-20020a25c501000000b00dc23a0382c6sm180986ybe.6.2024.01.19.18.50.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 18:50:58 -0800 (PST) From: Yury Norov To: Andrew Morton , Thomas Gleixner , Ming Lei , linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Breno Leitao , Nathan Chancellor , Rasmus Villemoes , Zi Yan Subject: [PATCH 2/9] lib/group_cpus: optimize inner loop in grp_spread_init_one() Date: Fri, 19 Jan 2024 18:50:46 -0800 Message-Id: <20240120025053.684838-3-yury.norov@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240120025053.684838-1-yury.norov@gmail.com> References: <20240120025053.684838-1-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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/group_cpus.c b/lib/group_cpus.c index ee272c4cefcc..063ed9ae1b8d 100644 --- a/lib/group_cpus.c +++ b/lib/group_cpus.c @@ -30,14 +30,14 @@ static void grp_spread_init_one(struct cpumask *irqmsk,= struct cpumask *nmsk, =20 /* If the cpu has siblings, use them first */ siblmsk =3D topology_sibling_cpumask(cpu); - for (sibl =3D -1; cpus_per_grp > 0; ) { - sibl =3D cpumask_next(sibl, siblmsk); - if (sibl >=3D nr_cpu_ids) - break; - if (!cpumask_test_and_clear_cpu(sibl, nmsk)) - continue; + sibl =3D cpu + 1; + + for_each_cpu_and_from(sibl, siblmsk, nmsk) { + if (cpus_per_grp-- =3D=3D 0) + return; + + cpumask_clear_cpu(sibl, nmsk); cpumask_set_cpu(sibl, irqmsk); - cpus_per_grp--; } } } --=20 2.40.1 From nobody Wed Dec 17 06:28:14 2025 Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) (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 3FA1020FA for ; Sat, 20 Jan 2024 02:51:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719063; cv=none; b=T281k4NrWBZCP8iFRW0hhw9EvQou0UlYIaEWkC1ZEY902PwMNQjp6bGn88Ldrd9jHQcWH4deaRAogC+wkIggNcqIwvwJJYe7hD0J8WkKJD3PxH88ypTrR6K5zbAPWwFjON38azox+l8RRpgIDmQJx7OP8nX804esofpZHsYcTPE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719063; c=relaxed/simple; bh=OMBbdYX8nOdWmRvXvgL49m4b2eYizvAW2/Ifazlfivo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=i3I+bM9dkeC1dLT4FHGNvPLqGqeTUhXruhzW7ysrk/AaoKJucUwSq9HTKACJxqYg3JLvQXwSr1RA/PqGlZGvCn3mXY/Qnz+zzppsCAGYpvGxcrU6P6t0fUcc7IsZDB7RHsfZW988hWFMtl8sjXO9ejLXZGLp+b0L3FD7Uim8QTw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=bXMMWiVM; arc=none smtp.client-ip=209.85.128.179 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bXMMWiVM" Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-5ffaabdcccbso4277367b3.0 for ; Fri, 19 Jan 2024 18:51:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705719061; x=1706323861; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NLcBksejFtB67CxPfS8EreSpNvKNv16Sj+tueH02QvE=; b=bXMMWiVM/zLsvQKhRVgfCi+oyjBFOnfnbJ1UVQraiOHWyfpbsb5UQ66s50gc7Mhudy Po/YLi0sMFbztX+VFA6guFg3MrsTQgCBiSEaE5lAAURL9H6ehM0rg6eoRY9lv0MaaNYp VCfiAHomXYB4GxRi9JVDI4gT5uPM/ROrLOLKTImtPXNHJEPcIbpLJhxwsjmuG5kizWgN /kV29pGRVRqiqxhkVA13f65neecDiyT7c6rgtnH2UfUl4Qa5v5ocRflV/lBa6k01I4IB 5UMTBCC7km/GzCuBckqQ9BUwoA9BZXc09DOXYBZy3SaLDC1zPvdTaY5YX01xpBcXSsLp 6/ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705719061; x=1706323861; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NLcBksejFtB67CxPfS8EreSpNvKNv16Sj+tueH02QvE=; b=ekEhCcEo1F7VpnPk1TjNSFDx+sdd8ImWyHY5N9ES06ClU8BK9XyOqGEcsn+L0GlY7a /ikm4sjUxkaRLZZJZ9IQ8MVvAUM3UFPxOnpejIav/Dj68Rp1gU/d6cR5tJTIZ34WGSM+ NfcjDexYAmTXpxRs1lWLMRKF2AVhqekeQ7L1/yo/6w/rt44xojDepDJH+B06LiiVCoFo sO5gjq7mKqiggZ/pVRWzyzaeIotlXRLcvuhXUXT2NiqOlbx+zuQChG3znxVeF0tlzfIl XdEo1yNqdivRNiinCz6AoIJP5CVYryQXQZdGDCDeZjPQisK00Z/LnGZUi/yvX3cBOlCD 6N1w== X-Gm-Message-State: AOJu0Yxc4bS/BFOlR7TjVcabTX3z54b6O4zZPRdhb8ELPPt1s+P7/FkE GheNthdvhPXhkm51tK6q6LrD1dQX8LY+6gHczHEp6jfGl8WafPy+ X-Google-Smtp-Source: AGHT+IHUfD1EUetixqI92Fcs1eodbLCLUE9NLpwn48zqo4+AwLgnBKW+VbglRqVVxjeVD36mN2k6Uw== X-Received: by 2002:a0d:e747:0:b0:5ff:9f17:654e with SMTP id q68-20020a0de747000000b005ff9f17654emr760006ywe.94.1705719060684; Fri, 19 Jan 2024 18:51:00 -0800 (PST) Received: from localhost ([2601:344:8301:57f0:2288:782e:a717:678d]) by smtp.gmail.com with ESMTPSA id r124-20020a819a82000000b005e92fd632e0sm7897419ywg.24.2024.01.19.18.50.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 18:50:59 -0800 (PST) From: Yury Norov To: Andrew Morton , Thomas Gleixner , Ming Lei , linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Breno Leitao , Nathan Chancellor , Rasmus Villemoes , Zi Yan Subject: [PATCH 3/9] lib/group_cpus: relax atomicity requirement in grp_spread_init_one() Date: Fri, 19 Jan 2024 18:50:47 -0800 Message-Id: <20240120025053.684838-4-yury.norov@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240120025053.684838-1-yury.norov@gmail.com> References: <20240120025053.684838-1-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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Because nmsk and irqmsk are stable, extra atomicity is not required. Signed-off-by: Yury Norov NAKed-by: Ming Lei --- Regarding the NAK discussion: > > > > I think this kind of change should be avoided, here the code is > > > > absolutely in slow path, and we care code cleanness and readabili= ty > > > > much more than the saved cycle from non atomicity. > > > > > > Atomic ops have special meaning and special function. This 'atomic'= way > > > of moving a bit from one bitmap to another looks completely non-tri= vial > > > and puzzling to me. > > > > > > A sequence of atomic ops is not atomic itself. Normally it's a sing= of > > > a bug. But in this case, both masks are stable, and we don't need > > > atomicity at all. > > > > Here we don't care the atomicity. > > > > > > > > It's not about performance, it's about readability. > > > > __cpumask_clear_cpu() and __cpumask_set_cpu() are more like private > > helper, and more hard to follow. > > No that's not true. Non-atomic version of the function is not a > private helper of course. > > > [@linux]$ git grep -n -w -E "cpumask_clear_cpu|cpumask_set_cpu" ./ | = wc > > 674 2055 53954 > > [@linux]$ git grep -n -w -E "__cpumask_clear_cpu|__cpumask_set_cpu" .= / | wc > > 21 74 1580 > > > > I don't object to comment the current usage, but NAK for this change. > > No problem, I'll add you NAK. =20 You can add the following words meantime: =20 __cpumask_clear_cpu() and __cpumask_set_cpu() are added in commit 6c8557b= db28d ("smp, cpumask: Use non-atomic cpumask_{set,clear}_cpu()") for fast code = path( smp_call_function_many()). =20 We have ~670 users of cpumask_clear_cpu & cpumask_set_cpu, lots of them fall into same category with group_cpus.c(doesn't care atomicity, not in = fast code path), and needn't change to __cpumask_clear_cpu() and __cpumask_set= _cpu(). Otherwise, this way may encourage to update others into the __cpumask_* v= ersion. lib/group_cpus.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/group_cpus.c b/lib/group_cpus.c index 063ed9ae1b8d..0a8ac7cb1a5d 100644 --- a/lib/group_cpus.c +++ b/lib/group_cpus.c @@ -24,8 +24,8 @@ static void grp_spread_init_one(struct cpumask *irqmsk, s= truct cpumask *nmsk, if (cpu >=3D nr_cpu_ids) return; =20 - cpumask_clear_cpu(cpu, nmsk); - cpumask_set_cpu(cpu, irqmsk); + __cpumask_clear_cpu(cpu, nmsk); + __cpumask_set_cpu(cpu, irqmsk); cpus_per_grp--; =20 /* If the cpu has siblings, use them first */ @@ -36,8 +36,8 @@ static void grp_spread_init_one(struct cpumask *irqmsk, s= truct cpumask *nmsk, if (cpus_per_grp-- =3D=3D 0) return; =20 - cpumask_clear_cpu(sibl, nmsk); - cpumask_set_cpu(sibl, irqmsk); + __cpumask_clear_cpu(sibl, nmsk); + __cpumask_set_cpu(sibl, irqmsk); } } } --=20 2.40.1 From nobody Wed Dec 17 06:28:14 2025 Received: from mail-yb1-f171.google.com (mail-yb1-f171.google.com [209.85.219.171]) (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 C118E23CB for ; Sat, 20 Jan 2024 02:51:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719064; cv=none; b=bsrpqbQLMtJYUXJw9HbNFoj3G8wPJmhPpUE8OSOXgUxno/Cp+VPs10KnArQ5KwLVTkme1slSvqpdJSsInp1S+jx/Frfgv3rNF0hnlJ2ltPLeiGFesTcnzW0HEVfPwL0+p8gFjeme8ERJZhsF8Nm0rT4nrIYLQyCANxDSL0kGYU0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719064; c=relaxed/simple; bh=GR+LGRPOvczJk2j19x5KNSDuIXLUu4YgzDfiDXEi9cc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T5SejcfEAklzsCdWJu3lX16T2ToRFK/h6fQQ6yJh30oSK4ePop5xCENk3dCj0hKlgt1s5uAFLjCYzrCABeBSsnuwK1jemf+qCN5eSOp6xvZIWTz6eYAleGY/ocse7KV0L96jvWT1SQzUO+VcnGSg64UPRShsEdMsb+jlD3kjP2s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=d9L37c1a; arc=none smtp.client-ip=209.85.219.171 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="d9L37c1a" Received: by mail-yb1-f171.google.com with SMTP id 3f1490d57ef6-db3fa47c2f7so1374564276.0 for ; Fri, 19 Jan 2024 18:51:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705719062; x=1706323862; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=I4yGOYCSVn8lyqdFOSiCVTXQVvYKRjg9XK/8kAi6PdU=; b=d9L37c1aIJGLDj+kvCHissK0/yu304CuV5dC/yrMl8V8T6STmfO0RczoljTXVJT8TO RohzmIt19T23RjqgIjLdqOF+QMDuBeWglMtsU/It8jp2L+zv5A+qJ61r4NS9o0Kp4qKI LOzWqoGZklx24wwO929/iBxfcr04wDEt0ObOt3KRJ115QPdzF2R3CMKw2JG26r3o6y89 +jH4fnE97q/Grh5X3G8spjKMRnm1FKr0yuW6ZYv7QxfFLmk3JWxqyO5nqv5tiw5m6hny KjqvLEGF0gcID9benrYNRydUgcpf0KM0NskjZeN7KEF5qd8obD1kYfro8c7adBu3x13x 6Ldw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705719062; x=1706323862; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=I4yGOYCSVn8lyqdFOSiCVTXQVvYKRjg9XK/8kAi6PdU=; b=QlsQi5dOx6cmyke4/hF2i7+XLjPVwlVXOFsrd+gnUwrOwHiIj2bbBLZXAA3RWKQNgp X8/9SYwzMUEI1kaNWkuzW4H9zfUbZYkuDLQcAfyPV1ZtpRGQ06BAlBVs48wv2P01r9dh q9TnfdQhmIE49KwIvLa42ZpD0vWQTvJ/R2d4AM5ZEZhyuFINzUfWSIcVAYRkumaDu/79 RQZuH0yYUTiGrC1dput2oI0M4dSshAf3inIndZKGrL/sVALn5xSIjAksIegAyihOP7WE U+TpA+BdXLoF8mJVSwX4QoawXRABD2ISFCpnexCs09daSoopMHkyKr0rwU9tWQLdYoxW Umuw== X-Gm-Message-State: AOJu0Yxtx3xA9+9esjqWkFw4SOMMEvw5fgHutBekIPU852I9CHS3jFX2 Ui6heGTJ+T5vqh2g658IsCRy0vzz9WTKEg6gtC9Y++OW5Al76NxZ X-Google-Smtp-Source: AGHT+IHtKrb3yU+scb81DPv7h/dROUP8AB720nTvll5J5n8vYbh0YXhMNXGk32S0K6Yvi/ymidAfkA== X-Received: by 2002:a81:4e48:0:b0:5fb:550c:9e7c with SMTP id c69-20020a814e48000000b005fb550c9e7cmr773237ywb.9.1705719061704; Fri, 19 Jan 2024 18:51:01 -0800 (PST) Received: from localhost ([2601:344:8301:57f0:2288:782e:a717:678d]) by smtp.gmail.com with ESMTPSA id m7-20020a817107000000b005ff8bcbe01dsm1557957ywc.16.2024.01.19.18.51.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 18:51:01 -0800 (PST) From: Yury Norov To: Andrew Morton , Thomas Gleixner , Ming Lei , linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Breno Leitao , Nathan Chancellor , Rasmus Villemoes , Zi Yan Subject: [PATCH 4/9] lib/group_cpus: optimize outer loop in grp_spread_init_one() Date: Fri, 19 Jan 2024 18:50:48 -0800 Message-Id: <20240120025053.684838-5-yury.norov@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240120025053.684838-1-yury.norov@gmail.com> References: <20240120025053.684838-1-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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Similarly to the inner loop, in the outer loop we can use for_each_cpu() macro, and skip CPUs that have been moved. With this patch, the function becomes O(1), despite that it's a double-loop. While here, add a comment why we can't merge outer logic into the inner loop. Signed-off-by: Yury Norov --- lib/group_cpus.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/group_cpus.c b/lib/group_cpus.c index 0a8ac7cb1a5d..952aac9eaa81 100644 --- a/lib/group_cpus.c +++ b/lib/group_cpus.c @@ -17,16 +17,17 @@ static void grp_spread_init_one(struct cpumask *irqmsk,= struct cpumask *nmsk, const struct cpumask *siblmsk; int cpu, sibl; =20 - for ( ; cpus_per_grp > 0; ) { - cpu =3D cpumask_first(nmsk); - - /* Should not happen, but I'm too lazy to think about it */ - if (cpu >=3D nr_cpu_ids) + for_each_cpu(cpu, nmsk) { + if (cpus_per_grp-- =3D=3D 0) return; =20 + /* + * If a caller wants to spread IRQa on offline CPUs, we need to + * take care of it explicitly because those offline CPUS are not + * included in siblings cpumask. + */ __cpumask_clear_cpu(cpu, nmsk); __cpumask_set_cpu(cpu, irqmsk); - cpus_per_grp--; =20 /* If the cpu has siblings, use them first */ siblmsk =3D topology_sibling_cpumask(cpu); @@ -38,6 +39,7 @@ static void grp_spread_init_one(struct cpumask *irqmsk, s= truct cpumask *nmsk, =20 __cpumask_clear_cpu(sibl, nmsk); __cpumask_set_cpu(sibl, irqmsk); + cpu =3D sibl + 1; } } } --=20 2.40.1 From nobody Wed Dec 17 06:28:14 2025 Received: from mail-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (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 3C5D44433 for ; Sat, 20 Jan 2024 02:51:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719065; cv=none; b=IbfYKpS/eamyfeM8OI4cYepUQPZ0VyAkulNJh4Xw4qQ4xikASBsDLhsjRDFd1uQBkxu+vNBUB922N2Wg+1EzGcNcqDsw2+xj2ALcXU1o/rEKdQsZHljLiSKhah6biRRzwekICTKP9G0WpJqxDYrgF+9UNtkvL0zjjlbRLcJVjgc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719065; c=relaxed/simple; bh=WqwHpejzHYLP9GNl7XmZ/L5b8WXSUZCU6xYAFELj57w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fpYxqk/x0ZLVRd1p9RK//ZRyirUp2bvNTUrwFN8YJk9skHu3q5ArL1E3whEUYxku+PfL8TBNUAfauzaLUex9Ife/OrGX+nvZe1addi+PgQIB6Zkz/R628eXkcehOt18EdcbDO6hDZ5zDkiOgGTqLV0wf2XmR/f2AEULZvfiGBBU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=bR7Fsljj; arc=none smtp.client-ip=209.85.128.178 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bR7Fsljj" Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-5f0c0ca5ef1so14586417b3.2 for ; Fri, 19 Jan 2024 18:51:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705719063; x=1706323863; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Fiqg7NMTo1hnms2N+QcuxP30iXk/HvqGHex9PC1KYAY=; b=bR7FsljjrtlFfdBUEufn68brwa6QZ2Cbjj0mM7BgpD++YIOOZAy3UEg7Mb/w+Bn7sW ABo6PH30H8eQfbKQYyfahgUXtAJjJTGPHig8uUVoV7xkW7zsMbEPlZrDTuKq1OYe9HNy /Yd0lTQ2/NuRu8/dcTMC7VSpYJr1r+33wGu3mGGc/qZFqOZ+Hyse2/8BL79z/SsCaxlK i7AcuWhzBh+ftZrqbqqwTAExCngt0YItF9haoOHxapuTIEPVmQGjDgBP4GXGpt15CMhZ sIMKzpqEZCfsJUGUYSetQ0bA49GFNLWP7xBVPzcs+vrgUxdM4wkyl9CuVQStxUllGq3U FqTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705719063; x=1706323863; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Fiqg7NMTo1hnms2N+QcuxP30iXk/HvqGHex9PC1KYAY=; b=AC4ManXu9+vNcpCZGRaqLSgbATXmmpySK0uR2oJ/XSw1k/dn2oeBkNQZ4nHYeIPeHX p0yRNMzqg/0KWHyd8/yBth8GcsgxrshbR39niaXGXEmuTv1ZH7xE83WBDayII6uPKbrz nb3slFNYJsi0omilEwTdwfiXEW2NvQR4E9t5HR/7lQJ+pthrmTFx+yXDzMYw790ffTOv x/p1AqpNfnU8Cyy4IStR4NL1sA61Kg7JtWJDyQ6DFfVEMJqGuxLBy5pvdLceXqdJ7eM/ uumldz/TIfQ/tYb+a1VVYpsaziuxTyW8mItmi9tzCW7WIyvDbQzOQ7hyYmCJW4Dy3jtC kpgQ== X-Gm-Message-State: AOJu0YwFkms9W0GIQhlERpfaTM0a+DhXpXcZywFbK3gkYAQ+hesVVBBn oWFI7dnj4jlWOEnIchL6/5lKAspUJZH9UFUZzo2iWu+0iNI0v4QG X-Google-Smtp-Source: AGHT+IFC2tSqmIiaWyrRaXDHNWjgaK4WhQ/sNI5EmVTZGnIlREFdxO1hY35x17WHyCYRLZbZOqcW0A== X-Received: by 2002:a0d:d4d2:0:b0:5ff:63e0:885b with SMTP id w201-20020a0dd4d2000000b005ff63e0885bmr787726ywd.76.1705719063162; Fri, 19 Jan 2024 18:51:03 -0800 (PST) Received: from localhost ([2601:344:8301:57f0:2288:782e:a717:678d]) by smtp.gmail.com with ESMTPSA id s204-20020a8177d5000000b005ff8c88bea0sm1537740ywc.14.2024.01.19.18.51.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 18:51:02 -0800 (PST) From: Yury Norov To: Andrew Morton , Thomas Gleixner , Ming Lei , linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Breno Leitao , Nathan Chancellor , Rasmus Villemoes , Zi Yan Subject: [PATCH 5/9] lib/group_cpus: don't zero cpumasks in group_cpus_evenly() on allocation Date: Fri, 19 Jan 2024 18:50:49 -0800 Message-Id: <20240120025053.684838-6-yury.norov@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240120025053.684838-1-yury.norov@gmail.com> References: <20240120025053.684838-1-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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" nmsk and npresmsk are both allocated with zalloc_cpumask_var(), but they are initialized by copying later in the code, and so may be allocated uninitialized. Signed-off-by: Yury Norov --- lib/group_cpus.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/group_cpus.c b/lib/group_cpus.c index 952aac9eaa81..72c308f8c322 100644 --- a/lib/group_cpus.c +++ b/lib/group_cpus.c @@ -354,10 +354,10 @@ struct cpumask *group_cpus_evenly(unsigned int numgrp= s) int ret =3D -ENOMEM; struct cpumask *masks =3D NULL; =20 - if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL)) + if (!alloc_cpumask_var(&nmsk, GFP_KERNEL)) return NULL; =20 - if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL)) + if (!alloc_cpumask_var(&npresmsk, GFP_KERNEL)) goto fail_nmsk; =20 node_to_cpumask =3D alloc_node_to_cpumask(); --=20 2.40.1 From nobody Wed Dec 17 06:28:14 2025 Received: from mail-yb1-f172.google.com (mail-yb1-f172.google.com [209.85.219.172]) (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 C3AA09454 for ; Sat, 20 Jan 2024 02:51:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719067; cv=none; b=JG3+l96/7AoPIMHJZ+hD/e925uRFC1zKwhUNOrst8MLZ4wMxpw/RaRqHN/UItQxQM2UKXkcLmDmqB8k9fMzJ3482gvWej6YCWu05nHzgDRqiJpjDTMHjk1gW4XQs/+trUDGz/xJzRF7YSqE2IQjrEv+hSnmiQ3QyHyuKBuy7C2I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719067; c=relaxed/simple; bh=J6OI4DVuVVTHIf5gTG9FN0R1OtqlAKVnLkI7p2AZ4ew=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OnwwCNIJt0pzqEMuZYiBUy7euYVmwl8ss+UDh+FoGn7Py4c3DIJnQOovBuYleQ5D6s5UJuAnweCJyaUZ3iS3HJaWVSh+OjveLpAtm8HJKxx2GqPSiwrEsQ+IiQ0pPtrWXqGegwNBslPrUai0wI5GeJdMqLD8rHS3iCZ52Z/IByo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=nlRAuSV5; arc=none smtp.client-ip=209.85.219.172 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nlRAuSV5" Received: by mail-yb1-f172.google.com with SMTP id 3f1490d57ef6-dc21d7a7042so1257591276.2 for ; Fri, 19 Jan 2024 18:51:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705719064; x=1706323864; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oeRpHv3Rhilh+EuNYr3bORiW/nxnS2Awjr3v2Rkry9o=; b=nlRAuSV5VCjXe4RQ3W1PRPchWoq3SzReL8nQdKnQkHNPHToC3opmAwGMhreMG37DaE mfnvFP2zdYDgsL+eBIBMeS56UBuCCU3Ts3KYPMRdclFmSk0bY8oMJN8blNsSNRcDo4he ajMH1CgBvYyFb5UGptzIJbWhV6q4hwTuu2nb5Mcl0JfUhTyBSoDPRKa5jO9ziPgtqD0d 1fp1uXf3Lms6g2ojI0ettynt4LV3zgJukWars1aWFZy9eT9k8f/a5S+o6EfJN1ycQaMc lStdfi1Le9VbLZwPCe68KxpI/MY47atnZuWOxaGvliGE705yrlcOjbz6qM4hl7zLb/Vp oenw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705719064; x=1706323864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oeRpHv3Rhilh+EuNYr3bORiW/nxnS2Awjr3v2Rkry9o=; b=AYg8lvvw9x4MW+odNdAwiEUMGp71xcVyAw359OEDf4eJWuk/uoHFlTPVWmh4UknnEc dYyCsVElf8fAK6R8P1dr2b5/emTBKC69tNby3cIV9pGp0WCzFYZh40FofGcH1G+AGFWt uqzG9p2kBxDLhFlZjWu1fDzoonVWjDvrMMPYsD3Ejg2ej8WCKf7gGcRPCTLiKb5l6I5V 8WqQ12KcsPL76U8TY94lhznEUGzbZR+i86522LBc2LkacVJS1Jt7832nv6Y8CuTotCig mdIE7VUv9Hl+5/ORMyBhX6BL2s0rIKfuSgkBIeeZyUG3nk7ztoghsrLRZ4Ch6sorBcJf izPw== X-Gm-Message-State: AOJu0YwY7/ffFeSUGPXoTkPqgVIojQnUS0WNas42UaY/U/F5OB2Wf0+x LH07Apgc94RF42n15wVGxazrlnbLSyWpX16Z4LNIL61/X1NaAbdWGlv+hP9/ReU= X-Google-Smtp-Source: AGHT+IEZWJeH36MIa/roOQ2t550jbQiJuOFbYxzCNAUvV1BHsnoWz3/Bqm2gWiDaY+11OqyCSXmrzA== X-Received: by 2002:a25:e015:0:b0:dbe:ab5b:c659 with SMTP id x21-20020a25e015000000b00dbeab5bc659mr758735ybg.37.1705719064630; Fri, 19 Jan 2024 18:51:04 -0800 (PST) Received: from localhost ([2601:344:8301:57f0:2288:782e:a717:678d]) by smtp.gmail.com with ESMTPSA id u16-20020a25ab10000000b00dc26a4ee68asm177391ybi.47.2024.01.19.18.51.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 18:51:03 -0800 (PST) From: Yury Norov To: Andrew Morton , Thomas Gleixner , Ming Lei , linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Breno Leitao , Nathan Chancellor , Rasmus Villemoes , Zi Yan Subject: [PATCH 6/9] lib/group_cpus: drop unneeded cpumask_empty() call in __group_cpus_evenly() Date: Fri, 19 Jan 2024 18:50:50 -0800 Message-Id: <20240120025053.684838-7-yury.norov@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240120025053.684838-1-yury.norov@gmail.com> References: <20240120025053.684838-1-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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The function is called twice. First time it's called with cpumask_present as a parameter, which can't be empty. Second time it's called with a mask created with cpumask_andnot(), which returns false if the result is an empty mask. We can safely drop redundant cpumask_empty() call from the __group_cpus_evenly() and save few cycles. Signed-off-by: Yury Norov Reviewed-by: Ming Lei --- lib/group_cpus.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/group_cpus.c b/lib/group_cpus.c index 72c308f8c322..b8c0c3ae2bbd 100644 --- a/lib/group_cpus.c +++ b/lib/group_cpus.c @@ -259,9 +259,6 @@ static int __group_cpus_evenly(unsigned int startgrp, u= nsigned int numgrps, nodemask_t nodemsk =3D NODE_MASK_NONE; struct node_groups *node_groups; =20 - if (cpumask_empty(cpu_mask)) - return 0; - nodes =3D get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk); =20 /* @@ -401,9 +398,14 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps) curgrp =3D 0; else curgrp =3D nr_present; - cpumask_andnot(npresmsk, cpu_possible_mask, npresmsk); - ret =3D __group_cpus_evenly(curgrp, numgrps, node_to_cpumask, - npresmsk, nmsk, masks); + + if (cpumask_andnot(npresmsk, cpu_possible_mask, npresmsk)) + /* If npresmsk is not empty */ + ret =3D __group_cpus_evenly(curgrp, numgrps, node_to_cpumask, + npresmsk, nmsk, masks); + else + ret =3D 0; + if (ret >=3D 0) nr_others =3D ret; =20 --=20 2.40.1 From nobody Wed Dec 17 06:28:14 2025 Received: from mail-yb1-f171.google.com (mail-yb1-f171.google.com [209.85.219.171]) (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 32FAFD2FA for ; Sat, 20 Jan 2024 02:51:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719068; cv=none; b=Wj4WJDhtv719NOfPfwkr2epZujBkf9XUOIyKa79QihzSLJ7zducK8Etx4gxxKCEUNMOYiUpLB/T+qryX8+tsPAIGg1DkJFZJwSCY3urU+8d/Ds7yIK6i3b4V3P07RZB0yuZo8mv0/6t9ZIErzVVLqRlnbMsfJNcbaiGHPAhMOcs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719068; c=relaxed/simple; bh=bfKkfZdK+j5iNHBICR9hXv9ldRAf+PdIDkkPbpbg0as=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=D45D+dGltoU82AFHGLFZ1HOuv+fDhfGQSFgON0Ri0X8bnjL2Aujvu8CJX0c8YOX2FdcZ9wJp9HgnODTPpDq3eHw1+hq14fs5l+T/JVVsGBjgVaO9AO2zitnsZvSPL67u+3ey8UJ62qfI766IDuackI+aW6lB7H5u1qRR1Ll2B4Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gLvbyuHt; arc=none smtp.client-ip=209.85.219.171 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gLvbyuHt" Received: by mail-yb1-f171.google.com with SMTP id 3f1490d57ef6-dbed0710c74so1360375276.1 for ; Fri, 19 Jan 2024 18:51:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705719066; x=1706323866; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=khrCIVPglrVXE8B6IKw0gUJXYk/zQUc0AIxevIyXOcA=; b=gLvbyuHtYu5VEXLdBUGGHjGXAIvDDfz4RTzy9tu+03I6BVnr2q+DMrpgF3xPKlV+BH +lWvWqoA4K3hbc6FHANmTqqWjEP97WwcFKsoHAuzNKUy1PjPeWACVSPOIPDKK6tU2Sk7 Nh/1Hxa2wAzt1Vxkd7IU+xEQCxwOGhmyoaYgTktYgT6ADo6anzGD/XH++vSsPKVYi8J1 EVoxhiOEk3Vet/ynJzkPsKrMHgsC8r7vXilIpxtEWKFcNl/a57z9QTqe573m0J2vk5EC juprm3LqIShVtboKF7ErW5uskqp0em6550CpzFeeIz9nALqmAtCCc9rTsRFz5SLuRvDW l2aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705719066; x=1706323866; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=khrCIVPglrVXE8B6IKw0gUJXYk/zQUc0AIxevIyXOcA=; b=PdpYdhxwydVelCMGV0qS0tRKB0NEOjyvYN+6xYNHdh+PCb2LXpJFXLFJK64KC3Lcv1 JwdpK25R6WCpmYu/KZvGTZlSLtYqqRvn7Tj7FSuf9WxGWXiu9UE8hchHku2Lkpi4olaZ ntgeIeXhkfvWULy88fU0tRcRxQq0WtRwgD6Pu1CtIHdBHBtgamrevlKKklbdvt7UdpB4 ve9xSAvkbx6nIhIY2JYtnOIRzVdre9X3Gpc9fGR91RlpwjlFtcB4L3nKxblKFlvLJar0 ieqxCVPuxBWj0q/JxQReEos7LAP8acSRjKov/qmB7nvEu7sHKOweTB70uurRdqhgBPFL /u4w== X-Gm-Message-State: AOJu0Yxbh9HwxfX8rqpoGZwm9FTSPpzPxlMOOXL3kHjQ/VpeGPV26m69 XoMf8uSjYryLAqABraTSXEN9QyIEct9lFzQ1yM7FMOMMw7mdB+y2 X-Google-Smtp-Source: AGHT+IGJAk2hlN5+5Sn7hGHowuv9qXVJkR2kSnp4V7X6LefuFI94JPINworViKWnbNzJ1lazqHyT7g== X-Received: by 2002:a25:9243:0:b0:dc2:2327:fcff with SMTP id e3-20020a259243000000b00dc22327fcffmr806679ybo.29.1705719066184; Fri, 19 Jan 2024 18:51:06 -0800 (PST) Received: from localhost ([2601:344:8301:57f0:2288:782e:a717:678d]) by smtp.gmail.com with ESMTPSA id k129-20020a256f87000000b00dc24ed83a6csm174854ybc.62.2024.01.19.18.51.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 18:51:05 -0800 (PST) From: Yury Norov To: Andrew Morton , Thomas Gleixner , Ming Lei , linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Breno Leitao , Nathan Chancellor , Rasmus Villemoes , Zi Yan Subject: [PATCH 7/9] cpumask: define cleanup function for cpumasks Date: Fri, 19 Jan 2024 18:50:51 -0800 Message-Id: <20240120025053.684838-8-yury.norov@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240120025053.684838-1-yury.norov@gmail.com> References: <20240120025053.684838-1-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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now we can simplify a code that allocates cpumasks for local needs. Automatic variables have to be initialized at declaration, or at least before any possibility for the logic to return, so that compiler wouldn't try to call an associate destructor function on a random stack number. Because cpumask_var_t, depending on the CPUMASK_OFFSTACK config, is either a pointer or an array, we have to have a macro for initialization. So define a CPUMASK_NULL macro, which allows to init struct cpumask pointer with NULL when CPUMASK_OFFSTACK is enabled, and effectively a no-op when CPUMASK_OFFSTACK is disabled. Signed-off-by: Yury Norov --- include/linux/cpumask.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index 73ff2e0ef090..0dd8e810200f 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -7,6 +7,7 @@ * set of CPUs in a system, one bit position per CPU number. In general, * only nr_cpu_ids (<=3D NR_CPUS) bits are valid. */ +#include #include #include #include @@ -898,6 +899,7 @@ typedef struct cpumask *cpumask_var_t; =20 #define this_cpu_cpumask_var_ptr(x) this_cpu_read(x) #define __cpumask_var_read_mostly __read_mostly +#define CPUMASK_NULL NULL =20 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node); =20 @@ -945,6 +947,7 @@ typedef struct cpumask cpumask_var_t[1]; =20 #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x) #define __cpumask_var_read_mostly +#define CPUMASK_NULL {} =20 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) { @@ -988,6 +991,8 @@ static inline bool cpumask_available(cpumask_var_t mask) } #endif /* CONFIG_CPUMASK_OFFSTACK */ =20 +DEFINE_FREE(free_cpumask_var, struct cpumask *, if (_T) free_cpumask_var(_= T)); + /* It's common to want to use cpu_all_mask in struct member initializers, * so it has to refer to an address rather than a pointer. */ extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS); --=20 2.40.1 From nobody Wed Dec 17 06:28:14 2025 Received: from mail-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (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 3CD46DDBC for ; Sat, 20 Jan 2024 02:51:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719069; cv=none; b=hyumcpfLMEifibOqBddMvrdE5fLrqWtz9d6nSEbPJYLkt9Q2GIroCmV8EyQkKup3seJ6vxmNQ3xxcHYyixXxa7FAeloYP7VFUXL9Rj74nNB4fWcFuDvV40f6U3dYXNMZd2TqHpqK/UxMDScyV+GGpHPr3AzTzuxpa/VB3jgxupo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719069; c=relaxed/simple; bh=g4Q/v+BsQEl39aheDnpAa2RdKnHhflblAdg2pabSwXk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=W/Qv5RK+I4jnbl43xkx1SzbaOneNAaTuYiEdml2cEePb+kOhrq2Q+7CmKFlcKLsA2InWvFhbBbzzi3v63nxXp+ZgyywvcCNSK7dYBr3aDUsEsE5QPoClCBU8MfB7PFIk5QiUD7lZAHvkDzCXKfedZrTQAKlFqQNuxv17IbNIKbc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=GQFBWkHG; arc=none smtp.client-ip=209.85.128.178 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GQFBWkHG" Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-5f0629e67f4so14828157b3.3 for ; Fri, 19 Jan 2024 18:51:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705719067; x=1706323867; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SK3MSQRvDzIpKb0PB7VHqnVj86mu8pY62lcMb7kaIwo=; b=GQFBWkHG27Zh41Z4Aecxi2ktaH5NuzK4VanYBIVwEHbsC/JwS9D48nU3WG2kw4s1wZ hfwqvI9Szud0Sxr27AEA9ML4Wi4petizqqfEzUGW3zyrCLQ018s94ZGBCPhMr5tlVP5Y F6+GgkTBKsKRGg8H6jcKvstn3TAzUR2TTFnfwKn3RUg+iEDZDSTs6+YsSRNsigFSnUDQ ZD5/3WM8aXmvXNK9VxitEOkM68xZBBW3RgLSyBo2xZup2bl8R59o3tObRp3ulvE3tar8 BVA9V7/k5J10Kui7hpL/r7bmp9r4Fr4YOM1sn+NzO3jIOicmkiC+HKTJ2ezvOQ+dxD8h eRYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705719067; x=1706323867; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SK3MSQRvDzIpKb0PB7VHqnVj86mu8pY62lcMb7kaIwo=; b=TnOgrPLWvC8dpbvwBp4FiBCqce8S/TooqTY2gNe1bwEZO0rLGtGxk5sDp5NGfqZuLC 7ahUiisfFIW9ZXMKH4I2Rq6Yfl8gGUzyXWpO1OmxnCQYrCQGLYnGOz2ZMOGh1h5h39DC 5q5lwPL9EPQcKb18sWzA172vbkn4SoR2wgzShV++DzEq7uP/JgU8CwAyhhGiO2K3Oxey oeJZrNeW6Rbs8ABnI+tBN1zhaWzCoCz6jwmGBYaO8fzhqqpdkhaX9NmIzlOQ3tIf2kLC 6/tSYOVR4/SJTXruKVgmR3sEDF46l1xAniauOT3b6lY/kwpeEPCgNZhKV4YCuFAG0pax xCjA== X-Gm-Message-State: AOJu0Yz5HC/HlShwXp7He67BQG4CLHJgNr0I4Q2AXupPgmd5VOjQIwYT CbK+nItoZ6Hzs7D4lkczInooFo4U0bPOxcSVHomlmiQDUmxkNauM X-Google-Smtp-Source: AGHT+IEwbvhpM0763THgchq4r36P/ykWzE2VFrUoLccf/V/oLviZzmPeNmVu64oa+SGSzgCqwCIfXw== X-Received: by 2002:a0d:e811:0:b0:5fa:ea54:f413 with SMTP id r17-20020a0de811000000b005faea54f413mr796695ywe.1.1705719067174; Fri, 19 Jan 2024 18:51:07 -0800 (PST) Received: from localhost ([2601:344:8301:57f0:2288:782e:a717:678d]) by smtp.gmail.com with ESMTPSA id r74-20020a819a4d000000b005ff8179bbfesm1926501ywg.104.2024.01.19.18.51.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 18:51:06 -0800 (PST) From: Yury Norov To: Andrew Morton , Thomas Gleixner , Ming Lei , linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Breno Leitao , Nathan Chancellor , Rasmus Villemoes , Zi Yan Subject: [PATCH 8/9] lib/group_cpus: rework group_cpus_evenly() Date: Fri, 19 Jan 2024 18:50:52 -0800 Message-Id: <20240120025053.684838-9-yury.norov@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240120025053.684838-1-yury.norov@gmail.com> References: <20240120025053.684838-1-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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Leverage cleanup machinery and drop most of housekeeping code. Particularly, drop unneeded and erroneously initialized with -ENOMEM variable ret. Signed-off-by: Yury Norov --- lib/group_cpus.c | 79 +++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 54 deletions(-) diff --git a/lib/group_cpus.c b/lib/group_cpus.c index b8c0c3ae2bbd..4c09df9eb886 100644 --- a/lib/group_cpus.c +++ b/lib/group_cpus.c @@ -76,6 +76,8 @@ static void free_node_to_cpumask(cpumask_var_t *masks) kfree(masks); } =20 +DEFINE_FREE(free_node_to_cpumask, cpumask_var_t *, if (_T) free_node_to_cp= umask(_T)); + static void build_node_to_cpumask(cpumask_var_t *masks) { int cpu; @@ -345,26 +347,16 @@ static int __group_cpus_evenly(unsigned int startgrp,= unsigned int numgrps, */ struct cpumask *group_cpus_evenly(unsigned int numgrps) { - unsigned int curgrp =3D 0, nr_present =3D 0, nr_others =3D 0; - cpumask_var_t *node_to_cpumask; - cpumask_var_t nmsk, npresmsk; - int ret =3D -ENOMEM; - struct cpumask *masks =3D NULL; - - if (!alloc_cpumask_var(&nmsk, GFP_KERNEL)) + cpumask_var_t *node_to_cpumask __free(free_node_to_cpumask) =3D alloc_nod= e_to_cpumask(); + struct cpumask *masks __free(kfree) =3D kcalloc(numgrps, sizeof(*masks), = GFP_KERNEL); + cpumask_var_t npresmsk __free(free_cpumask_var) =3D CPUMASK_NULL; + cpumask_var_t nmsk __free(free_cpumask_var) =3D CPUMASK_NULL; + int curgrp, nr_present, nr_others; + + if (!masks || !node_to_cpumask || !alloc_cpumask_var(&nmsk, GFP_KERNEL) + || !alloc_cpumask_var(&npresmsk, GFP_KERNEL)) return NULL; =20 - if (!alloc_cpumask_var(&npresmsk, GFP_KERNEL)) - goto fail_nmsk; - - node_to_cpumask =3D alloc_node_to_cpumask(); - if (!node_to_cpumask) - goto fail_npresmsk; - - masks =3D kcalloc(numgrps, sizeof(*masks), GFP_KERNEL); - if (!masks) - goto fail_node_to_cpumask; - build_node_to_cpumask(node_to_cpumask); =20 /* @@ -382,11 +374,15 @@ struct cpumask *group_cpus_evenly(unsigned int numgrp= s) cpumask_copy(npresmsk, data_race(cpu_present_mask)); =20 /* grouping present CPUs first */ - ret =3D __group_cpus_evenly(curgrp, numgrps, node_to_cpumask, - npresmsk, nmsk, masks); - if (ret < 0) - goto fail_build_affinity; - nr_present =3D ret; + nr_present =3D __group_cpus_evenly(0, numgrps, node_to_cpumask, npresmsk,= nmsk, masks); + if (nr_present < 0) + return NULL; + + /* If npresmsk is empty */ + if (!cpumask_andnot(npresmsk, cpu_possible_mask, npresmsk)) + return_ptr(masks); + + curgrp =3D nr_present < numgrps ? nr_present : 0; =20 /* * Allocate non present CPUs starting from the next group to be @@ -394,38 +390,13 @@ struct cpumask *group_cpus_evenly(unsigned int numgrp= s) * group space, assign the non present CPUs to the already * allocated out groups. */ - if (nr_present >=3D numgrps) - curgrp =3D 0; - else - curgrp =3D nr_present; - - if (cpumask_andnot(npresmsk, cpu_possible_mask, npresmsk)) - /* If npresmsk is not empty */ - ret =3D __group_cpus_evenly(curgrp, numgrps, node_to_cpumask, - npresmsk, nmsk, masks); - else - ret =3D 0; - - if (ret >=3D 0) - nr_others =3D ret; - - fail_build_affinity: - if (ret >=3D 0) - WARN_ON(nr_present + nr_others < numgrps); - - fail_node_to_cpumask: - free_node_to_cpumask(node_to_cpumask); - - fail_npresmsk: - free_cpumask_var(npresmsk); - - fail_nmsk: - free_cpumask_var(nmsk); - if (ret < 0) { - kfree(masks); + nr_others =3D __group_cpus_evenly(curgrp, numgrps, node_to_cpumask, + npresmsk, nmsk, masks); + if (nr_others < 0) return NULL; - } - return masks; + + WARN_ON(nr_present + nr_others < numgrps); + return_ptr(masks); } #else /* CONFIG_SMP */ struct cpumask *group_cpus_evenly(unsigned int numgrps) --=20 2.40.1 From nobody Wed Dec 17 06:28:14 2025 Received: from mail-yb1-f175.google.com (mail-yb1-f175.google.com [209.85.219.175]) (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 D0DFBEAF1 for ; Sat, 20 Jan 2024 02:51:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719071; cv=none; b=AYFvBZTtVBeNTOT3YI6VN+42rTJMIyrzIVEOQqW6oO1s8tEnquBJ/C4CpRqNeqIAgo4OP7st60D9dE9KaveCgC2810d0F4IyYSIByTcmnSIcR0ENABgIFO2yrxNv9CwJN4XzYTHgGCLsLSb0RDMK2KeJtiiDT9SjYW3qXeyIS4A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705719071; c=relaxed/simple; bh=UIQYKYf6qv+31oI5sMl8bv/xnQG3+D90nWie+7vmd3A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VykBEM68inBEg41/JvTH8FNrK/lYqX46V8VYcPJ30/GzdzIdOA46IdapMKGBo6M5Dmm/+gNmWdxIcbUxLElNQHZJc3ImHflFORLG0xjKgNeGw9/o9vtfBqtJjOuHfORgxtF27kCcrz9YQYhetAC+CbXw08LvfDvaLf6zg5JfpLs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=U/0q5VZ/; arc=none smtp.client-ip=209.85.219.175 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="U/0q5VZ/" Received: by mail-yb1-f175.google.com with SMTP id 3f1490d57ef6-dc223f3dd5eso1237950276.2 for ; Fri, 19 Jan 2024 18:51:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705719068; x=1706323868; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HHN46ZnhVRtGaHejqRmQyDg/24XoRBDottjOY+gFecc=; b=U/0q5VZ/ktJTriBXLaV5A6vyIl1cqBNY9Nt3bCcjafPH+WB/x2nFmanFi8yC9bsRET Mhdgj496ZZV9lTKOdqHJEOvIQ6FgYW7MVlzxru3T7fRJIHQkYd++kO/wt9HZgsyQkTVs CLvKZqflnEdEFbZTrwYfyyh1qf+HOgqvH3AdE9tg0lTvgoHCfzQfINod23wBLpcLRUHr GG+lrPmILnLtKNkqGgsXkmR3lU1veQ+EWCNhgVKt7Edw94ujieR+7pHP0AGOCHDWWxbt J6rfZkJEbgA5gPsc7s4mlQqnktrGnIkvVK2mGc2PnhUqAmrP6saO+PxWs0l6T5LYBJTJ KClQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705719068; x=1706323868; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HHN46ZnhVRtGaHejqRmQyDg/24XoRBDottjOY+gFecc=; b=u+wgAnPncYjBqBhFUjs6A4oreUuVOhbP4Kw4bTqGyeKiAw+Yvz2ofBJ8h5FjDwtSoj wd/RGlgVfWGxODDgVpokX1ZjteF77IWnJGQICi9fX9JQxtO7g3Phg+KAlwttq59jI5qn AzQAtTJLLTmf6uCrWLEB7yqFx6K86ugriaAgrxx8Z1UK6m+ZqHDoHGvdBYH6wzE9VOcp JwXajVoKmRawZ6V4/9J+s3P6N9/YuPQ8xGA3+KjgoHWMhETNO21djlxwdm0H7Zrd18W7 5AEXkEFkthEt7dj5mfdD8s5FALsVEeM6TF+S/Ekacy0a6TJuF9o3iKGBDRL09u7cL1Bs rhBw== X-Gm-Message-State: AOJu0YyU3dZp6270ps1J9k4qe6LglxyYkKIzx9NNtiLK+6kimN3InUlo 1nk6Gd3vmyQSsryYaaRluhNqk89nx8ONpVUOwDEXj2mZyMAADXc1 X-Google-Smtp-Source: AGHT+IHCZLAVrRY2oNLTx0e7hzc3AHDIllg73ghGrBa88/4cdw1RA9LuKkpABJdDuzgxVwF1pjClBA== X-Received: by 2002:a25:e081:0:b0:db5:3e3b:d2bb with SMTP id x123-20020a25e081000000b00db53e3bd2bbmr907407ybg.48.1705719068318; Fri, 19 Jan 2024 18:51:08 -0800 (PST) Received: from localhost ([2601:344:8301:57f0:2288:782e:a717:678d]) by smtp.gmail.com with ESMTPSA id s65-20020a25c244000000b00dc2310abe8bsm179790ybf.38.2024.01.19.18.51.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 18:51:07 -0800 (PST) From: Yury Norov To: Andrew Morton , Thomas Gleixner , Ming Lei , linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Breno Leitao , Nathan Chancellor , Rasmus Villemoes , Zi Yan Subject: [PATCH 9/9] lib/group_cpus: simplify group_cpus_evenly() for more Date: Fri, 19 Jan 2024 18:50:53 -0800 Message-Id: <20240120025053.684838-10-yury.norov@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240120025053.684838-1-yury.norov@gmail.com> References: <20240120025053.684838-1-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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The nmsk parameter is used only in helper function, so move it there. Suggested-by: Ming Lei Signed-off-by: Yury Norov --- lib/group_cpus.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/group_cpus.c b/lib/group_cpus.c index 4c09df9eb886..71e802fca35f 100644 --- a/lib/group_cpus.c +++ b/lib/group_cpus.c @@ -253,13 +253,17 @@ static void alloc_nodes_groups(unsigned int numgrps, static int __group_cpus_evenly(unsigned int startgrp, unsigned int numgrps, cpumask_var_t *node_to_cpumask, const struct cpumask *cpu_mask, - struct cpumask *nmsk, struct cpumask *masks) + struct cpumask *masks) { unsigned int i, n, nodes, cpus_per_grp, extra_grps, done =3D 0; unsigned int last_grp =3D numgrps; unsigned int curgrp =3D startgrp; nodemask_t nodemsk =3D NODE_MASK_NONE; struct node_groups *node_groups; + cpumask_var_t nmsk __free(free_cpumask_var) =3D CPUMASK_NULL; + + if (!alloc_cpumask_var(&nmsk, GFP_KERNEL)) + return -ENOMEM; =20 nodes =3D get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk); =20 @@ -350,11 +354,9 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps) cpumask_var_t *node_to_cpumask __free(free_node_to_cpumask) =3D alloc_nod= e_to_cpumask(); struct cpumask *masks __free(kfree) =3D kcalloc(numgrps, sizeof(*masks), = GFP_KERNEL); cpumask_var_t npresmsk __free(free_cpumask_var) =3D CPUMASK_NULL; - cpumask_var_t nmsk __free(free_cpumask_var) =3D CPUMASK_NULL; int curgrp, nr_present, nr_others; =20 - if (!masks || !node_to_cpumask || !alloc_cpumask_var(&nmsk, GFP_KERNEL) - || !alloc_cpumask_var(&npresmsk, GFP_KERNEL)) + if (!masks || !node_to_cpumask || !alloc_cpumask_var(&npresmsk, GFP_KERNE= L)) return NULL; =20 build_node_to_cpumask(node_to_cpumask); @@ -374,7 +376,7 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps) cpumask_copy(npresmsk, data_race(cpu_present_mask)); =20 /* grouping present CPUs first */ - nr_present =3D __group_cpus_evenly(0, numgrps, node_to_cpumask, npresmsk,= nmsk, masks); + nr_present =3D __group_cpus_evenly(0, numgrps, node_to_cpumask, npresmsk,= masks); if (nr_present < 0) return NULL; =20 @@ -390,8 +392,7 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps) * group space, assign the non present CPUs to the already * allocated out groups. */ - nr_others =3D __group_cpus_evenly(curgrp, numgrps, node_to_cpumask, - npresmsk, nmsk, masks); + nr_others =3D __group_cpus_evenly(curgrp, numgrps, node_to_cpumask, npres= msk, masks); if (nr_others < 0) return NULL; =20 --=20 2.40.1