Skip to contentSkip to conversation

cgroup: Sync cgroup id and inode number

PATCH In review Namhyung Kim 6 participants 8 messages Nov 4, 2019 – Nov 5, 2019

Subsystems (10)
lore
Share & tools
lore
#0 lore raw

v2 cover cgroup: Sync cgroup id and inode number

Hello,

This patchset changes cgroup inode number and id management to be in sync with kernfs. The cgroup inode number is managed by kernfs but cgroup id is allocated by a separate idr. The idea is to have a single id for internal usage, inode number and file handle which can be accessed from userspace. Actually this work is from Tejun who also provided the idea. I just took over the work and fixed some errors and finally was able to run perf for testing.

The background of this work is that I want to add cgroup sampling feature in the perf event subsystem. As Tejun mentioned that using cgroup id is not enough and it'd better using file handle instead. But getting file handle in perf NMI handler is not possible so I want to get the info from a cgroup node.

The first patch converted kernfs id into a single 64bit number and in the second patch cgroup uses the kernfs id as cgroup id.

The patches are based on the for-next branch in Tejun's cgroup tree. Tested with tools/testing/selftests/cgroup/test_stress.sh.

Thanks
Namhyung

Tejun Heo (2):
kernfs: Convert to u64 id
cgroup: Use 64bit id from kernfs

 fs/kernfs/dir.c                  | 36 +++++++++++++++++++------------
 fs/kernfs/file.c                 |  4 ++--
 fs/kernfs/inode.c                |  4 ++--
 fs/kernfs/kernfs-internal.h      |  2 --
 fs/kernfs/mount.c                | 92 +++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
 include/linux/cgroup-defs.h      | 18 ++++------------
 include/linux/cgroup.h           | 17 +++++++--------
 include/linux/exportfs.h         |  5 +++++
 include/linux/kernfs.h           | 47 +++++++++++++++++++++++++----------------
 include/net/netprio_cgroup.h     |  8 +++----
 include/trace/events/writeback.h | 92 ++++++++++++++++++++++++++++++++++++++++----------------------------------------
 kernel/bpf/helpers.c             |  2 +-
 kernel/cgroup/cgroup.c           | 68 +++++++++++++++++++----------------------------------------
 kernel/trace/blktrace.c          | 66 +++++++++++++++++++++++++++------------------------------
 net/core/filter.c                |  4 ++--
 net/core/netprio_cgroup.c        |  4 ++--
 16 files changed, 236 insertions(+), 233 deletions(-)
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#1 lore raw

PATCH v2 1/2 kernfs: Convert to u64 id

From: Tejun Heo <[email protected]>

The kernfs_id was an union type sharing a 64bit id with 32bit ino + gen. But it resulted in using 32bit inode even on 64bit systems. Also dealing with an union is annoying especially if you just want to use a single id.

Thus let's get rid of the kernfs_node_id type and use u64 directly. The upper 32bit is used for gen and lower is for ino on 32bit systems. The kernfs_id_ino() and kernfs_id_gen() helpers will take care of the bit handling depends on the system word size.

Cc Steven Rostedt <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Jens Axboe <[email protected]>
Cc "David S. Miller" <[email protected]>
[namhyung: fix build error in bpf_get_current_cgroup_id()]
---
 fs/kernfs/dir.c                  | 36 ++++++++-----
 fs/kernfs/file.c                 |  4 +-
 fs/kernfs/inode.c                |  4 +-
 fs/kernfs/kernfs-internal.h      |  2 -
 fs/kernfs/mount.c                | 92 +++++++++++++++++++-------------
 include/linux/cgroup.h           | 17 +++---
 include/linux/exportfs.h         |  5 ++
 include/linux/kernfs.h           | 47 +++++++++-------
 include/trace/events/writeback.h | 92 ++++++++++++++++----------------
 kernel/bpf/helpers.c             |  2 +-
 kernel/cgroup/cgroup.c           |  5 +-
 kernel/trace/blktrace.c          | 66 +++++++++++------------
 net/core/filter.c                |  4 +-
 13 files changed, 207 insertions(+), 169 deletions(-)
fs/kernfs/dir.c +23 -13
index 6ebae6bbe6a5..2beec2af809b 100644
@@ -509,7 +509,7 @@ void kernfs_put(struct kernfs_node *kn)
 	struct kernfs_root *root;
 
 	/*
-	 * kernfs_node is freed with ->count 0, kernfs_find_and_get_node_by_ino
+	 * kernfs_node is freed with ->count 0, kernfs_find_and_get_node_by_id
 	 * depends on this to filter reused stale node
 	 */
 	if (!kn || !atomic_dec_and_test(&kn->count))
@@ -536,7 +536,7 @@ void kernfs_put(struct kernfs_node *kn)
 		kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
 	}
 	spin_lock(&kernfs_idr_lock);
-	idr_remove(&root->ino_idr, kn->id.ino);
+	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
 	spin_unlock(&kernfs_idr_lock);
 	kmem_cache_free(kernfs_node_cache, kn);
 
@@ -644,12 +644,12 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 	idr_preload_end();
 	if (ret < 0)
 		goto err_out2;
-	kn->id.ino = ret;
-	kn->id.generation = gen;
+
+	kn->id = (u64)gen << 32 | ret;
 
 	/*
 	 * set ino first. This RELEASE is paired with atomic_inc_not_zero in
-	 * kernfs_find_and_get_node_by_ino
+	 * kernfs_find_and_get_node_by_id
 	 */
 	atomic_set_release(&kn->count, 1);
 	atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
@@ -680,7 +680,7 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 	return kn;
 
  err_out3:
-	idr_remove(&root->ino_idr, kn->id.ino);
+	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
  err_out2:
 	kmem_cache_free(kernfs_node_cache, kn);
  err_out1:
@@ -705,20 +705,25 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
 }
 
 /*
- * kernfs_find_and_get_node_by_ino - get kernfs_node from inode number
+ * kernfs_find_and_get_node_by_id - get kernfs_node from node id
  * @root: the kernfs root
- * @ino: inode number
+ * @id: the target node id
+ *
+ * @id's lower 32bits encode ino and upper gen.  If the gen portion is
+ * zero, all generations are matched.
  *
  * RETURNS:
  * NULL on failure. Return a kernfs node with reference counter incremented
  */
-struct kernfs_node *kernfs_find_and_get_node_by_ino(struct kernfs_root *root,
-						    unsigned int ino)
+struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
+						   u64 id)
 {
 	struct kernfs_node *kn;
+	ino_t ino = kernfs_id_ino(id);
+	u32 gen = kernfs_id_gen(id);
 
 	rcu_read_lock();
-	kn = idr_find(&root->ino_idr, ino);
+	kn = idr_find(&root->ino_idr, (u32)ino);
 	if (!kn)
 		goto out;
 
@@ -741,8 +746,13 @@ struct kernfs_node *kernfs_find_and_get_node_by_ino(struct kernfs_root *root,
 	 * before 'count'. So if 'count' is uptodate, 'ino' should be uptodate,
 	 * hence we can use 'ino' to filter stale node.
 	 */
-	if (kn->id.ino != ino)
+	if (kernfs_ino(kn) != ino)
 		goto out;
+
+	/* if upper 32bit of @id was zero, ignore gen */
+	if (gen && kernfs_gen(kn) != gen)
+		goto out;
+
 	rcu_read_unlock();
 
 	return kn;
@@ -1678,7 +1688,7 @@ static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
 		const char *name = pos->name;
 		unsigned int type = dt_type(pos);
 		int len = strlen(name);
-		ino_t ino = pos->id.ino;
+		ino_t ino = kernfs_ino(pos);
 
 		ctx->pos = pos->hash;
 		file->private_data = pos;
fs/kernfs/file.c +2 -2
index e8c792b49616..34366db3620d 100644
@@ -892,7 +892,7 @@ static void kernfs_notify_workfn(struct work_struct *work)
 		 * have the matching @file available.  Look up the inodes
 		 * and generate the events manually.
 		 */
-		inode = ilookup(info->sb, kn->id.ino);
+		inode = ilookup(info->sb, kernfs_ino(kn));
 		if (!inode)
 			continue;
 
@@ -901,7 +901,7 @@ static void kernfs_notify_workfn(struct work_struct *work)
 		if (parent) {
 			struct inode *p_inode;
 
-			p_inode = ilookup(info->sb, parent->id.ino);
+			p_inode = ilookup(info->sb, kernfs_ino(parent));
 			if (p_inode) {
 				fsnotify(p_inode, FS_MODIFY | FS_EVENT_ON_CHILD,
 					 inode, FSNOTIFY_EVENT_INODE, &name, 0);
fs/kernfs/inode.c +2 -2
index f3eaa8869f42..eac277c63d42 100644
@@ -201,7 +201,7 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
 	inode->i_private = kn;
 	inode->i_mapping->a_ops = &kernfs_aops;
 	inode->i_op = &kernfs_iops;
-	inode->i_generation = kn->id.generation;
+	inode->i_generation = kernfs_gen(kn);
 
 	set_default_inode_attr(inode, kn->mode);
 	kernfs_refresh_inode(kn, inode);
@@ -247,7 +247,7 @@ struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
 {
 	struct inode *inode;
 
-	inode = iget_locked(sb, kn->id.ino);
+	inode = iget_locked(sb, kernfs_ino(kn));
 	if (inode && (inode->i_state & I_NEW))
 		kernfs_init_inode(kn, inode);
 
fs/kernfs/kernfs-internal.h +0 -2
index 02ce570a9a3c..2f3c51d55261 100644
@@ -109,8 +109,6 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
 				    const char *name, umode_t mode,
 				    kuid_t uid, kgid_t gid,
 				    unsigned flags);
-struct kernfs_node *kernfs_find_and_get_node_by_ino(struct kernfs_root *root,
-						    unsigned int ino);
 
 /*
  * file.c
fs/kernfs/mount.c +56 -36

134 lines not on this page · raw message

include/linux/cgroup.h +8 -9
index f6b048902d6c..4b08a354039c 100644
@@ -616,7 +616,7 @@ static inline bool cgroup_is_populated(struct cgroup *cgrp)
 /* returns ino associated with a cgroup */
 static inline ino_t cgroup_ino(struct cgroup *cgrp)
 {
-	return cgrp->kn->id.ino;
+	return kernfs_ino(cgrp->kn);
 }
 
 /* cft/css accessors for cftype->write() operation */
@@ -687,13 +687,12 @@ static inline void cgroup_kthread_ready(void)
 	current->no_cgroup_migration = 0;
 }
 
-static inline union kernfs_node_id *cgroup_get_kernfs_id(struct cgroup *cgrp)
+static inline u64 cgroup_get_kernfs_id(struct cgroup *cgrp)
 {
-	return &cgrp->kn->id;
+	return cgrp->kn->id;
 }
 
-void cgroup_path_from_kernfs_id(const union kernfs_node_id *id,
-					char *buf, size_t buflen);
+void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen);
 #else /* !CONFIG_CGROUPS */
 
 struct cgroup_subsys_state;
@@ -718,9 +717,9 @@ static inline int cgroup_init_early(void) { return 0; }
 static inline int cgroup_init(void) { return 0; }
 static inline void cgroup_init_kthreadd(void) {}
 static inline void cgroup_kthread_ready(void) {}
-static inline union kernfs_node_id *cgroup_get_kernfs_id(struct cgroup *cgrp)
+static inline u64 cgroup_get_kernfs_id(struct cgroup *cgrp)
 {
-	return NULL;
+	return 0;
 }
 
 static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
@@ -739,8 +738,8 @@ static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
 	return true;
 }
 
-static inline void cgroup_path_from_kernfs_id(const union kernfs_node_id *id,
-	char *buf, size_t buflen) {}
+static inline void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
+{}
 #endif /* !CONFIG_CGROUPS */
 
 #ifdef CONFIG_CGROUPS
include/linux/exportfs.h +5 -0
index cf6571fc9c01..d896b8657085 100644
@@ -104,6 +104,11 @@ enum fid_type {
 	 */
 	FILEID_LUSTRE = 0x97,
 
+	/*
+	 * 64 bit unique kernfs id
+	 */
+	FILEID_KERNFS = 0xfe,
+
 	/*
 	 * Filesystems must not use 0xff file ID.
 	 */
include/linux/kernfs.h +29 -18

71 lines not on this page · raw message

include/trace/events/writeback.h +46 -46

296 lines not on this page · raw message

kernel/bpf/helpers.c +1 -1
index 5e28718928ca..912e761cd17a 100644
@@ -317,7 +317,7 @@ BPF_CALL_0(bpf_get_current_cgroup_id)
 {
 	struct cgroup *cgrp = task_dfl_cgroup(current);
 
-	return cgrp->kn->id.id;
+	return cgrp->kn->id;
 }
 
 const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
kernel/cgroup/cgroup.c +2 -3
index cf32c0c7a45d..b5dcbee5aa6c 100644
@@ -5786,12 +5786,11 @@ static int __init cgroup_wq_init(void)
 }
 core_initcall(cgroup_wq_init);
 
-void cgroup_path_from_kernfs_id(const union kernfs_node_id *id,
-					char *buf, size_t buflen)
+void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
 {
 	struct kernfs_node *kn;
 
-	kn = kernfs_get_node_by_id(cgrp_dfl_root.kf_root, id);
+	kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id);
 	if (!kn)
 		return;
 	kernfs_path(kn, buf, buflen);
kernel/trace/blktrace.c +31 -35

200 lines not on this page · raw message

net/core/filter.c +2 -2
index ed6563622ce3..b360a7beb6fc 100644
@@ -4089,7 +4089,7 @@ BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
 		return 0;
 
 	cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
-	return cgrp->kn->id.id;
+	return cgrp->kn->id;
 }
 
 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
@@ -4114,7 +4114,7 @@ BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
 	if (!ancestor)
 		return 0;
 
-	return ancestor->kn->id.id;
+	return ancestor->kn->id;
 }
 
 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
Signature · 1 line
2.24.0.rc1.363.gb1bccd3e3d-goog
maintainer Acked-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] kernfs: Convert to u64 id

Quoted 37 lines · On Mon, 4 Nov 2019 17:45:19 +0900 Namhyung Kim <[email protected]> wrote:

From: Tejun Heo <[email protected]>

The kernfs_id was an union type sharing a 64bit id with 32bit ino + gen. But it resulted in using 32bit inode even on 64bit systems. Also dealing with an union is annoying especially if you just want to use a single id.

Thus let's get rid of the kernfs_node_id type and use u64 directly. The upper 32bit is used for gen and lower is for ino on 32bit systems. The kernfs_id_ino() and kernfs_id_gen() helpers will take care of the bit handling depends on the system word size.

Cc Steven Rostedt <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Jens Axboe <[email protected]>
Cc "David S. Miller" <[email protected]>
[namhyung: fix build error in bpf_get_current_cgroup_id()]
---
 fs/kernfs/dir.c                  | 36 ++++++++-----
 fs/kernfs/file.c                 |  4 +-
 fs/kernfs/inode.c                |  4 +-
 fs/kernfs/kernfs-internal.h      |  2 -
 fs/kernfs/mount.c                | 92 +++++++++++++++++++-------------
 include/linux/cgroup.h           | 17 +++---
 include/linux/exportfs.h         |  5 ++
 include/linux/kernfs.h           | 47 +++++++++-------
Quoted 1 line
 include/trace/events/writeback.h | 92 ++++++++++++++++----------------

I only looked at the above file, and didn't see anything bad about it.

-- Steve

Quoted 5 lines
 kernel/bpf/helpers.c             |  2 +-
 kernel/cgroup/cgroup.c           |  5 +-
 kernel/trace/blktrace.c          | 66 +++++++++++------------
 net/core/filter.c                |  4 +-
 13 files changed, 207 insertions(+), 169 deletions(-)
#3 lore raw

Re: [PATCH v2 1/2] kernfs: Convert to u64 id

In reply to Namhyung Kim
I love your patch! Yet something to improve:

Hi Namhyung,

I love your patch! Yet something to improve:

[auto build test ERROR on driver-core/driver-core-testing]
[also build test ERROR on v5.4-rc6 next-20191031]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Namhyung-Kim/cgroup-Sync-cgroup-id-and-inode-number/20191104-165256
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git 9927c6fa3e1d941c9b89f807f5d0480390eb0471
config: x86_64-lkp (attached as .config)
compiler: gcc-7 (Debian 7.4.0-14) 7.4.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

kernel/bpf/local_storage.c: In function 'bpf_cgroup_storage_link':

>> kernel/bpf/local_storage.c:572:47: error: request for member 'id' in something not a structure or union
     storage->key.cgroup_inode_id = cgroup->kn->id.id;
                                                  ^

vim +/id +572 kernel/bpf/local_storage.c

de9cbbaadba5ad Roman Gushchin 2018-08-02  561  
de9cbbaadba5ad Roman Gushchin 2018-08-02  562  void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
de9cbbaadba5ad Roman Gushchin 2018-08-02  563  			     struct cgroup *cgroup,
de9cbbaadba5ad Roman Gushchin 2018-08-02  564  			     enum bpf_attach_type type)
de9cbbaadba5ad Roman Gushchin 2018-08-02  565  {
de9cbbaadba5ad Roman Gushchin 2018-08-02  566  	struct bpf_cgroup_storage_map *map;
de9cbbaadba5ad Roman Gushchin 2018-08-02  567  
de9cbbaadba5ad Roman Gushchin 2018-08-02  568  	if (!storage)
de9cbbaadba5ad Roman Gushchin 2018-08-02  569  		return;
de9cbbaadba5ad Roman Gushchin 2018-08-02  570  
de9cbbaadba5ad Roman Gushchin 2018-08-02  571  	storage->key.attach_type = type;
de9cbbaadba5ad Roman Gushchin 2018-08-02 @572  	storage->key.cgroup_inode_id = cgroup->kn->id.id;
de9cbbaadba5ad Roman Gushchin 2018-08-02  573  
de9cbbaadba5ad Roman Gushchin 2018-08-02  574  	map = storage->map;
de9cbbaadba5ad Roman Gushchin 2018-08-02  575  
de9cbbaadba5ad Roman Gushchin 2018-08-02  576  	spin_lock_bh(&map->lock);
de9cbbaadba5ad Roman Gushchin 2018-08-02  577  	WARN_ON(cgroup_storage_insert(map, storage));
de9cbbaadba5ad Roman Gushchin 2018-08-02  578  	list_add(&storage->list, &map->list);
de9cbbaadba5ad Roman Gushchin 2018-08-02  579  	spin_unlock_bh(&map->lock);
de9cbbaadba5ad Roman Gushchin 2018-08-02  580  }
de9cbbaadba5ad Roman Gushchin 2018-08-02  581  

:::::: The code at line 572 was first introduced by commit
:::::: de9cbbaadba5adf88a19e46df61f7054000838f6 bpf: introduce cgroup storage maps

:::::: TO: Roman Gushchin <[email protected]>
:::::: CC: Daniel Borkmann <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#4 lore raw

Re: [PATCH v2 1/2] kernfs: Convert to u64 id

In reply to Namhyung Kim

Hi Namhyung,

url:    https://github.com/0day-ci/linux/commits/Namhyung-Kim/cgroup-Sync-cgroup-id-and-inode-number/20191104-165256
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git 9927c6fa3e1d941c9b89f807f5d0480390eb0471

If you fix the issue, kindly add following tag

smatch warnings:
kernel/trace/blktrace.c:104 trace_note() warn: potential pointer math issue ('t' is a 384 bit pointer)

# https://github.com/0day-ci/linux/commit/a2dbd125b386ea824c182d058d78ddb627bb633c
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout a2dbd125b386ea824c182d058d78ddb627bb633c
vim +104 kernel/trace/blktrace.c

a863055b1066be block/blktrace.c        Jens Axboe               2006-12-04   66  static void trace_note(struct blk_trace *bt, pid_t pid, int action,
a2dbd125b386ea kernel/trace/blktrace.c Tejun Heo                2019-11-04   67  		       const void *data, size_t len, u64 cgid)
2056a782f8e7e6 block/blktrace.c        Jens Axboe               2006-03-23   68  {
2056a782f8e7e6 block/blktrace.c        Jens Axboe               2006-03-23   69  	struct blk_io_trace *t;
                                                                                                             ^
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   70  	struct ring_buffer_event *event = NULL;
e77405ad80f539 kernel/trace/blktrace.c Steven Rostedt           2009-09-02   71  	struct ring_buffer *buffer = NULL;
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   72  	int pc = 0;
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   73  	int cpu = smp_processor_id();
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   74  	bool blk_tracer = blk_tracer_enabled;
a2dbd125b386ea kernel/trace/blktrace.c Tejun Heo                2019-11-04   75  	ssize_t cgid_len = cgid ? sizeof(cgid) : 0;
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   76  
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   77  	if (blk_tracer) {
12883efb670c28 kernel/trace/blktrace.c Steven Rostedt (Red Hat  2013-03-05   78) 		buffer = blk_tr->trace_buffer.buffer;
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   79  		pc = preempt_count();
e77405ad80f539 kernel/trace/blktrace.c Steven Rostedt           2009-09-02   80  		event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
ca1136c99b66b1 kernel/trace/blktrace.c Shaohua Li               2017-07-12   81  						  sizeof(*t) + len + cgid_len,
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   82  						  0, pc);
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   83  		if (!event)
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   84  			return;
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   85  		t = ring_buffer_event_data(event);
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   86  		goto record_it;
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   87  	}
be1c63411addba block/blktrace.c        Olaf Kirch               2006-12-01   88  
c71a896154119f block/blktrace.c        Arnaldo Carvalho de Melo 2009-01-23   89  	if (!bt->rchan)
c71a896154119f block/blktrace.c        Arnaldo Carvalho de Melo 2009-01-23   90  		return;
c71a896154119f block/blktrace.c        Arnaldo Carvalho de Melo 2009-01-23   91  
ca1136c99b66b1 kernel/trace/blktrace.c Shaohua Li               2017-07-12   92  	t = relay_reserve(bt->rchan, sizeof(*t) + len + cgid_len);
d3d9d2a5ea9770 block/blktrace.c        Jens Axboe               2006-12-04   93  	if (t) {
2056a782f8e7e6 block/blktrace.c        Jens Axboe               2006-03-23   94  		t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
2997c8c4a0b179 block/blktrace.c        Ingo Molnar              2008-01-11   95  		t->time = ktime_to_ns(ktime_get());
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27   96  record_it:
2056a782f8e7e6 block/blktrace.c        Jens Axboe               2006-03-23   97  		t->device = bt->dev;
ca1136c99b66b1 kernel/trace/blktrace.c Shaohua Li               2017-07-12   98  		t->action = action | (cgid ? __BLK_TN_CGROUP : 0);
be1c63411addba block/blktrace.c        Olaf Kirch               2006-12-01   99  		t->pid = pid;
be1c63411addba block/blktrace.c        Olaf Kirch               2006-12-01  100  		t->cpu = cpu;
ca1136c99b66b1 kernel/trace/blktrace.c Shaohua Li               2017-07-12  101  		t->pdu_len = len + cgid_len;
a2dbd125b386ea kernel/trace/blktrace.c Tejun Heo                2019-11-04  102  		if (cgid_len)
a2dbd125b386ea kernel/trace/blktrace.c Tejun Heo                2019-11-04  103  			*(u64 *)(t + 1) = cgid;
a2dbd125b386ea kernel/trace/blktrace.c Tejun Heo                2019-11-04 @104  		memcpy((void *)( t + sizeof(*t)) + cgid_len, data, len);
                                                                                                                 ^

This will corrupt memory. It should be:

	memcpy((void *)t + sizeof(*t) + cgid_len, data, len);

Or

	memcpy((void *)(t + 1) + cgid_len, data, len);

I don't know why zero day bot is complaining to you instead of Tejun or Jens...

18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27  105  
18cea4591a9881 kernel/trace/blktrace.c Li Zefan                 2009-03-27  106  		if (blk_tracer)
b7f0c959edfb44 kernel/trace/blktrace.c Steven Rostedt (Red Hat  2015-09-25  107) 			trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc);
d3d9d2a5ea9770 block/blktrace.c        Jens Axboe               2006-12-04  108  	}
2056a782f8e7e6 block/blktrace.c        Jens Axboe               2006-03-23  109  }

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#5 lore raw

PATCH v2 2/2 cgroup: Use 64bit id from kernfs

In reply to Namhyung Kim

From: Tejun Heo <[email protected]>

Use 64 bit id allocated by kernfs instead of using its own idr since it seems not used for saving any information no more. So let's get rid of the cgroup_idr from cgroup_root.

The index of netprio_map is also changed to u64.

Cc "David S. Miller" <[email protected]>
Cc Neil Horman <[email protected]>
[namhyung: split cgroup changes and fix netprio_map access]
---
 include/linux/cgroup-defs.h  | 18 +++--------
 include/net/netprio_cgroup.h |  8 ++---
 kernel/cgroup/cgroup.c       | 63 +++++++++++-------------------------
 net/core/netprio_cgroup.c    |  4 +--
 4 files changed, 29 insertions(+), 64 deletions(-)
include/linux/cgroup-defs.h +4 -14
index 430e219e3aba..3811b3405ddc 100644
@@ -354,15 +354,8 @@ struct cgroup {
 
 	unsigned long flags;		/* "unsigned long" so bitops work */
 
-	/*
-	 * idr allocated in-hierarchy ID.
-	 *
-	 * ID 0 is not used, the ID of the root cgroup is always 1, and a
-	 * new cgroup will be assigned with a smallest available ID.
-	 *
-	 * Allocating/Removing ID must be protected by cgroup_mutex.
-	 */
-	int id;
+	/* in-hierarchy ID allocated from kernfs */
+	u64 id;
 
 	/*
 	 * The depth this cgroup is at.  The root is at depth zero and each
@@ -488,7 +481,7 @@ struct cgroup {
 	struct cgroup_freezer_state freezer;
 
 	/* ids of the ancestors at each level including self */
-	int ancestor_ids[];
+	u64 ancestor_ids[];
 };
 
 /*
@@ -509,7 +502,7 @@ struct cgroup_root {
 	struct cgroup cgrp;
 
 	/* for cgrp->ancestor_ids[0] */
-	int cgrp_ancestor_id_storage;
+	u64 cgrp_ancestor_id_storage;
 
 	/* Number of cgroups in the hierarchy, used only for /proc/cgroups */
 	atomic_t nr_cgrps;
@@ -520,9 +513,6 @@ struct cgroup_root {
 	/* Hierarchy-specific flags */
 	unsigned int flags;
 
-	/* IDs for cgroups in this hierarchy */
-	struct idr cgroup_idr;
-
 	/* The path to use for release notifications. */
 	char release_agent_path[PATH_MAX];
 
include/net/netprio_cgroup.h +4 -4
index cfc9441ef074..cd7c17cf73c7 100644
@@ -15,14 +15,14 @@
 #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
 struct netprio_map {
 	struct rcu_head rcu;
-	u32 priomap_len;
+	u64 priomap_len;
 	u32 priomap[];
 };
 
-static inline u32 task_netprioidx(struct task_struct *p)
+static inline u64 task_netprioidx(struct task_struct *p)
 {
 	struct cgroup_subsys_state *css;
-	u32 idx;
+	u64 idx;
 
 	rcu_read_lock();
 	css = task_css(p, net_prio_cgrp_id);
@@ -41,7 +41,7 @@ static inline void sock_update_netprioidx(struct sock_cgroup_data *skcd)
 
 #else /* !CONFIG_CGROUP_NET_PRIO */
 
-static inline u32 task_netprioidx(struct task_struct *p)
+static inline u64 task_netprioidx(struct task_struct *p)
 {
 	return 0;
 }
kernel/cgroup/cgroup.c +19 -44

156 lines not on this page · raw message

net/core/netprio_cgroup.c +2 -2
index 256b7954b720..326d2d6b86e0 100644
@@ -93,7 +93,7 @@ static int extend_netdev_table(struct net_device *dev, u32 target_idx)
 static u32 netprio_prio(struct cgroup_subsys_state *css, struct net_device *dev)
 {
 	struct netprio_map *map = rcu_dereference_rtnl(dev->priomap);
-	int id = css->cgroup->id;
+	u64 id = css->cgroup->id;
 
 	if (map && id < map->priomap_len)
 		return map->priomap[id];
@@ -113,7 +113,7 @@ static int netprio_set_prio(struct cgroup_subsys_state *css,
 			    struct net_device *dev, u32 prio)
 {
 	struct netprio_map *map;
-	int id = css->cgroup->id;
+	u64 id = css->cgroup->id;
 	int ret;
 
 	/* avoid extending priomap for zero writes */
Signature · 1 line
2.24.0.rc1.363.gb1bccd3e3d-goog
maintainer Acked-by on v2 2/2
#6 lore raw

Re: [PATCH v2 2/2] cgroup: Use 64bit id from kernfs

From: Namhyung Kim <[email protected]>
Date: Mon, 4 Nov 2019 17:45:20 +0900

Quoted 6 lines

From: Tejun Heo <[email protected]>

Use 64 bit id allocated by kernfs instead of using its own idr since it seems not used for saving any information no more. So let's get rid of the cgroup_idr from cgroup_root.

Quoted 8 lines

The index of netprio_map is also changed to u64.

Cc "David S. Miller" <[email protected]>
Cc Neil Horman <[email protected]>
[namhyung: split cgroup changes and fix netprio_map access]
maintainer
#7 lore raw
In reply to Namhyung Kim
Quoted 16 lines · Namhyung Kim

Hello,

This patchset changes cgroup inode number and id management to be in sync with kernfs. The cgroup inode number is managed by kernfs but cgroup id is allocated by a separate idr. The idea is to have a single id for internal usage, inode number and file handle which can be accessed from userspace. Actually this work is from Tejun who also provided the idea. I just took over the work and fixed some errors and finally was able to run perf for testing.

The background of this work is that I want to add cgroup sampling feature in the perf event subsystem. As Tejun mentioned that using cgroup id is not enough and it'd better using file handle instead. But getting file handle in perf NMI handler is not possible so I want to get the info from a cgroup node.

Quoted 5 lines

The first patch converted kernfs id into a single 64bit number and in the second patch cgroup uses the kernfs id as cgroup id.

The patches are based on the for-next branch in Tejun's cgroup tree. Tested with tools/testing/selftests/cgroup/test_stress.sh.

Sorry about the delay. I'm still working on the patch series and the draft patches currently posted aren't quite correct (e.g. netprio is now trying to allocate arrays which use u64 values as index and that u64 value can start with 33rd bit set from the get-go).

So, please hold on w/ this series for now.

Thanks.

Signature · 1 line
tejun