Cinolt Yuklair Cinolt Yuklair - 2 months ago 15
Linux Question

pthread mutexattr process-shared memory leak

By setting the

process-shared
attribute of a mutex (with pthread_mutexattr_setpshared), it allows a mutex to exist beyond the lifetime of the process that created it, according to the manpage for pthread_mutexattr_init:


In particular, these processes may exist beyond the lifetime of the initializing process.


So, if I create a mutex inside an
mmap(2)
d file that multiple processes can access, then I
unlink(2)
the file, will that cause a kernel-persistent memory leak? If so, where exactly does the data for the zombie mutex reside from an implementation viewpoint?

Answer

Cinolt, why do you think that linux.die.net published linux man pages? You have https://linux.die.net/man/3/pthread_mutexattr_init link, but it is hard to find where linux.die.net did find the text; author and date are not listed; and there is note on page:

Prolog

This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.

There is man7.org by Michael Kerrisk as home page of linux man-pages project: https://www.kernel.org/doc/man-pages/ and here is man of pthread_mutexattr_init http://man7.org/linux/man-pages/man3/pthread_mutexattr_init.3p.html. It is very short and have no information about leak.

In POSIX some implementation may use some in-kernel structures to save mutexattr properties; for such implementations to not have memory leak, POSIX requires programmer to destroy all created attrs.

There is no leak (existing beyond process lifetime) warnings on http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_mutexattr_init.html - Single UNIX ® Specification, Version 2 - Derived from the POSIX Threads Extension (1003.1c-1995)

But it is here in http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutexattr_destroy.html (The Open Group Base Specifications Issue 6 - IEEE Std 1003.1, 2004 Edition); with example just like one on die.net page:

Synchronization variables that are initialized with the PTHREAD_PROCESS_SHARED process-shared attribute may be operated on by any thread in any process that has access to it. In particular, these processes may exist beyond the lifetime of the initializing process. For example, the following code implements a simple counting semaphore in a mapped file that may be used by many processes.

In actual implementation in the GNU glibc, usually present in Linux variations (NPTL - https://en.wikipedia.org/wiki/Native_POSIX_Thread_Library) there is no additional in-kernel structure allocation:

http://code.metager.de/source/xref/gnu/glibc/nptl/pthread_mutexattr_init.c

24 __pthread_mutexattr_init (pthread_mutexattr_t *attr)
26   if (sizeof (struct pthread_mutexattr) != sizeof (pthread_mutexattr_t))
27     memset (attr, '\0', sizeof (*attr));
28
32   ((struct pthread_mutexattr *) attr)->mutexkind = PTHREAD_MUTEX_NORMAL;

And destroy is just NOP: http://code.metager.de/source/xref/gnu/glibc/nptl/pthread_mutexattr_destroy.c

22 int
23 __pthread_mutexattr_destroy (pthread_mutexattr_t *attr)
24 {
25   return 0;
26 }