Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp692016rdb; Fri, 2 Feb 2024 00:27:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IFqLMU9KUEwWf6R8GR3+gMzzYlk+ueEphj1NFEgjOkH47HVPG94fum5MRQjfaOT96bCgx0S X-Received: by 2002:a05:6a20:2a0f:b0:19e:30b0:6ad4 with SMTP id e15-20020a056a202a0f00b0019e30b06ad4mr4727310pzh.55.1706862448071; Fri, 02 Feb 2024 00:27:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706862448; cv=pass; d=google.com; s=arc-20160816; b=mctXOQ1w4lT3tt/EubPGfcBqCwT/VG5sExjZtqUgz2V2kHPyYC49ChrZUe4Arh6a96 yOOSkufi4Ug5Y6WsVanufcz67qYtWRI7x6u98xposLjb6aGhxslcYubUgWX9/+FPpzXE v5OWSs+OzfHu1vc0h/6vtKsS1IxjgRuMiTq01SE+0D1bF+VQUnN8AV8MbPcDgkcXVIWo CvSEG56HINtXBXDQZjUI+GbzWyPKtFjaLE4szlzgfAZFSoQZZnoqeclHdH485sIqmy5l ROiaoRi5TUldO41qefeJd36T6oExOdr2LTzpDlju24RBRwpiVvb+ancFjMA+hWCPH9AI +T3g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=MWiW15gYCd0qq5hmfxU5I8UTcZTFW4ysIqeBM84iI5Y=; fh=2fs7Ehn4ojbgnp0LVi8UcAUsVuNmQ/ozyge67R05efM=; b=ku1naCowZMyy7CrdfrhM7EGBIXGy8rgCImPioTtphwe73RN/EX7Zb0HEVaH7kYkB2o Iri65XX7Iwx1YNkEeEzv4cGFxIYFTkpfkv332BeCM38a5SQa2FnXPEKJiiFt8ypoztQs N3FKTfvPf35PWD3OtyfviW4y/VpHbAL+Ftnk153mfmkNLjo0cCj6uf1ol6bNjp/QLbAa M9pN+ut4iorACO+A/gBx2imL+RrgtQqM3Dfx4iPdWHtrRX9OSrYukJlpk1esLTuVu+Co vvhEmgmlWV9qbnEovBova6wdKrgUeFOO13knoobLebPFBv7Oqay/IdwfGjxyEz2S6OVx 0Hgg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=anlPeXsQ; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-49440-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49440-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCVSDUZUf1QelmgyKR/1VYHwwCiaBGvZDvkHn9Vt5km3EMMRBSCA+ugb6KeDfQNthBdVVsw3oefcd1zwE5KXVn0PZ81CnovYGBk7L3KmsQ== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id s11-20020a17090ad48b00b002962d6ce910si1328595pju.57.2024.02.02.00.27.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 00:27:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-49440-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=anlPeXsQ; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-49440-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49440-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 07536B21A35 for ; Fri, 2 Feb 2024 08:16:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 067C118C31; Fri, 2 Feb 2024 08:10:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="anlPeXsQ" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 4953F18658 for ; Fri, 2 Feb 2024 08:10:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706861448; cv=none; b=YJ5SdYelmP32V3WJ9ZZmuEQZOpdyetACuqhFFboobVH/WYEk3LU0/x64cqEJ/+Swre4jfMrJqoUsk5Pk8PIfKhoLwYG5UlfqE4J2l188PynAWB3PSBt9NjHsOMG1+W+IEO15KRTfHJg1XhJ9tI89XMSXFmtW4io3/L7lCIJTmiI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706861448; c=relaxed/simple; bh=Zk2mQ+ZW7M5p5xaR77pQtedUVa3RgHq4JSSfbueXse4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=E2ZVtPYSX020Xt22N2wuUOqxIo2zw9UvY61WwQufs54BhuxnGMHuysEc9avSt0ASYJrBtBdhZYn/eUVhTqArO2GEJ+PAlgn8ZXIiDTUYLWNB+IbzMNcLg4HHTdcOqxYIRYRy6ONVOr1vOB3Dj7nSSTlaK1BurPK6r72EF7w+OlM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=anlPeXsQ; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706861445; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MWiW15gYCd0qq5hmfxU5I8UTcZTFW4ysIqeBM84iI5Y=; b=anlPeXsQPG37DeisH0PRA/kAA1MDRcU5pHlNSWKRmnKqAMxVRn/smSrQ4OOUP9hHhpUsoN /odbLvP0hFbwrcRtPPAmNICScyZh4pKqEqSJengL7AMvaV2K6JhZRv0AVDcGJPZUmnZ2Tu MxmtIYHc9+CmBkSagrIHiyszppRqHS0= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-394-tjCGo5PvMb2pCh-tcMxXTQ-1; Fri, 02 Feb 2024 03:10:43 -0500 X-MC-Unique: tjCGo5PvMb2pCh-tcMxXTQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F00E22812FFA; Fri, 2 Feb 2024 08:10:42 +0000 (UTC) Received: from vschneid-thinkpadt14sgen2i.remote.csb (unknown [10.39.193.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3A3ADC2590E; Fri, 2 Feb 2024 08:10:40 +0000 (UTC) From: Valentin Schneider To: linux-kernel@vger.kernel.org Cc: Benjamin Segall , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Mel Gorman , Daniel Bristot de Oliveira , Phil Auld , Clark Williams , Tomas Glozar Subject: [RFC PATCH v2 2/5] sched: Note schedule() invocations at return-to-user with SM_USER Date: Fri, 2 Feb 2024 09:09:17 +0100 Message-ID: <20240202080920.3337862-3-vschneid@redhat.com> In-Reply-To: <20240202080920.3337862-1-vschneid@redhat.com> References: <20240202080920.3337862-1-vschneid@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 task_struct.in_return_to_user is currently updated via atomic operations in schedule_usermode(). However, one can note: o .in_return_to_user is only updated for the current task o There are no remote (smp_processor_id() != task_cpu(p)) accesses to .in_return_to_user Add schedule_with_mode() to factorize schedule() with different flags to pass down to __schedule_loop(). Add SM_USER to denote schedule() calls from return-to-userspace points. Update .in_return_to_user from within the preemption-disabled, rq_lock-held part of __schedule(). Suggested-by: Benjamin Segall Signed-off-by: Valentin Schneider --- include/linux/sched.h | 2 +- kernel/sched/core.c | 43 ++++++++++++++++++++++++++++++++----------- kernel/sched/fair.c | 17 ++++++++++++++++- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 4a0105d1eaa21..1b6f17b2150a6 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1544,7 +1544,7 @@ struct task_struct { #endif #ifdef CONFIG_CFS_BANDWIDTH - atomic_t in_return_to_user; + int in_return_to_user; #endif /* * New fields for task_struct should be added above here, so that diff --git a/kernel/sched/core.c b/kernel/sched/core.c index a7c028fad5a89..54e6690626b13 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4531,7 +4531,7 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p) #endif #ifdef CONFIG_CFS_BANDWIDTH INIT_LIST_HEAD(&p->se.kernel_node); - atomic_set(&p->in_return_to_user, 0); + p->in_return_to_user = false; #endif #ifdef CONFIG_SCHEDSTATS @@ -5147,6 +5147,9 @@ prepare_lock_switch(struct rq *rq, struct task_struct *next, struct rq_flags *rf static inline void finish_lock_switch(struct rq *rq) { +#ifdef CONFIG_CFS_BANDWIDTH + current->in_return_to_user = false; +#endif /* * If we are tracking spinlock dependencies then we have to * fix up the runqueue lock - which gets 'carried over' from @@ -6562,6 +6565,18 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) #define SM_PREEMPT 0x1 #define SM_RTLOCK_WAIT 0x2 +/* + * Special case for CFS_BANDWIDTH where we need to know if the call to + * __schedule() is directely preceding an entry into userspace. + * It is removed from the mode argument as soon as it is used to not go against + * the SM_MASK_PREEMPT optimisation below. + */ +#ifdef CONFIG_CFS_BANDWIDTH +# define SM_USER 0x4 +#else +# define SM_USER SM_NONE +#endif + #ifndef CONFIG_PREEMPT_RT # define SM_MASK_PREEMPT (~0U) #else @@ -6646,6 +6661,14 @@ static void __sched notrace __schedule(unsigned int sched_mode) rq_lock(rq, &rf); smp_mb__after_spinlock(); +#ifdef CONFIG_CFS_BANDWIDTH + if (sched_mode & SM_USER) { + prev->in_return_to_user = true; + sched_mode &= ~SM_USER; + } +#endif + SCHED_WARN_ON(sched_mode & SM_USER); + /* Promote REQ to ACT */ rq->clock_update_flags <<= 1; update_rq_clock(rq); @@ -6807,7 +6830,7 @@ static __always_inline void __schedule_loop(unsigned int sched_mode) } while (need_resched()); } -asmlinkage __visible void __sched schedule(void) +static __always_inline void schedule_with_mode(unsigned int sched_mode) { struct task_struct *tsk = current; @@ -6817,22 +6840,20 @@ asmlinkage __visible void __sched schedule(void) if (!task_is_running(tsk)) sched_submit_work(tsk); - __schedule_loop(SM_NONE); + __schedule_loop(sched_mode); sched_update_worker(tsk); } + +asmlinkage __visible void __sched schedule(void) +{ + schedule_with_mode(SM_NONE); +} EXPORT_SYMBOL(schedule); asmlinkage __visible void __sched schedule_usermode(void) { #ifdef CONFIG_CFS_BANDWIDTH - /* - * This is only atomic because of this simple implementation. We could - * do something with an SM_USER to avoid other-cpu scheduler operations - * racing against these writes. - */ - atomic_set(¤t->in_return_to_user, true); - schedule(); - atomic_set(¤t->in_return_to_user, false); + schedule_with_mode(SM_USER); #else schedule(); #endif diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index a1808459a5acc..96504be6ee14a 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -6631,7 +6631,22 @@ static void enqueue_kernel(struct cfs_rq *cfs_rq, struct sched_entity *se, int c static bool is_kernel_task(struct task_struct *p) { - return sysctl_sched_cfs_bandwidth_kernel_bypass && !atomic_read(&p->in_return_to_user); + /* + * The flag is updated within __schedule() with preemption disabled, + * under the rq lock, and only when the task is current. + * + * Holding the rq lock for that task's CPU is thus sufficient for the + * value to be stable, if the task is enqueued. + * + * If the task is dequeued, then task_cpu(p) *can* change, but this + * so far only happens in enqueue_task_fair() which means either: + * - the task is being activated, its CPU has been set previously in ttwu() + * - the task is going through a "change" cycle (e.g. sched_move_task()), + * the pi_lock is also held so the CPU is stable. + */ + lockdep_assert_rq_held(cpu_rq(task_cpu(p))); + + return sysctl_sched_cfs_bandwidth_kernel_bypass && !p->in_return_to_user; } /* -- 2.43.0