Skip to content

Commit 2219447

Browse files
rgushchintehcaster
authored andcommitted
tools/cgroup/slabinfo: update to work with struct slab
After the introduction of the dedicated struct slab to describe slab pages by commit d122019 ("mm: Split slab into its own type") and the following removal of the corresponding struct page's fields by commit 07f910f ("mm: Remove slab from struct page") the memcg_slabinfo tool broke. An attempt to run it produces a trace like this: Traceback (most recent call last): File "/usr/bin/drgn", line 33, in <module> sys.exit(load_entry_point('drgn==0.0.16', 'console_scripts', 'drgn')()) File "/usr/lib64/python3.9/site-packages/drgn/internal/cli.py", line 133, in main runpy.run_path(args.script[0], init_globals=init_globals, run_name="__main__") File "/usr/lib64/python3.9/runpy.py", line 268, in run_path return _run_module_code(code, init_globals, run_name, File "/usr/lib64/python3.9/runpy.py", line 97, in _run_module_code _run_code(code, mod_globals, init_globals, File "/usr/lib64/python3.9/runpy.py", line 87, in _run_code exec(code, run_globals) File "memcg_slabinfo.py", line 226, in <module> main() File "memcg_slabinfo.py", line 199, in main cache = page.slab_cache AttributeError: 'struct page' has no member 'slab_cache' The problem can be fixed by explicitly casting struct page * to struct slab * for slab pages. The tools works as expected with this fix, e.g.: cred_jar 776 776 192 21 1 : tunables 0 0 0 : slabdata 547 547 0 kmalloc-cg-32 6 6 32 128 1 : tunables 0 0 0 : slabdata 9 9 0 files_cache 3 3 832 39 8 : tunables 0 0 0 : slabdata 8 8 0 kmalloc-cg-512 1 1 512 32 4 : tunables 0 0 0 : slabdata 10 10 0 task_struct 10 10 6720 4 8 : tunables 0 0 0 : slabdata 63 63 0 mm_struct 3 3 1664 19 8 : tunables 0 0 0 : slabdata 9 9 0 kmalloc-cg-16 1 1 16 256 1 : tunables 0 0 0 : slabdata 8 8 0 pde_opener 1 1 40 102 1 : tunables 0 0 0 : slabdata 8 8 0 anon_vma_chain 375 375 64 64 1 : tunables 0 0 0 : slabdata 81 81 0 radix_tree_node 3 3 584 28 4 : tunables 0 0 0 : slabdata 419 419 0 dentry 98 98 312 26 2 : tunables 0 0 0 : slabdata 1420 1420 0 btrfs_inode 3 3 2368 13 8 : tunables 0 0 0 : slabdata 730 730 0 signal_cache 3 3 1600 20 8 : tunables 0 0 0 : slabdata 17 17 0 sighand_cache 3 3 2240 14 8 : tunables 0 0 0 : slabdata 20 20 0 filp 90 90 512 32 4 : tunables 0 0 0 : slabdata 95 95 0 anon_vma 214 214 200 20 1 : tunables 0 0 0 : slabdata 162 162 0 kmalloc-cg-1k 1 1 1024 32 8 : tunables 0 0 0 : slabdata 22 22 0 pid 10 10 256 32 2 : tunables 0 0 0 : slabdata 14 14 0 kmalloc-cg-64 2 2 64 64 1 : tunables 0 0 0 : slabdata 8 8 0 kmalloc-cg-96 3 3 96 42 1 : tunables 0 0 0 : slabdata 8 8 0 sock_inode_cache 5 5 1408 23 8 : tunables 0 0 0 : slabdata 29 29 0 UNIX 7 7 1920 17 8 : tunables 0 0 0 : slabdata 21 21 0 inode_cache 36 36 1152 28 8 : tunables 0 0 0 : slabdata 680 680 0 proc_inode_cache 26 26 1224 26 8 : tunables 0 0 0 : slabdata 64 64 0 kmalloc-cg-2k 2 2 2048 16 8 : tunables 0 0 0 : slabdata 9 9 0 v2: change naming and count_partial()/count_free()/for_each_slab() signatures to work with slabs, suggested by Matthew Wilcox Fixes: 07f910f ("mm: Remove slab from struct page") Reported-by: Vasily Averin <[email protected]> Signed-off-by: Roman Gushchin <[email protected]> Tested-by: Vasily Averin <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]> Link: https://lore.kernel.org/linux-patches/[email protected]/
1 parent 93dd04a commit 2219447

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

tools/cgroup/memcg_slabinfo.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from drgn.helpers.linux import for_each_page
1212
from drgn.helpers.linux.cpumask import for_each_online_cpu
1313
from drgn.helpers.linux.percpu import per_cpu_ptr
14-
from drgn import container_of, FaultError, Object
14+
from drgn import container_of, FaultError, Object, cast
1515

1616

1717
DESC = """
@@ -69,15 +69,15 @@ def oo_objects(s):
6969

7070

7171
def count_partial(n, fn):
72-
nr_pages = 0
73-
for page in list_for_each_entry('struct page', n.partial.address_of_(),
74-
'lru'):
75-
nr_pages += fn(page)
76-
return nr_pages
72+
nr_objs = 0
73+
for slab in list_for_each_entry('struct slab', n.partial.address_of_(),
74+
'slab_list'):
75+
nr_objs += fn(slab)
76+
return nr_objs
7777

7878

79-
def count_free(page):
80-
return page.objects - page.inuse
79+
def count_free(slab):
80+
return slab.objects - slab.inuse
8181

8282

8383
def slub_get_slabinfo(s, cfg):
@@ -145,14 +145,14 @@ def detect_kernel_config():
145145
return cfg
146146

147147

148-
def for_each_slab_page(prog):
148+
def for_each_slab(prog):
149149
PGSlab = 1 << prog.constant('PG_slab')
150150
PGHead = 1 << prog.constant('PG_head')
151151

152152
for page in for_each_page(prog):
153153
try:
154154
if page.flags.value_() & PGSlab:
155-
yield page
155+
yield cast('struct slab *', page)
156156
except FaultError:
157157
pass
158158

@@ -190,13 +190,13 @@ def main():
190190
'list'):
191191
obj_cgroups.add(ptr.value_())
192192

193-
# look over all slab pages, belonging to non-root memcgs
194-
# and look for objects belonging to the given memory cgroup
195-
for page in for_each_slab_page(prog):
196-
objcg_vec_raw = page.memcg_data.value_()
193+
# look over all slab folios and look for objects belonging
194+
# to the given memory cgroup
195+
for slab in for_each_slab(prog):
196+
objcg_vec_raw = slab.memcg_data.value_()
197197
if objcg_vec_raw == 0:
198198
continue
199-
cache = page.slab_cache
199+
cache = slab.slab_cache
200200
if not cache:
201201
continue
202202
addr = cache.value_()

0 commit comments

Comments
 (0)