From nobody Wed Feb 11 19:42:31 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01A7FC76196 for ; Tue, 11 Apr 2023 04:25:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230153AbjDKEZx (ORCPT ); Tue, 11 Apr 2023 00:25:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230103AbjDKEZ3 (ORCPT ); Tue, 11 Apr 2023 00:25:29 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5021B2D5A for ; Mon, 10 Apr 2023 21:25:28 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id v67-20020a254846000000b00b8189f73e94so30494744yba.12 for ; Mon, 10 Apr 2023 21:25:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1681187127; x=1683779127; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MXbqcM8S0eoqY1JXMQQLXkL4JKytwPxCmdsZ17HQXnE=; b=WK9/avoTybboSLoz1SBxCBBJnRPD/Fd/1to1j2hAhGaAsh+wRplwRqS4kx4v9MqJrY 7iSOkK8HXV8a8dc2GnPxpH6SqClEmMxKazCmc4zJbRyGQUb5MruIYt/wzpFfRAf6xrQS il7AC0DqyPgA+fYVr6P/St1k777N/15sA1RGQiR910rWVDV3tinIpXdyBQ8fYpEzqlx/ OUuD6e6aAcO6M/UorjnNeVZ75OZ1mFZjEbToiqb7tiycf3j5UUs1KlJKcUUiydTbkAuB wksAC9/P2sOfBQuqe3JDJylNcebdeV1VkkRKeMMkMxBX88yJseIYg9zjD3s5ks0wEzTl veFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681187127; x=1683779127; 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=MXbqcM8S0eoqY1JXMQQLXkL4JKytwPxCmdsZ17HQXnE=; b=WqHWyIMhBvQ4Fw/kglN/7C72gzlEmJ586YGTO8uDwN6yoPA/1t+m22ZRIieJ41j6Rt +eWdZMxxscdueS1BbhAyyMqi/w3BDLKMeGZ5IufY1wdH0w6DsioQ0klHHhfsaM1s+Ejp ZJAAjsRbf1YHvyAL0/Pe+dfSqjzUhOIDDQK0doyzf6gQVr+k+ih1F5+WxCPXmdCppkYl y9hGZYXkPNejD/S8rUmrsXGtV7e5SL+BypTVJroe0MdSwEpxAeM2U2XPZGI6rCo++vUf P/xV/tZ+7Kenx1x1Mw+LQDSq3UB2aucbMtX2JStmstRkbDpxuw1b1jDIhIgeREOPDwme zfpA== X-Gm-Message-State: AAQBX9fj1369Y0X9yjUtJT159s9Vwn+aYqTyYIebjDWb52LzJlnDgljS UYfLduMj47uGnCv0W2qaqUC/3/m+5UL+JH3OO0qbK0u5OHjWepuKr+QK3Leif+T1NjqKVhVa0xK +BcF+GSr/tYZezaGcWdSPxqUH7XZ0Jg08Jozq7Qtvrmy3nchcHlrT3aqoB4hhTY+dTVYuSV0= X-Google-Smtp-Source: AKy350YTwDNMNFMEMSlEf2un49G3Hi/s1Jg7Eo4F9wi9+MfJgOiKQO66YR3cMhFTxI1jfBmdVFHcBKboPBZ3 X-Received: from jstultz-noogler2.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:600]) (user=jstultz job=sendgmr) by 2002:a81:4320:0:b0:541:6941:5aa8 with SMTP id q32-20020a814320000000b0054169415aa8mr5051718ywa.7.1681187127553; Mon, 10 Apr 2023 21:25:27 -0700 (PDT) Date: Tue, 11 Apr 2023 04:25:02 +0000 In-Reply-To: <20230411042511.1606592-1-jstultz@google.com> Mime-Version: 1.0 References: <20230411042511.1606592-1-jstultz@google.com> X-Mailer: git-send-email 2.40.0.577.gac1e443424-goog Message-ID: <20230411042511.1606592-6-jstultz@google.com> Subject: [PATCH v3 05/14] locking/mutex: Add p->blocked_on wrappers From: John Stultz To: LKML Cc: Valentin Schneider , Joel Fernandes , Qais Yousef , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Mel Gorman , Daniel Bristot de Oliveira , Will Deacon , Waiman Long , Boqun Feng , "Paul E . McKenney" , "Connor O'Brien" , John Stultz Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Valentin Schneider This lets us assert p->blocked_lock is held whenever we access p->blocked_on. Cc: Joel Fernandes Cc: Qais Yousef Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Juri Lelli Cc: Vincent Guittot Cc: Dietmar Eggemann Cc: Valentin Schneider Cc: Steven Rostedt Cc: Ben Segall Cc: Zimuzo Ezeozue Cc: Mel Gorman Cc: Daniel Bristot de Oliveira Cc: Will Deacon Cc: Waiman Long Cc: Boqun Feng Cc: "Paul E . McKenney" Signed-off-by: Valentin Schneider [fix conflicts, call in more places] Signed-off-by: Connor O'Brien [jstultz: tweaked commit subject, added get_task_blocked_on() as well] Signed-off-by: John Stultz --- v2: * Added get_task_blocked_on() accessor --- include/linux/sched.h | 14 ++++++++++++++ kernel/locking/mutex-debug.c | 4 ++-- kernel/locking/mutex.c | 8 ++++---- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 2d736b6c44e9..9d46ca8ac221 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -2222,6 +2222,20 @@ static inline int rwlock_needbreak(rwlock_t *lock) #endif } =20 +static inline void set_task_blocked_on(struct task_struct *p, struct mutex= *m) +{ + lockdep_assert_held(&p->blocked_lock); + + p->blocked_on =3D m; +} + +static inline struct mutex *get_task_blocked_on(struct task_struct *p) +{ + lockdep_assert_held(&p->blocked_lock); + + return p->blocked_on; +} + static __always_inline bool need_resched(void) { return unlikely(tif_need_resched()); diff --git a/kernel/locking/mutex-debug.c b/kernel/locking/mutex-debug.c index 7228909c3e62..e3cd64ae6ea4 100644 --- a/kernel/locking/mutex-debug.c +++ b/kernel/locking/mutex-debug.c @@ -53,13 +53,13 @@ void debug_mutex_add_waiter(struct mutex *lock, struct = mutex_waiter *waiter, lockdep_assert_held(&lock->wait_lock); =20 /* Current thread can't be already blocked (since it's executing!) */ - DEBUG_LOCKS_WARN_ON(task->blocked_on); + DEBUG_LOCKS_WARN_ON(get_task_blocked_on(task)); } =20 void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *wa= iter, struct task_struct *task) { - struct mutex *blocked_on =3D READ_ONCE(task->blocked_on); + struct mutex *blocked_on =3D get_task_blocked_on(task); /*XXX jstultz: dr= opped READ_ONCE here, revisit.*/ =20 DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list)); DEBUG_LOCKS_WARN_ON(waiter->task !=3D task); diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 9cb2686fb974..45f1b7519f63 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -647,7 +647,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas goto err_early_kill; } =20 - current->blocked_on =3D lock; + set_task_blocked_on(current, lock); set_current_state(state); trace_contention_begin(lock, LCB_F_MUTEX); for (;;) { @@ -691,7 +691,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas /* * Gets reset by ttwu_runnable(). */ - current->blocked_on =3D lock; + set_task_blocked_on(current, lock); set_current_state(state); /* * Here we order against unlock; we must either see it change @@ -742,7 +742,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas debug_mutex_free_waiter(&waiter); =20 skip_wait: - current->blocked_on =3D NULL; + set_task_blocked_on(current, NULL); /* got the lock - cleanup and rejoice! */ lock_acquired(&lock->dep_map, ip); trace_contention_end(lock, 0); @@ -758,7 +758,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas return 0; =20 err: - current->blocked_on =3D NULL; + set_task_blocked_on(current, NULL); __set_current_state(TASK_RUNNING); __mutex_remove_waiter(lock, &waiter); err_early_kill: --=20 2.40.0.577.gac1e443424-goog