swapon04: Settle udev after mkswap to prevent tmpfs hole filling
PATCH 1/1 swapon04: Settle udev after mkswap to prevent tmpfs hole filling
When mkswap formats and closes the loop device, systemd-udevd asynchronously runs blkid probing on the device. On kernels prior to 5.18, tmpfs shmem_file_read_iter() treats non-iovec read iterators (!iter_is_iovec(to)) as stacking filesystem reads, allocating real zero-filled pages into holes (this behavior get fixed by commit 56a8c8eb1eaf ("tmpfs: do not allocate pages on read")).
When a udev worker reads the loop device after swapon-time discard has punched holes in the tmpfs backing file, the probing reads re-allocate up to 84 zero-filled pages (672 blocks), causing blocks_after to exceed max_header_blocks and failing the test intermittently on tmpfs (e.g. observed on SLE 15-SP4 aarch64):
swapon04.c:167: TFAIL: blocks_after (672) > max_header_blocks (16)
Tracing block:block_rq_issue confirms that udev-worker reads the loop device after discard has punched the holes:
# mkswap writes swap header mkswap-703 [000] ..... 2100.340705: block_rq_issue: 7,1 WS 4096 () 0 + 8 [mkswap] # sys_swapon reads swap header swapon04-701 [000] ..... 2100.345665: block_rq_issue: 7,1 R 4096 () 0 + 8 [swapon04] # swapon-time discard punches holes across sectors 8..32767 kworker/0:1H-31[000] ..... 2100.345715: block_rq_issue: 7,1 DS 16773120 () 8 + 32760 [kworker/0:1H] # udev-worker probes loop device 5.5ms after discard, re-allocating tmpfs pages (udev-worker)-655 [001] .. 2100.351255: block_rq_issue: 7,1 RA 4096 () 32640 + 8 [(udev-worker)] (udev-worker)-655 [000] .. 2100.351302: block_rq_issue: 7,1 RA 4096 () 32752 + 8 [(udev-worker)] (udev-worker)-655 [000] .. 2100.352286: block_rq_issue: 7,1 RA 126976 () 264 + 248 [(udev-worker)]
Wait for udev to settle after mkswap so that probing completes before swapon discards the area.
--- testcases/kernel/syscalls/swapon/swapon04.c | 2 ++ 1 file changed, 2 insertions(+)
testcases/kernel/syscalls/swapon/swapon04.c +2 -0
@@ -138,6 +138,7 @@ static void verify_swapon(unsigned int n)struct stat st;blkcnt_t blocks_before, blocks_after;const char *const mkswap_argv[] = {"mkswap" , loop_dev,NULL }; +const char *const udevadm_argv[] = {"udevadm" ,"settle" ,NULL };tst_res (TINFO ,"Testing swapon(%s, %s)" , loop_dev, tc->desc);
@@ -148,6 +149,7 @@ static void verify_swapon(unsigned int n)SAFE_CLOSE (fd);tst_cmd (mkswap_argv,"/dev/null" ,"/dev/null" ,0 ); +tst_cmd (udevadm_argv,"/dev/null" ,"/dev/null" ,TST_CMD_PASS_RETVAL );SAFE_STAT (BACKING_FILE , &st); blocks_before = st.st_blocks;
Signature · 5 lines
2.55.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Wei,
Quoted 1 line · Wei Gao
swapon04: Settle udev after mkswap to prevent tmpfs hole filling
Verdict - Reviewed
---
Note:
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 13 lines
testcases/kernel/syscalls/swapon/swapon04.c +1
-0@@ -138,6 +138,7 @@ static void verify_swapon(unsigned int n)struct stat st;blkcnt_t blocks_before, blocks_after;const char *const mkswap_argv[] = {"mkswap" , loop_dev,NULL }; +const char *const udevadm_argv[] = {"udevadm" ,"settle" ,NULL };tst_res (TINFO ,"Testing swapon(%s, %s)" , loop_dev, tc->desc);
testcases/kernel/syscalls/swapon/swapon04.c static void verify_swapon(unsigned int n) · line 155
Quoted 7 lines
testcases/kernel/syscalls/swapon/swapon04.c +1
-0@@ -148,6 +149,7 @@ static void verify_swapon(unsigned int n)SAFE_CLOSE (fd);tst_cmd (mkswap_argv,"/dev/null" ,"/dev/null" ,0 ); +tst_cmd (udevadm_argv,"/dev/null" ,"/dev/null" ,TST_CMD_PASS_RETVAL );SAFE_STAT (BACKING_FILE , &st); blocks_before = st.st_blocks;
The udevadm has to be added to .needs_cmds as well:
testcases/kernel/syscalls/swapon/swapon04.c +1 -0
@@ -195,6 +195,7 @@ static struct tst_test test = { }, .needs_cmds = (struct tst_cmd[]) { {.cmd ="mkswap" }, + {.cmd ="udevadm" }, {} }, .needs_kconfigs = (const char *[]) {
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 6 lines
When mkswap formats and closes the loop device, systemd-udevd asynchronously runs blkid probing on the device. On kernels prior to 5.18, tmpfs
shmem_file_read_iter()treats non-iovec read iterators (!iter_is_iovec(to)) as stacking filesystem reads, allocating real zero-filled pages into holes (this behavior get fixed by commit56a8c8eb1eaf("tmpfs: do not allocate pages on read")).
Also when this has been fixed by 5.18 we shouldn't apply the fix for 5.18+ otherwise this would mask regressions.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 1/1 swapon04: Settle udev after mkswap on kernels < 5.18
In reply to Wei Gao via ltpWhen mkswap formats and closes the loop device, systemd-udevd asynchronously runs blkid probing on the device. On kernels prior to 5.18, tmpfs shmem_file_read_iter() treats non-iovec read iterators (!iter_is_iovec(to)) as stacking filesystem reads, allocating real zero-filled pages into holes (this behavior get fixed by commit 56a8c8eb1eaf ("tmpfs: do not allocate pages on read")).
When a udev worker reads the loop device after swapon-time discard has punched holes in the tmpfs backing file, the probing reads re-allocate up to 84 zero-filled pages (672 blocks), causing blocks_after to exceed max_header_blocks and failing the test intermittently on tmpfs (e.g. observed on SLE 15-SP4 aarch64):
swapon04.c:167: TFAIL: blocks_after (672) > max_header_blocks (16)
Tracing block:block_rq_issue confirms that udev-worker reads the loop device after discard has punched the holes:
# mkswap writes swap header mkswap-703 [000] ..... 2100.340705: block_rq_issue: 7,1 WS 4096 () 0 + 8 [mkswap] # sys_swapon reads swap header swapon04-701 [000] ..... 2100.345665: block_rq_issue: 7,1 R 4096 () 0 + 8 [swapon04] # swapon-time discard punches holes across sectors 8..32767 kworker/0:1H-31[000] ..... 2100.345715: block_rq_issue: 7,1 DS 16773120 () 8 + 32760 [kworker/0:1H] # udev-worker probes loop device 5.5ms after discard, re-allocating tmpfs pages (udev-worker)-655 [001] .. 2100.351255: block_rq_issue: 7,1 RA 4096 () 32640 + 8 [(udev-worker)] (udev-worker)-655 [000] .. 2100.351302: block_rq_issue: 7,1 RA 4096 () 32752 + 8 [(udev-worker)] (udev-worker)-655 [000] .. 2100.352286: block_rq_issue: 7,1 RA 126976 () 264 + 248 [(udev-worker)]
On kernels prior to 5.18, wait for udev to settle after mkswap so that probing completes before swapon discards the area. On 5.18+, skip settling so that any regression causing tmpfs to allocate pages on loop reads is not masked.
---v1->v2:
- Add udevadm to .needs_cmds
- Skip udevadm settling on 5.18+
testcases/kernel/syscalls/swapon/swapon04.c | 4 ++++ 1 file changed, 4 insertions(+)
testcases/kernel/syscalls/swapon/swapon04.c +4 -0
@@ -138,6 +138,7 @@ static void verify_swapon(unsigned int n)struct stat st;blkcnt_t blocks_before, blocks_after;const char *const mkswap_argv[] = {"mkswap" , loop_dev,NULL }; +const char *const udevadm_argv[] = {"udevadm" ,"settle" ,NULL };tst_res (TINFO ,"Testing swapon(%s, %s)" , loop_dev, tc->desc);
@@ -148,6 +149,8 @@ static void verify_swapon(unsigned int n)SAFE_CLOSE (fd);tst_cmd (mkswap_argv,"/dev/null" ,"/dev/null" ,0 ); +if (tst_kvercmp (5 ,18 ,0 ) <0 ) +tst_cmd (udevadm_argv,"/dev/null" ,"/dev/null" ,0 );SAFE_STAT (BACKING_FILE , &st); blocks_before = st.st_blocks;
@@ -195,6 +198,7 @@ static struct tst_test test = { }, .needs_cmds = (struct tst_cmd[]) { {.cmd ="mkswap" }, + {.cmd ="udevadm" }, {} }, .needs_kconfigs = (const char *[]) {
Signature · 5 lines
2.55.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2] swapon04: Settle udev after mkswap on kernels < 5.18
Hi!
Pushed, thanks.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp