From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796106469; b=xZkPP8vVTdFZ9qkeaU9TI4BlaNRs6B6WIkdI3pXX8qHMAsOZaSKq7dqzZj1wVP2aiAhL xI/DvS6kbJn74Tg4Slgb35SZ/4vmxNW+g2eqrg4ZLaOrtPF5EXxodZ+RhmzAkDbcIxXhS WFign7j1nQn0f46Q8XAXKwwC9RhZOKk7WuYexoTUBjK03YJHQguLPk6ZElnYR6xmwGAYI AutZktFEeS10z2YPI+C5BeFbslo4x9Lq6vALCjXGD7ZoDKwZEyS1mfvLKMqVQmkiYgnV7 R9iWPQQOSY8ZSDo8bbSDhI9RDenku8gNmA7UXJo/YuC/XnquW+Dz1x0ykBYXFnZYVew== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796106469; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=bxP8VjQhn9R1tzvrMdkalEOm6ZCMcP7HPDzvHkdPIkg=; b=QGxNZC1XdK9SDQAuzT+q6yqjHJVS9L6kZwpDCYcR2zauKNX7YZsPfddT0m7S7TNsIgaN jWFrX5nNLYbEVuAUhe8h5sxYqSgg6d4EZiXy+L36hAV6bpVFxwaGe1HARAqI9YH/datnc xS2T8ZHo8YQjonfW1Ij8/C5PTFmu32m4H+jNlZc9dF47IEvHX3gFa2RqTRO2wzy0Tj1Y6 ceiX2fBUvCwHSTWInXBmO/oTnWG6mWuw3kh7YpgYTSNkliU2YcH+5nqrqcumnqTxKKOhw /lVWwJa62wLQwN9VNEKSQppYAPNHJeJyMLwxEXLCZ0yg6UCMKB8OtP5t8InkM4FoP3Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--penghuang.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Y7OkklWI; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 842E2606A331 for ; Tue, 1 Dec 2026 06:27:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 842E2606A331 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--penghuang.bounces.google.com 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=Y7OkklWI Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-33713e5e6daso16936022eec.0 for ; Mon, 30 Nov 2026 22:27:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796106468; x=1796711268; darn=vger.kernel.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=bxP8VjQhn9R1tzvrMdkalEOm6ZCMcP7HPDzvHkdPIkg=; b=Y7OkklWI6HNuRERiejKolZvKfYMuCgaSOAPV7x7+c5EIegKh0enmZncwq90nU6QPCc kLVoucM2sbG7y3oorB28U7ttRRZ1xGtFcZnU4Q5JBIxkLREb8SpYpDYvKnW+i2jus7i5 c9QaQecLrv1SHDhg3pxJ392muw9ZiQuqtDGagpyE90qt5He98kwSpeurpWowmH9gm5fR 0/t8K7d/Sk9gkPJwO7cN50nYPMIDokCt9C+j0DGQHUm5PseZz7f2lZWC9p+rqMitpRxb Ts0lT/bC+lBTwLV6uTqWmG2AGVgbhm1tpuTHH6Aw4nr1o4uAT0f8jY/UnNV5GCdsCBT4 /Czg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796106468; x=1796711268; 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=bxP8VjQhn9R1tzvrMdkalEOm6ZCMcP7HPDzvHkdPIkg=; b=Ilu5yhIcIxqoOX0dXSbh56oFXJO771k0VNVLAosJg3/xuZ7cUIlqHG/UDd2zdbU1kz 2igmUveNDejjj766rCF0Tb3Z6lVcergj6t63Y4Jkk6WFmthmFYrL3ssn9bc1YEJAtHYH WhBbHSNi1Aaded///okOMV9MS+jUinSldAvHAaYu3UQ5096ICLdZu0HWXbEj/mmdR2LV f1wpxlLb4LwpsWbHohfzwKqaskAjNd7yOzJo7XD3a8lJxoEX5PzvjLW7u+N0T4mmTX0l w0AX8Yf6vDLXI9Ih6IaTdUSCw4KcUxm6LUIu6XJ+Rg/6vguPJm6TqbmEj1DTVu6g+MKQ /x3Q== X-Forwarded-Encrypted: i=1; AL+hErH00HZAXey7nuvd9yIGJb/qjP3aGzIlw6m1IJ1zl0pbNsuCcwfPdZ8P/c6nSPbiyoW+M3T3qemL/G6v6no=@vger.kernel.org X-Gm-Message-State: APuU4Z9fY47fiXYpY9GtIniQpOHJaynsnN48PzwhqFs96Hk5/pje6GES acLc1bxi4LpASyVdlt/TG/LjUoZGakeXWsYObuiMw8/k6MLtL3L/T3V1AQ0iCDgCkCG/lbfolPx BmYajyDby4sg7K/BlmQ== X-Received: from dlbpu1.prod.google.com ([2002:a05:7022:e881:b0:1ad:2ffe:151b]) (user=penghuang job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:240d:b0:1b5:a00c:b733 with SMTP id a92af1059eb24-1b5a00cbbaamr9224885c88.36.1796106467415; Mon, 30 Nov 2026 22:27:47 -0800 (PST) Date: Mon, 30 Nov 2026 22:27:32 -0800 In-Reply-To: <20261201062738.1047159-1-penghuang@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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-6-penghuang@google.com> Subject: [RFC PATCH v2 5/6] sched/fair: Boost rwsem holders with contended high-priority waiters 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" A preempted fair task holding an rwsem keeps higher-priority waiters blocked until it runs again, and for readers the waiters cannot even tell whom to boost. Track runnable fair holders on per-CPU rq->rwsem_holders when enqueued or preempted, and refresh their boost then and at each tick, all within fair.c so uncontended lock paths never touch the list. A tick that raises a queued holder's boost reschedules current if the holder now outranks it and is eligible. Another task's held[] is read only under its rq->lock while it is not running elsewhere, so its rwsems cannot be released meanwhile. Holders unlink on any dequeue (including delayed dequeue, throttling, and class switches). Tasks entering fair, mutex-blocked Proxy Execution donors, and remotely running tasks are linked without reweighting so the tick boosts them once runnable. Under Proxy Execution, put_prev_task() also links a fair execution context when its donor is fair; under a non-fair donor, it waits for its next enqueue or preemption. Each tick scans at most 16 queued holders (rotating the list for fairness) to keep rq->lock work bounded, so a queued holder normally waits up to one tick. Signed-off-by: Peng Huang --- include/linux/sched.h | 8 +- init/init_task.c | 1 + kernel/sched/core.c | 2 + kernel/sched/fair.c | 229 ++++++++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 17 ++++ 5 files changed, 255 insertions(+), 2 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 92e32f58876e..8b2889ce7952 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -842,6 +842,7 @@ struct task_ipi_mask { }; * @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 + * @rq_node: entry on rq->rwsem_holders, protected by rq->lock * * Only the task itself writes @nr_held and @held, so the lock * fastpaths need no atomics. A slot is filled before the rwsem can @@ -849,13 +850,16 @@ struct task_ipi_mask { }; * 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. + * Other tasks read @held only under the task's rq->lock while it is + * not running, so none of those rwsems can be released or freed + * meanwhile. @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]; + struct list_head rq_node; }; #endif diff --git a/init/init_task.c b/init/init_task.c index 214d3f2ef8a0..507540e50657 100644 --- a/init/init_task.c +++ b/init/init_task.c @@ -137,6 +137,7 @@ struct task_struct init_task __aligned(L1_CACHE_BYTES) = { #ifdef CONFIG_RWSEM_SCHED_BOOST .rwsem_boost = { .prio = MAX_PRIO, + .rq_node = LIST_HEAD_INIT(init_task.rwsem_boost.rq_node), }, #endif #ifdef CONFIG_SMP diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 56e32d4ffc3c..288a8efcfb9d 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1570,6 +1570,7 @@ static inline void rwsem_boost_fork(struct task_struct *p) memset(&p->rwsem_boost, 0, sizeof(p->rwsem_boost)); p->rwsem_boost.prio = MAX_PRIO; + INIT_LIST_HEAD(&p->rwsem_boost.rq_node); /* * dup_task_struct() copied current->se.load without rq->lock; @@ -9168,6 +9169,7 @@ void __init sched_init(void) rq->max_idle_balance_cost = sysctl_sched_migration_cost; INIT_LIST_HEAD(&rq->cfs_tasks); + rwsem_boost_rq_init(rq); rq_attach_root(rq, &def_root_domain); #ifdef CONFIG_NO_HZ_COMMON diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index f755ee810ce2..08fbab673893 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -8270,6 +8270,27 @@ static inline bool rwsem_boost_should_resched_fair(struct rq *rq, task_current_donor(rq, p) && rq->cfs.h_nr_queued > 1; } +/* Link runnable rwsem holder @p onto rq->rwsem_holders. */ +static inline void rwsem_boost_rq_add(struct rq *rq, struct task_struct *p) +{ + if (list_empty(&p->rwsem_boost.rq_node)) + list_add_tail(&p->rwsem_boost.rq_node, &rq->rwsem_holders); +} + +/* Unlink @p from rq->rwsem_holders when leaving the runqueue. */ +static inline void rwsem_boost_rq_del(struct task_struct *p) +{ + if (!list_empty(&p->rwsem_boost.rq_node)) + list_del_init(&p->rwsem_boost.rq_node); +} + +/* True if @p holds an rwsem and could still be boosted by the tick. */ +static inline bool rwsem_boost_needs_scan(const struct task_struct *p) +{ + return READ_ONCE(p->rwsem_boost.nr_held) != 0 && + rwsem_boost_has_headroom(p); +} + /* * Refresh running @p's boost. Return true if it lost the boost while * other fair tasks wait, so the caller should reschedule. @@ -8279,6 +8300,8 @@ static bool rwsem_boost_refresh_curr_fair(struct rq *rq, { int old_prio = rwsem_boost_effective_prio(p); + if (!rwsem_boost_needs_scan(p)) + rwsem_boost_rq_del(p); if (!rwsem_boost_reweight_fair(rq, p)) return false; return rwsem_boost_should_resched_fair(rq, p, old_prio); @@ -8290,9 +8313,206 @@ 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); } + +/* True if @p holds any rwsem or still carries an active boost. */ +static inline bool rwsem_boost_holding_or_active(const struct task_struct *p) +{ + return unlikely(READ_ONCE(p->rwsem_boost.nr_held) != 0 || + rwsem_boost_active(p)); +} + +/* + * True if @p is runnable on_rq, not sleeping-delayed, not proxy-blocked + * on a mutex, and not running remotely. Test se.on_rq rather than + * task_on_rq_queued(): sched_change_begin() dequeues the task before + * put_prev_task() without clearing p->on_rq. + */ +static inline bool rwsem_boost_can_inspect_fair(struct rq *rq, + struct task_struct *p) +{ + if (!p->se.on_rq) + return false; + if (p->se.sched_delayed) + return false; + if (task_is_blocked(p)) + return false; + if (p != current && task_on_cpu(rq, p)) + return false; + return true; +} + +/* + * Link queued holder @p onto rq->rwsem_holders without reweighting it: + * a class switch before attach_task_cfs_rq(), a mutex-blocked proxy + * donor, or a proxy execution context that may run elsewhere. Reads + * only @p's nr_held, never held[]. + */ +static inline void rwsem_boost_link_fair(struct rq *rq, struct task_struct *p) +{ + if (!p->se.on_rq) + return; + if (p->se.sched_delayed) + return; + if (!rwsem_boost_needs_scan(p)) + return; + rwsem_boost_rq_add(rq, p); +} + +/* Sync @p on rq->rwsem_holders and refresh its boosted load weight. */ +static bool rwsem_boost_update_fair(struct rq *rq, struct task_struct *p) +{ + if (rwsem_boost_needs_scan(p)) + rwsem_boost_rq_add(rq, p); + else + rwsem_boost_rq_del(p); + return rwsem_boost_reweight_fair(rq, p); +} + +/* True if boosted, queued fair task @p should preempt @curr. */ +static inline bool +rwsem_boost_should_preempt_fair(struct rq *rq, struct task_struct *p, + const struct task_struct *curr) +{ + if (p->rwsem_boost.prio >= rwsem_boost_effective_prio(curr)) + return false; + /* Only task entities are on the EEVDF timeline, &rq->cfs. */ + return entity_eligible(&rq->cfs, &p->se); +} + +/* + * Track held-rwsem task @p on enqueue and refresh its load weight; + * called at the end of enqueue_task_fair(), once PELT and a delayed + * requeue are settled. + */ +static inline void rwsem_boost_enqueue_fair(struct rq *rq, + struct task_struct *p, + int flags) +{ + if (!rwsem_boost_holding_or_active(p)) + return; + + /* + * A class switch attaches PELT only in switched_to_fair(): link + * the task now and let the tick apply the boost. + */ + if (flags & ENQUEUE_CLASS) { + rwsem_boost_link_fair(rq, p); + return; + } + /* + * Link a mutex-blocked proxy donor or remotely running task + * without reading held[], so the tick catches it once runnable. + */ + if (!rwsem_boost_can_inspect_fair(rq, p)) { + rwsem_boost_link_fair(rq, p); + return; + } + + rwsem_boost_update_fair(rq, p); +} + +/* + * Under PROXY_EXEC with a fair donor, @prev is the donor and rq->curr + * ran on its behalf. Link that execution context if it took an rwsem + * while running, so the tick can boost it once it waits for the CPU. + * Non-fair donors dispatch put_prev_task() through their own class, so + * a fair proxy context waits for its next enqueue or own preemption. + */ +static inline void rwsem_boost_put_proxy_fair(struct rq *rq, + struct task_struct *prev) +{ + struct task_struct *exec = rq->curr; + + if (!sched_proxy_exec()) + return; + if (exec == prev) + return; + if (exec->sched_class != &fair_sched_class) + return; + rwsem_boost_link_fair(rq, exec); +} + +/* + * Track a preempted task that acquired an rwsem while running (so + * enqueue_task_fair() never saw nr_held > 0) and apply any boost. + */ +static inline void rwsem_boost_put_prev_fair(struct rq *rq, + struct task_struct *prev) +{ + rwsem_boost_put_proxy_fair(rq, prev); + + if (!rwsem_boost_holding_or_active(prev)) + return; + /* + * A PROXY_EXEC mutex-blocked task stays on_rq: link it without + * reweighting, so the tick sees it after an in-place wakeup. + */ + if (task_is_blocked(prev)) { + rwsem_boost_link_fair(rq, prev); + return; + } + if (rwsem_boost_can_inspect_fair(rq, prev)) + rwsem_boost_update_fair(rq, prev); +} + +#define RWSEM_BOOST_TICK_BUDGET 16 + +/* Refresh boost for running @curr and queued rwsem holders on tick. */ +static void rwsem_boost_tick_fair(struct rq *rq, struct task_struct *curr) +{ + int budget = RWSEM_BOOST_TICK_BUDGET; + struct task_struct *p, *next; + bool resched = false; + + /* + * Refresh running @curr without linking it: preemption links + * it if it is preempted while still holding an rwsem. + */ + if (rwsem_boost_holding_or_active(curr) && + rwsem_boost_can_inspect_fair(rq, curr)) + resched = rwsem_boost_refresh_curr_fair(rq, curr); + + list_for_each_entry_safe(p, next, &rq->rwsem_holders, + rwsem_boost.rq_node) { + int old_prio; + + if (budget-- == 0) { + /* Resume from @p next tick; keeps rq->lock O(1). */ + list_rotate_to_front(&p->rwsem_boost.rq_node, + &rq->rwsem_holders); + break; + } + + if (p == curr) { + if (!rwsem_boost_needs_scan(curr)) + rwsem_boost_rq_del(curr); + continue; + } + if (!rwsem_boost_can_inspect_fair(rq, p)) + continue; + + old_prio = p->rwsem_boost.prio; + if (rwsem_boost_update_fair(rq, p) && + p->rwsem_boost.prio < old_prio && + !task_current(rq, p) && + rwsem_boost_should_preempt_fair(rq, p, curr)) + resched = true; + } + + if (resched) + rwsem_boost_resched_fair(rq, curr); +} #else static inline void rwsem_boost_clear_fair(struct rq *rq, struct task_struct *p) { } +static inline void rwsem_boost_rq_del(struct task_struct *p) { } +static inline void rwsem_boost_enqueue_fair(struct rq *rq, + struct task_struct *p, + int flags) { } +static inline void rwsem_boost_put_prev_fair(struct rq *rq, + struct task_struct *prev) { } +static inline void rwsem_boost_tick_fair(struct rq *rq, + struct task_struct *curr) { } #endif /* @@ -8326,6 +8546,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) if (flags & ENQUEUE_DELAYED) { requeue_delayed_entity(cfs_rq, se); + rwsem_boost_enqueue_fair(rq, p, flags); return; } @@ -8383,6 +8604,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) assert_list_leaf_cfs_rq(rq); hrtick_update(rq); + rwsem_boost_enqueue_fair(rq, p, flags); } static void dequeue_hierarchy(struct task_struct *p, int flags) @@ -8514,6 +8736,9 @@ static bool __dequeue_task(struct rq *rq, struct task_struct *p, int flags) */ static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) { + /* Unlink first: throttled and delayed dequeues return early. */ + rwsem_boost_rq_del(p); + if (task_is_throttled(p)) { dequeue_throttled_task(p, flags); return true; @@ -10516,6 +10741,8 @@ static void put_prev_task_fair(struct rq *rq, struct task_struct *prev, struct t cfs_rq->curr = NULL; if (se->on_rq) __enqueue_entity(cfs_rq, se); + + rwsem_boost_put_prev_fair(rq, prev); } /* @@ -15505,6 +15732,8 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int hrtick) update_misfit_status(curr, rq); check_update_overutilized_status(task_rq(curr)); + rwsem_boost_tick_fair(rq, curr); + task_tick_core(rq, curr); } diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 058311c19cc0..3d7bf08223bc 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1336,6 +1336,14 @@ struct rq { int online; struct list_head cfs_tasks; +#ifdef CONFIG_RWSEM_SCHED_BOOST + /* + * Fair tasks holding rwsems, protected by rq->lock. They leave + * on dequeue and class switch; tasks that stopped holding are + * dropped lazily by the tick. + */ + struct list_head rwsem_holders; +#endif struct sched_avg avg_rt; struct sched_avg avg_dl; @@ -4450,6 +4458,11 @@ static inline int rwsem_boost_effective_prio(const struct task_struct *p) int rwsem_boost_calc_prio(struct task_struct *p); void rwsem_boost_drop_fair(struct rq *rq, struct task_struct *p); + +static inline void rwsem_boost_rq_init(struct rq *rq) +{ + INIT_LIST_HEAD(&rq->rwsem_holders); +} #else static inline bool rwsem_boost_active(const struct task_struct *p) { @@ -4460,6 +4473,10 @@ static inline int rwsem_boost_effective_prio(const struct task_struct *p) { return p->static_prio; } + +static inline void rwsem_boost_rq_init(struct rq *rq) +{ +} #endif #endif /* _KERNEL_SCHED_SCHED_H */ -- 2.56.1.839.g22a8290db9-goog