Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp36131115rwd; Mon, 10 Jul 2023 18:45:02 -0700 (PDT) X-Google-Smtp-Source: APBJJlHPzOeAUcxAuuqdf1JuGATn9iIBV727ozT4ZsYqY7p0iC8DQUZ8Z+nE6RUC3zGvTZmVSBFw X-Received: by 2002:a05:6a00:1888:b0:666:e1f4:5153 with SMTP id x8-20020a056a00188800b00666e1f45153mr13987744pfh.0.1689039902403; Mon, 10 Jul 2023 18:45:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689039902; cv=none; d=google.com; s=arc-20160816; b=jwWPKqWZy9bv65onKezlp7SZ1rd8emcV5F07nefzeHrKh5hYgpciBnJTrC1appG8gS ENVqi32wJ5mW1N5BekWUhBhz1mPXM9VYzClp4/XcFrv0pYyPxKiGvVBnkiRsCauvxNnF F3+DDA7GDAFuzKEZxmQa3n61Hz+MXE+vZjS6eJYDvUu/CeOj7CdTKFeSMPZv+Yp6OOD2 2WV6L69K8xBI7eE/XZUsvvhZm6xt69jeSYco5bXP7y3EOi4Nsuf6X3SkwD5uJwj6LA2r Px9FLp4wi5PK3SVkVW+IWbm4kC3lkc/o9EZ8iBCZb4yowcTh1gcpTWiiPpjrUf3xuJNo wbtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=j1IM/JP2r1ZFTSnx810IcSLrL+A34L8i6fPDD3ZG5Pc=; fh=5bvFqXZy/VrzcWOXnXaaKVCt2r2P+2FQUI6EKkM71t8=; b=dgrTsfz4XygF5as2N5kZ5hJp529ayB4liZ+I2CWseoGacR9ZBpYrZSc2nEPiEVcfGa cbZFwMstlhvO3wTtNhgNOqylduU1DS5yCBBZ+iEjINAO1gAdcVfNZhMuT60X02NlEh2X PgVVL4MtVpdgi8AlDAKqHZ+jKRiGBFylDsI6g+dYEfTousfh4cjIImvN0gkYfWwMZPc4 5i8WTLXEQiA4X/xT2k5YIa3wtuS+CVwuwiyfpvchbP4peqrvCWJJqVBMLLSBk15J+3U4 4L0UejmA0PwadyWP8wJw4+SWSrOy/U8wg/oojnmJ/qib49LViLbndPQ1GElBbvoYWZNj D0sQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=PaODUOPx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cw11-20020a056a00450b00b0066215192dd4si601623pfb.295.2023.07.10.18.44.44; Mon, 10 Jul 2023 18:45:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=PaODUOPx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231307AbjGKBOj (ORCPT + 99 others); Mon, 10 Jul 2023 21:14:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231213AbjGKBOY (ORCPT ); Mon, 10 Jul 2023 21:14:24 -0400 Received: from mail-oi1-x22e.google.com (mail-oi1-x22e.google.com [IPv6:2607:f8b0:4864:20::22e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F9C0106; Mon, 10 Jul 2023 18:14:23 -0700 (PDT) Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-3a37909a64eso3627194b6e.1; Mon, 10 Jul 2023 18:14:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689038062; x=1691630062; 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=j1IM/JP2r1ZFTSnx810IcSLrL+A34L8i6fPDD3ZG5Pc=; b=PaODUOPx1yxNS20hdl146+YCmD7pNImrYzuVCjr6QVWhEmvbKVKV77t4jlWeGLLmU6 XXid6aMQl2fJ655/Msg24u2fibWEkmyqYN679ZInA7L0jxD/E7cqW1TL0Er8R+oLdWsY jljQiL/u2Tnaj74X2X3r2AzI2S3olQ9VXGexZVR1mpuoCVzEI9Lu4yQ6IgOyRNNPFc0e IKgGnA2BTEos5qFnWS2B3c50K3YukfHv2uTym2+L6Yp7pQg7LsL5u+AguPcVUuSF1VGo iMxlTCJhZgc+dHXTWtpBh9lRvrU5xEWLIuzQ0FvTBTjcgPYXow39cWk8YnfZhH8EmbJy iQYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689038062; x=1691630062; 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=j1IM/JP2r1ZFTSnx810IcSLrL+A34L8i6fPDD3ZG5Pc=; b=amdJ33J8fOIq7EsDtP0m3LdvMcM3JY/kIe/6CFy9+nicHGF+sEF5JR6yDYEKUG1Avk X6ZVzHbW3CYUwZu4haqvQc/pdCvhLSJE4rAZUIyezQoaSVrQoU7TgFovFlq71UZs0+JJ 1gdkNhN/51xDwsLTv66KeGFngmuHcyeTrt9pnFaLkmw9LI24YocjbsX8ObWSk34rgkpz CwhILag5+EhHTb85SEbGQkMVLR/gBmjYJ0A/pYAQYMThFkuEumQ2YAD4IFFrEMpPCDIx 1GLbDauZ51jJPZ9G0hCc05BE//qC6wKnNhdkrJ/7F/NHQQTdpFvcsdkE1RmZWvs5KU/r TSMg== X-Gm-Message-State: ABy/qLacxfKnIqSMN/CBgM/hdg/NAODaTBflUCblkDxysUQmkg6H6rkO AdTe53rSA+8MqxtowhHQQmk= X-Received: by 2002:a05:6808:1b0f:b0:39e:8678:4035 with SMTP id bx15-20020a0568081b0f00b0039e86784035mr16213156oib.13.1689038062175; Mon, 10 Jul 2023 18:14:22 -0700 (PDT) Received: from localhost ([2620:10d:c090:400::5:e2fe]) by smtp.gmail.com with ESMTPSA id fk25-20020a056a003a9900b00682a0184742sm407771pfb.148.2023.07.10.18.14.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jul 2023 18:14:21 -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 Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, kernel-team@meta.com, Tejun Heo Subject: [PATCH 03/34] sched: Allow sched_cgroup_fork() to fail and introduce sched_cancel_fork() Date: Mon, 10 Jul 2023 15:13:21 -1000 Message-ID: <20230711011412.100319-4-tj@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230711011412.100319-1-tj@kernel.org> References: <20230711011412.100319-1-tj@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 537cbf9a2ade..5431b350ed96 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -62,7 +62,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 ed4e01daccaa..0a9c8c03889f 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2480,7 +2480,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; @@ -2615,7 +2615,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 @@ -2661,13 +2663,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. */ @@ -2743,10 +2745,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) { @@ -2785,6 +2788,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 173a42336d54..a869236d0735 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4749,7 +4749,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; @@ -4776,6 +4776,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.41.0