[PATCH 36/52] fuse: Kick worker when free memory drops below 20% of total ranges

From: Vivek Goyal
Date: Mon Dec 10 2018 - 12:18:22 EST


Kick worker to free up some memory when number of free ranges drops below
20% of total free ranges at the time of initialization.

Signed-off-by: Vivek Goyal <vgoyal@xxxxxxxxxx>
---
fs/fuse/file.c | 11 ++++++++++-
fs/fuse/fuse_i.h | 9 +++++++++
fs/fuse/inode.c | 1 +
3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index 13db83d105ff..1f172d372eeb 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -186,6 +186,7 @@ static void fuse_link_write_file(struct file *file)

static struct fuse_dax_mapping *alloc_dax_mapping(struct fuse_conn *fc)
{
+ unsigned long free_threshold;
struct fuse_dax_mapping *dmap = NULL;

spin_lock(&fc->lock);
@@ -193,7 +194,7 @@ static struct fuse_dax_mapping *alloc_dax_mapping(struct fuse_conn *fc)
/* TODO: Add logic to try to free up memory if wait is allowed */
if (fc->nr_free_ranges <= 0) {
spin_unlock(&fc->lock);
- return NULL;
+ goto out_kick;
}

WARN_ON(list_empty(&fc->free_ranges));
@@ -204,6 +205,14 @@ static struct fuse_dax_mapping *alloc_dax_mapping(struct fuse_conn *fc)
list_del_init(&dmap->list);
fc->nr_free_ranges--;
spin_unlock(&fc->lock);
+
+out_kick:
+ /* If number of free ranges are below threshold, start reclaim */
+ free_threshold = (fc->nr_ranges * FUSE_DAX_RECLAIM_THRESHOLD)/100;
+ if (free_threshold > 0 && fc->nr_free_ranges < free_threshold) {
+ pr_debug("fuse: Kicking dax memory reclaim worker. nr_free_ranges=0x%ld nr_total_ranges=%ld\n", fc->nr_free_ranges, fc->nr_ranges);
+ queue_delayed_work(system_long_wq, &fc->dax_free_work, 0);
+ }
return dmap;
}

diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h
index 383deaf0ecf1..bbefa7c11078 100644
--- a/fs/fuse/fuse_i.h
+++ b/fs/fuse/fuse_i.h
@@ -53,6 +53,13 @@
/* Number of ranges reclaimer will try to free in one invocation */
#define FUSE_DAX_RECLAIM_CHUNK (10)

+/*
+ * Dax memory reclaim threshold in percetage of total ranges. When free
+ * number of free ranges drops below this threshold, reclaim can trigger
+ * Default is 20%
+ * */
+#define FUSE_DAX_RECLAIM_THRESHOLD (20)
+
/** List of active connections */
extern struct list_head fuse_conn_list;

@@ -885,6 +892,8 @@ struct fuse_conn {
*/
unsigned long nr_free_ranges;
struct list_head free_ranges;
+
+ unsigned long nr_ranges;
};

static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c
index 44f7bc44e319..d31acb97eede 100644
--- a/fs/fuse/inode.c
+++ b/fs/fuse/inode.c
@@ -675,6 +675,7 @@ static int fuse_dax_mem_range_init(struct fuse_conn *fc,

list_replace_init(&mem_ranges, &fc->free_ranges);
fc->nr_free_ranges = allocated_ranges;
+ fc->nr_ranges = allocated_ranges;
return 0;
out_err:
/* Free All allocated elements */
--
2.13.6