Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3832244ybl; Tue, 21 Jan 2020 07:51:52 -0800 (PST) X-Google-Smtp-Source: APXvYqyKrOcChlpjMCzzlOPsyYRkoDIc1tXTEsuYIz/CGjPzGILA7L9KEyTPHDDwfF8gIBhR5RFt X-Received: by 2002:a05:6808:8d0:: with SMTP id k16mr3536401oij.68.1579621912826; Tue, 21 Jan 2020 07:51:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579621912; cv=none; d=google.com; s=arc-20160816; b=DwLpowMg9toNmoX3RrjILFD97wm6kWnNZ6RkV4+gd81qHvxt8DpIY1JlfTwE+KdO+2 XK7N0iY39sub515qITu0WqJjnVxuh8b454a0nAG48WbR4XnTe7uS/lFnwwIMNLyQYl0N LMtvOWjVEoea5UXMnHz/qUYetBLIUSNKema47MpR2/BGgZDn9hYX75MyqVOcYog+5sri K8Pc35C/K4jkrFaF8XC7w3pAtmdp4lH+6UM5IJVkzFWAdArL3Jzkuam802daLLvCPw3p bNxlBypq1Ze/qEInVcR5tpRvjJZaG+lNMFJmN/UJsKhUPeM58QPVmIg3lJjw7wkwBmHE oSzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=95dqBWkvf77gzJCQebV9Bvl+nbK/0vx5c5zuSTCcqVk=; b=P/bVG8mSirZQS13sDSbIIdwv7/LugKrRsBNATIUENE0A27np5TkcBnSW7Oc9LyuTSw 7GSWliwQ/Wdymn9kwh1aEkPByXylnQBwuUQmCqKHxCqb+OeiE33Yme11uXf966TkVOAZ 8ewUQUM1P++EagIHMseVBCxz3osLoWJ6Wr/1ttN2HLK12ovacRp0eNCZtTdnUwuHk+55 42VrzWGkRRPLW1p6o7yVM2byE+BQcX/mqv4241HI6nUN8QRK7TP2KBMjxpQLPHcXIpzh x5Ne0Y6/L2nPm/jPYIybKu9pzKZMCf/gC+2CovVKVK9Kjo3ati+StIO2Kvi47A+BBl6S YK1g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f187si20925956oia.218.2020.01.21.07.51.40; Tue, 21 Jan 2020 07:51:52 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729359AbgAUPui (ORCPT + 99 others); Tue, 21 Jan 2020 10:50:38 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:39907 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726714AbgAUPuh (ORCPT ); Tue, 21 Jan 2020 10:50:37 -0500 Received: from [154.119.55.246] (helo=localhost.localdomain) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1itvnV-0004D0-My; Tue, 21 Jan 2020 15:50:34 +0000 From: Christian Brauner To: linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, Tejun Heo Cc: Oleg Nesterov , Christian Brauner , Johannes Weiner , Li Zefan , cgroups@vger.kernel.org Subject: [PATCH v5 3/6] cgroup: refactor fork helpers Date: Tue, 21 Jan 2020 16:48:41 +0100 Message-Id: <20200121154844.411-4-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200121154844.411-1-christian.brauner@ubuntu.com> References: <20200121154844.411-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This refactors the fork helpers so they can be easily modified in the next patches. The patch just moves the cgroup threadgroup rwsem grab and release into the helpers. They don't need to be directly exposed in fork.c. Cc: Tejun Heo Cc: Johannes Weiner Cc: Li Zefan Cc: cgroups@vger.kernel.org Signed-off-by: Christian Brauner --- /* v1 */ patch not present /* v2 */ patch not present /* v3 */ Link: https://lore.kernel.org/r/20200117002143.15559-4-christian.brauner@ubuntu.com patch introduced - Tejun Heo : - split into separate commmit /* v4 */ Link: https://lore.kernel.org/r/20200117181219.14542-4-christian.brauner@ubuntu.com unchanged /* v5 */ - Oleg Nesterov : - remove struct task_struct *parent argument from clone helpers in favor of using current directly - Christian Brauner : - fix typo in commit message --- kernel/cgroup/cgroup.c | 47 ++++++++++++++++++++++++++---------------- kernel/fork.c | 6 +----- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 9b3241d67592..ce2d5b8aa19f 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -5895,17 +5895,21 @@ static struct cgroup *cgroup_get_from_file(struct file *f) /** * cgroup_can_fork - called on a new task before the process is exposed - * @child: the task in question. + * @child: the child process + * @kargs: the arguments passed to create the child process * - * This calls the subsystem can_fork() callbacks. If the can_fork() callback - * returns an error, the fork aborts with that error code. This allows for - * a cgroup subsystem to conditionally allow or deny new forks. + * This calls the subsystem can_fork() callbacks. If the cgroup_can_fork() + * callback returns an error, the fork aborts with that error code. This + * allows for a cgroup subsystem to conditionally allow or deny new forks. */ int cgroup_can_fork(struct task_struct *child) + __acquires(&cgroup_threadgroup_rwsem) __releases(&cgroup_threadgroup_rwsem) { struct cgroup_subsys *ss; int i, j, ret; + cgroup_threadgroup_change_begin(current); + do_each_subsys_mask(ss, i, have_canfork_callback) { ret = ss->can_fork(child); if (ret) @@ -5922,17 +5926,21 @@ int cgroup_can_fork(struct task_struct *child) ss->cancel_fork(child); } + cgroup_threadgroup_change_end(current); + return ret; } /** - * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork() - * @child: the task in question - * - * This calls the cancel_fork() callbacks if a fork failed *after* - * cgroup_can_fork() succeded. - */ + * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork() + * @child: the child process + * @kargs: the arguments passed to create the child process + * + * This calls the cancel_fork() callbacks if a fork failed *after* + * cgroup_can_fork() succeded. + */ void cgroup_cancel_fork(struct task_struct *child) + __releases(&cgroup_threadgroup_rwsem) { struct cgroup_subsys *ss; int i; @@ -5940,19 +5948,20 @@ void cgroup_cancel_fork(struct task_struct *child) for_each_subsys(ss, i) if (ss->cancel_fork) ss->cancel_fork(child); + + cgroup_threadgroup_change_end(current); } /** - * cgroup_post_fork - called on a new task after adding it to the task list - * @child: the task in question - * - * Adds the task to the list running through its css_set if necessary and - * call the subsystem fork() callbacks. Has to be after the task is - * visible on the task list in case we race with the first call to - * cgroup_task_iter_start() - to guarantee that the new task ends up on its - * list. + * cgroup_post_fork - finalize cgroup setup for the child process + * @child: the child process + * @kargs: the arguments passed to create the child process + * + * Attach the child process to its css_set calling the subsystem fork() + * callbacks. */ void cgroup_post_fork(struct task_struct *child) + __releases(&cgroup_threadgroup_rwsem) { struct cgroup_subsys *ss; struct css_set *cset; @@ -5995,6 +6004,8 @@ void cgroup_post_fork(struct task_struct *child) do_each_subsys_mask(ss, i, have_fork_callback) { ss->fork(child); } while_each_subsys_mask(); + + cgroup_threadgroup_change_end(current); } /** diff --git a/kernel/fork.c b/kernel/fork.c index 080809560072..ca5de25690c8 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2165,7 +2165,6 @@ static __latent_entropy struct task_struct *copy_process( INIT_LIST_HEAD(&p->thread_group); p->task_works = NULL; - cgroup_threadgroup_change_begin(current); /* * Ensure that the cgroup subsystem policies allow the new process to be * forked. It should be noted the the new process's css_set can be changed @@ -2174,7 +2173,7 @@ static __latent_entropy struct task_struct *copy_process( */ retval = cgroup_can_fork(p); if (retval) - goto bad_fork_cgroup_threadgroup_change_end; + goto bad_fork_put_pidfd; /* * From this point on we must avoid any synchronous user-space @@ -2280,7 +2279,6 @@ static __latent_entropy struct task_struct *copy_process( proc_fork_connector(p); cgroup_post_fork(p); - cgroup_threadgroup_change_end(current); perf_event_fork(p); trace_task_newtask(p, clone_flags); @@ -2292,8 +2290,6 @@ static __latent_entropy struct task_struct *copy_process( spin_unlock(¤t->sighand->siglock); write_unlock_irq(&tasklist_lock); cgroup_cancel_fork(p); -bad_fork_cgroup_threadgroup_change_end: - cgroup_threadgroup_change_end(current); bad_fork_put_pidfd: if (clone_flags & CLONE_PIDFD) { fput(pidfile); -- 2.25.0