Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools/slabratetop: call to undeclared function 'slab_address' #4438

Closed
Rtoax opened this issue Jan 16, 2023 · 6 comments · Fixed by #4447
Closed

tools/slabratetop: call to undeclared function 'slab_address' #4438

Rtoax opened this issue Jan 16, 2023 · 6 comments · Fixed by #4447

Comments

@Rtoax
Copy link
Contributor

Rtoax commented Jan 16, 2023

When my system uses SLUB And the kernel version >=5.16, there are the following problems:

$ sudo ./slabratetop.py
In file included from /virtual/main.c:13:
include/linux/slub_def.h:162:26: warning: call to undeclared function 'slab_address'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        void *object = x - (x - slab_address(slab)) % cache->size;
                                ^
include/linux/slub_def.h:162:46: error: invalid operands to binary expression ('void *' and 'unsigned int')
        void *object = x - (x - slab_address(slab)) % cache->size;
                           ~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
include/linux/slub_def.h:164:8: error: incomplete definition of type 'struct slab'
                (slab->objects - 1) * cache->size;
                 ~~~~^
include/linux/kasan.h:13:8: note: forward declaration of 'struct slab'
struct slab;
       ^
In file included from /virtual/main.c:13:
include/linux/slub_def.h:184:31: warning: call to undeclared function 'slab_address'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        return __obj_to_index(cache, slab_address(slab), obj);
                                     ^
include/linux/slub_def.h:184:31: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'void *' [-Wint-conversion]
        return __obj_to_index(cache, slab_address(slab), obj);
                                     ^~~~~~~~~~~~~~~~~~
include/linux/slub_def.h:173:14: note: passing argument to parameter 'addr' here
                                          void *addr, void *obj)
                                                ^
include/linux/slub_def.h:190:13: error: incomplete definition of type 'struct slab'
        return slab->objects;
               ~~~~^
include/linux/kasan.h:13:8: note: forward declaration of 'struct slab'
struct slab;
       ^
2 warnings and 4 errors generated.
Traceback (most recent call last):
  File "/home/rongtao/Git/bcc/tools/./slabratetop.py", line 111, in <module>
    b = BPF(text=bpf_text)
        ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/bcc/__init__.py", line 475, in __init__
    raise Exception("Failed to compile BPF module %s" % (src_file or "<text>"))
Exception: Failed to compile BPF module <text>

see commit 40f3bf0cb04c("mm: Convert struct page to struct slab in functions used by other subsystems")

This issue should be modified in the kernel, I'm trying to fix this, kernel submit mm: Functions used internally should not be put into slub_def.h totally solve this fatal error, hope it'll be merged to kernel souce code tree.

@Rtoax
Copy link
Contributor Author

Rtoax commented Jan 16, 2023

Rtoax added a commit to Rtoax/linux that referenced this issue Jan 16, 2023
commit 40f3bf0("mm: Convert struct page to struct slab in functions
used by other subsystems") introduce 'slab_address()' and 'struct slab'
in slab_def.h(CONFIG_SLAB) and slub_def.h(CONFIG_SLUB). When referencing
a header file <linux/slub_def.h> in a module or BPF code, 'slab_address()'
and 'struct slab' are not recognized, resulting in incomplete and
undefined errors(see bcc slabratetop.py error [0]).

Moving the function definitions of reference data structures such as
struct slab and slab_address() such as nearest_obj(), obj_to_index(),
and objs_per_slab() to the internal header file slab.h solves this
fatal problem.

[0] iovisor/bcc#4438

Signed-off-by: Rong Tao <rongtao@cestc.cn>
@Rtoax
Copy link
Contributor Author

Rtoax commented Jan 17, 2023

Sadly, kernel maintainer tends to not merge the above commit mm: Functions used internally should not be put into slub_def.h, that is to say, slabratetop.py could not include <sl*b_def.h> anymore since kernel commit 40f3bf0cb04c(“mm: Convert struct page to struct slab in functions used by other subsystems”). may we should solve the kmem_cache structure with BTF way.

@yonghong-song
Copy link
Collaborator

As a temporary workaround, something like below will work,

diff --git a/tools/slabratetop.py b/tools/slabratetop.py
index ac44b2bd..084875f3 100755
--- a/tools/slabratetop.py
+++ b/tools/slabratetop.py
@@ -65,6 +65,78 @@ bpf_text = """
 // 5.9, but it does not hurt to have it here for versions 5.4 to 5.8.
 struct memcg_cache_params {};
 
+struct slab {
+        unsigned long __page_flags;
+
+#if defined(CONFIG_SLAB)
+
+        struct kmem_cache *slab_cache;
+        union {
+                struct {
+                        struct list_head slab_list;
+                        void *freelist; /* array of free object indexes */
+                        void *s_mem;    /* first object */
+                };
+                struct rcu_head rcu_head;
+        };
+        unsigned int active;
+
+#elif defined(CONFIG_SLUB)
+
+        struct kmem_cache *slab_cache;
+        union {
+                struct {
+                        union {
+                                struct list_head slab_list;
+#ifdef CONFIG_SLUB_CPU_PARTIAL
+                                struct {
+                                        struct slab *next;
+                                        int slabs;      /* Nr of slabs left */
+                                };
+#endif
+                        };
+                        /* Double-word boundary */
+                        void *freelist;         /* first free object */
+                        union {
+                                unsigned long counters;
+                                struct {
+                                        unsigned inuse:16;
+                                        unsigned objects:15;
+                                        unsigned frozen:1;
+                                };
+                        };
+                };
+                struct rcu_head rcu_head;
+        };
+        unsigned int __unused;
+
+#elif defined(CONFIG_SLOB)
+
+        struct list_head slab_list;
+        void *__unused_1;
+        void *freelist;         /* first free block */
+        long units;
+        unsigned int __unused_2;
+
+#else
+#error "Unexpected slab allocator configured"
+#endif
+
+        atomic_t __page_refcount;
+#ifdef CONFIG_MEMCG
+        unsigned long memcg_data;
+#endif
+};
+
+#define slab_folio(s)           (_Generic((s),                          \
+        const struct slab *:    (const struct folio *)s,                \
+        struct slab *:          (struct folio *)s))
+
+static inline void *slab_address(const struct slab *slab)
+{
+        return folio_address(slab_folio(slab));
+}
+
 #ifdef CONFIG_SLUB
 #include <linux/slub_def.h>
 #else

The above is based on latest bpf-next code base and need to be guarded with proper kernel versions or by some btf struct change around 40f3bf0cb04c("mm: Convert struct page to struct slab in functions used by other subsystems"). Need to double check the structure definition for earlier versions as well.

Currently, there is an effort (#4405) to move bcc source to be more libbpf-processible. Hopefully with that it can make it easier to access the structure easier.

@Rtoax
Copy link
Contributor Author

Rtoax commented Jan 22, 2023

@yonghong-song Thanks, Song. This patch solved my problem as a temporary solution. I have followed the submission of 4405, thanks again.

@yonghong-song
Copy link
Collaborator

@Rtoax Although this is a temporary workaround, could you still add the above temporary solution to slabratetop tool? This way, we can at least make slabratetop work for the >= 5.16 kernels.

Rtoax added a commit to Rtoax/bcc that referenced this issue Jan 23, 2023
…lab'

kernel commit 40f3bf0cb04c("mm: Convert struct page to struct slab in functions
used by other subsystems") introduce slab_address() function, commit 6e48a966dfd1
("mm/kasan: Convert to struct folio and struct slab") linux/kasan.h adds a
dependency on the slab struct, This leads to the following problems:

    $ sudo ./slabratetop.py
    In file included from /virtual/main.c:13:
    include/linux/slub_def.h:162:26: warning: call to undeclared function 'slab_address';
    ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
            void *object = x - (x - slab_address(slab)) % cache->size;
                                    ^
    include/linux/slub_def.h:162:46: error: invalid operands to binary expression ('void *' and 'unsigned int')
            void *object = x - (x - slab_address(slab)) % cache->size;
                               ~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
    include/linux/slub_def.h:164:8: error: incomplete definition of type 'struct slab'
                    (slab->objects - 1) * cache->size;
                     ~~~~^
    include/linux/kasan.h:13:8: note: forward declaration of 'struct slab'
    struct slab;
           ^
    ...

At first, I wanted to fix this with a kernel patch [1], however, bcc as a
downstream project of the kernel, this issue should be solved inside the bcc
project. This is agreed by kernel maintainer and bcc maintainer @yonghong-song.

This solution is provided by @yonghong-song [0].

[0] iovisor#4438
[1] https://lore.kernel.org/all/tencent_ABA832E296819D1053D6C625ADCAF76BC706@qq.com/

Signed-off-by: Rong Tao <rongtao@cestc.cn>
Signed-off-by: Yonghong Song <yhs@fb.com>
@Rtoax
Copy link
Contributor Author

Rtoax commented Jan 23, 2023

@yonghong-song Hi, Song, I just submit a PR #4447

Rtoax added a commit to Rtoax/bcc that referenced this issue Jan 23, 2023
…lab'

kernel commit 40f3bf0cb04c("mm: Convert struct page to struct slab in functions
used by other subsystems") introduce slab_address() function, commit 6e48a966dfd1
("mm/kasan: Convert to struct folio and struct slab") linux/kasan.h adds a
dependency on the slab struct, This leads to the following problems:

    $ sudo ./slabratetop.py
    In file included from /virtual/main.c:13:
    include/linux/slub_def.h:162:26: warning: call to undeclared function 'slab_address';
    ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
            void *object = x - (x - slab_address(slab)) % cache->size;
                                    ^
    include/linux/slub_def.h:162:46: error: invalid operands to binary expression ('void *' and 'unsigned int')
            void *object = x - (x - slab_address(slab)) % cache->size;
                               ~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
    include/linux/slub_def.h:164:8: error: incomplete definition of type 'struct slab'
                    (slab->objects - 1) * cache->size;
                     ~~~~^
    include/linux/kasan.h:13:8: note: forward declaration of 'struct slab'
    struct slab;
           ^
    ...

At first, I wanted to fix this with a kernel patch [1], however, bcc as a
downstream project of the kernel, this issue should be solved inside the bcc
project. This is agreed by kernel maintainer and bcc maintainer @yonghong-song.

This solution is provided by @yonghong-song [0].

[0] iovisor#4438
[1] https://lore.kernel.org/all/tencent_ABA832E296819D1053D6C625ADCAF76BC706@qq.com/

Signed-off-by: Rong Tao <rongtao@cestc.cn>
Signed-off-by: Yonghong Song <yhs@fb.com>
Rtoax added a commit to Rtoax/bcc that referenced this issue Jan 23, 2023
…lab'

kernel commit 40f3bf0cb04c("mm: Convert struct page to struct slab in functions
used by other subsystems") introduce slab_address() function, commit 6e48a966dfd1
("mm/kasan: Convert to struct folio and struct slab") linux/kasan.h adds a
dependency on the slab struct, This leads to the following problems:

    $ sudo ./slabratetop.py
    In file included from /virtual/main.c:13:
    include/linux/slub_def.h:162:26: warning: call to undeclared function 'slab_address';
    ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
            void *object = x - (x - slab_address(slab)) % cache->size;
                                    ^
    include/linux/slub_def.h:162:46: error: invalid operands to binary expression ('void *' and 'unsigned int')
            void *object = x - (x - slab_address(slab)) % cache->size;
                               ~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
    include/linux/slub_def.h:164:8: error: incomplete definition of type 'struct slab'
                    (slab->objects - 1) * cache->size;
                     ~~~~^
    include/linux/kasan.h:13:8: note: forward declaration of 'struct slab'
    struct slab;
           ^
    ...

At first, I wanted to fix this with a kernel patch [1], however, bcc as a
downstream project of the kernel, this issue should be solved inside the bcc
project. This is agreed by kernel maintainer and bcc maintainer @yonghong-song.

This solution is provided by @yonghong-song [0].

[0] iovisor#4438
[1] https://lore.kernel.org/all/tencent_ABA832E296819D1053D6C625ADCAF76BC706@qq.com/

Signed-off-by: Rong Tao <rongtao@cestc.cn>
Signed-off-by: Yonghong Song <yhs@fb.com>
Rtoax added a commit to Rtoax/bcc that referenced this issue Jan 23, 2023
…lab'

kernel commit 40f3bf0cb04c("mm: Convert struct page to struct slab in functions
used by other subsystems") introduce slab_address() function, commit 6e48a966dfd1
("mm/kasan: Convert to struct folio and struct slab") linux/kasan.h adds a
dependency on the slab struct, This leads to the following problems:

    $ sudo ./slabratetop.py
    In file included from /virtual/main.c:13:
    include/linux/slub_def.h:162:26: warning: call to undeclared function 'slab_address';
    ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
            void *object = x - (x - slab_address(slab)) % cache->size;
                                    ^
    include/linux/slub_def.h:162:46: error: invalid operands to binary expression ('void *' and 'unsigned int')
            void *object = x - (x - slab_address(slab)) % cache->size;
                               ~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
    include/linux/slub_def.h:164:8: error: incomplete definition of type 'struct slab'
                    (slab->objects - 1) * cache->size;
                     ~~~~^
    include/linux/kasan.h:13:8: note: forward declaration of 'struct slab'
    struct slab;
           ^
    ...

At first, I wanted to fix this with a kernel patch [1], however, bcc as a
downstream project of the kernel, this issue should be solved inside the bcc
project. This is agreed by kernel maintainer and bcc maintainer @yonghong-song.

This solution is provided by @yonghong-song [0].

[0] iovisor#4438
[1] https://lore.kernel.org/all/tencent_ABA832E296819D1053D6C625ADCAF76BC706@qq.com/

Signed-off-by: Rong Tao <rongtao@cestc.cn>
Signed-off-by: Yonghong Song <yhs@fb.com>
yonghong-song pushed a commit that referenced this issue Feb 5, 2023
…lab'

kernel commit 40f3bf0cb04c("mm: Convert struct page to struct slab in functions
used by other subsystems") introduce slab_address() function, commit 6e48a966dfd1
("mm/kasan: Convert to struct folio and struct slab") linux/kasan.h adds a
dependency on the slab struct, This leads to the following problems:

    $ sudo ./slabratetop.py
    In file included from /virtual/main.c:13:
    include/linux/slub_def.h:162:26: warning: call to undeclared function 'slab_address';
    ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
            void *object = x - (x - slab_address(slab)) % cache->size;
                                    ^
    include/linux/slub_def.h:162:46: error: invalid operands to binary expression ('void *' and 'unsigned int')
            void *object = x - (x - slab_address(slab)) % cache->size;
                               ~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
    include/linux/slub_def.h:164:8: error: incomplete definition of type 'struct slab'
                    (slab->objects - 1) * cache->size;
                     ~~~~^
    include/linux/kasan.h:13:8: note: forward declaration of 'struct slab'
    struct slab;
           ^
    ...

At first, I wanted to fix this with a kernel patch [1], however, bcc as a
downstream project of the kernel, this issue should be solved inside the bcc
project. This is agreed by kernel maintainer and bcc maintainer @yonghong-song.

This solution is provided by @yonghong-song [0].

[0] #4438
[1] https://lore.kernel.org/all/tencent_ABA832E296819D1053D6C625ADCAF76BC706@qq.com/

Signed-off-by: Rong Tao <rongtao@cestc.cn>
Signed-off-by: Yonghong Song <yhs@fb.com>
curu pushed a commit to curu/bcc that referenced this issue Apr 20, 2023
…lab'

kernel commit 40f3bf0cb04c("mm: Convert struct page to struct slab in functions
used by other subsystems") introduce slab_address() function, commit 6e48a966dfd1
("mm/kasan: Convert to struct folio and struct slab") linux/kasan.h adds a
dependency on the slab struct, This leads to the following problems:

    $ sudo ./slabratetop.py
    In file included from /virtual/main.c:13:
    include/linux/slub_def.h:162:26: warning: call to undeclared function 'slab_address';
    ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
            void *object = x - (x - slab_address(slab)) % cache->size;
                                    ^
    include/linux/slub_def.h:162:46: error: invalid operands to binary expression ('void *' and 'unsigned int')
            void *object = x - (x - slab_address(slab)) % cache->size;
                               ~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
    include/linux/slub_def.h:164:8: error: incomplete definition of type 'struct slab'
                    (slab->objects - 1) * cache->size;
                     ~~~~^
    include/linux/kasan.h:13:8: note: forward declaration of 'struct slab'
    struct slab;
           ^
    ...

At first, I wanted to fix this with a kernel patch [1], however, bcc as a
downstream project of the kernel, this issue should be solved inside the bcc
project. This is agreed by kernel maintainer and bcc maintainer @yonghong-song.

This solution is provided by @yonghong-song [0].

[0] iovisor#4438
[1] https://lore.kernel.org/all/tencent_ABA832E296819D1053D6C625ADCAF76BC706@qq.com/

Signed-off-by: Rong Tao <rongtao@cestc.cn>
Signed-off-by: Yonghong Song <yhs@fb.com>
captain5050 pushed a commit to captain5050/bcc that referenced this issue Oct 12, 2023
…lab'

kernel commit 40f3bf0cb04c("mm: Convert struct page to struct slab in functions
used by other subsystems") introduce slab_address() function, commit 6e48a966dfd1
("mm/kasan: Convert to struct folio and struct slab") linux/kasan.h adds a
dependency on the slab struct, This leads to the following problems:

    $ sudo ./slabratetop.py
    In file included from /virtual/main.c:13:
    include/linux/slub_def.h:162:26: warning: call to undeclared function 'slab_address';
    ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
            void *object = x - (x - slab_address(slab)) % cache->size;
                                    ^
    include/linux/slub_def.h:162:46: error: invalid operands to binary expression ('void *' and 'unsigned int')
            void *object = x - (x - slab_address(slab)) % cache->size;
                               ~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
    include/linux/slub_def.h:164:8: error: incomplete definition of type 'struct slab'
                    (slab->objects - 1) * cache->size;
                     ~~~~^
    include/linux/kasan.h:13:8: note: forward declaration of 'struct slab'
    struct slab;
           ^
    ...

At first, I wanted to fix this with a kernel patch [1], however, bcc as a
downstream project of the kernel, this issue should be solved inside the bcc
project. This is agreed by kernel maintainer and bcc maintainer @yonghong-song.

This solution is provided by @yonghong-song [0].

[0] iovisor#4438
[1] https://lore.kernel.org/all/tencent_ABA832E296819D1053D6C625ADCAF76BC706@qq.com/

Signed-off-by: Rong Tao <rongtao@cestc.cn>
Signed-off-by: Yonghong Song <yhs@fb.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants