2023-09-24 08:40:18

by Matthew Wilcox

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

On Sun, Sep 24, 2023 at 12:50:43AM +0200, Thomas Gleixner wrote:
> cond_resched() cannot nest and is obviously scope-less.
>
> The TIF_ALLOW_RESCHED mechanism, which sparked this discussion only
> pretends to be scoped.
>
> As Peter pointed out it does not properly nest with other mechanisms and
> it cannot even nest in itself because it is boolean.

We can nest a single bit without turning it into a counter -- we
do this for memalloc_nofs_save() for example. Simply return the
current value of the bit, and pass it to _restore().

eg xfs_prepare_ioend():

/*
* We can allocate memory here while doing writeback on behalf of
* memory reclaim. To avoid memory allocation deadlocks set the
* task-wide nofs context for the following operations.
*/
nofs_flag = memalloc_nofs_save();

/* Convert CoW extents to regular */
if (!status && (ioend->io_flags & IOMAP_F_SHARED)) {
status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode),
ioend->io_offset, ioend->io_size);
}

memalloc_nofs_restore(nofs_flag);

I like your other approach better, but just in case anybody starts
worrying about turning a bit into a counter, there's no need to do
that.


2023-09-24 11:02:11

by Thomas Gleixner

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

On Sun, Sep 24 2023 at 08:19, Matthew Wilcox wrote:
> On Sun, Sep 24, 2023 at 12:50:43AM +0200, Thomas Gleixner wrote:
>> cond_resched() cannot nest and is obviously scope-less.
>>
>> The TIF_ALLOW_RESCHED mechanism, which sparked this discussion only
>> pretends to be scoped.
>>
>> As Peter pointed out it does not properly nest with other mechanisms and
>> it cannot even nest in itself because it is boolean.
>
> We can nest a single bit without turning it into a counter -- we
> do this for memalloc_nofs_save() for example. Simply return the
> current value of the bit, and pass it to _restore().

Right.

That works, but the reverse logic still does not make sense:

allow_resched();
....
spin_lock();

while
resched_now_is_suboptimal();
...
spin_lock();

works.

Thanks,

tglx

2023-09-24 15:36:50

by Matthew Wilcox

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

On Sun, Sep 24, 2023 at 09:55:52AM +0200, Thomas Gleixner wrote:
> On Sun, Sep 24 2023 at 08:19, Matthew Wilcox wrote:
> > On Sun, Sep 24, 2023 at 12:50:43AM +0200, Thomas Gleixner wrote:
> >> cond_resched() cannot nest and is obviously scope-less.
> >>
> >> The TIF_ALLOW_RESCHED mechanism, which sparked this discussion only
> >> pretends to be scoped.
> >>
> >> As Peter pointed out it does not properly nest with other mechanisms and
> >> it cannot even nest in itself because it is boolean.
> >
> > We can nest a single bit without turning it into a counter -- we
> > do this for memalloc_nofs_save() for example. Simply return the
> > current value of the bit, and pass it to _restore().
>
> Right.
>
> That works, but the reverse logic still does not make sense:
>
> allow_resched();
> ....
> spin_lock();
>
> while
> resched_now_is_suboptimal();
> ...
> spin_lock();
>
> works.

Oh, indeed. I had in mind

state = resched_now_is_suboptimal();
spin_lock();
...
spin_unlock();
resched_might_be_optimal_again(state);

... or we could bundle it up ...

state = spin_lock_resched_disable();
...
spin_unlock_resched_restore(state);