Skip to contentSkip to conversation

mm-madvise-implement-lightweight-guard-page-mechanism.patch removed from -mm tree

merged mm-stable Andrew Morton 1 participant 1 message Nov 11, 2024

lore
Share & tools
lore

Andrew Morton

author tree notice Applied
#0 lore raw

The quilt patch titled
Subject: mm: madvise: implement lightweight guard page mechanism
has been removed from the -mm tree. Its filename was
mm-madvise-implement-lightweight-guard-page-mechanism.patch

This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: Lorenzo Stoakes <[email protected]>
Subject: mm: madvise: implement lightweight guard page mechanism
Date: Mon, 28 Oct 2024 14:13:29 +0000

Implement a new lightweight guard page feature, that is regions of userland virtual memory that, when accessed, cause a fatal signal to arise.

Currently users must establish PROT_NONE ranges to achieve this.

However this is very costly memory-wise - we need a VMA for each and every one of these regions AND they become unmergeable with surrounding VMAs.

In addition repeated mmap() calls require repeated kernel context switches and contention of the mmap lock to install these ranges, potentially also having to unmap memory if installed over existing ranges.

The lightweight guard approach eliminates the VMA cost altogether - rather than establishing a PROT_NONE VMA, it operates at the level of page table entries - establishing PTE markers such that accesses to them cause a fault followed by a SIGSGEV signal being raised.

This is achieved through the PTE marker mechanism, which we have already extended to provide PTE_MARKER_GUARD, which we installed via the generic page walking logic which we have extended for this purpose.

These guard ranges are established with MADV_GUARD_INSTALL. If the range in which they are installed contain any existing mappings, they will be zapped, i.e. free the range and unmap memory (thus mimicking the behaviour of MADV_DONTNEED in this respect).

Any existing guard entries will be left untouched. There is therefore no nesting of guarded pages.

Guarded ranges are NOT cleared by MADV_DONTNEED nor MADV_FREE (in both instances the memory range may be reused at which point a user would expect guards to still be in place), but they are cleared via MADV_GUARD_REMOVE, process teardown or unmapping of memory ranges.

The guard property can be removed from ranges via MADV_GUARD_REMOVE. The ranges over which this is applied, should they contain non-guard entries, will be untouched, with only guard entries being cleared.

We permit this operation on anonymous memory only, and only VMAs which are non-special, non-huge and not mlock()'d (if we permitted this we'd have to drop locked pages which would be rather counterintuitive).

Racing page faults can cause repeated attempts to install guard pages that are interrupted, result in a zap, and this process can end up being repeated. If this happens more than would be expected in normal operation, we rescind locks and retry the whole thing, which avoids lock contention in this scenario.

Cc Arnd Bergmann <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Chris Zankel <[email protected]>
Cc Helge Deller <[email protected]>
Cc James E.J. Bottomley <[email protected]>
Cc Jeff Xu <[email protected]>
Cc John Hubbard <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Matthew Wilcox (Oracle) <[email protected]>
Cc Matt Turner <[email protected]>
Cc Max Filippov <[email protected]>
Cc Muchun Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Sidhartha Kumar <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Thomas Bogendoerfer <[email protected]>

---

 arch/alpha/include/uapi/asm/mman.h     |    3 
 arch/mips/include/uapi/asm/mman.h      |    3 
 arch/parisc/include/uapi/asm/mman.h    |    3 
 arch/xtensa/include/uapi/asm/mman.h    |    3 
 include/uapi/asm-generic/mman-common.h |    3 
 mm/madvise.c                           |  239 +++++++++++++++++++++++
 mm/mseal.c                             |    1 
 7 files changed, 255 insertions(+)
arch/alpha/include/uapi/asm/mman.h renamed from arch/alpha/include/uapi/asm/mman.h~mm-madvise-implement-lightweight-guard-page-mechanism +3 -0
@@ -78,6 +78,9 @@
 
 #define MADV_COLLAPSE	25		/* Synchronous hugepage collapse */
 
+#define MADV_GUARD_INSTALL 102		/* fatal signal on access to range */
+#define MADV_GUARD_REMOVE 103		/* unguard range */
+
 /* compatibility flags */
 #define MAP_FILE	0
 
arch/mips/include/uapi/asm/mman.h renamed from arch/mips/include/uapi/asm/mman.h~mm-madvise-implement-lightweight-guard-page-mechanism +3 -0
@@ -105,6 +105,9 @@
 
 #define MADV_COLLAPSE	25		/* Synchronous hugepage collapse */
 
+#define MADV_GUARD_INSTALL 102		/* fatal signal on access to range */
+#define MADV_GUARD_REMOVE 103		/* unguard range */
+
 /* compatibility flags */
 #define MAP_FILE	0
 
arch/parisc/include/uapi/asm/mman.h renamed from arch/parisc/include/uapi/asm/mman.h~mm-madvise-implement-lightweight-guard-page-mechanism +3 -0
@@ -75,6 +75,9 @@
 #define MADV_HWPOISON     100		/* poison a page for testing */
 #define MADV_SOFT_OFFLINE 101		/* soft offline page for testing */
 
+#define MADV_GUARD_INSTALL 102		/* fatal signal on access to range */
+#define MADV_GUARD_REMOVE 103		/* unguard range */
+
 /* compatibility flags */
 #define MAP_FILE	0
 
arch/xtensa/include/uapi/asm/mman.h renamed from arch/xtensa/include/uapi/asm/mman.h~mm-madvise-implement-lightweight-guard-page-mechanism +3 -0
@@ -113,6 +113,9 @@
 
 #define MADV_COLLAPSE	25		/* Synchronous hugepage collapse */
 
+#define MADV_GUARD_INSTALL 102		/* fatal signal on access to range */
+#define MADV_GUARD_REMOVE 103		/* unguard range */
+
 /* compatibility flags */
 #define MAP_FILE	0
 
include/uapi/asm-generic/mman-common.h renamed from include/uapi/asm-generic/mman-common.h~mm-madvise-implement-lightweight-guard-page-mechanism +3 -0
@@ -79,6 +79,9 @@
 
 #define MADV_COLLAPSE	25		/* Synchronous hugepage collapse */
 
+#define MADV_GUARD_INSTALL 102		/* fatal signal on access to range */
+#define MADV_GUARD_REMOVE 103		/* unguard range */
+
 /* compatibility flags */
 #define MAP_FILE	0
 
mm/madvise.c renamed from mm/madvise.c~mm-madvise-implement-lightweight-guard-page-mechanism +239 -0

275 lines not on this page · raw message

mm/mseal.c renamed from mm/mseal.c~mm-madvise-implement-lightweight-guard-page-mechanism +1 -0
@@ -30,6 +30,7 @@ static bool is_madv_discard(int behavior
 	case MADV_REMOVE:
 	case MADV_DONTFORK:
 	case MADV_WIPEONFORK:
+	case MADV_GUARD_INSTALL:
 		return true;
 	}
 

_

Patches currently in -mm which might be from [email protected] are

docs-mm-add-vma-locks-documentation.patch
docs-mm-add-vma-locks-documentation-fix.patch