Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753511AbcCRWQI (ORCPT ); Fri, 18 Mar 2016 18:16:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33327 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753059AbcCRWQF (ORCPT ); Fri, 18 Mar 2016 18:16:05 -0400 From: Bandan Das To: tj@kernel.org Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, mst@redhat.com, jiangshanlai@gmail.com, RAPOPORT@il.ibm.com Subject: [RFC PATCH 1/4] cgroup: Introduce a function to compare two tasks Date: Fri, 18 Mar 2016 18:14:48 -0400 Message-Id: <1458339291-4093-2-git-send-email-bsd@redhat.com> In-Reply-To: <1458339291-4093-1-git-send-email-bsd@redhat.com> References: <1458339291-4093-1-git-send-email-bsd@redhat.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2023 Lines: 72 This function takes two tasks and iterates through all hierarchies to check if they belong to the same cgroups. It ignores the check for default hierarchies or for hierarchies with no subsystems attached. Signed-off-by: Bandan Das --- include/linux/cgroup.h | 1 + kernel/cgroup.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 2162dca..6b66092 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -83,6 +83,7 @@ struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, struct cgroup *cgroup_get_from_path(const char *path); +bool cgroup_match_groups(struct task_struct *, struct task_struct *); int cgroup_attach_task_all(struct task_struct *from, struct task_struct *); int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from); diff --git a/kernel/cgroup.c b/kernel/cgroup.c index d27904c..3ffdbb4 100644 --- a/kernel/cgroup.c +++ b/kernel/cgroup.c @@ -2745,6 +2745,42 @@ out_unlock_threadgroup: } /** + * cgroup_match_groups - check if tsk1 and tsk2 belong to same cgroups + * in all hierarchies + */ +bool cgroup_match_groups(struct task_struct *tsk1, struct task_struct *tsk2) +{ + struct cgroup_root *root; + bool result = true; + + mutex_lock(&cgroup_mutex); + for_each_root(root) { + struct cgroup *cg_tsk1; + struct cgroup *cg_tsk2; + + if (root == &cgrp_dfl_root) + continue; + + if (!root->subsys_mask) + continue; + + spin_lock_bh(&css_set_lock); + cg_tsk1 = task_cgroup_from_root(tsk1, root); + cg_tsk2 = task_cgroup_from_root(tsk2, root); + spin_unlock_bh(&css_set_lock); + + if (cg_tsk1 != cg_tsk2) { + result = false; + break; + } + } + mutex_unlock(&cgroup_mutex); + + return result; +} +EXPORT_SYMBOL_GPL(cgroup_match_groups); + +/** * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' * @from: attach to all cgroups of a given task * @tsk: the task to be attached -- 2.5.0