Skip to content

Commit

Permalink
tools/cgroup/slabinfo: update to work with struct slab
Browse files Browse the repository at this point in the history
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]/
  • Loading branch information
rgushchin authored and tehcaster committed Feb 21, 2022
1 parent 93dd04a commit 2219447
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions tools/cgroup/memcg_slabinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from drgn.helpers.linux import for_each_page
from drgn.helpers.linux.cpumask import for_each_online_cpu
from drgn.helpers.linux.percpu import per_cpu_ptr
from drgn import container_of, FaultError, Object
from drgn import container_of, FaultError, Object, cast


DESC = """
Expand Down Expand Up @@ -69,15 +69,15 @@ def oo_objects(s):


def count_partial(n, fn):
nr_pages = 0
for page in list_for_each_entry('struct page', n.partial.address_of_(),
'lru'):
nr_pages += fn(page)
return nr_pages
nr_objs = 0
for slab in list_for_each_entry('struct slab', n.partial.address_of_(),
'slab_list'):
nr_objs += fn(slab)
return nr_objs


def count_free(page):
return page.objects - page.inuse
def count_free(slab):
return slab.objects - slab.inuse


def slub_get_slabinfo(s, cfg):
Expand Down Expand Up @@ -145,14 +145,14 @@ def detect_kernel_config():
return cfg


def for_each_slab_page(prog):
def for_each_slab(prog):
PGSlab = 1 << prog.constant('PG_slab')
PGHead = 1 << prog.constant('PG_head')

for page in for_each_page(prog):
try:
if page.flags.value_() & PGSlab:
yield page
yield cast('struct slab *', page)
except FaultError:
pass

Expand Down Expand Up @@ -190,13 +190,13 @@ def main():
'list'):
obj_cgroups.add(ptr.value_())

# look over all slab pages, belonging to non-root memcgs
# and look for objects belonging to the given memory cgroup
for page in for_each_slab_page(prog):
objcg_vec_raw = page.memcg_data.value_()
# look over all slab folios and look for objects belonging
# to the given memory cgroup
for slab in for_each_slab(prog):
objcg_vec_raw = slab.memcg_data.value_()
if objcg_vec_raw == 0:
continue
cache = page.slab_cache
cache = slab.slab_cache
if not cache:
continue
addr = cache.value_()
Expand Down

0 comments on commit 2219447

Please sign in to comment.