Skip to contentSkip to conversation

mm: slub: Convert sys slab alloc_calls, free_calls to bin attribute

PATCH Faiyaz Mohammed 3 participants 4 messages Feb 17, 2021 – Mar 19, 2021 3 messages not in the archive Memory management

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

+linux-mm, linux-kernel.

Quoted 197 lines · Faiyaz Mohammed

Reading the sys slab alloc_calls, free_calls returns the available object owners, but the size of this file is limited to PAGE_SIZE because of the limitation of sysfs attributes, it is returning the partial owner info, which is not sufficient to debug/account the slab memory and alloc_calls output is not matching with /proc/slabinfo.

To remove the PAGE_SIZE limitation converted the sys slab alloc_calls, free_calls to bin attribute.

---
 mm/slub.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 63 insertions(+), 21 deletions(-)
mm/slub.c +63 -21
index b22a4b1..71cfe3b 100644
@@ -37,6 +37,9 @@
 
 #include <trace/events/kmem.h>
 
+#define TRACE_ENTRY_MAX 80
+#define TRACKS_PER_PAGE  ((PAGE_SIZE - KSYM_SYMBOL_LEN - 100) / TRACE_ENTRY_MAX)
+
 #include "internal.h"
 
 /*
@@ -4748,6 +4751,7 @@ static int list_locations(struct kmem_cache *s, char *buf,
 	struct loc_track t = { 0, 0, NULL };
 	int node;
 	struct kmem_cache_node *n;
+	unsigned int previous_read_count = 0;
 
 	if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
 			     GFP_KERNEL)) {
@@ -4756,6 +4760,11 @@ static int list_locations(struct kmem_cache *s, char *buf,
 	/* Push back cpu slabs */
 	flush_all(s);
 
+	if (offset != 0)
+		previous_read_count = (offset / TRACE_ENTRY_MAX);
+
+	memset(buf, 0, PAGE_SIZE);
+
 	for_each_kmem_cache_node(s, node, n) {
 		unsigned long flags;
 		struct page *page;
@@ -4771,48 +4780,62 @@ static int list_locations(struct kmem_cache *s, char *buf,
 		spin_unlock_irqrestore(&n->list_lock, flags);
 	}
 
-	for (i = 0; i < t.count; i++) {
+	for (i = previous_read_count; i < t.count; i++) {
 		struct location *l = &t.loc[i];
+		unsigned int cur_len = 0;
 
-		len += sysfs_emit_at(buf, len, "%7ld ", l->count);
+		cur_len += sysfs_emit_at(buf, cur_len + len, "%7ld ", l->count);
 
 		if (l->addr)
-			len += sysfs_emit_at(buf, len, "%pS", (void *)l->addr);
+			cur_len += sysfs_emit_at(buf, cur_len + len, "%pS", (void *)l->addr);
 		else
-			len += sysfs_emit_at(buf, len, "<not-available>");
+			cur_len += sysfs_emit_at(buf, cur_len + len, "<not-available>");
 
 		if (l->sum_time != l->min_time)
-			len += sysfs_emit_at(buf, len, " age=%ld/%ld/%ld",
+			cur_len += sysfs_emit_at(buf, cur_len + len, " age=%ld/%ld/%ld",
 					     l->min_time,
 					     (long)div_u64(l->sum_time,
 							   l->count),
 					     l->max_time);
 		else
-			len += sysfs_emit_at(buf, len, " age=%ld", l->min_time);
+			cur_len += sysfs_emit_at(buf, cur_len + len, " age=%ld", l->min_time);
 
 		if (l->min_pid != l->max_pid)
-			len += sysfs_emit_at(buf, len, " pid=%ld-%ld",
+			cur_len += sysfs_emit_at(buf, cur_len + len, " pid=%ld-%ld",
 					     l->min_pid, l->max_pid);
 		else
-			len += sysfs_emit_at(buf, len, " pid=%ld",
+			cur_len += sysfs_emit_at(buf, cur_len + len, " pid=%ld",
 					     l->min_pid);
 
 		if (num_online_cpus() > 1 &&
 		    !cpumask_empty(to_cpumask(l->cpus)))
-			len += sysfs_emit_at(buf, len, " cpus=%*pbl",
+			cur_len += sysfs_emit_at(buf, cur_len + len, " cpus=%*pbl",
 					     cpumask_pr_args(to_cpumask(l->cpus)));
 
 		if (nr_online_nodes > 1 && !nodes_empty(l->nodes))
-			len += sysfs_emit_at(buf, len, " nodes=%*pbl",
+			cur_len += sysfs_emit_at(buf, cur_len + len, " nodes=%*pbl",
 					     nodemask_pr_args(&l->nodes));
 
+		if (cur_len >= TRACE_ENTRY_MAX)
+			cur_len -= (cur_len % TRACE_ENTRY_MAX) - 1;
+		else if (cur_len < TRACE_ENTRY_MAX)
+			cur_len += TRACE_ENTRY_MAX - (cur_len % TRACE_ENTRY_MAX) - 1;
+
+		len += cur_len;
+
 		len += sysfs_emit_at(buf, len, "\n");
+
+		if (i >= (previous_read_count + TRACKS_PER_PAGE))
+			break;
+
 	}
 
-	free_loc_track(&t);
-	if (!t.count)
-		len += sysfs_emit_at(buf, len, "No data\n");
+	if (((previous_read_count > t.count) | (i >= t.count)) && (offset != 0))
+		len = 0;
+	else if (!t.count)
+		len += sprintf(buf, "No data\n");
 
+	free_loc_track(&t);
 	return len;
 }
 #endif	/* CONFIG_SLUB_DEBUG */
@@ -5280,21 +5303,33 @@ static ssize_t validate_store(struct kmem_cache *s,
 }
 SLAB_ATTR(validate);
 
-static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
+static ssize_t alloc_calls_read(struct file *filp, struct kobject *kobj,
+				struct bin_attribute *bin_attr, char *buf,
+					loff_t offset, size_t count)
 {
+	struct kmem_cache *s;
+
+	s = to_slab(kobj);
 	if (!(s->flags & SLAB_STORE_USER))
 		return -ENOSYS;
-	return list_locations(s, buf, TRACK_ALLOC);
+
+	return list_locations(s, buf, offset, TRACK_ALLOC);
 }
-SLAB_ATTR_RO(alloc_calls);
+BIN_ATTR_RO(alloc_calls, 0);
 
-static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
+static ssize_t free_calls_read(struct file *filp, struct kobject *kobj,
+				struct bin_attribute *bin_attr, char *buf,
+					loff_t offset, size_t count)
 {
+	struct kmem_cache *s;
+
+	s = to_slab(kobj);
 	if (!(s->flags & SLAB_STORE_USER))
 		return -ENOSYS;
-	return list_locations(s, buf, TRACK_FREE);
+
+	return list_locations(s, buf, offset, TRACK_FREE);
 }
-SLAB_ATTR_RO(free_calls);
+BIN_ATTR_RO(free_calls, 0);
 #endif /* CONFIG_SLUB_DEBUG */
 
 #ifdef CONFIG_FAILSLAB
@@ -5430,6 +5465,14 @@ STAT_ATTR(CPU_PARTIAL_NODE, cpu_partial_node);
 STAT_ATTR(CPU_PARTIAL_DRAIN, cpu_partial_drain);
 #endif	/* CONFIG_SLUB_STATS */
 
+
+static struct bin_attribute *slab_bin_attrs[] = {
+#ifdef CONFIG_SLUB_DEBUG
+	&bin_attr_alloc_calls,
+	&bin_attr_free_calls,
+#endif
+};
+
 static struct attribute *slab_attrs[] = {
 	&slab_size_attr.attr,
 	&object_size_attr.attr,
@@ -5458,8 +5501,6 @@ static struct attribute *slab_attrs[] = {
 	&poison_attr.attr,
 	&store_user_attr.attr,
 	&validate_attr.attr,
-	&alloc_calls_attr.attr,
-	&free_calls_attr.attr,
 #endif
 #ifdef CONFIG_ZONE_DMA
 	&cache_dma_attr.attr,
@@ -5505,6 +5546,7 @@ static struct attribute *slab_attrs[] = {
 
 static const struct attribute_group slab_attr_group = {
 	.attrs = slab_attrs,
+	.bin_attrs = slab_bin_attrs,
 };
 
 static ssize_t slab_attr_show(struct kobject *kobj,

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Hi,

Sorry for late response!

Quoted 18 lines · Greg Kroah-Hartman
Quoted 10 lines · Vlastimil Babka
Quoted 9 lines · Faiyaz Mohammed

Reading the sys slab alloc_calls, free_calls returns the available object owners, but the size of this file is limited to PAGE_SIZE because of the limitation of sysfs attributes, it is returning the partial owner info, which is not sufficient to debug/account the slab memory and alloc_calls output is not matching with /proc/slabinfo.

To remove the PAGE_SIZE limitation converted the sys slab alloc_calls, free_calls to bin attribut

Quoted 6 lines
Quoted 1 line

After discussing this with Greg and PeterZ, sysfs should be one value per file, and is just not proper API for this kind of info. We should reimplement at least these clearly debugging "attributes" in debugfs, where they belong, instead of trying to hack around the limitation in sysfs.

Quoted 7 lines

sysfs is _REQUIRED_ to be "one value per file", any kernel code that abuses this needs to be fixed up.

Why are these slab files in sysfs at all anyway? They all feel like debugging stuff to me, why not move everything to debugfs? Would make for a lot less code overall.

Yes, we can move the /sys/kernel/slab/kmemcache/alloc_calls and /sys/kernel/slab/kmemcache/free_calls implementation to debugfs but is it okay to move only alloc_calls and free_calls? or we have to move whole sysfs interface to debugfs?.

If we are moving only alloc_calls/free_calls interface to debugfs then I think we can add all slab objects data into single file.

For example: /sys/kernel/debugfs/slab/alloc_calls, which will print all slab objects data.

Example Output: alloc_list: call_site=__request_region+0xb4/0x2f0 count=228 object_size=128 slab_size=640 slab_name=kmalloc-128

or

We can have just like current sysfs interface, have separate alloc_calls/free_calls traces per kmem cache.

Which one would be better?.

Quoted 1 line

Thanks and regards,
Mohammed Faiyaz

#5 lore raw
Quoted 26 lines · Faiyaz Mohammed

Hi,

Sorry for late response!

Quoted 21 lines · Greg Kroah-Hartman
Quoted 10 lines · Vlastimil Babka
Quoted 9 lines · Faiyaz Mohammed

Reading the sys slab alloc_calls, free_calls returns the available object owners, but the size of this file is limited to PAGE_SIZE because of the limitation of sysfs attributes, it is returning the partial owner info, which is not sufficient to debug/account the slab memory and alloc_calls output is not matching with /proc/slabinfo.

To remove the PAGE_SIZE limitation converted the sys slab alloc_calls, free_calls to bin attribut

Quoted 6 lines
Quoted 1 line

After discussing this with Greg and PeterZ, sysfs should be one value per file, and is just not proper API for this kind of info. We should reimplement at least these clearly debugging "attributes" in debugfs, where they belong, instead of trying to hack around the limitation in sysfs.

sysfs is _REQUIRED_ to be "one value per file", any kernel code that abuses this needs to be fixed up.

Quoted 8 lines
Quoted 4 lines

Why are these slab files in sysfs at all anyway? They all feel like debugging stuff to me, why not move everything to debugfs? Would make for a lot less code overall.

Yes, we can move the /sys/kernel/slab/kmemcache/alloc_calls and /sys/kernel/slab/kmemcache/free_calls implementation to debugfs but is it okay to move only alloc_calls and free_calls? or we have to move whole sysfs interface to debugfs?.

sysfs files should only have "one value" in them. Anything that violates that rule, should be moved to debugfs.

thanks,

greg k-h

#6 lore raw
In reply to Faiyaz Mohammed
Quoted 26 lines · Faiyaz Mohammed

Hi,

Sorry for late response!

Quoted 21 lines · Greg Kroah-Hartman
Quoted 10 lines · Vlastimil Babka
Quoted 9 lines · Faiyaz Mohammed

Reading the sys slab alloc_calls, free_calls returns the available object owners, but the size of this file is limited to PAGE_SIZE because of the limitation of sysfs attributes, it is returning the partial owner info, which is not sufficient to debug/account the slab memory and alloc_calls output is not matching with /proc/slabinfo.

To remove the PAGE_SIZE limitation converted the sys slab alloc_calls, free_calls to bin attribut

Quoted 6 lines
Quoted 1 line

After discussing this with Greg and PeterZ, sysfs should be one value per file, and is just not proper API for this kind of info. We should reimplement at least these clearly debugging "attributes" in debugfs, where they belong, instead of trying to hack around the limitation in sysfs.

sysfs is _REQUIRED_ to be "one value per file", any kernel code that abuses this needs to be fixed up.

Quoted 8 lines
Quoted 4 lines

Why are these slab files in sysfs at all anyway? They all feel like debugging stuff to me, why not move everything to debugfs? Would make for a lot less code overall.

Yes, we can move the /sys/kernel/slab/kmemcache/alloc_calls and /sys/kernel/slab/kmemcache/free_calls implementation to debugfs but is it okay to move only alloc_calls and free_calls? or we have to move whole sysfs interface to debugfs?.

I don't think we need to move everything, just files where it makes sense.

Quoted 8 lines

If we are moving only alloc_calls/free_calls interface to debugfs then I think we can add all slab objects data into single file.

For example: /sys/kernel/debugfs/slab/alloc_calls, which will print all slab objects data.

Example Output: alloc_list: call_site=__request_region+0xb4/0x2f0 count=228 object_size=128 slab_size=640 slab_name=kmalloc-128

I wouldn't do this, as processing all caches will have a large overhead and then somebody interested in single cache would throw most of the info away.

Quoted 12 lines

or

We can have just like current sysfs interface, have separate alloc_calls/free_calls traces per kmem cache.

Which one would be better?.

Quoted 1 line

Thanks and regards,
Mohammed Faiyaz