Skip to contentSkip to conversation

tests/gem_mmap_offset: Accept partial read in oob-read

PATCH New David Hildenbrand (Arm) 1 participant 1 message Nov 23

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/gem_mmap_offset: Accept partial read in oob-read

The oob-read test reads 8 bytes through /proc/self/mem, assuming that the kernel would reject the entire read with EIO.

However, we have 4 remaining bytes in the 4 KiB mapping we can read, so it's valid for the kernel to only do a partial read that covers these 4 bytes. Allow that as well.

Otherwise, the test will fail on a kernel with upcoming vm_ops->access fixes, whereby vm_ops->access is limited to the current VMA, so the read would continue with the next mapping (if any).

So we have to make sure that nothing is mapped after the GEM mapping. Let's reserve an 8 KiB memory region, map the GEM object into the first 4 KiB slot, and create a hole by unmapping the second 4 KiB slot.

With the kernel changes in place:

  $ build/tests/gem_mmap_offset --run-subtest oob-read
  [...]
  Using IGT_SRANDOM=1795467759 for randomisation
  Opened device: /dev/dri/card1
  Starting subtest: oob-read
  Subtest oob-read: SUCCESS (0,000s)
---
 tests/intel/gem_mmap_offset.c | 35 ++++++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)
tests/intel/gem_mmap_offset.c +28 -7
index d3c56ab40..e4c0cfbd9 100644
@@ -482,13 +482,29 @@ test_oob_read(int i915)
 	handle = gem_create(i915, 4096);
 
 	for_each_mmap_offset_type(i915, t) {
-		uint32_t *ptr;
+		struct drm_i915_gem_mmap_offset arg = {
+			.handle = handle,
+			.flags = t->type,
+		};
+		void *ptr;
 
-		ptr = __mmap_offset(i915, handle, 0, 4096,
-				    PROT_READ | PROT_WRITE,
-				    t->type);
-		if (!ptr)
+		if (mmap_offset_ioctl(i915, &arg))
+			continue;
+
+		/* Reserve a region for mapping + hole to stop read(). */
+		ptr = mmap(NULL, 8192, PROT_NONE,
+			   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+		igt_assert(ptr != MAP_FAILED);
+
+		/* Place our mapping into the first slot. */
+		if (mmap(ptr, 4096, PROT_READ | PROT_WRITE,
+			 MAP_SHARED | MAP_FIXED, i915, arg.offset) == MAP_FAILED) {
+			munmap(ptr, 8192);
 			continue;
+		}
+
+		/* And our hole into the second slot. */
+		munmap(ptr + 4096, 4096);
 
 		memfd = open("/proc/self/mem", O_RDWR);
 		igt_require_f(memfd != -1, "/proc/self/mem\n");
@@ -497,9 +513,14 @@ test_oob_read(int i915)
 		ret = lseek(memfd, addr, SEEK_SET);
 		igt_assert_f(ret != -1, "lseek failed\n");
 
-		/* Triggering the buf (out of bound read) */
+		/*
+		 * Read across the end of the mapping. The read is either
+		 * rejected entirely, or completes partially for the remaining
+		 * 4 bytes in the mapping.
+		 */
 		ret = read(memfd, read_buf, 8);
-		igt_assert(ret == -1 && errno ==  EIO);
+		igt_assert_f((ret == -1 && errno == EIO) || ret == 4,
+			     "ret=%d errno=%d\n", ret, errno);
 
 		munmap(ptr, 4096);
 		close(memfd);
Signature · 1 line
2.56.1