From nobody Sun Feb 8 07:07:48 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 187AE2D7DD0 for ; Fri, 19 Dec 2025 08:08:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766131734; cv=none; b=RZssx5PMqxZFE8rdWXRctiGpQikdnLit/1/fnoFzJydJAnfDC89Oi/qBeBbRz2vYI1qF4oxHOzYVkS81HI5zz26eMRKePBOY3aTcme0PWAz7PiftsLTuQxrNnTRlTqgSMmbdRP5o+4f/ES0Kx2rMqz2K32dESDfV/pOfB8bvUDw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766131734; c=relaxed/simple; bh=c08HL3X0gS3Tir32FNsPBvq2tRvqI6vCoJ/9N8wTGuc=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=k1Oo6CAX6mHiY5Jc9Veh0h7Ayt72oFtHJJpzozB4TfbcPMyxxi1X/DR38Lq8mfMDkja82iM+nOyYo2Znh55ngWNo539IUCqxF5gFNzYmMmDMHm4G4NH3mlX7TQHXO2UKSVv9YBosYGJkNtMNXsY4KCPChqCGMo/04JBqCtwqjqU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--davidgow.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1RHQoupV; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--davidgow.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1RHQoupV" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7b9c91b814cso4317818b3a.2 for ; Fri, 19 Dec 2025 00:08:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1766131732; x=1766736532; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=CVYP3bSK2KuLrEmdKNykpHO/s/KahanlguSoTHltRDI=; b=1RHQoupVcIUAoTzzmPCpT4LeNSNBCfkZLwNOMXXFGFK7ioYGn6rHOv/24EdsNL9xB1 5lsyrSdlv+4NCtW/SoYo6KjqRASMOddodfIv3bPFM6waEV44agYvZMa9ibvMIKDrQXZ0 DzW7Isj5xEmyuAuowlzrVw30XM0l4ftQiXl1R1Qz4woKdrqFffHWeCAR4pQjk59QPRM8 WiFlyKoFATjJaRnmeDOLz/IrjpLrMU+AbMJHJMOdXCQcH7LZm/UP9cUQUJVKHg6mSom4 VcBI0SDgqvfPLZhzZrG7POirNRmSfu8FO59SnJOa2NJrss4AK9UNepfsQd2be9y8XsvY a5Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766131732; x=1766736532; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=CVYP3bSK2KuLrEmdKNykpHO/s/KahanlguSoTHltRDI=; b=kKuN3J8KduUGbLQtd24R6NiHo3s+13qmoVrs2hpnjb52qgcai+fKLQX4UDaYl7cAqE YPn0nBpsjBxf8qo8AsD9FcmvTwBjOQiZ1p+7sjD1gXI1ihgzWV1mVnDVyRtBgRZusmbP E/+OgKfFGBTuR0wQx4gCQicU6r83AgZ2vYBtcqeFQkuP2/pQ5kOLRhVprPlhy4njTRrl d78pHvZTDukbtfNqpK2xyUUNhSINVLiskCDqI5tF/vtV7wqyaH6NKpw8xu3OyFouBUyo W+LMd6bRRPqEHDG0hOeFCTM+hdtBfz6ncHJtLpGAvbIo55ff5VHRRFJ2B2PdIULH2KqF UU3Q== X-Forwarded-Encrypted: i=1; AJvYcCWR5zqghPb6tFEpK92aTJIef4xJKKEJA4ZoXhDQWkRwOuNgwV1pCYcaunL+WmK3nOX/djjUxey6M4BpnSw=@vger.kernel.org X-Gm-Message-State: AOJu0YzmizaZfmV4XpPprjv4ySa0pxm2F9QlQaPLqLNq/8Rfp6kkOfJw GbZlBca1BhsTiUz4PlXOXkvquNrOhw89lQDn13087agY9NB9cTiSfdsQIizBgWq37OO4eYp4V6G HGr9ut1ccWkr54g== X-Google-Smtp-Source: AGHT+IF1HWHkidomjHSYrLOWRhyEBQjZoXpv/inqUNzrJu7XY2pVFTmtxyYaUnlT82pi8CaiMhNQXsM7MCuS1Q== X-Received: from pfbk28.prod.google.com ([2002:a05:6a00:b01c:b0:7c2:a212:2b32]) (user=davidgow job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3385:b0:7ab:5e68:e204 with SMTP id d2e1a72fcca58-7ff650cdf7amr2113593b3a.29.1766131732390; Fri, 19 Dec 2025 00:08:52 -0800 (PST) Date: Fri, 19 Dec 2025 16:08:48 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.52.0.322.g1dd061c0dc-goog Message-ID: <20251219080850.921416-1-davidgow@google.com> Subject: [PATCH] kunit: Enforce task execution in {soft,hard}irq contexts From: David Gow To: Eric Biggers , Brendan Higgins , Rae Moar , Shuah Khan Cc: David Gow , linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The kunit_run_irq_test() helper allows a function to be run in hardirq and softirq contexts (in addition to the task context). It does this by running the user-provided function concurrently in the three contexts, until either a timeout has expired or a number of iterations have completed in the normal task context. However, on setups where the initialisation of the hardirq and softirq contexts (or, indeed, the scheduling of those tasks) is significantly slower than the function execution, it's possible for that number of iterations to be exceeded before any runs in irq contexts actually occur. This occurs with the polyval.test_polyval_preparekey_in_irqs test, which runs 20000 iterations of the relatively fast preparekey function, and therefore fails often under many UML, 32-bit arm, m68k and other environments. Instead, ensure that the max_iterations limit counts executions in all three contexts, and requires at least one of each. This will cause the test to continue iterating until at least the irq contexts have been tested, or the 1s wall-clock limit has been exceeded. This causes the test to pass in all of my environments. In so doing, we also update the task counters to atomic ints, to better match both the 'int' max_iterations input, and to ensure they are correctly updated across contexts. Finally, we also fix a few potential assertion messages to be less-specific to the original crypto usecases. Fixes: b41dc83f0790 ("kunit, lib/crypto: Move run_irq_test() to common head= er") Signed-off-by: David Gow --- include/kunit/run-in-irq-context.h | 41 ++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/include/kunit/run-in-irq-context.h b/include/kunit/run-in-irq-= context.h index 108e96433ea4..4d25aee0de6e 100644 --- a/include/kunit/run-in-irq-context.h +++ b/include/kunit/run-in-irq-context.h @@ -20,8 +20,8 @@ struct kunit_irq_test_state { bool task_func_reported_failure; bool hardirq_func_reported_failure; bool softirq_func_reported_failure; - unsigned long hardirq_func_calls; - unsigned long softirq_func_calls; + atomic_t hardirq_func_calls; + atomic_t softirq_func_calls; struct hrtimer timer; struct work_struct bh_work; }; @@ -32,7 +32,7 @@ static enum hrtimer_restart kunit_irq_test_timer_func(str= uct hrtimer *timer) container_of(timer, typeof(*state), timer); =20 WARN_ON_ONCE(!in_hardirq()); - state->hardirq_func_calls++; + atomic_inc(&state->hardirq_func_calls); =20 if (!state->func(state->test_specific_state)) state->hardirq_func_reported_failure =3D true; @@ -48,7 +48,7 @@ static void kunit_irq_test_bh_work_func(struct work_struc= t *work) container_of(work, typeof(*state), bh_work); =20 WARN_ON_ONCE(!in_serving_softirq()); - state->softirq_func_calls++; + atomic_inc(&state->softirq_func_calls); =20 if (!state->func(state->test_specific_state)) state->softirq_func_reported_failure =3D true; @@ -59,7 +59,10 @@ static void kunit_irq_test_bh_work_func(struct work_stru= ct *work) * hardirq context concurrently, and reports a failure to KUnit if any * invocation of @func in any context returns false. @func is passed * @test_specific_state as its argument. At most 3 invocations of @func w= ill - * run concurrently: one in each of task, softirq, and hardirq context. + * run concurrently: one in each of task, softirq, and hardirq context. @f= unc + * will continue running until either @max_iterations calls have been made= (so + * long as at least one each runs in task, softirq, and hardirq contexts),= or + * one second has passed. * * The main purpose of this interrupt context testing is to validate fallb= ack * code paths that run in contexts where the normal code path cannot be us= ed, @@ -85,6 +88,10 @@ static inline void kunit_run_irq_test(struct kunit *test= , bool (*func)(void *), .test_specific_state =3D test_specific_state, }; unsigned long end_jiffies; + int hardirq_calls, softirq_calls; + bool allctx =3D false; + + max_iterations =3D 1; =20 /* * Set up a hrtimer (the way we access hardirq context) and a work @@ -94,14 +101,22 @@ static inline void kunit_run_irq_test(struct kunit *te= st, bool (*func)(void *), CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); INIT_WORK_ONSTACK(&state.bh_work, kunit_irq_test_bh_work_func); =20 - /* Run for up to max_iterations or 1 second, whichever comes first. */ + /* Run for up to max_iterations (including at least one task, softirq, + * and hardirq), or 1 second, whichever comes first. + */ end_jiffies =3D jiffies + HZ; hrtimer_start(&state.timer, KUNIT_IRQ_TEST_HRTIMER_INTERVAL, HRTIMER_MODE_REL_HARD); - for (int i =3D 0; i < max_iterations && !time_after(jiffies, end_jiffies); - i++) { + for (int task_calls =3D 0, calls =3D 0; + ((calls < max_iterations) || !allctx) && !time_after(jiffies, end_jiffi= es); + task_calls++) { if (!func(test_specific_state)) state.task_func_reported_failure =3D true; + + hardirq_calls =3D atomic_read(&state.hardirq_func_calls); + softirq_calls =3D atomic_read(&state.softirq_func_calls); + calls =3D task_calls + hardirq_calls + softirq_calls; + allctx =3D (task_calls > 0) && (hardirq_calls > 0) && (softirq_calls > 0= ); } =20 /* Cancel the timer and work. */ @@ -109,21 +124,21 @@ static inline void kunit_run_irq_test(struct kunit *t= est, bool (*func)(void *), flush_work(&state.bh_work); =20 /* Sanity check: the timer and BH functions should have been run. */ - KUNIT_EXPECT_GT_MSG(test, state.hardirq_func_calls, 0, + KUNIT_EXPECT_GT_MSG(test, atomic_read(&state.hardirq_func_calls), 0, "Timer function was not called"); - KUNIT_EXPECT_GT_MSG(test, state.softirq_func_calls, 0, + KUNIT_EXPECT_GT_MSG(test, atomic_read(&state.softirq_func_calls), 0, "BH work function was not called"); =20 /* Check for incorrect hash values reported from any context. */ KUNIT_EXPECT_FALSE_MSG( test, state.task_func_reported_failure, - "Incorrect hash values reported from task context"); + "Failure reported from task context"); KUNIT_EXPECT_FALSE_MSG( test, state.hardirq_func_reported_failure, - "Incorrect hash values reported from hardirq context"); + "Failure reported from hardirq context"); KUNIT_EXPECT_FALSE_MSG( test, state.softirq_func_reported_failure, - "Incorrect hash values reported from softirq context"); + "Failure reported from softirq context"); } =20 #endif /* _KUNIT_RUN_IN_IRQ_CONTEXT_H */ --=20 2.52.0.322.g1dd061c0dc-goog