The PGTY_guestmem_hugetlb is introduced so folios can be marked for further cleanup by guestmem_hugetlb. guestmem_hugetlb folios can have positive mapcounts, which will conflict with the installation of a page type. Hence, PGTY_guestmem_hugetlb will only be installed when a folio is truncated, after the folio has been unmapped and has a mapcount of 0. Signed-off-by: Fuad Tabba <tabba@xxxxxxxxxx> Acked-by: Vlastimil Babka <vbabka@xxxxxxx> Acked-by: David Hildenbrand <david@xxxxxxxxxx> Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx> Change-Id: I635f8929e06f73d7899737bd47090b7cbc7222dc --- include/linux/page-flags.h | 17 +++++++++++++++++ mm/Kconfig | 10 ++++++++++ mm/Makefile | 1 + mm/debug.c | 1 + mm/guestmem_hugetlb.c | 14 ++++++++++++++ mm/guestmem_hugetlb.h | 9 +++++++++ mm/swap.c | 9 +++++++++ 7 files changed, 61 insertions(+) create mode 100644 mm/guestmem_hugetlb.c create mode 100644 mm/guestmem_hugetlb.h diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 9dd60fb8c33f..543f6481ca60 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -965,6 +965,7 @@ enum pagetype { PGTY_zsmalloc = 0xf6, PGTY_unaccepted = 0xf7, PGTY_large_kmalloc = 0xf8, + PGTY_guestmem_hugetlb = 0xf9, PGTY_mapcount_underflow = 0xff }; @@ -1114,6 +1115,22 @@ FOLIO_TYPE_OPS(hugetlb, hugetlb) FOLIO_TEST_FLAG_FALSE(hugetlb) #endif +/* + * PGTY_guestmem_hugetlb, for now, is used to mark a folio as requiring further + * cleanup by the guestmem_hugetlb allocator. This page type is installed only + * at truncation time, by guest_memfd, if further cleanup is required. It is + * safe to install this page type at truncation time because by then mapcount + * would be 0. + * + * The plan is to always set this page type for any folios allocated by + * guestmem_hugetlb once typed folios can be mapped to userspace cleanly. + */ +#ifdef CONFIG_GUESTMEM_HUGETLB +FOLIO_TYPE_OPS(guestmem_hugetlb, guestmem_hugetlb) +#else +FOLIO_TEST_FLAG_FALSE(guestmem_hugetlb) +#endif + PAGE_TYPE_OPS(Zsmalloc, zsmalloc, zsmalloc) /* diff --git a/mm/Kconfig b/mm/Kconfig index e113f713b493..131adc49f58d 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1216,6 +1216,16 @@ config SECRETMEM memory areas visible only in the context of the owning process and not mapped to other processes and other kernel page tables. +config GUESTMEM_HUGETLB + bool "Enable guestmem_hugetlb allocator for guest_memfd" + depends on HUGETLBFS + help + Enable this to make HugeTLB folios available to guest_memfd + (KVM virtualization) as backing memory. + + This feature wraps HugeTLB as a custom allocator that + guest_memfd can use. + config ANON_VMA_NAME bool "Anonymous VMA name support" depends on PROC_FS && ADVISE_SYSCALLS && MMU diff --git a/mm/Makefile b/mm/Makefile index e7f6bbf8ae5f..c91c8e8fef71 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -127,6 +127,7 @@ obj-$(CONFIG_PAGE_EXTENSION) += page_ext.o obj-$(CONFIG_PAGE_TABLE_CHECK) += page_table_check.o obj-$(CONFIG_CMA_DEBUGFS) += cma_debug.o obj-$(CONFIG_SECRETMEM) += secretmem.o +obj-$(CONFIG_GUESTMEM_HUGETLB) += guestmem_hugetlb.o obj-$(CONFIG_CMA_SYSFS) += cma_sysfs.o obj-$(CONFIG_USERFAULTFD) += userfaultfd.o obj-$(CONFIG_IDLE_PAGE_TRACKING) += page_idle.o diff --git a/mm/debug.c b/mm/debug.c index db83e381a8ae..439ab128772d 100644 --- a/mm/debug.c +++ b/mm/debug.c @@ -56,6 +56,7 @@ static const char *page_type_names[] = { DEF_PAGETYPE_NAME(table), DEF_PAGETYPE_NAME(buddy), DEF_PAGETYPE_NAME(unaccepted), + DEF_PAGETYPE_NAME(guestmem_hugetlb), }; static const char *page_type_name(unsigned int page_type) diff --git a/mm/guestmem_hugetlb.c b/mm/guestmem_hugetlb.c new file mode 100644 index 000000000000..51a724ebcc50 --- /dev/null +++ b/mm/guestmem_hugetlb.c @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * guestmem_hugetlb is an allocator for guest_memfd. guest_memfd wraps HugeTLB + * as an allocator for guest_memfd. + */ + +#include <linux/mm_types.h> + +#include "guestmem_hugetlb.h" + +void guestmem_hugetlb_handle_folio_put(struct folio *folio) +{ + WARN_ONCE(1, "A placeholder that shouldn't trigger. Work in progress."); +} diff --git a/mm/guestmem_hugetlb.h b/mm/guestmem_hugetlb.h new file mode 100644 index 000000000000..5c9452b77252 --- /dev/null +++ b/mm/guestmem_hugetlb.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_MM_GUESTMEM_HUGETLB_H +#define _LINUX_MM_GUESTMEM_HUGETLB_H + +#include <linux/mm_types.h> + +void guestmem_hugetlb_handle_folio_put(struct folio *folio); + +#endif diff --git a/mm/swap.c b/mm/swap.c index d0a5971787c4..2747230ced89 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -40,6 +40,10 @@ #include "internal.h" +#ifdef CONFIG_GUESTMEM_HUGETLB +#include "guestmem_hugetlb.h" +#endif + #define CREATE_TRACE_POINTS #include <trace/events/pagemap.h> @@ -101,6 +105,11 @@ static void free_typed_folio(struct folio *folio) case PGTY_hugetlb: free_huge_folio(folio); return; +#endif +#ifdef CONFIG_GUESTMEM_HUGETLB + case PGTY_guestmem_hugetlb: + guestmem_hugetlb_handle_folio_put(folio); + return; #endif default: WARN_ON_ONCE(1); -- 2.49.0.1045.g170613ef41-goog