Skip to contentSkip to conversation

mm/damon: add DAMON_OBJ macro

PATCH In review Xin Hao 2 participants 4 messages Sep 21, 2022 – Sep 30, 2022

Subsystems (2)
lore
Share & tools
lore

Xin Hao

author
#0 lore raw

PATCH 1/1 mm/damon: add DAMON_OBJ macro

In damon/sysfs.c file, we use 'container_of' macro to get damon_sysfs_xxx struct instances, but i think it has a little inconvenience, because we have to pass three arguments to 'container_of', and the codes also look a bit long, so there i add a 'DAMON_OBJ' macro, you just need to pass one arguments, then you can get the right damon_sysfs_xxx struct instance.

---
 include/linux/damon.h |   7 ++
 mm/damon/sysfs.c      | 230 +++++++++++++++++-------------------------
 2 files changed, 102 insertions(+), 135 deletions(-)
include/linux/damon.h +7 -0
index e7808a84675f..a3b577677caa 100644
@@ -24,6 +24,13 @@ static inline unsigned long damon_rand(unsigned long l, unsigned long r)
 	return l + prandom_u32_max(r - l);
 }
 
+/*
+ * Get damon_sysfs_xxx relative struct instance.
+ */
+#define DAMON_OBJ(_type) ({						\
+	const typeof(((struct _type *)0)->kobj)*__mptr = (kobj);	\
+	(struct _type *)((char *)__mptr - offsetof(struct _type, kobj)); })
+
 /**
  * struct damon_addr_range - Represents an address region of [@start, @end).
  * @start:	Start address of the region (inclusive).
mm/damon/sysfs.c +95 -135

704 lines not on this page · raw message

Signature · 1 line
2.31.0
maintainer
#1 lore raw

Hi Xin,

Quoted 6 lines · On Wed, 21 Sep 2022 11:49:42 +0800 Xin Hao <[email protected]> wrote:

In damon/sysfs.c file, we use 'container_of' macro to get damon_sysfs_xxx struct instances, but i think it has a little inconvenience, because we have to pass three arguments to 'container_of', and the codes also look a bit long, so there i add a 'DAMON_OBJ' macro, you just need to pass one arguments, then you can get the right damon_sysfs_xxx struct instance.

Thank you always for your helps and efforts, but I have some comments below.

Quoted 12 lines
---
 include/linux/damon.h |   7 ++
 mm/damon/sysfs.c      | 230 +++++++++++++++++-------------------------
 2 files changed, 102 insertions(+), 135 deletions(-)
include/linux/damon.h +0 -0
index e7808a84675f..a3b577677caa 100644

include/linux/damon.h static inline unsigned long damon_rand(unsigned long l, unsigned long r) · line 33

Quoted 10 lines
include/linux/damon.h +7 -0
@@ -24,6 +24,13 @@ static inline unsigned long damon_rand(unsigned long l, unsigned long r)
 	return l + prandom_u32_max(r - l);
 }
 
+/*
+ * Get damon_sysfs_xxx relative struct instance.
+ */
+#define DAMON_OBJ(_type) ({						\
+	const typeof(((struct _type *)0)->kobj)*__mptr = (kobj);	\
+	(struct _type *)((char *)__mptr - offsetof(struct _type, kobj)); })
+

So, this macro assumes two implicit rules.
1. The caller would have a relevant 'struct kobject *' variable called 'kobj', and
2. The '_type' would have the field 'kobj'.

I think the implicit rules could make some people confused, so would be better to be well documented. Even though those are well documented, I think it cannot intuitively read by everyone. Making the name better self-explaining might help, but then the length of the code would be not so different.

So IMHO, this change makes the code a little bit shorter but unclear to understand what it does. And at least to my humble eyes, use of 'container_of()' makes the code a little bit more verbose, but clear to understand. I have no idea how we can make this code shorter while keeping it still easily understandable, and I think the level of verboseness is acceptable for the readability. So Nack at the moment, sorry.

Thanks,
SJ

[...]

author
#2 lore raw

Hi SJ,

Quoted 43 lines · SeongJae Park

Hi Xin,

Quoted 6 lines · On Wed, 21 Sep 2022 11:49:42 +0800 Xin Hao <[email protected]> wrote:

In damon/sysfs.c file, we use 'container_of' macro to get damon_sysfs_xxx struct instances, but i think it has a little inconvenience, because we have to pass three arguments to 'container_of', and the codes also look a bit long, so there i add a 'DAMON_OBJ' macro, you just need to pass one arguments, then you can get the right damon_sysfs_xxx struct instance.

Thank you always for your helps and efforts, but I have some comments below.

Quoted 11 lines
---
  include/linux/damon.h |   7 ++
  mm/damon/sysfs.c      | 230 +++++++++++++++++-------------------------
  2 files changed, 102 insertions(+), 135 deletions(-)
include/linux/damon.h +0 -0
index e7808a84675f..a3b577677caa 100644

include/linux/damon.h static inline unsigned long damon_rand(unsigned long l, unsigned long r) · line 33

Quoted 10 lines
include/linux/damon.h +7 -0
@@ -24,6 +24,13 @@ static inline unsigned long damon_rand(unsigned long l, unsigned long r)
  	return l + prandom_u32_max(r - l);
  }
 
+/*
+ * Get damon_sysfs_xxx relative struct instance.
+ */
+#define DAMON_OBJ(_type) ({						\
+	const typeof(((struct _type *)0)->kobj)*__mptr = (kobj);	\
+	(struct _type *)((char *)__mptr - offsetof(struct _type, kobj)); })
+

So, this macro assumes two implicit rules.
1. The caller would have a relevant 'struct kobject *' variable called 'kobj', and
2. The '_type' would have the field 'kobj'.

I think the implicit rules could make some people confused, so would be better to be well documented. Even though those are well documented, I think it cannot intuitively read by everyone. Making the name better self-explaining might help, but then the length of the code would be not so different.

Quoted 6 lines

So IMHO, this change makes the code a little bit shorter but unclear to understand what it does. And at least to my humble eyes, use of 'container_of()' makes the code a little bit more verbose, but clear to understand. I have no idea how we can make this code shorter while keeping it still easily understandable, and I think the level of verboseness is acceptable for the readability. So Nack at the moment, sorry.

I really feel the need to do that, how about keep it in the sysfs.c file, and change it like this:

#define DAMON_OBJ(_struct) container_of(kobj, struct _struct, kobj)

it will feel easy to understand and made sense.

Quoted 6 lines

Thanks,
SJ

[...]

maintainer
#3 lore raw

Hi Xin,

Quoted 53 lines · On Fri, 30 Sep 2022 11:46:26 +0800 haoxin <[email protected]> wrote:

Hi SJ,

Quoted 43 lines · SeongJae Park

Hi Xin,

Quoted 6 lines · On Wed, 21 Sep 2022 11:49:42 +0800 Xin Hao <[email protected]> wrote:

In damon/sysfs.c file, we use 'container_of' macro to get damon_sysfs_xxx struct instances, but i think it has a little inconvenience, because we have to pass three arguments to 'container_of', and the codes also look a bit long, so there i add a 'DAMON_OBJ' macro, you just need to pass one arguments, then you can get the right damon_sysfs_xxx struct instance.

Thank you always for your helps and efforts, but I have some comments below.

Quoted 11 lines
---
  include/linux/damon.h |   7 ++
  mm/damon/sysfs.c      | 230 +++++++++++++++++-------------------------
  2 files changed, 102 insertions(+), 135 deletions(-)
include/linux/damon.h +0 -0
index e7808a84675f..a3b577677caa 100644

include/linux/damon.h static inline unsigned long damon_rand(unsigned long l, unsigned long r) · line 33

Quoted 10 lines
include/linux/damon.h +7 -0
@@ -24,6 +24,13 @@ static inline unsigned long damon_rand(unsigned long l, unsigned long r)
  	return l + prandom_u32_max(r - l);
  }
 
+/*
+ * Get damon_sysfs_xxx relative struct instance.
+ */
+#define DAMON_OBJ(_type) ({						\
+	const typeof(((struct _type *)0)->kobj)*__mptr = (kobj);	\
+	(struct _type *)((char *)__mptr - offsetof(struct _type, kobj)); })
+

So, this macro assumes two implicit rules.
1. The caller would have a relevant 'struct kobject *' variable called 'kobj', and
2. The '_type' would have the field 'kobj'.

I think the implicit rules could make some people confused, so would be better to be well documented. Even though those are well documented, I think it cannot intuitively read by everyone. Making the name better self-explaining might help, but then the length of the code would be not so different.

Quoted 6 lines

So IMHO, this change makes the code a little bit shorter but unclear to understand what it does. And at least to my humble eyes, use of 'container_of()' makes the code a little bit more verbose, but clear to understand. I have no idea how we can make this code shorter while keeping it still easily understandable, and I think the level of verboseness is acceptable for the readability. So Nack at the moment, sorry.

Quoted 6 lines

I really feel the need to do that, how about keep it in the sysfs.c file, and change it like this:

#define DAMON_OBJ(_struct) container_of(kobj, struct _struct, kobj)

it will feel easy to understand and made sense.

I don't think this is enough, at least for my humble eyes. My questions for better self-explaining name and removing the implicit rules are not answered. After all, I don't think the usage of container_of() is a real problem, though we might always have rooms for improvement.

Thanks,
SJ

[...]