Skip to content

Commit

Permalink
memfd: make memfd_open() skip fd attributes not needed for vma mapping
Browse files Browse the repository at this point in the history
There is only one user of memfd_open() outside of memfd.c: open_filemap().
It is restoring a file-backed mapping and doesn't need to F_SETOWN
nor update the fd's position.

Signed-off-by: Michał Mirosław <emmir@google.com>
  • Loading branch information
osctobe committed Aug 21, 2023
1 parent e1cda9f commit bf599fd
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions criu/memfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ int memfd_open(struct file_desc *d, u32 *fdflags)

fd = memfd_open_inode(mfi->inode);
if (fd < 0)
goto err;
return -1;

/* Reopen the fd with original permissions */
flags = fdflags ? *fdflags : mfe->flags;
Expand All @@ -348,40 +348,40 @@ int memfd_open(struct file_desc *d, u32 *fdflags)
* important though.
*/
_fd = __open_proc(PROC_SELF, 0, flags, "fd/%d", fd);
if (_fd < 0) {
if (_fd < 0)
pr_perror("Can't reopen memfd id=%d", mfe->id);
goto err;
}

close(fd);
fd = _fd;
return _fd;
}

static int memfd_open_fe_fd(struct file_desc *d, int *new_fd)
{
MemfdFileEntry *mfe;
int fd;

fd = memfd_open(d, NULL);
if (fd < 0)
return -1;

mfe = container_of(d, struct memfd_info, d)->mfe;

if (restore_fown(fd, mfe->fown) < 0)
goto err;

if (lseek(fd, mfe->pos, SEEK_SET) < 0) {
pr_perror("Can't restore file position of memfd id=%d", mfe->id);
pr_perror("Can't restore file position of %d for memfd id=%d", fd, mfe->id);
goto err;
}

return fd;
*new_fd = fd;
return 0;

err:
if (fd >= 0)
close(fd);
close(fd);
return -1;
}

static int memfd_open_fe_fd(struct file_desc *fd, int *new_fd)
{
int tmp;

tmp = memfd_open(fd, NULL);
if (tmp < 0)
return -1;
*new_fd = tmp;
return 0;
}

static char *memfd_d_name(struct file_desc *d, char *buf, size_t s)
{
MemfdInodeEntry *mie = NULL;
Expand Down

0 comments on commit bf599fd

Please sign in to comment.