Skip to contentSkip to conversation

selftests/bpf: Fix a testmod compilation failure due to missing const modifier

PATCH In review Yonghong Song 2 participants 3 messages Jun 2, 2025

Subsystems (4)
lore
Share & tools
lore

Yonghong Song

author reviewer
#0 lore raw

PATCH 1/1 selftests/bpf: Fix a testmod compilation failure due to missing const modifier

Commit 97d06802d10a ("sysfs: constify bin_attribute argument of bin_attribute::read/write()") added constant modifier to 'struct bin_attribute *' for read/write
func pointer members. This caused compilation failure with clang20:

bpf_testmod.c:494:10: error: incompatible function pointer types initializing
'ssize_t (*)(struct file *, struct kobject *, const struct bin_attribute *, char *, loff_t, size_t)' (aka 'long (*)(struct file *, struct kobject *, const struct bin_attribute *,
char *, long long, unsigned long)')
with an expression of type 'ssize_t (struct file *, struct kobject *, struct bin_attribute *,

                                       char *, loff_t, size_t)'
  (aka 'long (struct file *, struct kobject *, struct bin_attribute *, char *, long long, unsigned long)')
  [-Wincompatible-function-pointer-types]
  494 |         .read = bpf_testmod_test_read,
      |                 ^~~~~~~~~~~~~~~~~~~~~

...

The same compilation error for functions bpf_testmod_test_write() and bpf_testmod_uprobe_write().

Fix the build failure by adding proper 'const' modifier.

---
 tools/testing/selftests/bpf/test_kmods/bpf_testmod.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +3 -3
index e6c248e3ae54..e9e918cdf31f 100644
@@ -385,7 +385,7 @@ int bpf_testmod_fentry_ok;
 
 noinline ssize_t
 bpf_testmod_test_read(struct file *file, struct kobject *kobj,
-		      struct bin_attribute *bin_attr,
+		      const struct bin_attribute *bin_attr,
 		      char *buf, loff_t off, size_t len)
 {
 	struct bpf_testmod_test_read_ctx ctx = {
@@ -465,7 +465,7 @@ ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
 
 noinline ssize_t
 bpf_testmod_test_write(struct file *file, struct kobject *kobj,
-		      struct bin_attribute *bin_attr,
+		      const struct bin_attribute *bin_attr,
 		      char *buf, loff_t off, size_t len)
 {
 	struct bpf_testmod_test_write_ctx ctx = {
@@ -567,7 +567,7 @@ static void testmod_unregister_uprobe(void)
 
 static ssize_t
 bpf_testmod_uprobe_write(struct file *file, struct kobject *kobj,
-			 struct bin_attribute *bin_attr,
+			 const struct bin_attribute *bin_attr,
 			 char *buf, loff_t off, size_t len)
 {
 	unsigned long offset = 0;
Signature · 1 line
2.47.1
#1 lore raw
Quoted 15 lines · Yonghong Song

Commit 97d06802d10a ("sysfs: constify bin_attribute argument of bin_attribute::read/write()") added constant modifier to 'struct bin_attribute *' for read/write
func pointer members. This caused compilation failure with clang20:

bpf_testmod.c:494:10: error: incompatible function pointer types initializing
'ssize_t (*)(struct file *, struct kobject *, const struct bin_attribute *, char *, loff_t, size_t)' (aka 'long (*)(struct file *, struct kobject *, const struct bin_attribute *,
char *, long long, unsigned long)')
with an expression of type 'ssize_t (struct file *, struct kobject *, struct bin_attribute *,

                                       char *, loff_t, size_t)'
  (aka 'long (struct file *, struct kobject *, struct bin_attribute *, char *, long long, unsigned long)')
  [-Wincompatible-function-pointer-types]
  494 |         .read = bpf_testmod_test_read,
      |                 ^~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines

...

The same compilation error for functions bpf_testmod_test_write() and bpf_testmod_uprobe_write().

Fix the build failure by adding proper 'const' modifier.

I already applied similar patch to bpf tree couple hours ago.

author reviewer
#2 lore raw
Quoted 17 lines · Alexei Starovoitov
Quoted 16 lines · Yonghong Song

Commit 97d06802d10a ("sysfs: constify bin_attribute argument of bin_attribute::read/write()") added constant modifier to 'struct bin_attribute *' for read/write
func pointer members. This caused compilation failure with clang20:

bpf_testmod.c:494:10: error: incompatible function pointer types initializing
'ssize_t (*)(struct file *, struct kobject *, const struct bin_attribute *, char *, loff_t, size_t)' (aka 'long (*)(struct file *, struct kobject *, const struct bin_attribute *,
char *, long long, unsigned long)')
with an expression of type 'ssize_t (struct file *, struct kobject *, struct bin_attribute *,

                                        char *, loff_t, size_t)'
   (aka 'long (struct file *, struct kobject *, struct bin_attribute *, char *, long long, unsigned long)')
   [-Wincompatible-function-pointer-types]
   494 |         .read = bpf_testmod_test_read,
       |                 ^~~~~~~~~~~~~~~~~~~~~

...

Quoted 6 lines
Quoted 5 lines

The same compilation error for functions bpf_testmod_test_write() and bpf_testmod_uprobe_write().

Fix the build failure by adding proper 'const' modifier.

I already applied similar patch to bpf tree couple hours ago.

Ok, sounds good. I only checked bpf-next tree.