Skip to contentSkip to conversation

Fallocate Volatile Ranges v3

RFC PATCH New John Stultz 1 participant 4 messages Jun 1, 2012

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover Fallocate Volatile Ranges v3

Just another quick iteration on the fallocate volatile ranges.

This version utilizes Dave Hansen's suggestion to use the shmem_writepage code to trigger the volatile range purging. This avoids using a shrinker as requested by KOSAKI Motohiro and makes unnecessary some of the page counting logic.

Also caught an off by one bug in the page purging.

Let me know if you have any further feedback or thoughts!

thanks
-john

CC Andrew Morton <[email protected]>
CC Android Kernel Team <[email protected]>
CC Robert Love <[email protected]>
CC Mel Gorman <[email protected]>
CC Hugh Dickins <[email protected]>
CC Dave Hansen <[email protected]>
CC Rik van Riel <[email protected]>
CC Dmitry Adamushko <[email protected]>
CC Dave Chinner <[email protected]>
CC Neil Brown <[email protected]>
CC Andrea Righi <[email protected]>
CC Aneesh Kumar K.V <[email protected]>
CC Taras Glek <[email protected]>
CC Mike Hommey <[email protected]>
CC Jan Kara <[email protected]>
CC KOSAKI Motohiro <[email protected]>

John Stultz (3):
[RFC] Interval tree implementation
[RFC] Add volatile range management code
[RFC] tmpfs: Add FALLOC_FL_MARK_VOLATILE/UNMARK_VOLATILE handlers

 fs/open.c                    |    3 +-
 include/linux/falloc.h       |    7 +-
 include/linux/intervaltree.h |   55 +++++
 include/linux/volatile.h     |   41 ++++
 lib/Makefile                 |    2 +-
 lib/intervaltree.c           |  119 ++++++++++
 mm/Makefile                  |    2 +-
 mm/shmem.c                   |   84 ++++++++
 mm/volatile.c                |  486 ++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 793 insertions(+), 6 deletions(-)
 create mode 100644 include/linux/intervaltree.h
 create mode 100644 include/linux/volatile.h
 create mode 100644 lib/intervaltree.c
 create mode 100644 mm/volatile.c
Signature · 1 line
1.7.3.2.146.gca209
#1 lore raw

RFC PATCH 1/3 Interval tree implementation

After Andrew suggested something like his mumbletree idea to better store a list of intervals, I worked on a few different approaches, and this is what I've finally managed to get working.

The idea of storing intervals in a tree is nice, but has a number of complications. When adding an interval, its possible that a large interval will consume and merge a number of smaller intervals. When removing a interval, its possible you may end up splitting an existing interval, causing one interval to become two. This makes it very difficult to provide generic list_head like behavior, as the parent structures would need to be duplicated and removed, and that has lots of memory ownership issues.

So, this is a much simplified and more list_head like implementation. You can add a node to a tree, or remove a node to a tree, but the generic implementation doesn't do the merging or splitting for you. But it does provide helpers to find overlapping and adjacent intervals.

Andrew also really wanted this interval-tree implementation to be resuable so we don't duplicate the file locking logic. I'm not totally convinced that the requirements between the volatile intervals and file locking are really equivelent, but this reduced impelementation may make it possible.

Changelog:
v2:
* Reworked code to use an rbtree instead of splaying

v3:
* Added range_tree_next_in_range() to avoid having to start lookups from the root every time.
* Fixed some comments and return NULL instead of 0, as suggested by Aneesh Kumar K.V

v6:
* Fixed range_tree_in_range() so that it finds the earliest range, rather then the first. This allows the next_in_range() function to properly cover all the ranges in the tree.
* Minor clenaups to simplify some of the functions

v7:
* Changed terminology from rangetree to intervaltree as suggested by Jan Kara

CC Andrew Morton <[email protected]>
CC Android Kernel Team <[email protected]>
CC Robert Love <[email protected]>
CC Mel Gorman <[email protected]>
CC Hugh Dickins <[email protected]>
CC Dave Hansen <[email protected]>
CC Rik van Riel <[email protected]>
CC Dmitry Adamushko <[email protected]>
CC Dave Chinner <[email protected]>
CC Neil Brown <[email protected]>
CC Andrea Righi <[email protected]>
CC Aneesh Kumar K.V <[email protected]>
CC Taras Glek <[email protected]>
CC Mike Hommey <[email protected]>
CC Jan Kara <[email protected]>
CC KOSAKI Motohiro <[email protected]>
---
 include/linux/intervaltree.h |   55 +++++++++++++++++++
 lib/Makefile                 |    2 +-
 lib/intervaltree.c           |  119 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 175 insertions(+), 1 deletions(-)
 create mode 100644 include/linux/intervaltree.h
 create mode 100644 lib/intervaltree.c
include/linux/intervaltree.h new file +55 -0
new file mode 100644
index 0000000..cfaa174
@@ -0,0 +1,55 @@
+#ifndef _LINUX_INTERVALTREE_H
+#define _LINUX_INTERVALTREE_H
+
+#include <linux/types.h>
+#include <linux/rbtree.h>
+
+struct interval_tree_node {
+	struct rb_node rb;
+	u64 start;
+	u64 end;
+};
+
+struct interval_tree_root {
+	struct rb_root head;
+};
+
+static inline void interval_tree_init(struct interval_tree_root *root)
+{
+	root->head = RB_ROOT;
+}
+
+static inline void interval_tree_node_init(struct interval_tree_node *node)
+{
+	rb_init_node(&node->rb);
+	node->start = 0;
+	node->end = 0;
+}
+
+static inline int interval_tree_empty(struct interval_tree_root *root)
+{
+	return RB_EMPTY_ROOT(&root->head);
+}
+
+static inline
+struct interval_tree_node *interval_tree_root_node(
+						struct interval_tree_root *root)
+{
+	struct interval_tree_node *ret;
+	ret = container_of(root->head.rb_node, struct interval_tree_node, rb);
+	return ret;
+}
+
+extern struct interval_tree_node *interval_tree_in_interval(
+						struct interval_tree_root *root,
+						 u64 start, u64 end);
+extern struct interval_tree_node *interval_tree_next_in_interval(
+						struct interval_tree_node *node,
+						u64 start, u64 end);
+extern void interval_tree_add(struct interval_tree_root *root,
+					struct interval_tree_node *node);
+extern void interval_tree_remove(struct interval_tree_root *root,
+					struct interval_tree_node *node);
+#endif
+
+
lib/Makefile +1 -1
index 8c31a0c..2bbad25 100644
@@ -12,7 +12,7 @@ lib-y := ctype.o string.o vsprintf.o cmdline.o \
 	 idr.o int_sqrt.o extable.o prio_tree.o \
 	 sha1.o md5.o irq_regs.o reciprocal_div.o argv_split.o \
 	 proportions.o prio_heap.o ratelimit.o show_mem.o \
-	 is_single_threaded.o plist.o decompress.o
+	 is_single_threaded.o plist.o decompress.o intervaltree.o
 
 lib-$(CONFIG_MMU) += ioremap.o
 lib-$(CONFIG_SMP) += cpumask.o
lib/intervaltree.c new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
1.7.3.2.146.gca209
#2 lore raw

RFC PATCH 2/3 Add volatile range management code

In reply to John Stultz

This patch provides the volatile range management code that filesystems can utilize when implementing FALLOC_FL_MARK_VOLATILE.

It tracks a collection of page ranges against a mapping stored in an interval-tree. This code handles coalescing overlapping and adjacent ranges, as well as splitting ranges when sub-chunks are removed.

The ranges can be marked purged or unpurged. And there is a per-fs lru list that tracks all the unpurged ranges for that fs.

v2:
* Fix bug in volatile_ranges_get_last_used returning bad start,end values
* Rework for intervaltree renaming
* Optimize volatile_range_lru_size to avoid running through lru list each time.

v3:
* Improve function name to make it clear what the volatile_ranges_pluck_lru() code does.
* Drop volatile_range_lru_size and unpurged_page_count mangement as its now unused

CC Andrew Morton <[email protected]>
CC Android Kernel Team <[email protected]>
CC Robert Love <[email protected]>
CC Mel Gorman <[email protected]>
CC Hugh Dickins <[email protected]>
CC Dave Hansen <[email protected]>
CC Rik van Riel <[email protected]>
CC Dmitry Adamushko <[email protected]>
CC Dave Chinner <[email protected]>
CC Neil Brown <[email protected]>
CC Andrea Righi <[email protected]>
CC Aneesh Kumar K.V <[email protected]>
CC Taras Glek <[email protected]>
CC Mike Hommey <[email protected]>
CC Jan Kara <[email protected]>
CC KOSAKI Motohiro <[email protected]>
---
 include/linux/volatile.h |   41 ++++
 mm/Makefile              |    2 +-
 mm/volatile.c            |  486 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 528 insertions(+), 1 deletions(-)
 create mode 100644 include/linux/volatile.h
 create mode 100644 mm/volatile.c
include/linux/volatile.h new file +41 -0
new file mode 100644
index 0000000..8deee06
@@ -0,0 +1,41 @@
+#ifndef _LINUX_VOLATILE_H
+#define _LINUX_VOLATILE_H
+
+#include <linux/fs.h>
+
+struct volatile_fs_head {
+	struct mutex lock;
+	struct list_head lru_head;
+};
+
+
+#define DEFINE_VOLATILE_FS_HEAD(name) struct volatile_fs_head name = {	\
+	.lock = __MUTEX_INITIALIZER(name.lock),				\
+	.lru_head = LIST_HEAD_INIT(name.lru_head),			\
+}
+
+
+static inline void volatile_range_lock(struct volatile_fs_head *head)
+{
+	mutex_lock(&head->lock);
+}
+
+static inline void volatile_range_unlock(struct volatile_fs_head *head)
+{
+	mutex_unlock(&head->lock);
+}
+
+extern long volatile_range_add(struct volatile_fs_head *head,
+				struct address_space *mapping,
+				pgoff_t start_index, pgoff_t end_index);
+extern long volatile_range_remove(struct volatile_fs_head *head,
+				struct address_space *mapping,
+				pgoff_t start_index, pgoff_t end_index);
+
+extern void volatile_range_clear(struct volatile_fs_head *head,
+					struct address_space *mapping);
+
+extern s64 volatile_ranges_pluck_lru(struct volatile_fs_head *head,
+				struct address_space **mapping,
+				loff_t *start, loff_t *end);
+#endif /* _LINUX_VOLATILE_H */
mm/Makefile +1 -1
index a156285..dc79eb8 100644
@@ -16,7 +16,7 @@ obj-y			:= filemap.o mempool.o oom_kill.o fadvise.o \
 			   readahead.o swap.o truncate.o vmscan.o shmem.o \
 			   prio_tree.o util.o mmzone.o vmstat.o backing-dev.o \
 			   page_isolation.o mm_init.o mmu_context.o percpu.o \
-			   compaction.o $(mmu-y)
+			   compaction.o volatile.o $(mmu-y)
 obj-y += init-mm.o
 
 ifdef CONFIG_NO_BOOTMEM
mm/volatile.c new file +486 -0

486 lines not on this page · raw message

Signature · 1 line
1.7.3.2.146.gca209
#3 lore raw

RFC PATCH 3/3 tmpfs: Add FALLOC_FL_MARK_VOLATILE/UNMARK_VOLATILE handlers

In reply to John Stultz

This patch enables FALLOC_FL_MARK_VOLATILE/UNMARK_VOLATILE functionality for tmpfs making use of the volatile range management code.

Conceptually, FALLOC_FL_MARK_VOLATILE is like a delayed FALLOC_FL_PUNCH_HOLE. This allows applications that have data caches that can be re-created to tell the kernel that some memory contains data that is useful in the future, but can be recreated if needed, so if the kernel needs, it can zap the memory without having to swap it out.

In use, applications use FALLOC_FL_MARK_VOLATILE to mark page ranges as volatile when they are not in use. Then later if they wants to reuse the data, they use FALLOC_FL_UNMARK_VOLATILE, which will return an error if the data has been purged.

This is very much influenced by the Android Ashmem interface by Robert Love so credits to him and the Android developers. In many cases the code & logic come directly from the ashmem patch. The intent of this patch is to allow for ashmem-like behavior, but embeds the idea a little deeper into the VM code.

This is a reworked version of the fadvise volatile idea submitted earlier to the list. Thanks to Dave Chinner for suggesting to rework the idea in this fashion. Also thanks to Dmitry Adamushko for continued review and bug reporting, and Dave Hansen for help with the original design and mentoring me in the VM code.

v3:
* Fix off by one issue when truncating page ranges
* Use Dave Hansesn's suggestion to use shmem_writepage to trigger range purging instead of using a shrinker.

CC Andrew Morton <[email protected]>
CC Android Kernel Team <[email protected]>
CC Robert Love <[email protected]>
CC Mel Gorman <[email protected]>
CC Hugh Dickins <[email protected]>
CC Dave Hansen <[email protected]>
CC Rik van Riel <[email protected]>
CC Dmitry Adamushko <[email protected]>
CC Dave Chinner <[email protected]>
CC Neil Brown <[email protected]>
CC Andrea Righi <[email protected]>
CC Aneesh Kumar K.V <[email protected]>
CC Taras Glek <[email protected]>
CC Mike Hommey <[email protected]>
CC Jan Kara <[email protected]>
CC KOSAKI Motohiro <[email protected]>
---
 fs/open.c              |    3 +-
 include/linux/falloc.h |    7 ++--
 mm/shmem.c             |   84 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 90 insertions(+), 4 deletions(-)
fs/open.c +2 -1
index d543012..448ed5a 100644
@@ -223,7 +223,8 @@ int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
 		return -EINVAL;
 
 	/* Return error if mode is not supported */
-	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
+	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
+			FALLOC_FL_MARK_VOLATILE | FALLOC_FL_UNMARK_VOLATILE))
 		return -EOPNOTSUPP;
 
 	/* Punch hole must have keep size set */
include/linux/falloc.h +4 -3
index 73e0b62..3e47ad5 100644
@@ -1,9 +1,10 @@
 #ifndef _FALLOC_H_
 #define _FALLOC_H_
 
-#define FALLOC_FL_KEEP_SIZE	0x01 /* default is extend size */
-#define FALLOC_FL_PUNCH_HOLE	0x02 /* de-allocates range */
-
+#define FALLOC_FL_KEEP_SIZE		0x01 /* default is extend size */
+#define FALLOC_FL_PUNCH_HOLE		0x02 /* de-allocates range */
+#define FALLOC_FL_MARK_VOLATILE		0x04 /* mark range volatile */
+#define FALLOC_FL_UNMARK_VOLATILE	0x08 /* mark range non-volatile */
 #ifdef __KERNEL__
 
 /*
mm/shmem.c +84 -0

119 lines not on this page · raw message

Signature · 1 line
1.7.3.2.146.gca209