Re: [peterz-queue:perf/core 18/22] kernel/events/core.c:6418:22: sparse: sparse: incorrect type in assignment (different address spaces)

From: Peter Zijlstra
Date: Thu Apr 15 2021 - 04:48:37 EST


On Wed, Apr 14, 2021 at 04:33:22PM +0200, Marco Elver wrote:
> On Wed, Apr 14, 2021 at 10:10PM +0800, kernel test robot wrote:
> > tree: https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git perf/core
> > head: 0da503cd07380952599b67ded6efe030d78ea42d
> > commit: c7d4112e9f0e69edd649665836ce72008b95ab9f [18/22] perf: Add support for SIGTRAP on perf events
> [...]
> > If you fix the issue, kindly add following tag as appropriate
> > Reported-by: kernel test robot <lkp@xxxxxxxxx>
> [...]
> > 6416 info.si_errno = event->attr.type;
> > 6417 info.si_perf = event->attr.sig_data;
> > > 6418 info.si_addr = (void *)event->sig_addr;
> > 6419 force_sig_info(&info);
>
> I think it wants the below (feel free to squash into "perf: Add support
> for SIGTRAP on perf events").
>
> Thanks,
> -- Marco
>
> ------ >8 ------
>
> From: Marco Elver <elver@xxxxxxxxxx>
> Date: Wed, 14 Apr 2021 16:26:26 +0200
> Subject: [PATCH] perf: Fix cast to void __user pointer
>
> sparse let us know that si_addr is 'void __user *', therefore add the
> missing __user attribute to the cast.
>
> Reported-by: kernel test robot <lkp@xxxxxxxxx>
> Signed-off-by: Marco Elver <elver@xxxxxxxxxx>
> ---
> kernel/events/core.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 1d2077389c0c..2677438ed668 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -6414,7 +6414,7 @@ static void perf_sigtrap(struct perf_event *event)
> info.si_code = TRAP_PERF;
> info.si_errno = event->attr.type;
> info.si_perf = event->attr.sig_data;
> - info.si_addr = (void *)event->sig_addr;
> + info.si_addr = (void __user *)event->sig_addr;
> force_sig_info(&info);
> }

Now the silly robot complains about:

CC kernel/events/core.o
../kernel/events/core.c: In function ‘perf_sigtrap’:
../kernel/events/core.c:6418:17: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
6418 | info.si_addr = (void __user *)event->sig_addr;

for all 32bit builds (because sig_addr is u64 and the pointer cast
truncates bits).

This had me look a little harder at sig_addr and I figured it should be
next to the pending fields for cache locality.

I've ended up with the below delta, does that work for you?

---

--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -735,6 +735,7 @@ struct perf_event {
int pending_wakeup;
int pending_kill;
int pending_disable;
+ unsigned long pending_addr; /* SIGTRAP */
struct irq_work pending;

atomic_t event_limit;
@@ -778,9 +779,6 @@ struct perf_event {
void *security;
#endif
struct list_head sb_list;
-
- /* Address associated with event, which can be passed to siginfo_t. */
- u64 sig_addr;
#endif /* CONFIG_PERF_EVENTS */
};

--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -6415,7 +6415,7 @@ static void perf_sigtrap(struct perf_eve
info.si_code = TRAP_PERF;
info.si_errno = event->attr.type;
info.si_perf = event->attr.sig_data;
- info.si_addr = (void __user *)event->sig_addr;
+ info.si_addr = (void __user *)event->pending_addr;
force_sig_info(&info);
}

@@ -9137,7 +9137,7 @@ static int __perf_event_overflow(struct
if (events && atomic_dec_and_test(&event->event_limit)) {
ret = 1;
event->pending_kill = POLL_HUP;
- event->sig_addr = data->addr;
+ event->pending_addr = data->addr;

perf_event_disable_inatomic(event);
}