From patchwork Mon Mar 20 23:37:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 72532 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp1493303wrt; Mon, 20 Mar 2023 16:42:28 -0700 (PDT) X-Google-Smtp-Source: AK7set9lR9pJ3hYY8HHi9Tn13SAsfYkyZAj1saXTSEbaCVHD5kbCuNn6c5G+8rsXJkryUbYXoQCt X-Received: by 2002:a17:902:ecd0:b0:19e:23c1:4c2e with SMTP id a16-20020a170902ecd000b0019e23c14c2emr180936plh.59.1679355748301; Mon, 20 Mar 2023 16:42:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679355748; cv=none; d=google.com; s=arc-20160816; b=KiJPsE6VGl5hrlyjcLfaaLPr8AnAUvsrLXCyQSRdWuTZ9cUi/xSnaW2dcJcNoKjzNi pFQv+BBBnZtQ8Zii2GWGhYvO6Woof4NR4dSPSAk9YHiIEAUf7cPkw/vTK0JOtRcr9mMD QOmGrK/mlVDAxoLtkok1c3yfcQCp+iumU3DMEpJbbshj/c1QgGSNRt1zNbki1HS8avxp B4pyv8XmQXIN9hBuEMrwjfBl7gK3ivvpfcqcNv8Oyu4ZTjuX8xavF1hzLuYgrqvpxmF/ 9pc0isFOe6JFbfw6Gtq9qtVwGXBo/dbyVG30/1qb4383z0ht1d2vGvzkPH4lO8uzdWNu NKzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=cMj53Ovo44UMn5m6zz+VUE8eJafwWOHMtJt0fmXwh1Q=; b=HYNScYbY5nvP+Wiv24c1NF6NfuTJ6LI2sblEdllVqQho8W4EdPTQ5GWlFtpnp0vOVh O+hns6aIqZHd3Wbs4NgpI1SCxXXak5hC2DE9MzXLdsCeeAOA3Q5wJBh67p7wHpf7chri kFfETkR9EFsPK84vTKVd0jtFoBJhixFRWngLf2sXDSFVHaSgt3P9Lf4mont0gGBCjHaG rhtiI10XzEjW7J67+9DbTNLN/xXD6/EENoDe6dFTL3Vl86c7g+6EZQpUr8olJEeUZ/Mi /cwnQevKSdyXqYDeRCdpMrtS9JEOOdry+/zimwZSKymACb2c3W87zgnrgtJ0tvZtqZM8 P9kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="FQri1/Sz"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y3-20020a1709027c8300b001a071f98c39si11795745pll.166.2023.03.20.16.42.15; Mon, 20 Mar 2023 16:42:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="FQri1/Sz"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229832AbjCTXhs (ORCPT + 99 others); Mon, 20 Mar 2023 19:37:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229757AbjCTXhj (ORCPT ); Mon, 20 Mar 2023 19:37:39 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2DFE31E38 for ; Mon, 20 Mar 2023 16:37:36 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id x4-20020a170902ec8400b001a1a5f6f272so6470275plg.1 for ; Mon, 20 Mar 2023 16:37:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679355456; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=cMj53Ovo44UMn5m6zz+VUE8eJafwWOHMtJt0fmXwh1Q=; b=FQri1/SzY9gLklv6+qN60xKrrefTsyZ6OtdQIFK6RF0aboHB7uqGPJy0dgEkyOdWnq +0FAHFif8sofWS6mrTOgZSG1uFg9qj0XBlM+7jw6manM67omYzvKkbhb2Y6nYo+2NlV3 2VBgkAoc3GPEdOjmNu3ghVW+rWr1YfUU3qcBMSzzcynTXqN2sAaqMxy6RhxWn9RgglFR QU1jG1edWO1EcSz41LRfC0NH4C68f6OiPLT9LwRV4AViwKSUo5F4suPQP95I/ktdbbRl 5o9YuSnr+C79AgheV6l+6z85hqyS58y80GjZyggaLq0weN6FltVKT47q+MhonBBWGiZy haGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679355456; 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=cMj53Ovo44UMn5m6zz+VUE8eJafwWOHMtJt0fmXwh1Q=; b=1Mpjp5yiugxCd5PPTsbo7+YqAbJGlgON45fOXi7Kar1/EBwzY5tkKXVW954xfolZad 87IeVYPXloYdMvV8G8Vt2QgD8p20l/Oq0AS4gvrIJqy7CNeHthsyIsuqqk63Me8teD/d sBfb2oQj1TUm/3LlmArPkZFCd3wOeBX9AimmIv51GQkp8D8YR0/Vz7sXSok/6ewBJ81O wUhumRjjM9cSmucCQX0EqxfRwknq3iBcjGeWbZb+HKjKMvWKbucp9gbNjL94nFZTZ1TF OZ7YicVsFrtg1dQ1IGpcpaFnLP4+NFYh/9Y5RVZWCtKeNfpfBueLcUb4nGRwFsrPlxWl jQtA== X-Gm-Message-State: AO0yUKWjWUS4NYUa5aQiKA0Fp8ISDVzQSvxgZE5oEebgipKHrYHVwXdz XGCDw+OjYTN88P8K1p70XpLos74lNqXc+OeF7NAZ6G8MffOd351edNw02oTHwSHDuuJmADI3rjx OtFWCwfzgcRKSR1rBYI8HMYCYcHAYGJoYd5UOOpKOsjAgNbxa57csnUfnA1go5YMYKloXc9U= X-Received: from jstultz-noogler2.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:600]) (user=jstultz job=sendgmr) by 2002:a65:4308:0:b0:509:3be7:eac9 with SMTP id j8-20020a654308000000b005093be7eac9mr132895pgq.0.1679355456284; Mon, 20 Mar 2023 16:37:36 -0700 (PDT) Date: Mon, 20 Mar 2023 23:37:11 +0000 In-Reply-To: <20230320233720.3488453-1-jstultz@google.com> Mime-Version: 1.0 References: <20230320233720.3488453-1-jstultz@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230320233720.3488453-4-jstultz@google.com> Subject: [PATCH v2 03/12] locking/mutex: Add task_struct::blocked_lock to serialize changes to the blocked_on state From: John Stultz To: LKML Cc: Peter Zijlstra , Joel Fernandes , Qais Yousef , Ingo Molnar , 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" , kernel-team@android.com, Valentin Schneider , "Connor O'Brien" , John Stultz X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760932133000494744?= X-GMAIL-MSGID: =?utf-8?q?1760932133000494744?= From: Peter Zijlstra This patch was split out from the later "sched: Add proxy execution" patch. Adds blocked_lock to the task_struct so we can safely keep track of which tasks are blocked on us. This will be used for tracking blocked-task/mutex chains with the prox-execution patch in a similar fashion to how priority inheritence is done with rt_mutexes. 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" Cc: kernel-team@android.com Signed-off-by: Peter Zijlstra (Intel) [rebased, added comments and changelog] Signed-off-by: Juri Lelli [Fixed rebase conflicts] [squashed sched: Ensure blocked_on is always guarded by blocked_lock] Signed-off-by: Valentin Schneider [fix rebase conflicts, various fixes & tweaks commented inline] [squashed sched: Use rq->curr vs rq->proxy checks] Signed-off-by: Connor O'Brien [jstultz: Split out from bigger patch] Signed-off-by: John Stultz --- v2: * Split out into its own patch TODO: Still need to clarify some of the locking changes here --- include/linux/sched.h | 1 + init/init_task.c | 1 + kernel/fork.c | 1 + kernel/locking/mutex.c | 27 +++++++++++++++++++++++---- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 9924d1926bc3..031615b5dc2a 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1141,6 +1141,7 @@ struct task_struct { struct task_struct *blocked_proxy; /* task that is boosting us */ struct mutex *blocked_on; /* lock we're blocked on */ + raw_spinlock_t blocked_lock; #ifdef CONFIG_DEBUG_ATOMIC_SLEEP int non_block_count; diff --git a/init/init_task.c b/init/init_task.c index ff6c4b9bfe6b..189ce67e9704 100644 --- a/init/init_task.c +++ b/init/init_task.c @@ -130,6 +130,7 @@ struct task_struct init_task .journal_info = NULL, INIT_CPU_TIMERS(init_task) .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(init_task.pi_lock), + .blocked_lock = __RAW_SPIN_LOCK_UNLOCKED(init_task.blocked_lock), .timer_slack_ns = 50000, /* 50 usec default slack */ .thread_pid = &init_struct_pid, .thread_group = LIST_HEAD_INIT(init_task.thread_group), diff --git a/kernel/fork.c b/kernel/fork.c index ad27fa09fe70..95410333332f 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2121,6 +2121,7 @@ static __latent_entropy struct task_struct *copy_process( ftrace_graph_init_task(p); rt_mutex_init_task(p); + raw_spin_lock_init(&p->blocked_lock); lockdep_assert_irqs_enabled(); #ifdef CONFIG_PROVE_LOCKING diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index f5296aa82255..2f31ebb08b4a 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -615,6 +615,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas } raw_spin_lock(&lock->wait_lock); + raw_spin_lock(¤t->blocked_lock); /* * After waiting to acquire the wait_lock, try again. */ @@ -676,6 +677,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas goto err; } + raw_spin_unlock(¤t->blocked_lock); raw_spin_unlock(&lock->wait_lock); if (ww_ctx) ww_ctx_wake(ww_ctx); @@ -683,6 +685,8 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas first = __mutex_waiter_is_first(lock, &waiter); + raw_spin_lock(&lock->wait_lock); + raw_spin_lock(¤t->blocked_lock); /* * Gets reset by ttwu_runnable(). */ @@ -697,15 +701,28 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas break; if (first) { + bool acquired; + + /* + * XXX connoro: mutex_optimistic_spin() can schedule, so + * we need to release these locks before calling it. + * This needs refactoring though b/c currently we take + * the locks earlier than necessary when proxy exec is + * disabled and release them unnecessarily when it's + * enabled. At a minimum, need to verify that releasing + * blocked_lock here doesn't create any races. + */ + raw_spin_unlock(¤t->blocked_lock); + raw_spin_unlock(&lock->wait_lock); trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN); - if (mutex_optimistic_spin(lock, ww_ctx, &waiter)) + acquired = mutex_optimistic_spin(lock, ww_ctx, &waiter); + raw_spin_lock(&lock->wait_lock); + raw_spin_lock(¤t->blocked_lock); + if (acquired) break; trace_contention_begin(lock, LCB_F_MUTEX); } - - raw_spin_lock(&lock->wait_lock); } - raw_spin_lock(&lock->wait_lock); acquired: __set_current_state(TASK_RUNNING); @@ -732,6 +749,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas if (ww_ctx) ww_mutex_lock_acquired(ww, ww_ctx); + raw_spin_unlock(¤t->blocked_lock); raw_spin_unlock(&lock->wait_lock); if (ww_ctx) ww_ctx_wake(ww_ctx); @@ -744,6 +762,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas __mutex_remove_waiter(lock, &waiter); err_early_kill: trace_contention_end(lock, ret); + raw_spin_unlock(¤t->blocked_lock); raw_spin_unlock(&lock->wait_lock); debug_mutex_free_waiter(&waiter); mutex_release(&lock->dep_map, ip);