msync04, pipe2_04: Support kernel/userspace page size mismatch
cover msync04, pipe2_04: Support kernel/userspace page size mismatch
Hi,
This is the follow-up to the earlier series [1] for running LTP under the 16K page size emulation of the Android Common Kernels [2] (page_shift=14 on the kernel command line on x86_64), where the kernel reports a 16K page size to userspace while keeping 4K pages internally.
These two patches were kept separate from [1] because they adjust how msync04 and pipe2_04 verify the tested behavior:
- msync04: under the emulation, each /proc/self/pagemap entry covers four 4K kernel pages and therefore exposes no PFN (PFN == 0 even for root), so the test falls back to TCONF. When PM_PRESENT is set and PFN is 0, check Shared_Dirty and Private_Dirty of the mapping in /proc/self/smaps instead. Nothing changes on kernels that expose the PFN to root.
- pipe2_04: F_SETPIPE_SZ(0) rounds the capacity up to one pipe buffer (one 4K kernel page), so checking that F_GETPIPE_SZ on the other end matches the size returned by F_SETPIPE_SZ and does not exceed getpagesize() preserves the cross-fd check without assuming a pipe buffer is getpagesize() bytes.
Tested on:
- x86_64 with a 4K page size (Debian, 7.1 kernel), where pipe2_04 passes and both tests compile without warnings with gcc and clang.
- An Android x86_64 emulator with a 4K page size (6.6 kernel), where both tests pass before and after.
- An Android x86_64 Cuttlefish device running 6.18.32-android17 with page_shift=14, where msync04 goes from 4x TCONF to 4x TPASS and pipe2_04 goes from TFAIL to TPASS.
[1] https://lore.kernel.org/ltp/[email protected]/
[2] https://source.android.com/docs/core/architecture/16kb-page-size/getting-started-cf-x86-64-pgagnostic
Wake Liu (2):
msync04: Check smaps when the page frame number is not exposed
pipe2_04: Don't assume the minimum pipe size is getpagesize()
testcases/kernel/syscalls/msync/msync04.c | 46 +++++++++++++++++++++- testcases/kernel/syscalls/pipe2/pipe2_04.c | 17 +++++--- 2 files changed, 56 insertions(+), 7 deletions(-)
base-commit: 686a331b6240e54ac3170717adfd28ec94cfe100
Signature · 5 lines
2.56.0.611.g1030f0b940-goog -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 1/2 msync04: Check smaps when the page frame number is not exposed
The test looks up the dirty flag of the mapped page in /proc/kpageflags, using the PFN it reads from /proc/self/pagemap. If the PFN is reported as zero, get_dirty_bit() returns 0 and the test falls back to checking the file content, which can end only with TCONF or TFAIL.
The PFN of a present page is reported as zero with the 16K page size emulation that Android uses on x86_64, where each entry of the emulated pagemap covers four 4K kernel pages and therefore carries no single PFN. The test then reports:
TCONF: Write was too fast, couldn't test msync()
When the page is present but its PFN is not exposed, sum up the Shared_Dirty and Private_Dirty counters of the mapping in /proc/self/smaps instead. They account for both the dirty bit in the page table and the dirty flag of the page, and drop to zero once msync() has written the page back. Nothing changes on kernels that expose the PFN to root.
--- testcases/kernel/syscalls/msync/msync04.c | 46 ++++++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/msync/msync04.c +44 -2
@@ -17,6 +17,7 @@#define _GNU_SOURCE#include <errno.h> #include "tst_test.h" +#include "tst_safe_stdio.h" static int test_fd;static char *mmaped_area;
@@ -24,6 +25,39 @@ static long pagesize;#define STRING_TO_WRITE "AAAAAAAAAA" +#define PM_PFRAME_MASK ((1ULL << 55) - 1) +#define PM_PRESENT (1ULL << 63) + +/* +* Returns the amount of dirty memory of the mapping starting at addr, as +* accounted in /proc/self/smaps. +*/ +static long get_smaps_dirty_kb (unsigned long addr) +{ +FILE *fp =SAFE_FOPEN ("/proc/self/smaps" ,"r" ); +unsigned long start, end; +long dirty_kb =0 , kb; +char line[1024 ]; +int found =0 ; + +while (fgets (line,sizeof (line), fp)) { +if (sscanf (line,"%lx-%lx " , &start, &end) ==2 ) { + found = start == addr; +continue ; + } + +if (!found) +continue ; + +if (sscanf (line,"Shared_Dirty: %ld kB" , &kb) ==1 || +sscanf (line,"Private_Dirty: %ld kB" , &kb) ==1 ) + dirty_kb += kb; + } + +SAFE_FCLOSE (fp); +return dirty_kb; +} +static uint64_t get_dirty_bit (void *data) {int pagemap_fd, pageflags_fd;
@@ -37,9 +71,17 @@ static uint64_t get_dirty_bit(void *data)SAFE_READ (SAFE_READ_ALL , pagemap_fd, &pagemap_entry,sizeof (pagemap_entry));SAFE_CLOSE (pagemap_fd); - pfn = pagemap_entry & ((1ULL <<55 ) -1 ); -if (!pfn) + pfn = pagemap_entry &PM_PFRAME_MASK ; +if (!pfn) { +/* +* The PFN of a present page may not be exposed, check the +* dirty accounting of the mapping in smaps instead. +*/ +if (pagemap_entry &PM_PRESENT ) +return get_smaps_dirty_kb (addr) >0 ; +return 0 ; + } pageflags_fd =SAFE_OPEN ("/proc/kpageflags" ,O_RDONLY ); index = pfn *sizeof (uint64_t );SAFE_LSEEK (pageflags_fd, index,SEEK_SET );
Signature · 5 lines
2.56.0.611.g1030f0b940-goog -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] msync04: Check smaps when the page frame number is not exposed
Hi Wake,
Quoted 1 line · Wake Liu
msync04: Check smaps when the page frame number is not exposed
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
PATCH 2/2 pipe2_04: Don't assume the minimum pipe size is getpagesize()
In reply to Wake Liu via ltpF_SETPIPE_SZ rounds the requested size up to the minimum pipe capacity, which is a single pipe buffer of one kernel page. The test expects that to be equal to the page size reported to userspace.
That is not the case when the kernel reports a larger page size to userspace than it uses internally, as with the 16K page size emulation that Android uses on x86_64:
TFAIL: Pipe size (4096) must be page size (16384)
Check that F_GETPIPE_SZ on the other end of the pipe returns the size set by F_SETPIPE_SZ, which is what the test is about, and that the minimum size does not exceed the page size. The rest of the test already uses the actual pipe size.
--- testcases/kernel/syscalls/pipe2/pipe2_04.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
testcases/kernel/syscalls/pipe2/pipe2_04.c +12 -5
@@ -57,7 +57,7 @@ static void test_pipe2(void)static void setup (void ) { -int page_size, pipe_size; +int page_size, pipe_size, set_size;char *write_buffer;SAFE_PIPE2 (fds,O_NONBLOCK );
@@ -70,17 +70,24 @@ static void setup(void)* A pipe has two file descriptors. * But in the kernel these two file descriptors point to the same pipe. * So setting size from first file handle set size for the pipe. +* +* The size is rounded up to the minimum pipe capacity, i.e. a single +* pipe buffer of one kernel page, which is not necessarily the same +* as the page size reported to userspace. */ -SAFE_FCNTL (fds[0 ],F_SETPIPE_SZ ,0 ); + set_size =SAFE_FCNTL (fds[0 ],F_SETPIPE_SZ ,0 ); +if (set_size > page_size) +tst_res (TFAIL ,"Minimum pipe size (%d) exceeds page size (%d)" , + set_size, page_size);/* * So getting size from the second file descriptor return the size of * the pipe which was changed before with first file descriptor. */ pipe_size =SAFE_FCNTL (fds[1 ],F_GETPIPE_SZ ); -if (pipe_size != page_size) -tst_res (TFAIL ,"Pipe size (%d) must be page size (%d)" , - pipe_size, page_size); +if (pipe_size != set_size) +tst_res (TFAIL ,"Pipe size (%d) must be the size set (%d)" , + pipe_size, set_size); write_buffer =SAFE_MALLOC (pipe_size);memset (write_buffer,'x' , pipe_size);
Signature · 5 lines
2.56.0.611.g1030f0b940-goog -- Mailing list info: https://lists.linux.it/listinfo/ltp
v2 cover msync04, pipe2_04: Support kernel/userspace page size mismatch
In reply to Wake Liu via ltpHi,
This is the follow-up to the earlier series [1] for running LTP under the 16K page size emulation of the Android Common Kernels [2] (page_shift=14 on the kernel command line on x86_64), where the kernel reports a 16K page size to userspace while keeping 4K pages internally.
These two patches were kept separate from [1] because they adjust how msync04 and pipe2_04 verify the tested behavior:
- msync04: under the emulation, each /proc/self/pagemap entry covers four 4K kernel pages and therefore exposes no PFN (PFN == 0 even for root), so the test falls back to TCONF. When PM_PRESENT is set and PFN is 0, check Shared_Dirty and Private_Dirty of the mapping in /proc/self/smaps instead. Nothing changes on kernels that expose the PFN to root.
- pipe2_04: F_SETPIPE_SZ(0) rounds the capacity up to a single pipe buffer of one kernel page, which can be smaller or larger than getpagesize(). Only checking that F_GETPIPE_SZ on the other end matches the size returned by F_SETPIPE_SZ preserves the cross-fd check without assuming a pipe buffer is getpagesize() bytes.
Changes in v2:
- pipe2_04: Drop the check that the minimum pipe size doesn't exceed getpagesize(), which fails with 16K kernel pages and a 4K page size reported to userspace (Kalesh).
- Rebased on current master.
Tested on:
- x86_64 with a 4K page size (Debian, 7.1 kernel), where pipe2_04 passes and both tests compile without warnings with gcc and clang.
- An Android x86_64 emulator with a 4K page size (6.6 kernel), where both tests pass before and after.
- An Android x86_64 Cuttlefish device running 6.18.32-android17 with page_shift=14, where msync04 goes from 4x TCONF to 4x TPASS and pipe2_04 goes from TFAIL to TPASS.
For v2, pipe2_04 was also run on the same Cuttlefish device and on the 4K x86_64 host with getpagesize() and sysconf(_SC_PAGESIZE) wrapped to return 1K, to mimic kernel pages that are larger than the page size reported to userspace. v1 fails there with "Minimum pipe size (4096) exceeds page size (1024)", while v2 passes both as is and wrapped.
v1: https://lore.kernel.org/ltp/[email protected]/
[1] https://lore.kernel.org/ltp/[email protected]/
[2] https://source.android.com/docs/core/architecture/16kb-page-size/getting-started-cf-x86-64-pgagnostic
Wake Liu (2):
msync04: Check smaps when the page frame number is not exposed
pipe2_04: Don't assume the minimum pipe size is getpagesize()
testcases/kernel/syscalls/msync/msync04.c | 46 +++++++++++++++++++++- testcases/kernel/syscalls/pipe2/pipe2_04.c | 15 ++++--- 2 files changed, 53 insertions(+), 8 deletions(-)
base-commit: c4be3107083cd11e4732884c4c8c5e9c6665e729
Signature · 5 lines
2.56.0.611.g1030f0b940-goog -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 1/2 msync04: Check smaps when the page frame number is not exposed
The test looks up the dirty flag of the mapped page in /proc/kpageflags, using the PFN it reads from /proc/self/pagemap. If the PFN is reported as zero, get_dirty_bit() returns 0 and the test falls back to checking the file content, which can end only with TCONF or TFAIL.
The PFN of a present page is reported as zero with the 16K page size emulation that Android uses on x86_64, where each entry of the emulated pagemap covers four 4K kernel pages and therefore carries no single PFN. The test then reports:
TCONF: Write was too fast, couldn't test msync()
When the page is present but its PFN is not exposed, sum up the Shared_Dirty and Private_Dirty counters of the mapping in /proc/self/smaps instead. They account for both the dirty bit in the page table and the dirty flag of the page, and drop to zero once msync() has written the page back. Nothing changes on kernels that expose the PFN to root.
--- testcases/kernel/syscalls/msync/msync04.c | 46 ++++++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/msync/msync04.c +44 -2
@@ -17,6 +17,7 @@#define _GNU_SOURCE#include <errno.h> #include "tst_test.h" +#include "tst_safe_stdio.h" static int test_fd;static char *mmaped_area;
@@ -24,6 +25,39 @@ static long pagesize;#define STRING_TO_WRITE "AAAAAAAAAA" +#define PM_PFRAME_MASK ((1ULL << 55) - 1) +#define PM_PRESENT (1ULL << 63) + +/* +* Returns the amount of dirty memory of the mapping starting at addr, as +* accounted in /proc/self/smaps. +*/ +static long get_smaps_dirty_kb (unsigned long addr) +{ +FILE *fp =SAFE_FOPEN ("/proc/self/smaps" ,"r" ); +unsigned long start, end; +long dirty_kb =0 , kb; +char line[1024 ]; +int found =0 ; + +while (fgets (line,sizeof (line), fp)) { +if (sscanf (line,"%lx-%lx " , &start, &end) ==2 ) { + found = start == addr; +continue ; + } + +if (!found) +continue ; + +if (sscanf (line,"Shared_Dirty: %ld kB" , &kb) ==1 || +sscanf (line,"Private_Dirty: %ld kB" , &kb) ==1 ) + dirty_kb += kb; + } + +SAFE_FCLOSE (fp); +return dirty_kb; +} +static uint64_t get_dirty_bit (void *data) {int pagemap_fd, pageflags_fd;
@@ -37,9 +71,17 @@ static uint64_t get_dirty_bit(void *data)SAFE_READ (SAFE_READ_ALL , pagemap_fd, &pagemap_entry,sizeof (pagemap_entry));SAFE_CLOSE (pagemap_fd); - pfn = pagemap_entry & ((1ULL <<55 ) -1 ); -if (!pfn) + pfn = pagemap_entry &PM_PFRAME_MASK ; +if (!pfn) { +/* +* The PFN of a present page may not be exposed, check the +* dirty accounting of the mapping in smaps instead. +*/ +if (pagemap_entry &PM_PRESENT ) +return get_smaps_dirty_kb (addr) >0 ; +return 0 ; + } pageflags_fd =SAFE_OPEN ("/proc/kpageflags" ,O_RDONLY ); index = pfn *sizeof (uint64_t );SAFE_LSEEK (pageflags_fd, index,SEEK_SET );
Signature · 5 lines
2.56.0.611.g1030f0b940-goog -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] msync04: Check smaps when the page frame number is not exposed
Hi Wake,
Quoted 1 line · Wake Liu
msync04: Check smaps when the page frame number is not exposed
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
PATCH v2 2/2 pipe2_04: Don't assume the minimum pipe size is getpagesize()
In reply to Wake Liu via ltpF_SETPIPE_SZ rounds the requested size up to the minimum pipe capacity, which is a single pipe buffer of one kernel page. The test expects that to be equal to the page size reported to userspace.
That is not the case when the page size reported to userspace differs from the one the kernel uses internally. With the 16K page size emulation that Android uses on x86_64 the test fails with:
TFAIL: Pipe size (4096) must be page size (16384)
Conversely, with 16K kernel pages and a 4K page size reported to userspace, the minimum pipe size is 16384 instead of the expected 4096.
As the minimum pipe size can't be derived from getpagesize(), only check that F_GETPIPE_SZ on the other end of the pipe returns the size set by F_SETPIPE_SZ, which is what the test is about. The rest of the test already uses the actual pipe size.
---
Notes:
Changes in v2:
- Drop the check that the minimum pipe size doesn't exceed getpagesize(), which fails when the kernel uses larger pages than reported to userspace (Kalesh)
testcases/kernel/syscalls/pipe2/pipe2_04.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/pipe2/pipe2_04.c +9 -6
@@ -57,11 +57,10 @@ static void test_pipe2(void)static void setup (void ) { -int page_size, pipe_size; +int pipe_size, set_size;char *write_buffer;SAFE_PIPE2 (fds,O_NONBLOCK ); - page_size =SAFE_SYSCONF (_SC_PAGESIZE); flags =SAFE_FCNTL (fds[1 ],F_GETFL );if (!(flags &O_NONBLOCK ))
@@ -70,17 +69,21 @@ static void setup(void)* A pipe has two file descriptors. * But in the kernel these two file descriptors point to the same pipe. * So setting size from first file handle set size for the pipe. +* +* The size is rounded up to the minimum pipe capacity, i.e. a single +* pipe buffer of one kernel page, which can be smaller or larger than +* the page size reported to userspace. */ -SAFE_FCNTL (fds[0 ],F_SETPIPE_SZ ,0 ); + set_size =SAFE_FCNTL (fds[0 ],F_SETPIPE_SZ ,0 );/* * So getting size from the second file descriptor return the size of * the pipe which was changed before with first file descriptor. */ pipe_size =SAFE_FCNTL (fds[1 ],F_GETPIPE_SZ ); -if (pipe_size != page_size) -tst_res (TFAIL ,"Pipe size (%d) must be page size (%d)" , - pipe_size, page_size); +if (pipe_size != set_size) +tst_res (TFAIL ,"Pipe size (%d) must be the size set (%d)" , + pipe_size, set_size); write_buffer =SAFE_MALLOC (pipe_size);memset (write_buffer,'x' , pipe_size);
Signature · 5 lines
2.56.0.611.g1030f0b940-goog -- Mailing list info: https://lists.linux.it/listinfo/ltp