Skip to content

Commit dcfe149

Browse files
committed
cgroup: move namespace code to kernel/cgroup/namespace.c
get/put_css_set() get exposed in cgroup-internal.h in the process. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Acked-by: Zefan Li <lizefan@huawei.com>
1 parent d62beb7 commit dcfe149

4 files changed

Lines changed: 189 additions & 175 deletions

File tree

‎kernel/cgroup/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
obj-y := cgroup.o cgroup-v1.o
1+
obj-y := cgroup.o namespace.o cgroup-v1.o
22

33
obj-$(CONFIG_CGROUP_FREEZER) += freezer.o
44
obj-$(CONFIG_CGROUP_PIDS) += pids.o

‎kernel/cgroup/cgroup-internal.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,33 @@ static inline bool notify_on_release(const struct cgroup *cgrp)
6565
return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
6666
}
6767

68+
void put_css_set_locked(struct css_set *cset);
69+
70+
static inline void put_css_set(struct css_set *cset)
71+
{
72+
unsigned long flags;
73+
74+
/*
75+
* Ensure that the refcount doesn't hit zero while any readers
76+
* can see it. Similar to atomic_dec_and_lock(), but for an
77+
* rwlock
78+
*/
79+
if (atomic_add_unless(&cset->refcount, -1, 1))
80+
return;
81+
82+
spin_lock_irqsave(&css_set_lock, flags);
83+
put_css_set_locked(cset);
84+
spin_unlock_irqrestore(&css_set_lock, flags);
85+
}
86+
87+
/*
88+
* refcounted get/put for css_set objects
89+
*/
90+
static inline void get_css_set(struct css_set *cset)
91+
{
92+
atomic_inc(&cset->refcount);
93+
}
94+
6895
bool cgroup_ssid_enabled(int ssid);
6996
bool cgroup_on_dfl(const struct cgroup *cgrp);
7097

@@ -107,6 +134,11 @@ int cgroup_rmdir(struct kernfs_node *kn);
107134
int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
108135
struct kernfs_root *kf_root);
109136

137+
/*
138+
* namespace.c
139+
*/
140+
extern const struct proc_ns_operations cgroupns_operations;
141+
110142
/*
111143
* cgroup-v1.c
112144
*/

‎kernel/cgroup/cgroup.c‎

Lines changed: 1 addition & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
718718
return key;
719719
}
720720

721-
static void put_css_set_locked(struct css_set *cset)
721+
void put_css_set_locked(struct css_set *cset)
722722
{
723723
struct cgrp_cset_link *link, *tmp_link;
724724
struct cgroup_subsys *ss;
@@ -748,31 +748,6 @@ static void put_css_set_locked(struct css_set *cset)
748748
kfree_rcu(cset, rcu_head);
749749
}
750750

751-
static void put_css_set(struct css_set *cset)
752-
{
753-
unsigned long flags;
754-
755-
/*
756-
* Ensure that the refcount doesn't hit zero while any readers
757-
* can see it. Similar to atomic_dec_and_lock(), but for an
758-
* rwlock
759-
*/
760-
if (atomic_add_unless(&cset->refcount, -1, 1))
761-
return;
762-
763-
spin_lock_irqsave(&css_set_lock, flags);
764-
put_css_set_locked(cset);
765-
spin_unlock_irqrestore(&css_set_lock, flags);
766-
}
767-
768-
/*
769-
* refcounted get/put for css_set objects
770-
*/
771-
static inline void get_css_set(struct css_set *cset)
772-
{
773-
atomic_inc(&cset->refcount);
774-
}
775-
776751
/**
777752
* compare_css_sets - helper function for find_existing_css_set().
778753
* @cset: candidate css_set being tested
@@ -5109,154 +5084,6 @@ void cgroup_sk_free(struct sock_cgroup_data *skcd)
51095084

51105085
#endif /* CONFIG_SOCK_CGROUP_DATA */
51115086

5112-
/* cgroup namespaces */
5113-
5114-
static struct ucounts *inc_cgroup_namespaces(struct user_namespace *ns)
5115-
{
5116-
return inc_ucount(ns, current_euid(), UCOUNT_CGROUP_NAMESPACES);
5117-
}
5118-
5119-
static void dec_cgroup_namespaces(struct ucounts *ucounts)
5120-
{
5121-
dec_ucount(ucounts, UCOUNT_CGROUP_NAMESPACES);
5122-
}
5123-
5124-
static struct cgroup_namespace *alloc_cgroup_ns(void)
5125-
{
5126-
struct cgroup_namespace *new_ns;
5127-
int ret;
5128-
5129-
new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL);
5130-
if (!new_ns)
5131-
return ERR_PTR(-ENOMEM);
5132-
ret = ns_alloc_inum(&new_ns->ns);
5133-
if (ret) {
5134-
kfree(new_ns);
5135-
return ERR_PTR(ret);
5136-
}
5137-
atomic_set(&new_ns->count, 1);
5138-
new_ns->ns.ops = &cgroupns_operations;
5139-
return new_ns;
5140-
}
5141-
5142-
void free_cgroup_ns(struct cgroup_namespace *ns)
5143-
{
5144-
put_css_set(ns->root_cset);
5145-
dec_cgroup_namespaces(ns->ucounts);
5146-
put_user_ns(ns->user_ns);
5147-
ns_free_inum(&ns->ns);
5148-
kfree(ns);
5149-
}
5150-
EXPORT_SYMBOL(free_cgroup_ns);
5151-
5152-
struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
5153-
struct user_namespace *user_ns,
5154-
struct cgroup_namespace *old_ns)
5155-
{
5156-
struct cgroup_namespace *new_ns;
5157-
struct ucounts *ucounts;
5158-
struct css_set *cset;
5159-
5160-
BUG_ON(!old_ns);
5161-
5162-
if (!(flags & CLONE_NEWCGROUP)) {
5163-
get_cgroup_ns(old_ns);
5164-
return old_ns;
5165-
}
5166-
5167-
/* Allow only sysadmin to create cgroup namespace. */
5168-
if (!ns_capable(user_ns, CAP_SYS_ADMIN))
5169-
return ERR_PTR(-EPERM);
5170-
5171-
ucounts = inc_cgroup_namespaces(user_ns);
5172-
if (!ucounts)
5173-
return ERR_PTR(-ENOSPC);
5174-
5175-
/* It is not safe to take cgroup_mutex here */
5176-
spin_lock_irq(&css_set_lock);
5177-
cset = task_css_set(current);
5178-
get_css_set(cset);
5179-
spin_unlock_irq(&css_set_lock);
5180-
5181-
new_ns = alloc_cgroup_ns();
5182-
if (IS_ERR(new_ns)) {
5183-
put_css_set(cset);
5184-
dec_cgroup_namespaces(ucounts);
5185-
return new_ns;
5186-
}
5187-
5188-
new_ns->user_ns = get_user_ns(user_ns);
5189-
new_ns->ucounts = ucounts;
5190-
new_ns->root_cset = cset;
5191-
5192-
return new_ns;
5193-
}
5194-
5195-
static inline struct cgroup_namespace *to_cg_ns(struct ns_common *ns)
5196-
{
5197-
return container_of(ns, struct cgroup_namespace, ns);
5198-
}
5199-
5200-
static int cgroupns_install(struct nsproxy *nsproxy, struct ns_common *ns)
5201-
{
5202-
struct cgroup_namespace *cgroup_ns = to_cg_ns(ns);
5203-
5204-
if (!ns_capable(current_user_ns(), CAP_SYS_ADMIN) ||
5205-
!ns_capable(cgroup_ns->user_ns, CAP_SYS_ADMIN))
5206-
return -EPERM;
5207-
5208-
/* Don't need to do anything if we are attaching to our own cgroupns. */
5209-
if (cgroup_ns == nsproxy->cgroup_ns)
5210-
return 0;
5211-
5212-
get_cgroup_ns(cgroup_ns);
5213-
put_cgroup_ns(nsproxy->cgroup_ns);
5214-
nsproxy->cgroup_ns = cgroup_ns;
5215-
5216-
return 0;
5217-
}
5218-
5219-
static struct ns_common *cgroupns_get(struct task_struct *task)
5220-
{
5221-
struct cgroup_namespace *ns = NULL;
5222-
struct nsproxy *nsproxy;
5223-
5224-
task_lock(task);
5225-
nsproxy = task->nsproxy;
5226-
if (nsproxy) {
5227-
ns = nsproxy->cgroup_ns;
5228-
get_cgroup_ns(ns);
5229-
}
5230-
task_unlock(task);
5231-
5232-
return ns ? &ns->ns : NULL;
5233-
}
5234-
5235-
static void cgroupns_put(struct ns_common *ns)
5236-
{
5237-
put_cgroup_ns(to_cg_ns(ns));
5238-
}
5239-
5240-
static struct user_namespace *cgroupns_owner(struct ns_common *ns)
5241-
{
5242-
return to_cg_ns(ns)->user_ns;
5243-
}
5244-
5245-
const struct proc_ns_operations cgroupns_operations = {
5246-
.name = "cgroup",
5247-
.type = CLONE_NEWCGROUP,
5248-
.get = cgroupns_get,
5249-
.put = cgroupns_put,
5250-
.install = cgroupns_install,
5251-
.owner = cgroupns_owner,
5252-
};
5253-
5254-
static __init int cgroup_namespaces_init(void)
5255-
{
5256-
return 0;
5257-
}
5258-
subsys_initcall(cgroup_namespaces_init);
5259-
52605087
#ifdef CONFIG_CGROUP_BPF
52615088
void cgroup_bpf_update(struct cgroup *cgrp,
52625089
struct bpf_prog *prog,

0 commit comments

Comments
 (0)