From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 020CE312815 for ; Sat, 14 Mar 2026 21:45:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773524710; cv=none; b=n2yrdQ6zZy53ZRUgWgCUi7tP8bgvw0YqF6GEA62dGfatEZwRylnEGs/0HU+R1xxuWJBn08Mm/+LyZX8mQ4YsokcyeIWqvGNMeye1vfOy3rLX+KrdtKytctDROgQQAiUL9fZVFt+i+mIE4a3y4MPML/sDy7sDiASaAKvAiOM3qdI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773524710; c=relaxed/simple; bh=LpqTP6BM3MENnbzLYWfdxBa3Ku75siKJCBfMQENznsY=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=a1YkdPHZ9fCvwntjP6kP7Ro9ZLFX3PEC70dRd3F/hz5duW14lHonspt9bC2abOshDU4hl1470eYE0p7qLYM1AglGJFxYwuVAy6thsl2M5R1ZA+BUW2T0QzPe6CSB/IrX/2DfqupDpVwiChV/iGKBRv0AbZKjVaS7Xi+u4qZKQx8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=wihppXMI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="wihppXMI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 523A3C116C6; Sat, 14 Mar 2026 21:45:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1773524709; bh=LpqTP6BM3MENnbzLYWfdxBa3Ku75siKJCBfMQENznsY=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=wihppXMIt2xzf4SRDUwMWEqr1h/qdjjh4oWAtQgwkPH/cVLzOwh4kcNzOgv2rvBvv ac4bblcUtx84ifT7YkAZd7qdNxvVaE5XzWMjYLKNNZ414s6Bl+8zrqzBuow2VI2GQY U+JikyGFKNXg5ZDdNfAgR9hxEkacxpZfGd3iRUQQ= Date: Sat, 14 Mar 2026 14:45:08 -0700 From: Andrew Morton To: David Carlier Cc: Josh Law , Dennis Zhou , linux-kernel@vger.kernel.org, Mathieu Desnoyers Subject: Re: [PATCH] lib: fix compare_delta parameter order in percpu_counter_tree Message-Id: <20260314144508.ba3504b4338eb1ae84017028@linux-foundation.org> In-Reply-To: <20260313175424.132735-1-devnexen@gmail.com> References: <20260313175424.132735-1-devnexen@gmail.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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 On Fri, 13 Mar 2026 17:54:24 +0000 David Carlier wrote: > 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. This affects mm-unstable's "lib: introduce hierarchical per-cpu counters", so let's cc Mathieu. > --- 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)