nfsrahead: enable event-driven mountinfo monitoring and skip non-NFS devices
Subsystems (2)
PATCH 1/1 nfsrahead: enable event-driven mountinfo monitoring and skip non-NFS devices
The nfsrahead utility relies on parsing "/proc/self/mountinfo" to correlate a device number with a specific NFS mount point. However, due to the asynchronous nature of system initialisation, the relevant entry in mountinfo may not be immediately available when the tool is executed.
Currently, the utility employs a naive polling mechanism, retrying the search five times with a fixed 50ms delay (totalling 250ms). This approach proves brittle on systems under heavy load or during distinctively slow boot sequences.
To mitigate this race condition and improve robustness, update get_device_info() to utilise the libmount monitoring API.
The new implementation introduces the following logic:
1. Initialises a monitor on /proc/self/mountinfo using mnt_new_monitor().
2. Replaces the fixed polling loop with mnt_monitor_wait().
3. Increases the maximum wait time to 10 seconds (MNT_NM_TIMEOUT).
4. Introduces a fast-path rejection mechanism. NFS backing devices are allocated from the kernel's unnamed block device pool (major number 0). While some local multi-device filesystems (such as Btrfs) also utilise anonymous device numbers, physical hardware block devices (e.g., sda, nvme) always possess specific, non-zero major numbers. By instantly exiting with -ENODEV for any device string not beginning with "0:", we safely bypass the monitor for physical drives, preventing the exhaustion of udev worker threads. See set_anon_super() and get_anon_bdev().
5. Implements strict monotonic deadline tracking within the monitor loop to prevent indefinite blocking.
---
Hi Steve,
This patch should resolve the udev worker exhaustion issue reported by Yi. It applies cleanly on top of the current nfs-utils tree, after your revert [1].
Thank you.
[1]: https://lore.kernel.org/linux-nfs/[email protected]/
tools/nfsrahead/main.c | 55 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-)
tools/nfsrahead/main.c +54 -1
@@ -3,6 +3,7 @@#include <stdlib.h> #include <errno.h> #include <unistd.h> +#include <time.h> #include <libmount/libmount.h> #include <sys/sysmacros.h>
@@ -17,6 +18,8 @@#define CONF_NAME "nfsrahead"#define NFS_DEFAULT_READAHEAD 128 +#define MNT_NM_TIMEOUT 10000 +/* Device information from the system */ struct device_info {char *device_number;
@@ -117,7 +120,57 @@ out_free_device_info:static int get_device_info (const char *device_number,struct device_info *device_info) { -int ret =get_mountinfo (device_number, device_info,MOUNTINFO_PATH ); +int ret; +struct libmnt_monitor *mn =NULL ; +struct timespec start, now; +int remaining_ms =MNT_NM_TIMEOUT ; + +/* +* Fast-path rejection: +* NFS backing devices always use the anonymous block device major number (0). +* If the device number does not start with "0:", it is a physical block device +* and will never be an NFS mount. Exit immediately to prevent blocking udev. +*/ +if (strncmp (device_number,"0:" ,2 ) !=0 ) +return -ENODEV ; + + ret =get_mountinfo (device_number, device_info,MOUNTINFO_PATH ); +if (ret ==0 ) +return 0 ; + + mn =mnt_new_monitor (); +if (!mn) + goto fallback; + +if (mnt_monitor_enable_kernel (mn,1 ) <0 ) { +mnt_unref_monitor (mn); + goto fallback; + } + +clock_gettime (CLOCK_MONOTONIC , &start); + +while (remaining_ms >0 ) { +int rc =mnt_monitor_wait (mn, remaining_ms); +if (rc >0 ) { + ret =get_mountinfo (device_number, device_info,MOUNTINFO_PATH ); +if (ret ==0 ) { +mnt_unref_monitor (mn); +return 0 ; + } + }else { +break ; + } + +clock_gettime (CLOCK_MONOTONIC , &now); +long elapsed_ms = (now.tv_sec - start.tv_sec) *1000 + + (now.tv_nsec - start.tv_nsec) /1000000 ; + remaining_ms =MNT_NM_TIMEOUT - elapsed_ms; + } + +mnt_unref_monitor (mn); +return ret; + +fallback:for (int retry_count =0 ; retry_count <5 && ret !=0 ; retry_count++) {usleep (50000 ); ret =get_mountinfo (device_number, device_info,MOUNTINFO_PATH );
Signature · 1 line
2.51.0
Quoted 32 lines · Aaron Tomlin
The nfsrahead utility relies on parsing "
/proc/self/mountinfo" to correlate a device number with a specific NFS mount point. However, due to the asynchronous nature of system initialisation, the relevant entry in mountinfo may not be immediately available when the tool is executed.Currently, the utility employs a naive polling mechanism, retrying the search five times with a fixed 50ms delay (totalling 250ms). This approach proves brittle on systems under heavy load or during distinctively slow boot sequences.
To mitigate this race condition and improve robustness, update
get_device_info()to utilise the libmount monitoring API.The new implementation introduces the following logic:
1. Initialises a monitor on
/proc/self/mountinfousingmnt_new_monitor().2. Replaces the fixed polling loop with
mnt_monitor_wait().3. Increases the maximum wait time to 10 seconds (
MNT_NM_TIMEOUT).4. Introduces a fast-path rejection mechanism. NFS backing devices are allocated from the kernel's unnamed block device pool (major number 0). While some local multi-device filesystems (such as Btrfs) also utilise anonymous device numbers, physical hardware block devices (e.g., sda, nvme) always possess specific, non-zero major numbers. By instantly exiting with
-ENODEVfor any device string not beginning with "0:", we safely bypass the monitor for physical drives, preventing the exhaustion of udev worker threads. Seeset_anon_super()andget_anon_bdev().
Quoted 7 lines
5. Implements strict monotonic deadline tracking within the monitor loop to prevent indefinite blocking.
Fixes 2b62ac4c ("nfsrahead: enable event-driven mountinfo monitoring")Reported-by Yi Zhang <[email protected]>Link https://lore.kernel.org/linux-block/CAHj4cs8URj2fJ7KyP9ViAm6npVOaMiAErnw2uFyPYEU2wb7G_w@mail.gmail.com/T/#t open hereSigned-off-by Aaron Tomlin <[email protected]>
Committed... (tag: nfs-utils-2-8-6-rc4)
steved.
Quoted 96 lines
---
Hi Steve,
This patch should resolve the udev worker exhaustion issue reported by Yi. It applies cleanly on top of the current nfs-utils tree, after your revert [1].
Thank you.
[1]: https://lore.kernel.org/linux-nfs/[email protected]/
tools/nfsrahead/main.c | 55 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-)tools/nfsrahead/main.c +54
-1@@ -3,6 +3,7 @@#include <stdlib.h> #include <errno.h> #include <unistd.h> +#include <time.h> #include <libmount/libmount.h> #include <sys/sysmacros.h> @@ -17,6 +18,8 @@#define CONF_NAME "nfsrahead"#define NFS_DEFAULT_READAHEAD 128 +#define MNT_NM_TIMEOUT 10000 +/* Device information from the system */ struct device_info {char *device_number;@@ -117,7 +120,57 @@ out_free_device_info:static int get_device_info (const char *device_number,struct device_info *device_info) { -int ret =get_mountinfo (device_number, device_info,MOUNTINFO_PATH ); +int ret; +struct libmnt_monitor *mn =NULL ; +struct timespec start, now; +int remaining_ms =MNT_NM_TIMEOUT ; + +/* +* Fast-path rejection: +* NFS backing devices always use the anonymous block device major number (0). +* If the device number does not start with "0:", it is a physical block device +* and will never be an NFS mount. Exit immediately to prevent blocking udev. +*/ +if (strncmp (device_number,"0:" ,2 ) !=0 ) +return -ENODEV ; + + ret =get_mountinfo (device_number, device_info,MOUNTINFO_PATH ); +if (ret ==0 ) +return 0 ; + + mn =mnt_new_monitor (); +if (!mn) + goto fallback; + +if (mnt_monitor_enable_kernel (mn,1 ) <0 ) { +mnt_unref_monitor (mn); + goto fallback; + } + +clock_gettime (CLOCK_MONOTONIC , &start); + +while (remaining_ms >0 ) { +int rc =mnt_monitor_wait (mn, remaining_ms); +if (rc >0 ) { + ret =get_mountinfo (device_number, device_info,MOUNTINFO_PATH ); +if (ret ==0 ) { +mnt_unref_monitor (mn); +return 0 ; + } + }else { +break ; + } + +clock_gettime (CLOCK_MONOTONIC , &now); +long elapsed_ms = (now.tv_sec - start.tv_sec) *1000 + + (now.tv_nsec - start.tv_nsec) /1000000 ; + remaining_ms =MNT_NM_TIMEOUT - elapsed_ms; + } + +mnt_unref_monitor (mn); +return ret; + +fallback:for (int retry_count =0 ; retry_count <5 && ret !=0 ; retry_count++) {usleep (50000 ); ret =get_mountinfo (device_number, device_info,MOUNTINFO_PATH );
Hi Steve/Aaron
The patch seems still have issues. Here is the journal log during blktests:
# rpm -qf /usr/libexec/nfsrahead nfs-common-utils-2.8.6-0.fc44.x86_64
Mar 09 08:35:05 (udev-worker)[9120]: 8:16: Process '/usr/libexec/nfsrahead 8:16' terminated by signal ABRT. Mar 09 08:35:05 (udev-worker)[9120]: 8:16: Failed to wait for spawned command '/usr/libexec/nfsrahead 8:16': Input/output error Mar 09 08:35:05 (udev-worker)[9120]: 8:16: /usr/lib/udev/rules.d/99-nfs.rules:1 PROGRAM="/usr/libexec/nfsrahead %k": Failed to execute "/usr/libexec/nfsrahead 8:16": Input/output error Mar 09 08:35:07 systemd-coredump[9148]: Process 9146 (nfsrahead) of
user 0 dumped core.
Module /usr/libexec/nfsrahead
from rpm nfs-utils-2.8.6-0.fc44.x86_64
Module/usr/lib64/ld-linux-x86-64.so.2 from rpm glibc-2.43.9000-2.fc45.x86_64
Module libpcre2-8.so.0 from
rpm pcre2-10.47-1.fc44.1.x86_64
Module libselinux.so.1 from
rpm libselinux-3.10-1.fc44.x86_64
Module libblkid.so.1 from rpm
util-linux-2.41.3-12.fc44.x86_64
Module libc.so.6 from rpm
glibc-2.43.9000-2.fc45.x86_64
Module libmount.so.1 from rpm
util-linux-2.41.3-12.fc44.x86_64
Stack trace of thread 9146:
#0 0x00007f7a2fdd89cc__pthread_kill_implementation (libc.so.6 + 0x759cc)
#1 0x00007f7a2fd7d34e raise
(libc.so.6 + 0x1a34e)
#2 0x00007f7a2fd647b3 abort
(libc.so.6 + 0x17b3)
#3 0x00007f7a2fd65804
__libc_message_impl.cold (libc.so.6 + 0x2804)
#4 0x00007f7a2fde2d2c
malloc_printerr (libc.so.6 + 0x7fd2c)
#5 0x00007f7a2fde4710
_int_free_merge_chunk (libc.so.6 + 0x81710)
#6 0x00007f7a2fde4789
_int_free_chunk (libc.so.6 + 0x81789)
#7 0x000056327361bd47 main
(/usr/libexec/nfsrahead + 0xd47)
#8 0x00007f7a2fd66681
__libc_start_call_main (libc.so.6 + 0x3681)
#9 0x00007f7a2fd66798
__libc_start_main@@GLIBC_2.34 (libc.so.6 + 0x3798)
#10 0x000056327361bfc5 _start
(/usr/libexec/nfsrahead + 0xfc5)
ELF object binary
architecture: AMD x86-64
Mar 09 08:35:07 (udev-worker)[9115]: 8:16: Process
'/usr/libexec/nfsrahead 8:16' terminated by signal ABRT.
Mar 09 08:35:07 (udev-worker)[9115]: 8:16: Failed to wait for spawned
command '/usr/libexec/nfsrahead 8:16': Input/output error
Mar 09 08:35:07 (udev-worker)[9115]: 8:16:/usr/lib/udev/rules.d/99-nfs.rules:1 PROGRAM="/usr/libexec/nfsrahead %k": Failed to execute "/usr/libexec/nfsrahead 8:16": Input/output error
Quoted 143 lines · Steve Dickson
Quoted 32 lines · Aaron Tomlin
The nfsrahead utility relies on parsing "
/proc/self/mountinfo" to correlate a device number with a specific NFS mount point. However, due to the asynchronous nature of system initialisation, the relevant entry in mountinfo may not be immediately available when the tool is executed.Currently, the utility employs a naive polling mechanism, retrying the search five times with a fixed 50ms delay (totalling 250ms). This approach proves brittle on systems under heavy load or during distinctively slow boot sequences.
To mitigate this race condition and improve robustness, update
get_device_info()to utilise the libmount monitoring API.The new implementation introduces the following logic:
1. Initialises a monitor on
/proc/self/mountinfousingmnt_new_monitor().2. Replaces the fixed polling loop with
mnt_monitor_wait().3. Increases the maximum wait time to 10 seconds (
MNT_NM_TIMEOUT).4. Introduces a fast-path rejection mechanism. NFS backing devices are allocated from the kernel's unnamed block device pool (major number 0). While some local multi-device filesystems (such as Btrfs) also utilise anonymous device numbers, physical hardware block devices (e.g., sda, nvme) always possess specific, non-zero major numbers. By instantly exiting with
-ENODEVfor any device string not beginning with "0:", we safely bypass the monitor for physical drives, preventing the exhaustion of udev worker threads. Seeset_anon_super()andget_anon_bdev().Quoted 7 lines
5. Implements strict monotonic deadline tracking within the monitor loop to prevent indefinite blocking.
Fixes 2b62ac4c ("nfsrahead: enable event-driven mountinfo monitoring")Reported-by Yi Zhang <[email protected]>Link https://lore.kernel.org/linux-block/CAHj4cs8URj2fJ7KyP9ViAm6npVOaMiAErnw2uFyPYEU2wb7G_w@mail.gmail.com/T/#t open hereSigned-off-by Aaron Tomlin <[email protected]>Committed... (tag: nfs-utils-2-8-6-rc4)
steved.
Quoted 96 lines
---
Hi Steve,
This patch should resolve the udev worker exhaustion issue reported by Yi. It applies cleanly on top of the current nfs-utils tree, after your revert [1].
Thank you.
[1]: https://lore.kernel.org/linux-nfs/[email protected]/
tools/nfsrahead/main.c | 55 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-)tools/nfsrahead/main.c +54
-1@@ -3,6 +3,7 @@#include <stdlib.h> #include <errno.h> #include <unistd.h> +#include <time.h> #include <libmount/libmount.h> #include <sys/sysmacros.h> @@ -17,6 +18,8 @@#define CONF_NAME "nfsrahead"#define NFS_DEFAULT_READAHEAD 128 +#define MNT_NM_TIMEOUT 10000 +/* Device information from the system */ struct device_info {char *device_number;@@ -117,7 +120,57 @@ out_free_device_info:static int get_device_info (const char *device_number,struct device_info *device_info) { -int ret =get_mountinfo (device_number, device_info,MOUNTINFO_PATH ); +int ret; +struct libmnt_monitor *mn =NULL ; +struct timespec start, now; +int remaining_ms =MNT_NM_TIMEOUT ; + +/* +* Fast-path rejection: +* NFS backing devices always use the anonymous block device major number (0). +* If the device number does not start with "0:", it is a physical block device +* and will never be an NFS mount. Exit immediately to prevent blocking udev. +*/ +if (strncmp (device_number,"0:" ,2 ) !=0 ) +return -ENODEV ; + + ret =get_mountinfo (device_number, device_info,MOUNTINFO_PATH ); +if (ret ==0 ) +return 0 ; + + mn =mnt_new_monitor (); +if (!mn) + goto fallback; + +if (mnt_monitor_enable_kernel (mn,1 ) <0 ) { +mnt_unref_monitor (mn); + goto fallback; + } + +clock_gettime (CLOCK_MONOTONIC , &start); + +while (remaining_ms >0 ) { +int rc =mnt_monitor_wait (mn, remaining_ms); +if (rc >0 ) { + ret =get_mountinfo (device_number, device_info,MOUNTINFO_PATH ); +if (ret ==0 ) { +mnt_unref_monitor (mn); +return 0 ; + } + }else { +break ; + } + +clock_gettime (CLOCK_MONOTONIC , &now); +long elapsed_ms = (now.tv_sec - start.tv_sec) *1000 + + (now.tv_nsec - start.tv_nsec) /1000000 ; + remaining_ms =MNT_NM_TIMEOUT - elapsed_ms; + } + +mnt_unref_monitor (mn); +return ret; + +fallback:for (int retry_count =0 ; retry_count <5 && ret !=0 ; retry_count++) {usleep (50000 ); ret =get_mountinfo (device_number, device_info,MOUNTINFO_PATH );
Signature · 2 lines
Best Regards, Yi Zhang
Quoted 3 lines · Yi Zhang
Hi Steve/Aaron
The patch seems still have issues. Here is the journal log during blktests:
Hi Yi, Steve,
Quoted 20 lines
malloc_printerr(libc.so.6 +0x7fd2c)#5 0x00007f7a2fde4710 _int_free_merge_chunk (libc.so.6 + 0x81710) #6 0x00007f7a2fde4789 _int_free_chunk (libc.so.6 + 0x81789) #7 0x000056327361bd47 main (/usr/libexec/nfsrahead + 0xd47) #8 0x00007f7a2fd66681 __libc_start_call_main (libc.so.6 + 0x3681) #9 0x00007f7a2fd66798 __libc_start_main@@GLIBC_2.34 (libc.so.6 + 0x3798) #10 0x000056327361bfc5 _start (/usr/libexec/nfsrahead + 0xfc5) ELF object binary architecture: AMD x86-64 Mar 09 08:35:07 (udev-worker)[9115]: 8:16: Process '/usr/libexec/nfsrahead 8:16' terminated by signal ABRT. Mar 09 08:35:07 (udev-worker)[9115]: 8:16: Failed to wait for spawned command '/usr/libexec/nfsrahead 8:16': Input/output error Mar 09 08:35:07 (udev-worker)[9115]: 8:16:
Quoted 3 lines
/usr/lib/udev/rules.d/99-nfs.rules:1PROGRAM="/usr/libexec/nfsrahead %k": Failed to execute "/usr/libexec/nfsrahead8:16": Input/output error
Thank you for reporting this issue and for providing such a detailed journal log. It is incredibly helpful in pinpointing the problem.
The log points directly to an unintended side effect of the recent fast-path optimisation. While the new logic correctly rejects non-NFS block devices (such as 8:16) without blocking the udev worker, this early exit completely bypasses the initialisation of the device_info struct in main().
Consequently, when the code jumps to the cleanup path upon exiting, free_device_info() attempts to free pointers containing uninitialised stack memory. This is what triggers the glibc abort(3) you observed during the tests.
The fix is quite straightforward. I will shortly send a patch that explicitly zero-initialises the struct at its declaration:
struct device_info device = { 0 };This ensures all internal pointers begin as NULL, allowing the cleanup path to safely handle an early exit without crashing.
I will Cc you on the patch once it is sent. I would appreciate it if you could test it against your blktests environment to confirm the issue is fully resolved.
Kind regards,
Signature · 1 line
Aaron Tomlin