Skip to content

Commit 64dd684

Browse files
Faiyaz Mohammedtorvalds
authored andcommitted
mm: slub: move sysfs slab alloc/free interfaces to debugfs
alloc_calls and free_calls implementation in sysfs have two issues, one is PAGE_SIZE limitation of sysfs and other is it does not adhere to "one value per file" rule. To overcome this issues, move the alloc_calls and free_calls implementation to debugfs. Debugfs cache will be created if SLAB_STORE_USER flag is set. Rename the alloc_calls/free_calls to alloc_traces/free_traces, to be inline with what it does. [faiyazm@codeaurora.org: fix the leak of alloc/free traces debugfs interface] Link: https://lkml.kernel.org/r/1624248060-30286-1-git-send-email-faiyazm@codeaurora.org Link: https://lkml.kernel.org/r/1623438200-19361-1-git-send-email-faiyazm@codeaurora.org Signed-off-by: Faiyaz Mohammed <faiyazm@codeaurora.org> Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Christoph Lameter <cl@linux.com> Cc: Pekka Enberg <penberg@kernel.org> Cc: David Rientjes <rientjes@google.com> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7927029 commit 64dd684

3 files changed

Lines changed: 189 additions & 93 deletions

File tree

‎mm/slab.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,12 @@ static inline bool slab_want_init_on_free(struct kmem_cache *c)
631631
return false;
632632
}
633633

634+
#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
635+
void debugfs_slab_release(struct kmem_cache *);
636+
#else
637+
static inline void debugfs_slab_release(struct kmem_cache *s) { }
638+
#endif
639+
634640
#ifdef CONFIG_PRINTK
635641
#define KS_ADDRS_COUNT 16
636642
struct kmem_obj_info {

‎mm/slab_common.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
448448
rcu_barrier();
449449

450450
list_for_each_entry_safe(s, s2, &to_destroy, list) {
451+
debugfs_slab_release(s);
451452
kfence_shutdown_cache(s);
452453
#ifdef SLAB_SUPPORTS_SYSFS
453454
sysfs_slab_release(s);
@@ -475,6 +476,7 @@ static int shutdown_cache(struct kmem_cache *s)
475476
schedule_work(&slab_caches_to_rcu_destroy_work);
476477
} else {
477478
kfence_shutdown_cache(s);
479+
debugfs_slab_release(s);
478480
#ifdef SLAB_SUPPORTS_SYSFS
479481
sysfs_slab_unlink(s);
480482
sysfs_slab_release(s);

‎mm/slub.c‎

Lines changed: 181 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <linux/random.h>
3939
#include <kunit/test.h>
4040

41+
#include <linux/debugfs.h>
4142
#include <trace/events/kmem.h>
4243

4344
#include "internal.h"
@@ -238,6 +239,12 @@ static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
238239
{ return 0; }
239240
#endif
240241

242+
#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
243+
static void debugfs_slab_add(struct kmem_cache *);
244+
#else
245+
static inline void debugfs_slab_add(struct kmem_cache *s) { }
246+
#endif
247+
241248
static inline void stat(const struct kmem_cache *s, enum stat_item si)
242249
{
243250
#ifdef CONFIG_SLUB_STATS
@@ -4593,6 +4600,9 @@ int __kmem_cache_create(struct kmem_cache *s, slab_flags_t flags)
45934600
if (err)
45944601
__kmem_cache_release(s);
45954602

4603+
if (s->flags & SLAB_STORE_USER)
4604+
debugfs_slab_add(s);
4605+
45964606
return err;
45974607
}
45984608

@@ -4739,6 +4749,7 @@ long validate_slab_cache(struct kmem_cache *s)
47394749
}
47404750
EXPORT_SYMBOL(validate_slab_cache);
47414751

4752+
#ifdef CONFIG_DEBUG_FS
47424753
/*
47434754
* Generate lists of code addresses where slabcache objects are allocated
47444755
* and freed.
@@ -4762,6 +4773,8 @@ struct loc_track {
47624773
struct location *loc;
47634774
};
47644775

4776+
static struct dentry *slab_debugfs_root;
4777+
47654778
static void free_loc_track(struct loc_track *t)
47664779
{
47674780
if (t->max)
@@ -4878,82 +4891,7 @@ static void process_slab(struct loc_track *t, struct kmem_cache *s,
48784891
add_location(t, s, get_track(s, p, alloc));
48794892
put_map(map);
48804893
}
4881-
4882-
static int list_locations(struct kmem_cache *s, char *buf,
4883-
enum track_item alloc)
4884-
{
4885-
int len = 0;
4886-
unsigned long i;
4887-
struct loc_track t = { 0, 0, NULL };
4888-
int node;
4889-
struct kmem_cache_node *n;
4890-
4891-
if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
4892-
GFP_KERNEL)) {
4893-
return sysfs_emit(buf, "Out of memory\n");
4894-
}
4895-
/* Push back cpu slabs */
4896-
flush_all(s);
4897-
4898-
for_each_kmem_cache_node(s, node, n) {
4899-
unsigned long flags;
4900-
struct page *page;
4901-
4902-
if (!atomic_long_read(&n->nr_slabs))
4903-
continue;
4904-
4905-
spin_lock_irqsave(&n->list_lock, flags);
4906-
list_for_each_entry(page, &n->partial, slab_list)
4907-
process_slab(&t, s, page, alloc);
4908-
list_for_each_entry(page, &n->full, slab_list)
4909-
process_slab(&t, s, page, alloc);
4910-
spin_unlock_irqrestore(&n->list_lock, flags);
4911-
}
4912-
4913-
for (i = 0; i < t.count; i++) {
4914-
struct location *l = &t.loc[i];
4915-
4916-
len += sysfs_emit_at(buf, len, "%7ld ", l->count);
4917-
4918-
if (l->addr)
4919-
len += sysfs_emit_at(buf, len, "%pS", (void *)l->addr);
4920-
else
4921-
len += sysfs_emit_at(buf, len, "<not-available>");
4922-
4923-
if (l->sum_time != l->min_time)
4924-
len += sysfs_emit_at(buf, len, " age=%ld/%ld/%ld",
4925-
l->min_time,
4926-
(long)div_u64(l->sum_time,
4927-
l->count),
4928-
l->max_time);
4929-
else
4930-
len += sysfs_emit_at(buf, len, " age=%ld", l->min_time);
4931-
4932-
if (l->min_pid != l->max_pid)
4933-
len += sysfs_emit_at(buf, len, " pid=%ld-%ld",
4934-
l->min_pid, l->max_pid);
4935-
else
4936-
len += sysfs_emit_at(buf, len, " pid=%ld",
4937-
l->min_pid);
4938-
4939-
if (num_online_cpus() > 1 &&
4940-
!cpumask_empty(to_cpumask(l->cpus)))
4941-
len += sysfs_emit_at(buf, len, " cpus=%*pbl",
4942-
cpumask_pr_args(to_cpumask(l->cpus)));
4943-
4944-
if (nr_online_nodes > 1 && !nodes_empty(l->nodes))
4945-
len += sysfs_emit_at(buf, len, " nodes=%*pbl",
4946-
nodemask_pr_args(&l->nodes));
4947-
4948-
len += sysfs_emit_at(buf, len, "\n");
4949-
}
4950-
4951-
free_loc_track(&t);
4952-
if (!t.count)
4953-
len += sysfs_emit_at(buf, len, "No data\n");
4954-
4955-
return len;
4956-
}
4894+
#endif /* CONFIG_DEBUG_FS */
49574895
#endif /* CONFIG_SLUB_DEBUG */
49584896

49594897
#ifdef CONFIG_SYSFS
@@ -5343,21 +5281,6 @@ static ssize_t validate_store(struct kmem_cache *s,
53435281
}
53445282
SLAB_ATTR(validate);
53455283

5346-
static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
5347-
{
5348-
if (!(s->flags & SLAB_STORE_USER))
5349-
return -ENOSYS;
5350-
return list_locations(s, buf, TRACK_ALLOC);
5351-
}
5352-
SLAB_ATTR_RO(alloc_calls);
5353-
5354-
static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
5355-
{
5356-
if (!(s->flags & SLAB_STORE_USER))
5357-
return -ENOSYS;
5358-
return list_locations(s, buf, TRACK_FREE);
5359-
}
5360-
SLAB_ATTR_RO(free_calls);
53615284
#endif /* CONFIG_SLUB_DEBUG */
53625285

53635286
#ifdef CONFIG_FAILSLAB
@@ -5521,8 +5444,6 @@ static struct attribute *slab_attrs[] = {
55215444
&poison_attr.attr,
55225445
&store_user_attr.attr,
55235446
&validate_attr.attr,
5524-
&alloc_calls_attr.attr,
5525-
&free_calls_attr.attr,
55265447
#endif
55275448
#ifdef CONFIG_ZONE_DMA
55285449
&cache_dma_attr.attr,
@@ -5810,6 +5731,173 @@ static int __init slab_sysfs_init(void)
58105731
__initcall(slab_sysfs_init);
58115732
#endif /* CONFIG_SYSFS */
58125733

5734+
#if defined(CONFIG_SLUB_DEBUG) && defined(CONFIG_DEBUG_FS)
5735+
static int slab_debugfs_show(struct seq_file *seq, void *v)
5736+
{
5737+
5738+
struct location *l;
5739+
unsigned int idx = *(unsigned int *)v;
5740+
struct loc_track *t = seq->private;
5741+
5742+
if (idx < t->count) {
5743+
l = &t->loc[idx];
5744+
5745+
seq_printf(seq, "%7ld ", l->count);
5746+
5747+
if (l->addr)
5748+
seq_printf(seq, "%pS", (void *)l->addr);
5749+
else
5750+
seq_puts(seq, "<not-available>");
5751+
5752+
if (l->sum_time != l->min_time) {
5753+
seq_printf(seq, " age=%ld/%llu/%ld",
5754+
l->min_time, div_u64(l->sum_time, l->count),
5755+
l->max_time);
5756+
} else
5757+
seq_printf(seq, " age=%ld", l->min_time);
5758+
5759+
if (l->min_pid != l->max_pid)
5760+
seq_printf(seq, " pid=%ld-%ld", l->min_pid, l->max_pid);
5761+
else
5762+
seq_printf(seq, " pid=%ld",
5763+
l->min_pid);
5764+
5765+
if (num_online_cpus() > 1 && !cpumask_empty(to_cpumask(l->cpus)))
5766+
seq_printf(seq, " cpus=%*pbl",
5767+
cpumask_pr_args(to_cpumask(l->cpus)));
5768+
5769+
if (nr_online_nodes > 1 && !nodes_empty(l->nodes))
5770+
seq_printf(seq, " nodes=%*pbl",
5771+
nodemask_pr_args(&l->nodes));
5772+
5773+
seq_puts(seq, "\n");
5774+
}
5775+
5776+
if (!idx && !t->count)
5777+
seq_puts(seq, "No data\n");
5778+
5779+
return 0;
5780+
}
5781+
5782+
static void slab_debugfs_stop(struct seq_file *seq, void *v)
5783+
{
5784+
}
5785+
5786+
static void *slab_debugfs_next(struct seq_file *seq, void *v, loff_t *ppos)
5787+
{
5788+
struct loc_track *t = seq->private;
5789+
5790+
v = ppos;
5791+
++*ppos;
5792+
if (*ppos <= t->count)
5793+
return v;
5794+
5795+
return NULL;
5796+
}
5797+
5798+
static void *slab_debugfs_start(struct seq_file *seq, loff_t *ppos)
5799+
{
5800+
return ppos;
5801+
}
5802+
5803+
static const struct seq_operations slab_debugfs_sops = {
5804+
.start = slab_debugfs_start,
5805+
.next = slab_debugfs_next,
5806+
.stop = slab_debugfs_stop,
5807+
.show = slab_debugfs_show,
5808+
};
5809+
5810+
static int slab_debug_trace_open(struct inode *inode, struct file *filep)
5811+
{
5812+
5813+
struct kmem_cache_node *n;
5814+
enum track_item alloc;
5815+
int node;
5816+
struct loc_track *t = __seq_open_private(filep, &slab_debugfs_sops,
5817+
sizeof(struct loc_track));
5818+
struct kmem_cache *s = file_inode(filep)->i_private;
5819+
5820+
if (strcmp(filep->f_path.dentry->d_name.name, "alloc_traces") == 0)
5821+
alloc = TRACK_ALLOC;
5822+
else
5823+
alloc = TRACK_FREE;
5824+
5825+
if (!alloc_loc_track(t, PAGE_SIZE / sizeof(struct location), GFP_KERNEL))
5826+
return -ENOMEM;
5827+
5828+
/* Push back cpu slabs */
5829+
flush_all(s);
5830+
5831+
for_each_kmem_cache_node(s, node, n) {
5832+
unsigned long flags;
5833+
struct page *page;
5834+
5835+
if (!atomic_long_read(&n->nr_slabs))
5836+
continue;
5837+
5838+
spin_lock_irqsave(&n->list_lock, flags);
5839+
list_for_each_entry(page, &n->partial, slab_list)
5840+
process_slab(t, s, page, alloc);
5841+
list_for_each_entry(page, &n->full, slab_list)
5842+
process_slab(t, s, page, alloc);
5843+
spin_unlock_irqrestore(&n->list_lock, flags);
5844+
}
5845+
5846+
return 0;
5847+
}
5848+
5849+
static int slab_debug_trace_release(struct inode *inode, struct file *file)
5850+
{
5851+
struct seq_file *seq = file->private_data;
5852+
struct loc_track *t = seq->private;
5853+
5854+
free_loc_track(t);
5855+
return seq_release_private(inode, file);
5856+
}
5857+
5858+
static const struct file_operations slab_debugfs_fops = {
5859+
.open = slab_debug_trace_open,
5860+
.read = seq_read,
5861+
.llseek = seq_lseek,
5862+
.release = slab_debug_trace_release,
5863+
};
5864+
5865+
static void debugfs_slab_add(struct kmem_cache *s)
5866+
{
5867+
struct dentry *slab_cache_dir;
5868+
5869+
if (unlikely(!slab_debugfs_root))
5870+
return;
5871+
5872+
slab_cache_dir = debugfs_create_dir(s->name, slab_debugfs_root);
5873+
5874+
debugfs_create_file("alloc_traces", 0400,
5875+
slab_cache_dir, s, &slab_debugfs_fops);
5876+
5877+
debugfs_create_file("free_traces", 0400,
5878+
slab_cache_dir, s, &slab_debugfs_fops);
5879+
}
5880+
5881+
void debugfs_slab_release(struct kmem_cache *s)
5882+
{
5883+
debugfs_remove_recursive(debugfs_lookup(s->name, slab_debugfs_root));
5884+
}
5885+
5886+
static int __init slab_debugfs_init(void)
5887+
{
5888+
struct kmem_cache *s;
5889+
5890+
slab_debugfs_root = debugfs_create_dir("slab", NULL);
5891+
5892+
list_for_each_entry(s, &slab_caches, list)
5893+
if (s->flags & SLAB_STORE_USER)
5894+
debugfs_slab_add(s);
5895+
5896+
return 0;
5897+
5898+
}
5899+
__initcall(slab_debugfs_init);
5900+
#endif
58135901
/*
58145902
* The /proc/slabinfo ABI
58155903
*/

0 commit comments

Comments
 (0)