cachefiles: unbind cachefiles gracefully in on-demand mode
authorJeffle Xu <jefflexu@linux.alibaba.com>
Mon, 25 Apr 2022 12:21:25 +0000 (20:21 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Tue, 17 May 2022 16:11:17 +0000 (00:11 +0800)
Add a refcount to avoid the deadlock in on-demand read mode. The
on-demand read mode will pin the corresponding cachefiles object for
each anonymous fd. The cachefiles object is unpinned when the anonymous
fd gets closed. When the user daemon exits and the fd of
"/dev/cachefiles" device node gets closed, it will wait for all
cahcefiles objects getting withdrawn. Then if there's any anonymous fd
getting closed after the fd of the device node, the user daemon will
hang forever, waiting for all objects getting withdrawn.

To fix this, add a refcount indicating if there's any object pinned by
anonymous fds. The cachefiles cache gets unbound and withdrawn when the
refcount is decreased to 0. It won't change the behaviour of the
original mode, in which case the cachefiles cache gets unbound and
withdrawn as long as the fd of the device node gets closed.

Signed-off-by: Jeffle Xu <jefflexu@linux.alibaba.com>
Link: https://lore.kernel.org/r/20220509074028.74954-4-jefflexu@linux.alibaba.com
Acked-by: David Howells <dhowells@redhat.com>
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
fs/cachefiles/daemon.c
fs/cachefiles/internal.h
fs/cachefiles/ondemand.c

index d5417da7f792f2c711b8d17770031d559b7ff473..5b1d0642c74909ad16a927e27c06e3760059eb35 100644 (file)
@@ -111,6 +111,7 @@ static int cachefiles_daemon_open(struct inode *inode, struct file *file)
        INIT_LIST_HEAD(&cache->volumes);
        INIT_LIST_HEAD(&cache->object_list);
        spin_lock_init(&cache->object_list_lock);
+       refcount_set(&cache->unbind_pincount, 1);
        xa_init_flags(&cache->reqs, XA_FLAGS_ALLOC);
        xa_init_flags(&cache->ondemand_ids, XA_FLAGS_ALLOC1);
 
@@ -164,6 +165,20 @@ static void cachefiles_flush_reqs(struct cachefiles_cache *cache)
        xa_destroy(&cache->ondemand_ids);
 }
 
+void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache)
+{
+       if (refcount_dec_and_test(&cache->unbind_pincount)) {
+               cachefiles_daemon_unbind(cache);
+               cachefiles_open = 0;
+               kfree(cache);
+       }
+}
+
+void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache)
+{
+       refcount_inc(&cache->unbind_pincount);
+}
+
 /*
  * Release a cache.
  */
@@ -179,14 +194,12 @@ static int cachefiles_daemon_release(struct inode *inode, struct file *file)
 
        if (cachefiles_in_ondemand_mode(cache))
                cachefiles_flush_reqs(cache);
-       cachefiles_daemon_unbind(cache);
 
        /* clean up the control file interface */
        cache->cachefilesd = NULL;
        file->private_data = NULL;
-       cachefiles_open = 0;
 
-       kfree(cache);
+       cachefiles_put_unbind_pincount(cache);
 
        _leave("");
        return 0;
index 4f5150a96849a22fd711baf6de30c97c1c34a6d5..e5c612888f84637ffab372f835465b6e7b71d756 100644 (file)
@@ -109,6 +109,7 @@ struct cachefiles_cache {
        char                            *rootdirname;   /* name of cache root directory */
        char                            *secctx;        /* LSM security context */
        char                            *tag;           /* cache binding tag */
+       refcount_t                      unbind_pincount;/* refcount to do daemon unbind */
        struct xarray                   reqs;           /* xarray of pending on-demand requests */
        struct xarray                   ondemand_ids;   /* xarray for ondemand_id allocation */
        u32                             ondemand_id_next;
@@ -171,6 +172,8 @@ extern int cachefiles_has_space(struct cachefiles_cache *cache,
  * daemon.c
  */
 extern const struct file_operations cachefiles_daemon_fops;
+extern void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache);
+extern void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache);
 
 /*
  * error_inject.c
index 64fc312b16d348af4c727605b961e5e7f9dcc04c..7946ee6c40bef00a6b67ce1ee2a109804ec3b1f8 100644 (file)
@@ -14,6 +14,7 @@ static int cachefiles_ondemand_fd_release(struct inode *inode,
        object->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED;
        xa_erase(&cache->ondemand_ids, object_id);
        cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
+       cachefiles_put_unbind_pincount(cache);
        return 0;
 }
 
@@ -169,6 +170,8 @@ static int cachefiles_ondemand_get_fd(struct cachefiles_req *req)
        load->fd = fd;
        req->msg.object_id = object_id;
        object->ondemand_id = object_id;
+
+       cachefiles_get_unbind_pincount(cache);
        return 0;
 
 err_put_fd: