Re: [PATCH] landlock: Do not hold exec_update_lock across the TSYNC wait
From: Mickaël Salaün
Date: Mon Nov 23 2026 - 08:15:33 EST
On Sun, Nov 22, 2026 at 06:35:12PM +0100, Oleg Nesterov wrote:
> On 11/20, Mickaël Salaün wrote:
> >
> > @@ -492,6 +533,19 @@ int landlock_restrict_sibling_threads(const struct cred *old_cred,
> > size_t newly_discovered_threads;
> > bool found_more_threads;
> >
> > + /*
> > + * Serialize concurrent TSYNC operations to prevent deadlocks when
> > + * multiple threads call landlock_restrict_self() simultaneously. If
> > + * another operation got there first, gracefully yield by restarting the
> > + * syscall. This allows the current thread to process pending
> > + * task_works before retrying.
> > + *
> > + * sighand->siglock protects only the marker, not the wait below.
> > + */
> > + CLASS(current_tsync_marker, marker)();
> > + if (!marker)
> > + return restart_syscall();
> > +
> > atomic_set(&shared_ctx.preparation_error, 0);
> > init_completion(&shared_ctx.all_prepared);
> > init_completion(&shared_ctx.ready_to_commit);
> > @@ -503,16 +557,6 @@ int landlock_restrict_sibling_threads(const struct cred *old_cred,
> > (restrict_flags & LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS) ||
> > task_no_new_privs(current);
> >
> > - /*
> > - * Serialize concurrent TSYNC operations to prevent deadlocks when
> > - * multiple threads call landlock_restrict_self() simultaneously.
> > - * If the lock is already held, we gracefully yield by restarting the
> > - * syscall. This allows the current thread to process pending
> > - * task_works before retrying.
> > - */
> > - if (!down_write_trylock(¤t->signal->exec_update_lock))
> > - return restart_syscall();
>
> Just in case, the patch looks obviously correct, so feel free to add
>
> Reviewed-by: Oleg Nesterov <oleg@xxxxxxxxxx>
Thanks for the review.
>
> -------------------------------------------------------------------------------
> But when I look at this code...
>
> Well, if we change landlock_restrict_sibling_threads() to rely on ->siglock,
> it seems that it can be greatly simplified? I mean, it can roughly do:
>
> ...
>
> for (nt nr_threads = 0;; tsync_works_grow_by(&works, nr_threads))
> {
> guard(spinlock_irq)(¤t->sighand->siglock);
>
> if (!nr_threads) {
> if (signal->flags & SIGNAL_LANDLOCK_TSYNC || ...)
> return restart_syscall();
> else
> signal->flags |= SIGNAL_LANDLOCK_TSYNC;
> }
>
> if (nr_threads < get_nr_threads(current)) {
> nr_threads = get_nr_threads(current));
> continue;
> }
>
> for_other_threads(current, thread) {
> if (thread->flags & PF_EXITING) // stable under siglock
> continue;
>
> ctx = tsync_works_provide(...); // can't fail
>
> ...
>
> task_work_add(thread, &ctx->work, TWA_SIGNAL); // can't fail
> }
>
> break;
> }
>
> complete_all(&shared_ctx.ready_to_commit);
> ...
>
> We can kill tsync_works_trim/tsync_works_contains_task/count_additional_threads/etc.
That would indeed be simpler, but a single pass would misses threads
that a sibling is creating at the same time: the cred is copied before
taking siglock and only fatal_signal_pending is checked.
Keeping the rescan loop and walking under siglock would be correct and
would drop tsync_works_trim() and count_additional_threads(). However,
it would hold siglock with IRQ disabled across a task_work_add() for
every thread, which /proc/<pid>/stat readers would then wait for. That
is bounded and not worse than what SIGCONT already does, but the RCU
walk avoids it entirely, so I'm wondering if it's worth it.
>
> Sure, this needs another change even if I am right.
Yes, I'll send a follow-up if needed.
>
> Oleg.
>
>