[PATCH 3/5] Linux: Implement per-thread file system attributes

Florian Weimer fweimer@redhat.com
Wed Dec 10 10:25:41 GMT 2025


* Adhemerval Zanella Netto:

> On 09/12/25 14:31, Florian Weimer wrote:
>> This commit adds the functions pthread_attr_setperthreadfs_np and
>> pthread_attr_getperthreadfs_np.
>> 
>> The implementation is based on suppressing the CLONE_FS clone flag when
>> creating the new thread.  The new flag is sticky and is applied to
>> all threads created from a thread with the PTHREAD_PER_THREAD_NP
>> attribute.
>
> Should we add auxiliary functions to get the current per-thread file
> system attribute, similar to what pthread_setcancelstate does for
> the second argument?

It's possible to get this information using pthread_getattr_np as  far
as glibc concerned, but the result could be misleading because the
thread may have called unshare directly.

The kernel only allows us to check if two threads have the same FS, I
don't think it tells us on which thread the unshare has happened.

>>> +If a thread that has been created with the
>>> +@code{PTHREAD_PER_THREAD_NP} flag creates further threads, these
>>> +threads are implicitly created with the @code{PTHREAD_PER_THREAD_NP}
>>> +flag, ignoring the value of this thread creation attribute.
>
> Does it mean that if I create a thread with PTHREAD_PER_THREAD_NP, and
> later create a thread with PTHREAD_PER_PROCESS_NP from the newly
> created thread, the PTHREAD_PER_PROCESS_NP will be silent ignored? If
> so, this seems confusing.

It's not possible to get out from under this at the kernel level.  We
would have to reframe it in terms of inheritance (share with the current
thread, whatever its sharing properties are).

>>> +(If this behavior is not desirable, it is possible to call
>>> +@samp{unshare (CLONE_FS)} from the new thread instead of creating it
>>> +with the @code{PTHREAD_PER_THREAD_NP} flag.)
>
> Requering to call a different API to 'undo' the stick bit setup also
> seems confusing. Why this can't be done at thread creation before
> calling the user-provded pthread_cancel routine?

We would need a helper thread that gives us access to the original FS
and call clone from that.

Thanks,
Florian



More information about the Libc-alpha mailing list