Skip to contentSkip to conversation

Fallocate Volatile Ranges

RFC PATCH In review John Stultz 2 participants 9 messages May 25, 2012 – May 31, 2012

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

cover Fallocate Volatile Ranges

So here's the first pass at re-implementing the volatile range idea using the fallocate interface, as suggested by Dave Chinner.

The range tree code is mostly unchanged.

The volatile range management code has been somewhat abstracted out as helper functions that allow filesystems to get the same logic but leaving it to the filesystem to handle the page purging.

Finally there is the tmpfs enablement for FALLOC_FL_MARK_VOLATILE.

This is all done on top of Hugh/Cong's tmpfs FALLOC_FL_PUNCH_HOLE enablement.

I've only managed to do minimal testing at this point, but I wanted to get this out before the long-weekend holiday in the US.

Few of the things still on my todo list:
* Move the volatile mapping hash to be per volatile_fs_head
* Kill the extra hash locking
* Keep track of unpurged pages in volatile_fs_head, to avoid running through the lru at shrink time.

Your thoughts and feedback will be appreciated!

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]>

Hugh Dickins (1):
tmpfs: support fallocate FALLOC_FL_PUNCH_HOLE

John Stultz (3):
[RFC] Range 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/rangetree.h |   53 +++++
 include/linux/volatile.h  |   43 ++++
 lib/Makefile              |    2 +-
 lib/rangetree.c           |  107 ++++++++++
 mm/Makefile               |    2 +-
 mm/shmem.c                |  175 +++++++++++++++-
 mm/volatile.c             |  493 +++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 868 insertions(+), 17 deletions(-)
 create mode 100644 include/linux/rangetree.h
 create mode 100644 include/linux/volatile.h
 create mode 100644 lib/rangetree.c
 create mode 100644 mm/volatile.c
Signature · 1 line
1.7.3.2.146.gca209
#1 lore raw

PATCH 1/4 tmpfs: support fallocate FALLOC_FL_PUNCH_HOLE

From: Hugh Dickins <[email protected]>

tmpfs has supported hole-punching since 2.6.16, via madvise(,,MADV_REMOVE). But nowadays fallocate(,FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE,,) is the agreed way to punch holes.

So add shmem_fallocate() to support that, and tweak shmem_truncate_range() to support partial pages at both the beginning and end of range (never needed for madvise, which demands rounded addr and rounds up length).

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]>
---
 mm/shmem.c |   68 ++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 57 insertions(+), 11 deletions(-)
mm/shmem.c +57 -11
index d7b433a..9b1c6b4 100644
@@ -53,6 +53,7 @@ static struct vfsmount *shm_mnt;
 #include <linux/blkdev.h>
 #include <linux/pagevec.h>
 #include <linux/percpu_counter.h>
+#include <linux/falloc.h>
 #include <linux/splice.h>
 #include <linux/security.h>
 #include <linux/swapops.h>
@@ -429,21 +430,23 @@ void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
 	struct address_space *mapping = inode->i_mapping;
 	struct shmem_inode_info *info = SHMEM_I(inode);
 	pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
-	unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
-	pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
+	pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
+	unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
+	unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
 	struct pagevec pvec;
 	pgoff_t indices[PAGEVEC_SIZE];
 	long nr_swaps_freed = 0;
 	pgoff_t index;
 	int i;
 
-	BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
+	if (lend == -1)
+		end = -1;	/* unsigned, so actually very big */
 
 	pagevec_init(&pvec, 0);
 	index = start;
-	while (index <= end) {
+	while (index < end) {
 		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
-			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
+				min(end - index, (pgoff_t)PAGEVEC_SIZE),
 							pvec.pages, indices);
 		if (!pvec.nr)
 			break;
@@ -452,7 +455,7 @@ void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
 			struct page *page = pvec.pages[i];
 
 			index = indices[i];
-			if (index > end)
+			if (index >= end)
 				break;
 
 			if (radix_tree_exceptional_entry(page)) {
@@ -476,22 +479,39 @@ void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
 		index++;
 	}
 
-	if (partial) {
+	if (partial_start) {
 		struct page *page = NULL;
 		shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
 		if (page) {
-			zero_user_segment(page, partial, PAGE_CACHE_SIZE);
+			unsigned int top = PAGE_CACHE_SIZE;
+			if (start > end) {
+				top = partial_end;
+				partial_end = 0;
+			}
+			zero_user_segment(page, partial_start, top);
+			set_page_dirty(page);
+			unlock_page(page);
+			page_cache_release(page);
+		}
+	}
+	if (partial_end) {
+		struct page *page = NULL;
+		shmem_getpage(inode, end, &page, SGP_READ, NULL);
+		if (page) {
+			zero_user_segment(page, 0, partial_end);
 			set_page_dirty(page);
 			unlock_page(page);
 			page_cache_release(page);
 		}
 	}
+	if (start >= end)
+		return;
 
 	index = start;
 	for ( ; ; ) {
 		cond_resched();
 		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
-			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
+				min(end - index, (pgoff_t)PAGEVEC_SIZE),
 							pvec.pages, indices);
 		if (!pvec.nr) {
 			if (index == start)
@@ -499,7 +519,7 @@ void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
 			index = start;
 			continue;
 		}
-		if (index == start && indices[0] > end) {
+		if (index == start && indices[0] >= end) {
 			shmem_deswap_pagevec(&pvec);
 			pagevec_release(&pvec);
 			break;
@@ -509,7 +529,7 @@ void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
 			struct page *page = pvec.pages[i];
 
 			index = indices[i];
-			if (index > end)
+			if (index >= end)
 				break;
 
 			if (radix_tree_exceptional_entry(page)) {
@@ -1462,6 +1482,31 @@ static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
 	return error;
 }
 
+static long shmem_fallocate(struct file *file, int mode, loff_t offset,
+							 loff_t len)
+{
+	struct inode *inode = file->f_path.dentry->d_inode;
+	int error = -EOPNOTSUPP;
+
+	mutex_lock(&inode->i_mutex);
+
+	if (mode & FALLOC_FL_PUNCH_HOLE) {
+		struct address_space *mapping = file->f_mapping;
+		loff_t unmap_start = round_up(offset, PAGE_SIZE);
+		loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
+
+		if ((u64)unmap_end > (u64)unmap_start)
+			unmap_mapping_range(mapping, unmap_start,
+					    1 + unmap_end - unmap_start, 0);
+		shmem_truncate_range(inode, offset, offset + len - 1);
+		/* No need to unmap again: hole-punching leaves COWed pages */
+		error = 0;
+	}
+
+	mutex_unlock(&inode->i_mutex);
+	return error;
+}
+
 static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
 {
 	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
@@ -2372,6 +2417,7 @@ static const struct file_operations shmem_file_operations = {
 	.fsync		= noop_fsync,
 	.splice_read	= shmem_file_splice_read,
 	.splice_write	= generic_file_splice_write,
+	.fallocate	= shmem_fallocate,
 #endif
 };
 
Signature · 1 line
1.7.3.2.146.gca209
#2 lore raw

RFC PATCH 2/4 Range tree implementation

In reply to John Stultz

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

I suspect range-tree isn't a totally accurate name, but I couldn't quite make out the difference between range trees and interval trees, so I just picked one to call it. Do let me know if you have a better name.

The idea of storing ranges in a tree is nice, but has a number of complications. When adding a range, its possible that a large range will consume and merge a number of smaller ranges. When removing a range, its possible you may end up splitting an existing range, causing one range 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 ranges.

Andrew also really wanted this range-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 ranges 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

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]>
---
 include/linux/rangetree.h |   53 ++++++++++++++++++++++
 lib/Makefile              |    2 +-
 lib/rangetree.c           |  107 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 161 insertions(+), 1 deletions(-)
 create mode 100644 include/linux/rangetree.h
 create mode 100644 lib/rangetree.c
include/linux/rangetree.h new file +53 -0
new file mode 100644
index 0000000..9fa24de
@@ -0,0 +1,53 @@
+#ifndef _LINUX_RANGETREE_H
+#define _LINUX_RANGETREE_H
+
+#include <linux/types.h>
+#include <linux/rbtree.h>
+
+struct range_tree_node {
+	struct rb_node rb;
+	u64 start;
+	u64 end;
+};
+
+struct range_tree_root {
+	struct rb_root head;
+};
+
+static inline void range_tree_init(struct range_tree_root *root)
+{
+	root->head = RB_ROOT;
+}
+
+static inline void range_tree_node_init(struct range_tree_node *node)
+{
+	rb_init_node(&node->rb);
+	node->start = 0;
+	node->end = 0;
+}
+
+static inline int range_tree_empty(struct range_tree_root *root)
+{
+	return RB_EMPTY_ROOT(&root->head);
+}
+
+static inline
+struct range_tree_node *range_tree_root_node(struct range_tree_root *root)
+{
+	struct range_tree_node *ret;
+	ret = container_of(root->head.rb_node, struct range_tree_node, rb);
+	return ret;
+}
+
+extern struct range_tree_node *range_tree_in_range(struct range_tree_root *root,
+							 u64 start, u64 end);
+extern struct range_tree_node *range_tree_next_in_range(
+						struct range_tree_node *node,
+						u64 start, u64 end);
+extern void range_tree_add(struct range_tree_root *root,
+						struct range_tree_node *node);
+extern void range_tree_remove(struct range_tree_root *root,
+						struct range_tree_node *node);
+#endif
+
+
lib/Makefile +1 -1
index 74290c9..a636a2d 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 rangetree.o
 
 lib-$(CONFIG_MMU) += ioremap.o
 lib-$(CONFIG_SMP) += cpumask.o
lib/rangetree.c new file +107 -0

107 lines not on this page · raw message

Signature · 1 line
1.7.3.2.146.gca209
reviewer
#3 lore raw

Re: [PATCH 2/4] [RFC] Range tree implementation

Quoted 4 lines · John Stultz

I suspect range-tree isn't a totally accurate name, but I couldn't quite make out the difference between range trees and interval trees, so I just picked one to call it. Do let me know if you have a better name.

Well, interval tree is a data structure for tracking a set of
possibly overlapping intervals. Range tree is a data structure tracking points allowing for fast queries on a set of points contained in a given range (gets useful and interesting when dimension > 1). Your data structure is neither so it would be good to have a different name. OTOH there are so many data structures that it's hard to find a reasonable unused name ;)

Quoted 3 lines

+/**
+ * range_tree_next_in_range - Return the next range in a range_tree still
+ * contained within a specified range.

'within' isn't really correct, right? It should rather be 'intersecting'.

Quoted 20 lines

+ * @root: range_tree root
+ * @start: range start
+ * @end: range end
+ *
+ */

lib/rangetree.c new file +15 -0
+struct range_tree_node *range_tree_next_in_range(struct range_tree_node *node,
+							u64 start, u64 end)
+{
+	struct rb_node *next;
+	struct range_tree_node *candidate;
+	if (!node)
+		return NULL;
+	next = rb_next(&node->rb);
+	if (!next)
+		return NULL;
+
+	candidate = container_of(next, struct range_tree_node, rb);
+
+	if ((candidate->start > end) || (candidate->end < start))
+		return NULL;

lib/rangetree.c line 68

Quoted 10 lines
lib/rangetree.c new file +10 -0
+
+	return candidate;
+}
+
+/**
+ * range_tree_add - Add a node to a range tree
+ * @root: range tree to be added to
+ * @node: range_tree_node to be added
+ *
+ * Adds a node to the range tree.

I think you should document here that the added range must not intersect
with any other range in the tree.

Quoted 11 lines

+ */

lib/rangetree.c new file +10 -0
+void range_tree_add(struct range_tree_root *root,
+					struct range_tree_node *node)
+{
+	struct rb_node **p = &root->head.rb_node;
+	struct rb_node *parent = NULL;
+	struct range_tree_node *ptr;
+
+	WARN_ON_ONCE(!RB_EMPTY_NODE(&node->rb));
+
+	while (*p) {

lib/rangetree.c line 89

Quoted 10 lines
lib/rangetree.c new file +10 -0
+		parent = *p;
+		ptr = rb_entry(parent, struct range_tree_node, rb);
+		if (node->start < ptr->start)
+			p = &(*p)->rb_left;
+		else
+			p = &(*p)->rb_right;
+	}
+	rb_link_node(&node->rb, parent, p);
+	rb_insert_color(&node->rb, &root->head);
+

^^ Spurious empty line.

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw

Re: [PATCH 2/4] [RFC] Range tree implementation

Quoted 11 lines · Jan Kara
Quoted 4 lines · John Stultz

I suspect range-tree isn't a totally accurate name, but I couldn't quite make out the difference between range trees and interval trees, so I just picked one to call it. Do let me know if you have a better name.

Well, interval tree is a data structure for tracking a set of
possibly overlapping intervals. Range tree is a data structure tracking points allowing for fast queries on a set of points contained in a given range (gets useful and interesting when dimension> 1). Your data structure is neither so it would be good to have a different name. OTOH there are so many data structures that it's hard to find a reasonable unused name ;)

Although I'm not sure your interval tree description doesn't match what I'm trying to provide. Could you clarify why that doesn't match?

Quoted 4 lines
Quoted 3 lines

+/**
+ * range_tree_next_in_range - Return the next range in a range_tree still
+ * contained within a specified range.

'within' isn't really correct, right? It should rather be 'intersecting'.

That is more clear, thanks for the suggestion!

Quoted 25 lines
Quoted 25 lines

+ * @root: range_tree root
+ * @start: range start
+ * @end: range end
+ *
+ */

lib/rangetree.c new file +19 -0
+struct range_tree_node *range_tree_next_in_range(struct range_tree_node *node,
+							u64 start, u64 end)
+{
+	struct rb_node *next;
+	struct range_tree_node *candidate;
+	if (!node)
+		return NULL;
+	next = rb_next(&node->rb);
+	if (!next)
+		return NULL;
+
+	candidate = container_of(next, struct range_tree_node, rb);
+
+	if ((candidate->start>  end) || (candidate->end<  start))
+		return NULL;
+
+	return candidate;
+}
+
Quoted 8 lines
Quoted 6 lines

+/**
+ * range_tree_add - Add a node to a range tree
+ * @root: range tree to be added to
+ * @node: range_tree_node to be added
+ *
+ * Adds a node to the range tree.

I think you should document here that the added range must not intersect
with any other range in the tree.

So for my usage in the volatile range code, I don't want intersecting or overlapping ranges added, but I didn't feel it was necessary to add this restriction to my rangetree code as well, since someone might want to store overlapping ranges.

thanks for the review!
-john

reviewer
#5 lore raw

Re: [PATCH 2/4] [RFC] Range tree implementation

Quoted 13 lines · John Stultz
Quoted 11 lines · Jan Kara
Quoted 4 lines · John Stultz

I suspect range-tree isn't a totally accurate name, but I couldn't quite make out the difference between range trees and interval trees, so I just picked one to call it. Do let me know if you have a better name.

Well, interval tree is a data structure for tracking a set of
possibly overlapping intervals. Range tree is a data structure tracking points allowing for fast queries on a set of points contained in a given range (gets useful and interesting when dimension> 1). Your data structure is neither so it would be good to have a different name. OTOH there are so many data structures that it's hard to find a reasonable unused name ;)

Quoted 3 lines

Although I'm not sure your interval tree description doesn't match what I'm trying to provide. Could you clarify why that doesn't match?

Wikipedia has a good description of Interval trees:
http://en.wikipedia.org/wiki/Interval_tree

For example they are tertiary trees.

Quoted 33 lines
Quoted 24 lines
Quoted 24 lines

+ * @root: range_tree root
+ * @start: range start
+ * @end: range end
+ *
+ */

lib/rangetree.c new file +19 -0
+struct range_tree_node *range_tree_next_in_range(struct range_tree_node *node,
+							u64 start, u64 end)
+{
+	struct rb_node *next;
+	struct range_tree_node *candidate;
+	if (!node)
+		return NULL;
+	next = rb_next(&node->rb);
+	if (!next)
+		return NULL;
+
+	candidate = container_of(next, struct range_tree_node, rb);
+
+	if ((candidate->start>  end) || (candidate->end<  start))
+		return NULL;
+
+	return candidate;
+}
+
Quoted 8 lines
Quoted 6 lines

+/**
+ * range_tree_add - Add a node to a range tree
+ * @root: range tree to be added to
+ * @node: range_tree_node to be added
+ *
+ * Adds a node to the range tree.

I think you should document here that the added range must not intersect
with any other range in the tree.

Quoted 4 lines

So for my usage in the volatile range code, I don't want intersecting or overlapping ranges added, but I didn't feel it was necessary to add this restriction to my rangetree code as well, since someone might want to store overlapping ranges.

Ok, but then you should define where an interval that is intersecting
other intervals ends up sorted...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#6 lore raw

Re: [PATCH 2/4] [RFC] Range tree implementation

Quoted 13 lines · Jan Kara
Quoted 12 lines · John Stultz
Quoted 11 lines · Jan Kara
Quoted 4 lines · John Stultz

I suspect range-tree isn't a totally accurate name, but I couldn't quite make out the difference between range trees and interval trees, so I just picked one to call it. Do let me know if you have a better name.

Well, interval tree is a data structure for tracking a set of
possibly overlapping intervals. Range tree is a data structure tracking points allowing for fast queries on a set of points contained in a given
range (gets useful and interesting when dimension> 1). Your data structure
is neither so it would be good to have a different name. OTOH there are so many data structures that it's hard to find a reasonable unused name ;)

Quoted 7 lines
Quoted 3 lines

Although I'm not sure your interval tree description doesn't match what I'm trying to provide. Could you clarify why that doesn't match?

Wikipedia has a good description of Interval trees:
http://en.wikipedia.org/wiki/Interval_tree

For example they are tertiary trees.

So roughly the naive approach listed in the wikipedia link is what I'm using here.
I'm fine renaming it to interval_tree, but if you really think it should be something else, by all means let me know.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

lib/rangetree.c line 68

Quoted 7 lines
lib/rangetree.c new file +7 -0
+
+/**
+ * range_tree_add - Add a node to a range tree
+ * @root: range tree to be added to
+ * @node: range_tree_node to be added
+ *
+ * Adds a node to the range tree.

I think you should document here that the added range must not intersect
with any other range in the tree.

Quoted 6 lines
Quoted 4 lines

So for my usage in the volatile range code, I don't want intersecting or overlapping ranges added, but I didn't feel it was necessary to add this restriction to my rangetree code as well, since someone might want to store overlapping ranges.

Ok, but then you should define where an interval that is intersecting
other intervals ends up sorted...

Ah. I see the issue you're concerned about!

Since the ranges are sorted by starting value, you could have a set of values: (0,5),(0,100),(0,50) and since they have the same start value, they could be connected adjacently in any way possible in the tree. Thus, the current search function wouldn't necessarily handle searching for (25,30) properly. Since if it hit (0,5) first, it would move right, where as (0,100) could reasonably be left of (0,5).

Thanks for pointing this out. While I didn't want to needlessly add the restriction that added ranges didn't intersect, it seems by focusing on my usage where it didn't intersect, I in effect encoded that restriction into the search. I'll go ahead and clarify this restriction in the comment and if folks want to extend the code to handle overlapping intervals, we can re-implement the logic then.

thanks
-john

#7 lore raw

RFC PATCH 3/4 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 a range-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.

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]>
---
 include/linux/volatile.h |   43 ++++
 mm/Makefile              |    2 +-
 mm/volatile.c            |  493 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 537 insertions(+), 1 deletions(-)
 create mode 100644 include/linux/volatile.h
 create mode 100644 mm/volatile.c
include/linux/volatile.h new file +43 -0
new file mode 100644
index 0000000..69148d0
@@ -0,0 +1,43 @@
+#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 s64 volatile_range_lru_size(struct volatile_fs_head *head);
+
+extern void volatile_range_clear(struct volatile_fs_head *head,
+					struct address_space *mapping);
+
+extern s64 volatile_ranges_get_last_used(struct volatile_fs_head *head,
+				struct address_space **mapping,
+				loff_t *start, loff_t *end);
+#endif /* _LINUX_VOLATILE_H */
mm/Makefile +1 -1
index 50ec00e..7b6c7a8 100644
@@ -13,7 +13,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 \
-			   $(mmu-y)
+			   volatile.o $(mmu-y)
 obj-y += init-mm.o
 
 ifdef CONFIG_NO_BOOTMEM
mm/volatile.c new file +493 -0

493 lines not on this page · raw message

Signature · 1 line
1.7.3.2.146.gca209
#8 lore raw

RFC PATCH 4/4 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.

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]>
---
 fs/open.c              |    3 +-
 include/linux/falloc.h |    7 ++-
 mm/shmem.c             |  107 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 113 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 +107 -0

134 lines not on this page · raw message

Signature · 1 line
1.7.3.2.146.gca209