From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sender-of-o59.zoho.eu (sender-of-o59.zoho.eu [136.143.169.59]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 388B12F547F for ; Sat, 14 Mar 2026 15:30:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.169.59 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773502221; cv=pass; b=TwWd2R6DmSufMifSEkqL2j7cs7PPNM6bIMSLastnqd49LRmPO2zsDF9FZMP+z2wSxbU8yM/vS6R24VrbrvFjofn/jTaPCY9/bKJT+vggV4Sw2xjNtbu7R+DZwoPa+nv3TSWcu2Bxa6wwaBlqNH7DfQLBFsFvVdimwH3yT6+ZAvE= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773502221; c=relaxed/simple; bh=x+8UKS9VEpyaRYHyrO8IIx+ukY415e3ESqiGQ781VfM=; h=Date:From:To:CC:Subject:In-Reply-To:References:Message-ID: MIME-Version:Content-Type; b=pWBwJH/MgitbU+VTBorFpp+FPVPjtpdrybvn35yCHbAvQd+KyzOecKsOGiyZ7b2vpbULs2yupcZZxMa5JjSO24ru52y4kUKe0obIKsxcBSmG13ujbuaREuTubnvgxNeFpEgQ13rYW9tD0ZBv6minV0dQauO2sDx2Bx18Njbabk4= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=objecting.org; spf=pass smtp.mailfrom=objecting.org; dkim=pass (1024-bit key) header.d=objecting.org header.i=objecting@objecting.org header.b=nr43xGJZ; arc=pass smtp.client-ip=136.143.169.59 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=objecting.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=objecting.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=objecting.org header.i=objecting@objecting.org header.b="nr43xGJZ" ARC-Seal: i=1; a=rsa-sha256; t=1773502205; cv=none; d=zohomail.eu; s=zohoarc; b=RIOzbwCFTgPJmUNjtO20b3tR2ZA7p6eodzejHjUJkhN2BT4lLqWrsE6hxT3D1yhK4mLSJoDOnmcu/Gc7OnaV67F9PDBjYfH0/+HndHHggDVt66VkYZuHsYK1fmq3FvX216v4kKmRXl7nTh3MseS8bNL4QyPTKgOS/IXW8FCBXBI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.eu; s=zohoarc; t=1773502205; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=t/rV2mHFGPPPD3PsRdf5qYGGyZ4tWd5UYYhY2/M6utE=; b=AAxhcRkwEsJZ6c+SGjipkQDH0FP20lKCATl7DfdTNeQaLutkQnxYHAD/Oc+veN5mwA4WboQDrDLTunllU4K80WJW9qAbyKnPyK1NALgkDojhwsosRYd+aVqK7EWeTiElv6BdO414F+Cc3ZsLOhOuLh/suNoEIBLTW3SeUUitXF4= ARC-Authentication-Results: i=1; mx.zohomail.eu; dkim=pass header.i=objecting.org; spf=pass smtp.mailfrom=objecting@objecting.org; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1773502205; s=zmail; d=objecting.org; i=objecting@objecting.org; h=Date:Date:From:From:To:To:CC:Subject:Subject:In-Reply-To:References:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Reply-To:Cc; bh=t/rV2mHFGPPPD3PsRdf5qYGGyZ4tWd5UYYhY2/M6utE=; b=nr43xGJZG8Mkla+cnxe0rNiZKIkWK4DMofNArTiQzFKB7D86VutuV1SYHr5LzOkb ivMDMOv3lf8SyZmXxtVrZDeIG43crxNWpMHVeDoSBhkIL6eM4oWMh++YsWqIx2Iqru2 JYKBtbtxaAD9/gIsdIdkN5u3QQmPAPmcaY4TIHGA= Received: by mx.zoho.eu with SMTPS id 1773502204038747.247247195097; Sat, 14 Mar 2026 16:30:04 +0100 (CET) Date: Sat, 14 Mar 2026 15:30:03 +0000 From: Josh Law To: David Carlier , Andrew Morton , Dennis Zhou CC: linux-kernel@vger.kernel.org Subject: =?US-ASCII?Q?Re=3A_=5BPATCH=5D_lib=3A_fix_compare=5Fdelta_p?= =?US-ASCII?Q?arameter_order_in_percpu=5Fcounter=5Ftree?= User-Agent: Thunderbird for Android In-Reply-To: <20260313175424.132735-1-devnexen@gmail.com> References: <20260313175424.132735-1-devnexen@gmail.com> Message-ID: <1BDF7DFA-1515-4962-B0A9-8184AFF48084@objecting.org> 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=utf-8 Content-Transfer-Encoding: quoted-printable X-ZohoMailClient: External On 13 March 2026 17:54:24 GMT, David Carlier wrote: >The compare_delta() helper takes (delta, accuracy_neg, accuracy_pos), >but every call site passes (delta, accuracy_pos, accuracy_neg) =E2=80=94 = the >last two arguments are consistently swapped=2E > >The documented invariant (include/linux/percpu_counter_tree=2Eh) is: > > (precise_sum - under) <=3D approx_sum <=3D (precise_sum + over) > >Which means precise_sum is in [approx_sum - over, approx_sum + under]=2E > >For a positive delta (v - approx_sum >=3D 0), accuracy_pos must be >"under" (the maximum amount precise_sum can exceed approx_sum)=2E >For a negative delta, accuracy_neg must be "over"=2E Since under > over >always (batch_size * M vs (batch_size - 1) * M), swapping them causes >false definitive results: the functions return 1 ("v > counter") when >the correct answer is 0 (indeterminate)=2E > >This affects all comparison functions: >- percpu_counter_tree_approximate_compare_value() >- percpu_counter_tree_approximate_compare() >- percpu_counter_tree_precise_compare_value() >- percpu_counter_tree_precise_compare() > >The precise variants are also affected because their approximate >fast-path can short-circuit with a wrong result, skipping the precise >sum computation=2E > >Fix by swapping the parameter order in compare_delta() itself, since >all call sites are consistently swapped=2E > >Signed-off-by: David Carlier >--- > lib/percpu_counter_tree=2Ec | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > >diff --git a/lib/percpu_counter_tree=2Ec b/lib/percpu_counter_tree=2Ec >index 5c8fc2dcdc16=2E=2Ebeb1144e6450 100644 >--- a/lib/percpu_counter_tree=2Ec >+++ b/lib/percpu_counter_tree=2Ec >@@ -458,7 +458,7 @@ long percpu_counter_tree_precise_sum(struct percpu_co= unter_tree *counter) > EXPORT_SYMBOL_GPL(percpu_counter_tree_precise_sum); >=20 > static >-int compare_delta(long delta, unsigned long accuracy_neg, unsigned long = accuracy_pos) >+int compare_delta(long delta, unsigned long accuracy_pos, unsigned long = accuracy_neg) > { > if (delta >=3D 0) { > if (delta <=3D accuracy_pos) Good patch=2E Reviewed-by: Josh Law V/R Josh Law