Commit 47b9012e authored by Marc-André Lureau's avatar Marc-André Lureau Committed by Linus Torvalds

shmem: add sealing support to hugetlb-backed memfd

Adapt add_seals()/get_seals() to work with hugetbfs-backed memory.

Teach memfd_create() to allow sealing operations on MFD_HUGETLB.

Link: http://lkml.kernel.org/r/20171107122800.25517-6-marcandre.lureau@redhat.comSigned-off-by: default avatarMarc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: default avatarMike Kravetz <mike.kravetz@oracle.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: David Herrmann <dh.herrmann@gmail.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent ff62a342
...@@ -2717,6 +2717,19 @@ static int shmem_wait_for_pins(struct address_space *mapping) ...@@ -2717,6 +2717,19 @@ static int shmem_wait_for_pins(struct address_space *mapping)
return error; return error;
} }
static unsigned int *memfd_file_seals_ptr(struct file *file)
{
if (file->f_op == &shmem_file_operations)
return &SHMEM_I(file_inode(file))->seals;
#ifdef CONFIG_HUGETLBFS
if (file->f_op == &hugetlbfs_file_operations)
return &HUGETLBFS_I(file_inode(file))->seals;
#endif
return NULL;
}
#define F_ALL_SEALS (F_SEAL_SEAL | \ #define F_ALL_SEALS (F_SEAL_SEAL | \
F_SEAL_SHRINK | \ F_SEAL_SHRINK | \
F_SEAL_GROW | \ F_SEAL_GROW | \
...@@ -2725,7 +2738,7 @@ static int shmem_wait_for_pins(struct address_space *mapping) ...@@ -2725,7 +2738,7 @@ static int shmem_wait_for_pins(struct address_space *mapping)
static int memfd_add_seals(struct file *file, unsigned int seals) static int memfd_add_seals(struct file *file, unsigned int seals)
{ {
struct inode *inode = file_inode(file); struct inode *inode = file_inode(file);
struct shmem_inode_info *info = SHMEM_I(inode); unsigned int *file_seals;
int error; int error;
/* /*
...@@ -2758,8 +2771,6 @@ static int memfd_add_seals(struct file *file, unsigned int seals) ...@@ -2758,8 +2771,6 @@ static int memfd_add_seals(struct file *file, unsigned int seals)
* other file types. * other file types.
*/ */
if (file->f_op != &shmem_file_operations)
return -EINVAL;
if (!(file->f_mode & FMODE_WRITE)) if (!(file->f_mode & FMODE_WRITE))
return -EPERM; return -EPERM;
if (seals & ~(unsigned int)F_ALL_SEALS) if (seals & ~(unsigned int)F_ALL_SEALS)
...@@ -2767,12 +2778,18 @@ static int memfd_add_seals(struct file *file, unsigned int seals) ...@@ -2767,12 +2778,18 @@ static int memfd_add_seals(struct file *file, unsigned int seals)
inode_lock(inode); inode_lock(inode);
if (info->seals & F_SEAL_SEAL) { file_seals = memfd_file_seals_ptr(file);
if (!file_seals) {
error = -EINVAL;
goto unlock;
}
if (*file_seals & F_SEAL_SEAL) {
error = -EPERM; error = -EPERM;
goto unlock; goto unlock;
} }
if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) { if ((seals & F_SEAL_WRITE) && !(*file_seals & F_SEAL_WRITE)) {
error = mapping_deny_writable(file->f_mapping); error = mapping_deny_writable(file->f_mapping);
if (error) if (error)
goto unlock; goto unlock;
...@@ -2784,7 +2801,7 @@ static int memfd_add_seals(struct file *file, unsigned int seals) ...@@ -2784,7 +2801,7 @@ static int memfd_add_seals(struct file *file, unsigned int seals)
} }
} }
info->seals |= seals; *file_seals |= seals;
error = 0; error = 0;
unlock: unlock:
...@@ -2794,10 +2811,9 @@ static int memfd_add_seals(struct file *file, unsigned int seals) ...@@ -2794,10 +2811,9 @@ static int memfd_add_seals(struct file *file, unsigned int seals)
static int memfd_get_seals(struct file *file) static int memfd_get_seals(struct file *file)
{ {
if (file->f_op != &shmem_file_operations) unsigned int *seals = memfd_file_seals_ptr(file);
return -EINVAL;
return SHMEM_I(file_inode(file))->seals; return seals ? *seals : -EINVAL;
} }
long memfd_fcntl(struct file *file, unsigned int cmd, unsigned long arg) long memfd_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
...@@ -3655,7 +3671,7 @@ SYSCALL_DEFINE2(memfd_create, ...@@ -3655,7 +3671,7 @@ SYSCALL_DEFINE2(memfd_create,
const char __user *, uname, const char __user *, uname,
unsigned int, flags) unsigned int, flags)
{ {
struct shmem_inode_info *info; unsigned int *file_seals;
struct file *file; struct file *file;
int fd, error; int fd, error;
char *name; char *name;
...@@ -3665,9 +3681,6 @@ SYSCALL_DEFINE2(memfd_create, ...@@ -3665,9 +3681,6 @@ SYSCALL_DEFINE2(memfd_create,
if (flags & ~(unsigned int)MFD_ALL_FLAGS) if (flags & ~(unsigned int)MFD_ALL_FLAGS)
return -EINVAL; return -EINVAL;
} else { } else {
/* Sealing not supported in hugetlbfs (MFD_HUGETLB) */
if (flags & MFD_ALLOW_SEALING)
return -EINVAL;
/* Allow huge page size encoding in flags. */ /* Allow huge page size encoding in flags. */
if (flags & ~(unsigned int)(MFD_ALL_FLAGS | if (flags & ~(unsigned int)(MFD_ALL_FLAGS |
(MFD_HUGE_MASK << MFD_HUGE_SHIFT))) (MFD_HUGE_MASK << MFD_HUGE_SHIFT)))
...@@ -3720,12 +3733,8 @@ SYSCALL_DEFINE2(memfd_create, ...@@ -3720,12 +3733,8 @@ SYSCALL_DEFINE2(memfd_create,
file->f_flags |= O_RDWR | O_LARGEFILE; file->f_flags |= O_RDWR | O_LARGEFILE;
if (flags & MFD_ALLOW_SEALING) { if (flags & MFD_ALLOW_SEALING) {
/* file_seals = memfd_file_seals_ptr(file);
* flags check at beginning of function ensures *file_seals &= ~F_SEAL_SEAL;
* this is not a hugetlbfs (MFD_HUGETLB) file.
*/
info = SHMEM_I(file_inode(file));
info->seals &= ~F_SEAL_SEAL;
} }
fd_install(fd, file); fd_install(fd, file);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment