From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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 4787E2367DC for ; Fri, 13 Mar 2026 17:54:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773424471; cv=none; b=Jv5R2+3J7217jQv3HrlSL9m8PDtPUNPIZ7dwfgSxc/wwc2bsHyaVzfCDX10qpImFf32cB8oOrOdi+oUYJnOHIeUqCd6M6m4eHKsq0O4tLrFVDm3lbEEc/7MaA/OMMTmN9aXJUE1OSLJPJBCNwQgDaivxkVhzJu97WkYvmYSmjRE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773424471; c=relaxed/simple; bh=OG3mngXWd0Rs11/xwTUn9M9+Afd8qBr1fPXEAfqlJLk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=E2AIYhAJnWJLVdkAGVGRkjNnmZe+v//vPNDGcCYEs75zyXyTimqnohcv4oGSEmfpsqsO6WD4/Fe1lCmeJCXESeuEWjPUyFYr9JuQHp7KRVCBnhkcvI1AFybfEZ7xz6ix5c9R6fdx4dSHqNw8S6T8t92To3AxB4buWO6GxFgTxM0= 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=KLhiJmq6; arc=none smtp.client-ip=209.85.221.53 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="KLhiJmq6" Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-439fe4985efso2226949f8f.3 for ; Fri, 13 Mar 2026 10:54:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773424468; x=1774029268; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=qbAba3ykAuvBj2oUAd/OGQP3DrNnRZycn1qYo4T6O64=; b=KLhiJmq6nXhusCUIu3HFWelp1WUPpVAn6IA5xfb3bRFKIIHsRnmHbDxHnG90BoVzu/ BsC/MEcQbXYberSTLntaktLSD9yZDqtLrGe5IYI4sM2l6o1vU5CVo6KfTZmFLE21Ge+0 NOIN5N6lU+8PUx18H5UcRwRokDZQsLBnk+av0ZiEizgJ3AlFlMIyZRuzHaY5QJD0aFtG 5JXZ+3TTK/USwIWgKxOc+psZ1wxPV2UH6CQ6y863fLAoOuEMQknJ4Fb+hyBFheq9FqzQ 6hARn8us95JpIpo33oY66C6rNHE0mHqJ2wf4Dsde1qgRMSwn4Gwec+pUoNG3c1WHZabo 7HCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773424468; x=1774029268; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=qbAba3ykAuvBj2oUAd/OGQP3DrNnRZycn1qYo4T6O64=; b=gOulxfJy2VuMmchaSLzTDc5juEbTFjcGPtsNQV7MZVfvb1ojE1ZPk5H2QX82WJBTw0 +mYIXeNvTZWUJZHj24R6frb1YMFcsAODwNyMmYBMTACzJdFzSo4kWINHOXdZfz45Zt+P x/m68ILIdbrQRoR9LdQ9g16wl04TwRBRmc8HwmFaO8vlGSNiRKoub7VDjBcgmqJ4SHBV GeCng0XmTO48HAB8vfj4Dsv411NbhRcTGPmNrVjxg/LpSUsOepjUj9HZaFdbejsXg26v r/ogy0J6DJ655grGs0S62TW9ynreGT3qbU8TrUy61EY8BHuvw7435VCgimcV3y9JKCvn CVMg== X-Gm-Message-State: AOJu0Yy18089Mw1CRs9+6boTBsY/MabpJwEqhVaF3YFtWokQhHMnP68T jpV5E9hKyfzNgNXdmaCaUXi93UwJNv4yhbE8efja079NGLixmo2/Q4cMlKqY/5mR X-Gm-Gg: ATEYQzyKJCBc1pmlhzEqCIvOOHUNp2O2MfCcu2NWf0pC7YPFBJQpdfve22yUGNxgNRw panatWZO32eaMtcsOPqv+Q/R+dsvVYT1xX5UCsMe28AUfOoz4awY7P3AkULDivF9sojjhbFCAYj 8ySbYUsEKtJWUWdQhKN2SOKIBqotZSrMa6+1zMRoLxVxLTfMU57dHBD5Meneu2hGaSWncpJ9DJh MTQlz4gXT+HfZKcmyPnRmt5qfCRiYQAZty8DVZDX5kAUqX9pMJ8MLyWVPcJDpx5sOHWk+rqc3Yx HCWYML8U6niJBUPliLQ/gWz5JjRRMmB5tCbYKD2VpzOMa3HVMMCO5Yc2ji70IuFZVL0w8rEwyp0 GkzPtl1McRlufAi1O1dup00AKUoI9hApa3IJVnoKJzL+sDZnq88AIQx/RQao54NDcfprbMK6l+d cn6R8PYM5usa/BIgwi8nf0XQqPBCrJ/qiRTRCj3vlwdioB6CLPoztG6NuQd384vkwBbEX2Ckm3/ /12Ew0T+NFj3MnETP6d X-Received: by 2002:a05:6000:1ac8:b0:439:bc79:9f8f with SMTP id ffacd0b85a97d-43a04d14d83mr8886619f8f.0.1773424468286; Fri, 13 Mar 2026 10:54:28 -0700 (PDT) Received: from rozandragon.chello.ie (188-141-5-72.dynamic.upc.ie. [188.141.5.72]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-439fe20bd9csm19261100f8f.21.2026.03.13.10.54.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Mar 2026 10:54:27 -0700 (PDT) From: David Carlier To: Andrew Morton , Josh Law , Dennis Zhou Cc: linux-kernel@vger.kernel.org, David Carlier Subject: [PATCH] lib: fix compare_delta parameter order in percpu_counter_tree Date: Fri, 13 Mar 2026 17:54:24 +0000 Message-ID: <20260313175424.132735-1-devnexen@gmail.com> X-Mailer: git-send-email 2.51.0 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: 8bit The compare_delta() helper takes (delta, accuracy_neg, accuracy_pos), but every call site passes (delta, accuracy_pos, accuracy_neg) — the last two arguments are consistently swapped. The documented invariant (include/linux/percpu_counter_tree.h) is: (precise_sum - under) <= approx_sum <= (precise_sum + over) Which means precise_sum is in [approx_sum - over, approx_sum + under]. For a positive delta (v - approx_sum >= 0), accuracy_pos must be "under" (the maximum amount precise_sum can exceed approx_sum). For a negative delta, accuracy_neg must be "over". 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). 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. Fix by swapping the parameter order in compare_delta() itself, since all call sites are consistently swapped. Signed-off-by: David Carlier --- lib/percpu_counter_tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/percpu_counter_tree.c b/lib/percpu_counter_tree.c index 5c8fc2dcdc16..beb1144e6450 100644 --- a/lib/percpu_counter_tree.c +++ b/lib/percpu_counter_tree.c @@ -458,7 +458,7 @@ long percpu_counter_tree_precise_sum(struct percpu_counter_tree *counter) EXPORT_SYMBOL_GPL(percpu_counter_tree_precise_sum); 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 >= 0) { if (delta <= accuracy_pos) -- 2.51.0