Re: [patch 04/11] posix-timers: Remove pointless unlock_timer() wrapper

From: Peter Zijlstra
Date: Mon Feb 24 2025 - 16:56:07 EST


On Mon, Feb 24, 2025 at 07:43:26PM +0100, Thomas Gleixner wrote:
> On Mon, Feb 24 2025 at 17:21, Peter Zijlstra wrote:
> > On Mon, Feb 24, 2025 at 11:15:28AM +0100, Thomas Gleixner wrote:
> >> It's just a wrapper around spin_unlock_irqrestore() with zero value.
> >
> > Well, I disagree... the value is that is matches lock_timer(). Both in
> > naming and in argument types.
>
> Sure, but it's not used consistently as we have places where
> lock_timer() is not involved.
>
> > @@ -327,14 +350,13 @@ bool posixtimer_deliver_signal(struct ke
> > * Release siglock to ensure proper locking order versus
> > * timr::it_lock. Keep interrupts disabled.
> > */
> > - spin_unlock(&current->sighand->siglock);
> > + guard(spinlock)(&current->sighand->siglock);
>
> How is that equivalent?

I R idiot :-)

> So the resulting code is:
>
> scoped_guard (lock_timer, timer_id) {
> struct k_itimer *timr = __guard_ptr(lock_timer)(&scope);
> const struct k_clock *kc;
>
> memset(setting, 0, sizeof(*setting));
> kc = timr->kclock;
> if (WARN_ON_ONCE(!kc || !kc->timer_get))
> return -EINVAL;
>
> return 0;
> }
> return -EINVAL;
>
> I had to go and stare at the guard/class muck 10 times to convince
> myself, that this actually works. This really wants to be express the
> condition of the scoped_guard() somehow, e.g. scoped_cond_guard() or
> such.

Right, so the alternative form is something like:

scoped_cond_guard (lock_timer, return -EINVAL, timer_id) {
struct k_itimer *timr = __guard_ptr(lock_timer)(&scope);
const struct k_clock *kc;

memset(setting, 0, sizeof(*setting));
kc = timr->kclock;
if (WARN_ON_ONCE(!kc || !kc->timer_get))
return -EINVAL;
}
return 0;

Is that really so much better?

> > /* Delete a POSIX.1b interval timer. */
> > SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
> > {
> > - return posix_timer_delete(NULL, timer_id);
> > + scoped_guard (lock_timer, timer_id) {
> > + posix_timer_invalidate(scope.lock, scope.flags);
> > + scoped_guard_end(lock_timer);
> > + posix_timer_unhash_and_free(scope.lock);
>
> Not sure whether it's a good idea to free the scope.lock and not
> scope.timer :)

There is no scope.timer, the way this work is that the main pointer is
.lock, per the __DEFINE_UNLOCK_GUARD() helper.

I said there were rough edges :-/

Anyway, should I continue poking at this to see if I can clean it up /
extract more useful helpers.

Or shall I just let it be.