[Devel] [PATCH RH9 13/22] ve/fs/files: Add new argument to expand_files()

Andrey Zhadchenko andrey.zhadchenko at virtuozzo.com
Thu Oct 7 13:57:37 MSK 2021


From: Kirill Tkhai <ktkhai at virtuozzo.com>

Patchset description:
Shrink big fdtable on criu restore

This patchset allows to avoid memory overuse introduced by service fds on criu
restore.
The solution is simple: smartly check for closed fd number, and shrink fdtable
if this could be made. The checks are happen in is_pseudosuper mode, so we do
not affect performance on normal work mode.

The problem is we can't solve this for 100% case in userspace.
Kernel allows to fix that completely.

https://jira.sw.ru/browse/PSBM-78827

Eric Dumazet (1):
      ms/fs/file.c: don't acquire files->file_lock in fd_install()

Kirill Tkhai (3):
      files: Add new argument to expand_files()
      files: Add fdtable_align() helper
      files: Shrink big fdtable on close in is_pseudosuper mode

Mateusz Guzik (1):
      ms/vfs: grab the lock instead of blocking in __fd_install during resizing

============================================================
This patch description:

Add argument "shrink" to the functions.
This is refactoring, which will be used in next patches.

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
Reviewed-by: Cyrill Gorcunov <gorcunov at openvz.org>

(cherry picked from vz7 commit 997f6cab4a45 ("ve/fs/files: Add new argument to
expand_files()"))
Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko at virtuozzo.com>

(cherry picked from vz8 commit 2a800bdcf95a47ebd2c1d38c42e63d325cb3248f)
Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko at virtuozzo.com>
---
 fs/file.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/fs/file.c b/fs/file.c
index 86dc995..3a17945 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -73,7 +73,7 @@ static void copy_fd_bitmaps(struct fdtable *nfdt, struct fdtable *ofdt,
  * Copy all file descriptors from the old table to the new, expanded table and
  * clear the extra space.  Called with the files spinlock held for write.
  */
-static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
+static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt, bool shrink)
 {
 	size_t cpy, set;
 
@@ -150,7 +150,7 @@ static struct fdtable * alloc_fdtable(unsigned int nr)
  * Return <0 error code on error; 1 on successful completion.
  * The files->file_lock should be held on entry, and will be held on exit.
  */
-static int expand_fdtable(struct files_struct *files, unsigned int nr)
+static int expand_fdtable(struct files_struct *files, unsigned int nr, bool shrink)
 	__releases(files->file_lock)
 	__acquires(files->file_lock)
 {
@@ -178,7 +178,7 @@ static int expand_fdtable(struct files_struct *files, unsigned int nr)
 	}
 	cur_fdt = files_fdtable(files);
 	BUG_ON(nr < cur_fdt->max_fds);
-	copy_fdtable(new_fdt, cur_fdt);
+	copy_fdtable(new_fdt, cur_fdt, shrink);
 	rcu_assign_pointer(files->fdt, new_fdt);
 	if (cur_fdt != &files->fdtab)
 		call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
@@ -195,7 +195,7 @@ static int expand_fdtable(struct files_struct *files, unsigned int nr)
  * expanded and execution may have blocked.
  * The files->file_lock should be held on entry, and will be held on exit.
  */
-static int expand_files(struct files_struct *files, unsigned int nr)
+static int expand_files(struct files_struct *files, unsigned int nr, bool shrink)
 	__releases(files->file_lock)
 	__acquires(files->file_lock)
 {
@@ -223,7 +223,7 @@ static int expand_files(struct files_struct *files, unsigned int nr)
 
 	/* All good, so we try */
 	files->resize_in_progress = true;
-	expanded = expand_fdtable(files, nr);
+	expanded = expand_fdtable(files, nr, shrink);
 	files->resize_in_progress = false;
 
 	wake_up_all(&files->resize_wait);
@@ -492,7 +492,7 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags)
 	if (fd >= end)
 		goto out;
 
-	error = expand_files(files, fd);
+	error = expand_files(files, fd, false);
 	if (error < 0)
 		goto out;
 
@@ -1069,7 +1069,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags)
 		return -EBADF;
 
 	spin_lock(&files->file_lock);
-	err = expand_files(files, fd);
+	err = expand_files(files, fd, false);
 	if (unlikely(err < 0))
 		goto out_unlock;
 	return do_dup2(files, file, fd, flags);
@@ -1150,7 +1150,7 @@ static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags)
 		return -EBADF;
 
 	spin_lock(&files->file_lock);
-	err = expand_files(files, newfd);
+	err = expand_files(files, newfd, false);
 	file = files_lookup_fd_locked(files, oldfd);
 	if (unlikely(!file))
 		goto Ebadf;
-- 
1.8.3.1



More information about the Devel mailing list