[RHEL8,COMMIT] ve/fs/binfmt: move all internal data to structure

Submitted by Konstantin Khorenko on May 28, 2020, 9:24 a.m.

Details

Message ID 202005280924.04S9OGpM023830@finist_co8.work.ct
State New
Series "ve/fs/binfmt: move all internal data to structure"
Headers show

Commit Message

Konstantin Khorenko May 28, 2020, 9:24 a.m.
The commit is pushed to "branch-rh8-4.18.0-80.1.2.vz8.3.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh8-4.18.0-80.1.2.vz8.3.9
------>
commit cd1179093e6411bc115fdee11de3ef34099d5b0e
Author: Valeriy Vdovin <valeriy.vdovin@virtuozzo.com>
Date:   Thu May 28 12:24:16 2020 +0300

    ve/fs/binfmt: move all internal data to structure
    
    Allows to simplify management in further patches.
    
    Signed-off-by: Stanislav Kinsbursky <skinsbursky@parallels.com>
    Signed-off-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
    
    (cherry picked from 19551dbbfb60e2803b8e4d553f283acc3753858a
    due to https://jira.sw.ru/browse/PSBM-103973)
    
    Signed-off-by: Valeriy Vdovin <valeriy.vdovin@virtuozzo.com>
---
 fs/binfmt_misc.c | 74 +++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 46 insertions(+), 28 deletions(-)

Patch hide | download patch | download mbox

diff --git a/fs/binfmt_misc.c b/fs/binfmt_misc.c
index aa4a7a23ff99..97bd44f1b297 100644
--- a/fs/binfmt_misc.c
+++ b/fs/binfmt_misc.c
@@ -38,9 +38,6 @@  enum {
 	VERBOSE_STATUS = 1 /* make it zero to save 400 bytes kernel memory */
 };
 
-static LIST_HEAD(entries);
-static int enabled = 1;
-
 enum {Enabled, Magic};
 #define MISC_FMT_PRESERVE_ARGV0 (1 << 31)
 #define MISC_FMT_OPEN_BINARY (1 << 30)
@@ -60,10 +57,22 @@  typedef struct {
 	struct file *interp_file;
 } Node;
 
-static DEFINE_RWLOCK(entries_lock);
 static struct file_system_type bm_fs_type;
-static struct vfsmount *bm_mnt;
-static int entry_count;
+
+struct binfmt_misc {
+	struct list_head entries;
+	int enabled;
+
+	rwlock_t entries_lock;
+	struct vfsmount *bm_mnt;
+	int entry_count;
+};
+
+struct binfmt_misc binfmt_data = {
+	.entries	= LIST_HEAD_INIT(binfmt_data.entries),
+	.enabled	= 1,
+	.entries_lock	= __RW_LOCK_UNLOCKED(binfmt_data.entries_lock),
+};
 
 /*
  * Max length of the register string.  Determined by:
@@ -85,13 +94,13 @@  static int entry_count;
  * if we do, return the node, else NULL
  * locking is done in load_misc_binary
  */
-static Node *check_file(struct linux_binprm *bprm)
+static Node *check_file(struct binfmt_misc *bm_data, struct linux_binprm *bprm)
 {
 	char *p = strrchr(bprm->interp, '.');
 	struct list_head *l;
 
 	/* Walk all the registered handlers. */
-	list_for_each(l, &entries) {
+	list_for_each(l, &bm_data->entries) {
 		Node *e = list_entry(l, Node, list);
 		char *s;
 		int j;
@@ -133,17 +142,18 @@  static int load_misc_binary(struct linux_binprm *bprm)
 	struct file *interp_file = NULL;
 	int retval;
 	int fd_binary = -1;
+	struct binfmt_misc *bm_data = &binfmt_data;
 
 	retval = -ENOEXEC;
-	if (!enabled)
+	if (!bm_data || !bm_data->enabled)
 		return retval;
 
 	/* to keep locking time low, we copy the interpreter string */
-	read_lock(&entries_lock);
-	fmt = check_file(bprm);
+	read_lock(&bm_data->entries_lock);
+	fmt = check_file(bm_data, bprm);
 	if (fmt)
 		dget(fmt->dentry);
-	read_unlock(&entries_lock);
+	read_unlock(&bm_data->entries_lock);
 	if (!fmt)
 		return retval;
 
@@ -609,19 +619,19 @@  static void bm_evict_inode(struct inode *inode)
 	kfree(e);
 }
 
-static void kill_node(Node *e)
+static void kill_node(struct binfmt_misc *bm_data, Node *e)
 {
 	struct dentry *dentry;
 
-	write_lock(&entries_lock);
+	write_lock(&bm_data->entries_lock);
 	list_del_init(&e->list);
-	write_unlock(&entries_lock);
+	write_unlock(&bm_data->entries_lock);
 
 	dentry = e->dentry;
 	drop_nlink(d_inode(dentry));
 	d_drop(dentry);
 	dput(dentry);
-	simple_release_fs(&bm_mnt, &entry_count);
+	simple_release_fs(&bm_data->bm_mnt, &bm_data->entry_count);
 }
 
 /* /<entry> */
@@ -651,6 +661,8 @@  static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
 	struct dentry *root;
 	Node *e = file_inode(file)->i_private;
 	int res = parse_command(buffer, count);
+	struct super_block *sb = file->f_path.dentry->d_sb;
+	struct binfmt_misc *bm_data = sb->s_fs_info;
 
 	switch (res) {
 	case 1:
@@ -667,7 +679,7 @@  static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
 		inode_lock(d_inode(root));
 
 		if (!list_empty(&e->list))
-			kill_node(e);
+			kill_node(bm_data, e);
 
 		inode_unlock(d_inode(root));
 		break;
@@ -692,6 +704,7 @@  static ssize_t bm_register_write(struct file *file, const char __user *buffer,
 	Node *e;
 	struct inode *inode;
 	struct super_block *sb = file_inode(file)->i_sb;
+	struct binfmt_misc *bm_data = sb->s_fs_info;
 	struct dentry *root = sb->s_root, *dentry;
 	int err = 0;
 
@@ -716,7 +729,7 @@  static ssize_t bm_register_write(struct file *file, const char __user *buffer,
 	if (!inode)
 		goto out2;
 
-	err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
+	err = simple_pin_fs(&bm_fs_type, &bm_data->bm_mnt, &bm_data->entry_count);
 	if (err) {
 		iput(inode);
 		inode = NULL;
@@ -730,7 +743,7 @@  static ssize_t bm_register_write(struct file *file, const char __user *buffer,
 		if (IS_ERR(f)) {
 			err = PTR_ERR(f);
 			pr_notice("register: failed to install interpreter file %s\n", e->interpreter);
-			simple_release_fs(&bm_mnt, &entry_count);
+			simple_release_fs(&bm_data->bm_mnt, &bm_data->entry_count);
 			iput(inode);
 			inode = NULL;
 			goto out2;
@@ -743,9 +756,9 @@  static ssize_t bm_register_write(struct file *file, const char __user *buffer,
 	inode->i_fop = &bm_entry_operations;
 
 	d_instantiate(dentry, inode);
-	write_lock(&entries_lock);
-	list_add(&e->list, &entries);
-	write_unlock(&entries_lock);
+	write_lock(&bm_data->entries_lock);
+	list_add(&e->list, &bm_data->entries);
+	write_unlock(&bm_data->entries_lock);
 
 	err = 0;
 out2:
@@ -770,7 +783,8 @@  static const struct file_operations bm_register_operations = {
 static ssize_t
 bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
 {
-	char *s = enabled ? "enabled\n" : "disabled\n";
+	struct binfmt_misc *bm_data = file->f_path.dentry->d_sb->s_fs_info;
+	char *s = bm_data->enabled ? "enabled\n" : "disabled\n";
 
 	return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
 }
@@ -778,25 +792,27 @@  bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
 static ssize_t bm_status_write(struct file *file, const char __user *buffer,
 		size_t count, loff_t *ppos)
 {
+	struct binfmt_misc *bm_data = file->f_path.dentry->d_sb->s_fs_info;
 	int res = parse_command(buffer, count);
 	struct dentry *root;
 
 	switch (res) {
 	case 1:
 		/* Disable all handlers. */
-		enabled = 0;
+		bm_data->enabled = 0;
 		break;
 	case 2:
 		/* Enable all handlers. */
-		enabled = 1;
+		bm_data->enabled = 1;
 		break;
 	case 3:
 		/* Delete all handlers. */
 		root = file_inode(file)->i_sb->s_root;
 		inode_lock(d_inode(root));
 
-		while (!list_empty(&entries))
-			kill_node(list_first_entry(&entries, Node, list));
+		while (!list_empty(&bm_data->entries))
+			kill_node(bm_data, list_first_entry(
+				&bm_data->entries, Node, list));
 
 		inode_unlock(d_inode(root));
 		break;
@@ -830,8 +846,10 @@  static int bm_fill_super(struct super_block *sb, void *data, int silent)
 	};
 
 	err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
-	if (!err)
+	if (!err) {
 		sb->s_op = &s_ops;
+		sb->s_fs_info = &binfmt_data;
+	}
 	return err;
 }