From: Mike Rapoport <rppt@kernel.org> Date: 2020-07-06 17:21:01
From: Mike Rapoport <redacted>
Hi,
This is a second version of "secret" mappings implementation backed by a
file descriptor.
The file descriptor is created using memfd_create() syscall with a new
MFD_SECRET flag. The file descriptor should be configured using ioctl() to
define the desired protection and then mmap() of the fd 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.
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.
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 and an ability to reserve large chunks of the
physical memory at boot time and then use this memory as an allocation pool
for the secret memory areas.
In addition, I've tried to find some numbers that show the benefit of using
larger pages in the direct map, but I couldn't find anything so I've run a
couple of benchmarks from phoronix-test-suite on my laptop (i7-8650U with
32G RAM).
I've tested three variants: the default with 28G of the physical memory
covered with 1G pages, then I disabled 1G pages using "nogbpages" in the
kernel command line and at last I've forced the entire direct map to use 4K
pages using a simple patch to arch/x86/mm/init.c.
I've made runs of the benchmarks with SSD and tmpfs.
Surprisingly, the results does not show huge advantage for large pages. For
instance, here the results for kernel build with 'make -j8', in seconds:
| 1G | 2M | 4K
------------------------+--------+--------+---------
ssd, mitigations=on | 308.75 | 317.37 | 314.9
ssd, mitigations=off | 305.25 | 295.32 | 304.92
ram, mitigations=on | 301.58 | 322.49 | 306.54
ram, mitigations=off | 299.32 | 288.44 | 310.65
All the results I have are available at [1].
If anybody is interested in plain text, please let me know.
[1] https://docs.google.com/spreadsheets/d/1tdD-cu8e93vnfGsTFxZ5YdaEfs2E1GELlvWNOGkJV2U/edit?usp=sharing
Mike Rapoport (5):
mm: make HPAGE_PxD_{SHIFT,MASK,SIZE} always available
mmap: make mlock_future_check() global
mm: extend memfd with ability to create "secret" memory areas
mm: secretmem: use PMD-size pages to amortize direct map fragmentation
mm: secretmem: add ability to reserve memory at boot
include/linux/huge_mm.h | 10 +-
include/linux/memfd.h | 9 +
include/uapi/linux/magic.h | 1 +
include/uapi/linux/memfd.h | 6 +
mm/Kconfig | 3 +
mm/Makefile | 1 +
mm/internal.h | 3 +
mm/memfd.c | 10 +-
mm/mmap.c | 5 +-
mm/secretmem.c | 445 +++++++++++++++++++++++++++++++++++++
10 files changed, 480 insertions(+), 13 deletions(-)
create mode 100644 mm/secretmem.c
base-commit: 7c30b859a947535f2213277e827d7ac7dcff9c84
--
2.26.2
From: Mike Rapoport <rppt@kernel.org> Date: 2020-07-06 17:21:07
From: Mike Rapoport <redacted>
The definitions of shift, mask and size for the second and the third level
of the leaf pages are available only when CONFIG_TRANSPARENT_HUGEPAGE is
set. Otherwise they evaluate to BUILD_BUG().
There is no explanation neither in the code nor in the changelog why the
usage of, e.g. HPAGE_PMD_SIZE should be only allowed with THP and forbidden
otherwise while the definitions of HPAGE_PMD_SIZE and HPAGE_PUD_SIZE
express the sizes better than ambiguous HPAGE_SIZE.
Make HPAGE_PxD_{SHIFT,MASK,SIZE} definitions available unconditionally.
Signed-off-by: Mike Rapoport <redacted>
---
include/linux/huge_mm.h | 10 ++--------
1 file changed, 2 insertions(+), 8 deletions(-)
From: Mike Rapoport <rppt@kernel.org> Date: 2020-07-06 17:21:13
From: Mike Rapoport <redacted>
It will be used by the upcoming secret memory implementation.
Signed-off-by: Mike Rapoport <redacted>
---
mm/internal.h | 3 +++
mm/mmap.c | 5 ++---
2 files changed, 5 insertions(+), 3 deletions(-)
From: Mike Rapoport <rppt@kernel.org> Date: 2020-07-06 17:21:18
From: Mike Rapoport <redacted>
Extend memfd_create() 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_create system call.
The user than has to use ioctl() to define the desired protection mode for
the memory associated with that file descriptor and only when the mode is
set it is possible to mmap() the memory. For instance, the following
exapmple will create an uncached mapping (error handling is omitted):
fd = memfd_create("secret", MFD_SECRET);
ioctl(fd, MFD_SECRET_UNCACHED);
ftruncate(fd. MAP_SIZE);
ptr = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
fd, 0);
Signed-off-by: Mike Rapoport <redacted>
---
include/linux/memfd.h | 9 ++
include/uapi/linux/magic.h | 1 +
include/uapi/linux/memfd.h | 6 +
mm/Kconfig | 3 +
mm/Makefile | 1 +
mm/memfd.c | 10 +-
mm/secretmem.c | 247 +++++++++++++++++++++++++++++++++++++
7 files changed, 275 insertions(+), 2 deletions(-)
create mode 100644 mm/secretmem.c
@@ -0,0 +1,247 @@+// SPDX-License-Identifier: GPL-2.0+#include<linux/mm.h>+#include<linux/fs.h>+#include<linux/mount.h>+#include<linux/memfd.h>+#include<linux/printk.h>+#include<linux/pagemap.h>+#include<linux/pseudo_fs.h>+#include<linux/set_memory.h>+#include<linux/sched/signal.h>++#include<uapi/linux/memfd.h>+#include<uapi/linux/magic.h>++#include<asm/tlbflush.h>++#include"internal.h"++#undef pr_fmt+#define pr_fmt(fmt) "secretmem: " fmt++#define SECRETMEM_EXCLUSIVE 0x1+#define SECRETMEM_UNCACHED 0x2++structsecretmem_ctx{+unsignedintmode;+};++staticstructpage*secretmem_alloc_page(gfp_tgfp)+{+/*+*FIXME:useacacheoflargepagestoreducethedirectmap+*fragmentation+*/+returnalloc_page(gfp);+}++staticvm_fault_tsecretmem_fault(structvm_fault*vmf)+{+structaddress_space*mapping=vmf->vma->vm_file->f_mapping;+structinode*inode=file_inode(vmf->vma->vm_file);+pgoff_toffset=vmf->pgoff;+unsignedlongaddr;+structpage*page;+intret=0;++if(((loff_t)vmf->pgoff<<PAGE_SHIFT)>=i_size_read(inode))+returnvmf_error(-EINVAL);++page=find_get_entry(mapping,offset);+if(!page){+page=secretmem_alloc_page(vmf->gfp_mask);+if(!page)+returnvmf_error(-ENOMEM);++ret=add_to_page_cache_lru(page,mapping,offset,vmf->gfp_mask);+if(unlikely(ret))+gotoerr_put_page;++ret=set_direct_map_invalid_noflush(page);+if(ret)+gotoerr_del_page_cache;++addr=(unsignedlong)page_address(page);+flush_tlb_kernel_range(addr,addr+PAGE_SIZE);++__SetPageUptodate(page);++ret=VM_FAULT_LOCKED;+}++vmf->page=page;+returnret;++err_del_page_cache:+delete_from_page_cache(page);+err_put_page:+put_page(page);+returnvmf_error(ret);+}++staticconststructvm_operations_structsecretmem_vm_ops={+.fault=secretmem_fault,+};++staticintsecretmem_mmap(structfile*file,structvm_area_struct*vma)+{+structsecretmem_ctx*ctx=file->private_data;+unsignedlongmode=ctx->mode;+unsignedlonglen=vma->vm_end-vma->vm_start;++if(!mode)+return-EINVAL;++if(mlock_future_check(vma->vm_mm,vma->vm_flags|VM_LOCKED,len))+return-EAGAIN;++switch(mode){+caseSECRETMEM_UNCACHED:+vma->vm_page_prot=pgprot_noncached(vma->vm_page_prot);+fallthrough;+caseSECRETMEM_EXCLUSIVE:+vma->vm_ops=&secretmem_vm_ops;+break;+default:+return-EINVAL;+}++vma->vm_flags|=VM_LOCKED;++return0;+}++staticlongsecretmem_ioctl(structfile*file,unsignedcmd,unsignedlongarg)+{+structsecretmem_ctx*ctx=file->private_data;+unsignedlongmode=ctx->mode;++if(mode)+return-EINVAL;++switch(cmd){+caseMFD_SECRET_EXCLUSIVE:+mode=SECRETMEM_EXCLUSIVE;+break;+caseMFD_SECRET_UNCACHED:+mode=SECRETMEM_UNCACHED;+break;+default:+return-EINVAL;+}++ctx->mode=mode;++return0;+}++conststructfile_operationssecretmem_fops={+.mmap=secretmem_mmap,+.unlocked_ioctl=secretmem_ioctl,+.compat_ioctl=secretmem_ioctl,+};++staticboolsecretmem_isolate_page(structpage*page,isolate_mode_tmode)+{+returnfalse;+}++staticintsecretmem_migratepage(structaddress_space*mapping,+structpage*newpage,structpage*page,+enummigrate_modemode)+{+return-EBUSY;+}++staticvoidsecretmem_freepage(structpage*page)+{+set_direct_map_default_noflush(page);+}++staticconststructaddress_space_operationssecretmem_aops={+.freepage=secretmem_freepage,+.migratepage=secretmem_migratepage,+.isolate_page=secretmem_isolate_page,+};++staticstructvfsmount*secretmem_mnt;++structfile*secretmem_file_create(constchar*name,unsignedintflags)+{+structinode*inode=alloc_anon_inode(secretmem_mnt->mnt_sb);+structfile*file=ERR_PTR(-ENOMEM);+structsecretmem_ctx*ctx;++if(IS_ERR(inode))+returnERR_CAST(inode);++ctx=kzalloc(sizeof(*ctx),GFP_KERNEL);+if(!ctx)+gotoerr_free_inode;++file=alloc_file_pseudo(inode,secretmem_mnt,"secretmem",+O_RDWR,&secretmem_fops);+if(IS_ERR(file))+gotoerr_free_ctx;++mapping_set_unevictable(inode->i_mapping);++inode->i_mapping->private_data=ctx;+inode->i_mapping->a_ops=&secretmem_aops;++/* pretend we are a normal file with zero size */+inode->i_mode|=S_IFREG;+inode->i_size=0;++file->private_data=ctx;++returnfile;++err_free_ctx:+kfree(ctx);+err_free_inode:+iput(inode);+returnfile;+}++staticvoidsecretmem_evict_inode(structinode*inode)+{+structsecretmem_ctx*ctx=inode->i_private;++truncate_inode_pages_final(&inode->i_data);+clear_inode(inode);+kfree(ctx);+}++staticconststructsuper_operationssecretmem_super_ops={+.evict_inode=secretmem_evict_inode,+};++staticintsecretmem_init_fs_context(structfs_context*fc)+{+structpseudo_fs_context*ctx=init_pseudo(fc,SECRETMEM_MAGIC);++if(!ctx)+return-ENOMEM;+ctx->ops=&secretmem_super_ops;++return0;+}++staticstructfile_system_typesecretmem_fs={+.name="secretmem",+.init_fs_context=secretmem_init_fs_context,+.kill_sb=kill_anon_super,+};++staticintsecretmem_init(void)+{+intret=0;++secretmem_mnt=kern_mount(&secretmem_fs);+if(IS_ERR(secretmem_mnt))+ret=PTR_ERR(secretmem_mnt);++returnret;+}+fs_initcall(secretmem_init);
From: Mike Rapoport <rppt@kernel.org> Date: 2020-07-06 17:21:22
From: Mike Rapoport <redacted>
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.
Signed-off-by: Mike Rapoport <redacted>
---
mm/secretmem.c | 107 ++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 88 insertions(+), 19 deletions(-)
From: Mike Rapoport <rppt@kernel.org> Date: 2020-07-06 17:21:27
From: Mike Rapoport <redacted>
Taking pages out from the direct map and bringing them back may create
undesired fragmentation and usage of the smaller pages in the direct
mapping of the physical memory.
This can be avoided if a significantly large area of the physical memory
would be reserved for secretmem purposes at boot time.
Add ability to reserve physical memory for secretmem at boot time using
"secretmem" kernel parameter and then use that reserved memory as a global
pool for secret memory needs.
Signed-off-by: Mike Rapoport <redacted>
---
mm/secretmem.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 137 insertions(+), 8 deletions(-)
From: Mike Rapoport <redacted>
The definitions of shift, mask and size for the second and the third level
of the leaf pages are available only when CONFIG_TRANSPARENT_HUGEPAGE is
set. Otherwise they evaluate to BUILD_BUG().
There is no explanation neither in the code nor in the changelog why the
usage of, e.g. HPAGE_PMD_SIZE should be only allowed with THP and forbidden
otherwise while the definitions of HPAGE_PMD_SIZE and HPAGE_PUD_SIZE
express the sizes better than ambiguous HPAGE_SIZE.
Make HPAGE_PxD_{SHIFT,MASK,SIZE} definitions available unconditionally.
Adding Andrea to Cc, he's the one who structured it that way,
and should be consulted.
I'm ambivalent myself. Many's the time I've been irritated by the
BUILD_BUG() in HPAGE_etc, and it's responsible for very many #ifdef
CONFIG_TRANSPARENT_HUGEPAGEs or IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)s
that you find uglily scattered around the source.
But that's the point of it: it's warning when you write code peculiar
to THP, that is going to bloat the build of kernels without any THP.
So although I've often been tempted to do as you suggest, I've always
ended up respecting Andrea's intention, and worked around it instead
(sometimes with #ifdef or IS_ENABLED(), sometimes with
PMD_{SHIFT,MASK_SIZE}, sometimes with a local definition).
Hugh
From: Mike Rapoport <rppt@kernel.org> Date: 2020-07-07 06:47:50
Hi Hugh,
On Mon, Jul 06, 2020 at 10:07:34PM -0700, Hugh Dickins wrote:
On Mon, 6 Jul 2020, Mike Rapoport wrote:
quoted
From: Mike Rapoport <redacted>
The definitions of shift, mask and size for the second and the third level
of the leaf pages are available only when CONFIG_TRANSPARENT_HUGEPAGE is
set. Otherwise they evaluate to BUILD_BUG().
There is no explanation neither in the code nor in the changelog why the
usage of, e.g. HPAGE_PMD_SIZE should be only allowed with THP and forbidden
otherwise while the definitions of HPAGE_PMD_SIZE and HPAGE_PUD_SIZE
express the sizes better than ambiguous HPAGE_SIZE.
Make HPAGE_PxD_{SHIFT,MASK,SIZE} definitions available unconditionally.
Adding Andrea to Cc, he's the one who structured it that way,
and should be consulted.
I'm ambivalent myself. Many's the time I've been irritated by the
BUILD_BUG() in HPAGE_etc, and it's responsible for very many #ifdef
CONFIG_TRANSPARENT_HUGEPAGEs or IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)s
that you find uglily scattered around the source.
But that's the point of it: it's warning when you write code peculiar
to THP, that is going to bloat the build of kernels without any THP.
So although I've often been tempted to do as you suggest, I've always
ended up respecting Andrea's intention, and worked around it instead
(sometimes with #ifdef or IS_ENABLED(), sometimes with
PMD_{SHIFT,MASK_SIZE}, sometimes with a local definition).
I could do with a local definition as well, but I think HPAGE_PxD_SHIFT
is better and more descriptive than ambiguous HPAGE_SHIFT and I was
thinking about wider change to use "THP" defines rather than "hugetlb"
defines wherever possible.
In the end, HPAGE_PMD_SIZE does not have to be associated with THP and
limited to it, it just says what is the size of a leaf page at PMD
level.
From: Andrea Arcangeli <hidden> Date: 2020-07-10 16:40:50
Hello Hugh and Mike,
On Mon, Jul 06, 2020 at 10:07:34PM -0700, Hugh Dickins wrote:
Adding Andrea to Cc, he's the one who structured it that way,
and should be consulted.
I'm ambivalent myself. Many's the time I've been irritated by the
BUILD_BUG() in HPAGE_etc, and it's responsible for very many #ifdef
CONFIG_TRANSPARENT_HUGEPAGEs or IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)s
that you find uglily scattered around the source.
But that's the point of it: it's warning when you write code peculiar
to THP, that is going to bloat the build of kernels without any THP.
So although I've often been tempted to do as you suggest, I've always
ended up respecting Andrea's intention, and worked around it instead
(sometimes with #ifdef or IS_ENABLED(), sometimes with
PMD_{SHIFT,MASK_SIZE}, sometimes with a local definition).
The only other reasons that comes to mind in addition of optimizing
the bloat away at build time is to make it easier to identify the THP
code and to make it explicit that hugetlbfs shouldn't us it or it
could be wrong on some arches.
However for this case the BUILD_BUG() looks right and this doesn't
look like a false positive.
This patchset has nothing to do THP, so it'd be more correct to use
MAX_ORDER whenever the fragmentation is about the buddy (doesn't look
the case here) or PUD_SIZE/ORDER/PMD_SIZE/ORDER if the objective is
not to unnecessarily split extra and unrelated hugepud/hugepmds in the
direct mapping (as in this case).
The real issue exposed by the BUILD_BUG is the lack of PMD_ORDER
definition and fs/dax.c already run into and it solved it locally in the
dax.c file:
/* The order of a PMD entry */
#define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT)
The fact it's not just this patch but also dax.c that run into the
same issue, makes me think PMD_ORDER should be defined and then you
can use PMD_* and PUD_* for this non-THP purpose.
Then the question if to remove the BUILD_BUG becomes orthogonal to
this patchset, but I don't see much value in retaining HPAGE_PMD/PUD_*
unless the BUILD_BUG is retained too, because this patchset already
hints that without the BUILD_BUG() the HPAGE_PMD_* definitions would
likely spill into non THP paths and they would lose also the only
value left (the ability to localize the THP code paths). So I wouldn't
be against removing the BUILD_BUG if it's causing maintenance
overhead, but then I would drop HPAGE_PMD_* too along with it or it
may just cause confusion.
Thanks,
Andrea
From: Matthew Wilcox <willy@infradead.org> Date: 2020-07-10 16:57:54
On Fri, Jul 10, 2020 at 12:40:37PM -0400, Andrea Arcangeli wrote:
Hello Hugh and Mike,
On Mon, Jul 06, 2020 at 10:07:34PM -0700, Hugh Dickins wrote:
quoted
Adding Andrea to Cc, he's the one who structured it that way,
and should be consulted.
I'm ambivalent myself. Many's the time I've been irritated by the
BUILD_BUG() in HPAGE_etc, and it's responsible for very many #ifdef
CONFIG_TRANSPARENT_HUGEPAGEs or IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)s
that you find uglily scattered around the source.
But that's the point of it: it's warning when you write code peculiar
to THP, that is going to bloat the build of kernels without any THP.
So although I've often been tempted to do as you suggest, I've always
ended up respecting Andrea's intention, and worked around it instead
(sometimes with #ifdef or IS_ENABLED(), sometimes with
PMD_{SHIFT,MASK_SIZE}, sometimes with a local definition).
The only other reasons that comes to mind in addition of optimizing
the bloat away at build time is to make it easier to identify the THP
code and to make it explicit that hugetlbfs shouldn't us it or it
could be wrong on some arches.
However for this case the BUILD_BUG() looks right and this doesn't
look like a false positive.
This patchset has nothing to do THP, so it'd be more correct to use
MAX_ORDER whenever the fragmentation is about the buddy (doesn't look
the case here) or PUD_SIZE/ORDER/PMD_SIZE/ORDER if the objective is
not to unnecessarily split extra and unrelated hugepud/hugepmds in the
direct mapping (as in this case).
The real issue exposed by the BUILD_BUG is the lack of PMD_ORDER
definition and fs/dax.c already run into and it solved it locally in the
dax.c file:
/* The order of a PMD entry */
#define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT)
The fact it's not just this patch but also dax.c that run into the
same issue, makes me think PMD_ORDER should be defined and then you
can use PMD_* and PUD_* for this non-THP purpose.
We'll run into some namespace issues.
arch/arm/kernel/head.S:#define PMD_ORDER 3
arch/arm/kernel/head.S:#define PMD_ORDER 2
arch/mips/include/asm/pgtable-32.h:#define PMD_ORDER aieeee_attempt_to_allocate_pmd
arch/mips/include/asm/pgtable-64.h:#define PMD_ORDER 0
arch/parisc/include/asm/pgtable.h:#define PMD_ORDER 1 /* Number of pages per pmd */
Then the question if to remove the BUILD_BUG becomes orthogonal to
this patchset, but I don't see much value in retaining HPAGE_PMD/PUD_*
unless the BUILD_BUG is retained too, because this patchset already
hints that without the BUILD_BUG() the HPAGE_PMD_* definitions would
likely spill into non THP paths and they would lose also the only
value left (the ability to localize the THP code paths). So I wouldn't
be against removing the BUILD_BUG if it's causing maintenance
overhead, but then I would drop HPAGE_PMD_* too along with it or it
may just cause confusion.
The confusion seem to have happened only about hpage_nr_pages not
about HPAGE_PMD_*. It's just the hpage_ prefix alone that is commonly
used by hugetlbfs only and so it's not surprising it caused confusion.
So I certainly agree hpage_nr_pages would better be renamed to
something more THP specific (either hpage_pmd_nr_pages or
trans_huge_nr_pages or as you wish), but HPAGE_PMD_ don't look too
confusing about the fact it's only for the THP case since the non-THP
case won't necessarily care about PMDs.
Thanks,
Andrea
From: Mike Rapoport <rppt@kernel.org> Date: 2020-07-10 17:13:01
On Fri, Jul 10, 2020 at 05:57:46PM +0100, Matthew Wilcox wrote:
On Fri, Jul 10, 2020 at 12:40:37PM -0400, Andrea Arcangeli wrote:
quoted
Hello Hugh and Mike,
On Mon, Jul 06, 2020 at 10:07:34PM -0700, Hugh Dickins wrote:
quoted
Adding Andrea to Cc, he's the one who structured it that way,
and should be consulted.
I'm ambivalent myself. Many's the time I've been irritated by the
BUILD_BUG() in HPAGE_etc, and it's responsible for very many #ifdef
CONFIG_TRANSPARENT_HUGEPAGEs or IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)s
that you find uglily scattered around the source.
But that's the point of it: it's warning when you write code peculiar
to THP, that is going to bloat the build of kernels without any THP.
So although I've often been tempted to do as you suggest, I've always
ended up respecting Andrea's intention, and worked around it instead
(sometimes with #ifdef or IS_ENABLED(), sometimes with
PMD_{SHIFT,MASK_SIZE}, sometimes with a local definition).
The only other reasons that comes to mind in addition of optimizing
the bloat away at build time is to make it easier to identify the THP
code and to make it explicit that hugetlbfs shouldn't us it or it
could be wrong on some arches.
However for this case the BUILD_BUG() looks right and this doesn't
look like a false positive.
This patchset has nothing to do THP, so it'd be more correct to use
MAX_ORDER whenever the fragmentation is about the buddy (doesn't look
the case here) or PUD_SIZE/ORDER/PMD_SIZE/ORDER if the objective is
not to unnecessarily split extra and unrelated hugepud/hugepmds in the
direct mapping (as in this case).
The real issue exposed by the BUILD_BUG is the lack of PMD_ORDER
definition and fs/dax.c already run into and it solved it locally in the
dax.c file:
/* The order of a PMD entry */
#define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT)
The fact it's not just this patch but also dax.c that run into the
same issue, makes me think PMD_ORDER should be defined and then you
can use PMD_* and PUD_* for this non-THP purpose.
We'll run into some namespace issues.
arch/arm/kernel/head.S:#define PMD_ORDER 3
arch/arm/kernel/head.S:#define PMD_ORDER 2
arch/mips/include/asm/pgtable-32.h:#define PMD_ORDER aieeee_attempt_to_allocate_pmd
arch/mips/include/asm/pgtable-64.h:#define PMD_ORDER 0
arch/parisc/include/asm/pgtable.h:#define PMD_ORDER 1 /* Number of pages per pmd */
This can be easily solved with, e.g.
#define PMD_PAGE_ORDER (PMD_SHIFT - PAGE_SHIFT)
or by renaming the current defines to PMD_ALLOC_ORDER.
quoted
Then the question if to remove the BUILD_BUG becomes orthogonal to
this patchset, but I don't see much value in retaining HPAGE_PMD/PUD_*
unless the BUILD_BUG is retained too, because this patchset already
hints that without the BUILD_BUG() the HPAGE_PMD_* definitions would
likely spill into non THP paths and they would lose also the only
value left (the ability to localize the THP code paths). So I wouldn't
be against removing the BUILD_BUG if it's causing maintenance
overhead, but then I would drop HPAGE_PMD_* too along with it or it
may just cause confusion.
I agree that THP_PMD_* and THP_PUD_* would be less confusing if we are
to differentiate THP and non-THP usage of 2nd and 3rd level leaf pages.
--
Sincerely yours,
Mike.
From: Kirill A. Shutemov <hidden> Date: 2020-07-13 10:58:16
On Mon, Jul 06, 2020 at 08:20:49PM +0300, Mike Rapoport wrote:
From: Mike Rapoport <redacted>
Extend memfd_create() 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_create system call.
The user than has to use ioctl() to define the desired protection mode for
the memory associated with that file descriptor and only when the mode is
set it is possible to mmap() the memory. For instance, the following
exapmple will create an uncached mapping (error handling is omitted):
fd = memfd_create("secret", MFD_SECRET);
I'm not convinced that it belong to memfd. You don't share anything with
memfd, but the syscall.
Didn't you just broke MFD_ALLOW_SEALING and MFD_HUGETLB with this?
I guess the check has to be under 'if (flags & MFD_SECRET) {' check, no?
And (unsigned int) case looks redundant to me.
quoted hunk
if (!(flags & MFD_HUGETLB)) {
if (flags & ~(unsigned int)MFD_ALL_FLAGS)
return -EINVAL;
From: Kirill A. Shutemov <hidden> Date: 2020-07-13 11:05:09
On Mon, Jul 06, 2020 at 08:20:50PM +0300, Mike Rapoport wrote:
quoted hunk
From: Mike Rapoport <redacted>
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.
Signed-off-by: Mike Rapoport <redacted>
---
mm/secretmem.c | 107 ++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 88 insertions(+), 19 deletions(-)
It's worth nothing that unlike flush_tlb_kernel_range(),
__kernel_map_pages() only flushed local TLB, so other CPU may still have
access to the page. It's shouldn't be a blocker, but deserve a comment.
From: Mike Rapoport <rppt@kernel.org> Date: 2020-07-13 15:31:41
On Mon, Jul 13, 2020 at 01:58:12PM +0300, Kirill A. Shutemov wrote:
On Mon, Jul 06, 2020 at 08:20:49PM +0300, Mike Rapoport wrote:
quoted
From: Mike Rapoport <redacted>
Extend memfd_create() 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_create system call.
The user than has to use ioctl() to define the desired protection mode for
the memory associated with that file descriptor and only when the mode is
set it is possible to mmap() the memory. For instance, the following
exapmple will create an uncached mapping (error handling is omitted):
fd = memfd_create("secret", MFD_SECRET);
I'm not convinced that it belong to memfd. You don't share anything with
memfd, but the syscall.
I've chosen memfd because it implements file descriptor for memory
access. Indeed, there similarities end and this can be entirely new
system call.
And, TBH, I was too lazy to wire up a new syscall for RFC :)
From: Mike Rapoport <rppt@kernel.org> Date: 2020-07-13 15:32:47
On Mon, Jul 13, 2020 at 02:05:05PM +0300, Kirill A. Shutemov wrote:
On Mon, Jul 06, 2020 at 08:20:50PM +0300, Mike Rapoport wrote:
quoted
From: Mike Rapoport <redacted>
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.
Signed-off-by: Mike Rapoport <redacted>
---
mm/secretmem.c | 107 ++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 88 insertions(+), 19 deletions(-)
It's worth nothing that unlike flush_tlb_kernel_range(),
__kernel_map_pages() only flushed local TLB, so other CPU may still have
access to the page. It's shouldn't be a blocker, but deserve a comment.
From: Pavel Machek <hidden> Date: 2020-07-17 08:36:08
Hi!
This is a second version of "secret" mappings implementation backed by a
file descriptor.
The file descriptor is created using memfd_create() syscall with a new
MFD_SECRET flag. The file descriptor should be configured using ioctl() to
define the desired protection and then mmap() of the fd 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.
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.
From: James Bottomley <hidden> Date: 2020-07-17 14:44:28
On Fri, 2020-07-17 at 10:36 +0200, Pavel Machek wrote:
Hi!
quoted
This is a second version of "secret" mappings implementation backed
by a file descriptor.
The file descriptor is created using memfd_create() syscall with a
new MFD_SECRET flag. The file descriptor should be configured using
ioctl() to define the desired protection and then mmap() of the fd
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.
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.
I believe unix userspace normally requires mappings to be... well...
protected from other users. How is this "secret" thing different? How
do you explain the difference to userland programmers?
That's true in the normal case, but for the container cloud the threat
model we're considering is a hostile other tenant trying to trick the
kernel into giving them access to your mappings. In the FOSDEM talk we
did about this:
https://fosdem.org/2020/schedule/event/kernel_address_space_isolation/
We demonstrated the case where the hostile tenant obtained host root
and then tried to get access via ptrace. The point being that pushing
the pages out of the direct map means that even root can't get access
to the secret by any means the OS provides. If you want to play with
this yourself, we have a userspace library:
https://git.kernel.org/pub/scm/linux/kernel/git/jejb/secret-memory-preloader.git/
It 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. I 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.
James