[v2,2/4] Consolidate stdio-lock.h

Message ID 20220426191523.833171-3-adhemerval.zanella@linaro.org
State Superseded
Headers
Series Move libio lock single-thread optimization to generic libc-lock |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Adhemerval Zanella Netto April 26, 2022, 7:15 p.m. UTC
  The NPTL replicate the sysdeps/nptl/libc-lock.h recursive lock.  Use
the Hurd one as generic one, which already uses the __libc_lock
functions.

The libc_malloc_debug is the only module that uses the
__libc_lock_init_recursive directly in the code and it is kept to
use the internal lock definitions.

Checked on x86_64-linux-gnu and i686-linux-gnu.
---
 sysdeps/generic/stdio-lock.h |  21 +++-----
 sysdeps/htl/stdio-lock.h     |  57 --------------------
 sysdeps/nptl/libc-lock.h     |  20 +++----
 sysdeps/nptl/stdio-lock.h    | 101 -----------------------------------
 4 files changed, 15 insertions(+), 184 deletions(-)
 delete mode 100644 sysdeps/htl/stdio-lock.h
 delete mode 100644 sysdeps/nptl/stdio-lock.h
  

Comments

Florian Weimer April 27, 2022, 1:25 p.m. UTC | #1
* Adhemerval Zanella via Libc-alpha:

> diff --git a/sysdeps/generic/stdio-lock.h b/sysdeps/generic/stdio-lock.h
> index 14cf458bdd..fd61f0b5b7 100644
> --- a/sysdeps/generic/stdio-lock.h
> +++ b/sysdeps/generic/stdio-lock.h
> @@ -45,20 +45,13 @@ __libc_lock_define_recursive (typedef, _IO_lock_t)
>  #define _IO_cleanup_region_end(_doit) \
>    __libc_cleanup_region_end (_doit)
>  
> -#if defined _LIBC && IS_IN (libc)
> -
> -# ifdef __EXCEPTIONS
> -# define _IO_acquire_lock(_fp) \
> +#define _IO_acquire_lock(_fp) \
>    do {									      \
> -    FILE *_IO_acquire_lock_file						      \
> -	__attribute__((cleanup (_IO_acquire_lock_fct)))			      \
> -	= (_fp);							      \
> -    _IO_flockfile (_IO_acquire_lock_file);
> -# else
> -#  define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
> -# endif
> -# define _IO_release_lock(_fp) ; } while (0)
> -
> -#endif
> +    _IO_cleanup_region_start((void (*) (void *)) &_IO_funlockfile, _fp);      \
> +    _IO_flockfile (_fp);
> +#define _IO_release_lock(_fp) \
> +    _IO_funlockfile (_fp);						      \
> +    _IO_cleanup_region_end (0);						      \
> +  } while (0)
>  
>  #endif /* stdio-lock.h */

I think this change replaces unwind tables for -fexceptions builds.  If
GCC can't turn the indirect call to the unlock function into a direct
call, this will result in a loss of hardening due to the additional
indirect function call.

This change may also lose C++ unwinding compatibility for some
fopencookie use cases, I think.

Thanks,
Florian
  
Adhemerval Zanella Netto April 27, 2022, 4:15 p.m. UTC | #2
On 27/04/2022 10:25, Florian Weimer wrote:
> * Adhemerval Zanella via Libc-alpha:
> 
>> diff --git a/sysdeps/generic/stdio-lock.h b/sysdeps/generic/stdio-lock.h
>> index 14cf458bdd..fd61f0b5b7 100644
>> --- a/sysdeps/generic/stdio-lock.h
>> +++ b/sysdeps/generic/stdio-lock.h
>> @@ -45,20 +45,13 @@ __libc_lock_define_recursive (typedef, _IO_lock_t)
>>  #define _IO_cleanup_region_end(_doit) \
>>    __libc_cleanup_region_end (_doit)
>>  
>> -#if defined _LIBC && IS_IN (libc)
>> -
>> -# ifdef __EXCEPTIONS
>> -# define _IO_acquire_lock(_fp) \
>> +#define _IO_acquire_lock(_fp) \
>>    do {									      \
>> -    FILE *_IO_acquire_lock_file						      \
>> -	__attribute__((cleanup (_IO_acquire_lock_fct)))			      \
>> -	= (_fp);							      \
>> -    _IO_flockfile (_IO_acquire_lock_file);
>> -# else
>> -#  define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
>> -# endif
>> -# define _IO_release_lock(_fp) ; } while (0)
>> -
>> -#endif
>> +    _IO_cleanup_region_start((void (*) (void *)) &_IO_funlockfile, _fp);      \
>> +    _IO_flockfile (_fp);
>> +#define _IO_release_lock(_fp) \
>> +    _IO_funlockfile (_fp);						      \
>> +    _IO_cleanup_region_end (0);						      \
>> +  } while (0)
>>  
>>  #endif /* stdio-lock.h */
> 
> I think this change replaces unwind tables for -fexceptions builds.  If
> GCC can't turn the indirect call to the unlock function into a direct
> call, this will result in a loss of hardening due to the additional
> indirect function call.
> 
> This change may also lose C++ unwinding compatibility for some
> fopencookie use cases, I think.

This is an internal header where if __EXCEPTIONS is not defined we will
get a compiler error because of an undefined symbol
(_IO_acquire_lock_needs_exceptions_enabled).  So internally all
_IO_acquire_lock usage already requires __EXCEPTIONS, so the fallback
is just unused definitions.
  
Florian Weimer April 27, 2022, 6 p.m. UTC | #3
* Adhemerval Zanella:

> On 27/04/2022 10:25, Florian Weimer wrote:
>> * Adhemerval Zanella via Libc-alpha:
>> 
>>> diff --git a/sysdeps/generic/stdio-lock.h b/sysdeps/generic/stdio-lock.h
>>> index 14cf458bdd..fd61f0b5b7 100644
>>> --- a/sysdeps/generic/stdio-lock.h
>>> +++ b/sysdeps/generic/stdio-lock.h
>>> @@ -45,20 +45,13 @@ __libc_lock_define_recursive (typedef, _IO_lock_t)
>>>  #define _IO_cleanup_region_end(_doit) \
>>>    __libc_cleanup_region_end (_doit)
>>>  
>>> -#if defined _LIBC && IS_IN (libc)
>>> -
>>> -# ifdef __EXCEPTIONS
>>> -# define _IO_acquire_lock(_fp) \
>>> +#define _IO_acquire_lock(_fp) \
>>>    do {									      \
>>> -    FILE *_IO_acquire_lock_file						      \
>>> -	__attribute__((cleanup (_IO_acquire_lock_fct)))			      \
>>> -	= (_fp);							      \
>>> -    _IO_flockfile (_IO_acquire_lock_file);
>>> -# else
>>> -#  define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
>>> -# endif
>>> -# define _IO_release_lock(_fp) ; } while (0)
>>> -
>>> -#endif
>>> +    _IO_cleanup_region_start((void (*) (void *)) &_IO_funlockfile, _fp);      \
>>> +    _IO_flockfile (_fp);
>>> +#define _IO_release_lock(_fp) \
>>> +    _IO_funlockfile (_fp);						      \
>>> +    _IO_cleanup_region_end (0);						      \
>>> +  } while (0)
>>>  
>>>  #endif /* stdio-lock.h */
>> 
>> I think this change replaces unwind tables for -fexceptions builds.  If
>> GCC can't turn the indirect call to the unlock function into a direct
>> call, this will result in a loss of hardening due to the additional
>> indirect function call.
>> 
>> This change may also lose C++ unwinding compatibility for some
>> fopencookie use cases, I think.
>
> This is an internal header where if __EXCEPTIONS is not defined we will
> get a compiler error because of an undefined symbol
> (_IO_acquire_lock_needs_exceptions_enabled).  So internally all
> _IO_acquire_lock usage already requires __EXCEPTIONS, so the fallback
> is just unused definitions.

I see this code generation change in libio/fputc.os.  The new code uses
an on-stack pointer saved at the start of the cleanup region:

+  90:  48 8b 05 00 00 00 00    mov    0x0(%rip),%rax        # 97 <fputc+0x97>
+                       93: R_X86_64_REX_GOTPCRELX      _IO_funlockfile-0x4
+  97:  48 89 e7                mov    %rsp,%rdi
+  9a:  48 89 04 24             mov    %rax,(%rsp)
+  9e:  e8 00 00 00 00          call   a3 <fputc+0xa3>
+                       9f: R_X86_64_PLT32      __GI___libc_cleanup_push_defer-0x4
+  a3:  8b 45 00                mov    0x0(%rbp),%eax
+  a6:  25 00 80 00 00          and    $0x8000,%eax
+  ab:  0f 85 d1 00 00 00       jne    182 <fputc+0x182>
+  b1:  64 4c 8b 2c 25 10 00    mov    %fs:0x10,%r13
+  b8:  00 00 
+  ba:  48 8b bd 88 00 00 00    mov    0x88(%rbp),%rdi
+  c1:  4c 39 6f 08             cmp    %r13,0x8(%rdi)
+  c5:  74 1a                   je     e1 <fputc+0xe1>
+  c7:  ba 01 00 00 00          mov    $0x1,%edx
+  cc:  f0 0f b1 17             lock cmpxchg %edx,(%rdi)
+  d0:  0f 85 a2 00 00 00       jne    178 <fputc+0x178>
+  d6:  48 8b bd 88 00 00 00    mov    0x88(%rbp),%rdi
+  dd:  4c 89 6f 08             mov    %r13,0x8(%rdi)
+  e1:  83 47 04 01             addl   $0x1,0x4(%rdi)
+  e5:  41 bd 01 00 00 00       mov    $0x1,%r13d
+  eb:  e9 3d ff ff ff          jmp    2d <fputc+0x2d>
+  f0:  48 89 e7                mov    %rsp,%rdi
+  f3:  e8 00 00 00 00          call   f8 <fputc+0xf8>
+                       f4: R_X86_64_PLT32      __GI___libc_cleanup_pop_restore-0x4

This is a from a build with CFLAGS="-O2 -fexceptions -s -DNDEBUG"
(for comparison purposes).

The old code just inlined the _IO_funlockfile fast path.

(We seem to lack libc_hidden_proto/libc_hidden_def for _IO_funlockfile.)

Thanks,
Florian
  
Adhemerval Zanella Netto April 27, 2022, 6:35 p.m. UTC | #4
On 27/04/2022 15:00, Florian Weimer wrote:
> * Adhemerval Zanella:
> 
>> On 27/04/2022 10:25, Florian Weimer wrote:
>>> * Adhemerval Zanella via Libc-alpha:
>>>
>>>> diff --git a/sysdeps/generic/stdio-lock.h b/sysdeps/generic/stdio-lock.h
>>>> index 14cf458bdd..fd61f0b5b7 100644
>>>> --- a/sysdeps/generic/stdio-lock.h
>>>> +++ b/sysdeps/generic/stdio-lock.h
>>>> @@ -45,20 +45,13 @@ __libc_lock_define_recursive (typedef, _IO_lock_t)
>>>>  #define _IO_cleanup_region_end(_doit) \
>>>>    __libc_cleanup_region_end (_doit)
>>>>  
>>>> -#if defined _LIBC && IS_IN (libc)
>>>> -
>>>> -# ifdef __EXCEPTIONS
>>>> -# define _IO_acquire_lock(_fp) \
>>>> +#define _IO_acquire_lock(_fp) \
>>>>    do {									      \
>>>> -    FILE *_IO_acquire_lock_file						      \
>>>> -	__attribute__((cleanup (_IO_acquire_lock_fct)))			      \
>>>> -	= (_fp);							      \
>>>> -    _IO_flockfile (_IO_acquire_lock_file);
>>>> -# else
>>>> -#  define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
>>>> -# endif
>>>> -# define _IO_release_lock(_fp) ; } while (0)
>>>> -
>>>> -#endif
>>>> +    _IO_cleanup_region_start((void (*) (void *)) &_IO_funlockfile, _fp);      \
>>>> +    _IO_flockfile (_fp);
>>>> +#define _IO_release_lock(_fp) \
>>>> +    _IO_funlockfile (_fp);						      \
>>>> +    _IO_cleanup_region_end (0);						      \
>>>> +  } while (0)
>>>>  
>>>>  #endif /* stdio-lock.h */
>>>
>>> I think this change replaces unwind tables for -fexceptions builds.  If
>>> GCC can't turn the indirect call to the unlock function into a direct
>>> call, this will result in a loss of hardening due to the additional
>>> indirect function call.
>>>
>>> This change may also lose C++ unwinding compatibility for some
>>> fopencookie use cases, I think.
>>
>> This is an internal header where if __EXCEPTIONS is not defined we will
>> get a compiler error because of an undefined symbol
>> (_IO_acquire_lock_needs_exceptions_enabled).  So internally all
>> _IO_acquire_lock usage already requires __EXCEPTIONS, so the fallback
>> is just unused definitions.
> 
> I see this code generation change in libio/fputc.os.  The new code uses
> an on-stack pointer saved at the start of the cleanup region:
> 
> +  90:  48 8b 05 00 00 00 00    mov    0x0(%rip),%rax        # 97 <fputc+0x97>
> +                       93: R_X86_64_REX_GOTPCRELX      _IO_funlockfile-0x4
> +  97:  48 89 e7                mov    %rsp,%rdi
> +  9a:  48 89 04 24             mov    %rax,(%rsp)
> +  9e:  e8 00 00 00 00          call   a3 <fputc+0xa3>
> +                       9f: R_X86_64_PLT32      __GI___libc_cleanup_push_defer-0x4
> +  a3:  8b 45 00                mov    0x0(%rbp),%eax
> +  a6:  25 00 80 00 00          and    $0x8000,%eax
> +  ab:  0f 85 d1 00 00 00       jne    182 <fputc+0x182>
> +  b1:  64 4c 8b 2c 25 10 00    mov    %fs:0x10,%r13
> +  b8:  00 00 
> +  ba:  48 8b bd 88 00 00 00    mov    0x88(%rbp),%rdi
> +  c1:  4c 39 6f 08             cmp    %r13,0x8(%rdi)
> +  c5:  74 1a                   je     e1 <fputc+0xe1>
> +  c7:  ba 01 00 00 00          mov    $0x1,%edx
> +  cc:  f0 0f b1 17             lock cmpxchg %edx,(%rdi)
> +  d0:  0f 85 a2 00 00 00       jne    178 <fputc+0x178>
> +  d6:  48 8b bd 88 00 00 00    mov    0x88(%rbp),%rdi
> +  dd:  4c 89 6f 08             mov    %r13,0x8(%rdi)
> +  e1:  83 47 04 01             addl   $0x1,0x4(%rdi)
> +  e5:  41 bd 01 00 00 00       mov    $0x1,%r13d
> +  eb:  e9 3d ff ff ff          jmp    2d <fputc+0x2d>
> +  f0:  48 89 e7                mov    %rsp,%rdi
> +  f3:  e8 00 00 00 00          call   f8 <fputc+0xf8>
> +                       f4: R_X86_64_PLT32      __GI___libc_cleanup_pop_restore-0x4
> 
> This is a from a build with CFLAGS="-O2 -fexceptions -s -DNDEBUG"
> (for comparison purposes).
> 
> The old code just inlined the _IO_funlockfile fast path.
> 
> (We seem to lack libc_hidden_proto/libc_hidden_def for _IO_funlockfile.)

You are right.  The change now uses __libc_cleanup_region_start from libc-lock.h
instead of the cleanup version.  I am not sure about hardening, but afaiu 
__libc_cleanup_push_defer should handle C++ unwinding for fopencookie.

It seems that the Hurd version is indeed to the best option, so I think for
generic it would be better to just use:

diff --git a/sysdeps/generic/stdio-lock.h b/sysdeps/generic/stdio-lock.h
index 14cf458bdd..a42131f5a5 100644
--- a/sysdeps/generic/stdio-lock.h
+++ b/sysdeps/generic/stdio-lock.h
@@ -45,20 +45,12 @@ __libc_lock_define_recursive (typedef, _IO_lock_t)
 #define _IO_cleanup_region_end(_doit) \
   __libc_cleanup_region_end (_doit)

-#if defined _LIBC && IS_IN (libc)
-
-# ifdef __EXCEPTIONS
-# define _IO_acquire_lock(_fp) \
+#define _IO_acquire_lock(_fp) \
   do {                                                                       \
     FILE *_IO_acquire_lock_file                                                      \
        __attribute__((cleanup (_IO_acquire_lock_fct)))                       \
        = (_fp);                                                              \
     _IO_flockfile (_IO_acquire_lock_file);
-# else
-#  define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
-# endif
-# define _IO_release_lock(_fp) ; } while (0)
-
-#endif
+#define _IO_release_lock(_fp) ; } while (0)

 #endif /* stdio-lock.h */
  
Florian Weimer April 27, 2022, 6:44 p.m. UTC | #5
* Adhemerval Zanella:

> You are right.  The change now uses __libc_cleanup_region_start from
> libc-lock.h instead of the cleanup version.  I am not sure about
> hardening, but afaiu __libc_cleanup_push_defer should handle C++
> unwinding for fopencookie.

No, C++ unwinding will right go through these frames.  We only
interleave in the other direction (pthread_cancel unwinding running C++
destructors).

> It seems that the Hurd version is indeed to the best option, so I think for
> generic it would be better to just use:
>
> diff --git a/sysdeps/generic/stdio-lock.h b/sysdeps/generic/stdio-lock.h
> index 14cf458bdd..a42131f5a5 100644
> --- a/sysdeps/generic/stdio-lock.h
> +++ b/sysdeps/generic/stdio-lock.h
> @@ -45,20 +45,12 @@ __libc_lock_define_recursive (typedef, _IO_lock_t)
>  #define _IO_cleanup_region_end(_doit) \
>    __libc_cleanup_region_end (_doit)
>
> -#if defined _LIBC && IS_IN (libc)
> -
> -# ifdef __EXCEPTIONS
> -# define _IO_acquire_lock(_fp) \
> +#define _IO_acquire_lock(_fp) \
>    do {                                                                       \
>      FILE *_IO_acquire_lock_file                                                      \
>         __attribute__((cleanup (_IO_acquire_lock_fct)))                       \
>         = (_fp);                                                              \
>      _IO_flockfile (_IO_acquire_lock_file);
> -# else
> -#  define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
> -# endif
> -# define _IO_release_lock(_fp) ; } while (0)
> -
> -#endif
> +#define _IO_release_lock(_fp) ; } while (0)
>
>  #endif /* stdio-lock.h */

I suppose so.

Thanks,
Florian
  
Adhemerval Zanella Netto April 27, 2022, 6:49 p.m. UTC | #6
On 27/04/2022 15:44, Florian Weimer wrote:
> * Adhemerval Zanella:
> 
>> You are right.  The change now uses __libc_cleanup_region_start from
>> libc-lock.h instead of the cleanup version.  I am not sure about
>> hardening, but afaiu __libc_cleanup_push_defer should handle C++
>> unwinding for fopencookie.
> 
> No, C++ unwinding will right go through these frames.  We only
> interleave in the other direction (pthread_cancel unwinding running C++
> destructors).

Right, so Hurd version is indeed broken.

> 
>> It seems that the Hurd version is indeed to the best option, so I think for
>> generic it would be better to just use:
>>
>> diff --git a/sysdeps/generic/stdio-lock.h b/sysdeps/generic/stdio-lock.h
>> index 14cf458bdd..a42131f5a5 100644
>> --- a/sysdeps/generic/stdio-lock.h
>> +++ b/sysdeps/generic/stdio-lock.h
>> @@ -45,20 +45,12 @@ __libc_lock_define_recursive (typedef, _IO_lock_t)
>>  #define _IO_cleanup_region_end(_doit) \
>>    __libc_cleanup_region_end (_doit)
>>
>> -#if defined _LIBC && IS_IN (libc)
>> -
>> -# ifdef __EXCEPTIONS
>> -# define _IO_acquire_lock(_fp) \
>> +#define _IO_acquire_lock(_fp) \
>>    do {                                                                       \
>>      FILE *_IO_acquire_lock_file                                                      \
>>         __attribute__((cleanup (_IO_acquire_lock_fct)))                       \
>>         = (_fp);                                                              \
>>      _IO_flockfile (_IO_acquire_lock_file);
>> -# else
>> -#  define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
>> -# endif
>> -# define _IO_release_lock(_fp) ; } while (0)
>> -
>> -#endif
>> +#define _IO_release_lock(_fp) ; } while (0)
>>
>>  #endif /* stdio-lock.h */
> 
> I suppose so.
> 

I will update the patch.
  

Patch

diff --git a/sysdeps/generic/stdio-lock.h b/sysdeps/generic/stdio-lock.h
index 14cf458bdd..fd61f0b5b7 100644
--- a/sysdeps/generic/stdio-lock.h
+++ b/sysdeps/generic/stdio-lock.h
@@ -45,20 +45,13 @@  __libc_lock_define_recursive (typedef, _IO_lock_t)
 #define _IO_cleanup_region_end(_doit) \
   __libc_cleanup_region_end (_doit)
 
-#if defined _LIBC && IS_IN (libc)
-
-# ifdef __EXCEPTIONS
-# define _IO_acquire_lock(_fp) \
+#define _IO_acquire_lock(_fp) \
   do {									      \
-    FILE *_IO_acquire_lock_file						      \
-	__attribute__((cleanup (_IO_acquire_lock_fct)))			      \
-	= (_fp);							      \
-    _IO_flockfile (_IO_acquire_lock_file);
-# else
-#  define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
-# endif
-# define _IO_release_lock(_fp) ; } while (0)
-
-#endif
+    _IO_cleanup_region_start((void (*) (void *)) &_IO_funlockfile, _fp);      \
+    _IO_flockfile (_fp);
+#define _IO_release_lock(_fp) \
+    _IO_funlockfile (_fp);						      \
+    _IO_cleanup_region_end (0);						      \
+  } while (0)
 
 #endif /* stdio-lock.h */
diff --git a/sysdeps/htl/stdio-lock.h b/sysdeps/htl/stdio-lock.h
deleted file mode 100644
index c0a03c5a13..0000000000
--- a/sysdeps/htl/stdio-lock.h
+++ /dev/null
@@ -1,57 +0,0 @@ 
-/* Thread package specific definitions of stream lock type.  Hurd version.
-   Copyright (C) 2000-2022 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <https://www.gnu.org/licenses/>.  */
-
-#ifndef _STDIO_LOCK_H
-#define _STDIO_LOCK_H 1
-
-#include <libc-lock.h>
-
-__libc_lock_define_recursive (typedef, _IO_lock_t)
-#define _IO_lock_t_defined 1
-
-/* We need recursive (counting) mutexes.  */
-# define _IO_lock_initializer _LIBC_LOCK_RECURSIVE_INITIALIZER
-
-#define _IO_lock_init(_name)	__libc_lock_init_recursive (_name)
-#define _IO_lock_fini(_name)	__libc_lock_fini_recursive (_name)
-#define _IO_lock_lock(_name)	__libc_lock_lock_recursive (_name)
-#define _IO_lock_trylock(_name)	__libc_lock_trylock_recursive (_name)
-#define _IO_lock_unlock(_name)	__libc_lock_unlock_recursive (_name)
-
-
-#define _IO_cleanup_region_start(_fct, _fp) \
-  __libc_cleanup_region_start (((_fp)->_flags & _IO_USER_LOCK) == 0, _fct, _fp)
-#define _IO_cleanup_region_start_noarg(_fct) \
-  __libc_cleanup_region_start (1, _fct, NULL)
-#define _IO_cleanup_region_end(_doit) \
-  __libc_cleanup_region_end (_doit)
-
-#if defined _LIBC && IS_IN (libc)
-
-# define _IO_acquire_lock(_fp) \
-  do {									      \
-    _IO_cleanup_region_start((void (*) (void *)) &_IO_funlockfile, _fp);      \
-    _IO_flockfile (_fp);
-# define _IO_release_lock(_fp) \
-    _IO_funlockfile (_fp);						      \
-    _IO_cleanup_region_end (0);						      \
-  } while (0)
-
-#endif
-
-#endif /* stdio-lock.h */
diff --git a/sysdeps/nptl/libc-lock.h b/sysdeps/nptl/libc-lock.h
index c186375c31..6c2d6acfd1 100644
--- a/sysdeps/nptl/libc-lock.h
+++ b/sysdeps/nptl/libc-lock.h
@@ -25,14 +25,10 @@ 
 
 
 /* Mutex type.  */
-#if defined _LIBC
-# if (!IS_IN (libc) && !IS_IN (libpthread)) || !defined _LIBC
+#if !IS_IN (libc) && !IS_IN (libc_malloc_debug)
 typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t;
-# else
-typedef struct { int lock; int cnt; void *owner; } __libc_lock_recursive_t;
-# endif
 #else
-typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
+typedef struct { int lock; int cnt; void *owner; } __libc_lock_recursive_t;
 #endif
 
 /* Define a lock variable NAME with storage class CLASS.  The lock must be
@@ -47,7 +43,7 @@  typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
 
 /* Define an initialized recursive lock variable NAME with storage
    class CLASS.  */
-#if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
+#if IS_IN (libc) || IS_IN (libc_malloc_debug)
 # define __libc_lock_define_initialized_recursive(CLASS, NAME) \
   CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
 # define _LIBC_LOCK_RECURSIVE_INITIALIZER \
@@ -60,7 +56,7 @@  typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
 #endif
 
 /* Initialize a recursive mutex.  */
-#if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
+#if IS_IN (libc) || IS_IN (libc_malloc_debug)
 # define __libc_lock_init_recursive(NAME) \
   ((void) ((NAME) = (__libc_lock_recursive_t) _LIBC_LOCK_RECURSIVE_INITIALIZER))
 #else
@@ -78,7 +74,7 @@  typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
 #endif
 
 /* Finalize recursive named lock.  */
-#if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
+#if IS_IN (libc) || IS_IN (libc_malloc_debug)
 # define __libc_lock_fini_recursive(NAME) ((void) 0)
 #else
 # define __libc_lock_fini_recursive(NAME) \
@@ -86,7 +82,7 @@  typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
 #endif
 
 /* Lock the recursive named lock variable.  */
-#if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
+#if IS_IN (libc) || IS_IN (libc_malloc_debug)
 # define __libc_lock_lock_recursive(NAME) \
   do {									      \
     void *self = THREAD_SELF;						      \
@@ -103,7 +99,7 @@  typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
 #endif
 
 /* Try to lock the recursive named lock variable.  */
-#if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
+#if IS_IN (libc) || IS_IN (libc_malloc_debug)
 # define __libc_lock_trylock_recursive(NAME) \
   ({									      \
     int result = 0;							      \
@@ -128,7 +124,7 @@  typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
 #endif
 
 /* Unlock the recursive named lock variable.  */
-#if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
+#if IS_IN (libc) || IS_IN (libc_malloc_debug)
 /* We do no error checking here.  */
 # define __libc_lock_unlock_recursive(NAME) \
   do {									      \
diff --git a/sysdeps/nptl/stdio-lock.h b/sysdeps/nptl/stdio-lock.h
deleted file mode 100644
index afa0b779c8..0000000000
--- a/sysdeps/nptl/stdio-lock.h
+++ /dev/null
@@ -1,101 +0,0 @@ 
-/* Thread package specific definitions of stream lock type.  NPTL version.
-   Copyright (C) 2000-2022 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <https://www.gnu.org/licenses/>.  */
-
-#ifndef _STDIO_LOCK_H
-#define _STDIO_LOCK_H 1
-
-#include <libc-lock.h>
-#include <lowlevellock.h>
-
-
-typedef struct { int lock; int cnt; void *owner; } _IO_lock_t;
-#define _IO_lock_t_defined 1
-
-#define _IO_lock_initializer { LLL_LOCK_INITIALIZER, 0, NULL }
-
-#define _IO_lock_init(_name) \
-  ((void) ((_name) = (_IO_lock_t) _IO_lock_initializer))
-
-#define _IO_lock_fini(_name) \
-  ((void) 0)
-
-#define _IO_lock_lock(_name) \
-  do {									      \
-    void *__self = THREAD_SELF;						      \
-    if ((_name).owner != __self)					      \
-      {									      \
-	lll_lock ((_name).lock, LLL_PRIVATE);				      \
-        (_name).owner = __self;						      \
-      }									      \
-    ++(_name).cnt;							      \
-  } while (0)
-
-#define _IO_lock_trylock(_name) \
-  ({									      \
-    int __result = 0;							      \
-    void *__self = THREAD_SELF;						      \
-    if ((_name).owner != __self)					      \
-      {									      \
-        if (lll_trylock ((_name).lock) == 0)				      \
-          {								      \
-            (_name).owner = __self;					      \
-            (_name).cnt = 1;						      \
-          }								      \
-        else								      \
-          __result = EBUSY;						      \
-      }									      \
-    else								      \
-      ++(_name).cnt;							      \
-    __result;								      \
-  })
-
-#define _IO_lock_unlock(_name) \
-  do {									      \
-    if (--(_name).cnt == 0)						      \
-      {									      \
-        (_name).owner = NULL;						      \
-	lll_unlock ((_name).lock, LLL_PRIVATE);				      \
-      }									      \
-  } while (0)
-
-
-
-#define _IO_cleanup_region_start(_fct, _fp) \
-  __libc_cleanup_region_start (((_fp)->_flags & _IO_USER_LOCK) == 0, _fct, _fp)
-#define _IO_cleanup_region_start_noarg(_fct) \
-  __libc_cleanup_region_start (1, _fct, NULL)
-#define _IO_cleanup_region_end(_doit) \
-  __libc_cleanup_region_end (_doit)
-
-#if defined _LIBC && IS_IN (libc)
-
-# ifdef __EXCEPTIONS
-#  define _IO_acquire_lock(_fp) \
-  do {									      \
-    FILE *_IO_acquire_lock_file						      \
-	__attribute__((cleanup (_IO_acquire_lock_fct)))			      \
-	= (_fp);							      \
-    _IO_flockfile (_IO_acquire_lock_file);
-# else
-#  define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
-# endif
-# define _IO_release_lock(_fp) ; } while (0)
-
-#endif
-
-#endif /* stdio-lock.h */