Skip to contentSkip to conversation

fuse: writable mmap + batched write

PATCH In review Miklos Szeredi 3 participants 29 messages Mar 17, 2008 – Mar 18, 2008

Subsystems (9)
lore
Share & tools
lore

Miklos Szeredi

author maintainer
#0 lore raw

cover fuse: writable mmap + batched write

Hi Andrew,

These are fuse updates for 2.6.26.

1-4) small tweaks to core code to make writable mmap in fuse possible
5) the fuse writable mmap support itself
6-7) allows buffered fuse writes to be bigger than 4k
8) handle short buffered reads better

Thanks,
Miklos

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#1 lore raw

PATCH 1/8 mm: bdi: export bdi_writeout_inc()

Fuse needs this for writable mmap support.

---
 include/linux/backing-dev.h |    2 ++
 mm/page-writeback.c         |   10 ++++++++++
 2 files changed, 12 insertions(+)
include/linux/backing-dev.h +2 -0
Index: linux/include/linux/backing-dev.h
===================================================================
@@ -134,6 +134,8 @@ static inline s64 bdi_stat_sum(struct ba
 	return sum;
 }
 
+extern void bdi_writeout_inc(struct backing_dev_info *bdi);
+
 /*
  * maximal error of a stat counter.
  */
mm/page-writeback.c +10 -0
Index: linux/mm/page-writeback.c
===================================================================
@@ -168,6 +168,16 @@ static inline void __bdi_writeout_inc(st
 			      bdi->max_prop_frac);
 }
 
+void bdi_writeout_inc(struct backing_dev_info *bdi)
+{
+	unsigned long flags;
+
+	local_irq_save(flags);
+	__bdi_writeout_inc(bdi);
+	local_irq_restore(flags);
+}
+EXPORT_SYMBOL(bdi_writeout_inc);
+
 static inline void task_dirty_inc(struct task_struct *tsk)
 {
 	prop_inc_single(&vm_dirties, &tsk->dirties);

--

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw

Re: [patch 1/8] mm: bdi: export bdi_writeout_inc()

Quoted 32 lines · Miklos Szeredi

plain text document attachment (export_bdi_writeout_inc.patch)
From: Miklos Szeredi <[email protected]>

Fuse needs this for writable mmap support.

---
 include/linux/backing-dev.h |    2 ++
 mm/page-writeback.c         |   10 ++++++++++
 2 files changed, 12 insertions(+)

Index: linux/include/linux/backing-dev.h
===================================================================

include/linux/backing-dev.h +2 -0
@@ -134,6 +134,8 @@ static inline s64 bdi_stat_sum(struct ba
 	return sum;
 }
 
+extern void bdi_writeout_inc(struct backing_dev_info *bdi);
+
 /*
  * maximal error of a stat counter.
  */

Index: linux/mm/page-writeback.c
===================================================================

mm/page-writeback.c +0 -0
@@ -168,6 +168,16 @@ static inline void __bdi_writeout_inc(st
 			      bdi->max_prop_frac);
 }
 

mm/page-writeback.c static inline void __bdi_writeout_inc(st · line 180

Quoted 10 lines
mm/page-writeback.c +10 -0
+void bdi_writeout_inc(struct backing_dev_info *bdi)
+{
+	unsigned long flags;
+
+	local_irq_save(flags);
+	__bdi_writeout_inc(bdi);
+	local_irq_restore(flags);
+}
+EXPORT_SYMBOL(bdi_writeout_inc);
+

May I ask to make this a _GPL export, please?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#3 lore raw

Re: [patch 1/8] mm: bdi: export bdi_writeout_inc()

Quoted 4 lines
Quoted 4 lines
mm/page-writeback.c +4 -0
+void bdi_writeout_inc(struct backing_dev_info *bdi)
+{
+	unsigned long flags;
+
Quoted 8 lines

mm/page-writeback.c static inline void __bdi_writeout_inc(st · line 180

Quoted 6 lines
mm/page-writeback.c +6 -0
+	local_irq_save(flags);
+	__bdi_writeout_inc(bdi);
+	local_irq_restore(flags);
+}
+EXPORT_SYMBOL(bdi_writeout_inc);
+

May I ask to make this a _GPL export, please?

Sure.

Miklos

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#4 lore raw

PATCH 2/8 mm: Add NR_WRITEBACK_TEMP counter

In reply to Miklos Szeredi

Fuse will use temporary buffers to write back dirty data from memory mappings (normal writes are done synchronously). This is needed, because there cannot be any guarantee about the time in which a write will complete.

By using temporary buffers, from the MM's point if view the page is written back immediately. If the writeout was due to memory pressure, this effectively migrates data from a full zone to a less full zone.

This patch adds a new counter (NR_WRITEBACK_TEMP) for the number of pages used as temporary buffers.

---
 drivers/base/node.c    |    2 ++
 fs/proc/proc_misc.c    |    2 ++
 include/linux/mmzone.h |    1 +
 mm/page-writeback.c    |    3 ++-
 4 files changed, 7 insertions(+), 1 deletion(-)
fs/proc/proc_misc.c +2 -0
Index: linux/fs/proc/proc_misc.c
===================================================================
@@ -179,6 +179,7 @@ static int meminfo_read_proc(char *page,
 		"PageTables:   %8lu kB\n"
 		"NFS_Unstable: %8lu kB\n"
 		"Bounce:       %8lu kB\n"
+		"WritebackTmp: %8lu kB\n"
 		"CommitLimit:  %8lu kB\n"
 		"Committed_AS: %8lu kB\n"
 		"VmallocTotal: %8lu kB\n"
@@ -210,6 +211,7 @@ static int meminfo_read_proc(char *page,
 		K(global_page_state(NR_PAGETABLE)),
 		K(global_page_state(NR_UNSTABLE_NFS)),
 		K(global_page_state(NR_BOUNCE)),
+		K(global_page_state(NR_WRITEBACK_TEMP)),
 		K(allowed),
 		K(committed),
 		(unsigned long)VMALLOC_TOTAL >> 10,
include/linux/mmzone.h +1 -0
Index: linux/include/linux/mmzone.h
===================================================================
@@ -95,6 +95,7 @@ enum zone_stat_item {
 	NR_UNSTABLE_NFS,	/* NFS unstable pages */
 	NR_BOUNCE,
 	NR_VMSCAN_WRITE,
+	NR_WRITEBACK_TEMP,	/* Writeback using temporary buffers */
 #ifdef CONFIG_NUMA
 	NUMA_HIT,		/* allocated in intended node */
 	NUMA_MISS,		/* allocated in non intended node */
drivers/base/node.c +2 -0
Index: linux/drivers/base/node.c
===================================================================
@@ -64,6 +64,7 @@ static ssize_t node_read_meminfo(struct 
 		       "Node %d PageTables:   %8lu kB\n"
 		       "Node %d NFS_Unstable: %8lu kB\n"
 		       "Node %d Bounce:       %8lu kB\n"
+		       "Node %d WritebackTmp: %8lu kB\n"
 		       "Node %d Slab:         %8lu kB\n"
 		       "Node %d SReclaimable: %8lu kB\n"
 		       "Node %d SUnreclaim:   %8lu kB\n",
@@ -86,6 +87,7 @@ static ssize_t node_read_meminfo(struct 
 		       nid, K(node_page_state(nid, NR_PAGETABLE)),
 		       nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
 		       nid, K(node_page_state(nid, NR_BOUNCE)),
+		       nid, K(node_page_state(nid, NR_WRITEBACK_TEMP)),
 		       nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
 				node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
 		       nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
mm/page-writeback.c +2 -1
Index: linux/mm/page-writeback.c
===================================================================
@@ -211,7 +211,8 @@ clip_bdi_dirty_limit(struct backing_dev_
 	avail_dirty = dirty -
 		(global_page_state(NR_FILE_DIRTY) +
 		 global_page_state(NR_WRITEBACK) +
-		 global_page_state(NR_UNSTABLE_NFS));
+		 global_page_state(NR_UNSTABLE_NFS) +
+		 global_page_state(NR_WRITEBACK_TEMP));
 
 	if (avail_dirty < 0)
 		avail_dirty = 0;

--

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#5 lore raw

Re: [patch 2/8] mm: Add NR_WRITEBACK_TEMP counter

fs/proc/proc_misc.c static int meminfo_read_proc(char *page, · line 182

Quoted 5 lines · On Mon, 17 Mar 2008 20:19:10 +0100 Miklos Szeredi <[email protected]> wrote:
fs/proc/proc_misc.c +1 -0
@@ -179,6 +179,7 @@ static int meminfo_read_proc(char *page,
 		"PageTables:   %8lu kB\n"
 		"NFS_Unstable: %8lu kB\n"
 		"Bounce:       %8lu kB\n"
+		"WritebackTmp: %8lu kB\n"

These fields are documented in Documentation/filesystems/proc.txt (please), although we're missing some of them now.

author maintainer
#6 lore raw

PATCH 3/8 mm: rotate_reclaimable_page() cleanup

In reply to Miklos Szeredi

Clean up messy conditional calling of test_clear_page_writeback() from both rotate_reclaimable_page() and end_page_writeback().

The only user of rotate_reclaimable_page() is end_page_writeback() so this is OK.

---
 include/linux/swap.h |    2 +-
 mm/filemap.c         |   10 ++++++----
 mm/swap.c            |   37 ++++++++++++-------------------------
 3 files changed, 19 insertions(+), 30 deletions(-)
include/linux/swap.h +1 -1
Index: linux/include/linux/swap.h
===================================================================
@@ -177,7 +177,7 @@ extern void activate_page(struct page *)
 extern void mark_page_accessed(struct page *);
 extern void lru_add_drain(void);
 extern int lru_add_drain_all(void);
-extern int rotate_reclaimable_page(struct page *page);
+extern void rotate_reclaimable_page(struct page *page);
 extern void swap_setup(void);
 
 /* linux/mm/vmscan.c */
mm/filemap.c +6 -4
Index: linux/mm/filemap.c
===================================================================
@@ -577,10 +577,12 @@ EXPORT_SYMBOL(unlock_page);
  */
 void end_page_writeback(struct page *page)
 {
-	if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) {
-		if (!test_clear_page_writeback(page))
-			BUG();
-	}
+	if (TestClearPageReclaim(page))
+		rotate_reclaimable_page(page);
+
+	if (!test_clear_page_writeback(page))
+		BUG();
+
 	smp_mb__after_clear_bit();
 	wake_up_page(page, PG_writeback);
 }
mm/swap.c +12 -25
Index: linux/mm/swap.c
===================================================================
@@ -133,34 +133,21 @@ static void pagevec_move_tail(struct pag
  * Writeback is about to end against a page which has been marked for immediate
  * reclaim.  If it still appears to be reclaimable, move it to the tail of the
  * inactive list.
- *
- * Returns zero if it cleared PG_writeback.
  */
-int rotate_reclaimable_page(struct page *page)
+void  rotate_reclaimable_page(struct page *page)
 {
-	struct pagevec *pvec;
-	unsigned long flags;
-
-	if (PageLocked(page))
-		return 1;
-	if (PageDirty(page))
-		return 1;
-	if (PageActive(page))
-		return 1;
-	if (!PageLRU(page))
-		return 1;
-
-	page_cache_get(page);
-	local_irq_save(flags);
-	pvec = &__get_cpu_var(lru_rotate_pvecs);
-	if (!pagevec_add(pvec, page))
-		pagevec_move_tail(pvec);
-	local_irq_restore(flags);
-
-	if (!test_clear_page_writeback(page))
-		BUG();
+	if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
+	    PageLRU(page)) {
+		struct pagevec *pvec;
+		unsigned long flags;
 
-	return 0;
+		page_cache_get(page);
+		local_irq_save(flags);
+		pvec = &__get_cpu_var(lru_rotate_pvecs);
+		if (!pagevec_add(pvec, page))
+			pagevec_move_tail(pvec);
+		local_irq_restore(flags);
+	}
 }
 
 /*

--

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw

Re: [patch 3/8] mm: rotate_reclaimable_page() cleanup

Quoted 10 lines · Miklos Szeredi

plain text document attachment (rotate_reclaimable_page_cleanup.patch)
From: Miklos Szeredi <[email protected]>

Clean up messy conditional calling of test_clear_page_writeback() from both rotate_reclaimable_page() and end_page_writeback().

mm/swap.c +1 -2
-int rotate_reclaimable_page(struct page *page)
+void  rotate_reclaimable_page(struct page *page)
 {
-	struct pagevec *pvec;

mm/swap.c static void pagevec_move_tail(struct pag · line 151

Quoted 10 lines
mm/swap.c +0 -10
-	unsigned long flags;
-
-	if (PageLocked(page))
-		return 1;
-	if (PageDirty(page))
-		return 1;
-	if (PageActive(page))
-		return 1;
-	if (!PageLRU(page))
-		return 1;

Might be me, but I find the above easier to read than

mm/swap.c static void pagevec_move_tail(struct pag · line 140

Quoted 3 lines
mm/swap.c +2 -0
+	if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
+	    PageLRU(page)) {

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#8 lore raw

Re: [patch 3/8] mm: rotate_reclaimable_page() cleanup

Quoted 17 lines
Quoted 4 lines
mm/swap.c +1 -2
-int rotate_reclaimable_page(struct page *page)
+void  rotate_reclaimable_page(struct page *page)
 {
-	struct pagevec *pvec;

mm/swap.c static void pagevec_move_tail(struct pag · line 151

Quoted 10 lines
mm/swap.c +0 -10
-	unsigned long flags;
-
-	if (PageLocked(page))
-		return 1;
-	if (PageDirty(page))
-		return 1;
-	if (PageActive(page))
-		return 1;
-	if (!PageLRU(page))
-		return 1;

Might be me, but I find the above easier to read than

Quoted 3 lines

mm/swap.c static void pagevec_move_tail(struct pag · line 140

Quoted 3 lines
mm/swap.c +2 -0
+	if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
+	    PageLRU(page)) {

Matter of taste, returning from a middle of a function is generally to be avoided (unless not). Anyway, this is just a side effect of the main cleanup, so I think I'm entitled to choose the style I prefer ;)

Miklos

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#9 lore raw

Re: [patch 3/8] mm: rotate_reclaimable_page() cleanup

Quoted 8 lines · On Tue, 18 Mar 2008 12:56:34 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 8 lines
Quoted 8 lines
mm/swap.c +1 -6
-int rotate_reclaimable_page(struct page *page)
+void  rotate_reclaimable_page(struct page *page)
 {
-	struct pagevec *pvec;
-	unsigned long flags;
-
-	if (PageLocked(page))
-		return 1;
Quoted 8 lines
Quoted 8 lines

mm/swap.c static void pagevec_move_tail(struct pag · line 151

Quoted 6 lines
mm/swap.c +0 -6
-	if (PageDirty(page))
-		return 1;
-	if (PageActive(page))
-		return 1;
-	if (!PageLRU(page))
-		return 1;

Might be me, but I find the above easier to read than

Me too, but (believe it or not) sometimes I will eschew comment ;)

Quoted 6 lines
Quoted 3 lines

mm/swap.c static void pagevec_move_tail(struct pag · line 140

Quoted 3 lines
mm/swap.c +2 -0
+	if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
+	    PageLRU(page)) {

Matter of taste, returning from a middle of a function is generally to be avoided (unless not).

Avoiding multiple returns is more than a matter of taste: the practice is a source of code bloat, resource leaks and locking errors.

But we do do it quite commonly at the start of the function when checking the arguments, before the function has actually altered anything.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#10 lore raw

PATCH 4/8 mm: allow not updating BDI stats in end_page_writeback()

In reply to Miklos Szeredi

Fuse's writepage will need to clear page writeback separately from updating the per BDI counters.

This patch renames end_page_writeback() to __end_page_writeback() and adds a boolean parameter to indicate if the per BDI stats need to be updated.

Regular callers get an inline end_page_writeback() without the boolean parameter.

---
 include/linux/page-flags.h |    2 +-
 include/linux/pagemap.h    |    7 ++++++-
 mm/filemap.c               |    7 ++++---
 mm/page-writeback.c        |    4 ++--
 4 files changed, 13 insertions(+), 7 deletions(-)
include/linux/page-flags.h +1 -1
Index: linux/include/linux/page-flags.h
===================================================================
@@ -300,7 +300,7 @@ struct page;	/* forward declaration */
 
 extern void cancel_dirty_page(struct page *page, unsigned int account_size);
 
-int test_clear_page_writeback(struct page *page);
+int test_clear_page_writeback(struct page *page, bool bdi_stats);
 int test_set_page_writeback(struct page *page);
 
 static inline void set_page_writeback(struct page *page)
include/linux/pagemap.h +6 -1
Index: linux/include/linux/pagemap.h
===================================================================
@@ -223,7 +223,12 @@ static inline void wait_on_page_writebac
 		wait_on_page_bit(page, PG_writeback);
 }
 
-extern void end_page_writeback(struct page *page);
+extern void __end_page_writeback(struct page *page, bool bdi_stats);
+
+static inline void end_page_writeback(struct page *page)
+{
+	__end_page_writeback(page, true);
+}
 
 /*
  * Fault a userspace page into pagetables.  Return non-zero on a fault.
mm/filemap.c +4 -3
Index: linux/mm/filemap.c
===================================================================
@@ -574,19 +574,20 @@ EXPORT_SYMBOL(unlock_page);
 /**
  * end_page_writeback - end writeback against a page
  * @page: the page
+ * @bdi_stats: update the per-bdi writeback counter
  */
-void end_page_writeback(struct page *page)
+void __end_page_writeback(struct page *page, bool bdi_stats)
 {
 	if (TestClearPageReclaim(page))
 		rotate_reclaimable_page(page);
 
-	if (!test_clear_page_writeback(page))
+	if (!test_clear_page_writeback(page, bdi_stats))
 		BUG();
 
 	smp_mb__after_clear_bit();
 	wake_up_page(page, PG_writeback);
 }
-EXPORT_SYMBOL(end_page_writeback);
+EXPORT_SYMBOL(__end_page_writeback);
 
 /**
  * __lock_page - get a lock on the page, assuming we need to sleep to get it
mm/page-writeback.c +2 -2
Index: linux/mm/page-writeback.c
===================================================================
@@ -1242,7 +1242,7 @@ int clear_page_dirty_for_io(struct page 
 }
 EXPORT_SYMBOL(clear_page_dirty_for_io);
 
-int test_clear_page_writeback(struct page *page)
+int test_clear_page_writeback(struct page *page, bool bdi_stats)
 {
 	struct address_space *mapping = page_mapping(page);
 	int ret;
@@ -1257,7 +1257,7 @@ int test_clear_page_writeback(struct pag
 			radix_tree_tag_clear(&mapping->page_tree,
 						page_index(page),
 						PAGECACHE_TAG_WRITEBACK);
-			if (bdi_cap_writeback_dirty(bdi)) {
+			if (bdi_stats && bdi_cap_writeback_dirty(bdi)) {
 				__dec_bdi_stat(bdi, BDI_WRITEBACK);
 				__bdi_writeout_inc(bdi);
 			}

--

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#11 lore raw

Re: [patch 4/8] mm: allow not updating BDI stats in end_page_writeback()

Quoted 87 lines · On Mon, 17 Mar 2008 20:19:12 +0100 Miklos Szeredi <[email protected]> wrote:

From: Miklos Szeredi <[email protected]>

Fuse's writepage will need to clear page writeback separately from updating the per BDI counters.

This patch renames end_page_writeback() to __end_page_writeback() and adds a boolean parameter to indicate if the per BDI stats need to be updated.

Regular callers get an inline end_page_writeback() without the boolean parameter.

...

Index: linux/include/linux/page-flags.h
===================================================================

include/linux/page-flags.h +1 -1
@@ -300,7 +300,7 @@ struct page;	/* forward declaration */
 
 extern void cancel_dirty_page(struct page *page, unsigned int account_size);
 
-int test_clear_page_writeback(struct page *page);
+int test_clear_page_writeback(struct page *page, bool bdi_stats);
 int test_set_page_writeback(struct page *page);
 
 static inline void set_page_writeback(struct page *page)

Index: linux/include/linux/pagemap.h
===================================================================

include/linux/pagemap.h +6 -1
@@ -223,7 +223,12 @@ static inline void wait_on_page_writebac
 		wait_on_page_bit(page, PG_writeback);
 }
 
-extern void end_page_writeback(struct page *page);
+extern void __end_page_writeback(struct page *page, bool bdi_stats);
+
+static inline void end_page_writeback(struct page *page)
+{
+	__end_page_writeback(page, true);
+}
 
 /*
  * Fault a userspace page into pagetables.  Return non-zero on a fault.

Index: linux/mm/filemap.c
===================================================================

mm/filemap.c +4 -3
@@ -574,19 +574,20 @@ EXPORT_SYMBOL(unlock_page);
 /**
  * end_page_writeback - end writeback against a page
  * @page: the page
+ * @bdi_stats: update the per-bdi writeback counter
  */
-void end_page_writeback(struct page *page)
+void __end_page_writeback(struct page *page, bool bdi_stats)
 {
 	if (TestClearPageReclaim(page))
 		rotate_reclaimable_page(page);
 
-	if (!test_clear_page_writeback(page))
+	if (!test_clear_page_writeback(page, bdi_stats))
 		BUG();
 
 	smp_mb__after_clear_bit();
 	wake_up_page(page, PG_writeback);
 }
-EXPORT_SYMBOL(end_page_writeback);
+EXPORT_SYMBOL(__end_page_writeback);
 
 /**
  * __lock_page - get a lock on the page, assuming we need to sleep to get it

Index: linux/mm/page-writeback.c
===================================================================

mm/page-writeback.c +1 -1
@@ -1242,7 +1242,7 @@ int clear_page_dirty_for_io(struct page 
 }
 EXPORT_SYMBOL(clear_page_dirty_for_io);
 
-int test_clear_page_writeback(struct page *page)
+int test_clear_page_writeback(struct page *page, bool bdi_stats)
 {
 	struct address_space *mapping = page_mapping(page);
 	int ret;

mm/page-writeback.c int test_clear_page_writeback(struct pag · line 1263

Quoted 8 lines
mm/page-writeback.c +1 -1
@@ -1257,7 +1257,7 @@ int test_clear_page_writeback(struct pag
 			radix_tree_tag_clear(&mapping->page_tree,
 						page_index(page),
 						PAGECACHE_TAG_WRITEBACK);
-			if (bdi_cap_writeback_dirty(bdi)) {
+			if (bdi_stats && bdi_cap_writeback_dirty(bdi)) {
 				__dec_bdi_stat(bdi, BDI_WRITEBACK);
 				__bdi_writeout_inc(bdi);
 			}

Adding `mode' flags to a core function is generally considered poor form. And it adds additional overhead and possibly stack utilisation for all callers.

We generally prefer that a new function be created. After all, that's what you've done here, only the code has gone and wedged two different functions into one.

Another approach might be to add a new bdi_cap_foo() flag. We could then do

	if (bdi_cap_writeback_dirty(bdi) && bdi_cap_mumble(bdi)) {

here. But even better would be to create a new BDI capability which indicates that this address_space doesn't want this treatment in test_clear_page_writeback(), then go fix up all the !bdi_cap_writeback_dirty() address_spaces to set that flag.

So then the code becomes

	if (!bdi_cap_account_writeback_in_test_clear_page_writeback(bdi)) {

(good luck thinking up a better name ;))

Reason: bdi_cap_writeback_dirty() is kinda weirdly intrepreted to mean various different things in different places and we really should separate its multiple interpretations into separate flags.

Note that this becomes a standalone VFS cleanup patch, and the fuse code can then just use it later on.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#12 lore raw

Re: [patch 4/8] mm: allow not updating BDI stats in end_page_writeback()

[PeterZ added to CC]

Quoted 102 lines · On Mon, 17 Mar 2008 20:19:12 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 88 lines

From: Miklos Szeredi <[email protected]>

Fuse's writepage will need to clear page writeback separately from updating the per BDI counters.

This patch renames end_page_writeback() to __end_page_writeback() and adds a boolean parameter to indicate if the per BDI stats need to be updated.

Regular callers get an inline end_page_writeback() without the boolean parameter.

...

Index: linux/include/linux/page-flags.h
===================================================================

include/linux/page-flags.h +1 -1
@@ -300,7 +300,7 @@ struct page;	/* forward declaration */
 
 extern void cancel_dirty_page(struct page *page, unsigned int account_size);
 
-int test_clear_page_writeback(struct page *page);
+int test_clear_page_writeback(struct page *page, bool bdi_stats);
 int test_set_page_writeback(struct page *page);
 
 static inline void set_page_writeback(struct page *page)

Index: linux/include/linux/pagemap.h
===================================================================

include/linux/pagemap.h +6 -1
@@ -223,7 +223,12 @@ static inline void wait_on_page_writebac
 		wait_on_page_bit(page, PG_writeback);
 }
 
-extern void end_page_writeback(struct page *page);
+extern void __end_page_writeback(struct page *page, bool bdi_stats);
+
+static inline void end_page_writeback(struct page *page)
+{
+	__end_page_writeback(page, true);
+}
 
 /*
  * Fault a userspace page into pagetables.  Return non-zero on a fault.

Index: linux/mm/filemap.c
===================================================================

mm/filemap.c +4 -3
@@ -574,19 +574,20 @@ EXPORT_SYMBOL(unlock_page);
 /**
  * end_page_writeback - end writeback against a page
  * @page: the page
+ * @bdi_stats: update the per-bdi writeback counter
  */
-void end_page_writeback(struct page *page)
+void __end_page_writeback(struct page *page, bool bdi_stats)
 {
 	if (TestClearPageReclaim(page))
 		rotate_reclaimable_page(page);
 
-	if (!test_clear_page_writeback(page))
+	if (!test_clear_page_writeback(page, bdi_stats))
 		BUG();
 
 	smp_mb__after_clear_bit();
 	wake_up_page(page, PG_writeback);
 }
-EXPORT_SYMBOL(end_page_writeback);
+EXPORT_SYMBOL(__end_page_writeback);
 
 /**
  * __lock_page - get a lock on the page, assuming we need to sleep to get it

Index: linux/mm/page-writeback.c
===================================================================

mm/page-writeback.c +1 -1
@@ -1242,7 +1242,7 @@ int clear_page_dirty_for_io(struct page 
 }
 EXPORT_SYMBOL(clear_page_dirty_for_io);
 
-int test_clear_page_writeback(struct page *page)
+int test_clear_page_writeback(struct page *page, bool bdi_stats)
 {
 	struct address_space *mapping = page_mapping(page);
 	int ret;

mm/page-writeback.c int test_clear_page_writeback(struct pag · line 1263

Quoted 8 lines
mm/page-writeback.c +1 -1
@@ -1257,7 +1257,7 @@ int test_clear_page_writeback(struct pag
 			radix_tree_tag_clear(&mapping->page_tree,
 						page_index(page),
 						PAGECACHE_TAG_WRITEBACK);
-			if (bdi_cap_writeback_dirty(bdi)) {
+			if (bdi_stats && bdi_cap_writeback_dirty(bdi)) {
 				__dec_bdi_stat(bdi, BDI_WRITEBACK);
 				__bdi_writeout_inc(bdi);
 			}

Adding `mode' flags to a core function is generally considered poor form. And it adds additional overhead and possibly stack utilisation for all callers.

Quoted 3 lines

We generally prefer that a new function be created. After all, that's what you've done here, only the code has gone and wedged two different functions into one.

Yes, although duplicating such a not entirely trivial function has it's dangers as well, I think.

Quoted 13 lines

Another approach might be to add a new bdi_cap_foo() flag. We could then do

	if (bdi_cap_writeback_dirty(bdi) && bdi_cap_mumble(bdi)) {

here. But even better would be to create a new BDI capability which indicates that this address_space doesn't want this treatment in test_clear_page_writeback(), then go fix up all the !bdi_cap_writeback_dirty() address_spaces to set that flag.

So then the code becomes

	if (!bdi_cap_account_writeback_in_test_clear_page_writeback(bdi)) {
Quoted 8 lines

(good luck thinking up a better name ;))

Reason: bdi_cap_writeback_dirty() is kinda weirdly intrepreted to mean various different things in different places and we really should separate its multiple interpretations into separate flags.

Note that this becomes a standalone VFS cleanup patch, and the fuse code can then just use it later on.

Hmm, I can see two slightly different meanings of bdi_cap_writeback_dirty():

1) need to call ->writepage (sync_page_range(), ...)
2) need to update BDI stats (test_clear_page_writeback(), ...)

If these two were different flags, then fuse could set the NEED_WRITEPAGE flag, but clear the NEED_UPDATE_BDI_STATS flag, and do it manually.

Does that sound workable?

Thanks,
Miklos

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#13 lore raw

Re: [patch 4/8] mm: allow not updating BDI stats in end_page_writeback()

Quoted 10 lines · On Tue, 18 Mar 2008 09:11:49 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 7 lines

Reason: bdi_cap_writeback_dirty() is kinda weirdly intrepreted to mean various different things in different places and we really should separate its multiple interpretations into separate flags.

Note that this becomes a standalone VFS cleanup patch, and the fuse code can then just use it later on.

Hmm, I can see two slightly different meanings of bdi_cap_writeback_dirty():

Quoted 8 lines

1) need to call ->writepage (sync_page_range(), ...)
2) need to update BDI stats (test_clear_page_writeback(), ...)

If these two were different flags, then fuse could set the NEED_WRITEPAGE flag, but clear the NEED_UPDATE_BDI_STATS flag, and do it manually.

Does that sound workable?

Yup, thanks.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#14 lore raw

Re: [patch 4/8] mm: allow not updating BDI stats in end_page_writeback()

In reply to Miklos Szeredi
Quoted 5 lines · Miklos Szeredi

plain text document attachment (end_page_writeback_nobdi.patch)
From: Miklos Szeredi <[email protected]>

Fuse's writepage will need to clear page writeback separately from updating the per BDI counters.

This is because of the juggling with temporary pages, right?

Would be nice to have some comments in the code explaining this.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#15 lore raw

Re: [patch 4/8] mm: allow not updating BDI stats in end_page_writeback()

Quoted 10 lines · Miklos Szeredi
Quoted 5 lines

plain text document attachment (end_page_writeback_nobdi.patch)
From: Miklos Szeredi <[email protected]>

Fuse's writepage will need to clear page writeback separately from updating the per BDI counters.

This is because of the juggling with temporary pages, right?

Would be nice to have some comments in the code explaining this.

Yup, well it will go through a bigger cleanup, as discussed with Andrew, if that's OK with you?

Miklos

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#16 lore raw

Re: [patch 4/8] mm: allow not updating BDI stats in end_page_writeback()

Quoted 7 lines · Miklos Szeredi
Quoted 7 lines · Miklos Szeredi
Quoted 5 lines

plain text document attachment (end_page_writeback_nobdi.patch)
From: Miklos Szeredi <[email protected]>

Fuse's writepage will need to clear page writeback separately from updating the per BDI counters.

Quoted 6 lines
Quoted 3 lines

This is because of the juggling with temporary pages, right?

Would be nice to have some comments in the code explaining this.

Yup, well it will go through a bigger cleanup, as discussed with Andrew, if that's OK with you?

Well, I was pondering that - it hadn't made its way out to the other side of my brain yet.. but I'll dump have I have:

Yes, it does two things, _however_ those two things are very much related. Your use-case that breaks this relation is an execption - and I haven't really grasped it yet..

I'm in general not too keen about you having to export the BDI accounting stuff and using it explicitly like this, but I'm afraid I don't see a way around it - the danger is that other filesystems will get creative (hence the req for GPL - that excludes the most creative ones).

Yes, it makes sense to delay the write completion accounting until its actually completed.. but I would suggest all writeback accounting.

So the thing that's in your way is that removing a page from the radix tree doesn't imply its done writing. So perhaps we should make that distinction instead?

So instead of conditionally do part of the accounting, never do it and require something like: page_writeback_complete() to be called after a successfull test_clear_page_writeback().

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#17 lore raw

Re: [patch 4/8] mm: allow not updating BDI stats in end_page_writeback()

Quoted 4 lines

Yes, it does two things, _however_ those two things are very much related. Your use-case that breaks this relation is an execption - and I haven't really grasped it yet..

Quoted 8 lines

I'm in general not too keen about you having to export the BDI accounting stuff and using it explicitly like this, but I'm afraid I don't see a way around it - the danger is that other filesystems will get creative (hence the req for GPL - that excludes the most creative ones).

Yes, it makes sense to delay the write completion accounting until its actually completed.. but I would suggest all writeback accounting.

Doesn't work, as long as we have throttle_vm_writeout() waiting for NR_WRITEBACK to go below a threshold, delaying the NR_WRITEBACK accounting could lead to a deadlock.

So at least until that's resolved NR_WRITEBACK_TEMP needs to be separate from NR_WRITEBACK_TEMP. And it makes sense possibly even after that, as they are fundamentally different things. The first one is page cache pages being under writeout, the second is just kernel buffers (mostly) unrelated to the page cache.

Quoted 7 lines

So the thing that's in your way is that removing a page from the radix tree doesn't imply its done writing. So perhaps we should make that distinction instead?

So instead of conditionally do part of the accounting, never do it and require something like: page_writeback_complete() to be called after a successfull test_clear_page_writeback().

Yes, that's a possibility, but then normal filesystems miss out on the small optimization provided by doing the BDI accounting functions inside the same IRQ disabled region as the radix tree operation. Would that have any significant performance impact?

Miklos

#18 lore raw

Re: [patch 4/8] mm: allow not updating BDI stats in end_page_writeback()

Quoted 17 lines · Miklos Szeredi
Quoted 4 lines

Yes, it does two things, _however_ those two things are very much related. Your use-case that breaks this relation is an execption - and I haven't really grasped it yet..

Quoted 8 lines

I'm in general not too keen about you having to export the BDI accounting stuff and using it explicitly like this, but I'm afraid I don't see a way around it - the danger is that other filesystems will get creative (hence the req for GPL - that excludes the most creative ones).

Yes, it makes sense to delay the write completion accounting until its actually completed.. but I would suggest all writeback accounting.

Doesn't work, as long as we have throttle_vm_writeout() waiting for NR_WRITEBACK to go below a threshold, delaying the NR_WRITEBACK accounting could lead to a deadlock.

Quoted 5 lines

So at least until that's resolved NR_WRITEBACK_TEMP needs to be separate from NR_WRITEBACK_TEMP. And it makes sense possibly even after that, as they are fundamentally different things. The first one is page cache pages being under writeout, the second is just kernel buffers (mostly) unrelated to the page cache.

Urgh, throttle_vm_writeout() again.. Agreed, that'll deadlock.

Quoted 4 lines
Quoted 4 lines

So the thing that's in your way is that removing a page from the radix tree doesn't imply its done writing. So perhaps we should make that distinction instead?

Quoted 8 lines
Quoted 3 lines

So instead of conditionally do part of the accounting, never do it and require something like: page_writeback_complete() to be called after a successfull test_clear_page_writeback().

Yes, that's a possibility, but then normal filesystems miss out on the small optimization provided by doing the BDI accounting functions inside the same IRQ disabled region as the radix tree operation. Would that have any significant performance impact?

Yeah, realized that. Don't know, would have to measure it somehow... some archs are rather slow with disabling IRQs, but we're talking about writeout which should be dominated by the IO times.

Its just that your proposal exposes too much guts, I'd like the interface to be a little higher level.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#19 lore raw

Re: [patch 4/8] mm: allow not updating BDI stats in end_page_writeback()

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines

So the thing that's in your way is that removing a page from the radix tree doesn't imply its done writing. So perhaps we should make that distinction instead?

Quoted 8 lines
Quoted 3 lines

So instead of conditionally do part of the accounting, never do it and require something like: page_writeback_complete() to be called after a successfull test_clear_page_writeback().

Yes, that's a possibility, but then normal filesystems miss out on the small optimization provided by doing the BDI accounting functions inside the same IRQ disabled region as the radix tree operation. Would that have any significant performance impact?

Quoted 6 lines

Yeah, realized that. Don't know, would have to measure it somehow... some archs are rather slow with disabling IRQs, but we're talking about writeout which should be dominated by the IO times.

Its just that your proposal exposes too much guts, I'd like the interface to be a little higher level.

Well, but this is the kernel, you can't really make foolproof interfaces. If we'll go with Andrew's suggestion, I'll add comments warning users about not touching those flags unless they know what they are doing, OK?

Miklos

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#20 lore raw

Re: [patch 4/8] mm: allow not updating BDI stats in end_page_writeback()

Quoted 17 lines · Miklos Szeredi
Quoted 17 lines
Quoted 4 lines
Quoted 4 lines

So the thing that's in your way is that removing a page from the radix tree doesn't imply its done writing. So perhaps we should make that distinction instead?

Quoted 8 lines
Quoted 3 lines

So instead of conditionally do part of the accounting, never do it and require something like: page_writeback_complete() to be called after a successfull test_clear_page_writeback().

Yes, that's a possibility, but then normal filesystems miss out on the small optimization provided by doing the BDI accounting functions inside the same IRQ disabled region as the radix tree operation. Would that have any significant performance impact?

Yeah, realized that. Don't know, would have to measure it somehow... some archs are rather slow with disabling IRQs, but we're talking about writeout which should be dominated by the IO times.

Quoted 7 lines
Quoted 2 lines

Its just that your proposal exposes too much guts, I'd like the interface to be a little higher level.

Well, but this is the kernel, you can't really make foolproof interfaces. If we'll go with Andrew's suggestion, I'll add comments warning users about not touching those flags unless they know what they are doing, OK?

Yeah, I guess so :-)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#21 lore raw

Re: [patch 4/8] mm: allow not updating BDI stats in end_page_writeback()

Quoted 6 lines
Quoted 4 lines

Well, but this is the kernel, you can't really make foolproof interfaces. If we'll go with Andrew's suggestion, I'll add comments warning users about not touching those flags unless they know what they are doing, OK?

Yeah, I guess so :-)

Cool :)

On a related note, is there a reason why bdi_cap_writeback_dirty() and friends need to be macros instead of inline functions? If not I'd clean that up as well.

Miklos

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#22 lore raw

Re: [patch 4/8] mm: allow not updating BDI stats in end_page_writeback()

Quoted 2 lines · On Tue, 18 Mar 2008 16:53:52 +0100 Miklos Szeredi <[email protected]> wrote:

On a related note, is there a reason why bdi_cap_writeback_dirty() and friends need to be macros instead of inline functions?

None whatsoever.

Quoted 1 line

If not I'd clean that up as well.

Goodness.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#23 lore raw

PATCH 5/8 fuse: support writable mmap

In reply to Miklos Szeredi

Quoting Linus (3 years ago, FUSE inclusion discussions):

"User-space filesystems are hard to get right. I'd claim that they
are almost impossible, unless you limit them somehow (shared writable mappings are the nastiest part - if you don't have those, you can reasonably limit your problems by limiting the number of dirty pages you accept through normal "write()" calls)."

Instead of attempting the impossible, I've just waited for the dirty page accounting infrastructure to materialize (thanks to Peter Zijlstra and others). This nicely solved the biggest problem:
limiting the number of pages used for write caching.

Some small details remained, however, which this largish patch attempts to address. It provides a page writeback implementation for fuse, which is completely safe against VM related deadlocks. Performance may not be very good for certain usage patterns, but generally it should be acceptable.

It has been tested extensively with fsx-linux and bash-shared-mapping.

This patch depends on mm-bdi-allow-setting-a-maximum-for-the-bdi-dirty-limit-fix.patch

Fuse page writeback design
--------------------------

fuse_writepage() allocates a new temporary page with GFP_NOFS|__GFP_HIGHMEM. It copies the contents of the original page, and queues a WRITE request to the userspace filesystem using this temp page.

The writeback is finished instantly from the MM's point of view: the page is removed from the radix trees, and the PageDirty and PageWriteback flags are cleared.

For the duration of the actual write, the NR_WRITEBACK_TEMP counter is incremented. The per-bdi writeback count is not decremented until the actual write completes.

On dirtying the page, fuse waits for a previous write to finish before proceeding. This makes sure, there can only be one temporary page used at a time for one cached page.

This approach is wasteful in both memory and CPU bandwidth, so why is this complication needed?

The basic problem is that there can be no guarantee about the time in which the userspace filesystem will complete a write. It may be buggy or even malicious, and fail to complete WRITE requests. We don't want unrelated parts of the system to grind to a halt in such cases.

Also a filesystem may need additional resources (particularly memory) to complete a WRITE request. There's a great danger of a deadlock if that allocation may wait for the writepage to finish.

Currently there are several cases where the kernel can block on page writeback:

- allocation order is larger than PAGE_ALLOC_COSTLY_ORDER
- page migration
- throttle_vm_writeout (through NR_WRITEBACK)
- sync(2)

Of course in some cases (fsync, msync) we explicitly want to allow blocking. So for these cases new code has to be added to fuse, since the VM is not tracking writeback pages for us any more.

As an extra safetly measure, the maximum dirty ratio allocated to a single fuse filesystem is set to 1% by default. This way one (or several) buggy or malicious fuse filesystems cannot slow down the rest of the system by hogging dirty memory.

With appropriate privileges, this limit can be raised through '/sys/class/bdi/<bdi>/max_ratio'.

---
 fs/fuse/dev.c    |   19 +++
 fs/fuse/dir.c    |   84 +++++++++++++-
 fs/fuse/file.c   |  320 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 fs/fuse/fuse_i.h |   37 ++++++
 fs/fuse/inode.c  |   47 ++++++--
 5 files changed, 478 insertions(+), 29 deletions(-)
fs/fuse/dev.c +19 -0
Index: linux/fs/fuse/dev.c
===================================================================
@@ -47,6 +47,14 @@ struct fuse_req *fuse_request_alloc(void
 	return req;
 }
 
+struct fuse_req *fuse_request_alloc_nofs(void)
+{
+	struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_NOFS);
+	if (req)
+		fuse_request_init(req);
+	return req;
+}
+
 void fuse_request_free(struct fuse_req *req)
 {
 	kmem_cache_free(fuse_req_cachep, req);
@@ -430,6 +438,17 @@ void request_send_background(struct fuse
 }
 
 /*
+ * Called under fc->lock
+ *
+ * fc->connected must have been checked previously
+ */
+void request_send_background_locked(struct fuse_conn *fc, struct fuse_req *req)
+{
+	req->isreply = 1;
+	request_send_nowait_locked(fc, req);
+}
+
+/*
  * Lock the request.  Up to the next unlock_request() there mustn't be
  * anything that could cause a page-fault.  If the request was already
  * aborted bail out.
fs/fuse/dir.c +81 -3

121 lines not on this page · raw message

fs/fuse/file.c +304 -16

396 lines not on this page · raw message

fs/fuse/fuse_i.h +37 -0
Index: linux/fs/fuse/fuse_i.h
===================================================================
@@ -15,6 +15,7 @@
 #include <linux/mm.h>
 #include <linux/backing-dev.h>
 #include <linux/mutex.h>
+#include <linux/rwsem.h>
 
 /** Max number of pages that can be used in a single read request */
 #define FUSE_MAX_PAGES_PER_REQ 32
@@ -25,6 +26,9 @@
 /** Congestion starts at 75% of maximum */
 #define FUSE_CONGESTION_THRESHOLD (FUSE_MAX_BACKGROUND * 75 / 100)
 
+/** Bias for fi->writectr, meaning new writepages must not be sent */
+#define FUSE_NOWRITE INT_MIN
+
 /** It could be as large as PATH_MAX, but would that have any uses? */
 #define FUSE_NAME_MAX 1024
 
@@ -73,6 +77,19 @@ struct fuse_inode {
 
 	/** Files usable in writepage.  Protected by fc->lock */
 	struct list_head write_files;
+
+	/** Writepages pending on truncate or fsync */
+	struct list_head queued_writes;
+
+	/** Number of sent writes, a negative bias (FUSE_NOWRITE)
+	 * means more writes are blocked */
+	int writectr;
+
+	/** Waitq for writepage completion */
+	wait_queue_head_t page_waitq;
+
+	/** List of writepage requestst (pending or sent) */
+	struct list_head writepages;
 };
 
 /** FUSE specific file data */
@@ -242,6 +259,12 @@ struct fuse_req {
 	/** File used in the request (or NULL) */
 	struct fuse_file *ff;
 
+	/** Inode used in the request or NULL */
+	struct inode *inode;
+
+	/** Link on fi->writepages */
+	struct list_head writepages_entry;
+
 	/** Request completion callback */
 	void (*end)(struct fuse_conn *, struct fuse_req *);
 
@@ -504,6 +527,11 @@ void fuse_init_symlink(struct inode *ino
 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
 			    u64 attr_valid, u64 attr_version);
 
+void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
+				   u64 attr_valid);
+
+void fuse_truncate(struct address_space *mapping, loff_t offset);
+
 /**
  * Initialize the client device
  */
@@ -522,6 +550,8 @@ void fuse_ctl_cleanup(void);
  */
 struct fuse_req *fuse_request_alloc(void);
 
+struct fuse_req *fuse_request_alloc_nofs(void);
+
 /**
  * Free a request
  */
@@ -558,6 +588,8 @@ void request_send_noreply(struct fuse_co
  */
 void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
 
+void request_send_background_locked(struct fuse_conn *fc, struct fuse_req *req);
+
 /* Abort all requests */
 void fuse_abort_conn(struct fuse_conn *fc);
 
@@ -600,3 +632,8 @@ u64 fuse_lock_owner_id(struct fuse_conn 
 
 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
 			   struct file *file, bool *refreshed);
+
+void fuse_flush_writepages(struct inode *inode);
+
+void fuse_set_nowrite(struct inode *inode);
+void fuse_release_nowrite(struct inode *inode);
fs/fuse/inode.c +37 -10

88 lines not on this page · raw message

--

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#24 lore raw

PATCH 6/8 fuse: clean up setting i_size in write

In reply to Miklos Szeredi

Extract common code for setting i_size in write functions into a common helper.

---
 fs/fuse/file.c |   28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)
fs/fuse/file.c +15 -13
Index: linux/fs/fuse/file.c
===================================================================
@@ -610,13 +610,24 @@ static int fuse_write_begin(struct file 
 	return 0;
 }
 
+static void fuse_write_update_size(struct inode *inode, loff_t pos)
+{
+	struct fuse_conn *fc = get_fuse_conn(inode);
+	struct fuse_inode *fi = get_fuse_inode(inode);
+
+	spin_lock(&fc->lock);
+	fi->attr_version = ++fc->attr_version;
+	if (pos > inode->i_size)
+		i_size_write(inode, pos);
+	spin_unlock(&fc->lock);
+}
+
 static int fuse_buffered_write(struct file *file, struct inode *inode,
 			       loff_t pos, unsigned count, struct page *page)
 {
 	int err;
 	size_t nres;
 	struct fuse_conn *fc = get_fuse_conn(inode);
-	struct fuse_inode *fi = get_fuse_inode(inode);
 	unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
 	struct fuse_req *req;
 
@@ -643,12 +654,7 @@ static int fuse_buffered_write(struct fi
 		err = -EIO;
 	if (!err) {
 		pos += nres;
-		spin_lock(&fc->lock);
-		fi->attr_version = ++fc->attr_version;
-		if (pos > inode->i_size)
-			i_size_write(inode, pos);
-		spin_unlock(&fc->lock);
-
+		fuse_write_update_size(inode, pos);
 		if (count == PAGE_CACHE_SIZE)
 			SetPageUptodate(page);
 	}
@@ -766,12 +772,8 @@ static ssize_t fuse_direct_io(struct fil
 	}
 	fuse_put_request(fc, req);
 	if (res > 0) {
-		if (write) {
-			spin_lock(&fc->lock);
-			if (pos > inode->i_size)
-				i_size_write(inode, pos);
-			spin_unlock(&fc->lock);
-		}
+		if (write)
+			fuse_write_update_size(inode, pos);
 		*ppos = pos;
 	}
 	fuse_invalidate_attr(inode);

--

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#25 lore raw

Re: [patch 6/8] fuse: clean up setting i_size in write

Quoted 34 lines · On Mon, 17 Mar 2008 20:19:14 +0100 Miklos Szeredi <[email protected]> wrote:

From: Miklos Szeredi <[email protected]>

Extract common code for setting i_size in write functions into a common helper.

---
 fs/fuse/file.c |   28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

Index: linux/fs/fuse/file.c
===================================================================

fs/fuse/file.c +11 -0
@@ -610,13 +610,24 @@ static int fuse_write_begin(struct file 
 	return 0;
 }
 
+static void fuse_write_update_size(struct inode *inode, loff_t pos)
+{
+	struct fuse_conn *fc = get_fuse_conn(inode);
+	struct fuse_inode *fi = get_fuse_inode(inode);
+
+	spin_lock(&fc->lock);
+	fi->attr_version = ++fc->attr_version;
+	if (pos > inode->i_size)
+		i_size_write(inode, pos);
+	spin_unlock(&fc->lock);
+}
 
...
@@ -766,12 +772,8 @@ static ssize_t fuse_direct_io(struct fil
 	}

fs/fuse/file.c static ssize_t fuse_direct_io(struct fil · line 776

Quoted 10 lines
fs/fuse/file.c +2 -6
 	fuse_put_request(fc, req);
 	if (res > 0) {
-		if (write) {
-			spin_lock(&fc->lock);
-			if (pos > inode->i_size)
-				i_size_write(inode, pos);
-			spin_unlock(&fc->lock);
-		}
+		if (write)
+			fuse_write_update_size(inode, pos);

We require that i_mutex be held here, to prevent i_size_write() deadlocks. Is it held?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#26 lore raw

Re: [patch 6/8] fuse: clean up setting i_size in write

Quoted 34 lines · On Mon, 17 Mar 2008 20:19:14 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 33 lines

From: Miklos Szeredi <[email protected]>

Extract common code for setting i_size in write functions into a common helper.

---
 fs/fuse/file.c |   28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

Index: linux/fs/fuse/file.c
===================================================================

fs/fuse/file.c +11 -0
@@ -610,13 +610,24 @@ static int fuse_write_begin(struct file 
 	return 0;
 }
 
+static void fuse_write_update_size(struct inode *inode, loff_t pos)
+{
+	struct fuse_conn *fc = get_fuse_conn(inode);
+	struct fuse_inode *fi = get_fuse_inode(inode);
+
+	spin_lock(&fc->lock);
+	fi->attr_version = ++fc->attr_version;
+	if (pos > inode->i_size)
+		i_size_write(inode, pos);
+	spin_unlock(&fc->lock);
+}
 
...
Quoted 16 lines

fs/fuse/file.c static ssize_t fuse_direct_io(struct fil · line 776

Quoted 12 lines
fs/fuse/file.c +2 -6
@@ -766,12 +772,8 @@ static ssize_t fuse_direct_io(struct fil
 	}
 	fuse_put_request(fc, req);
 	if (res > 0) {
-		if (write) {
-			spin_lock(&fc->lock);
-			if (pos > inode->i_size)
-				i_size_write(inode, pos);
-			spin_unlock(&fc->lock);
-		}
+		if (write)
+			fuse_write_update_size(inode, pos);

We require that i_mutex be held here, to prevent i_size_write() deadlocks. Is it held?

No, fuse uses the per connection spinlock to protect against concurrent calls to i_size_write(), because in some cases holding i_mutex would be difficult.

Fuse already got painfully bitten by the i_size_write() deadlock, so I'm well aware of the problem :)

Miklos

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#27 lore raw

PATCH 7/8 fuse: implement perform_write

In reply to Miklos Szeredi

Introduce fuse_perform_write. With fusexmp (a passthrough filesystem), large (1MB) writes into a backing tmpfs filesystem are sped up by almost 4 times (256MB/s vs 71MB/s).

[[email protected]]:

- split into smaller functions
- testing
- duplicate generic_file_aio_write(), so that there's no need to add a new ->perform_write() a_op. Comment from hch.

---
 fs/fuse/file.c |  194 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 193 insertions(+), 1 deletion(-)
fs/fuse/file.c +193 -1
Index: linux/fs/fuse/file.c
===================================================================
@@ -677,6 +677,198 @@ static int fuse_write_end(struct file *f
 	return res;
 }
 
+static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
+				    struct inode *inode, loff_t pos,
+				    size_t count)
+{
+	size_t res;
+	unsigned offset;
+	unsigned i;
+
+	for (i = 0; i < req->num_pages; i++)
+		fuse_wait_on_page_writeback(inode, req->pages[i]->index);
+
+	res = fuse_send_write(req, file, inode, pos, count, NULL);
+
+	offset = req->page_offset;
+	count = res;
+	for (i = 0; i < req->num_pages; i++) {
+		struct page *page = req->pages[i];
+
+		if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
+			SetPageUptodate(page);
+
+		if (count > PAGE_CACHE_SIZE - offset)
+			count -= PAGE_CACHE_SIZE - offset;
+		else
+			count = 0;
+		offset = 0;
+
+		unlock_page(page);
+		page_cache_release(page);
+	}
+
+	return res;
+}
+
+static ssize_t fuse_fill_write_pages(struct fuse_req *req,
+			       struct address_space *mapping,
+			       struct iov_iter *ii, loff_t pos)
+{
+	struct fuse_conn *fc = get_fuse_conn(mapping->host);
+	unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
+	size_t count = 0;
+	int err;
+
+	req->page_offset = offset;
+
+	do {
+		size_t tmp;
+		struct page *page;
+		pgoff_t index = pos >> PAGE_CACHE_SHIFT;
+		size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
+				     iov_iter_count(ii));
+
+		bytes = min_t(size_t, bytes, fc->max_write - count);
+
+ again:
+		err = -EFAULT;
+		if (iov_iter_fault_in_readable(ii, bytes))
+			break;
+
+		err = -ENOMEM;
+		page = __grab_cache_page(mapping, index);
+		if (!page)
+			break;
+
+		pagefault_disable();
+		tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
+		pagefault_enable();
+		flush_dcache_page(page);
+
+		if (!tmp) {
+			unlock_page(page);
+			page_cache_release(page);
+			bytes = min(bytes, iov_iter_single_seg_count(ii));
+			goto again;
+		}
+
+		err = 0;
+		req->pages[req->num_pages] = page;
+		req->num_pages++;
+
+		iov_iter_advance(ii, tmp);
+		count += tmp;
+		pos += tmp;
+		offset += tmp;
+		if (offset == PAGE_CACHE_SIZE)
+			offset = 0;
+
+	} while (iov_iter_count(ii) && count < fc->max_write &&
+		 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
+
+	return count > 0 ? count : err;
+}
+
+static ssize_t fuse_perform_write(struct file *file,
+				  struct address_space *mapping,
+				  struct iov_iter *ii, loff_t pos)
+{
+	struct inode *inode = mapping->host;
+	struct fuse_conn *fc = get_fuse_conn(inode);
+	int err = 0;
+	ssize_t res = 0;
+
+	if (is_bad_inode(inode))
+		return -EIO;
+
+	do {
+		struct fuse_req *req;
+		ssize_t count;
+
+		req = fuse_get_req(fc);
+		if (IS_ERR(req)) {
+			err = PTR_ERR(req);
+			break;
+		}
+
+		count = fuse_fill_write_pages(req, mapping, ii, pos);
+		if (count <= 0) {
+			err = count;
+		} else {
+			size_t num_written;
+
+			num_written = fuse_send_write_pages(req, file, inode,
+							    pos, count);
+			err = req->out.h.error;
+			if (!err) {
+				res += num_written;
+				pos += num_written;
+
+				/* break out of the loop on short write */
+				if (num_written != count)
+					err = -EIO;
+			}
+		}
+		fuse_put_request(fc, req);
+	} while (!err && iov_iter_count(ii));
+
+	if (res > 0)
+		fuse_write_update_size(inode, pos);
+
+	fuse_invalidate_attr(inode);
+
+	return res > 0 ? res : err;
+}
+
+static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
+				   unsigned long nr_segs, loff_t pos)
+{
+	struct file *file = iocb->ki_filp;
+	struct address_space *mapping = file->f_mapping;
+	size_t count = 0;
+	ssize_t written = 0;
+	struct inode *inode = mapping->host;
+	ssize_t err;
+	struct iov_iter i;
+
+	WARN_ON(iocb->ki_pos != pos);
+
+	err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
+	if (err)
+		return err;
+
+	mutex_lock(&inode->i_mutex);
+	vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
+
+	/* We can write back this queue in page reclaim */
+	current->backing_dev_info = mapping->backing_dev_info;
+
+	err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
+	if (err)
+		goto out;
+
+	if (count == 0)
+		goto out;
+
+	err = remove_suid(file->f_path.dentry);
+	if (err)
+		goto out;
+
+	file_update_time(file);
+
+	iov_iter_init(&i, iov, nr_segs, count, 0);
+	written = fuse_perform_write(file, mapping, &i, pos);
+	if (written >= 0)
+		iocb->ki_pos = pos + written;
+
+out:
+	current->backing_dev_info = NULL;
+	mutex_unlock(&inode->i_mutex);
+
+	return written ? written : err;
+}
+
 static void fuse_release_user_pages(struct fuse_req *req, int write)
 {
 	unsigned i;
@@ -1202,7 +1394,7 @@ static const struct file_operations fuse
 	.read		= do_sync_read,
 	.aio_read	= fuse_file_aio_read,
 	.write		= do_sync_write,
-	.aio_write	= generic_file_aio_write,
+	.aio_write	= fuse_file_aio_write,
 	.mmap		= fuse_file_mmap,
 	.open		= fuse_open,
 	.flush		= fuse_flush,

--

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#28 lore raw

PATCH 8/8 fuse: update file size on short read

In reply to Miklos Szeredi

If the READ request returned a short count, then either

- cached size is incorrect
- filesystem is buggy, as short reads are only allowed on EOF

So assume, that the size is wrong and refresh it, so that cached read() doesn't zero fill the missing chunk.

---
 fs/fuse/file.c |   42 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)
fs/fuse/file.c +38 -4
Index: linux/fs/fuse/file.c
===================================================================
@@ -398,11 +398,26 @@ static size_t fuse_send_read(struct fuse
 	return req->out.args[0].size;
 }
 
+static void fuse_read_update_size(struct inode *inode, loff_t size)
+{
+	struct fuse_conn *fc = get_fuse_conn(inode);
+	struct fuse_inode *fi = get_fuse_inode(inode);
+
+	spin_lock(&fc->lock);
+	fi->attr_version = ++fc->attr_version;
+	if (size < inode->i_size)
+		i_size_write(inode, size);
+	spin_unlock(&fc->lock);
+}
+
 static int fuse_readpage(struct file *file, struct page *page)
 {
 	struct inode *inode = page->mapping->host;
 	struct fuse_conn *fc = get_fuse_conn(inode);
 	struct fuse_req *req;
+	size_t num_read;
+	loff_t pos = page_offset(page);
+	size_t count = PAGE_CACHE_SIZE;
 	int err;
 
 	err = -EIO;
@@ -424,12 +439,20 @@ static int fuse_readpage(struct file *fi
 	req->out.page_zeroing = 1;
 	req->num_pages = 1;
 	req->pages[0] = page;
-	fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE,
-		       NULL);
+	num_read = fuse_send_read(req, file, inode, pos, count, NULL);
 	err = req->out.h.error;
 	fuse_put_request(fc, req);
-	if (!err)
+
+	if (!err) {
+		/*
+		 * Short read means EOF.  If file size is larger, truncate it
+		 */
+		if (num_read < count)
+			fuse_read_update_size(inode, pos + num_read);
+
 		SetPageUptodate(page);
+	}
+
 	fuse_invalidate_attr(inode); /* atime changed */
  out:
 	unlock_page(page);
@@ -439,8 +462,19 @@ static int fuse_readpage(struct file *fi
 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
 {
 	int i;
+	size_t count = req->misc.read_in.size;
+	size_t num_read = req->out.args[0].size;
+	struct inode *inode = req->pages[0]->mapping->host;
 
-	fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
+	/*
+	 * Short read means EOF.  If file size is larger, truncate it
+	 */
+	if (!req->out.h.error && num_read < count) {
+		loff_t pos = page_offset(req->pages[0]) + num_read;
+		fuse_read_update_size(inode, pos);
+	}
+
+	fuse_invalidate_attr(inode); /* atime changed */
 
 	for (i = 0; i < req->num_pages; i++) {
 		struct page *page = req->pages[i];

--

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>