[PATCH v11 16/16] iommu/arm-smmu-v3: Add KUnit unit tests for Runtime PM

Nicolin Chen nicolinc at nvidia.com
Thu Oct 1 12:12:32 PDT 2026


On Tue, Sep 29, 2026 at 03:45:10AM +0000, Pranjal Shrivastava wrote:
>  #include <kunit/test.h>
> +#include <linux/delay.h>
> +#include <linux/kthread.h>
> +#include <linux/sched.h>

kthread is used here, so sched is not necessary?

> +struct arm_smmu_mock_cmdq {
> +	/*
> +	 * Mock register in RAM. Typed as __le32 to match writel_relaxed()
> +	 * endianness without calling readl_* accessors directly on RAM.
> +	 */
> +	__le32 mock_prod_reg;
> +};
> +
> +/* Helper to allocate a self-consuming mock cmdq */
> +static void arm_smmu_v3_test_init_mock_cmdq(struct kunit *test,
> +					    struct arm_smmu_device *smmu,
> +					    struct arm_smmu_mock_cmdq *mock)
> +{
> +	struct arm_smmu_cmdq *cmdq = &smmu->cmdq;
> +	unsigned long *mock_valid_map;
> +	u64 *mock_base;
> +
> +	mock_base = kunit_kzalloc(test, 1024 * sizeof(struct arm_smmu_cmd), GFP_KERNEL);
> +	mock_valid_map = kunit_kzalloc(test, BITS_TO_LONGS(1024) * sizeof(long), GFP_KERNEL);
> +
> +	KUNIT_ASSERT_NOT_NULL(test, mock_base);
> +	KUNIT_ASSERT_NOT_NULL(test, mock_valid_map);
> +
> +	smmu->features = 0;
> +	/* 1024 entries */
> +	cmdq->q.llq.max_n_shift = 10;
> +	cmdq->q.ent_dwords = CMDQ_ENT_DWORDS;
> +	cmdq->q.base = (__le64 *)mock_base;
> +	cmdq->valid_map = (atomic_long_t *)mock_valid_map;
> +
> +	/* Self-Consuming, prod == cons always ensures queue empty */
> +	cmdq->q.prod_reg = (__force void __iomem *)&mock->mock_prod_reg;
> +	cmdq->q.cons_reg = (__force void __iomem *)&mock->mock_prod_reg;

Is it really necessary to return mock->mock_prod_reg?

mock_base and mock_valid_map are working well here.

Couldn't mock_prod_reg be allocated here, then the caller just uses
cmdq->q.prod_reg?

E.g.

static inline u32 arm_smmu_v3_test_read_prod(struct arm_smmu_cmdq *cmdq)
{
      return le32_to_cpu(READ_ONCE(*(__le32 __force *)cmdq->q.prod_reg));
}

> +struct arm_smmu_test_timer_context {
> +	struct arm_smmu_device *smmu;
> +	struct timer_list timer;
> +	bool suspended;
> +};
> +
> +static void arm_smmu_v3_test_rpm_timer_callback(struct timer_list *t)
> +{
> +	struct arm_smmu_test_timer_context *ctx =
> +		timer_container_of(ctx, t, timer);
> +	struct arm_smmu_cmdq *cmdq = &ctx->smmu->cmdq;
> +
> +	/* Simulate a concurrent suspend event interrupting the invalidations */
> +	atomic_or(CMDQ_PROD_STOP_FLAG, &cmdq->q.llq.atomic.prod);
> +	WRITE_ONCE(ctx->suspended, true);

Both STOP and suspended flags are set at the same time, seemingly
a bit redundant to me..

> +struct arm_smmu_test_kthread_context {
> +	struct arm_smmu_device *smmu;
> +	int error;
> +};
> +
> +static int arm_smmu_v3_test_kthread_worker(void *data)
> +{
> +	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_cfgi_all();
> +	struct arm_smmu_test_kthread_context *ctx = data;
> +	struct arm_smmu_cmdq *cmdq = &ctx->smmu->cmdq;
> +
> +	while (!kthread_should_stop()) {
> +		if (arm_smmu_cmdq_issue_cmdlist(ctx->smmu, cmdq, &cmd, 1, false))
> +			WRITE_ONCE(ctx->error, 1);

With "sync=false", arm_smmu_cmdq_issue_cmdlist cannot fail. I don't
see any "sync=true" call site in this patch. But even there was one,
would the mock cmdq ever time out?

So, error (and arm_smmu_test_kthread_context) seems useless?

> +static void arm_smmu_v3_rpm_test_kthread_race(struct kunit *test)
> +{
> +	struct arm_smmu_test_kthread_context *ctx;
> +	struct arm_smmu_device *mock_smmu;
> +	struct arm_smmu_mock_cmdq *mock;
> +	struct task_struct *thread1, *thread2;
> +	u32 stopped_prod;
> +	int i;
> +
> +	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
> +	mock_smmu = kunit_kzalloc(test, sizeof(*mock_smmu), GFP_KERNEL);

The other function simply does:
	struct arm_smmu_device mock_smmu = smmu;

> +	/* Wait for worker threads to start and issue initial commands */
> +	for (i = 0; i < 20; i++) {

I am a bit confused by the number in the for loop: it's 20 here...

> +	KUNIT_EXPECT_GT(test, le32_to_cpu(READ_ONCE(mock->mock_prod_reg)), 0);
> +
> +	/* Gate the CMDQ */
> +	atomic_or(CMDQ_PROD_STOP_FLAG, &mock_smmu->cmdq.q.llq.atomic.prod);
> +
> +	/* Wait for in-flight submissions to settle */
> +	for (i = 0; i < 50; i++) {

... but it's 50 here. Mind elaborating?

Nicolin



More information about the linux-arm-kernel mailing list