Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752746AbbKPTxo (ORCPT ); Mon, 16 Nov 2015 14:53:44 -0500 Received: from h2.hallyn.com ([78.46.35.8]:44965 "EHLO h2.hallyn.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750982AbbKPTwJ (ORCPT ); Mon, 16 Nov 2015 14:52:09 -0500 From: serge@hallyn.com To: linux-kernel@vger.kernel.org Cc: adityakali@google.com, tj@kernel.org, linux-api@vger.kernel.org, containers@lists.linux-foundation.org, cgroups@vger.kernel.org, lxc-devel@lists.linuxcontainers.org, akpm@linux-foundation.org, ebiederm@xmission.com Subject: [PATCH 3/8] cgroup: add function to get task's cgroup Date: Mon, 16 Nov 2015 13:51:40 -0600 Message-Id: <1447703505-29672-4-git-send-email-serge@hallyn.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1447703505-29672-1-git-send-email-serge@hallyn.com> References: <1447703505-29672-1-git-send-email-serge@hallyn.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2152 Lines: 68 From: Aditya Kali get_task_cgroup() returns the (reference counted) cgroup of the given task. Signed-off-by: Aditya Kali Acked-by: Serge Hallyn --- include/linux/cgroup.h | 1 + kernel/cgroup.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 22e3754..29f0b02 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -326,6 +326,7 @@ static inline bool css_tryget_online(struct cgroup_subsys_state *css) return percpu_ref_tryget_live(&css->refcnt); return true; } +struct cgroup *get_task_cgroup(struct task_struct *task); /** * css_put - put a css reference diff --git a/kernel/cgroup.c b/kernel/cgroup.c index f1603c1..e29c346 100644 --- a/kernel/cgroup.c +++ b/kernel/cgroup.c @@ -2210,6 +2210,31 @@ char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen) } EXPORT_SYMBOL_GPL(task_cgroup_path); +/* + * get_task_cgroup - returns the cgroup of the task in the default cgroup + * hierarchy. + * + * @task: target task + * This function returns the @task's cgroup on the default cgroup hierarchy. The + * returned cgroup has its reference incremented (by calling cgroup_get()). So + * the caller must cgroup_put() the obtained reference once it is done with it. + */ +struct cgroup *get_task_cgroup(struct task_struct *task) +{ + struct cgroup *cgrp; + + mutex_lock(&cgroup_mutex); + spin_lock_bh(&css_set_lock); + + cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); + cgroup_get(cgrp); + + spin_unlock_bh(&css_set_lock); + mutex_unlock(&cgroup_mutex); + return cgrp; +} +EXPORT_SYMBOL_GPL(get_task_cgroup); + /* used to track tasks and other necessary states during migration */ struct cgroup_taskset { /* the src and dst cset list running through cset->mg_node */ -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/