[RH7] cgroup: add export_operations to cgroup super block

Submitted by Andrey Zhadchenko on July 24, 2020, 3:19 p.m.

Details

Message ID AM5PR0802MB25789D14841EF4E821D90578E1770@AM5PR0802MB2578.eurprd08.prod.outlook.com
State New
Series "cgroup: add export_operations to cgroup super block"
Headers show

Commit Message

Andrey Zhadchenko July 24, 2020, 3:19 p.m.
criu uses fhandle from fdinfo to dump inotify objects. cgroup super block has
no export operations, but .encode_fh and .fh_to_dentry are needed for
inotify_fdinfo function and open_by_handle_at syscall in order to correctly
open files located on cgroupfs by fhandle.
Add hash table as a storage for inodes with exported fhandle.

https://jira.sw.ru/browse/PSBM-105889
Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
---
 kernel/cgroup.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 106 insertions(+), 1 deletion(-)

--
1.8.3.1

Patch hide | download patch | download mbox

diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index 9fdba79..a06c809 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -62,6 +62,7 @@ 
 #include <linux/kthread.h>
 #include <linux/ve.h>
 #include <linux/stacktrace.h>
+#include <linux/exportfs.h>

 #include <linux/atomic.h>

@@ -1390,9 +1391,112 @@  out:
 }
 #endif

+/*
+ * hashtable for inodes that have exported fhandles.
+ * When we export fhandle, we add it's inode into
+ * hashtable so we can find it fast
+ */
+
+#define CGROUP_INODE_HASH_BITS 10
+static DEFINE_HASHTABLE(cgroup_inode_table, CGROUP_INODE_HASH_BITS);
+static DEFINE_SPINLOCK(cgroup_inode_table_lock);
+
+struct cgroup_inode_hash_item {
+ struct inode* inode;
+ struct hlist_node hlist;
+};
+
+static inline unsigned long cgroup_inode_get_hash(unsigned int i_ino)
+{
+ return hash_32(i_ino, CGROUP_INODE_HASH_BITS);
+}
+
+static struct cgroup_inode_hash_item* cgroup_inode_hash_find(unsigned int i_ino)
+{
+ struct cgroup_inode_hash_item *i;
+ struct hlist_head *head = cgroup_inode_table + cgroup_inode_get_hash(i_ino);
+ struct cgroup_inode_hash_item* found = 0;
+
+ spin_lock(&cgroup_inode_table_lock);
+ hlist_for_each_entry(i, head, hlist) {
+ if(i->inode->i_ino == i_ino) {
+ found = i;
+ break;
+ }
+ }
+ spin_unlock(&cgroup_inode_table_lock);
+
+ return found;
+}
+
+static struct dentry *cgroup_fh_to_dentry(struct super_block *sb,
+ struct fid *fid, int fh_len, int fh_type)
+{
+ struct cgroup_inode_hash_item *item;
+ struct dentry *dentry = ERR_PTR(-ENOENT);
+
+ if (fh_len < 1)
+ return NULL;
+
+ item = cgroup_inode_hash_find(fid->raw[0]);
+ if (item)
+ dentry = d_find_alias(item->inode);
+
+ return dentry;
+}
+
+static int cgroup_encode_fh(struct inode *inode, __u32 *fh, int *len,
+ struct inode *parent)
+{
+ struct hlist_head *head = cgroup_inode_table + cgroup_inode_get_hash(inode->i_ino);
+ struct cgroup_inode_hash_item *item;
+
+ if (*len < 1) {
+ *len = 1;
+ return FILEID_INVALID;
+ }
+
+ if(cgroup_inode_hash_find(inode->i_ino) == 0) {
+ item = kmalloc(sizeof(struct cgroup_inode_hash_item), GFP_KERNEL);
+ /*
+ * encode_fh is expected to return 255 (FILEID_INVALID) in case of
+ * failure. We can't return ENOMEM, so return FILEID_INVALID at least
+ */
+ if(!item)
+ return FILEID_INVALID;
+ item->inode = inode;
+
+ spin_lock(&cgroup_inode_table_lock);
+ hlist_add_head(&item->hlist, head);
+ spin_unlock(&cgroup_inode_table_lock);
+ }
+
+ fh[0] = inode->i_ino;
+ *len = 1;
+ return 1;
+}
+
+static const struct export_operations cgroup_export_ops = {
+ .encode_fh      = cgroup_encode_fh,
+ .fh_to_dentry = cgroup_fh_to_dentry,
+};
+
+static int cgroup_delete_inode(struct inode *inode){
+ struct cgroup_inode_hash_item *item = cgroup_inode_hash_find(inode->i_ino);
+ if(item) {
+ spin_lock(&cgroup_inode_table_lock);
+ hlist_del(&item->hlist);
+ spin_unlock(&cgroup_inode_table_lock);
+
+ kfree(item);
+ }
+
+ return generic_delete_inode(inode);
+}
+
 static const struct super_operations cgroup_ops = {
  .statfs = simple_statfs,
- .drop_inode = generic_delete_inode,
+ .drop_inode = cgroup_delete_inode,
  .show_options = cgroup_show_options,
 #ifdef CONFIG_VE
  .show_path = cgroup_show_path,
@@ -1539,6 +1643,7 @@  static int cgroup_set_super(struct super_block *sb, void *data)
  sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  sb->s_magic = CGROUP_SUPER_MAGIC;
  sb->s_op = &cgroup_ops;
+ sb->s_export_op = &cgroup_export_ops;

  return 0;
 }

Comments

Vasily Averin July 25, 2020, 6:12 a.m.
Pavel,

it looks correct for me,
though could you please review it too?

thank you,
	Vasily Averin

On 7/24/20 6:19 PM, Andrey Zhadchenko wrote:
> criu uses fhandle from fdinfo to dump inotify objects. cgroup super block has
> no export operations, but .encode_fh and .fh_to_dentry are needed for
> inotify_fdinfo function and open_by_handle_at syscall in order to correctly
> open files located on cgroupfs by fhandle.
> Add hash table as a storage for inodes with exported fhandle.
> 
> https://jira.sw.ru/browse/PSBM-105889
> Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
> ---
>  kernel/cgroup.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 106 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/cgroup.c b/kernel/cgroup.c
> index 9fdba79..a06c809 100644
> --- a/kernel/cgroup.c
> +++ b/kernel/cgroup.c
> @@ -62,6 +62,7 @@
>  #include <linux/kthread.h>
>  #include <linux/ve.h>
>  #include <linux/stacktrace.h>
> +#include <linux/exportfs.h>
>  
>  #include <linux/atomic.h>
>  
> @@ -1390,9 +1391,112 @@ out:
>  }
>  #endif
>  
> +/*
> + * hashtable for inodes that have exported fhandles.
> + * When we export fhandle, we add it's inode into
> + * hashtable so we can find it fast
> + */
> +
> +#define CGROUP_INODE_HASH_BITS 10
> +static DEFINE_HASHTABLE(cgroup_inode_table, CGROUP_INODE_HASH_BITS);
> +static DEFINE_SPINLOCK(cgroup_inode_table_lock);
> +
> +struct cgroup_inode_hash_item {
> + struct inode* inode;
> + struct hlist_node hlist;
> +};
> +
> +static inline unsigned long cgroup_inode_get_hash(unsigned int i_ino)
> +{
> + return hash_32(i_ino, CGROUP_INODE_HASH_BITS);
> +}
> +
> +static struct cgroup_inode_hash_item* cgroup_inode_hash_find(unsigned int i_ino)
> +{
> + struct cgroup_inode_hash_item *i;
> + struct hlist_head *head = cgroup_inode_table + cgroup_inode_get_hash(i_ino);
> + struct cgroup_inode_hash_item* found = 0;
> +
> + spin_lock(&cgroup_inode_table_lock);
> + hlist_for_each_entry(i, head, hlist) {
> + if(i->inode->i_ino == i_ino) {
> + found = i;
> + break;
> + }
> + }
> + spin_unlock(&cgroup_inode_table_lock);
> +
> + return found;
> +}
> +
> +static struct dentry *cgroup_fh_to_dentry(struct super_block *sb,
> + struct fid *fid, int fh_len, int fh_type)
> +{
> + struct cgroup_inode_hash_item *item;
> + struct dentry *dentry = ERR_PTR(-ENOENT);
> +
> + if (fh_len < 1)
> + return NULL;
> +
> + item = cgroup_inode_hash_find(fid->raw[0]);
> + if (item)
> + dentry = d_find_alias(item->inode);
> +
> + return dentry;
> +}
> +
> +static int cgroup_encode_fh(struct inode *inode, __u32 *fh, int *len,
> + struct inode *parent)
> +{
> + struct hlist_head *head = cgroup_inode_table + cgroup_inode_get_hash(inode->i_ino);
> + struct cgroup_inode_hash_item *item;
> +
> + if (*len < 1) {
> + *len = 1;
> + return FILEID_INVALID;
> + }
> +
> + if(cgroup_inode_hash_find(inode->i_ino) == 0) {
> + item = kmalloc(sizeof(struct cgroup_inode_hash_item), GFP_KERNEL);
> + /*
> + * encode_fh is expected to return 255 (FILEID_INVALID) in case of
> + * failure. We can't return ENOMEM, so return FILEID_INVALID at least
> + */
> + if(!item)
> + return FILEID_INVALID;
> + item->inode = inode;
> +
> + spin_lock(&cgroup_inode_table_lock);
> + hlist_add_head(&item->hlist, head);
> + spin_unlock(&cgroup_inode_table_lock);
> + }
> +
> + fh[0] = inode->i_ino;
> + *len = 1;
> + return 1;
> +}
> +
> +static const struct export_operations cgroup_export_ops = {
> + .encode_fh      = cgroup_encode_fh,
> + .fh_to_dentry = cgroup_fh_to_dentry,
> +};
> +
> +static int cgroup_delete_inode(struct inode *inode){
> + struct cgroup_inode_hash_item *item = cgroup_inode_hash_find(inode->i_ino);
> + if(item) {
> + spin_lock(&cgroup_inode_table_lock);
> + hlist_del(&item->hlist);
> + spin_unlock(&cgroup_inode_table_lock);
> +
> + kfree(item);
> + }
> +
> + return generic_delete_inode(inode);
> +}
> +
>  static const struct super_operations cgroup_ops = {
>   .statfs = simple_statfs,
> - .drop_inode = generic_delete_inode,
> + .drop_inode = cgroup_delete_inode,
>   .show_options = cgroup_show_options,
>  #ifdef CONFIG_VE
>   .show_path = cgroup_show_path,
> @@ -1539,6 +1643,7 @@ static int cgroup_set_super(struct super_block *sb, void *data)
>   sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
>   sb->s_magic = CGROUP_SUPER_MAGIC;
>   sb->s_op = &cgroup_ops;
> + sb->s_export_op = &cgroup_export_ops;
>  
>   return 0;
>  }
> -- 
> 1.8.3.1
> 
> 
> _______________________________________________
> Devel mailing list
> Devel@openvz.org
> https://lists.openvz.org/mailman/listinfo/devel
>