From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD8E52CA8 for ; Mon, 6 Jan 2025 11:10:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736161833; cv=none; b=omNGP7uaIt291leqkfIusKXGKvexKWTl6W9BkE32z3zJg9KLE56xsV0EeFqTbaNZk3j7xBoCtn2mzzqPWgk1ZSDagadW5dMI3s44+qensug6H/qq1isnr8ENzuKq2eOje3iZ3l8B15CUuY5k8SWRQFsHypLULtk1ygK9nN1gmKc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736161833; c=relaxed/simple; bh=uuGH2SYQNOizhmYmUhURav19iJrVfbSwkbT3yQ4qz3Y=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=TkB9T6gpETJn6EM8rDw9PUswF9mLxaDPT7xqXATUALZwkHj13nsiPBxm/ny/X4xuQ70iwLdBxp1AduMFnuj+YG74vFCaj9Q5RAVc5dMQBDHkODpRKqVBEHKnvx80jihOq/EStXHBoaZ3YzhKjeavRiaAz/dzUn3voCU4AhsUyus= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=lun3ExHA; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="lun3ExHA" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=2QlQddX3B0a6iwflX5CWNrDrN7dBFmXZVc9RqhJwacc=; b=lun3ExHAdyewsnFn0S7ixQOAK+ CPPjNJkEyueaz2pLmfkNQyUPfWqNDBXHSP08/1nU2zyqVtG9kr9Mp6NMAMqec+OPe4U9OpJDjVON/ 4GeMpB5Fz1UYJxxIPjYN2QyJGToWtlZVOPsgmN4GBzMjXlx4UW3jsnAOnUezhARvgXgxJC5l6DSGY 0O5kLPiV1LTCs5dLVnfZakq8AypKq1p1LwPtC+UAk/GD7/Tur5geYBLSJ+a4DKy0ZIJY5lNZlTWFH 9nv/zXxw/MJTTtUBiEwCTXiP7PZKjCdeYY9EJjVwbTTfKTzYpo7ugUOwr+QNONW/18/ph/j60ak/A sUpNAGkw==; Received: from 77-249-17-89.cable.dynamic.v4.ziggo.nl ([77.249.17.89] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tUl02-00000007gCT-0vZ3; Mon, 06 Jan 2025 11:10:22 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id AA3C130063F; Mon, 6 Jan 2025 12:10:21 +0100 (CET) Date: Mon, 6 Jan 2025 12:10:21 +0100 From: Peter Zijlstra To: Tianchen Ding Cc: linux-kernel@vger.kernel.org, Ingo Molnar , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Marcelo Tosatti , Mike Galbraith , Rik van Riel Subject: Re: [PATCH] sched: Fix race between yield_to() and try_to_wake_up() Message-ID: <20250106111021.GD20870@noisy.programming.kicks-ass.net> References: <20241231055020.6521-1-dtcccc@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20241231055020.6521-1-dtcccc@linux.alibaba.com> On Tue, Dec 31, 2024 at 01:50:20PM +0800, Tianchen Ding wrote: > We met a SCHED_WARN in set_next_buddy(): > __warn_printk > set_next_buddy > yield_to_task_fair > yield_to > kvm_vcpu_yield_to [kvm] > ... > > After a short dig, we found the rq_lock held by yield_to() may not > be exactly the rq that the target task belongs to. There is a race > window against try_to_wake_up(). > > CPU0 target_task > > blocking on CPU1 > lock rq0 & rq1 > double check task_rq == p_rq, ok > woken to CPU2 (lock task_pi & rq2) > task_rq = rq2 > yield_to_task_fair (w/o lock rq2) > > In this race window, yield_to() is operating the task w/o the currect > lock. Fix this by taking task pi_lock first. > > Fixes: d95f41220065 ("sched: Add yield_to(task, preempt) functionality") > Signed-off-by: Tianchen Ding > --- > kernel/sched/syscalls.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c > index ff0e5ab4e37c..943406c4ee86 100644 > --- a/kernel/sched/syscalls.c > +++ b/kernel/sched/syscalls.c > @@ -1433,7 +1433,7 @@ int __sched yield_to(struct task_struct *p, bool preempt) > struct rq *rq, *p_rq; > int yielded = 0; > > - scoped_guard (irqsave) { > + scoped_guard (raw_spinlock_irqsave, &p->pi_lock) { > rq = this_rq(); Thanks!