From nobody Sun Oct 5 18:15:10 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 BD50C7262A for ; Fri, 1 Aug 2025 02:34:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754015645; cv=none; b=gs6EQ6hy3F20pl0jdMsJcQfCfs39jfN+VV9FHduj/TQQBhbqp0etrmBraYKy0GPtwMCRMBu/z6fz1JLyA5jT8SwngM3Z9cHMnnq/KZ1VehEYKv3u03QYeH5gstnwQmGQTgiw1ythytSNLblhEUU9zkuml0A+wNXnq29dxRvJmTY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754015645; c=relaxed/simple; bh=6ucLfRiCdCW/6sTwAlSDv6XySbgJ6Dok2T5flbAKWUg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XBETzwEDnSlAZuiPM4N/DUFQwick/CCzakpxlLCovuyNwYMXkBbot7yTPCmOX2sHxM4Cma5JGIhWlIPAcmpFaqL541p38wMmM/WN6ffOjFzHv2cRiXbfygW9TZRhN7pX50wLsRt0Qsw3SPpAeon6bpSy8BFoMDygw/isrAjU640= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jstultz.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Iy0TqnjG; arc=none smtp.client-ip=209.85.216.74 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--jstultz.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Iy0TqnjG" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-31edd69d754so417335a91.1 for ; Thu, 31 Jul 2025 19:34:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1754015643; x=1754620443; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+0juvJELNCYKjYnqeY8elu6ed+jmv5fbSpBgJfsJU9k=; b=Iy0TqnjGEz5CMdpEKA2ukQtNSTMxmzfv6odoNXHGpXLHjyt6FPRevC+D/FEBy9TSBe SaOBBRe9xSnlLy/T7xy8tkE97zgRdfoBGJlV6D9cSw9GxzGmxC7Y9SDvH7aldG0/8w+H ZaQ5CjUvw38FwPS3FiqWgZaUTocDWgbXi10cDUsil8+C5aPEYNTdHscwViWy1+Marw3m 1sQZ8jLZT4RGYalMQ7Hq5zCzvSx7aw5AvLsskibYJYK2kEN6KAU0stvtphfarnO3ObHJ jNtkAYy5BxU3c2kjEXg9u4G1yrVg0QR3xcIEkT3DNvi7Km+Pa1WEbRISQ0BUT1ygaFTk +o/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754015643; x=1754620443; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+0juvJELNCYKjYnqeY8elu6ed+jmv5fbSpBgJfsJU9k=; b=IIxic2q2vuqVfV1HFD/b6lIAKmfTyEZ4edlsZnkBFrdzKrD8O26L6x9lCMxs5Gpe+3 upjDJw4N+rEQBEGz4njj9MFCWoEMVqYLy0jhG3mfOBTFoPN9ujDn1mmsC1c1TzPSI83G Fz5cdTM40ATV1tL9Q2PERgq57AQXum6gWAmh8xfET1UiMnrWIJd+DzmbUQFJ0UrQmfLa qPO9NC8Jnr6d1MBmunFdHz0rivnHSpv0/Dq+ACreQCCDiniLXKQ6ERCnD8/tTccaT4G1 USfiUTyaKRempk06DQ+DI1BY62Z222zLcbDxPj0liZ7FgLLoJb89zbjIi0hXuD8GfieJ Ccig== X-Gm-Message-State: AOJu0Ywnr6Ze5/vhPC/uy9GSeqICy70I25Oah8caXz3DgWyI93btciJi dfDeCYJylkcEBxv1pttsyTjRz5SuORiNYVOZj5dEM2PTFSroJHB0NoyvUXOSOD8NozInjhD8GpI a4PXcmCqA3U3U7ySTAlYF0yL6sduKT1pZ7nlsI/r6YKPO7W/zaBGSR6hlxf/G63c+vIlEftiLCq 0SqbXLR9T/qFeheJ6hvnLWtOUvkajNGIVGKagEVXJHlbeM0BpY X-Google-Smtp-Source: AGHT+IHzcsU+b3F4ZpqOW7hYQJxHwY4ZKEBxaRX5DF4+0k3waL1rIOWXbmqRQbkJ3OLXtdHFtobhhS49rqgU X-Received: from ploj13.prod.google.com ([2002:a17:902:da2d:b0:240:7c7d:49db]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f683:b0:240:79d5:8dd0 with SMTP id d9443c01a7336-24096adc97amr148450265ad.13.1754015642708; Thu, 31 Jul 2025 19:34:02 -0700 (PDT) Date: Fri, 1 Aug 2025 02:33:47 +0000 In-Reply-To: <20250801023358.562525-1-jstultz@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250801023358.562525-1-jstultz@google.com> X-Mailer: git-send-email 2.50.1.565.gc32cd1483b-goog Message-ID: <20250801023358.562525-2-jstultz@google.com> Subject: [PATCH 1/3] test-ww_mutex: Extend ww_mutex tests to test both classes of ww_mutexes From: John Stultz To: LKML Cc: John Stultz , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng , "Paul E . McKenney" , Joel Fernandes , Dietmar Eggemann , Suleiman Souhlal , kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently the test-ww_mutex tool only utilizes the wait-die class of ww_mutexes, and thus isn't very helpful in exercising the wait-wound class of ww_mutexes. So extend the test to exercise both classes of ww_mutexes for all of the subtests. Signed-off-by: John Stultz --- Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Will Deacon Cc: Waiman Long Cc: Boqun Feng Cc: "Paul E . McKenney" Cc: Joel Fernandes Cc: Dietmar Eggemann Cc: Suleiman Souhlal Cc: kernel-team@android.com --- kernel/locking/test-ww_mutex.c | 114 +++++++++++++++++++++------------ 1 file changed, 73 insertions(+), 41 deletions(-) diff --git a/kernel/locking/test-ww_mutex.c b/kernel/locking/test-ww_mutex.c index bcb1b9fea5880..20f509ca17e16 100644 --- a/kernel/locking/test-ww_mutex.c +++ b/kernel/locking/test-ww_mutex.c @@ -13,7 +13,8 @@ #include #include =20 -static DEFINE_WD_CLASS(ww_class); +static DEFINE_WD_CLASS(wd_class); +static DEFINE_WW_CLASS(ww_class); struct workqueue_struct *wq; =20 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH @@ -54,16 +55,16 @@ static void test_mutex_work(struct work_struct *work) ww_mutex_unlock(&mtx->mutex); } =20 -static int __test_mutex(unsigned int flags) +static int __test_mutex(struct ww_class *class, unsigned int flags) { #define TIMEOUT (HZ / 16) struct test_mutex mtx; struct ww_acquire_ctx ctx; int ret; =20 - ww_mutex_init(&mtx.mutex, &ww_class); + ww_mutex_init(&mtx.mutex, class); if (flags & TEST_MTX_CTX) - ww_acquire_init(&ctx, &ww_class); + ww_acquire_init(&ctx, class); =20 INIT_WORK_ONSTACK(&mtx.work, test_mutex_work); init_completion(&mtx.ready); @@ -106,13 +107,13 @@ static int __test_mutex(unsigned int flags) #undef TIMEOUT } =20 -static int test_mutex(void) +static int test_mutex(struct ww_class *class) { int ret; int i; =20 for (i =3D 0; i < __TEST_MTX_LAST; i++) { - ret =3D __test_mutex(i); + ret =3D __test_mutex(class, i); if (ret) return ret; } @@ -120,15 +121,15 @@ static int test_mutex(void) return 0; } =20 -static int test_aa(bool trylock) +static int test_aa(struct ww_class *class, bool trylock) { struct ww_mutex mutex; struct ww_acquire_ctx ctx; int ret; const char *from =3D trylock ? "trylock" : "lock"; =20 - ww_mutex_init(&mutex, &ww_class); - ww_acquire_init(&ctx, &ww_class); + ww_mutex_init(&mutex, class); + ww_acquire_init(&ctx, class); =20 if (!trylock) { ret =3D ww_mutex_lock(&mutex, &ctx); @@ -177,6 +178,7 @@ static int test_aa(bool trylock) =20 struct test_abba { struct work_struct work; + struct ww_class *class; struct ww_mutex a_mutex; struct ww_mutex b_mutex; struct completion a_ready; @@ -191,7 +193,7 @@ static void test_abba_work(struct work_struct *work) struct ww_acquire_ctx ctx; int err; =20 - ww_acquire_init_noinject(&ctx, &ww_class); + ww_acquire_init_noinject(&ctx, abba->class); if (!abba->trylock) ww_mutex_lock(&abba->b_mutex, &ctx); else @@ -217,23 +219,24 @@ static void test_abba_work(struct work_struct *work) abba->result =3D err; } =20 -static int test_abba(bool trylock, bool resolve) +static int test_abba(struct ww_class *class, bool trylock, bool resolve) { struct test_abba abba; struct ww_acquire_ctx ctx; int err, ret; =20 - ww_mutex_init(&abba.a_mutex, &ww_class); - ww_mutex_init(&abba.b_mutex, &ww_class); + ww_mutex_init(&abba.a_mutex, class); + ww_mutex_init(&abba.b_mutex, class); INIT_WORK_ONSTACK(&abba.work, test_abba_work); init_completion(&abba.a_ready); init_completion(&abba.b_ready); + abba.class =3D class; abba.trylock =3D trylock; abba.resolve =3D resolve; =20 schedule_work(&abba.work); =20 - ww_acquire_init_noinject(&ctx, &ww_class); + ww_acquire_init_noinject(&ctx, class); if (!trylock) ww_mutex_lock(&abba.a_mutex, &ctx); else @@ -278,6 +281,7 @@ static int test_abba(bool trylock, bool resolve) =20 struct test_cycle { struct work_struct work; + struct ww_class *class; struct ww_mutex a_mutex; struct ww_mutex *b_mutex; struct completion *a_signal; @@ -291,7 +295,7 @@ static void test_cycle_work(struct work_struct *work) struct ww_acquire_ctx ctx; int err, erra =3D 0; =20 - ww_acquire_init_noinject(&ctx, &ww_class); + ww_acquire_init_noinject(&ctx, cycle->class); ww_mutex_lock(&cycle->a_mutex, &ctx); =20 complete(cycle->a_signal); @@ -314,7 +318,7 @@ static void test_cycle_work(struct work_struct *work) cycle->result =3D err ?: erra; } =20 -static int __test_cycle(unsigned int nthreads) +static int __test_cycle(struct ww_class *class, unsigned int nthreads) { struct test_cycle *cycles; unsigned int n, last =3D nthreads - 1; @@ -327,7 +331,8 @@ static int __test_cycle(unsigned int nthreads) for (n =3D 0; n < nthreads; n++) { struct test_cycle *cycle =3D &cycles[n]; =20 - ww_mutex_init(&cycle->a_mutex, &ww_class); + cycle->class =3D class; + ww_mutex_init(&cycle->a_mutex, class); if (n =3D=3D last) cycle->b_mutex =3D &cycles[0].a_mutex; else @@ -367,13 +372,13 @@ static int __test_cycle(unsigned int nthreads) return ret; } =20 -static int test_cycle(unsigned int ncpus) +static int test_cycle(struct ww_class *class, unsigned int ncpus) { unsigned int n; int ret; =20 for (n =3D 2; n <=3D ncpus + 1; n++) { - ret =3D __test_cycle(n); + ret =3D __test_cycle(class, n); if (ret) return ret; } @@ -384,6 +389,7 @@ static int test_cycle(unsigned int ncpus) struct stress { struct work_struct work; struct ww_mutex *locks; + struct ww_class *class; unsigned long timeout; int nlocks; }; @@ -443,7 +449,7 @@ static void stress_inorder_work(struct work_struct *wor= k) int contended =3D -1; int n, err; =20 - ww_acquire_init(&ctx, &ww_class); + ww_acquire_init(&ctx, stress->class); retry: err =3D 0; for (n =3D 0; n < nlocks; n++) { @@ -511,7 +517,7 @@ static void stress_reorder_work(struct work_struct *wor= k) order =3D NULL; =20 do { - ww_acquire_init(&ctx, &ww_class); + ww_acquire_init(&ctx, stress->class); =20 list_for_each_entry(ll, &locks, link) { err =3D ww_mutex_lock(ll->lock, &ctx); @@ -570,7 +576,7 @@ static void stress_one_work(struct work_struct *work) #define STRESS_ONE BIT(2) #define STRESS_ALL (STRESS_INORDER | STRESS_REORDER | STRESS_ONE) =20 -static int stress(int nlocks, int nthreads, unsigned int flags) +static int stress(struct ww_class *class, int nlocks, int nthreads, unsign= ed int flags) { struct ww_mutex *locks; struct stress *stress_array; @@ -588,7 +594,7 @@ static int stress(int nlocks, int nthreads, unsigned in= t flags) } =20 for (n =3D 0; n < nlocks; n++) - ww_mutex_init(&locks[n], &ww_class); + ww_mutex_init(&locks[n], class); =20 count =3D 0; for (n =3D 0; nthreads; n++) { @@ -617,6 +623,7 @@ static int stress(int nlocks, int nthreads, unsigned in= t flags) stress =3D &stress_array[count++]; =20 INIT_WORK(&stress->work, fn); + stress->class =3D class; stress->locks =3D locks; stress->nlocks =3D nlocks; stress->timeout =3D jiffies + 2*HZ; @@ -635,57 +642,82 @@ static int stress(int nlocks, int nthreads, unsigned = int flags) return 0; } =20 -static int __init test_ww_mutex_init(void) +static int __init run_tests(struct ww_class *class) { int ncpus =3D num_online_cpus(); int ret, i; =20 - printk(KERN_INFO "Beginning ww mutex selftests\n"); - - prandom_seed_state(&rng, get_random_u64()); - - wq =3D alloc_workqueue("test-ww_mutex", WQ_UNBOUND, 0); - if (!wq) - return -ENOMEM; - - ret =3D test_mutex(); + ret =3D test_mutex(class); if (ret) return ret; =20 - ret =3D test_aa(false); + ret =3D test_aa(class, false); if (ret) return ret; =20 - ret =3D test_aa(true); + ret =3D test_aa(class, true); if (ret) return ret; =20 for (i =3D 0; i < 4; i++) { - ret =3D test_abba(i & 1, i & 2); + ret =3D test_abba(class, i & 1, i & 2); if (ret) return ret; } =20 - ret =3D test_cycle(ncpus); + ret =3D test_cycle(class, ncpus); if (ret) return ret; =20 - ret =3D stress(16, 2*ncpus, STRESS_INORDER); + ret =3D stress(class, 16, 2*ncpus, STRESS_INORDER); if (ret) return ret; =20 - ret =3D stress(16, 2*ncpus, STRESS_REORDER); + ret =3D stress(class, 16, 2*ncpus, STRESS_REORDER); if (ret) return ret; =20 - ret =3D stress(2046, hweight32(STRESS_ALL)*ncpus, STRESS_ALL); + ret =3D stress(class, 2046, hweight32(STRESS_ALL)*ncpus, STRESS_ALL); if (ret) return ret; =20 - printk(KERN_INFO "All ww mutex selftests passed\n"); return 0; } =20 +static int __init run_test_classes(void) +{ + int ret; + + pr_info("Beginning ww (wound) mutex selftests\n"); + + ret =3D run_tests(&ww_class); + if (ret) + return ret; + + pr_info("Beginning ww (die) mutex selftests\n"); + ret =3D run_tests(&wd_class); + if (ret) + return ret; + + pr_info("All ww mutex selftests passed\n"); + return 0; +} + +static int __init test_ww_mutex_init(void) +{ + int ret; + + prandom_seed_state(&rng, get_random_u64()); + + wq =3D alloc_workqueue("test-ww_mutex", WQ_UNBOUND, 0); + if (!wq) + return -ENOMEM; + + ret =3D run_test_classes(); + + return ret; +} + static void __exit test_ww_mutex_exit(void) { destroy_workqueue(wq); --=20 2.50.1.565.gc32cd1483b-goog