From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.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 D3F232C11ED for ; Mon, 16 Mar 2026 14:38:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773671894; cv=none; b=DlXDZx/2mOQhoBsUXLY1lqHceEag54a09VX/0VF9o7bmDbOtYMZcOFZ61AKeIGZdXNkjtiVuWTkwfzIwoIFGGj0Zl0kIvP+NUi5nC7ah+Qu++cOKHz7OG7vPqOwD1K8J4k7HNY4BfICIS5pD+tknhsZfwZusT2yStu8SB9X48qM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773671894; c=relaxed/simple; bh=hOo/N9R1lCP/fHkmPktVWTPnzOK2sxe3TG5WL7Wh3O0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=F8J8wVT1WhwrI3+xbBdgtXocDYWXlE5qROu0E+j0Q2R+a9waV94VG5D46m+p+gktrXj2FF89gh+jzH90B3/tMF5Xz/pECJay3sMlsu+SZEypV3qTkEq3yE9qyFQpTNkrB4ssf+XUeUBcbQV9G7fFPv7jUc+284/CFoQ7BoeCO2c= 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=NV7+RBbH; arc=none smtp.client-ip=209.85.128.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="NV7+RBbH" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4853fd7b59aso28588835e9.2 for ; Mon, 16 Mar 2026 07:38:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773671891; x=1774276691; 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=2st02toQOWJKju1Ge/R81+c9QAv3+bzNK6AJWF+YRUc=; b=NV7+RBbHiMBZHKnZIZvGXst5opxgYWmVOykctxJTX57s93CvCb2/5D0zILVifkgzKe AC8KYvRXmYxmZLkRmQxYJkMKV4dpxfsmd7BFQkyhYxAQUG/oDuSfxv3u295SEuAkvKXi RhVnrRyghbPr1+59JG7fy7iNi8BFdP1F4uDxNMzbU5BaSHqsWeReB/ZQkVTlojbxz/bt 4Q6Nf3H15OzODMNmknk4GoUsSXCguPX0amnDocGccSMyVVSuaNkYsIF6c2/fxe+JVfv1 qx1H0JEPzlLNgAJ3/BXfT+nrRzLrqZPklxezI1jyv8vrai3dzfHhbh8qm310MeS40DQJ /puA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773671891; x=1774276691; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=2st02toQOWJKju1Ge/R81+c9QAv3+bzNK6AJWF+YRUc=; b=ZuVkEanWsnplP9y2rGT19DoOv6d38j0RJ+Eat3FDBUq7awolY8FDXsv9+ASzmsjpjg u2K/x8lHmoU3EyZUBy1CQZ9cvbYiNV9xYReY38ncOtKNmv/G70d7vm0buoYjHx31n5hm w/+XhjliBqcK/kFOJaH2xLQAV8He+S2hSsRvcalpS7LMpQr28dtxEzd4WG7JDuZ3hh0/ F9XpxQwLiXbRinYGCfqkE89cnIgB502G4CRHLSjM/TSBttrBav4XfFXPTu8SHp9AOnVu PLd6QMHMRj/qqleho2fBeDKcwhpDbfsCZHrmfGh6Txe/e6U8F6xfBJFl8A19oO+PsUh2 g2Nw== X-Forwarded-Encrypted: i=1; AJvYcCVm0QnXtpHiYXPl9uPQDuzxdkd4gg/eN5ihq5C3kYaO6x/5wsutzgvecuEHRQzU5NfIv+Qu8iGMySX/cSM=@vger.kernel.org X-Gm-Message-State: AOJu0Yztgxskrf5AZyZL0przI5Ymgjz4kP/dDwJ7kZvwzaUhwPduetfC +LgSZwQm9G7cKsUHZgrNhz5Qn5QmBKbyLc9q7Aql85NHup5cUzpryy9/y64BI83x X-Gm-Gg: ATEYQzyCAsrDSqIkcrEoo9+pX6rUwLve2/4jijAfRTmWtD2gsanhv35E99gkMLJhr2m LC09at6bKbBrk0tSv+4vF9KJBrG8EeKS4Dgiz/ChUBNgYPHwwtSTtaAFDnqlLuNFSiAepmlRwgf XiPFJcQzhONMG4nBcbjXWg2O5S+wNONhbpOay714oAKrUoF4XWc60L4MsuTi4dD7E7aCmho3XUk GCqu1XItSz8D5kkqqDozgqNC3hWhLYBWAkMDn64Ay00xURgSd0rfPdX++QVN1CHoC3N6VnlZvwr RrZ/xOtc2dz1rcVwoUQqRuL/K0h5yfbUZ2NupZaLV88am1yBw2QWHkbfmHYzDmG1Ujr2CI5YVux XYGRjBltNEzXWOk/I5CD9dgi0vPMtencJw3vkUqHcGo/pHl9KRydUCOgiqNHPytMwIjrq9ssYv7 l6OPlUSU5mGumdZxCBW3X/X2ZfIWFDSiSL5wUkveK+Yt3N4dEE/4hpC8dRTO5QudmeG1NHwi2hM 9y5HJ9u0+N1HgcY0HnP X-Received: by 2002:a05:600c:6912:b0:485:4eaf:eb54 with SMTP id 5b1f17b1804b1-485566fcb3cmr212955925e9.20.1773671890761; Mon, 16 Mar 2026 07:38:10 -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 5b1f17b1804b1-4854e2537c3sm458521895e9.15.2026.03.16.07.38.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Mar 2026 07:38:10 -0700 (PDT) From: David Carlier To: Josh Law , Dennis Zhou Cc: Mathieu Desnoyers , Andrew Morton , linux-kernel@vger.kernel.org, David Carlier Subject: [PATCH 2/2] lib: add kunit boundary tests for percpu_counter_tree comparisons Date: Mon, 16 Mar 2026 14:38:05 +0000 Message-ID: <20260316143805.36805-2-devnexen@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260316143805.36805-1-devnexen@gmail.com> References: <20260316143805.36805-1-devnexen@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: 8bit Add boundary tests for all four comparison APIs to validate that the accuracy parameters are correctly applied: - percpu_counter_tree_approximate_compare_value() - percpu_counter_tree_precise_compare_value() - percpu_counter_tree_approximate_compare() - percpu_counter_tree_precise_compare() The single-value tests probe the accuracy range boundaries (at, beyond, and in the asymmetric gap between under and over) to catch swapped accuracy parameters. The two-counter tests verify the symmetric combined accuracy boundary for counter-to-counter comparisons. Signed-off-by: David Carlier --- lib/tests/percpu_counter_tree_kunit.c | 190 ++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) diff --git a/lib/tests/percpu_counter_tree_kunit.c b/lib/tests/percpu_counter_tree_kunit.c index a79176655c4b..4d058bc78f7d 100644 --- a/lib/tests/percpu_counter_tree_kunit.c +++ b/lib/tests/percpu_counter_tree_kunit.c @@ -86,6 +86,194 @@ static void check_counters(struct kunit *test) KUNIT_EXPECT_EQ(test, 0, percpu_counter_tree_approximate_compare(&counter[0], &counter[1])); } +static void hpcc_test_compare_value_boundaries(struct kunit *test) +{ + struct percpu_counter_tree pct; + struct percpu_counter_tree_level_item *counter_items; + unsigned long under = 0, over = 0; + int ret; + + counter_items = kzalloc(percpu_counter_tree_items_size(), GFP_KERNEL); + KUNIT_ASSERT_PTR_NE(test, counter_items, NULL); + ret = percpu_counter_tree_init(&pct, counter_items, 32, GFP_KERNEL); + KUNIT_ASSERT_EQ(test, ret, 0); + + percpu_counter_tree_set(&pct, 0); + percpu_counter_tree_approximate_accuracy_range(&pct, &under, &over); + + /* + * With approx_sum = precise_sum = 0, from the accuracy invariant: + * approx_sum - over <= precise_sum <= approx_sum + under + * Positive deltas use 'under' as tolerance, negative use 'over'. + */ + + /* --- percpu_counter_tree_approximate_compare_value --- */ + + /* At boundary: indeterminate */ + KUNIT_EXPECT_EQ(test, 0, + percpu_counter_tree_approximate_compare_value(&pct, + (long)under)); + KUNIT_EXPECT_EQ(test, 0, + percpu_counter_tree_approximate_compare_value(&pct, + -(long)over)); + + /* Beyond boundary: definitive */ + KUNIT_EXPECT_EQ(test, 1, + percpu_counter_tree_approximate_compare_value(&pct, + (long)(under + 1))); + KUNIT_EXPECT_EQ(test, -1, + percpu_counter_tree_approximate_compare_value(&pct, + -(long)(over + 1))); + + /* Asymmetric gap: catches swapped accuracy parameters */ + if (under != over) { + if (under > over) { + KUNIT_EXPECT_EQ(test, 0, + percpu_counter_tree_approximate_compare_value( + &pct, (long)(over + 1))); + KUNIT_EXPECT_EQ(test, -1, + percpu_counter_tree_approximate_compare_value( + &pct, -(long)(over + 1))); + } else { + KUNIT_EXPECT_EQ(test, 1, + percpu_counter_tree_approximate_compare_value( + &pct, (long)(under + 1))); + KUNIT_EXPECT_EQ(test, 0, + percpu_counter_tree_approximate_compare_value( + &pct, -(long)(under + 1))); + } + } + + /* --- percpu_counter_tree_precise_compare_value --- */ + + /* + * With approx_sum = precise_sum = 0, the precise comparison + * must return the exact result. At boundary values the + * approximate fast-path returns indeterminate, exercising + * the precise sum fallback. + */ + KUNIT_EXPECT_EQ(test, 0, + percpu_counter_tree_precise_compare_value(&pct, 0)); + KUNIT_EXPECT_EQ(test, 1, + percpu_counter_tree_precise_compare_value(&pct, + (long)under)); + KUNIT_EXPECT_EQ(test, -1, + percpu_counter_tree_precise_compare_value(&pct, + -(long)over)); + KUNIT_EXPECT_EQ(test, 1, + percpu_counter_tree_precise_compare_value(&pct, + (long)(under + 1))); + KUNIT_EXPECT_EQ(test, -1, + percpu_counter_tree_precise_compare_value(&pct, + -(long)(over + 1))); + + if (under != over) { + if (under > over) { + KUNIT_EXPECT_EQ(test, 1, + percpu_counter_tree_precise_compare_value( + &pct, (long)(over + 1))); + KUNIT_EXPECT_EQ(test, -1, + percpu_counter_tree_precise_compare_value( + &pct, -(long)(over + 1))); + } else { + KUNIT_EXPECT_EQ(test, 1, + percpu_counter_tree_precise_compare_value( + &pct, (long)(under + 1))); + KUNIT_EXPECT_EQ(test, -1, + percpu_counter_tree_precise_compare_value( + &pct, -(long)(under + 1))); + } + } + + percpu_counter_tree_destroy(&pct); + kfree(counter_items); +} + +static void hpcc_test_compare_counter_boundaries(struct kunit *test) +{ + struct percpu_counter_tree pct[2]; + struct percpu_counter_tree_level_item *counter_items; + unsigned long under = 0, over = 0; + unsigned long combined; + int ret; + + counter_items = kzalloc(percpu_counter_tree_items_size() * 2, + GFP_KERNEL); + KUNIT_ASSERT_PTR_NE(test, counter_items, NULL); + ret = percpu_counter_tree_init_many(pct, counter_items, 2, 32, + GFP_KERNEL); + KUNIT_ASSERT_EQ(test, ret, 0); + + percpu_counter_tree_approximate_accuracy_range(&pct[0], + &under, &over); + + /* + * Both counters have the same configuration. The combined + * accuracy for two-counter comparison is symmetric: + * accuracy_pos = over + under = under + over = accuracy_neg + */ + combined = under + over; + + /* --- percpu_counter_tree_approximate_compare --- */ + + /* At boundary: indeterminate */ + percpu_counter_tree_set(&pct[0], (long)combined); + percpu_counter_tree_set(&pct[1], 0); + KUNIT_EXPECT_EQ(test, 0, + percpu_counter_tree_approximate_compare(&pct[0], &pct[1])); + + percpu_counter_tree_set(&pct[0], 0); + percpu_counter_tree_set(&pct[1], (long)combined); + KUNIT_EXPECT_EQ(test, 0, + percpu_counter_tree_approximate_compare(&pct[0], &pct[1])); + + /* Beyond boundary: definitive */ + percpu_counter_tree_set(&pct[0], (long)(combined + 1)); + percpu_counter_tree_set(&pct[1], 0); + KUNIT_EXPECT_EQ(test, 1, + percpu_counter_tree_approximate_compare(&pct[0], &pct[1])); + + percpu_counter_tree_set(&pct[0], 0); + percpu_counter_tree_set(&pct[1], (long)(combined + 1)); + KUNIT_EXPECT_EQ(test, -1, + percpu_counter_tree_approximate_compare(&pct[0], &pct[1])); + + /* --- percpu_counter_tree_precise_compare --- */ + + /* At boundary: precise gives exact result */ + percpu_counter_tree_set(&pct[0], (long)combined); + percpu_counter_tree_set(&pct[1], 0); + KUNIT_EXPECT_EQ(test, 1, + percpu_counter_tree_precise_compare(&pct[0], &pct[1])); + + percpu_counter_tree_set(&pct[0], 0); + percpu_counter_tree_set(&pct[1], (long)combined); + KUNIT_EXPECT_EQ(test, -1, + percpu_counter_tree_precise_compare(&pct[0], &pct[1])); + + /* Beyond boundary: definitive */ + percpu_counter_tree_set(&pct[0], (long)(combined + 1)); + percpu_counter_tree_set(&pct[1], 0); + KUNIT_EXPECT_EQ(test, 1, + percpu_counter_tree_precise_compare(&pct[0], &pct[1])); + + percpu_counter_tree_set(&pct[0], 0); + percpu_counter_tree_set(&pct[1], (long)(combined + 1)); + KUNIT_EXPECT_EQ(test, -1, + percpu_counter_tree_precise_compare(&pct[0], &pct[1])); + + /* Equal counters */ + percpu_counter_tree_set(&pct[0], 42); + percpu_counter_tree_set(&pct[1], 42); + KUNIT_EXPECT_EQ(test, 0, + percpu_counter_tree_approximate_compare(&pct[0], &pct[1])); + KUNIT_EXPECT_EQ(test, 0, + percpu_counter_tree_precise_compare(&pct[0], &pct[1])); + + percpu_counter_tree_destroy_many(pct, 2); + kfree(counter_items); +} + static int multi_thread_worker_fn(void *data) { struct multi_thread_test_data *td = data; @@ -383,6 +571,8 @@ static struct kunit_case hpcc_test_cases[] = { KUNIT_CASE(hpcc_test_single_thread_random), KUNIT_CASE(hpcc_test_multi_thread_batch_increment), KUNIT_CASE(hpcc_test_multi_thread_random_walk), + KUNIT_CASE(hpcc_test_compare_value_boundaries), + KUNIT_CASE(hpcc_test_compare_counter_boundaries), KUNIT_CASE(hpcc_test_init_one), KUNIT_CASE(hpcc_test_set), {} -- 2.51.0