Re: [PATCH v2 7/9] sched: define TIF_ALLOW_RESCHED

From: Ankur Arora
Date: Sat Sep 09 2023 - 23:49:50 EST



Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> writes:

> On Sat, 9 Sept 2023 at 13:16, Ankur Arora <ankur.a.arora@xxxxxxxxxx> wrote:
>>
>> > + if (WARN_ON(resched_allowed()))
>> > + return;
>>
>> And, maybe something like this to guard against __this_cpu_read()
>> etc:
>>
>> +++ b/lib/smp_processor_id.c
>> @@ -13,6 +13,9 @@ unsigned int check_preemption_disabled(const char *what1, const char *what2)
>> {
>> int this_cpu = raw_smp_processor_id();
>>
>> + if (unlikely(resched_allowed()))
>> + goto out_error;
>
> Again, both of those checks are WRONG.
>
> They'll error out even in exceptions / interrupts, when we have a
> preempt count already from the exception itself.
>
> So testing "resched_allowed()" that only tests the TIF_RESCHED_ALLOW
> bit is wrong, wrong, wrong.

Yeah, you are right.

I think we can keep these checks, but with this fixed definition of
resched_allowed(). This might be better:

--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2260,7 +2260,8 @@ static inline void disallow_resched(void)

static __always_inline bool resched_allowed(void)
{
- return unlikely(test_tsk_thread_flag(current, TIF_RESCHED_ALLOW));
+ return unlikely(!preempt_count() &&
+ test_tsk_thread_flag(current, TIF_RESCHED_ALLOW));
}

Ankur

> These situations aren't errors if we already had a preemption count
> for other reasons. Only trying to disable preemption when in process
> context (while TIF_RESCHED_ALLOW) is a problem. Your patch is missing
> the check for "are we in a process context" part.
>
> Linus