>From eda6d3d1acf23f63e8e29219b1379e479cf90d7d Mon Sep 17 00:00:00 2001
From: Pavel Begunkov <asml.silence@gmail.com>
Date: Mon, 13 Sep 2021 09:13:30 -0600
Subject: [PATCH 1/6] io_uring: place fixed tables under memcg limits

commit 0bea96f59ba40e63c0ae93ad6a02417b95f22f4d upstream.

Fixed tables may be large enough, place all of them together with
allocated tags under memcg limits.

Cc: stable@vger.kernel.org
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/b3ac9f5da9821bb59837b5fe25e8ef4be982218c.1629451684.git.asml.silence@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
---
 fs/autofs/modules.builtin          |  1 +
 fs/btrfs/modules.builtin           |  1 +
 fs/configfs/modules.builtin        |  1 +
 fs/crypto/modules.builtin          |  0
 fs/debugfs/modules.builtin         |  0
 fs/devpts/modules.builtin          |  0
 fs/ecryptfs/modules.builtin        |  1 +
 fs/efivarfs/modules.builtin        |  1 +
 fs/exportfs/modules.builtin        |  1 +
 fs/ext2/modules.builtin            |  1 +
 fs/ext4/modules.builtin            |  1 +
 fs/fat/modules.builtin             |  2 ++
 fs/fuse/modules.builtin            |  1 +
 fs/hugetlbfs/modules.builtin       |  0
 fs/io-wq.h.rej                     | 25 +++++++++++++++++++++++++
 fs/io_uring.c                      |  8 ++++----
 fs/iomap/modules.builtin           |  0
 fs/isofs/modules.builtin           |  1 +
 fs/jbd2/modules.builtin            |  1 +
 fs/kernfs/modules.builtin          |  0
 fs/lockd/modules.builtin           |  1 +
 fs/modules.builtin                 |  9 +++++++++
 fs/nfs/modules.builtin             |  4 ++++
 fs/nfs_common/modules.builtin      |  2 ++
 fs/nls/modules.builtin             |  1 +
 fs/notify/dnotify/modules.builtin  |  0
 fs/notify/fanotify/modules.builtin |  0
 fs/notify/inotify/modules.builtin  |  0
 fs/notify/modules.builtin          |  0
 fs/proc/modules.builtin            |  0
 fs/pstore/modules.builtin          |  1 +
 fs/quota/modules.builtin           |  0
 fs/ramfs/modules.builtin           |  0
 fs/squashfs/modules.builtin        |  1 +
 fs/sysfs/modules.builtin           |  0
 fs/tracefs/modules.builtin         |  0
 fs/xfs/modules.builtin             |  1 +
 37 files changed, 62 insertions(+), 4 deletions(-)
 create mode 100644 fs/autofs/modules.builtin
 create mode 100644 fs/btrfs/modules.builtin
 create mode 100644 fs/configfs/modules.builtin
 create mode 100644 fs/crypto/modules.builtin
 create mode 100644 fs/debugfs/modules.builtin
 create mode 100644 fs/devpts/modules.builtin
 create mode 100644 fs/ecryptfs/modules.builtin
 create mode 100644 fs/efivarfs/modules.builtin
 create mode 100644 fs/exportfs/modules.builtin
 create mode 100644 fs/ext2/modules.builtin
 create mode 100644 fs/ext4/modules.builtin
 create mode 100644 fs/fat/modules.builtin
 create mode 100644 fs/fuse/modules.builtin
 create mode 100644 fs/hugetlbfs/modules.builtin
 create mode 100644 fs/io-wq.h.rej
 create mode 100644 fs/iomap/modules.builtin
 create mode 100644 fs/isofs/modules.builtin
 create mode 100644 fs/jbd2/modules.builtin
 create mode 100644 fs/kernfs/modules.builtin
 create mode 100644 fs/lockd/modules.builtin
 create mode 100644 fs/modules.builtin
 create mode 100644 fs/nfs/modules.builtin
 create mode 100644 fs/nfs_common/modules.builtin
 create mode 100644 fs/nls/modules.builtin
 create mode 100644 fs/notify/dnotify/modules.builtin
 create mode 100644 fs/notify/fanotify/modules.builtin
 create mode 100644 fs/notify/inotify/modules.builtin
 create mode 100644 fs/notify/modules.builtin
 create mode 100644 fs/proc/modules.builtin
 create mode 100644 fs/pstore/modules.builtin
 create mode 100644 fs/quota/modules.builtin
 create mode 100644 fs/ramfs/modules.builtin
 create mode 100644 fs/squashfs/modules.builtin
 create mode 100644 fs/sysfs/modules.builtin
 create mode 100644 fs/tracefs/modules.builtin
 create mode 100644 fs/xfs/modules.builtin

diff --git a/fs/autofs/modules.builtin b/fs/autofs/modules.builtin
new file mode 100644
index 000000000000..3425a57879aa
--- /dev/null
+++ b/fs/autofs/modules.builtin
@@ -0,0 +1 @@
+fs/autofs/autofs4.ko
diff --git a/fs/btrfs/modules.builtin b/fs/btrfs/modules.builtin
new file mode 100644
index 000000000000..c625a36770c8
--- /dev/null
+++ b/fs/btrfs/modules.builtin
@@ -0,0 +1 @@
+fs/btrfs/btrfs.ko
diff --git a/fs/configfs/modules.builtin b/fs/configfs/modules.builtin
new file mode 100644
index 000000000000..75bb304ca48b
--- /dev/null
+++ b/fs/configfs/modules.builtin
@@ -0,0 +1 @@
+fs/configfs/configfs.ko
diff --git a/fs/crypto/modules.builtin b/fs/crypto/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/debugfs/modules.builtin b/fs/debugfs/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/devpts/modules.builtin b/fs/devpts/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/ecryptfs/modules.builtin b/fs/ecryptfs/modules.builtin
new file mode 100644
index 000000000000..6216eb603190
--- /dev/null
+++ b/fs/ecryptfs/modules.builtin
@@ -0,0 +1 @@
+fs/ecryptfs/ecryptfs.ko
diff --git a/fs/efivarfs/modules.builtin b/fs/efivarfs/modules.builtin
new file mode 100644
index 000000000000..e877f7a6ff24
--- /dev/null
+++ b/fs/efivarfs/modules.builtin
@@ -0,0 +1 @@
+fs/efivarfs/efivarfs.ko
diff --git a/fs/exportfs/modules.builtin b/fs/exportfs/modules.builtin
new file mode 100644
index 000000000000..6e653c2a0b47
--- /dev/null
+++ b/fs/exportfs/modules.builtin
@@ -0,0 +1 @@
+fs/exportfs/exportfs.ko
diff --git a/fs/ext2/modules.builtin b/fs/ext2/modules.builtin
new file mode 100644
index 000000000000..f4c027967504
--- /dev/null
+++ b/fs/ext2/modules.builtin
@@ -0,0 +1 @@
+fs/ext2/ext2.ko
diff --git a/fs/ext4/modules.builtin b/fs/ext4/modules.builtin
new file mode 100644
index 000000000000..87c8fe1b7c30
--- /dev/null
+++ b/fs/ext4/modules.builtin
@@ -0,0 +1 @@
+fs/ext4/ext4.ko
diff --git a/fs/fat/modules.builtin b/fs/fat/modules.builtin
new file mode 100644
index 000000000000..a4c36176a69d
--- /dev/null
+++ b/fs/fat/modules.builtin
@@ -0,0 +1,2 @@
+fs/fat/fat.ko
+fs/fat/vfat.ko
diff --git a/fs/fuse/modules.builtin b/fs/fuse/modules.builtin
new file mode 100644
index 000000000000..36a0c8d41d7a
--- /dev/null
+++ b/fs/fuse/modules.builtin
@@ -0,0 +1 @@
+fs/fuse/fuse.ko
diff --git a/fs/hugetlbfs/modules.builtin b/fs/hugetlbfs/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/io-wq.h.rej b/fs/io-wq.h.rej
new file mode 100644
index 000000000000..b911d356dca5
--- /dev/null
+++ b/fs/io-wq.h.rej
@@ -0,0 +1,25 @@
+--- fs/io-wq.h
++++ fs/io-wq.h
+@@ -94,9 +95,20 @@ static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
+ typedef struct io_wq_work *(free_work_fn)(struct io_wq_work *);
+ typedef void (io_wq_work_fn)(struct io_wq_work *);
+ 
+-struct io_wq_data {
+-	unsigned long *hash_map;
++struct io_wq_hash {
++	refcount_t refs;
++	unsigned long map;
++	struct wait_queue_head wait;
++};
++
++static inline void io_wq_put_hash(struct io_wq_hash *hash)
++{
++	if (refcount_dec_and_test(&hash->refs))
++		kfree(hash);
++}
+ 
++struct io_wq_data {
++	struct io_wq_hash *hash;
+ 	io_wq_work_fn *do_work;
+ 	free_work_fn *free_work;
+ };
diff --git a/fs/io_uring.c b/fs/io_uring.c
index 58ae2eab99ef..a1b6d338e3a0 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -7195,11 +7195,11 @@ static struct io_rsrc_data *io_rsrc_data_alloc(struct io_ring_ctx *ctx,
 {
 	struct io_rsrc_data *data;
 
-	data = kzalloc(sizeof(*data), GFP_KERNEL);
+	data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
 	if (!data)
 		return NULL;
 
-	data->tags = kvcalloc(nr, sizeof(*data->tags), GFP_KERNEL);
+	data->tags = kvcalloc(nr, sizeof(*data->tags), GFP_KERNEL_ACCOUNT);
 	if (!data->tags) {
 		kfree(data);
 		return NULL;
@@ -7477,7 +7477,7 @@ static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
 {
 	unsigned i, nr_tables = DIV_ROUND_UP(nr_files, IORING_MAX_FILES_TABLE);
 
-	table->files = kcalloc(nr_tables, sizeof(*table->files), GFP_KERNEL);
+	table->files = kcalloc(nr_tables, sizeof(*table->files), GFP_KERNEL_ACCOUNT);
 	if (!table->files)
 		return false;
 
@@ -7485,7 +7485,7 @@ static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
 		unsigned int this_files = min(nr_files, IORING_MAX_FILES_TABLE);
 
 		table->files[i] = kcalloc(this_files, sizeof(*table->files[i]),
-					GFP_KERNEL);
+					GFP_KERNEL_ACCOUNT);
 		if (!table->files[i])
 			break;
 		nr_files -= this_files;
diff --git a/fs/iomap/modules.builtin b/fs/iomap/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/isofs/modules.builtin b/fs/isofs/modules.builtin
new file mode 100644
index 000000000000..7f55c4f4a938
--- /dev/null
+++ b/fs/isofs/modules.builtin
@@ -0,0 +1 @@
+fs/isofs/isofs.ko
diff --git a/fs/jbd2/modules.builtin b/fs/jbd2/modules.builtin
new file mode 100644
index 000000000000..ca3ee1e7d87a
--- /dev/null
+++ b/fs/jbd2/modules.builtin
@@ -0,0 +1 @@
+fs/jbd2/jbd2.ko
diff --git a/fs/kernfs/modules.builtin b/fs/kernfs/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/lockd/modules.builtin b/fs/lockd/modules.builtin
new file mode 100644
index 000000000000..3166e33857b4
--- /dev/null
+++ b/fs/lockd/modules.builtin
@@ -0,0 +1 @@
+fs/lockd/lockd.ko
diff --git a/fs/modules.builtin b/fs/modules.builtin
new file mode 100644
index 000000000000..28063ed6d4f5
--- /dev/null
+++ b/fs/modules.builtin
@@ -0,0 +1,9 @@
+fs/binfmt_script.ko
+fs/mbcache.ko
+fs/configfs/configfs.ko
+fs/exportfs/exportfs.ko
+fs/ext2/ext2.ko
+fs/ext4/ext4.ko
+fs/jbd2/jbd2.ko
+fs/nls/nls_base.ko
+fs/xfs/xfs.ko
diff --git a/fs/nfs/modules.builtin b/fs/nfs/modules.builtin
new file mode 100644
index 000000000000..69e4c62e80ed
--- /dev/null
+++ b/fs/nfs/modules.builtin
@@ -0,0 +1,4 @@
+fs/nfs/nfs.ko
+fs/nfs/nfsv2.ko
+fs/nfs/nfsv3.ko
+fs/nfs/nfsv4.ko
diff --git a/fs/nfs_common/modules.builtin b/fs/nfs_common/modules.builtin
new file mode 100644
index 000000000000..cd9997308742
--- /dev/null
+++ b/fs/nfs_common/modules.builtin
@@ -0,0 +1,2 @@
+fs/nfs_common/nfs_acl.ko
+fs/nfs_common/grace.ko
diff --git a/fs/nls/modules.builtin b/fs/nls/modules.builtin
new file mode 100644
index 000000000000..fc1e1ebb52b3
--- /dev/null
+++ b/fs/nls/modules.builtin
@@ -0,0 +1 @@
+fs/nls/nls_base.ko
diff --git a/fs/notify/dnotify/modules.builtin b/fs/notify/dnotify/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/notify/fanotify/modules.builtin b/fs/notify/fanotify/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/notify/inotify/modules.builtin b/fs/notify/inotify/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/notify/modules.builtin b/fs/notify/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/proc/modules.builtin b/fs/proc/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/pstore/modules.builtin b/fs/pstore/modules.builtin
new file mode 100644
index 000000000000..df9f660810d4
--- /dev/null
+++ b/fs/pstore/modules.builtin
@@ -0,0 +1 @@
+fs/pstore/pstore.ko
diff --git a/fs/quota/modules.builtin b/fs/quota/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/ramfs/modules.builtin b/fs/ramfs/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/squashfs/modules.builtin b/fs/squashfs/modules.builtin
new file mode 100644
index 000000000000..1e576034deba
--- /dev/null
+++ b/fs/squashfs/modules.builtin
@@ -0,0 +1 @@
+fs/squashfs/squashfs.ko
diff --git a/fs/sysfs/modules.builtin b/fs/sysfs/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/tracefs/modules.builtin b/fs/tracefs/modules.builtin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/fs/xfs/modules.builtin b/fs/xfs/modules.builtin
new file mode 100644
index 000000000000..b84139d4cd94
--- /dev/null
+++ b/fs/xfs/modules.builtin
@@ -0,0 +1 @@
+fs/xfs/xfs.ko
-- 
2.33.0

