Skip to content

Commit 284cd24

Browse files
ebiggerstorvalds
authored andcommitted
userfaultfd: convert to use anon_inode_getfd()
Nothing actually calls userfaultfd_file_create() besides the userfaultfd() system call itself. So simplify things by folding it into the system call and using anon_inode_getfd() instead of anon_inode_getfile(). Do the same in resolve_userfault_fork() as well. This removes over 50 lines with no change in functionality. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]> Reviewed-by: Mike Rapoport <[email protected]> Cc: Andrea Arcangeli <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent c5c6383 commit 284cd24

File tree

1 file changed

+9
-61
lines changed

1 file changed

+9
-61
lines changed

fs/userfaultfd.c

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -988,24 +988,14 @@ static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
988988
struct uffd_msg *msg)
989989
{
990990
int fd;
991-
struct file *file;
992-
unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS;
993991

994-
fd = get_unused_fd_flags(flags);
992+
fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, new,
993+
O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS));
995994
if (fd < 0)
996995
return fd;
997996

998-
file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new,
999-
O_RDWR | flags);
1000-
if (IS_ERR(file)) {
1001-
put_unused_fd(fd);
1002-
return PTR_ERR(file);
1003-
}
1004-
1005-
fd_install(fd, file);
1006997
msg->arg.reserved.reserved1 = 0;
1007998
msg->arg.fork.ufd = fd;
1008-
1009999
return 0;
10101000
}
10111001

@@ -1887,39 +1877,23 @@ static void init_once_userfaultfd_ctx(void *mem)
18871877
seqcount_init(&ctx->refile_seq);
18881878
}
18891879

1890-
/**
1891-
* userfaultfd_file_create - Creates a userfaultfd file pointer.
1892-
* @flags: Flags for the userfaultfd file.
1893-
*
1894-
* This function creates a userfaultfd file pointer, w/out installing
1895-
* it into the fd table. This is useful when the userfaultfd file is
1896-
* used during the initialization of data structures that require
1897-
* extra setup after the userfaultfd creation. So the userfaultfd
1898-
* creation is split into the file pointer creation phase, and the
1899-
* file descriptor installation phase. In this way races with
1900-
* userspace closing the newly installed file descriptor can be
1901-
* avoided. Returns a userfaultfd file pointer, or a proper error
1902-
* pointer.
1903-
*/
1904-
static struct file *userfaultfd_file_create(int flags)
1880+
SYSCALL_DEFINE1(userfaultfd, int, flags)
19051881
{
1906-
struct file *file;
19071882
struct userfaultfd_ctx *ctx;
1883+
int fd;
19081884

19091885
BUG_ON(!current->mm);
19101886

19111887
/* Check the UFFD_* constants for consistency. */
19121888
BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
19131889
BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
19141890

1915-
file = ERR_PTR(-EINVAL);
19161891
if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
1917-
goto out;
1892+
return -EINVAL;
19181893

1919-
file = ERR_PTR(-ENOMEM);
19201894
ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
19211895
if (!ctx)
1922-
goto out;
1896+
return -ENOMEM;
19231897

19241898
atomic_set(&ctx->refcount, 1);
19251899
ctx->flags = flags;
@@ -1930,39 +1904,13 @@ static struct file *userfaultfd_file_create(int flags)
19301904
/* prevent the mm struct to be freed */
19311905
mmgrab(ctx->mm);
19321906

1933-
file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
1934-
O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
1935-
if (IS_ERR(file)) {
1907+
fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, ctx,
1908+
O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
1909+
if (fd < 0) {
19361910
mmdrop(ctx->mm);
19371911
kmem_cache_free(userfaultfd_ctx_cachep, ctx);
19381912
}
1939-
out:
1940-
return file;
1941-
}
1942-
1943-
SYSCALL_DEFINE1(userfaultfd, int, flags)
1944-
{
1945-
int fd, error;
1946-
struct file *file;
1947-
1948-
error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
1949-
if (error < 0)
1950-
return error;
1951-
fd = error;
1952-
1953-
file = userfaultfd_file_create(flags);
1954-
if (IS_ERR(file)) {
1955-
error = PTR_ERR(file);
1956-
goto err_put_unused_fd;
1957-
}
1958-
fd_install(fd, file);
1959-
19601913
return fd;
1961-
1962-
err_put_unused_fd:
1963-
put_unused_fd(fd);
1964-
1965-
return error;
19661914
}
19671915

19681916
static int __init userfaultfd_init(void)

0 commit comments

Comments
 (0)