mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] btf: Use BIN_ATTR_SIMPLE_RO() to define vmlinux attribute
@ 2024-11-22 12:56 Thomas Weißschuh
  2024-11-26 21:37 ` Andrii Nakryiko
  2024-11-27  1:52 ` Alexei Starovoitov
  0 siblings, 2 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2024-11-22 12:56 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa
  Cc: bpf, linux-kernel, Thomas Weißschuh

The usage of the macro allows to remove the custom handler function,
saving some memory. Additionally the code is easier to read.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
Something similar can be done to btf_module_read() in kernel/bpf/btf.c.
But doing it here and now would lead to some conflicts with some other
sysfs refactorings I'm doing. It will be part of a future series.
---
 kernel/bpf/sysfs_btf.c | 21 +++++----------------
 1 file changed, 5 insertions(+), 16 deletions(-)

diff --git a/kernel/bpf/sysfs_btf.c b/kernel/bpf/sysfs_btf.c
index fedb54c94cdb830a4890d33677dcc5a6e236c13f..a24381f933d0b80b11116d05463c35e9fa66acb1 100644
--- a/kernel/bpf/sysfs_btf.c
+++ b/kernel/bpf/sysfs_btf.c
@@ -12,34 +12,23 @@
 extern char __start_BTF[];
 extern char __stop_BTF[];
 
-static ssize_t
-btf_vmlinux_read(struct file *file, struct kobject *kobj,
-		 struct bin_attribute *bin_attr,
-		 char *buf, loff_t off, size_t len)
-{
-	memcpy(buf, __start_BTF + off, len);
-	return len;
-}
-
-static struct bin_attribute bin_attr_btf_vmlinux __ro_after_init = {
-	.attr = { .name = "vmlinux", .mode = 0444, },
-	.read = btf_vmlinux_read,
-};
+static __ro_after_init BIN_ATTR_SIMPLE_RO(vmlinux);
 
 struct kobject *btf_kobj;
 
 static int __init btf_vmlinux_init(void)
 {
-	bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;
+	bin_attr_vmlinux.private = __start_BTF;
+	bin_attr_vmlinux.size = __stop_BTF - __start_BTF;
 
-	if (bin_attr_btf_vmlinux.size == 0)
+	if (bin_attr_vmlinux.size == 0)
 		return 0;
 
 	btf_kobj = kobject_create_and_add("btf", kernel_kobj);
 	if (!btf_kobj)
 		return -ENOMEM;
 
-	return sysfs_create_bin_file(btf_kobj, &bin_attr_btf_vmlinux);
+	return sysfs_create_bin_file(btf_kobj, &bin_attr_vmlinux);
 }
 
 subsys_initcall(btf_vmlinux_init);

---
base-commit: 28eb75e178d389d325f1666e422bc13bbbb9804c
change-id: 20241122-sysfs-const-bin_attr-bpf-737286bb9f27

Best regards,
-- 
Thomas Weißschuh <linux@weissschuh.net>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] btf: Use BIN_ATTR_SIMPLE_RO() to define vmlinux attribute
  2024-11-22 12:56 [PATCH] btf: Use BIN_ATTR_SIMPLE_RO() to define vmlinux attribute Thomas Weißschuh
@ 2024-11-26 21:37 ` Andrii Nakryiko
  2024-11-26 21:42   ` Thomas Weißschuh
  2024-11-27  1:52 ` Alexei Starovoitov
  1 sibling, 1 reply; 6+ messages in thread
From: Andrii Nakryiko @ 2024-11-26 21:37 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	bpf, linux-kernel

On Fri, Nov 22, 2024 at 4:57 AM Thomas Weißschuh <linux@weissschuh.net> wrote:
>
> The usage of the macro allows to remove the custom handler function,
> saving some memory. Additionally the code is easier to read.
>
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
> Something similar can be done to btf_module_read() in kernel/bpf/btf.c.
> But doing it here and now would lead to some conflicts with some other
> sysfs refactorings I'm doing. It will be part of a future series.
> ---
>  kernel/bpf/sysfs_btf.c | 21 +++++----------------
>  1 file changed, 5 insertions(+), 16 deletions(-)
>

Nice, let's simplify. But why change the name to generic "vmlinux" if
it's actually btf_vmlinux? Can we keep the original btf-specific name?

pw-bot: cr

> diff --git a/kernel/bpf/sysfs_btf.c b/kernel/bpf/sysfs_btf.c
> index fedb54c94cdb830a4890d33677dcc5a6e236c13f..a24381f933d0b80b11116d05463c35e9fa66acb1 100644
> --- a/kernel/bpf/sysfs_btf.c
> +++ b/kernel/bpf/sysfs_btf.c
> @@ -12,34 +12,23 @@
>  extern char __start_BTF[];
>  extern char __stop_BTF[];
>
> -static ssize_t
> -btf_vmlinux_read(struct file *file, struct kobject *kobj,
> -                struct bin_attribute *bin_attr,
> -                char *buf, loff_t off, size_t len)
> -{
> -       memcpy(buf, __start_BTF + off, len);
> -       return len;
> -}
> -
> -static struct bin_attribute bin_attr_btf_vmlinux __ro_after_init = {
> -       .attr = { .name = "vmlinux", .mode = 0444, },
> -       .read = btf_vmlinux_read,
> -};
> +static __ro_after_init BIN_ATTR_SIMPLE_RO(vmlinux);
>
>  struct kobject *btf_kobj;
>
>  static int __init btf_vmlinux_init(void)
>  {
> -       bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;
> +       bin_attr_vmlinux.private = __start_BTF;
> +       bin_attr_vmlinux.size = __stop_BTF - __start_BTF;
>
> -       if (bin_attr_btf_vmlinux.size == 0)
> +       if (bin_attr_vmlinux.size == 0)
>                 return 0;
>
>         btf_kobj = kobject_create_and_add("btf", kernel_kobj);
>         if (!btf_kobj)
>                 return -ENOMEM;
>
> -       return sysfs_create_bin_file(btf_kobj, &bin_attr_btf_vmlinux);
> +       return sysfs_create_bin_file(btf_kobj, &bin_attr_vmlinux);
>  }
>
>  subsys_initcall(btf_vmlinux_init);
>
> ---
> base-commit: 28eb75e178d389d325f1666e422bc13bbbb9804c
> change-id: 20241122-sysfs-const-bin_attr-bpf-737286bb9f27
>
> Best regards,
> --
> Thomas Weißschuh <linux@weissschuh.net>
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] btf: Use BIN_ATTR_SIMPLE_RO() to define vmlinux attribute
  2024-11-26 21:37 ` Andrii Nakryiko
@ 2024-11-26 21:42   ` Thomas Weißschuh
  2024-11-26 21:57     ` Andrii Nakryiko
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Weißschuh @ 2024-11-26 21:42 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	bpf, linux-kernel

On 2024-11-26 13:37:26-0800, Andrii Nakryiko wrote:
> On Fri, Nov 22, 2024 at 4:57 AM Thomas Weißschuh <linux@weissschuh.net> wrote:
> >
> > The usage of the macro allows to remove the custom handler function,
> > saving some memory. Additionally the code is easier to read.
> >
> > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > ---
> > Something similar can be done to btf_module_read() in kernel/bpf/btf.c.
> > But doing it here and now would lead to some conflicts with some other
> > sysfs refactorings I'm doing. It will be part of a future series.
> > ---
> >  kernel/bpf/sysfs_btf.c | 21 +++++----------------
> >  1 file changed, 5 insertions(+), 16 deletions(-)
> >
> 
> Nice, let's simplify. But why change the name to generic "vmlinux" if
> it's actually btf_vmlinux? Can we keep the original btf-specific name?

The file in sysfs is named "vmlinux", /sys/kernel/btf/vmlinux.
This is what needs to be passed to the macro, it will name both the
variable and the file after it.

One alternative would be to use __BIN_ATTR_SIMPLE_RO() which allows a
custom name.

> 
> pw-bot: cr
> 
> > diff --git a/kernel/bpf/sysfs_btf.c b/kernel/bpf/sysfs_btf.c
> > index fedb54c94cdb830a4890d33677dcc5a6e236c13f..a24381f933d0b80b11116d05463c35e9fa66acb1 100644
> > --- a/kernel/bpf/sysfs_btf.c
> > +++ b/kernel/bpf/sysfs_btf.c
> > @@ -12,34 +12,23 @@
> >  extern char __start_BTF[];
> >  extern char __stop_BTF[];
> >
> > -static ssize_t
> > -btf_vmlinux_read(struct file *file, struct kobject *kobj,
> > -                struct bin_attribute *bin_attr,
> > -                char *buf, loff_t off, size_t len)
> > -{
> > -       memcpy(buf, __start_BTF + off, len);
> > -       return len;
> > -}
> > -
> > -static struct bin_attribute bin_attr_btf_vmlinux __ro_after_init = {
> > -       .attr = { .name = "vmlinux", .mode = 0444, },
> > -       .read = btf_vmlinux_read,
> > -};
> > +static __ro_after_init BIN_ATTR_SIMPLE_RO(vmlinux);
> >
> >  struct kobject *btf_kobj;
> >
> >  static int __init btf_vmlinux_init(void)
> >  {
> > -       bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;
> > +       bin_attr_vmlinux.private = __start_BTF;
> > +       bin_attr_vmlinux.size = __stop_BTF - __start_BTF;
> >
> > -       if (bin_attr_btf_vmlinux.size == 0)
> > +       if (bin_attr_vmlinux.size == 0)
> >                 return 0;
> >
> >         btf_kobj = kobject_create_and_add("btf", kernel_kobj);
> >         if (!btf_kobj)
> >                 return -ENOMEM;
> >
> > -       return sysfs_create_bin_file(btf_kobj, &bin_attr_btf_vmlinux);
> > +       return sysfs_create_bin_file(btf_kobj, &bin_attr_vmlinux);
> >  }
> >
> >  subsys_initcall(btf_vmlinux_init);
> >
> > ---
> > base-commit: 28eb75e178d389d325f1666e422bc13bbbb9804c
> > change-id: 20241122-sysfs-const-bin_attr-bpf-737286bb9f27
> >
> > Best regards,
> > --
> > Thomas Weißschuh <linux@weissschuh.net>
> >

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] btf: Use BIN_ATTR_SIMPLE_RO() to define vmlinux attribute
  2024-11-26 21:42   ` Thomas Weißschuh
@ 2024-11-26 21:57     ` Andrii Nakryiko
  0 siblings, 0 replies; 6+ messages in thread
From: Andrii Nakryiko @ 2024-11-26 21:57 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	bpf, linux-kernel

On Tue, Nov 26, 2024 at 1:42 PM Thomas Weißschuh <linux@weissschuh.net> wrote:
>
> On 2024-11-26 13:37:26-0800, Andrii Nakryiko wrote:
> > On Fri, Nov 22, 2024 at 4:57 AM Thomas Weißschuh <linux@weissschuh.net> wrote:
> > >
> > > The usage of the macro allows to remove the custom handler function,
> > > saving some memory. Additionally the code is easier to read.
> > >
> > > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > > ---
> > > Something similar can be done to btf_module_read() in kernel/bpf/btf.c.
> > > But doing it here and now would lead to some conflicts with some other
> > > sysfs refactorings I'm doing. It will be part of a future series.
> > > ---
> > >  kernel/bpf/sysfs_btf.c | 21 +++++----------------
> > >  1 file changed, 5 insertions(+), 16 deletions(-)
> > >
> >
> > Nice, let's simplify. But why change the name to generic "vmlinux" if
> > it's actually btf_vmlinux? Can we keep the original btf-specific name?
>
> The file in sysfs is named "vmlinux", /sys/kernel/btf/vmlinux.
> This is what needs to be passed to the macro, it will name both the
> variable and the file after it.
>
> One alternative would be to use __BIN_ATTR_SIMPLE_RO() which allows a
> custom name.
>

Probably not worth it, ok, I'm fine with the name change, it's a local
name anyways.

> >
> > pw-bot: cr
> >
> > > diff --git a/kernel/bpf/sysfs_btf.c b/kernel/bpf/sysfs_btf.c
> > > index fedb54c94cdb830a4890d33677dcc5a6e236c13f..a24381f933d0b80b11116d05463c35e9fa66acb1 100644
> > > --- a/kernel/bpf/sysfs_btf.c
> > > +++ b/kernel/bpf/sysfs_btf.c
> > > @@ -12,34 +12,23 @@
> > >  extern char __start_BTF[];
> > >  extern char __stop_BTF[];
> > >
> > > -static ssize_t
> > > -btf_vmlinux_read(struct file *file, struct kobject *kobj,
> > > -                struct bin_attribute *bin_attr,
> > > -                char *buf, loff_t off, size_t len)
> > > -{
> > > -       memcpy(buf, __start_BTF + off, len);
> > > -       return len;
> > > -}
> > > -
> > > -static struct bin_attribute bin_attr_btf_vmlinux __ro_after_init = {
> > > -       .attr = { .name = "vmlinux", .mode = 0444, },
> > > -       .read = btf_vmlinux_read,
> > > -};
> > > +static __ro_after_init BIN_ATTR_SIMPLE_RO(vmlinux);
> > >
> > >  struct kobject *btf_kobj;
> > >
> > >  static int __init btf_vmlinux_init(void)
> > >  {
> > > -       bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;
> > > +       bin_attr_vmlinux.private = __start_BTF;
> > > +       bin_attr_vmlinux.size = __stop_BTF - __start_BTF;
> > >
> > > -       if (bin_attr_btf_vmlinux.size == 0)
> > > +       if (bin_attr_vmlinux.size == 0)
> > >                 return 0;
> > >
> > >         btf_kobj = kobject_create_and_add("btf", kernel_kobj);
> > >         if (!btf_kobj)
> > >                 return -ENOMEM;
> > >
> > > -       return sysfs_create_bin_file(btf_kobj, &bin_attr_btf_vmlinux);
> > > +       return sysfs_create_bin_file(btf_kobj, &bin_attr_vmlinux);
> > >  }
> > >
> > >  subsys_initcall(btf_vmlinux_init);
> > >
> > > ---
> > > base-commit: 28eb75e178d389d325f1666e422bc13bbbb9804c
> > > change-id: 20241122-sysfs-const-bin_attr-bpf-737286bb9f27
> > >
> > > Best regards,
> > > --
> > > Thomas Weißschuh <linux@weissschuh.net>
> > >

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] btf: Use BIN_ATTR_SIMPLE_RO() to define vmlinux attribute
  2024-11-22 12:56 [PATCH] btf: Use BIN_ATTR_SIMPLE_RO() to define vmlinux attribute Thomas Weißschuh
  2024-11-26 21:37 ` Andrii Nakryiko
@ 2024-11-27  1:52 ` Alexei Starovoitov
  2024-11-27 15:22   ` Thomas Weißschuh
  1 sibling, 1 reply; 6+ messages in thread
From: Alexei Starovoitov @ 2024-11-27  1:52 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	bpf, LKML, Christian Brauner, Linus Torvalds, Greg Kroah-Hartman

On Fri, Nov 22, 2024 at 4:57 AM Thomas Weißschuh <linux@weissschuh.net> wrote:
>
> The usage of the macro allows to remove the custom handler function,
> saving some memory. Additionally the code is easier to read.
>
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
> Something similar can be done to btf_module_read() in kernel/bpf/btf.c.
> But doing it here and now would lead to some conflicts with some other
> sysfs refactorings I'm doing. It will be part of a future series.
> ---
>  kernel/bpf/sysfs_btf.c | 21 +++++----------------
>  1 file changed, 5 insertions(+), 16 deletions(-)
>
> diff --git a/kernel/bpf/sysfs_btf.c b/kernel/bpf/sysfs_btf.c
> index fedb54c94cdb830a4890d33677dcc5a6e236c13f..a24381f933d0b80b11116d05463c35e9fa66acb1 100644
> --- a/kernel/bpf/sysfs_btf.c
> +++ b/kernel/bpf/sysfs_btf.c
> @@ -12,34 +12,23 @@
>  extern char __start_BTF[];
>  extern char __stop_BTF[];
>
> -static ssize_t
> -btf_vmlinux_read(struct file *file, struct kobject *kobj,
> -                struct bin_attribute *bin_attr,
> -                char *buf, loff_t off, size_t len)
> -{
> -       memcpy(buf, __start_BTF + off, len);
> -       return len;
> -}
> -
> -static struct bin_attribute bin_attr_btf_vmlinux __ro_after_init = {
> -       .attr = { .name = "vmlinux", .mode = 0444, },
> -       .read = btf_vmlinux_read,
> -};
> +static __ro_after_init BIN_ATTR_SIMPLE_RO(vmlinux);

To be honest I really don't like when code is hidden by macros like this.
Looks like you guys already managed to sprinkle it in a few places.

btf_vmlinux_read() can be replaced with sysfs_bin_attr_simple_read().
This part is fine, but macro pls dont.
It doesn't help readability.
imo mode = 0444 vs mode = 0400 is easier to understand
instead of _RO vs _ADMIN_RO suffix.
__ro_after_init should be a part of it, at least.

I'd like to hear what other maintainers think about
such obfuscation.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] btf: Use BIN_ATTR_SIMPLE_RO() to define vmlinux attribute
  2024-11-27  1:52 ` Alexei Starovoitov
@ 2024-11-27 15:22   ` Thomas Weißschuh
  0 siblings, 0 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2024-11-27 15:22 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	bpf, LKML, Christian Brauner, Linus Torvalds, Greg Kroah-Hartman

On 2024-11-26 17:52:29-0800, Alexei Starovoitov wrote:
> On Fri, Nov 22, 2024 at 4:57 AM Thomas Weißschuh <linux@weissschuh.net> wrote:
> >
> > The usage of the macro allows to remove the custom handler function,
> > saving some memory. Additionally the code is easier to read.
> >
> > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > ---
> > Something similar can be done to btf_module_read() in kernel/bpf/btf.c.
> > But doing it here and now would lead to some conflicts with some other
> > sysfs refactorings I'm doing. It will be part of a future series.
> > ---
> >  kernel/bpf/sysfs_btf.c | 21 +++++----------------
> >  1 file changed, 5 insertions(+), 16 deletions(-)
> >
> > diff --git a/kernel/bpf/sysfs_btf.c b/kernel/bpf/sysfs_btf.c
> > index fedb54c94cdb830a4890d33677dcc5a6e236c13f..a24381f933d0b80b11116d05463c35e9fa66acb1 100644
> > --- a/kernel/bpf/sysfs_btf.c
> > +++ b/kernel/bpf/sysfs_btf.c
> > @@ -12,34 +12,23 @@
> >  extern char __start_BTF[];
> >  extern char __stop_BTF[];
> >
> > -static ssize_t
> > -btf_vmlinux_read(struct file *file, struct kobject *kobj,
> > -                struct bin_attribute *bin_attr,
> > -                char *buf, loff_t off, size_t len)
> > -{
> > -       memcpy(buf, __start_BTF + off, len);
> > -       return len;
> > -}
> > -
> > -static struct bin_attribute bin_attr_btf_vmlinux __ro_after_init = {
> > -       .attr = { .name = "vmlinux", .mode = 0444, },
> > -       .read = btf_vmlinux_read,
> > -};
> > +static __ro_after_init BIN_ATTR_SIMPLE_RO(vmlinux);
> 
> To be honest I really don't like when code is hidden by macros like this.
> Looks like you guys already managed to sprinkle it in a few places.
> 
> btf_vmlinux_read() can be replaced with sysfs_bin_attr_simple_read().
> This part is fine, but macro pls dont.
> It doesn't help readability.
> imo mode = 0444 vs mode = 0400 is easier to understand
> instead of _RO vs _ADMIN_RO suffix.

I'm fine with either solution.

My patch is motivated by my current effort to constify 'struct
bin_attribute' throughout the kernel.
With the macro I only have to touch this location once,
without it twice.

If we go with a plain sysfs_bin_attr_simple_read() please let me do the
patch in another series I have prepared, to be submitted after 6.13-rc1.

> __ro_after_init should be a part of it, at least.

I see where you are coming from, it would break the pattern with all the
other attribute macros however.

> I'd like to hear what other maintainers think about
> such obfuscation.

Ack.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-11-27 15:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-22 12:56 [PATCH] btf: Use BIN_ATTR_SIMPLE_RO() to define vmlinux attribute Thomas Weißschuh
2024-11-26 21:37 ` Andrii Nakryiko
2024-11-26 21:42   ` Thomas Weißschuh
2024-11-26 21:57     ` Andrii Nakryiko
2024-11-27  1:52 ` Alexei Starovoitov
2024-11-27 15:22   ` Thomas Weißschuh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®