mm: introduce memfd_secret system call to create "secret" memory areas
Subsystems (14)
Mike Rapoport
maintainerv5 cover mm: introduce memfd_secret system call to create "secret" memory areas
From: Mike Rapoport <[email protected]>
Hi,
This is an implementation of "secret" mappings backed by a file descriptor. I've dropped the boot time reservation patch for now as it is not strictly required for the basic usage and can be easily added later either with or without CMA.
v5 changes:
* rebase on v5.9-rc5
* drop boot time memory reservation patch
v4 changes:
* rebase on v5.9-rc1
* Do not redefine PMD_PAGE_ORDER in fs/dax.c, thanks Kirill
* Make secret mappings exclusive by default and only require flags to memfd_secret() system call for uncached mappings, thanks again Kirill :)
v3 changes:
* Squash kernel-parameters.txt update into the commit that added the command line option.
* Make uncached mode explicitly selectable by architectures. For now enable it only on x86.
v2 changes:
* Follow Michael's suggestion and name the new system call 'memfd_secret'
* Add kernel-parameters documentation about the boot option
* Fix i386-tinyconfig regression reported by the kbuild bot. CONFIG_SECRETMEM now depends on !EMBEDDED to disable it on small systems from one side and still make it available unconditionally on architectures that support SET_DIRECT_MAP.
The file descriptor backing secret memory mappings is created using a dedicated memfd_secret system call The desired protection mode for the memory is configured using flags parameter of the system call. The mmap() of the file descriptor created with memfd_secret() will create a "secret" memory mapping. The pages in that mapping will be marked as not present in the direct map and will have desired protection bits set in the user page table. For instance, current implementation allows uncached mappings.
Although normally Linux userspace mappings are protected from other users, such secret mappings are useful for environments where a hostile tenant is trying to trick the kernel into giving them access to other tenants mappings.
Additionally, the secret mappings may be used as a mean to protect guest memory in a virtual machine host.
For demonstration of secret memory usage we've created a userspace library
[1] that does two things: the first is act as a preloader for openssl to redirect all the OPENSSL_malloc calls to secret memory meaning any secret keys get automatically protected this way and the other thing it does is expose the API to the user who needs it. We anticipate that a lot of the use cases would be like the openssl one: many toolkits that deal with secret keys already have special handling for the memory to try to give them greater protection, so this would simply be pluggable into the toolkits without any need for user application modification.
I've hesitated whether to continue to use new flags to memfd_create() or to add a new system call and I've decided to use a new system call after I've started to look into man pages update. There would have been two completely independent descriptions and I think it would have been very confusing.
Hiding secret memory mappings behind an anonymous file allows (ab)use of the page cache for tracking pages allocated for the "secret" mappings as well as using address_space_operations for e.g. page migration callbacks.
The anonymous file may be also used implicitly, like hugetlb files, to implement mmap(MAP_SECRET) and use the secret memory areas with "native" mm ABIs in the future.
As the fragmentation of the direct map was one of the major concerns raised during the previous postings, I've added an amortizing cache of PMD-size pages to each file descriptor that is used as an allocation pool for the secret memory areas.
v4: https://lore.kernel.org/lkml/[email protected]
v3: https://lore.kernel.org/lkml/[email protected]
v2: https://lore.kernel.org/lkml/[email protected]
v1: https://lore.kernel.org/lkml/[email protected]/
rfc-v2: https://lore.kernel.org/lkml/[email protected]/
rfc-v1: https://lore.kernel.org/lkml/20200130162340.GA14232@rapoport-lnx/
Mike Rapoport (5):
mm: add definition of PMD_PAGE_ORDER
mmap: make mlock_future_check() global
mm: introduce memfd_secret system call to create "secret" memory areas arch, mm: wire up memfd_secret system call were relevant
mm: secretmem: use PMD-size pages to amortize direct map fragmentation
arch/Kconfig | 7 + arch/arm64/include/asm/unistd.h | 2 +- arch/arm64/include/asm/unistd32.h | 2 + arch/arm64/include/uapi/asm/unistd.h | 1 + arch/riscv/include/asm/unistd.h | 1 + arch/x86/Kconfig | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + fs/dax.c | 11 +- include/linux/pgtable.h | 3 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/unistd.h | 7 +- include/uapi/linux/magic.h | 1 + include/uapi/linux/secretmem.h | 8 + kernel/sys_ni.c | 2 + mm/Kconfig | 4 + mm/Makefile | 1 + mm/internal.h | 3 + mm/mmap.c | 5 +- mm/secretmem.c | 333 +++++++++++++++++++++++++ 20 files changed, 383 insertions(+), 12 deletions(-) create mode 100644 include/uapi/linux/secretmem.h create mode 100644 mm/secretmem.c
Signature · 1 line
2.28.0
Mike Rapoport
maintainerPATCH v5 1/5 mm: add definition of PMD_PAGE_ORDER
From: Mike Rapoport <[email protected]>
The definition of PMD_PAGE_ORDER denoting the number of base pages in the second-level leaf page is already used by DAX and maybe handy in other cases as well.
Several architectures already have definition of PMD_ORDER as the size of second level page table, so to avoid conflict with these definitions use PMD_PAGE_ORDER name and update DAX respectively.
--- fs/dax.c | 11 ++++------- include/linux/pgtable.h | 3 +++ 2 files changed, 7 insertions(+), 7 deletions(-)
fs/dax.c +4 -7
@@ -49,9 +49,6 @@ static inline unsigned int pe_order(enum page_entry_size pe_size)#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)#define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT) -/* The order of a PMD entry */ -#define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT) -static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES ];static int __init init_dax_wait_table (void )
@@ -98,7 +95,7 @@ static bool dax_is_locked(void *entry)static unsigned int dax_entry_order (void *entry) {if (xa_to_value (entry) &DAX_PMD ) -return PMD_ORDER ; +return PMD_PAGE_ORDER ;return 0 ; }
@@ -1455,7 +1452,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, {struct vm_area_struct *vma = vmf->vma;struct address_space *mapping = vma->vm_file->f_mapping; -XA_STATE_ORDER (xas, &mapping->i_pages, vmf->pgoff,PMD_ORDER ); +XA_STATE_ORDER (xas, &mapping->i_pages, vmf->pgoff,PMD_PAGE_ORDER );unsigned long pmd_addr = vmf->address &PMD_MASK ;bool write = vmf->flags &FAULT_FLAG_WRITE ;bool sync;
@@ -1514,7 +1511,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,* entry is already in the array, for instance), it will return * VM_FAULT_FALLBACK. */ - entry =grab_mapping_entry (&xas, mapping,PMD_ORDER ); + entry =grab_mapping_entry (&xas, mapping,PMD_PAGE_ORDER );if (xa_is_internal (entry)) { result =xa_to_internal (entry); goto fallback;
@@ -1680,7 +1677,7 @@ dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order)if (order ==0 ) ret =vmf_insert_mixed_mkwrite (vmf->vma, vmf->address, pfn);#ifdef CONFIG_FS_DAX_PMD - else if (order == PMD_ORDER) + else if (order == PMD_PAGE_ORDER) ret =vmf_insert_pfn_pmd (vmf, pfn,FAULT_FLAG_WRITE );#endif else
include/linux/pgtable.h +3 -0
@@ -28,6 +28,9 @@#define USER_PGTABLES_CEILING 0UL#endif +/* Number of base pages in a second level leaf page */ +#define PMD_PAGE_ORDER (PMD_SHIFT - PAGE_SHIFT) +/* * A page table page can be thought of an array like this: pXd_t[PTRS_PER_PxD] *
Signature · 1 line
2.28.0
Mike Rapoport
maintainerPATCH v5 2/5 mmap: make mlock_future_check() global
In reply to Mike RapoportFrom: Mike Rapoport <[email protected]>
It will be used by the upcoming secret memory implementation.
--- mm/internal.h | 3 +++ mm/mmap.c | 5 ++--- 2 files changed, 5 insertions(+), 3 deletions(-)
mm/internal.h +3 -0
@@ -350,6 +350,9 @@ static inline void munlock_vma_pages_all(struct vm_area_struct *vma)extern void mlock_vma_page (struct page *page);extern unsigned int munlock_vma_page (struct page *page); +extern int mlock_future_check (struct mm_struct *mm,unsigned long flags, +unsigned long len); +/* * Clear the page's PageMlocked(). This can be useful in a situation where * we want to unconditionally remove a page from the pagecache -- e.g.,
mm/mmap.c +2 -3
@@ -1310,9 +1310,8 @@ static inline unsigned long round_hint_to_min(unsigned long hint)return hint; } -static inline int mlock_future_check (struct mm_struct *mm, -unsigned long flags, -unsigned long len) +int mlock_future_check (struct mm_struct *mm,unsigned long flags, +unsigned long len) {unsigned long locked, lock_limit;
Signature · 4 lines
2.28.0 _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Mike Rapoport
maintainerPATCH v5 3/5 mm: introduce memfd_secret system call to create "secret" memory areas
In reply to Mike RapoportFrom: Mike Rapoport <[email protected]>
Introduce "memfd_secret" system call with the ability to create memory areas visible only in the context of the owning process and not mapped not only to other processes but in the kernel page tables as well.
The user will create a file descriptor using the memfd_secret() system call where flags supplied as a parameter to this system call will define the desired protection mode for the memory associated with that file descriptor.
Currently there are two protection modes:
* exclusive - the memory area is unmapped from the kernel direct map and it is present only in the page tables of the owning mm.
* uncached - the memory area is present only in the page tables of the owning mm and it is mapped there as uncached.
The "exclusive" mode is enabled implicitly and it is the default mode for memfd_secret().
The "uncached" mode requires architecture support and an architecture should opt-in for this mode using HAVE_SECRETMEM_UNCACHED configuration option.
For instance, the following example will create an uncached mapping (error handling is omitted):
fd = memfd_secret(SECRETMEM_UNCACHED); ftruncate(fd, MAP_SIZE); ptr = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
--- arch/Kconfig | 7 + arch/x86/Kconfig | 1 + include/uapi/linux/magic.h | 1 + include/uapi/linux/secretmem.h | 8 + kernel/sys_ni.c | 2 + mm/Kconfig | 4 + mm/Makefile | 1 + mm/secretmem.c | 264 +++++++++++++++++++++++++++++++++ 8 files changed, 288 insertions(+) create mode 100644 include/uapi/linux/secretmem.h create mode 100644 mm/secretmem.c
arch/Kconfig +7 -0
@@ -975,6 +975,13 @@ config HAVE_SPARSE_SYSCALL_NR config ARCH_HAS_VDSO_DATA bool +config HAVE_SECRETMEM_UNCACHED + bool + help + An architecture can select this if its semantics of non-cached + mappings can be used to prevent speculative loads and it is + useful for secret protection. + source "kernel/gcov/Kconfig" source "scripts/gcc-plugins/Kconfig"
arch/x86/Kconfig +1 -0
@@ -220,6 +220,7 @@ config X86 select HAVE_UNSTABLE_SCHED_CLOCK select HAVE_USER_RETURN_NOTIFIER select HAVE_GENERIC_VDSO + select HAVE_SECRETMEM_UNCACHED select HOTPLUG_SMT if SMP select IRQ_FORCED_THREADING select NEED_SG_DMA_LENGTH
include/uapi/linux/magic.h +1 -0
@@ -97,5 +97,6 @@#define DEVMEM_MAGIC 0x454d444d/* "DMEM" */ #define Z3FOLD_MAGIC 0x33#define PPC_CMM_MAGIC 0xc7571590 +#define SECRETMEM_MAGIC 0x5345434d/* "SECM" */ #endif /* __LINUX_MAGIC_H__ */
include/uapi/linux/secretmem.h new file +8 -0
@@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_LINUX_SECRERTMEM_H +#define _UAPI_LINUX_SECRERTMEM_H + +/* secretmem operation modes */ +#define SECRETMEM_UNCACHED 0x1 + +#endif /* _UAPI_LINUX_SECRERTMEM_H */
kernel/sys_ni.c +2 -0
@@ -349,6 +349,8 @@ COND_SYSCALL(pkey_mprotect);COND_SYSCALL (pkey_alloc);COND_SYSCALL (pkey_free); +/* memfd_secret */ +COND_SYSCALL (memfd_secret);/* * Architecture specific weak syscall entries.
mm/Kconfig +4 -0
@@ -868,4 +868,8 @@ config ARCH_HAS_HUGEPD config MAPPING_DIRTY_HELPERS bool +config SECRETMEM + def_bool ARCH_HAS_SET_DIRECT_MAP && !EMBEDDED + select GENERIC_ALLOCATOR + endmenu
mm/Makefile +1 -0
@@ -121,3 +121,4 @@ obj-$(CONFIG_MEMFD_CREATE) += memfd.o obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o obj-$(CONFIG_PTDUMP_CORE) += ptdump.o obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o +obj-$(CONFIG_SECRETMEM) += secretmem.o
mm/secretmem.c new file +264 -0
264 lines not on this page · raw message
Signature · 1 line
2.28.0
Hi Mike,
Quoted 22 lines · Mike Rapoport
From: Mike Rapoport <[email protected]>
Signed-off-by Mike Rapoport <[email protected]>--- arch/Kconfig | 7 + arch/x86/Kconfig | 1 + include/uapi/linux/magic.h | 1 + include/uapi/linux/secretmem.h | 8 + kernel/sys_ni.c | 2 + mm/Kconfig | 4 + mm/Makefile | 1 + mm/secretmem.c | 264 +++++++++++++++++++++++++++++++++ 8 files changed, 288 insertions(+) create mode 100644 include/uapi/linux/secretmem.h create mode 100644 mm/secretmem.carch/Kconfig +0
-0
arch/Kconfig config HAVE_SPARSE_SYSCALL_NR · line 983
Quoted 9 lines
arch/Kconfig +6
-0@@ -975,6 +975,13 @@ config HAVE_SPARSE_SYSCALL_NR config ARCH_HAS_VDSO_DATA bool +config HAVE_SECRETMEM_UNCACHED + bool + help + An architecture can select this if its semantics of non-cached + mappings can be used to prevent speculative loads and it is + useful for secret protection.
Please use tabs instead of spaces for indentation.
Quoted 10 lines
arch/Kconfig +1
-0+ source "kernel/gcov/Kconfig" source "scripts/gcc-plugins/Kconfig"mm/Kconfig +0
-0
mm/Kconfig config ARCH_HAS_HUGEPD · line 872
Quoted 5 lines
mm/Kconfig +2
-0@@ -868,4 +868,8 @@ config ARCH_HAS_HUGEPD config MAPPING_DIRTY_HELPERS bool +config SECRETMEM + def_bool ARCH_HAS_SET_DIRECT_MAP && !EMBEDDED
Use tab above for indentation.
mm/Kconfig config ARCH_HAS_HUGEPD · line 875
Quoted 3 lines
mm/Kconfig +2
-0+ select GENERIC_ALLOCATOR + endmenu
thanks.
Signature · 4 lines
~Randy _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Mike Rapoport
maintainerPATCH v5 4/5 arch, mm: wire up memfd_secret system call were relevant
In reply to Mike RapoportFrom: Mike Rapoport <[email protected]>
Wire up memfd_secret system call on architectures that define ARCH_HAS_SET_DIRECT_MAP, namely arm64, risc-v and x86.
--- arch/arm64/include/asm/unistd.h | 2 +- arch/arm64/include/asm/unistd32.h | 2 ++ arch/arm64/include/uapi/asm/unistd.h | 1 + arch/riscv/include/asm/unistd.h | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/unistd.h | 7 ++++++- 8 files changed, 14 insertions(+), 2 deletions(-)
arch/arm64/include/asm/unistd.h +1 -1
@@ -38,7 +38,7 @@#define __ARM_NR_compat_set_tls (__ARM_NR_COMPAT_BASE + 5)#define __ARM_NR_COMPAT_END (__ARM_NR_COMPAT_BASE + 0x800) -#define __NR_compat_syscalls 440 +#define __NR_compat_syscalls 441#endif #define __ARCH_WANT_SYS_CLONE
arch/arm64/include/asm/unistd32.h +2 -0
@@ -887,6 +887,8 @@ __SYSCALL(__NR_openat2, sys_openat2) __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd)#define __NR_faccessat2 439 __SYSCALL(__NR_faccessat2, sys_faccessat2) +#define __NR_memfd_secret 440 +__SYSCALL (__NR_memfd_secret, sys_memfd_secret)/* * Please add new compat syscalls above this comment and update
arch/arm64/include/uapi/asm/unistd.h +1 -0
@@ -20,5 +20,6 @@#define __ARCH_WANT_SET_GET_RLIMIT#define __ARCH_WANT_TIME32_SYSCALLS#define __ARCH_WANT_SYS_CLONE3 +#define __ARCH_WANT_MEMFD_SECRET#include <asm-generic/unistd.h>
arch/riscv/include/asm/unistd.h +1 -0
@@ -9,6 +9,7 @@*/ #define __ARCH_WANT_SYS_CLONE +#define __ARCH_WANT_MEMFD_SECRET#include <uapi/asm/unistd.h>
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
@@ -444,3 +444,4 @@ 437 i386 openat2 sys_openat2 438 i386 pidfd_getfd sys_pidfd_getfd 439 i386 faccessat2 sys_faccessat2 +440 i386 memfd_secret sys_memfd_secret
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -361,6 +361,7 @@ 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 +440 common memfd_secret sys_memfd_secret # # x32-specific system call numbers start at 512 to avoid cache impact
include/linux/syscalls.h +1 -0
@@ -1006,6 +1006,7 @@ asmlinkage long sys_pidfd_send_signal(int pidfd, int sig,siginfo_t __user *info, unsigned int flags);asmlinkage long sys_pidfd_getfd (int pidfd,int fd,unsigned int flags); +asmlinkage long sys_memfd_secret (unsigned long flags);/* * Architecture-specific system calls
include/uapi/asm-generic/unistd.h +6 -1
@@ -860,8 +860,13 @@ __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd)#define __NR_faccessat2 439 __SYSCALL(__NR_faccessat2, sys_faccessat2) +#ifdef __ARCH_WANT_MEMFD_SECRET +#define __NR_memfd_secret 440 +__SYSCALL(__NR_memfd_secret, sys_memfd_secret) +#endif +#undef __NR_syscalls -#define __NR_syscalls 440 +#define __NR_syscalls 441/* * 32 bit systems traditionally used different
Signature · 1 line
2.28.0
Mike Rapoport
maintainerPATCH v5 5/5 mm: secretmem: use PMD-size pages to amortize direct map fragmentation
In reply to Mike RapoportFrom: Mike Rapoport <[email protected]>
Removing a PAGE_SIZE page from the direct map every time such page is allocated for a secret memory mapping will cause severe fragmentation of the direct map. This fragmentation can be reduced by using PMD-size pages as a pool for small pages for secret memory mappings.
Add a gen_pool per secretmem inode and lazily populate this pool with PMD-size pages.
--- mm/secretmem.c | 107 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 88 insertions(+), 19 deletions(-)
mm/secretmem.c +88 -19
@@ -12,6 +12,7 @@#include <linux/bitops.h> #include <linux/printk.h> #include <linux/pagemap.h> +#include <linux/genalloc.h> #include <linux/syscalls.h> #include <linux/pseudo_fs.h> #include <linux/set_memory.h>
@@ -40,24 +41,66 @@#define SECRETMEM_FLAGS_MASK SECRETMEM_MODE_MASKstruct secretmem_ctx { +struct gen_pool *pool;unsigned int mode; }; -static struct page *secretmem_alloc_page (gfp_t gfp) +static int secretmem_pool_increase (struct secretmem_ctx *ctx,gfp_t gfp) { -/* -* FIXME: use a cache of large pages to reduce the direct map -* fragmentation -*/ -return alloc_page (gfp); +unsigned long nr_pages = (1 <<PMD_PAGE_ORDER ); +struct gen_pool *pool = ctx->pool; +unsigned long addr; +struct page *page; +int err; + + page =alloc_pages (gfp,PMD_PAGE_ORDER ); +if (!page) +return -ENOMEM ; + + addr = (unsigned long )page_address (page); +split_page (page,PMD_PAGE_ORDER ); + + err =gen_pool_add (pool, addr,PMD_SIZE ,NUMA_NO_NODE ); +if (err) { +__free_pages (page,PMD_PAGE_ORDER ); +return err; + } + +__kernel_map_pages (page, nr_pages,0 ); + +return 0 ; +} + +static struct page *secretmem_alloc_page (struct secretmem_ctx *ctx, +gfp_t gfp) +{ +struct gen_pool *pool = ctx->pool; +unsigned long addr; +struct page *page; +int err; + +if (gen_pool_avail (pool) <PAGE_SIZE ) { + err =secretmem_pool_increase (ctx, gfp); +if (err) +return NULL ; + } + + addr =gen_pool_alloc (pool,PAGE_SIZE ); +if (!addr) +return NULL ; + + page =virt_to_page (addr); +get_page (page); + +return page; }static vm_fault_t secretmem_fault (struct vm_fault *vmf) { +struct secretmem_ctx *ctx = vmf->vma->vm_file->private_data;struct address_space *mapping = vmf->vma->vm_file->f_mapping;struct inode *inode =file_inode (vmf->vma->vm_file);pgoff_t offset = vmf->pgoff; -unsigned long addr;struct page *page;int ret =0 ;
@@ -66,7 +109,7 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf) page =find_get_entry (mapping, offset);if (!page) { - page =secretmem_alloc_page (vmf->gfp_mask); + page =secretmem_alloc_page (ctx, vmf->gfp_mask);if (!page)return vmf_error (-ENOMEM );
@@ -74,14 +117,8 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf)if (unlikely (ret)) goto err_put_page; - ret =set_direct_map_invalid_noflush (page); -if (ret) - goto err_del_page_cache; - - addr = (unsigned long )page_address (page); -flush_tlb_kernel_range (addr, addr +PAGE_SIZE ); -__SetPageUptodate (page); +set_page_private (page, (unsigned long )ctx); ret =VM_FAULT_LOCKED ; }
@@ -89,8 +126,6 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf) vmf->page = page;return ret; -err_del_page_cache: -delete_from_page_cache (page); err_put_page:put_page (page);return vmf_error (ret);
@@ -138,7 +173,11 @@ static int secretmem_migratepage(struct address_space *mapping,static void secretmem_freepage (struct page *page) { -set_direct_map_default_noflush (page); +unsigned long addr = (unsigned long )page_address (page); +struct secretmem_ctx *ctx = (struct secretmem_ctx *)page_private (page); +struct gen_pool *pool = ctx->pool; + +gen_pool_free (pool, addr,PAGE_SIZE ); }static const struct address_space_operations secretmem_aops = {
@@ -163,13 +202,18 @@ static struct file *secretmem_file_create(unsigned long flags)if (!ctx) goto err_free_inode; + ctx->pool =gen_pool_create (PAGE_SHIFT ,NUMA_NO_NODE ); +if (!ctx->pool) + goto err_free_ctx; + file =alloc_file_pseudo (inode, secretmem_mnt,"secretmem" ,O_RDWR , &secretmem_fops);if (IS_ERR (file)) - goto err_free_ctx; + goto err_free_pool;mapping_set_unevictable (inode->i_mapping); + inode->i_private = ctx; inode->i_mapping->private_data = ctx; inode->i_mapping->a_ops = &secretmem_aops;
@@ -183,6 +227,8 @@ static struct file *secretmem_file_create(unsigned long flags)return file; +err_free_pool: +gen_pool_destroy (ctx->pool); err_free_ctx:kfree (ctx); err_free_inode:
@@ -221,11 +267,34 @@ SYSCALL_DEFINE1(memfd_secret, unsigned long, flags)return err; } +static void secretmem_cleanup_chunk (struct gen_pool *pool, +struct gen_pool_chunk *chunk,void *data) +{ +unsigned long start = chunk->start_addr; +unsigned long end = chunk->end_addr; +unsigned long nr_pages, addr; + + nr_pages = (end - start +1 ) /PAGE_SIZE ; +__kernel_map_pages (virt_to_page (start), nr_pages,1 ); + +for (addr = start; addr < end; addr +=PAGE_SIZE ) +put_page (virt_to_page (addr)); +} + +static void secretmem_cleanup_pool (struct secretmem_ctx *ctx) +{ +struct gen_pool *pool = ctx->pool; + +gen_pool_for_each_chunk (pool, secretmem_cleanup_chunk, ctx); +gen_pool_destroy (pool); +} +static void secretmem_evict_inode (struct inode *inode) {struct secretmem_ctx *ctx = inode->i_private;truncate_inode_pages_final (&inode->i_data); +secretmem_cleanup_pool (ctx);clear_inode (inode);kfree (ctx); }
Signature · 4 lines
2.28.0 _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Andrew Morton
maintainerQuoted 4 lines · On Wed, 16 Sep 2020 10:35:34 +0300 Mike Rapoport <[email protected]> wrote:
This is an implementation of "secret" mappings backed by a file descriptor. I've dropped the boot time reservation patch for now as it is not strictly required for the basic usage and can be easily added later either with or without CMA.
It seems early days for this, especially as regards reviewer buyin. But I'll toss it in there to get it some additional testing.
A test suite in tools/testging/selftests/ would be helpful, especially for arch maintainers.
I assume that user-facing manpage alterations are planned?
Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Wed, 16 Sep 2020 10:35:34 +0300 Mike Rapoport <[email protected]> wrote:
This is an implementation of "secret" mappings backed by a file descriptor. I've dropped the boot time reservation patch for now as it is not strictly required for the basic usage and can be easily added later either with or without CMA.
Quoted 7 lines
It seems early days for this, especially as regards reviewer buyin. But I'll toss it in there to get it some additional testing.
A test suite in
tools/testging/selftests/would be helpful, especially for arch maintainers.I assume that user-facing manpage alterations are planned?
I was just about to write a mail into this thread when I saw this :-).
So far, I don't think I saw a manual page patch. Mike, how about it?
Thanks,
Michael
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Mike Rapoport
maintainerQuoted 12 lines · Michael Kerrisk (man-pages)
Quoted 11 lines · Andrew Morton
Quoted 4 lines · On Wed, 16 Sep 2020 10:35:34 +0300 Mike Rapoport <[email protected]> wrote:
This is an implementation of "secret" mappings backed by a file descriptor. I've dropped the boot time reservation patch for now as it is not strictly required for the basic usage and can be easily added later either with or without CMA.
It seems early days for this, especially as regards reviewer buyin. But I'll toss it in there to get it some additional testing.
Quoted 8 lines
Quoted 4 lines
A test suite in
tools/testging/selftests/would be helpful, especially for arch maintainers.I assume that user-facing manpage alterations are planned?
I was just about to write a mail into this thread when I saw this :-).
So far, I don't think I saw a manual page patch. Mike, how about it?
It is planned :)
I have a draft, but I'm waiting for consensus about the uncached mappings before sending it out.
Signature · 5 lines
Sincerely yours, Mike. _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Mike Rapoport
maintainerQuoted 9 lines · Andrew Morton
Quoted 4 lines · On Wed, 16 Sep 2020 10:35:34 +0300 Mike Rapoport <[email protected]> wrote:
This is an implementation of "secret" mappings backed by a file descriptor. I've dropped the boot time reservation patch for now as it is not strictly required for the basic usage and can be easily added later either with or without CMA.
It seems early days for this, especially as regards reviewer buyin. But I'll toss it in there to get it some additional testing.
Thanks!
Quoted 2 lines
A test suite in
tools/testging/selftests/would be helpful, especially for arch maintainers.
I'll look into it.
Quoted 1 line
I assume that user-facing manpage alterations are planned?
Of course.
Signature · 2 lines
Sincerely yours, Mike.
Quoted 8 lines · Mike Rapoport
From: Mike Rapoport <[email protected]>
Hi,
This is an implementation of "secret" mappings backed by a file descriptor. I've dropped the boot time reservation patch for now as it is not strictly required for the basic usage and can be easily added later either with or without CMA.
On powerpc: https://gitlab.com/cailca/linux-mm/-/blob/master/powerpc.config
There is a compiling warning from the today's linux-next:
<stdin>:1532:2: warning: #warning syscall memfd_secret not implemented [-Wcpp]
Quoted 10 lines · Qian Cai
Quoted 8 lines · Mike Rapoport
From: Mike Rapoport <[email protected]>
Hi,
This is an implementation of "secret" mappings backed by a file descriptor. I've dropped the boot time reservation patch for now as it is not strictly required for the basic usage and can be easily added later either with or without CMA.
Quoted 5 lines
On powerpc: https://gitlab.com/cailca/linux-mm/-/blob/master/powerpc.config
There is a compiling warning from the today's linux-next:
<stdin>:1532:2: warning: #warning syscall
memfd_secretnot implemented [-Wcpp]
This should silence the warning:
scripts/checksyscalls.sh +4 -0
@@ -40,6 +40,10 @@ cat << EOF #define __IGNORE_setrlimit /* setrlimit */ #endif +#ifndef __ARCH_WANT_MEMFD_SECRET +#define __IGNORE_memfd_secret +#endif + /* Missing flags argument */ #define __IGNORE_renameat /* renameat2 */
Hi all,
Quoted 23 lines · On Fri, 18 Sep 2020 14:25:15 -0400 Qian Cai <[email protected]> wrote:
Quoted 8 lines · Qian Cai
Quoted 6 lines · Mike Rapoport
From: Mike Rapoport <[email protected]>
This is an implementation of "secret" mappings backed by a file descriptor. I've dropped the boot time reservation patch for now as it is not strictly required for the basic usage and can be easily added later either with or without CMA.
Quoted 5 lines
On powerpc: https://gitlab.com/cailca/linux-mm/-/blob/master/powerpc.config
There is a compiling warning from the today's linux-next:
<stdin>:1532:2: warning: #warning syscall
memfd_secretnot implemented [-Wcpp]This should silence the warning:
scripts/checksyscalls.sh +0
-0
scripts/checksyscalls.sh cat << EOF · line 49
Quoted 10 lines
scripts/checksyscalls.sh +4
-0@@ -40,6 +40,10 @@ cat << EOF #define __IGNORE_setrlimit /* setrlimit */ #endif +#ifndef __ARCH_WANT_MEMFD_SECRET +#define __IGNORE_memfd_secret +#endif + /* Missing flags argument */ #define __IGNORE_renameat /* renameat2 */
Added to linux-next today.
Signature · 2 lines
Cheers, Stephen Rothwell
_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]