Re: [PATCH 8/8] membarrier: Rewrite sync_core_before_usermode() and improve documentation

Nicholas Piggin
2021-06-20T02:12:33Z

Excerpts from Andy Lutomirski's message of June 20, 2021 1:50 am:
>
>
> On Fri, Jun 18, 2021, at 11:02 PM, Nicholas Piggin wrote:
>> Excerpts from Mathieu Desnoyers's message of June 19, 2021 6:09 am:
>> > ----- On Jun 18, 2021, at 3:58 PM, Andy Lutomirski luto@kernel.org wrote:
>> >
>> >> On Fri, Jun 18, 2021, at 9:31 AM, Mathieu Desnoyers wrote:
>> >>> ----- On Jun 17, 2021, at 8:12 PM, Andy Lutomirski luto@kernel.org wrote:
>> >>>
>> >>> > On 6/17/21 7:47 AM, Mathieu Desnoyers wrote:
>> >>> >
>> >>> >> Please change back this #ifndef / #else / #endif within function for
>> >>> >>
>> >>> >> if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE)) {
>> >>> >> ...
>> >>> >> } else {
>> >>> >> ...
>> >>> >> }
>> >>> >>
>> >>> >> I don't think mixing up preprocessor and code logic makes it more readable.
>> >>> >
>> >>> > I agree, but I don't know how to make the result work well.
>> >>> > membarrier_sync_core_before_usermode() isn't defined in the !IS_ENABLED
>> >>> > case, so either I need to fake up a definition or use #ifdef.
>> >>> >
>> >>> > If I faked up a definition, I would want to assert, at build time, that
>> >>> > it isn't called. I don't think we can do:
>> >>> >
>> >>> > static void membarrier_sync_core_before_usermode()
>> >>> > {
>> >>> > BUILD_BUG_IF_REACHABLE();
>> >>> > }
>> >>>
>> >>> Let's look at the context here:
>> >>>
>> >>> static void ipi_sync_core(void *info)
>> >>> {
>> >>> [....]
>> >>> membarrier_sync_core_before_usermode()
>> >>> }
>> >>>
>> >>> ^ this can be within #ifdef / #endif
>> >>>
>> >>> static int membarrier_private_expedited(int flags, int cpu_id)
>> >>> [...]
>> >>> if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
>> >>> return -EINVAL;
>> >>> if (!(atomic_read(&mm->membarrier_state) &
>> >>> MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY))
>> >>> return -EPERM;
>> >>> ipi_func = ipi_sync_core;
>> >>>
>> >>> All we need to make the line above work is to define an empty ipi_sync_core
>> >>> function in the #else case after the ipi_sync_core() function definition.
>> >>>
>> >>> Or am I missing your point ?
>> >>
>> >> Maybe?
>> >>
>> >> My objection is that an empty ipi_sync_core is a lie — it doesn’t sync the core.
>> >> I would be fine with that if I could have the compiler statically verify that
>> >> it’s not called, but I’m uncomfortable having it there if the implementation is
>> >> actively incorrect.
>> >
>> > I see. Another approach would be to implement a "setter" function to populate
>> > "ipi_func". That setter function would return -EINVAL in its #ifndef CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE
>> > implementation.
>>
>> I still don't get the problem with my suggestion. Sure the
>> ipi is a "lie", but it doesn't get used. That's how a lot of
>> ifdef folding works out. E.g.,
>>
>> diff --git a/kernel/sched/membarrier.c b/kernel/sched/membarrier.c
>> index b5add64d9698..54cb32d064af 100644
>> --- a/kernel/sched/membarrier.c
>> +++ b/kernel/sched/membarrier.c
>> @@ -5,6 +5,15 @@
>> * membarrier system call
>> */
>> #include "sched.h"
>> +#ifdef CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE
>> +#include <asm/sync_core.h>
>> +#else
>> +static inline void membarrier_sync_core_before_usermode(void)
>> +{
>> + compiletime_assert(0, "architecture does not implement
>> membarrier_sync_core_before_usermode");
>> +}
>> +
>
> With the assert there, I’m fine with this. Let me see if the result builds.

It had better, because compiletime_assert already relies on a similar
level of code elimination to work.

I think it's fine to use for now, but it may not be quite the the
logically correct primitive if we want to be really clean, because a
valid compiletime_assert implementation should be able to fire even for
code that is never linked. We would want something like to be clean
IMO:

#ifdef CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE
#include <asm/sync_core.h>
#else
extern void membarrier_sync_core_before_usermode(void) __compiletime_error("architecture does not implement membarrier_sync_core_before_usermode");
#endif

However that does not have the ifdef for optimising compile so AFAIKS it
could break with a false positive in some cases.

Something like compiletime_assert_not_called("msg") that either compiles
to a noop or a __compiletime_error depending on __OPTIMIZE__ would be
the way to go IMO. I don't know if anything exists that fits, but it's
certainly not a unique thing in the kernel so I may not be looking hard
enough.

Thanks,
Nick