Skip to contentSkip to conversation

cgroup: Sync cgroup id and inode number

PATCH Superseded Namhyung Kim 2 participants 15 messages Oct 16, 2019 – Nov 1, 2019

Subsystems (12)
Networking x86 Block layer BPF +8 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover cgroup: Sync cgroup id and inode number

Hello,

This patchset changes cgroup inode number and id management to be in sync with kernfs. Currently cgroup inode is managed by kernfs while cgroup id is allocated by its own idr. And kernfs/cgroup file handle uses inode and generation numbers. So I added generation number to cgroup and pass the numbers to kernfs.

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 added generation number to cgroup and the second patch allows kernfs node created with external generation and inode numbers.

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

Namhyung Kim (2):
cgroup: Add generation number with cgroup id
kernfs: Allow creation with external gen + ino numbers

 arch/x86/kernel/cpu/resctrl/rdtgroup.c |  4 +-
 fs/kernfs/dir.c                        | 63 +++++++++++++++++------
 fs/kernfs/file.c                       |  9 ++--
 fs/kernfs/kernfs-internal.h            |  5 ++
 fs/sysfs/file.c                        |  2 +-
 include/linux/cgroup-defs.h            | 12 ++++-
 include/linux/kernfs.h                 | 25 ++++++---
 kernel/cgroup/cgroup.c                 | 70 +++++++++++++++++++-------
 8 files changed, 143 insertions(+), 47 deletions(-)
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#1 lore raw

PATCH 1/2 cgroup: Add generation number with cgroup id

Current cgroup id is 32-bit and might be recycled while system is running. To support unique id, add generation number (gen) to catch recycling and make 64 bit number. This number will be used as kernfs id and inode number (and file handle).

Also introduced cgroup_idr struct to keep the idr and generation together. The related functions are little bit modified as well and I made some change to cgroup_idr_alloc() to use cyclic allocator.

Later 64 bit system can have a simpler implementation with a single 64 bit sequence number and a RB tree. But it'll need to grab a spinlock during lookup. I'm not entirely sure it's ok, so I left it as is.

---
 include/linux/cgroup-defs.h | 12 ++++++--
 kernel/cgroup/cgroup.c      | 58 +++++++++++++++++++++++++++----------
 2 files changed, 53 insertions(+), 17 deletions(-)
include/linux/cgroup-defs.h +10 -2
index 430e219e3aba..e3f2b47c5c7b 100644
@@ -364,6 +364,9 @@ struct cgroup {
 	 */
 	int id;
 
+	/* generation number in case of recycled id */
+	int gen;
+
 	/*
 	 * The depth this cgroup is at.  The root is at depth zero and each
 	 * step down the hierarchy increments the level.  This along with
@@ -491,6 +494,11 @@ struct cgroup {
 	int ancestor_ids[];
 };
 
+struct cgroup_idr {
+	struct idr idr;
+	int generation;
+};
+
 /*
  * A cgroup_root represents the root of a cgroup hierarchy, and may be
  * associated with a kernfs_root to form an active hierarchy.  This is
@@ -521,7 +529,7 @@ struct cgroup_root {
 	unsigned int flags;
 
 	/* IDs for cgroups in this hierarchy */
-	struct idr cgroup_idr;
+	struct cgroup_idr cgroup_idr;
 
 	/* The path to use for release notifications. */
 	char release_agent_path[PATH_MAX];
@@ -701,7 +709,7 @@ struct cgroup_subsys {
 	struct cgroup_root *root;
 
 	/* idr for css->id */
-	struct idr css_idr;
+	struct cgroup_idr css_idr;
 
 	/*
 	 * List of cftypes.  Each entry is the first entry of an array
kernel/cgroup/cgroup.c +43 -15

147 lines not on this page · raw message

Signature · 1 line
2.23.0.700.g56cf767bdb-goog
maintainer
#2 lore raw

Re: [PATCH 1/2] cgroup: Add generation number with cgroup id

Hello,

First of all, thanks a lot for working on this.

Quoted 5 lines · Namhyung Kim

Current cgroup id is 32-bit and might be recycled while system is running. To support unique id, add generation number (gen) to catch recycling and make 64 bit number. This number will be used as kernfs id and inode number (and file handle).

Quoted 7 lines

Also introduced cgroup_idr struct to keep the idr and generation together. The related functions are little bit modified as well and I made some change to cgroup_idr_alloc() to use cyclic allocator.

Later 64 bit system can have a simpler implementation with a single 64 bit sequence number and a RB tree. But it'll need to grab a spinlock during lookup. I'm not entirely sure it's ok, so I left it as is.

Any chance I can persuade you into making this conversion? idr is exactly the wrong data structure to use for cyclic allocations. We've been doing it mostly for historical reasons but I really hope we can move away from it. These lookups aren't in super hot paths and doing locked lookups should be fine.

include/linux/cgroup-defs.h struct cgroup_root { · line 532

Quoted 9 lines

/*
* A cgroup_root represents the root of a cgroup hierarchy, and may be
* associated with a kernfs_root to form an active hierarchy. This is

include/linux/cgroup-defs.h +1 -1
@@ -521,7 +529,7 @@ struct cgroup_root {
 	unsigned int flags;
 
 	/* IDs for cgroups in this hierarchy */
-	struct idr cgroup_idr;
+	struct cgroup_idr cgroup_idr;

Given that there's cgroup->self css, can we get rid of the above? Also, can we make css->id a 64bit value too?

Thanks.

Signature · 1 line
tejun
#3 lore raw

Re: [PATCH 1/2] cgroup: Add generation number with cgroup id

Hi Tejun,

Quoted 10 lines · Tejun Heo
Quoted 3 lines · Namhyung Kim

Later 64 bit system can have a simpler implementation with a single 64 bit sequence number and a RB tree. But it'll need to grab a spinlock during lookup. I'm not entirely sure it's ok, so I left it as is.

Any chance I can persuade you into making this conversion? idr is exactly the wrong data structure to use for cyclic allocations. We've been doing it mostly for historical reasons but I really hope we can move away from it. These lookups aren't in super hot paths and doing locked lookups should be fine.

As you know, it entails change in kernfs id and its users. And I really want to finish the perf cgroup sampling work first. Can I work on this after the perf work is done?

Quoted 7 lines
Quoted 7 lines

/*
* A cgroup_root represents the root of a cgroup hierarchy, and may be
* associated with a kernfs_root to form an active hierarchy. This is

include/linux/cgroup-defs.h +0 -0
@@ -521,7 +529,7 @@ struct cgroup_root {
      unsigned int flags;
 
Quoted 5 lines

include/linux/cgroup-defs.h struct cgroup_root { · line 532

Quoted 3 lines
include/linux/cgroup-defs.h +1 -1
      /* IDs for cgroups in this hierarchy */
-     struct idr cgroup_idr;
+     struct cgroup_idr cgroup_idr;

Given that there's cgroup->self css, can we get rid of the above?

I don't follow. Do you want to remove cgroup_idr and share the css_idr for cgroup id?

Thanks
Namhyung

#4 lore raw

Re: [PATCH 1/2] cgroup: Add generation number with cgroup id

Quoted 6 lines · Namhyung Kim
Quoted 6 lines
Quoted 6 lines

/*
* A cgroup_root represents the root of a cgroup hierarchy, and may be
* associated with a kernfs_root to form an active hierarchy. This is

include/linux/cgroup-defs.h +0 -0
@@ -521,7 +529,7 @@ struct cgroup_root {
      unsigned int flags;
 
Quoted 8 lines
Quoted 5 lines

include/linux/cgroup-defs.h struct cgroup_root { · line 532

Quoted 3 lines
include/linux/cgroup-defs.h +1 -1
      /* IDs for cgroups in this hierarchy */
-     struct idr cgroup_idr;
+     struct cgroup_idr cgroup_idr;

Given that there's cgroup->self css, can we get rid of the above?

I don't follow. Do you want to remove cgroup_idr and share the css_idr for cgroup id?

Or, you don't want to add cgroup_idr struct and use idr directly?

Thanks
Namhyung

maintainer
#5 lore raw

Re: [PATCH 1/2] cgroup: Add generation number with cgroup id

Hello,

Quoted 7 lines · Namhyung Kim
Quoted 6 lines · Namhyung Kim
Quoted 6 lines
Quoted 6 lines

/*
* A cgroup_root represents the root of a cgroup hierarchy, and may be
* associated with a kernfs_root to form an active hierarchy. This is

include/linux/cgroup-defs.h +0 -0
@@ -521,7 +529,7 @@ struct cgroup_root {
      unsigned int flags;
 
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

include/linux/cgroup-defs.h struct cgroup_root { · line 532

Quoted 3 lines
include/linux/cgroup-defs.h +1 -1
      /* IDs for cgroups in this hierarchy */
-     struct idr cgroup_idr;
+     struct cgroup_idr cgroup_idr;

Given that there's cgroup->self css, can we get rid of the above?

I don't follow. Do you want to remove cgroup_idr and share the css_idr for cgroup id?

Yeah, so, each cgroup has its own css at cgroup->self which has css id and everything, so I was wondering whether it'd make sense to get rid of the cgroup id and use cgroup->self.id in its place.

Thanks.

Signature · 1 line
tejun
#6 lore raw

Re: [PATCH 1/2] cgroup: Add generation number with cgroup id

Hi Tejun,

Quoted 17 lines · Tejun Heo

Hello,

Quoted 13 lines · Namhyung Kim
Quoted 12 lines · Namhyung Kim
Quoted 11 lines

include/linux/cgroup-defs.h struct cgroup_root { · line 532

Quoted 9 lines

/*
* A cgroup_root represents the root of a cgroup hierarchy, and may be
* associated with a kernfs_root to form an active hierarchy. This is

include/linux/cgroup-defs.h +1 -1
@@ -521,7 +529,7 @@ struct cgroup_root {
      unsigned int flags;
 
      /* IDs for cgroups in this hierarchy */
-     struct idr cgroup_idr;
+     struct cgroup_idr cgroup_idr;

Given that there's cgroup->self css, can we get rid of the above?

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

I don't follow. Do you want to remove cgroup_idr and share the css_idr for cgroup id?

Yeah, so, each cgroup has its own css at cgroup->self which has css id and everything, so I was wondering whether it'd make sense to get rid of the cgroup id and use cgroup->self.id in its place.

I think it's possible to use self.id as cgroup id. But css->id is managed for each subsys, so it still needs to have an idr in cgroup_root for cgroup->self.id, right?

Or do you want to get rid of subsys->idr and use a single idr for both cgroup and css?

Thanks
Namhyung

maintainer
#7 lore raw

Re: [PATCH 1/2] cgroup: Add generation number with cgroup id

In reply to Namhyung Kim

Hello,

Quoted 9 lines · Namhyung Kim
Quoted 5 lines

Any chance I can persuade you into making this conversion? idr is exactly the wrong data structure to use for cyclic allocations. We've been doing it mostly for historical reasons but I really hope we can move away from it. These lookups aren't in super hot paths and doing locked lookups should be fine.

As you know, it entails change in kernfs id and its users. And I really want to finish the perf cgroup sampling work first. Can I work on this after the perf work is done?

Sure, but I think we should get the userland visible behaviors right. Ignoring implementation details:

* cgroup vs. css IDs doesn't matter for now. css IDs aren't visible to userland anyway and it could be that keeping using idr as-is or always using 64bit IDs is the better solution for them.

* On 32bit ino setups, 32bit ino + gen as cgroup and export fs IDs.

* On 64bit ino setups, 64bit unique ino (allocated whichever way) + 0 gen as cgroup and export fs IDs.

Thanks.

Signature · 1 line
tejun
maintainer
#8 lore raw

Re: [PATCH 1/2] cgroup: Add generation number with cgroup id

Quoted 16 lines · Tejun Heo

Hello,

Quoted 9 lines · Namhyung Kim
Quoted 5 lines

Any chance I can persuade you into making this conversion? idr is exactly the wrong data structure to use for cyclic allocations. We've been doing it mostly for historical reasons but I really hope we can move away from it. These lookups aren't in super hot paths and doing locked lookups should be fine.

As you know, it entails change in kernfs id and its users. And I really want to finish the perf cgroup sampling work first. Can I work on this after the perf work is done?

Sure, but I think we should get the userland visible behaviors right. Ignoring implementation details:

Quoted 8 lines

* cgroup vs. css IDs doesn't matter for now. css IDs aren't visible to userland anyway and it could be that keeping using idr as-is or always using 64bit IDs is the better solution for them.

* On 32bit ino setups, 32bit ino + gen as cgroup and export fs IDs.

* On 64bit ino setups, 64bit unique ino (allocated whichever way) + 0 gen as cgroup and export fs IDs.

So, something like the following. Just to show the direction. Only compile tested and full of holes. I'll see if I can get it working over the weekend.

include/trace/events/writeback.h +46 -46
Index: work/include/trace/events/writeback.h
===================================================================
@@ -61,7 +61,7 @@ DECLARE_EVENT_CLASS(writeback_page_templ
 
 	TP_STRUCT__entry (
 		__array(char, name, 32)
-		__field(unsigned long, ino)
+		__field(ino_t, ino)
 		__field(pgoff_t, index)
 	),
 
@@ -102,7 +102,7 @@ DECLARE_EVENT_CLASS(writeback_dirty_inod
 
 	TP_STRUCT__entry (
 		__array(char, name, 32)
-		__field(unsigned long, ino)
+		__field(ino_t, ino)
 		__field(unsigned long, state)
 		__field(unsigned long, flags)
 	),
@@ -150,28 +150,28 @@ DEFINE_EVENT(writeback_dirty_inode_templ
 #ifdef CREATE_TRACE_POINTS
 #ifdef CONFIG_CGROUP_WRITEBACK
 
-static inline unsigned int __trace_wb_assign_cgroup(struct bdi_writeback *wb)
+static inline ino_t __trace_wb_assign_cgroup(struct bdi_writeback *wb)
 {
-	return wb->memcg_css->cgroup->kn->id.ino;
+	return cgroup_ino(wb->memcg_css->cgroup);
 }
 
-static inline unsigned int __trace_wbc_assign_cgroup(struct writeback_control *wbc)
+static inline ino_t __trace_wbc_assign_cgroup(struct writeback_control *wbc)
 {
 	if (wbc->wb)
 		return __trace_wb_assign_cgroup(wbc->wb);
 	else
-		return -1U;
+		return 0;
 }
 #else	/* CONFIG_CGROUP_WRITEBACK */
 
-static inline unsigned int __trace_wb_assign_cgroup(struct bdi_writeback *wb)
+static inline ino_t __trace_wb_assign_cgroup(struct bdi_writeback *wb)
 {
-	return -1U;
+	return 0;
 }
 
-static inline unsigned int __trace_wbc_assign_cgroup(struct writeback_control *wbc)
+static inline ino_t __trace_wbc_assign_cgroup(struct writeback_control *wbc)
 {
-	return -1U;
+	return 0;
 }
 
 #endif	/* CONFIG_CGROUP_WRITEBACK */
@@ -187,8 +187,8 @@ TRACE_EVENT(inode_foreign_history,
 
 	TP_STRUCT__entry(
 		__array(char,		name, 32)
-		__field(unsigned long,	ino)
-		__field(unsigned int,	cgroup_ino)
+		__field(ino_t,		ino)
+		__field(ino_t,		cgroup_ino)
 		__field(unsigned int,	history)
 	),
 
@@ -199,7 +199,7 @@ TRACE_EVENT(inode_foreign_history,
 		__entry->history	= history;
 	),
 
-	TP_printk("bdi %s: ino=%lu cgroup_ino=%u history=0x%x",
+	TP_printk("bdi %s: ino=%lu cgroup_ino=%lu history=0x%x",
 		__entry->name,
 		__entry->ino,
 		__entry->cgroup_ino,
@@ -216,9 +216,9 @@ TRACE_EVENT(inode_switch_wbs,
 
 	TP_STRUCT__entry(
 		__array(char,		name, 32)
-		__field(unsigned long,	ino)
-		__field(unsigned int,	old_cgroup_ino)
-		__field(unsigned int,	new_cgroup_ino)
+		__field(ino_t,		ino)
+		__field(ino_t,		old_cgroup_ino)
+		__field(ino_t,		new_cgroup_ino)
 	),
 
 	TP_fast_assign(
@@ -228,7 +228,7 @@ TRACE_EVENT(inode_switch_wbs,
 		__entry->new_cgroup_ino	= __trace_wb_assign_cgroup(new_wb);
 	),
 
-	TP_printk("bdi %s: ino=%lu old_cgroup_ino=%u new_cgroup_ino=%u",
+	TP_printk("bdi %s: ino=%lu old_cgroup_ino=%lu new_cgroup_ino=%lu",
 		__entry->name,
 		__entry->ino,
 		__entry->old_cgroup_ino,
@@ -245,10 +245,10 @@ TRACE_EVENT(track_foreign_dirty,
 	TP_STRUCT__entry(
 		__array(char,		name, 32)
 		__field(u64,		bdi_id)
-		__field(unsigned long,	ino)
+		__field(ino_t,		ino)
 		__field(unsigned int,	memcg_id)
-		__field(unsigned int,	cgroup_ino)
-		__field(unsigned int,	page_cgroup_ino)
+		__field(ino_t,		cgroup_ino)
+		__field(ino_t,		page_cgroup_ino)
 	),
 
 	TP_fast_assign(
@@ -260,10 +260,10 @@ TRACE_EVENT(track_foreign_dirty,
 		__entry->ino		= inode ? inode->i_ino : 0;
 		__entry->memcg_id	= wb->memcg_css->id;
 		__entry->cgroup_ino	= __trace_wb_assign_cgroup(wb);
-		__entry->page_cgroup_ino = page->mem_cgroup->css.cgroup->kn->id.ino;
+		__entry->page_cgroup_ino = cgroup_ino(page->mem_cgroup->css.cgroup);
 	),
 
-	TP_printk("bdi %s[%llu]: ino=%lu memcg_id=%u cgroup_ino=%u page_cgroup_ino=%u",
+	TP_printk("bdi %s[%llu]: ino=%lu memcg_id=%u cgroup_ino=%lu page_cgroup_ino=%lu",
 		__entry->name,
 		__entry->bdi_id,
 		__entry->ino,
@@ -282,7 +282,7 @@ TRACE_EVENT(flush_foreign,
 
 	TP_STRUCT__entry(
 		__array(char,		name, 32)
-		__field(unsigned int,	cgroup_ino)
+		__field(ino_t,		cgroup_ino)
 		__field(unsigned int,	frn_bdi_id)
 		__field(unsigned int,	frn_memcg_id)
 	),
@@ -294,7 +294,7 @@ TRACE_EVENT(flush_foreign,
 		__entry->frn_memcg_id	= frn_memcg_id;
 	),
 
-	TP_printk("bdi %s: cgroup_ino=%u frn_bdi_id=%u frn_memcg_id=%u",
+	TP_printk("bdi %s: cgroup_ino=%lu frn_bdi_id=%u frn_memcg_id=%u",
 		__entry->name,
 		__entry->cgroup_ino,
 		__entry->frn_bdi_id,
@@ -311,9 +311,9 @@ DECLARE_EVENT_CLASS(writeback_write_inod
 
 	TP_STRUCT__entry (
 		__array(char, name, 32)
-		__field(unsigned long, ino)
+		__field(ino_t, ino)
 		__field(int, sync_mode)
-		__field(unsigned int, cgroup_ino)
+		__field(ino_t, cgroup_ino)
 	),
 
 	TP_fast_assign(
@@ -324,7 +324,7 @@ DECLARE_EVENT_CLASS(writeback_write_inod
 		__entry->cgroup_ino	= __trace_wbc_assign_cgroup(wbc);
 	),
 
-	TP_printk("bdi %s: ino=%lu sync_mode=%d cgroup_ino=%u",
+	TP_printk("bdi %s: ino=%lu sync_mode=%d cgroup_ino=%lu",
 		__entry->name,
 		__entry->ino,
 		__entry->sync_mode,
@@ -358,7 +358,7 @@ DECLARE_EVENT_CLASS(writeback_work_class
 		__field(int, range_cyclic)
 		__field(int, for_background)
 		__field(int, reason)
-		__field(unsigned int, cgroup_ino)
+		__field(ino_t, cgroup_ino)
 	),
 	TP_fast_assign(
 		strscpy_pad(__entry->name,
@@ -374,7 +374,7 @@ DECLARE_EVENT_CLASS(writeback_work_class
 		__entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
 	),
 	TP_printk("bdi %s: sb_dev %d:%d nr_pages=%ld sync_mode=%d "
-		  "kupdate=%d range_cyclic=%d background=%d reason=%s cgroup_ino=%u",
+		  "kupdate=%d range_cyclic=%d background=%d reason=%s cgroup_ino=%lu",
 		  __entry->name,
 		  MAJOR(__entry->sb_dev), MINOR(__entry->sb_dev),
 		  __entry->nr_pages,
@@ -413,13 +413,13 @@ DECLARE_EVENT_CLASS(writeback_class,
 	TP_ARGS(wb),
 	TP_STRUCT__entry(
 		__array(char, name, 32)
-		__field(unsigned int, cgroup_ino)
+		__field(ino_t, cgroup_ino)
 	),
 	TP_fast_assign(
 		strscpy_pad(__entry->name, dev_name(wb->bdi->dev), 32);
 		__entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
 	),
-	TP_printk("bdi %s: cgroup_ino=%u",
+	TP_printk("bdi %s: cgroup_ino=%lu",
 		  __entry->name,
 		  __entry->cgroup_ino
 	)
@@ -459,7 +459,7 @@ DECLARE_EVENT_CLASS(wbc_class,
 		__field(int, range_cyclic)
 		__field(long, range_start)
 		__field(long, range_end)
-		__field(unsigned int, cgroup_ino)
+		__field(ino_t, cgroup_ino)
 	),
 
 	TP_fast_assign(
@@ -478,7 +478,7 @@ DECLARE_EVENT_CLASS(wbc_class,
 
 	TP_printk("bdi %s: towrt=%ld skip=%ld mode=%d kupd=%d "
 		"bgrd=%d reclm=%d cyclic=%d "
-		"start=0x%lx end=0x%lx cgroup_ino=%u",
+		"start=0x%lx end=0x%lx cgroup_ino=%lu",
 		__entry->name,
 		__entry->nr_to_write,
 		__entry->pages_skipped,
@@ -510,7 +510,7 @@ TRACE_EVENT(writeback_queue_io,
 		__field(long,		age)
 		__field(int,		moved)
 		__field(int,		reason)
-		__field(unsigned int,	cgroup_ino)
+		__field(ino_t,		cgroup_ino)
 	),
 	TP_fast_assign(
 		unsigned long *older_than_this = work->older_than_this;
@@ -522,7 +522,7 @@ TRACE_EVENT(writeback_queue_io,
 		__entry->reason	= work->reason;
 		__entry->cgroup_ino	= __trace_wb_assign_cgroup(wb);
 	),
-	TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup_ino=%u",
+	TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup_ino=%lu",
 		__entry->name,
 		__entry->older,	/* older_than_this in jiffies */
 		__entry->age,	/* older_than_this in relative milliseconds */
@@ -596,7 +596,7 @@ TRACE_EVENT(bdi_dirty_ratelimit,
 		__field(unsigned long,	dirty_ratelimit)
 		__field(unsigned long,	task_ratelimit)
 		__field(unsigned long,	balanced_dirty_ratelimit)
-		__field(unsigned int,	cgroup_ino)
+		__field(ino_t,		cgroup_ino)
 	),
 
 	TP_fast_assign(
@@ -614,7 +614,7 @@ TRACE_EVENT(bdi_dirty_ratelimit,
 	TP_printk("bdi %s: "
 		  "write_bw=%lu awrite_bw=%lu dirty_rate=%lu "
 		  "dirty_ratelimit=%lu task_ratelimit=%lu "
-		  "balanced_dirty_ratelimit=%lu cgroup_ino=%u",
+		  "balanced_dirty_ratelimit=%lu cgroup_ino=%lu",
 		  __entry->bdi,
 		  __entry->write_bw,		/* write bandwidth */
 		  __entry->avg_write_bw,	/* avg write bandwidth */
@@ -660,7 +660,7 @@ TRACE_EVENT(balance_dirty_pages,
 		__field(	 long,	pause)
 		__field(unsigned long,	period)
 		__field(	 long,	think)
-		__field(unsigned int,	cgroup_ino)
+		__field(ino_t,		cgroup_ino)
 	),
 
 	TP_fast_assign(
@@ -692,7 +692,7 @@ TRACE_EVENT(balance_dirty_pages,
 		  "bdi_setpoint=%lu bdi_dirty=%lu "
 		  "dirty_ratelimit=%lu task_ratelimit=%lu "
 		  "dirtied=%u dirtied_pause=%u "
-		  "paused=%lu pause=%ld period=%lu think=%ld cgroup_ino=%u",
+		  "paused=%lu pause=%ld period=%lu think=%ld cgroup_ino=%lu",
 		  __entry->bdi,
 		  __entry->limit,
 		  __entry->setpoint,
@@ -718,10 +718,10 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 
 	TP_STRUCT__entry(
 		__array(char, name, 32)
-		__field(unsigned long, ino)
+		__field(ino_t, ino)
 		__field(unsigned long, state)
 		__field(unsigned long, dirtied_when)
-		__field(unsigned int, cgroup_ino)
+		__field(ino_t, cgroup_ino)
 	),
 
 	TP_fast_assign(
@@ -733,7 +733,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		__entry->cgroup_ino	= __trace_wb_assign_cgroup(inode_to_wb(inode));
 	),
 
-	TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu cgroup_ino=%u",
+	TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu cgroup_ino=%lu",
 		  __entry->name,
 		  __entry->ino,
 		  show_inode_state(__entry->state),
@@ -789,13 +789,13 @@ DECLARE_EVENT_CLASS(writeback_single_ino
 
 	TP_STRUCT__entry(
 		__array(char, name, 32)
-		__field(unsigned long, ino)
+		__field(ino_t, ino)
 		__field(unsigned long, state)
 		__field(unsigned long, dirtied_when)
 		__field(unsigned long, writeback_index)
 		__field(long, nr_to_write)
 		__field(unsigned long, wrote)
-		__field(unsigned int, cgroup_ino)
+		__field(ino_t, cgroup_ino)
 	),
 
 	TP_fast_assign(
@@ -811,7 +811,7 @@ DECLARE_EVENT_CLASS(writeback_single_ino
 	),
 
 	TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu "
-		  "index=%lu to_write=%ld wrote=%lu cgroup_ino=%u",
+		  "index=%lu to_write=%ld wrote=%lu cgroup_ino=%lu",
 		  __entry->name,
 		  __entry->ino,
 		  show_inode_state(__entry->state),
@@ -845,7 +845,7 @@ DECLARE_EVENT_CLASS(writeback_inode_temp
 
 	TP_STRUCT__entry(
 		__field(	dev_t,	dev			)
-		__field(unsigned long,	ino			)
+		__field(	ino_t,	ino			)
 		__field(unsigned long,	state			)
 		__field(	__u16, mode			)
 		__field(unsigned long, dirtied_when		)
fs/kernfs/dir.c +23 -13
Index: work/fs/kernfs/dir.c
===================================================================
@@ -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, 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_
 	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_
 	return kn;
 
  err_out3:
-	idr_remove(&root->ino_idr, kn->id.ino);
+	idr_remove(&root->ino_idr, kernfs_ino(kn));
  err_out2:
 	kmem_cache_free(kernfs_node_cache, kn);
  err_out1:
@@ -705,20 +705,25 @@ struct kernfs_node *kernfs_new_node(stru
 }
 
 /*
- * 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_
 	 * 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 fil
 		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: work/fs/kernfs/file.c
===================================================================
@@ -892,7 +892,7 @@ repeat:
 		 * 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 @@ repeat:
 		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: work/fs/kernfs/inode.c
===================================================================
@@ -201,7 +201,7 @@ static void kernfs_init_inode(struct ker
 	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 su
 {
 	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: work/fs/kernfs/kernfs-internal.h
===================================================================
@@ -109,8 +109,6 @@ struct kernfs_node *kernfs_new_node(stru
 				    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: work/include/linux/cgroup.h
===================================================================
@@ -616,7 +616,7 @@ static inline bool cgroup_is_populated(s
 /* 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(
 	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
 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 union 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_hie
 	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/kernfs.h +29 -18

71 lines not on this page · raw message

kernel/cgroup/cgroup.c +22 -48

175 lines not on this page · raw message

kernel/trace/blktrace.c +31 -35

200 lines not on this page · raw message

net/core/filter.c +2 -2
Index: work/net/core/filter.c
===================================================================
@@ -4089,7 +4089,7 @@ BPF_CALL_1(bpf_skb_cgroup_id, const stru
 		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, c
 	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 = {
include/linux/exportfs.h +5 -0
Index: work/include/linux/exportfs.h
===================================================================
@@ -105,6 +105,11 @@ enum fid_type {
 	FILEID_LUSTRE = 0x97,
 
 	/*
+	 * 64 bit unique kernfs id
+	 */
+	FILEID_KERNFS = 0xfe,
+
+	/*
 	 * Filesystems must not use 0xff file ID.
 	 */
 	FILEID_INVALID = 0xff,
#9 lore raw

Re: [PATCH 1/2] cgroup: Add generation number with cgroup id

Quoted 3 lines · Tejun Heo

So, something like the following. Just to show the direction. Only compile tested and full of holes. I'll see if I can get it working over the weekend.

Thanks a lot for doing this!
Namhyung

#10 lore raw

Re: [PATCH 1/2] cgroup: Add generation number with cgroup id

Hi Tejun,

Quoted 5 lines · Namhyung Kim
Quoted 3 lines · Tejun Heo

So, something like the following. Just to show the direction. Only compile tested and full of holes. I'll see if I can get it working over the weekend.

Any update on this? In the meantime, I played with it and managed to run the cgroup selftest. I uploaded it to my tree at

git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git

on the "cgroup/id-rework" branch. I can send it to the list if you want.

Thanks,
Namhyung

#11 lore raw

Re: [PATCH 1/2] cgroup: Add generation number with cgroup id

In reply to Tejun Heo
Quoted 6 lines · Tejun Heo

Sure, but I think we should get the userland visible behaviors right. Ignoring implementation details:

* cgroup vs. css IDs doesn't matter for now. css IDs aren't visible to userland anyway and it could be that keeping using idr as-is or always using 64bit IDs is the better solution for them.

Yes, it can be done easily IMHO.

Quoted 2 lines

* On 32bit ino setups, 32bit ino + gen as cgroup and export fs IDs.

This is the current behavior, right?

Quoted 3 lines

* On 64bit ino setups, 64bit unique ino (allocated whichever way) + 0 gen as cgroup and export fs IDs.

Hmm.. we still need gen for 64bit? Do you want 12-byte export fs ID?

Thanks
Namhyung

#12 lore raw

PATCH 2/2 kernfs: Allow creation with external gen + ino numbers

In reply to Namhyung Kim

Extend file and directory creation API to take external generation number and inode number. Passing 0 as inode number will keep original behavior.

The cgroup id will be used as inode number from now on so allocate id for each file as well.

Cc Fenghua Yu <[email protected]>
Cc Reinette Chatre <[email protected]>
---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c |  4 +-
 fs/kernfs/dir.c                        | 63 ++++++++++++++++++++------
 fs/kernfs/file.c                       |  9 ++--
 fs/kernfs/kernfs-internal.h            |  5 ++
 fs/sysfs/file.c                        |  2 +-
 include/linux/kernfs.h                 | 25 +++++++---
 kernel/cgroup/cgroup.c                 | 12 ++++-
 7 files changed, 90 insertions(+), 30 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -2
index a46dee8e78db..a2fbcab3189e 100644
@@ -206,7 +206,7 @@ static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
 
 	kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
 				  GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
-				  0, rft->kf_ops, rft, NULL, NULL);
+				  0, rft->kf_ops, rft, NULL, NULL, 0, 0);
 	if (IS_ERR(kn))
 		return PTR_ERR(kn);
 
@@ -2294,7 +2294,7 @@ static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
 
 	kn = __kernfs_create_file(parent_kn, name, 0444,
 				  GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0,
-				  &kf_mondata_ops, priv, NULL, NULL);
+				  &kf_mondata_ops, priv, NULL, NULL, 0, 0);
 	if (IS_ERR(kn))
 		return PTR_ERR(kn);
 
fs/kernfs/dir.c +48 -15

110 lines not on this page · raw message

fs/kernfs/file.c +6 -3
index e8c792b49616..8280b750b733 100644
@@ -981,6 +981,8 @@ const struct file_operations kernfs_file_fops = {
  * @priv: private data for the file
  * @ns: optional namespace tag of the file
  * @key: lockdep key for the file's active_ref, %NULL to disable lockdep
+ * @gen: optional inode generation number
+ * @ino: optional inode number
  *
  * Returns the created node on success, ERR_PTR() value on error.
  */
@@ -990,7 +992,8 @@ struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
 					 loff_t size,
 					 const struct kernfs_ops *ops,
 					 void *priv, const void *ns,
-					 struct lock_class_key *key)
+					 struct lock_class_key *key,
+					 u32 gen, int ino)
 {
 	struct kernfs_node *kn;
 	unsigned flags;
@@ -998,8 +1001,8 @@ struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
 
 	flags = KERNFS_FILE;
 
-	kn = kernfs_new_node(parent, name, (mode & S_IALLUGO) | S_IFREG,
-			     uid, gid, flags);
+	kn = kernfs_new_node_with_id(parent, name, (mode & S_IALLUGO) | S_IFREG,
+				     uid, gid, gen, ino, flags);
 	if (!kn)
 		return ERR_PTR(-ENOMEM);
 
fs/kernfs/kernfs-internal.h +5 -0
index 02ce570a9a3c..42c787720a1f 100644
@@ -109,6 +109,11 @@ 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_new_node_with_id(struct kernfs_node *parent,
+					    const char *name, umode_t mode,
+					    kuid_t uid, kgid_t gid,
+					    u32 gen, int ino,
+					    unsigned flags);
 struct kernfs_node *kernfs_find_and_get_node_by_ino(struct kernfs_root *root,
 						    unsigned int ino);
 
fs/sysfs/file.c +1 -1
index 130fc6fbcc03..a21aa1aa2106 100644
@@ -303,7 +303,7 @@ int sysfs_add_file_mode_ns(struct kernfs_node *parent,
 #endif
 
 	kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid,
-				  size, ops, (void *)attr, ns, key);
+				  size, ops, (void *)attr, ns, key, 0, 0);
 	if (IS_ERR(kn)) {
 		if (PTR_ERR(kn) == -EEXIST)
 			sysfs_warn_dup(parent, attr->name);
include/linux/kernfs.h +18 -7
index 936b61bd504e..3764a870a279 100644
@@ -340,10 +340,11 @@ struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
 				       unsigned int flags, void *priv);
 void kernfs_destroy_root(struct kernfs_root *root);
 
-struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
+struct kernfs_node *__kernfs_create_dir(struct kernfs_node *parent,
 					 const char *name, umode_t mode,
 					 kuid_t uid, kgid_t gid,
-					 void *priv, const void *ns);
+					 void *priv, const void *ns,
+					 u32 gen, int ino);
 struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
 					    const char *name);
 struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
@@ -352,7 +353,8 @@ struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
 					 loff_t size,
 					 const struct kernfs_ops *ops,
 					 void *priv, const void *ns,
-					 struct lock_class_key *key);
+					 struct lock_class_key *key,
+					 u32 gen, int ino);
 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
 				       const char *name,
 				       struct kernfs_node *target);
@@ -438,16 +440,17 @@ kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
 static inline void kernfs_destroy_root(struct kernfs_root *root) { }
 
 static inline struct kernfs_node *
-kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
+__kernfs_create_dir(struct kernfs_node *parent, const char *name,
 		     umode_t mode, kuid_t uid, kgid_t gid,
-		     void *priv, const void *ns)
+		     void *priv, const void *ns, u32 gen, int ino)
 { return ERR_PTR(-ENOSYS); }
 
 static inline struct kernfs_node *
 __kernfs_create_file(struct kernfs_node *parent, const char *name,
 		     umode_t mode, kuid_t uid, kgid_t gid,
 		     loff_t size, const struct kernfs_ops *ops,
-		     void *priv, const void *ns, struct lock_class_key *key)
+		     void *priv, const void *ns, struct lock_class_key *key,
+		     u32 gen, int ino)
 { return ERR_PTR(-ENOSYS); }
 
 static inline struct kernfs_node *
@@ -528,6 +531,14 @@ kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
 	return kernfs_walk_and_get_ns(kn, path, NULL);
 }
 
+static inline struct kernfs_node *
+kernfs_create_dir_ns(struct kernfs_node *parent, const char *name, umode_t mode,
+		     kuid_t uid, kgid_t gid, void *priv, const void *ns)
+{
+	return __kernfs_create_dir(parent, name, mode,
+				   uid, gid, priv, ns, 0, 0);
+}
+
 static inline struct kernfs_node *
 kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
 		  void *priv)
@@ -549,7 +560,7 @@ kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
 	key = (struct lock_class_key *)&ops->lockdep_key;
 #endif
 	return __kernfs_create_file(parent, name, mode, uid, gid,
-				    size, ops, priv, ns, key);
+				    size, ops, priv, ns, key, 0, 0);
 }
 
 static inline struct kernfs_node *
kernel/cgroup/cgroup.c +10 -2
index 44c67d26c1fe..13d0d181a9f8 100644
@@ -3916,16 +3916,22 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
 	char name[CGROUP_FILE_NAME_MAX];
 	struct kernfs_node *kn;
 	struct lock_class_key *key = NULL;
+	struct cgroup_root *root = cgrp->root;
+	int ino, gen;
 	int ret;
 
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 	key = &cft->lockdep_key;
 #endif
+
+	ino = cgroup_idr_alloc(&root->cgroup_idr, NULL, false, GFP_KERNEL);
+	gen = root->cgroup_idr.generation;
+
 	kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
 				  cgroup_file_mode(cft),
 				  GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
 				  0, cft->kf_ops, cft,
-				  NULL, key);
+				  NULL, key, gen, ino);
 	if (IS_ERR(kn))
 		return PTR_ERR(kn);
 
@@ -5426,7 +5432,9 @@ int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
 	}
 
 	/* create the directory */
-	kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
+	kn = __kernfs_create_dir(parent->kn, name, mode,
+				 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
+				 cgrp, NULL, cgrp->gen, cgrp->id);
 	if (IS_ERR(kn)) {
 		ret = PTR_ERR(kn);
 		goto out_destroy;
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
maintainer
#13 lore raw

Re: [PATCH 2/2] kernfs: Allow creation with external gen + ino numbers

fs/kernfs/dir.c static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, · line 621

Quoted 10 lines · Namhyung Kim
arch/x86/kernel/cpu/resctrl/rdtgroup.c +0 -0
fs/kernfs/dir.c +1 -0
index 6ebae6bbe6a5..f2e54532c110 100644
@@ -618,10 +618,10 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 					     struct kernfs_node *parent,
 					     const char *name, umode_t mode,
 					     kuid_t uid, kgid_t gid,
+					     u32 gen, int ino,

Shouldn't this be ino_t so that we can use 64bit uniq id as ino directly where possible? Also, it might make more sense if ino comes before gen.

Quoted 16 lines
fs/kernfs/dir.c +7 -5
@@ -635,11 +635,24 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 
 	idr_preload(GFP_KERNEL);
 	spin_lock(&kernfs_idr_lock);
-	cursor = idr_get_cursor(&root->ino_idr);
-	ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
-	if (ret >= 0 && ret < cursor)
-		root->next_generation++;
-	gen = root->next_generation;
+
+	if (ino == 0) {
+		cursor = idr_get_cursor(&root->ino_idr);
+		ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
+		if (ret >= 0 && ret < cursor)
+			root->next_generation++;
+		gen = root->next_generation;

fs/kernfs/dir.c static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, · line 654

Quoted 10 lines
fs/kernfs/dir.c +10 -0
+	} else {
+		ret = idr_alloc(&root->ino_idr, kn, ino, ino + 1, GFP_ATOMIC);
+		if (ret != ino) {
+			WARN_ONCE(1, "kernfs ino was used: %d", ino);
+			ret = -EINVAL;
+		} else {
+			WARN_ON(root->next_generation > gen);
+			root->next_generation = gen;
+		}
+	}

Oh, I see, so the code is still depending on idr and thus the use of 32bit ino. Hmm....

Quoted 16 lines
fs/kernfs/dir.c +0 -0
 static inline struct kernfs_node *
kernel/cgroup/cgroup.c +2 -0
index 44c67d26c1fe..13d0d181a9f8 100644
@@ -3916,16 +3916,22 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
 	char name[CGROUP_FILE_NAME_MAX];
 	struct kernfs_node *kn;
 	struct lock_class_key *key = NULL;
+	struct cgroup_root *root = cgrp->root;
+	int ino, gen;
 	int ret;
 
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 	key = &cft->lockdep_key;
 #endif

kernel/cgroup/cgroup.c static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp, · line 3934

Quoted 10 lines
kernel/cgroup/cgroup.c +5 -1
+
+	ino = cgroup_idr_alloc(&root->cgroup_idr, NULL, false, GFP_KERNEL);
+	gen = root->cgroup_idr.generation;
+
 	kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
 				  cgroup_file_mode(cft),
 				  GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
 				  0, cft->kf_ops, cft,
-				  NULL, key);
+				  NULL, key, gen, ino);

Can we move this to a separate patch so that this patch can be an identity conversion?

Thanks.

Signature · 1 line
tejun
#14 lore raw

Re: [PATCH 2/2] kernfs: Allow creation with external gen + ino numbers

Quoted 16 lines · Tejun Heo

fs/kernfs/dir.c static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, · line 621

Quoted 10 lines · Namhyung Kim
arch/x86/kernel/cpu/resctrl/rdtgroup.c +0 -0
fs/kernfs/dir.c +1 -0
index 6ebae6bbe6a5..f2e54532c110 100644
@@ -618,10 +618,10 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
                                           struct kernfs_node *parent,
                                           const char *name, umode_t mode,
                                           kuid_t uid, kgid_t gid,
+                                          u32 gen, int ino,

Shouldn't this be ino_t so that we can use 64bit uniq id as ino directly where possible? Also, it might make more sense if ino comes before gen.

Will change the order.

Quoted 22 lines
Quoted 22 lines
fs/kernfs/dir.c +12 -5
@@ -635,11 +635,24 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 
      idr_preload(GFP_KERNEL);
      spin_lock(&kernfs_idr_lock);
-     cursor = idr_get_cursor(&root->ino_idr);
-     ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
-     if (ret >= 0 && ret < cursor)
-             root->next_generation++;
-     gen = root->next_generation;
+
+     if (ino == 0) {
+             cursor = idr_get_cursor(&root->ino_idr);
+             ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
+             if (ret >= 0 && ret < cursor)
+                     root->next_generation++;
+             gen = root->next_generation;
+     } else {
+             ret = idr_alloc(&root->ino_idr, kn, ino, ino + 1, GFP_ATOMIC);
+             if (ret != ino) {
+                     WARN_ONCE(1, "kernfs ino was used: %d", ino);
+                     ret = -EINVAL;
Quoted 8 lines

fs/kernfs/dir.c static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, · line 654

Quoted 5 lines
fs/kernfs/dir.c +5 -0
+             } else {
+                     WARN_ON(root->next_generation > gen);
+                     root->next_generation = gen;
+             }
+     }

Oh, I see, so the code is still depending on idr and thus the use of 32bit ino. Hmm....

Right.

Quoted 14 lines
Quoted 14 lines
fs/kernfs/dir.c +0 -0
 static inline struct kernfs_node *
kernel/cgroup/cgroup.c +2 -0
index 44c67d26c1fe..13d0d181a9f8 100644
@@ -3916,16 +3916,22 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
      char name[CGROUP_FILE_NAME_MAX];
      struct kernfs_node *kn;
      struct lock_class_key *key = NULL;
+     struct cgroup_root *root = cgrp->root;
+     int ino, gen;
      int ret;
 
Quoted 16 lines

kernel/cgroup/cgroup.c static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp, · line 3934

Quoted 13 lines
kernel/cgroup/cgroup.c +5 -1
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
      key = &cft->lockdep_key;
 #endif
+
+     ino = cgroup_idr_alloc(&root->cgroup_idr, NULL, false, GFP_KERNEL);
+     gen = root->cgroup_idr.generation;
+
      kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
                                cgroup_file_mode(cft),
                                GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
                                0, cft->kf_ops, cft,
-                               NULL, key);
+                               NULL, key, gen, ino);

Can we move this to a separate patch so that this patch can be an identity conversion?

Will do.

Thanks
Namhyung