From: Jia Zhu zhujia.zj@bytedance.com
anolis inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/IB5UKT
Reference: https://gitee.com/anolis/cloud-kernel/commit/679445f70359
--------------------------------
ANBZ: #3213
cherry-picked from [1]
We'll introduce a @work_struct field for @object in subsequent patches, it will enlarge the size of @object. As the result of that, this commit extracts ondemand info field from @object.
Reviewed-by: Jingbo Xu jefflexu@linux.alibaba.com Signed-off-by: Jia Zhu zhujia.zj@bytedance.com Link: [1] https://lore.kernel.org/lkml/20221014080559.42108-5-zhujia.zj@bytedance.com/... Signed-off-by: Jingbo Xu jefflexu@linux.alibaba.com Acked-by: Gao Xiang hsiangkao@linux.alibaba.com Acked-by: Joseph Qi joseph.qi@linux.alibaba.com Link: https://gitee.com/anolis/cloud-kernel/pulls/894 Signed-off-by: Zizhi Wo wozizhi@huawei.com Signed-off-by: Baokun Li libaokun1@huawei.com --- fs/cachefiles/interface.c | 6 ++++++ fs/cachefiles/internal.h | 22 ++++++++++++++++------ fs/cachefiles/ondemand.c | 29 +++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 12 deletions(-)
diff --git a/fs/cachefiles/interface.c b/fs/cachefiles/interface.c index 634e7041c0f3..0a946d046724 100644 --- a/fs/cachefiles/interface.c +++ b/fs/cachefiles/interface.c @@ -51,6 +51,9 @@ static struct fscache_object *cachefiles_alloc_object(
fscache_object_init(&object->fscache, cookie, &cache->cache);
+ if (cachefiles_ondemand_init_obj_info(object)) + goto nomem_obj_info; + object->type = cookie->def->type;
/* get hold of the raw key @@ -102,6 +105,8 @@ static struct fscache_object *cachefiles_alloc_object( nomem_key: kfree(buffer); nomem_buffer: + kfree(object->private); +nomem_obj_info: BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)); kmem_cache_free(cachefiles_object_jar, object); fscache_object_destroyed(&cache->cache); @@ -373,6 +378,7 @@ static void cachefiles_put_object(struct fscache_object *_object, }
cache = object->fscache.cache; + kfree(object->private); fscache_object_destroy(&object->fscache); kmem_cache_free(cachefiles_object_jar, object); fscache_object_destroyed(cache); diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index 20d4c41bed2e..c44782e12ab1 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -36,6 +36,12 @@ enum cachefiles_object_state { CACHEFILES_ONDEMAND_OBJSTATE_open, /* Anonymous fd associated with object is available */ };
+struct cachefiles_ondemand_info { + int ondemand_id; + enum cachefiles_object_state state; + struct cachefiles_object *object; +}; + /* * node records */ @@ -53,10 +59,7 @@ struct cachefiles_object { uint8_t new; /* T if object new */ spinlock_t work_lock; struct rb_node active_node; /* link in active tree (dentry is key) */ -#ifdef CONFIG_CACHEFILES_ONDEMAND - int ondemand_id; - enum cachefiles_object_state state; -#endif + struct cachefiles_ondemand_info *private; };
extern struct kmem_cache *cachefiles_object_jar; @@ -270,17 +273,19 @@ extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object); extern int cachefiles_ondemand_read(struct cachefiles_object *object, loff_t pos, size_t len);
+extern int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object); + #define CACHEFILES_OBJECT_STATE_FUNCS(_state) \ static inline bool \ cachefiles_ondemand_object_is_##_state(const struct cachefiles_object *object) \ { \ - return object->state == CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ + return object->private->state == CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ } \ \ static inline void \ cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \ { \ - object->state = CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ + object->private->state = CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ }
CACHEFILES_OBJECT_STATE_FUNCS(open); @@ -305,6 +310,11 @@ static inline int cachefiles_ondemand_read(struct cachefiles_object *object, { return -EOPNOTSUPP; } + +static inline int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object) +{ + return 0; +} #endif
/* diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index 253d7e3a2bf5..27da8cc76899 100644 --- a/fs/cachefiles/ondemand.c +++ b/fs/cachefiles/ondemand.c @@ -13,17 +13,18 @@ static int cachefiles_ondemand_fd_release(struct inode *inode, struct file *file) { struct cachefiles_object *object = file->private_data; - int object_id = object->ondemand_id; struct cachefiles_cache *cache; void **slot; struct radix_tree_iter iter; + struct cachefiles_ondemand_info *info = object->private; + int object_id = info->ondemand_id; struct cachefiles_req *req;
cache = container_of(object->fscache.cache, struct cachefiles_cache, cache);
xa_lock(&cache->reqs); - object->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED; + info->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED; cachefiles_ondemand_set_object_close(object);
/* @@ -235,7 +236,7 @@ static int cachefiles_ondemand_get_fd(struct cachefiles_req *req) load = (void *)req->msg.data; load->fd = fd; req->msg.object_id = object_id; - object->ondemand_id = object_id; + object->private->ondemand_id = object_id;
cachefiles_get_unbind_pincount(cache); return 0; @@ -404,7 +405,7 @@ static int cachefiles_ondemand_send_req(struct cachefiles_object *object, smp_mb(); if (opcode != CACHEFILES_OP_OPEN && !cachefiles_ondemand_object_is_open(object)) { - WARN_ON_ONCE(object->ondemand_id == 0); + WARN_ON_ONCE(object->private->ondemand_id == 0); xa_unlock(&cache->reqs); ret = -EIO; goto out; @@ -471,7 +472,7 @@ static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req, if (!cachefiles_ondemand_object_is_open(object)) return -ENOENT;
- req->msg.object_id = object->ondemand_id; + req->msg.object_id = object->private->ondemand_id; return 0; }
@@ -486,7 +487,7 @@ static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req, struct cachefiles_object *object = req->object; struct cachefiles_read *load = (void *)req->msg.data; struct cachefiles_read_ctx *read_ctx = private; - int object_id = object->ondemand_id; + int object_id = object->private->ondemand_id;
/* Stop enqueuing requests when daemon has closed anon_fd. */ if (!cachefiles_ondemand_object_is_open(object)) { @@ -539,3 +540,19 @@ int cachefiles_ondemand_read(struct cachefiles_object *object, sizeof(struct cachefiles_read), cachefiles_ondemand_init_read_req, &read_ctx); } + +int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object) +{ + struct cachefiles_cache *cache; + + cache = container_of(object->fscache.cache, struct cachefiles_cache, cache); + if (!cachefiles_in_ondemand_mode(cache)) + return 0; + + object->private = kzalloc(sizeof(struct cachefiles_ondemand_info), GFP_KERNEL); + if (!object->private) + return -ENOMEM; + + object->private->object = object; + return 0; +}