Re: [PATCH v9 1/6] mm: introduce MAP_SHARED_VALIDATE, a mechanism to safely define new mmap flags
On Thu 12-10-17 09:32:17, Linus Torvalds wrote: > On Thu, Oct 12, 2017 at 6:51 AM, Jan Karawrote: > > > > When thinking a bit more about this I've realized one problem: Currently > > user can call mmap() with MAP_SHARED type and MAP_SYNC or MAP_DIRECT flags > > and he will get the new semantics (if the kernel happens to support it). I > > think that is undesirable [..] > > Why? > > If you have a performance preference for MAP_DIRECT or something like > that, but you don't want to *enforce* it, you'd use just plain > MAP_SHARED with it. > > Ie there may well be "I want this to work, possibly with downsides" issues. > > So it seems to be a reasonable model, and disallowing it seems to > limit people and not really help anything. I have two concerns: 1) IMHO it supports sloppy programming from userspace - if application asks e.g. for MAP_DIRECT and doesn't know whether it gets it or not, it would have to be very careful not to assume anything about that in its code. And frankly I think the most likely scenario is that a programmer will just use MAP_SHARED | MAP_DIRECT, *assume* he will get the MAP_DIRECT semantics if the call does not fail and then complain when his application breaks. 2) In theory there could be an application that inadvertedly sets some high flag bits and now it would get confused by getting different mmap(2) semantics. But I agree this is mostly theoretical. Overall I think the benefit of being able to say "do MAP_DIRECT if you can" does not outweight the risk of bugs in userspace applications. Especially since userspace can easily implement the same semantics by retrying the mmap(2) call without MAP_SHARED_VALIDATE | MAP_DIRECT. Honza -- Jan Kara SUSE Labs, CR ___ Linux-nvdimm mailing list Linux-nvdimm@lists.01.org https://lists.01.org/mailman/listinfo/linux-nvdimm
Re: [PATCH v9 1/6] mm: introduce MAP_SHARED_VALIDATE, a mechanism to safely define new mmap flags
On Thu, Oct 12, 2017 at 09:32:17AM -0700, Linus Torvalds wrote: > On Thu, Oct 12, 2017 at 6:51 AM, Jan Karawrote: > > > > When thinking a bit more about this I've realized one problem: Currently > > user can call mmap() with MAP_SHARED type and MAP_SYNC or MAP_DIRECT flags > > and he will get the new semantics (if the kernel happens to support it). I > > think that is undesirable [..] > > Why? > > If you have a performance preference for MAP_DIRECT or something like > that, but you don't want to *enforce* it, you'd use just plain > MAP_SHARED with it. > > Ie there may well be "I want this to work, possibly with downsides" issues. > > So it seems to be a reasonable model, and disallowing it seems to > limit people and not really help anything. I don't think for MAP_DIRECT it matters (and I think we shouldn't have MAP_DIRECT to start with, see the discussions later in the thread). But for the main use case, MAP_SYNC you really want a hard error when you don't get it. And while we could tell people that they should only use MAP_SYNC with MAP_SHARED_VALIDATE instead of MAP_SHARED chances that they get it wrong are extremely high. On the other hand if you really only want a flag to optimize calling mmap twice is very little overhead, and a very good documentation of you intent: addr = mmap(, MAP_SHARED_VALIDATE | MAP_DIRECT, ...); if (!addr && errno = EOPNOTSUPP) { /* MAP_DIRECT didn't work, we'll just cope using blah, blah */ addr = mmap(, MAP_SHARED, ...); } if (!addr) goto handle_error; ___ Linux-nvdimm mailing list Linux-nvdimm@lists.01.org https://lists.01.org/mailman/listinfo/linux-nvdimm
Re: [PATCH v9 1/6] mm: introduce MAP_SHARED_VALIDATE, a mechanism to safely define new mmap flags
On Thu, Oct 12, 2017 at 6:51 AM, Jan Karawrote: > > When thinking a bit more about this I've realized one problem: Currently > user can call mmap() with MAP_SHARED type and MAP_SYNC or MAP_DIRECT flags > and he will get the new semantics (if the kernel happens to support it). I > think that is undesirable [..] Why? If you have a performance preference for MAP_DIRECT or something like that, but you don't want to *enforce* it, you'd use just plain MAP_SHARED with it. Ie there may well be "I want this to work, possibly with downsides" issues. So it seems to be a reasonable model, and disallowing it seems to limit people and not really help anything. Linus ___ Linux-nvdimm mailing list Linux-nvdimm@lists.01.org https://lists.01.org/mailman/listinfo/linux-nvdimm
Re: [PATCH v9 1/6] mm: introduce MAP_SHARED_VALIDATE, a mechanism to safely define new mmap flags
Hi, > diff --git a/mm/mmap.c b/mm/mmap.c > index 680506faceae..2649c00581a0 100644 > --- a/mm/mmap.c > +++ b/mm/mmap.c > @@ -1389,6 +1389,18 @@ unsigned long do_mmap(struct file *file, unsigned long > addr, > struct inode *inode = file_inode(file); > > switch (flags & MAP_TYPE) { > + case MAP_SHARED_VALIDATE: > + if ((flags & ~LEGACY_MAP_MASK) == 0) { > + /* > + * If all legacy mmap flags, downgrade > + * to MAP_SHARED, i.e. invoke ->mmap() > + * instead of ->mmap_validate() > + */ > + flags &= ~MAP_TYPE; > + flags |= MAP_SHARED; > + } else if (!file->f_op->mmap_validate) > + return -EOPNOTSUPP; > + /* fall through */ > case MAP_SHARED: > if ((prot_WRITE) && !(file->f_mode_WRITE)) > return -EACCES; When thinking a bit more about this I've realized one problem: Currently user can call mmap() with MAP_SHARED type and MAP_SYNC or MAP_DIRECT flags and he will get the new semantics (if the kernel happens to support it). I think that is undesirable and we should force usage of MAP_SHARED_VALIDATE when you want to use flags outside of LEGACY_MAP_MASK. So I'd just mask off non-legacy flags for MAP_SHARED mappings (so they would be silently ignored as they used to be until now). Honza -- Jan KaraSUSE Labs, CR ___ Linux-nvdimm mailing list Linux-nvdimm@lists.01.org https://lists.01.org/mailman/listinfo/linux-nvdimm
[PATCH v9 1/6] mm: introduce MAP_SHARED_VALIDATE, a mechanism to safely define new mmap flags
The mmap(2) syscall suffers from the ABI anti-pattern of not validating unknown flags. However, proposals like MAP_SYNC and MAP_DIRECT need a mechanism to define new behavior that is known to fail on older kernels without the support. Define a new MAP_SHARED_VALIDATE flag pattern that is guaranteed to fail on all legacy mmap implementations. It is worth noting that the original proposal was for a standalone MAP_VALIDATE flag. However, when that could not be supported by all archs Linus observed: I see why you *think* you want a bitmap. You think you want a bitmap because you want to make MAP_VALIDATE be part of MAP_SYNC etc, so that people can do ret = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_SYNC, fd, 0); and "know" that MAP_SYNC actually takes. And I'm saying that whole wish is bogus. You're fundamentally depending on special semantics, just make it explicit. It's already not portable, so don't try to make it so. Rename that MAP_VALIDATE as MAP_SHARED_VALIDATE, make it have a value of 0x3, and make people do ret = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED_VALIDATE | MAP_SYNC, fd, 0); and then the kernel side is easier too (none of that random garbage playing games with looking at the "MAP_VALIDATE bit", but just another case statement in that map type thing. Boom. Done. Similar to ->fallocate() we also want the ability to validate the support for new flags on a per ->mmap() 'struct file_operations' instance basis. Towards that end arrange for flags to be generically validated against a mmap_supported_mask exported by 'struct file_operations'. By default all existing flags are implicitly supported, but new flags require MAP_SHARED_VALIDATE and per-instance-opt-in. Cc: Arnd BergmannCc: Andy Lutomirski Cc: Andrew Morton Suggested-by: Christoph Hellwig Suggested-by: Linus Torvalds Reviewed-by: Jan Kara Signed-off-by: Dan Williams --- arch/alpha/include/uapi/asm/mman.h |1 + arch/mips/include/uapi/asm/mman.h|1 + arch/mips/kernel/vdso.c |2 + arch/parisc/include/uapi/asm/mman.h |1 + arch/tile/mm/elf.c |3 +- arch/xtensa/include/uapi/asm/mman.h |1 + include/linux/fs.h |2 + include/linux/mm.h |2 + include/linux/mman.h | 39 ++ include/uapi/asm-generic/mman-common.h |1 + mm/mmap.c| 21 -- tools/include/uapi/asm-generic/mman-common.h |1 + 12 files changed, 69 insertions(+), 6 deletions(-) diff --git a/arch/alpha/include/uapi/asm/mman.h b/arch/alpha/include/uapi/asm/mman.h index 3b26cc62dadb..f85f18ffbf8c 100644 --- a/arch/alpha/include/uapi/asm/mman.h +++ b/arch/alpha/include/uapi/asm/mman.h @@ -11,6 +11,7 @@ #define MAP_SHARED 0x01/* Share changes */ #define MAP_PRIVATE0x02/* Changes are private */ +#define MAP_SHARED_VALIDATE 0x3/* share + validate extension flags */ #define MAP_TYPE 0x0f/* Mask for type of mapping (OSF/1 is _wrong_) */ #define MAP_FIXED 0x100 /* Interpret addr exactly */ #define MAP_ANONYMOUS 0x10/* don't use a file */ diff --git a/arch/mips/include/uapi/asm/mman.h b/arch/mips/include/uapi/asm/mman.h index da3216007fe0..054314bb062a 100644 --- a/arch/mips/include/uapi/asm/mman.h +++ b/arch/mips/include/uapi/asm/mman.h @@ -28,6 +28,7 @@ */ #define MAP_SHARED 0x001 /* Share changes */ #define MAP_PRIVATE0x002 /* Changes are private */ +#define MAP_SHARED_VALIDATE 0x3/* share + validate extension flags */ #define MAP_TYPE 0x00f /* Mask for type of mapping */ #define MAP_FIXED 0x010 /* Interpret addr exactly */ diff --git a/arch/mips/kernel/vdso.c b/arch/mips/kernel/vdso.c index 019035d7225c..cf10654477a9 100644 --- a/arch/mips/kernel/vdso.c +++ b/arch/mips/kernel/vdso.c @@ -110,7 +110,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) base = mmap_region(NULL, STACK_TOP, PAGE_SIZE, VM_READ|VM_WRITE|VM_EXEC| VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, - 0, NULL); + 0, NULL, 0); if (IS_ERR_VALUE(base)) { ret = base; goto out; diff --git a/arch/parisc/include/uapi/asm/mman.h b/arch/parisc/include/uapi/asm/mman.h index 775b5d5e41a1..a66fdb9c4b6d 100644 --- a/arch/parisc/include/uapi/asm/mman.h +++ b/arch/parisc/include/uapi/asm/mman.h @@ -11,6 +11,7 @@ #define