Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3832087ybl; Tue, 21 Jan 2020 07:51:39 -0800 (PST) X-Google-Smtp-Source: APXvYqyzevRBN7c4YHCVh5B5maY1p/kqaAbKj061PDw7whWvWPzXL+FqDcWK/P6UiWA2wWjKO2UL X-Received: by 2002:a05:6830:4ca:: with SMTP id s10mr4057053otd.268.1579621898894; Tue, 21 Jan 2020 07:51:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579621898; cv=none; d=google.com; s=arc-20160816; b=K1bgfIyFWfPjtxI+CaIeScWG2AsEUfkgSMcrwUmJJbfO9iP79Dlqk8DrDSQo94PAtL jgwJGYL1P8eos92A0BkBiTk3xoCPMUp43OxdTyRnAX9XDeuzv0JLD2LcmaXMZZWadwEx Krhr/0NdfUIMIgcSlIdZwDGuqXTg3BPAFQmsrQDqdwyrP5C25UR44Efkyd0WC1feZ7B9 Dn5rTbibDOkAQajaayVEX9eg0A5l2A6uHdBzrVaHSgi3kAlERFOBcz61IgGJRW57IpdH iJMkroyoYBRfkMG7fAfVaUmx7NH/FG83NJbPW6ySiY2wgsSfzJREMEhlz0UCDk95EeEy eqvg== 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=3ujTIZqxurEWuND7M9HNYJRGn+SZzJDbmMQLrrVHqVY=; b=Up5nIoR5NUR0k2/qQ6lXHW4NgHVBi/AGch64dto2SX+4hq0lxrU4oCo54xRdcSd9Un 89QxQ/M8lV1q1KrAFh3HlnoW9vKIvWfPumQCUUbsHRMt8fPhIGenCNcfwtOOG90jxHky IeZ2kXGsabF01tNDlgBrsQqpi23DEwovHnuRDkDQghv9crhZLcXtzIy2x+de3uwaRE5S Xh+Uo1BD3KXoUUCQPVCSGqV5LUZyjdYqYu9XV+CoYi7zZJsocbaDpE4dyqfqqqPiVHZL BDpsZzR/qeabRcWZFolc1cfDzTBvgXIBCmWNygQV8uGD4kDvWCzbBCHIKBSoKNN8kSSf cUkA== 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 u16si21873292otq.92.2020.01.21.07.51.26; Tue, 21 Jan 2020 07:51:38 -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 S1729285AbgAUPud (ORCPT + 99 others); Tue, 21 Jan 2020 10:50:33 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:39896 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726714AbgAUPuc (ORCPT ); Tue, 21 Jan 2020 10:50:32 -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 1itvnS-0004D0-9B; Tue, 21 Jan 2020 15:50:30 +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 2/6] cgroup: add cgroup_get_from_file() helper Date: Tue, 21 Jan 2020 16:48:40 +0100 Message-Id: <20200121154844.411-3-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 Add a helper cgroup_get_from_file(). The helper will be used in subsequent patches to retrieve a cgroup while holding a reference to the struct file it was taken from. 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-3-christian.brauner@ubuntu.com patch introduced - Tejun Heo : - split cgroup_get_from_file() changes into separate commmit /* v4 */ Link: https://lore.kernel.org/r/20200117181219.14542-3-christian.brauner@ubuntu.com unchanged /* v5 */ unchanged --- kernel/cgroup/cgroup.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 7b98cc389dae..9b3241d67592 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -5875,6 +5875,24 @@ void cgroup_fork(struct task_struct *child) INIT_LIST_HEAD(&child->cg_list); } +static struct cgroup *cgroup_get_from_file(struct file *f) +{ + struct cgroup_subsys_state *css; + struct cgroup *cgrp; + + css = css_tryget_online_from_dir(f->f_path.dentry, NULL); + if (IS_ERR(css)) + return ERR_CAST(css); + + cgrp = css->cgroup; + if (!cgroup_on_dfl(cgrp)) { + cgroup_put(cgrp); + return ERR_PTR(-EBADF); + } + + return cgrp; +} + /** * cgroup_can_fork - called on a new task before the process is exposed * @child: the task in question. @@ -6163,7 +6181,6 @@ EXPORT_SYMBOL_GPL(cgroup_get_from_path); */ struct cgroup *cgroup_get_from_fd(int fd) { - struct cgroup_subsys_state *css; struct cgroup *cgrp; struct file *f; @@ -6171,17 +6188,8 @@ struct cgroup *cgroup_get_from_fd(int fd) if (!f) return ERR_PTR(-EBADF); - css = css_tryget_online_from_dir(f->f_path.dentry, NULL); + cgrp = cgroup_get_from_file(f); fput(f); - if (IS_ERR(css)) - return ERR_CAST(css); - - cgrp = css->cgroup; - if (!cgroup_on_dfl(cgrp)) { - cgroup_put(cgrp); - return ERR_PTR(-EBADF); - } - return cgrp; } EXPORT_SYMBOL_GPL(cgroup_get_from_fd); -- 2.25.0