From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 B6A17368968 for ; Wed, 15 Jul 2026 13:54:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784123664; cv=none; b=mETtLj2xb+iRugsHWMJ9gQjgIX884bSIOW4VlasyOwp5eHFcsD2lVwtgBaFT/X4D09Z0lHDPOHB3CppBJDoLRXWa5KnGyp7Mwsw5mgcIrBRWZHu/OyODssPi/iCVVodITJefWDBi1a363pLGLNlxXduV9D78Lsf0NYeK4qSAisI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784123664; c=relaxed/simple; bh=s5hb/Kw3rcAoFxSlcuTzTL76KGG32mmLGjRIuJTzZwA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=d4134mXmQMTKvN5cXD6wsttcxjt3H9lReGDImnV7M25e2eJSA+w5XVdVuI8+RcixzKeDz+4YVCKd4ndKl6POFRC9tYmi62gx78oKJ/RULwJtHVM3EkkKX75Ymj21CIpgk4AOPmtJ06GpvuWLYUfad/d00pp9tt+UmykUz+W6fY0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org; spf=pass smtp.mailfrom=cmpxchg.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.b=sxRYZZDz; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.b="sxRYZZDz" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-493c19bad03so49034775e9.2 for ; Wed, 15 Jul 2026 06:54:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg.org; s=google; t=1784123660; x=1784728460; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=5CPsfCbOCYpsNTxRu7/z3HEhqfynKIHhSGjsTmZ1QwE=; b=sxRYZZDz2Qz9PkYwHhJYG/lyt2gW4B1JM3FkvPiLc2tIv3zUPWLDCyur/v0JgXrTWx BICjNqSJX1xHRm7uA7hcNlYvKzp7cR31Av3iP3+3USMoK0Ktc5RMs87z66CiVi4nskAz aIxLboMj/3Zz1xBFKoTAhBfr6ZIK3vyhq2ft7RJFVW1jxAlyaJO3yuuxcRuxNikl/jZO E4b+2jr3aFSN4bwO2qAO02HQiLe9Dm698/gu8a47+lPr4ySb8IcbBaXWDl+dFOTjV9Fv 1PYRmqp5gJxLtvfUiJPfYnpIfEv0dwvmqoYCzxZG2R8/OvnlDLn/Cb85GyGv06GLSg8J uv9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784123660; x=1784728460; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=5CPsfCbOCYpsNTxRu7/z3HEhqfynKIHhSGjsTmZ1QwE=; b=GRuOSI8lIQjZy2JoMWO4VGeTdyCBNcGu0/b9DWG2Zo2KbT5I9uckMen0NGWcCrb/z3 V7slUKmnObK6IHb2g48+pLS6ILXDyi59LItztE9+fGlb6GANqAzuBC1zd9upZlLC4xKp w9Ya6dJVqv9TQ+OfcjoN6qnYG3MNGxRprIAz7+qrHl1TEppn08CRi4RnVV291KtaPRXu CpSZqmYe9jqsMyXauDKoUrnHPmRwBSK68m0r+zbgvU0oqkN2mdwn/ZUYMjqi9JbUr1p0 65wy10yjFQoyPh6fFUBSH5090vo85/krrvVn1a5rMNaHRFPeMNut/krflzXMP5zOhmVM afiA== X-Forwarded-Encrypted: i=1; AHgh+RoEsgiHOEcXRpqNhnNz630bT7cxCobKzv2zvkF0Hg2Ygi1ZoZyNfb5anuH5HGB+qLXeRnQ29Cw/bgY04ic=@vger.kernel.org X-Gm-Message-State: AOJu0YxCXCE3P7EsXrct0BAdAdRhGqgG+NCpK1oGsGpqW8q8MElpbp/N V5BNIkFJpNOlv/IsbxwKqfI06lCavJXVbxslnyDOooaGBjK1K6b0fniR8qbeDSEQxes= X-Gm-Gg: AfdE7cltKCJ6xmU5N8+5/zNsHmrNYKO+WrqEJjQRp+8wrIbi4TB3HQsb6pZ98jkTsIr uLr8C+2sQfMG0HNg11PnwbJCiT1aAS8OQVk6cbJMxyy5IpnPU4hC8LMT+wbj7j4l3M6edM47jVy eDfdOQowEO+bHcPOy6pVmE35nIOiqsWNzKOBSj1mA7ocip/AgFKsezSSZ7sPX7E5inDv2mFNPHy aY3E1ed10QRG+euZwA2r7EjrBLR8fYzXnK18NpOceI2DZySxd4VM8dN0Aq4nWQMylL53oUPaoDv XQGAc4mOSWL4MbAiUmAL68IUwfWQ8zEI5obRsCqAJ/MSGuXfhogBwTYmamtekx73K/lj22gxKl/ inYmB6jg8qWq24Zr7V5mRuGDqrcWXvbXiYZ2sSuAKz9+S1mt9a63M1Opom/sHTHh0tNEHiSVznY h4U2J3WfDQp+Vsow== X-Received: by 2002:a05:600c:a4a:b0:495:3e81:770f with SMTP id 5b1f17b1804b1-4953e817874mr21024615e9.15.1784123659544; Wed, 15 Jul 2026 06:54:19 -0700 (PDT) Received: from localhost ([2a02:8071:8280:d6e0:1353:8eb8:c84a:b6d4]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4950a322c86sm142853985e9.11.2026.07.15.06.54.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jul 2026 06:54:18 -0700 (PDT) Date: Wed, 15 Jul 2026 15:54:12 +0200 From: Johannes Weiner To: Usama Arif Cc: bsegall@google.com, dietmar.eggemann@arm.com, juri.lelli@redhat.com, kprateek.nayak@amd.com, linux-kernel@vger.kernel.org, mgorman@suse.de, mingo@redhat.com, peterz@infradead.org, rostedt@goodmis.org, surenb@google.com, vincent.guittot@linaro.org, vschneid@redhat.com, shakeel.butt@linux.dev, riel@surriel.com, kernel-team@meta.com Subject: Re: [PATCH] sched/psi: use for_each_set_bit() in psi_group_change() task-count walk Message-ID: <20260715135412.GN276793@cmpxchg.org> References: <20260714142057.181135-1-usama.arif@linux.dev> 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: <20260714142057.181135-1-usama.arif@linux.dev> On Tue, Jul 14, 2026 at 07:20:57AM -0700, Usama Arif wrote: > psi_group_change() walks the @clear and @set bitmasks to > decrement/increment groupc->tasks[t]. Both masks are at most > NR_PSI_TASK_COUNTS (=4) wide, dense at [0, 4), and typically > sparse. Today's form visits every position up to the highest set > bit: > > for (t = 0, m = clear; m; m &= ~(1 << t), t++) { > if (!(m & (1 << t))) > continue; > ... > } > > so a mask with only bit 3 set still spins four times; the same > open-coded shape repeats for @set. The code is also unnecessarily > hard to read. > > Switch both walks to for_each_set_bit() which is easier to read > and also more efficient. As NR_PSI_TASK_COUNTS is a compile-time > constant <= BITS_PER_LONG, find_next_bit() folds into its > small_const_nbits() fast path (single load + GENMASK + __ffs), lowering > to a bit-scan where one exists (x86 TZCNT/BSF, arm64 RBIT+CLZ). > > psi_group_change() runs from psi_task_switch() and psi_task_change() > once per ancestor psi_group per event, so the saved iterations > multiply out on any hot scheduler workload. > > No functional change intended. This actually started out using ffs. Because the performance is so sensitive in this path, this was handtuned to scheduler benchmarks. https://lore.kernel.org/all/20180718120318.GC2476@hirez.programming.kicks-ass.net/ Not the worst idea to revisit this, but you have to be careful, look at the asm, and benchmark it. gcc is producing more code for me with your patch.