From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 19696606A31A for ; Tue, 1 Dec 2026 06:28:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=gUsTLCLD Received: by kanga.kvack.org (Postfix) id B9BF76B05B9; Tue, 1 Dec 2026 01:27:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B25336B05BA; Tue, 1 Dec 2026 01:27:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A14A56B05BB; Tue, 1 Dec 2026 01:27:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 809676B05B9 for ; Tue, 1 Dec 2026 01:27:50 -0500 (EST) Received: from smtpin09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 07833A8B67 for ; Tue, 1 Dec 2026 06:27:49 +0000 (UTC) X-FDA: 85494433458.09.4A90F4D Received: from mail-oa1-f69.google.com (mail-oa1-f69.google.com [209.85.160.69]) by imf23.hostedemail.com (Postfix) with ESMTP id 3DACC14000B for ; Tue, 1 Dec 2026 06:27:47 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20260707 header.b=gUsTLCLD; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 34WgOawkKCEw3s1uv8o1uu22uzs.q20zw18B-00y9oqy.25u@flex--penghuang.bounces.google.com designates 209.85.160.69 as permitted sender) smtp.mailfrom=34WgOawkKCEw3s1uv8o1uu22uzs.q20zw18B-00y9oqy.25u@flex--penghuang.bounces.google.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796106467; b=qLOE/xkK5H7V/tG1qkGSdgtOdGTkAGpVyBHIFh7NtqODXJHue+9cCtwwL2ThiP7XVyDG6v DhTQVRcUIxfB7Zd1P9IsP5hYMQduscwksujrs5W10FiKIlQtNkf6oAgB/XOcTwrYVW05Xl ew3oviOa7PGtJ3Rr0+xESrwGlB8lIy4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796106467; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ShqlAh8Gvagn6CSIEjoIGeGsHpN9mJcSn4aK745KtmY=; b=EDb+a60CATgvYjYUuNWBWFUXSQOQKXKZHcsC+Y9mquOcS8xRuttFypzCerelCfgfUcv1Av 4SGyJANfaH8aRwvadtIN59/4Mow+0fYh1TmLa5jQZKSkrjvYx5v/cPA6ToKWaWBoq/XfYf UlqZ1uNlnUmwOilWad5RX6vjkczxZVk= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20260707 header.b=gUsTLCLD; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 34WgOawkKCEw3s1uv8o1uu22uzs.q20zw18B-00y9oqy.25u@flex--penghuang.bounces.google.com designates 209.85.160.69 as permitted sender) smtp.mailfrom=34WgOawkKCEw3s1uv8o1uu22uzs.q20zw18B-00y9oqy.25u@flex--penghuang.bounces.google.com Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-45ecb9dd589so6045548fac.0 for ; Mon, 30 Nov 2026 22:27:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796106466; x=1796711266; darn=kvack.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ShqlAh8Gvagn6CSIEjoIGeGsHpN9mJcSn4aK745KtmY=; b=gUsTLCLDD/lfwjdFJJf75dLEfp/ho1ixz2ZmBBs0SpsYfTOOmLaZOBEaSkrH2EHRT+ PbJ3UJBhF/CEfy+Xf0521PDoCIvxOj7O/OmaPm8SiY8WexT1FNWJrKGvaVWrlkqKhvbw u/6zeijEH+gZqiYN28lwjZzUjGFlkKkolmvdpy+YKvPLicdQ6S3ETdMjIwUheI17EEz+ jdw1o6lAQDdDjEdp7RQppWGev9WaBLVUcizd6alUXZtZW9/jllw5pohsakXDvZpOdeqG bz1/BLohxtaOG7529DKz+MOjBgHkpwQu4foIOH6SH6wH03dpAEqgZa3mpIHr+ct4EInO K9bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796106466; x=1796711266; h=content-type: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:content-type; bh=ShqlAh8Gvagn6CSIEjoIGeGsHpN9mJcSn4aK745KtmY=; b=PwMh+T6YCqfEIphbnfXMnlpxSTGkCL1f5s9rGEzEZjjkEr9Cy6Ld3E5LJ8FqJtkEx0 Z+N9jO7CiowEBhJQlGuTXqXgoymI31nZCkmGGNsr4WOqeMy2fJuaxy4DrgWvx1JPpXZS KCbIUVxq/DMnLFa5y01JsDKwQ4YP34N0J3PdJKHhy1uf1iInu2TiE07Gi1ALIWU67Ba6 INggkxiKtdqwK69QDBi/8Td2oRzlPgJaIU4v7jAOjyvkc8Sjy1XrKulPzxL5kkZrsvgH /MJ2TjTfdpedm8hcMgnvcmLjAOL6SHzTjQmiPzzfdrrlt12Gf6zSOl0V+ILsXyxTYGFr mTGg== X-Forwarded-Encrypted: i=1; AL+hErEn32ZKCwF907Z8ofOTmWUBnMUVT8DgieuIpGkD4xC9fRMqUGNkoI1nxaaI4lZWHNKE/2k5WEYJVg==@kvack.org X-Gm-Message-State: APuU4Z9tR9qRt81CpoaJWnBl5p/S5NC9rDoKFbKTfh36/WvwqudlAnSG 7JcMykqlUJ/hEgu0DfHqUXu/VBFR5sWqaHIgwmI4zYt7T2WMcSUTMx3xDsCpa6isGPhC29mfftv L7j9FMUM6abB58kGhhg== X-Received: from oapv44.prod.google.com ([2002:a05:6870:b52c:b0:4af:ef90:ce2f]) (user=penghuang job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6871:5207:b0:4b3:fa1d:d8ec with SMTP id 586e51a60fabf-4d1fde8d6afmr12075617fac.24.1796106465701; Mon, 30 Nov 2026 22:27:45 -0800 (PST) Date: Mon, 30 Nov 2026 22:27:31 -0800 In-Reply-To: <20261201062738.1047159-1-penghuang@google.com> X-Archived-By: Mime-Version: 1.0 References: <20261201062738.1047159-1-penghuang@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261201062738.1047159-5-penghuang@google.com> Subject: [RFC PATCH v2 4/6] sched: Apply rwsem holder boost to the fair load weight From: Peng Huang To: Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng , Juri Lelli , Vincent Guittot Cc: Waiman Long , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak , John Stultz , Suren Baghdasaryan , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Peng Huang Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: pi1n5ww1qmrnyaa9hd3tjbfkcgr7ajp4 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 3DACC14000B X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DIET_1, DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS,USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796106467-609329 X-HE-Meta: U2FsdGVkX18f+xTiauRtmyQFM+/LcFSZsUW8clN+cZ9p2zOPEMYsaC2xOHGxgDppe2KpVrjleaHSxsaVVUsuy6vQ8V6jJ2hXscibBZiLBDH0q7UNgbaDahr1eCfcP8pwBL+fgwKpnUtpIXN5Djkb/+fIuO+XJbMbvjRjJpM/ljowVqxuo/u55DAbRsdZPMu3btktXea5GAcmFOJGBUPCahoI1uvx5qYDuK/fZsa86XC7ifO4lKfNCNQvc6Vc1IbZltbModozPYUAP0BmSvaVH4lHygV+a2mGyoQQ4vrsVHwLdErZrpod0WGySNJLCK3lGjXgOoZyu3c+diVHDOQP3Se0fW9kntmeKk8T1aVMfqGJv0lHTyRKKBFogEdODwnjwqmwrCDOMdRstqOEzsAcrWwNE56VrUWYlPLLWA2ePRkj2M7IjpTzrR9r+031ytBLxRBD+fADXrM+jgjPEnJrOsk1hwfxRKqT85PyJuhhwromS+snaCmYm5z0zpfVX4ofdyKcdTCd3rbas0USNdMqsIcrpqNz05lIQoLRY4O5qm4R7xQbGQTF0cD/G+19ktV7CIXlWys1FBHDhp0M/wwXOnJsKyxUJm16Gjz/djb066JRWKw0iqy9AjYZJ16+qZ0yAoZ6vsjqXVaiJO/3kpk0KxqMp4RIL2cMTIcU+vibf0gH4a8+BksFVmFjBzmb/7oQWgXiPw7sh0sY7y+n1ZrTP1S6WOCQ6kPYdsr2RUPseac4+RsuBQvsyU/ZFY7sQIj7HXGKJpiseqF5lms2ssY3vwSf3ovb4LsEm7vRiBxWB4gkRCeka4E+OSlby8UnAcl76GunFMpnclpMA6PF8wAEKD9CZTidOzZJbw/88549B0ToE8w3jDACgqcErK6QyNEkqBDU96uIlw92GgfH2My/LpjdahGeyRLHYY4NK3FTbC+thaiQg3SsAiMhDW+1ErrYXAoRGoDcbH0xBPV17ux OI46cwxW H0Lmr4rOEAaVxPr/1a24+WigQcyvEutcWHcjGqCakT43ulq17DkIRLc1fe0iEEV86scp81Domj5vZnWLR5PWligxng769OsWA573AbbWxO2Di4itXq6J4fPgQ+dsoZaFsHPy4ngVNB32MJ+YhnqZxbXFJH8g5+cQ0vRJY457X4oVmRNuY0gCvA3IdXlXnXF2JCICsa+FJhNJsIcbGLCMSq3QhijCtbiuOoNmQuGps93AEwxHv3d/vErySZAy0Pe2pV8DrdnNLLXCTpQ9G66iN5jzvv8Jtj7d1V12iDrbU5anFMgjU5Q7pvT8sFltMT53grv7S2fJjFgT3neatGF1JJuhQ4UERSD4i6DbrKV7Urm+hSv7R6XgDkLvkxdz9ETPlqyPZf+FTEQrG6bYqT3LTDA9OthZFYG+2PR3MPw6fM8G2Xn+1S6itWfB9rWprJ5+RTaeHZgF6DbMFOspqQSy5ut/r5tq1mEHvzliEVyos5+WwmYOUfz31pPlPCB3/w0T1epMcJ2HlFfc+GN/0ApSyofcFka/YXK+YUFBcKZMjp+6maiYy9OfoOKAdzTJTs4IRO+rUgaLWZvazm8nZ7MvxhcTKBkQfRoysvIpykMKbHpHdbr/zNeY77BMhiQ/GPMdpqPomPJNYCoTat1U9Ozf3HTDraGz+OVAARCRlM+5wQh8OMaASHa69v2xVizO6UB/DwVeg56wmGTVA9P0MF4aCxcys+aKEyydpVi6hNP2/z6lc3epUXCx5sQ+7yqrqN5HhlVa4JafjtHIanxhXouMYR7SAoA0rDj5M8hqGSmiBSYERdL8BebR0nJixKfhxyVs9auDFZQVngDEHuvOB/BkMOYJ2vmIqjfNfdxaq Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 The previous patch publishes how urgent an rwsem's waiters are, but a preempted holder still runs at its own weight and keeps them blocked. Add p->rwsem_boost.prio (MAX_PRIO when none) and feed it into set_load_weight(). p->prio, static_prio and rt_mutex PI are never touched: a boost only raises the fair load weight (capped at nice -20) and never leaves the fair class. rwsem_boost_calc_prio() scans the held slots via rwsem_top_waiter_prio() and boosts only when a waiter outranks the holder. On unlock, downgrade_write(), or a signal-interrupted read wait, a boosted task recomputes without locks and takes rq->lock only if its boost dropped, rescheduling if other fair tasks are runnable on its CPU. Leaving the fair class clears the boost; queued waiters also sample their own boost so it propagates across rwsem chains. The next patch adds the fair-class paths that apply the boost. Signed-off-by: Peng Huang --- include/linux/rwsem.h | 1 + include/linux/sched.h | 5 +++ init/init_task.c | 5 +++ kernel/locking/rwsem.c | 37 ++++++++++++++-- kernel/sched/core.c | 90 ++++++++++++++++++++++++++++++++++++++- kernel/sched/fair.c | 97 ++++++++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 42 ++++++++++++++++++ 7 files changed, 272 insertions(+), 5 deletions(-) diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h index d8a49170efd2..30dfa2920dfa 100644 --- a/include/linux/rwsem.h +++ b/include/linux/rwsem.h @@ -370,6 +370,7 @@ void up_read_non_owner(struct rw_semaphore *sem) __releases_shared(sem); #ifdef CONFIG_RWSEM_SCHED_BOOST void rwsem_boost_untrack(struct rw_semaphore *sem); bool rwsem_boost_set(bool enable); +void rwsem_boost_drop(void); #else static inline void rwsem_boost_untrack(struct rw_semaphore *sem) { diff --git a/include/linux/sched.h b/include/linux/sched.h index a98be7c4537d..92e32f58876e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -840,6 +840,7 @@ struct task_ipi_mask { }; /** * struct rwsem_sched_boost - per-task rw_semaphore boost state * @nr_held: number of non-NULL entries in @held + * @prio: fair priority lent by waiters on @held, MAX_PRIO if none * @held: rw_semaphores held, one slot per tracked acquisition * * Only the task itself writes @nr_held and @held, so the lock @@ -847,9 +848,13 @@ struct task_ipi_mask { }; * be granted (a blocking reader fills it before it queues) and cleared * before the rwsem is released or the wait is abandoned, so a * non-NULL slot always names an rwsem the task holds or waits for. + * + * @prio is written under rq->lock; the task itself also reads it + * locklessly. */ struct rwsem_sched_boost { u8 nr_held; + u8 prio; struct rw_semaphore *held[RWSEM_SCHED_BOOST_SLOTS]; }; #endif diff --git a/init/init_task.c b/init/init_task.c index ce7c2b07d855..214d3f2ef8a0 100644 --- a/init/init_task.c +++ b/init/init_task.c @@ -134,6 +134,11 @@ struct task_struct init_task __aligned(L1_CACHE_BYTES) = { .time_slice = RR_TIMESLICE, }, .tasks = LIST_HEAD_INIT(init_task.tasks), +#ifdef CONFIG_RWSEM_SCHED_BOOST + .rwsem_boost = { + .prio = MAX_PRIO, + }, +#endif #ifdef CONFIG_SMP .pushable_tasks = PLIST_NODE_INIT(init_task.pushable_tasks, MAX_PRIO), #endif diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index 6d9b78a3d33a..33fda82c2acc 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -104,7 +104,9 @@ static inline void rwsem_boost_track_trylock(struct rw_semaphore *sem) * another task can acquire or free @sem, scheduler inspection no * longer finds it. Unlike tracking, ignore rwsem_boost_key and * irqs_disabled() so no slot outlives its lock; an acquire that was - * not tracked can only end tracking early (losing boost). + * not tracked can only end tracking early (losing boost). Unlock + * paths drop the boost themselves after the wakeup; + * rwsem_boost_untrack() does both for callers that hand @sem over. */ static inline void __rwsem_boost_untrack(struct rw_semaphore *sem) { @@ -127,16 +129,32 @@ static inline void __rwsem_boost_untrack(struct rw_semaphore *sem) } } +static inline void rwsem_boost_maybe_drop(void) +{ + /* + * Check prio first so unboosted unlocks skip preempt_count. + * rwsem_boost_drop() takes rq->lock only if the boost drops, + * normally right after releasing the contended rwsem that set + * it, where up_*() can already take pi_lock and rq->lock via + * rwsem_wake(). + */ + if (unlikely(READ_ONCE(current->rwsem_boost.prio) < MAX_PRIO && + in_task())) + rwsem_boost_drop(); +} + /** * rwsem_boost_untrack - Stop tracking @sem as held by current * @sem: rwsem that current holds * * Call before another context can release @sem, e.g. an io_uring - * completion that runs in a different task. Task context only. + * completion that runs in a different task. This also drops any + * boost that @sem gave current. Task context only. */ void rwsem_boost_untrack(struct rw_semaphore *sem) { __rwsem_boost_untrack(sem); + rwsem_boost_maybe_drop(); } EXPORT_SYMBOL_GPL(rwsem_boost_untrack); #else @@ -151,6 +169,10 @@ static inline void rwsem_boost_track_trylock(struct rw_semaphore *sem) static inline void __rwsem_boost_untrack(struct rw_semaphore *sem) { } + +static inline void rwsem_boost_maybe_drop(void) +{ +} #endif #ifndef CONFIG_PREEMPT_RT @@ -499,7 +521,8 @@ enum rwsem_wake_type { /* * Sample current's priority for boost decisions before taking * wait_lock, so recalc never chases w->task. SCHED_IDLE ranks below - * nice 19 unless PI-boosted into RT, and nice -20 caps it to protect + * nice 19 unless PI-boosted into RT, a transitive rwsem boost counts + * without changing current->prio, and nice -20 caps it to protect * RT/DL. */ static inline void rwsem_waiter_init_prio(struct rwsem_waiter *waiter) @@ -513,6 +536,7 @@ static inline void rwsem_waiter_init_prio(struct rwsem_waiter *waiter) prio = READ_ONCE(current->prio); if (READ_ONCE(current->policy) == SCHED_IDLE && prio >= MAX_RT_PRIO) prio = MAX_PRIO; + prio = min_t(int, prio, READ_ONCE(current->rwsem_boost.prio)); waiter->boost_prio = max_t(int, prio, MAX_RT_PRIO); } @@ -1427,6 +1451,8 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat __rwsem_boost_untrack(sem); rwsem_del_wake_waiter(sem, &waiter, &wake_q); __set_current_state(TASK_RUNNING); + /* Undo any boost that the signal wakeup took from the slot. */ + rwsem_boost_maybe_drop(); lockevent_inc(rwsem_rlock_fail); trace_contention_end(sem, -EINTR); return ERR_PTR(-EINTR); @@ -1707,6 +1733,9 @@ static inline void __up_read(struct rw_semaphore *sem) rwsem_wake(sem); } preempt_enable(); + + /* Drop any boost after the wakeup, with preemption enabled. */ + rwsem_boost_maybe_drop(); } /* @@ -1733,6 +1762,7 @@ static inline void __up_write(struct rw_semaphore *sem) rwsem_wake(sem); } preempt_enable(); + rwsem_boost_maybe_drop(); } /* @@ -1759,6 +1789,7 @@ static inline void __downgrade_write(struct rw_semaphore *sem) rwsem_downgrade_wake(sem); } preempt_enable(); + rwsem_boost_maybe_drop(); } #else /* !CONFIG_PREEMPT_RT */ diff --git a/kernel/sched/core.c b/kernel/sched/core.c index ea89471d4743..56e32d4ffc3c 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -1562,12 +1563,96 @@ int tg_nop(struct task_group *tg, void *data) } #endif +#ifdef CONFIG_RWSEM_SCHED_BOOST +static inline void rwsem_boost_fork(struct task_struct *p) +{ + BUILD_BUG_ON(MAX_PRIO > U8_MAX); + + memset(&p->rwsem_boost, 0, sizeof(p->rwsem_boost)); + p->rwsem_boost.prio = MAX_PRIO; + + /* + * dup_task_struct() copied current->se.load without rq->lock; + * it may be boosted or torn. Recompute it so the child neither + * inherits the boost nor seeds its PELT load_avg from it. + */ + set_load_weight(p, false); +} + +/** + * rwsem_boost_calc_prio - Compute @p's rwsem boost priority + * @p: task whose held rwsems are inspected + * + * Callers must ensure @p cannot run elsewhere (see struct + * rwsem_sched_boost). + * + * Return: the lowest fair priority of any waiter on @p's held rwsems + * if it outranks @p, or MAX_PRIO otherwise. + */ +int rwsem_boost_calc_prio(struct task_struct *p) +{ + struct rwsem_sched_boost *b = &p->rwsem_boost; + int best = MAX_PRIO; + int i; + + if (!rwsem_boost_has_headroom(p)) + return MAX_PRIO; + + if (READ_ONCE(b->nr_held) == 0) + return MAX_PRIO; + + for (i = 0; i < RWSEM_SCHED_BOOST_SLOTS; i++) { + struct rw_semaphore *sem = READ_ONCE(b->held[i]); + int wprio; + + if (!sem) + continue; + wprio = rwsem_top_waiter_prio(sem); + if (wprio < best) + best = wprio; + } + + /* Cap at nice -20; boost only if the waiter outranks @p. */ + best = max_t(int, best, MAX_RT_PRIO); + return best < rwsem_boost_base_prio(p) ? best : MAX_PRIO; +} + +/** + * rwsem_boost_drop - Drop current's rwsem boost after releasing a lock + * + * Only current writes its held[] and it still holds every rwsem listed + * there, so the boost can be recomputed without rq->lock. Take the + * lock only if the boost dropped: releasing an rwsem that did not set + * it changes nothing, and upgrades are left to enqueue, preemption and + * the tick. + */ +void rwsem_boost_drop(void) +{ + struct task_struct *p = current; + struct rq_flags rf; + struct rq *rq; + + if (rwsem_boost_calc_prio(p) <= READ_ONCE(p->rwsem_boost.prio)) + return; + + rq = task_rq_lock(p, &rf); + if (p->sched_class == &fair_sched_class) { + update_rq_clock(rq); + rwsem_boost_drop_fair(rq, p); + } + task_rq_unlock(rq, p, &rf); +} +#else +static inline void rwsem_boost_fork(struct task_struct *p) { } +#endif + void set_load_weight(struct task_struct *p, bool update_load) { - int prio = p->static_prio - MAX_RT_PRIO; + int prio = rwsem_boost_effective_prio(p) - MAX_RT_PRIO; struct load_weight lw; - if (task_has_idle_policy(p)) { + /* Boosted SCHED_IDLE holders use normal nice weights. */ + if (task_has_idle_policy(p) && !rwsem_boost_active(p)) { lw.weight = scale_load(WEIGHT_IDLEPRIO); lw.inv_weight = WMULT_IDLEPRIO; } else { @@ -4916,6 +5001,7 @@ int sched_fork(u64 clone_flags, struct task_struct *p) * Make sure we do not leak PI boosting priority to the child. */ p->prio = current->normal_prio; + rwsem_boost_fork(p); uclamp_fork(p); diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 2d9d3d686261..f755ee810ce2 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -8199,6 +8199,102 @@ static inline void update_curr_eevdf(struct cfs_rq *cfs_rq) update_curr(cfs_rq_of(cfs_rq->curr)); } +#ifdef CONFIG_RWSEM_SCHED_BOOST +/* + * Recompute fair task @p's load weight from its held rwsems without + * touching p->prio, so rt_mutex PI and syscall priorities stay + * orthogonal. Callers hold @rq->lock with @p attached to cfs_rq->avg + * and not running elsewhere (see struct rwsem_sched_boost). + */ +static bool rwsem_boost_reweight_fair(struct rq *rq, struct task_struct *p) +{ + struct sched_entity *se = &p->se; + int new_boost; + + lockdep_assert_rq_held(rq); + + new_boost = rwsem_boost_calc_prio(p); + if (new_boost == p->rwsem_boost.prio) + return false; + + /* Sync @se and its cfs_rq PELT before reweight_entity(). */ + update_load_avg(cfs_rq_of(se), se, 0); + WRITE_ONCE(p->rwsem_boost.prio, new_boost); + set_load_weight(p, true); + return true; +} + +/* + * Clear @p's rwsem boost when it leaves fair_sched_class, so unlocking + * outside CFS never leaves a stale weight behind. This runs before + * detach_task_cfs_rq(), so handle PELT like detach_entity_cfs_rq(). + */ +static void rwsem_boost_clear_fair(struct rq *rq, struct task_struct *p) +{ + struct sched_entity *se = &p->se; + + lockdep_assert_rq_held(rq); + + if (!rwsem_boost_active(p)) + return; + + /* Wakeup still pending: no cfs_rq holds this load yet. */ + if (!se->avg.last_update_time) { + WRITE_ONCE(p->rwsem_boost.prio, MAX_PRIO); + set_load_weight(p, false); + return; + } + /* Sync @se and its cfs_rq PELT before reweight_entity(). */ + update_load_avg(cfs_rq_of(se), se, 0); + WRITE_ONCE(p->rwsem_boost.prio, MAX_PRIO); + set_load_weight(p, true); +} + +/* + * Cancel @curr's slice protection and resched it. Not lazy: a boost + * change, like a priority change in prio_changed_fair(), should not + * wait for the next tick. + */ +static void rwsem_boost_resched_fair(struct rq *rq, struct task_struct *curr) +{ + cancel_protect_slice(&curr->se); + resched_curr(rq); +} + +/* True if running @p lost weight while other fair tasks wait. */ +static inline bool rwsem_boost_should_resched_fair(struct rq *rq, + struct task_struct *p, + int old_prio) +{ + return rwsem_boost_effective_prio(p) > old_prio && + task_current_donor(rq, p) && rq->cfs.h_nr_queued > 1; +} + +/* + * Refresh running @p's boost. Return true if it lost the boost while + * other fair tasks wait, so the caller should reschedule. + */ +static bool rwsem_boost_refresh_curr_fair(struct rq *rq, + struct task_struct *p) +{ + int old_prio = rwsem_boost_effective_prio(p); + + if (!rwsem_boost_reweight_fair(rq, p)) + return false; + return rwsem_boost_should_resched_fair(rq, p, old_prio); +} + +/* Refresh @p's boost weight after rwsem unlock or downgrade_write(). */ +void rwsem_boost_drop_fair(struct rq *rq, struct task_struct *p) +{ + if (rwsem_boost_refresh_curr_fair(rq, p)) + rwsem_boost_resched_fair(rq, p); +} +#else +static inline void rwsem_boost_clear_fair(struct rq *rq, + struct task_struct *p) { } +#endif + /* * The enqueue_task method is called before nr_running is * increased. Here we update the fair scheduling stats and @@ -15538,6 +15634,7 @@ static void switching_from_fair(struct rq *rq, struct task_struct *p) static void switched_from_fair(struct rq *rq, struct task_struct *p) { + rwsem_boost_clear_fair(rq, p); detach_task_cfs_rq(p); } diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 32f76551a0c5..058311c19cc0 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -2848,6 +2848,7 @@ struct sched_class { /* * set_load_weight: task_rq_lock + sched_change * __setscheduler_parms: task_rq_lock + sched_change + * rwsem boost (fair): rq->lock, @task may be queued or running */ void (*reweight_task)(struct rq *this_rq, struct task_struct *task, const struct load_weight *lw); @@ -4420,4 +4421,45 @@ void sched_push_current_non_preferred_cpu(struct rq *rq); static inline void sched_push_current_non_preferred_cpu(struct rq *rq) { } #endif +#ifdef CONFIG_RWSEM_SCHED_BOOST +static inline bool rwsem_boost_active(const struct task_struct *p) +{ + return p->rwsem_boost.prio < MAX_PRIO; +} + +/* + * Base CFS prio for boost decisions. SCHED_IDLE tasks keep + * static_prio == NICE_TO_PRIO(0) but rank below nice 19. + */ +static inline int rwsem_boost_base_prio(const struct task_struct *p) +{ + return idle_policy(p->policy) ? MAX_PRIO : p->static_prio; +} + +/* True if @p is below nice -20 and can still be boosted. */ +static inline bool rwsem_boost_has_headroom(const struct task_struct *p) +{ + return rwsem_boost_base_prio(p) > MAX_RT_PRIO; +} + +/* Effective fair prio; MAX_PRIO for an unboosted SCHED_IDLE task. */ +static inline int rwsem_boost_effective_prio(const struct task_struct *p) +{ + return min_t(int, rwsem_boost_base_prio(p), p->rwsem_boost.prio); +} + +int rwsem_boost_calc_prio(struct task_struct *p); +void rwsem_boost_drop_fair(struct rq *rq, struct task_struct *p); +#else +static inline bool rwsem_boost_active(const struct task_struct *p) +{ + return false; +} + +static inline int rwsem_boost_effective_prio(const struct task_struct *p) +{ + return p->static_prio; +} +#endif + #endif /* _KERNEL_SCHED_SCHED_H */ -- 2.56.1.839.g22a8290db9-goog