Re: [PATCH v3 7/7] selftest/mm: ksm_functional_tests: Add PROT_NONE test

From: David Hildenbrand
Date: Fri Aug 04 2023 - 14:01:27 EST


On 03.08.23 21:06, Peter Xu wrote:
On Thu, Aug 03, 2023 at 04:32:08PM +0200, David Hildenbrand wrote:
Let's test whether merging and unmerging in PROT_NONE areas works as
expected.

Pass a page protection to mmap_and_merge_range(), which will trigger
an mprotect() after writing to the pages, but before enabling merging.

Make sure that unsharing works as expected, by performing a ptrace write
(using /proc/self/mem) and by setting MADV_UNMERGEABLE.

Note that this implicitly tests that ptrace writes in an inaccessible
(PROT_NONE) mapping work as expected.

Signed-off-by: David Hildenbrand <david@xxxxxxxxxx>

[...]

+static void test_prot_none(void)
+{
+ const unsigned int size = 2 * MiB;
+ char *map;
+ int i;
+
+ ksft_print_msg("[RUN] %s\n", __func__);
+
+ map = mmap_and_merge_range(0x11, size, PROT_NONE, false);
+ if (map == MAP_FAILED)
+ goto unmap;
+
+ /* Store a unique value in each page on one half using ptrace */
+ for (i = 0; i < size / 2; i += pagesize) {
+ lseek(mem_fd, (uintptr_t) map + i, SEEK_SET);
+ if (write(mem_fd, &i, sizeof(size)) != sizeof(size)) {

sizeof(i)? May not matter a huge lot, though..

Oh, indeed, thanks!


+ ksft_test_result_fail("ptrace write failed\n");
+ goto unmap;
+ }
+ }

Acked-by: Peter Xu <peterx@xxxxxxxxxx>


Thanks!

--
Cheers,

David / dhildenb