Re: [PATCH v8 09/13] module: Move kallsyms support into a separate file

From: Petr Mladek
Date: Fri Feb 25 2022 - 05:16:00 EST


On Fri 2022-02-25 09:27:33, Christophe Leroy wrote:
>
>
> Le 25/02/2022 à 10:15, Petr Mladek a écrit :
> > On Tue 2022-02-22 14:12:59, Aaron Tomlin wrote:
> >> No functional change.
> >
> > The patch adds rcu_dereference_sched() into several locations.
> > It triggers lockdep warnings, see below.
> >
> > It is good example why avoid any hidden changes when shuffling
> > code. The changes in the code should be done in a preparatory
> > patch or not at all.
> >
> > This patch is even worse because these changes were not
> > mentioned in the commit message. It should describe what
> > is done and why.
> >
> > I wonder how many other changes are hidden in this patchset
> > and if anyone really checked them.
>
> That's probably my fault, when I reviewed version v5 of the series I
> mentionned all checkpatch and sparse reports asking Aaron to make his
> series exempt of such warnings. Most warnings where related to style
> (parenthesis alignment, blank lines, spaces, etc ...) or erroneous
> casting etc....
>
> But for that particular patch we had:
>
> kernel/module/kallsyms.c:174:23: warning: incorrect type in assignment
> (different address spaces)
> kernel/module/kallsyms.c:174:23: expected struct mod_kallsyms
> [noderef] __rcu *kallsyms
> kernel/module/kallsyms.c:174:23: got void *
> kernel/module/kallsyms.c:176:12: warning: dereference of noderef expression
> kernel/module/kallsyms.c:177:12: warning: dereference of noderef expression
> kernel/module/kallsyms.c:179:12: warning: dereference of noderef expression
> kernel/module/kallsyms.c:180:12: warning: dereference of noderef expression
> kernel/module/kallsyms.c:189:18: warning: dereference of noderef expression
> kernel/module/kallsyms.c:190:35: warning: dereference of noderef expression
> kernel/module/kallsyms.c:191:20: warning: dereference of noderef expression
> kernel/module/kallsyms.c:196:32: warning: dereference of noderef expression
> kernel/module/kallsyms.c:199:45: warning: dereference of noderef expression
>
> Aaron used rcu_dereference_sched() in order to fix that.
>
> How should this be fixed if using rcu_dereference_sched() is not correct ?

IMHO, sparse complains that _rcu pointer is not accessed using RCU
API.

rcu_dereference_sched() makes sparse happy. But lockdep complains
because the _rcu pointer is not accessed under:

rcu_read_lock_sched();
rcu_read_unlock_sched();

This is not the case here. Note that module_mutex does not
disable preemtion.

Now, the code is safe. The RCU access makes sure that "mod"
can't be freed in the meantime:

+ add_kallsyms() is called by the module loaded when the module
is being loaded. It could not get removed in parallel
by definition.

+ module_kallsyms_on_each_symbol() takes module_mutex.
It means that the module could not get removed.


IMHO, we have two possibilities here:

+ Make sparse and lockdep happy by using rcu_dereference_sched()
and calling the code under rcu_read_lock_sched().

+ Cast (struct mod_kallsyms *)mod->kallsyms when accessing
the value.

I do not have strong preference. I am fine with both.

Anyway, such a fix should be done in a separate patch!

Best Regards,
Petr