[PATCH v8-RESEND 04/33] dyndbg: make ddebug_class_param union members same size

Jim Cromie posted 33 patches 1 year, 9 months ago
[PATCH v8-RESEND 04/33] dyndbg: make ddebug_class_param union members same size
Posted by Jim Cromie 1 year, 9 months ago
struct ddebug_class_param keeps a ref to the state-storage of the
param; make both class-types use the same unsigned long storage type.
ISTM this is simpler and safer.

Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
 include/linux/dynamic_debug.h | 2 +-
 lib/dynamic_debug.c           | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/include/linux/dynamic_debug.h b/include/linux/dynamic_debug.h
index 4fcbf4d4fd0a..5231aaf361c4 100644
--- a/include/linux/dynamic_debug.h
+++ b/include/linux/dynamic_debug.h
@@ -124,7 +124,7 @@ struct _ddebug_info {
 struct ddebug_class_param {
 	union {
 		unsigned long *bits;
-		unsigned int *lvl;
+		unsigned long *lvl;
 	};
 	char flags[8];
 	const struct ddebug_class_map *map;
diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
index 73ccf947d4aa..152b04c05981 100644
--- a/lib/dynamic_debug.c
+++ b/lib/dynamic_debug.c
@@ -799,7 +799,7 @@ int param_get_dyndbg_classes(char *buffer, const struct kernel_param *kp)
 
 	case DD_CLASS_TYPE_LEVEL_NAMES:
 	case DD_CLASS_TYPE_LEVEL_NUM:
-		return scnprintf(buffer, PAGE_SIZE, "%d\n", *dcp->lvl);
+		return scnprintf(buffer, PAGE_SIZE, "%ld\n", *dcp->lvl);
 	default:
 		return -1;
 	}
-- 
2.45.0
Re: [PATCH v8-RESEND 04/33] dyndbg: make ddebug_class_param union members same size
Posted by Łukasz Bartosik 1 year, 8 months ago
On Thu, May 16, 2024 at 7:44 PM Jim Cromie <jim.cromie@gmail.com> wrote:
>
> struct ddebug_class_param keeps a ref to the state-storage of the
> param; make both class-types use the same unsigned long storage type.
> ISTM this is simpler and safer.

Why is it safer for members of a union to have the same size ?


>
> Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
> ---
>  include/linux/dynamic_debug.h | 2 +-
>  lib/dynamic_debug.c           | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/include/linux/dynamic_debug.h b/include/linux/dynamic_debug.h
> index 4fcbf4d4fd0a..5231aaf361c4 100644
> --- a/include/linux/dynamic_debug.h
> +++ b/include/linux/dynamic_debug.h
> @@ -124,7 +124,7 @@ struct _ddebug_info {
>  struct ddebug_class_param {
>         union {
>                 unsigned long *bits;
> -               unsigned int *lvl;
> +               unsigned long *lvl;
>         };
>         char flags[8];
>         const struct ddebug_class_map *map;
> diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
> index 73ccf947d4aa..152b04c05981 100644
> --- a/lib/dynamic_debug.c
> +++ b/lib/dynamic_debug.c
> @@ -799,7 +799,7 @@ int param_get_dyndbg_classes(char *buffer, const struct kernel_param *kp)
>
>         case DD_CLASS_TYPE_LEVEL_NAMES:
>         case DD_CLASS_TYPE_LEVEL_NUM:
> -               return scnprintf(buffer, PAGE_SIZE, "%d\n", *dcp->lvl);
> +               return scnprintf(buffer, PAGE_SIZE, "%ld\n", *dcp->lvl);
>         default:
>                 return -1;
>         }
> --
> 2.45.0
>
Re: [PATCH v8-RESEND 04/33] dyndbg: make ddebug_class_param union members same size
Posted by jim.cromie@gmail.com 1 year, 8 months ago
On Tue, May 21, 2024 at 5:42 AM Łukasz Bartosik <ukaszb@chromium.org> wrote:
>
> On Thu, May 16, 2024 at 7:44 PM Jim Cromie <jim.cromie@gmail.com> wrote:
> >
> > struct ddebug_class_param keeps a ref to the state-storage of the
> > param; make both class-types use the same unsigned long storage type.
> > ISTM this is simpler and safer.
>
> Why is it safer for members of a union to have the same size ?
>

Its mostly extra paranoia.
but if 2 users somehow get the class-type mixed up,
at least theres no alignment issue added into it.

And a problem like this would naturally happen (murphys law)
when the PARAM_REF macro is used, which uses/shares the existing storage,
so that existing code can use that storage.
For example, DRM uses PARAM_REF so it can continue to use __drm_debug
for a few spots in code that still examine the value during normal operations.

I'll reword the commit msg to say that more clearly (than this?)