dmap_removemapping_list() allocates an array of fuse_removemapping_one
entries to batch the FUSE_REMOVEMAPPING request, and returns -ENOMEM if
that fails.  The only caller, inode_reclaim_dmap_range(), has no way to
recover: it just warns and returns the ranges to the free pool, where
they can be handed to another inode while the server still has them
mapped.  The request is never retried either, so a single failed
allocation is enough to lose the removal.

The allocation is up to FUSE_REMOVEMAPPING_MAX_ENTRY (PAGE_SIZE / 16)
entries, i.e. a page, and it is GFP_NOFS.  Inode eviction and the DAX
free worker are exactly the paths that run under memory pressure, so
this is not hypothetical.

The batch array is only a convenience: dmap_removemapping_one() already
sends a single entry entirely from the stack.  Move it above
dmap_removemapping_list() and use it to remove the ranges one at a time
when the array cannot be allocated.  That is slower, but it cannot fail
for lack of memory, and it only happens under memory pressure.

While at it, initialize ret, which was left uninitialized if the list
passed in was empty.  The current caller checks for that beforehand, so
this is not a bug today.

Fixes: c2d0ad00d948 ("virtiofs: implement dax read/write operations")
Signed-off-by: Miklos Szeredi <[email protected]>
---
 fs/fuse/dax.c | 49 +++++++++++++++++++++++++++++++------------------
 1 file changed, 31 insertions(+), 18 deletions(-)

diff --git a/fs/fuse/dax.c b/fs/fuse/dax.c
index e20a5ae6fecc..c5bbb5d91026 100644
--- a/fs/fuse/dax.c
+++ b/fs/fuse/dax.c
@@ -248,18 +248,46 @@ static int fuse_send_removemapping(struct inode *inode,
        return fuse_simple_request(fm, &args);
 }
 
+static int dmap_removemapping_one(struct inode *inode,
+                                 struct fuse_dax_mapping *dmap)
+{
+       struct fuse_removemapping_one forget_one;
+       struct fuse_removemapping_in inarg;
+
+       memset(&inarg, 0, sizeof(inarg));
+       inarg.count = 1;
+       memset(&forget_one, 0, sizeof(forget_one));
+       forget_one.moffset = dmap->window_offset;
+       forget_one.len = dmap->length;
+
+       return fuse_send_removemapping(inode, &inarg, &forget_one);
+}
+
 static int dmap_removemapping_list(struct inode *inode, unsigned int num,
                                   struct list_head *to_remove)
 {
        struct fuse_removemapping_one *remove_one, *ptr;
        struct fuse_removemapping_in inarg;
        struct fuse_dax_mapping *dmap;
-       int ret, i = 0, nr_alloc;
+       int ret = 0, i = 0, nr_alloc;
 
        nr_alloc = min_t(unsigned int, num, FUSE_REMOVEMAPPING_MAX_ENTRY);
        remove_one = kmalloc_objs(*remove_one, nr_alloc, GFP_NOFS);
-       if (!remove_one)
-               return -ENOMEM;
+       if (!remove_one) {
+               /*
+                * The ranges are not reusable until the server has dropped
+                * the mappings, and the request is never retried, so failing
+                * here would take them out of use for good.  Fall back to
+                * removing them one at a time, which needs no allocation;
+                * slow, but this only happens under memory pressure.
+                */
+               list_for_each_entry(dmap, to_remove, list) {
+                       ret = dmap_removemapping_one(inode, dmap);
+                       if (ret)
+                               break;
+               }
+               return ret;
+       }
 
        ptr = remove_one;
        list_for_each_entry(dmap, to_remove, list) {
@@ -350,21 +378,6 @@ static void inode_reclaim_dmap_range(struct fuse_conn_vdax 
*fcd,
        spin_unlock(&fcd->lock);
 }
 
-static int dmap_removemapping_one(struct inode *inode,
-                                 struct fuse_dax_mapping *dmap)
-{
-       struct fuse_removemapping_one forget_one;
-       struct fuse_removemapping_in inarg;
-
-       memset(&inarg, 0, sizeof(inarg));
-       inarg.count = 1;
-       memset(&forget_one, 0, sizeof(forget_one));
-       forget_one.moffset = dmap->window_offset;
-       forget_one.len = dmap->length;
-
-       return fuse_send_removemapping(inode, &inarg, &forget_one);
-}
-
 /*
  * It is called from evict_inode() and by that time inode is going away. So
  * this function does not take any locks like fi->vdax->sem for traversing
-- 
2.56.1


Reply via email to