[PATCH] mm: introduce MADV_CLR_HUGEPAGE

Mike Rapoport
2017-05-22T06:12:53Z

Currently applications can explicitly enable or disable THP for a memory
region using MADV_HUGEPAGE or MADV_NOHUGEPAGE. However, once either of
these advises is used, the region will always have
VM_HUGEPAGE/VM_NOHUGEPAGE flag set in vma->vm_flags.
The MADV_CLR_HUGEPAGE resets both these flags and allows managing THP in
the region according to system-wide settings.

Signed-off-by: Mike Rapoport <rppt@linux.vnet.ibm.com>
---
include/uapi/asm-generic/mman-common.h | 3 +++
mm/khugepaged.c | 7 +++++++
mm/madvise.c | 5 +++++
3 files changed, 15 insertions(+)

diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h
index 8c27db0..3201712 100644
--- a/include/uapi/asm-generic/mman-common.h
+++ b/include/uapi/asm-generic/mman-common.h
@@ -58,6 +58,9 @@
overrides the coredump filter bits */
#define MADV_DODUMP 17 /* Clear the MADV_DONTDUMP flag */

+#define MADV_CLR_HUGEPAGE 18 /* Clear flags controlling backing with
+ hugepages */
+
/* compatibility flags */
#define MAP_FILE 0

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 945fd1c..b9ee9bb 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -336,6 +336,13 @@ int hugepage_madvise(struct vm_area_struct *vma,
* it got registered before VM_NOHUGEPAGE was set.
*/
break;
+ case MADV_CLR_HUGEPAGE:
+ *vm_flags &= ~(VM_HUGEPAGE | VM_NOHUGEPAGE);
+ /*
+ * The vma will be treated according to the
+ * system-wide settings in transparent_hugepage_flags
+ */
+ break;
}

return 0;
diff --git a/mm/madvise.c b/mm/madvise.c
index 25b78ee..ae650a3 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -105,6 +105,7 @@ static long madvise_behavior(struct vm_area_struct *vma,
break;
case MADV_HUGEPAGE:
case MADV_NOHUGEPAGE:
+ case MADV_CLR_HUGEPAGE:
error = hugepage_madvise(vma, &new_flags, behavior);
if (error) {
/*
@@ -684,6 +685,7 @@ madvise_behavior_valid(int behavior)
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
case MADV_HUGEPAGE:
case MADV_NOHUGEPAGE:
+ case MADV_CLR_HUGEPAGE:
#endif
case MADV_DONTDUMP:
case MADV_DODUMP:
@@ -739,6 +741,9 @@ madvise_behavior_valid(int behavior)
* MADV_NOHUGEPAGE - mark the given range as not worth being backed by
* transparent huge pages so the existing pages will not be
* coalesced into THP and new pages will not be allocated as THP.
+ * MADV_CLR_HUGEPAGE - clear MADV_HUGEPAGE/MADV_NOHUGEPAGE marking;
+ * the range will be treated by khugepaged according to the
+ * system wide settings
* MADV_DONTDUMP - the application wants to prevent pages in the given range
* from being included in its core dump.
* MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
--
2.7.4