Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp538038lqm; Wed, 1 May 2024 08:14:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVCWbBikmCiPyo9gfdDNH5H07mtGy/HHfmjSBMQ11g6DzIcanIR1VoYhcySZqOyWoOCDos72g5ZeWVtfF8fSyava69MGrBoUIKuEsMEEQ== X-Google-Smtp-Source: AGHT+IGN/2Fv762qeMW3UTIqx+xz760KicllHUUxVdpYIzxBV2W9sB4eB16uplqsQg+z6XaQhrhT X-Received: by 2002:a25:b127:0:b0:dc6:ddfc:1736 with SMTP id g39-20020a25b127000000b00dc6ddfc1736mr2963545ybj.17.1714576446943; Wed, 01 May 2024 08:14:06 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714576446; cv=pass; d=google.com; s=arc-20160816; b=Vpv2ox2g/Tmcn1DzNIM0Lau+BRwGjHocVADYMrVFWyPUFfADIWIS7PE0fVD1p/phMT jxfIZyJFbpB2fs2MYFP/Bm6l4M2xsUHExclHmqTap3+oopDpoUEsoLlxuMitUbawIFlo O5hF+RsFaMa53mN826iPgiKvDnnSlTVVDHQLMgdPmPzCyD4x6bBTRtT4daDc7x+bb8U2 A0xfEM8jSQObtCgF64TkbqJbJ5qa/CGcysAUMii6MQaKeMduN+p6cO41U2323IW7Gxif bALxSSK2NkQqaFLSfBQlb6oS3lf+/4guNKkKbBU5kDsgQpWpLYnM4EqzflpBykjgo46B 3DAA== 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:sender:dkim-signature; bh=Z3ZT9Rw/39D/TLYwSmpsvTvswHfmxEgGNOtx5e567jg=; fh=jB5rwvOYw5f5rg11/I5XvDVvfeEgsX2kleObsKJim2U=; b=ASLHhmPIAwTL3Fd30rXwORVxTiJGhPvCO6sBFNVT5vxEE/bSCDBrP+v/M5xx+1GjV7 EmTcyshrd2qnPlIwlnnvymaukIpF8u6ZfIrPOKe5Qp0oXDtbYhF35GJY5W7z/7Q4T4u4 iKYs1TW4bNzXFkb2oQjHxklaGgAho3TGj1ydXCuKHnMi2xRFZdM8HFnMTovdDZnKqL9R 49lREu/OS6RB1YSjWAsCSzSjeE//xhgdQ4vViFQyU4qcgj3isOtFfz2S8Ox0IaNyfEQE zy2BObNd5U3oGYVKq6UnCTaYvGj+jQQONwymrR2IvrhHFeTIvuTKrMlmAZj4pX/2qgd2 7ndQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="H9bb51/p"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-165421-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165421-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id j6-20020a67ef06000000b0047b902400f4si4891121vsr.749.2024.05.01.08.14.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 08:14:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165421-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="H9bb51/p"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-165421-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165421-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id B3EBF1C228A4 for ; Wed, 1 May 2024 15:14:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2EFAF12E1C4; Wed, 1 May 2024 15:13:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="H9bb51/p" Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 923A312FB09; Wed, 1 May 2024 15:13:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714576406; cv=none; b=ZCTp31MfAnOTmegUbEDotDy4eY604LXyl3EoG6dQ5J05cu9K/le4PATAwtuw223C8Yag0LA7YY/3TTk/Qibw/MuxabAtKGLdosLiHkP/cW5RQMZmPcwgA+kbmCGyyEydgeREJiIKR38sqY0sVWeDI69y0hqvYg5Yf30sCfws8VU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714576406; c=relaxed/simple; bh=6nIutmRNFd0r49yfIngp+M0hG56ty8wRFB/PjeZKWbI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b2bBKurhKzYR7Dm190dRMzIp1zKZnAwjge5nPWqXEgXBm+WtbBt8WEHsu5X7T0jxYpMYQRNjhJVDWqoRSg+Oc8ddPBEn0J/h5GZBRP99hLKOFpv4vrudIiTT3YkUI/L4udkilG8zImAaSKJcrFEYZn2+qWe/fj74NSW7jynEhl8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=H9bb51/p; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1e3f17c6491so57021745ad.2; Wed, 01 May 2024 08:13:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714576404; x=1715181204; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=Z3ZT9Rw/39D/TLYwSmpsvTvswHfmxEgGNOtx5e567jg=; b=H9bb51/p4eicTajgMkXGOOmxolHS6JPkXxnfYC1DjvWjv24cljF/ciPtDXc1ctlICA 1Wy3q1SUASf+nz9P5bsGw+zvS1yTug5RT+BA0pbmXo4jOiPZ8O45KRZl13Qdms4+n8Or 8etEWA/KRJ7ee5mADKFjtiUQLiubq/5djvn337qT6UJn3k3QLfPUg4gXHmJldmcbbEGc 5Z0pirov0J9LGqdRQH5B1CmvznWDd52I90v9mJIRMvKy293Lkg9DBkfYrZH0USqGgNiy iOFcZX6EUkVvfVijeZCalhPllHPuHeQ2IYF3f+9VmC676jcc2a6X/hgAerf6eEqbnUnh S8ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714576404; x=1715181204; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Z3ZT9Rw/39D/TLYwSmpsvTvswHfmxEgGNOtx5e567jg=; b=QIPWmae7Fdu5T5KmTWu3NpM9WjnTIPUVoyV9mK6lfUIxI4SLhFe96TBJvbV27N8ba8 WY6TwWMdRT+017SzqE/h28v/ptU2xQapI6vJpq3PNYEhHI5rEMtzmuFHfrSbXFW/6fcW rkInkL221WscNn9VXxjVCb1LByxl/Nl6gq8045DN7iFRoA1oZnIthReNPE0qTFFuMkPC bI8iA8xqzwVpQWmEXRe5tAWgLWD0XBEDnA+pWnAtfWj0NmGccLyB9opfJOsb1aZK1BMs wOrrAcJHQfp5ebpmOpDzv/oes+22NpOUd1PWS9IDj+cez03b52IhqWx0xvwj7aH2FYW5 im9A== X-Forwarded-Encrypted: i=1; AJvYcCW6GsKywcsSXJD3BUIOBoOdzftr+8VHg1a6Dn16PYjRG9sYznefOxM8Bn7PDzV3BVTQ+2z0Etep6xaN/xoc2KbXr1QL X-Gm-Message-State: AOJu0YwGpSf8E3h1KvSja8ygP2TkAnd8toCDk1hK15XxFIINTReD57es Jdvw/PcUodACplBmJwD2GlxXPFt437mcnAO/eAK5nqdllL1VzV3O X-Received: by 2002:a17:902:bc42:b0:1e2:a5b3:e5 with SMTP id t2-20020a170902bc4200b001e2a5b300e5mr2481342plz.32.1714576403660; Wed, 01 May 2024 08:13:23 -0700 (PDT) Received: from localhost (dhcp-141-239-159-203.hawaiiantel.net. [141.239.159.203]) by smtp.gmail.com with ESMTPSA id n15-20020a170902e54f00b001e512537a5fsm24318600plf.9.2024.05.01.08.13.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 08:13:23 -0700 (PDT) Sender: Tejun Heo From: Tejun Heo To: torvalds@linux-foundation.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, joshdon@google.com, brho@google.com, pjt@google.com, derkling@google.com, haoluo@google.com, dvernet@meta.com, dschatzberg@meta.com, dskarlat@cs.cmu.edu, riel@surriel.com, changwoo@igalia.com, himadrics@inria.fr, memxor@gmail.com, andrea.righi@canonical.com, joel@joelfernandes.org Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, kernel-team@meta.com, Tejun Heo Subject: [PATCH 03/39] sched: Allow sched_cgroup_fork() to fail and introduce sched_cancel_fork() Date: Wed, 1 May 2024 05:09:38 -1000 Message-ID: <20240501151312.635565-4-tj@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240501151312.635565-1-tj@kernel.org> References: <20240501151312.635565-1-tj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A new BPF extensible sched_class will need more control over the forking process. It wants to be able to fail from sched_cgroup_fork() after the new task's sched_task_group is initialized so that the loaded BPF program can prepare the task with its cgroup association is established and reject fork if e.g. allocation fails. Allow sched_cgroup_fork() to fail by making it return int instead of void and adding sched_cancel_fork() to undo sched_fork() in the error path. sched_cgroup_fork() doesn't fail yet and this patch shouldn't cause any behavior changes. v2: Patch description updated to detail the expected use. Signed-off-by: Tejun Heo Reviewed-by: David Vernet Acked-by: Josh Don Acked-by: Hao Luo Acked-by: Barret Rhoden --- include/linux/sched/task.h | 3 ++- kernel/fork.c | 15 ++++++++++----- kernel/sched/core.c | 8 +++++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h index d362aacf9f89..4df2f9055587 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -63,7 +63,8 @@ extern asmlinkage void schedule_tail(struct task_struct *prev); extern void init_idle(struct task_struct *idle, int cpu); extern int sched_fork(unsigned long clone_flags, struct task_struct *p); -extern void sched_cgroup_fork(struct task_struct *p, struct kernel_clone_args *kargs); +extern int sched_cgroup_fork(struct task_struct *p, struct kernel_clone_args *kargs); +extern void sched_cancel_fork(struct task_struct *p); extern void sched_post_fork(struct task_struct *p); extern void sched_dead(struct task_struct *p); diff --git a/kernel/fork.c b/kernel/fork.c index 39a5046c2f0b..02f12033db9c 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2362,7 +2362,7 @@ __latent_entropy struct task_struct *copy_process( retval = perf_event_init_task(p, clone_flags); if (retval) - goto bad_fork_cleanup_policy; + goto bad_fork_sched_cancel_fork; retval = audit_alloc(p); if (retval) goto bad_fork_cleanup_perf; @@ -2495,7 +2495,9 @@ __latent_entropy struct task_struct *copy_process( * cgroup specific, it unconditionally needs to place the task on a * runqueue. */ - sched_cgroup_fork(p, args); + retval = sched_cgroup_fork(p, args); + if (retval) + goto bad_fork_cancel_cgroup; /* * From this point on we must avoid any synchronous user-space @@ -2541,13 +2543,13 @@ __latent_entropy struct task_struct *copy_process( /* Don't start children in a dying pid namespace */ if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) { retval = -ENOMEM; - goto bad_fork_cancel_cgroup; + goto bad_fork_core_free; } /* Let kill terminate clone/fork in the middle */ if (fatal_signal_pending(current)) { retval = -EINTR; - goto bad_fork_cancel_cgroup; + goto bad_fork_core_free; } /* No more failure paths after this point. */ @@ -2621,10 +2623,11 @@ __latent_entropy struct task_struct *copy_process( return p; -bad_fork_cancel_cgroup: +bad_fork_core_free: sched_core_free(p); spin_unlock(¤t->sighand->siglock); write_unlock_irq(&tasklist_lock); +bad_fork_cancel_cgroup: cgroup_cancel_fork(p, args); bad_fork_put_pidfd: if (clone_flags & CLONE_PIDFD) { @@ -2663,6 +2666,8 @@ __latent_entropy struct task_struct *copy_process( audit_free(p); bad_fork_cleanup_perf: perf_event_free_task(p); +bad_fork_sched_cancel_fork: + sched_cancel_fork(p); bad_fork_cleanup_policy: lockdep_free_task(p); #ifdef CONFIG_NUMA diff --git a/kernel/sched/core.c b/kernel/sched/core.c index c166c506244f..b12b1b7405fd 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4817,7 +4817,7 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p) return 0; } -void sched_cgroup_fork(struct task_struct *p, struct kernel_clone_args *kargs) +int sched_cgroup_fork(struct task_struct *p, struct kernel_clone_args *kargs) { unsigned long flags; @@ -4844,6 +4844,12 @@ void sched_cgroup_fork(struct task_struct *p, struct kernel_clone_args *kargs) if (p->sched_class->task_fork) p->sched_class->task_fork(p); raw_spin_unlock_irqrestore(&p->pi_lock, flags); + + return 0; +} + +void sched_cancel_fork(struct task_struct *p) +{ } void sched_post_fork(struct task_struct *p) -- 2.44.0