Skip to contentSkip to conversation

xfs: fix AGF/alloc workqueue deadlock.

PATCH In review Dave Chinner 3 participants 6 messages Oct 5, 2012 – Oct 18, 2012 XFS filesystem

lore
Share & tools
lore
#0 lore raw

cover xfs: fix AGF/alloc workqueue deadlock.

Hi folks,

This is a followup from the last conversation with Mark about this deadlock. I haven't heard anything in the last coupl eof days, so I figured I'd just write the patches that did what I thought is needed.

Basically, the stack switch is only needed for userdata in the delayed allocation path, and nowhere else at this point in time. To make it so we switch stacks only at this point in time, I introduced a flag to pass to xfs_bmapi_write(). This means we don't switch stacks during direct IO, unwritten extent conversion or preallocation via fallocate/XFS_IOC_RESVSP.

I then moved the stack switch to xfs_bmapi_allocate(), which is where allocation happens. All the allocation calls within a single userdata extent allocation occur in this function (both data extent and bmap btree blocks), so switching stacks here will ensure that only mapping calls that require allocation will have the stack switched.

Comments welcome...

Cheers,

Dave.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#3 lore raw

PATCH 2/2 xfs: move allocation stack switch up to xfs_bmapi_allocate

From: Dave Chinner <[email protected]>

Switching stacks are xfs_alloc_vextent can cause deadlocks when we run out of worker threads on the allocation workqueue. This can occur because xfs_bmap_btalloc can make multiple calls to xfs_alloc_vextent() and even if xfs_alloc_vextent() fails it can return with the AGF locked in the current allocation transaction.

If we then need to make another allocation, and all the allocation worker contexts are exhausted because the are blocked waiting for the AGF lock, holder of the AGF cannot get it's xfs-alloc_vextent work completed to release the AGF. Hence allocation effectively deadlocks.

To avoid this, move the stack switch one layer up to xfs_bmapi_allocate() so that all of the allocation attempts in a single switched stack transaction occur in a single worker context. This avoids the problem of an allocation being blocked waiting for a worker thread whilst holding the AGF.

---
 fs/xfs/xfs_alloc.c |   42 +-----------------------------------
 fs/xfs/xfs_alloc.h |    4 ----
 fs/xfs/xfs_bmap.c  |   60 ++++++++++++++++++++++++++++++++++++++++++----------
 fs/xfs/xfs_bmap.h  |    4 ++++
 4 files changed, 54 insertions(+), 56 deletions(-)
fs/xfs/xfs_alloc.c +1 -41
index 43f791b..335206a 100644
@@ -2208,7 +2208,7 @@ xfs_alloc_read_agf(
  * group or loop over the allocation groups to find the result.
  */
 int				/* error */
-__xfs_alloc_vextent(
+xfs_alloc_vextent(
 	xfs_alloc_arg_t	*args)	/* allocation argument structure */
 {
 	xfs_agblock_t	agsize;	/* allocation group size */
@@ -2418,46 +2418,6 @@ error0:
 	return error;
 }
 
-static void
-xfs_alloc_vextent_worker(
-	struct work_struct	*work)
-{
-	struct xfs_alloc_arg	*args = container_of(work,
-						struct xfs_alloc_arg, work);
-	unsigned long		pflags;
-
-	/* we are in a transaction context here */
-	current_set_flags_nested(&pflags, PF_FSTRANS);
-
-	args->result = __xfs_alloc_vextent(args);
-	complete(args->done);
-
-	current_restore_flags_nested(&pflags, PF_FSTRANS);
-}
-
-/*
- * Data allocation requests often come in with little stack to work on. Push
- * them off to a worker thread so there is lots of stack to use. Metadata
- * requests, OTOH, are generally from low stack usage paths, so avoid the
- * context switch overhead here.
- */
-int
-xfs_alloc_vextent(
-	struct xfs_alloc_arg	*args)
-{
-	DECLARE_COMPLETION_ONSTACK(done);
-
-	if (!args->stack_switch)
-		return __xfs_alloc_vextent(args);
-
-
-	args->done = &done;
-	INIT_WORK_ONSTACK(&args->work, xfs_alloc_vextent_worker);
-	queue_work(xfs_alloc_wq, &args->work);
-	wait_for_completion(&done);
-	return args->result;
-}
-
 /*
  * Free an extent.
  * Just break up the extent address and hand off to xfs_free_ag_extent
fs/xfs/xfs_alloc.h +0 -4
index ef7d488..feacb06 100644
@@ -120,10 +120,6 @@ typedef struct xfs_alloc_arg {
 	char		isfl;		/* set if is freelist blocks - !acctg */
 	char		userdata;	/* set if this is user data */
 	xfs_fsblock_t	firstblock;	/* io first block allocated */
-	struct completion *done;
-	struct work_struct work;
-	int		result;
-	char		stack_switch;
 } xfs_alloc_arg_t;
 
 /*
fs/xfs/xfs_bmap.c +49 -11

119 lines not on this page · raw message

fs/xfs/xfs_bmap.h +4 -0
index b68c598..5f469c3 100644
@@ -136,6 +136,10 @@ typedef struct xfs_bmalloca {
 	char			aeof;	/* allocated space at eof */
 	char			conv;	/* overwriting unwritten extents */
 	char			stack_switch;
+	int			flags;
+	struct completion	*done;
+	struct work_struct	work;
+	int			result;
 } xfs_bmalloca_t;
 
 /*
Signature · 6 lines
1.7.10

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw

PATCH 1/2 xfs: introduce XFS_BMAPI_STACK_SWITCH

In reply to Dave Chinner

From: Dave Chinner <[email protected]>

Certain allocation paths through xfs_bmapi_write() are in situations where we have limited stack available. These are almost always in the buffered IO writeback path when convertion delayed allocation extents to real extents.

The current stack switch occurs for userdata allocations, which means we also do stack switches for preallocation, direct IO and unwritten extent conversion, even those these call chains have never been implicated in a stack overrun.

Hence, let's target just the single stack overun offended for stack switches. To do that, introduce a XFS_BMAPI_STACK_SWITCH flag that the caller can pass xfs_bmapi_write() to indicate it should switch stacks if it needs to do allocation.

---
 fs/xfs/xfs_alloc.c |    2 +-
 fs/xfs/xfs_alloc.h |    1 +
 fs/xfs/xfs_bmap.c  |    4 ++++
 fs/xfs/xfs_bmap.h  |    5 ++++-
 fs/xfs/xfs_iomap.c |    4 +++-
 5 files changed, 13 insertions(+), 3 deletions(-)
fs/xfs/xfs_alloc.c +1 -1
index 0287f3b..43f791b 100644
@@ -2447,7 +2447,7 @@ xfs_alloc_vextent(
 {
 	DECLARE_COMPLETION_ONSTACK(done);
 
-	if (!args->userdata)
+	if (!args->stack_switch)
 		return __xfs_alloc_vextent(args);
 
 
fs/xfs/xfs_alloc.h +1 -0
index 93be4a6..ef7d488 100644
@@ -123,6 +123,7 @@ typedef struct xfs_alloc_arg {
 	struct completion *done;
 	struct work_struct work;
 	int		result;
+	char		stack_switch;
 } xfs_alloc_arg_t;
 
 /*
fs/xfs/xfs_bmap.c +4 -0
index e1545ec..9125955 100644
@@ -2441,6 +2441,7 @@ xfs_bmap_btalloc(
 	args.tp = ap->tp;
 	args.mp = mp;
 	args.fsbno = ap->blkno;
+	args.stack_switch = ap->stack_switch;
 
 	/* Trim the allocation back to the maximum an AG can fit. */
 	args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
@@ -4675,6 +4676,9 @@ xfs_bmapi_allocate(
 			return error;
 	}
 
+	if (flags & XFS_BMAPI_STACK_SWITCH)
+		bma->stack_switch = 1;
+
 	error = xfs_bmap_alloc(bma);
 	if (error)
 		return error;
fs/xfs/xfs_bmap.h +4 -1
index 803b56d..b68c598 100644
@@ -77,6 +77,7 @@ typedef	struct xfs_bmap_free
  * from written to unwritten, otherwise convert from unwritten to written.
  */
 #define XFS_BMAPI_CONVERT	0x040
+#define XFS_BMAPI_STACK_SWITCH	0x080
 
 #define XFS_BMAPI_FLAGS \
 	{ XFS_BMAPI_ENTIRE,	"ENTIRE" }, \
@@ -85,7 +86,8 @@ typedef	struct xfs_bmap_free
 	{ XFS_BMAPI_PREALLOC,	"PREALLOC" }, \
 	{ XFS_BMAPI_IGSTATE,	"IGSTATE" }, \
 	{ XFS_BMAPI_CONTIG,	"CONTIG" }, \
-	{ XFS_BMAPI_CONVERT,	"CONVERT" }
+	{ XFS_BMAPI_CONVERT,	"CONVERT" }, \
+	{ XFS_BMAPI_STACK_SWITCH, "STACK_SWITCH" }
 
 
 static inline int xfs_bmapi_aflag(int w)
@@ -133,6 +135,7 @@ typedef struct xfs_bmalloca {
 	char			userdata;/* set if is user data */
 	char			aeof;	/* allocated space at eof */
 	char			conv;	/* overwriting unwritten extents */
+	char			stack_switch;
 } xfs_bmalloca_t;
 
 /*
fs/xfs/xfs_iomap.c +3 -1
index f858b90..a066cf1 100644
@@ -575,7 +575,9 @@ xfs_iomap_write_allocate(
 			 * pointer that the caller gave to us.
 			 */
 			error = xfs_bmapi_write(tp, ip, map_start_fsb,
-						count_fsb, 0, &first_block, 1,
+						count_fsb,
+						XFS_BMAPI_STACK_SWITCH,
+						&first_block, 1,
 						imap, &nimaps, &free_list);
 			if (error)
 				goto trans_cancel;
Signature · 6 lines
1.7.10

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] xfs: introduce XFS_BMAPI_STACK_SWITCH

Quoted 12 lines · Dave Chinner

From: Dave Chinner<[email protected]>

Certain allocation paths through xfs_bmapi_write() are in situations where we have limited stack available. These are almost always in the buffered IO writeback path when convertion delayed allocation extents to real extents.

The current stack switch occurs for userdata allocations, which means we also do stack switches for preallocation, direct IO and unwritten extent conversion, even those these call chains have never been implicated in a stack overrun.

Quoted 7 lines

Hence, let's target just the single stack overun offended for stack switches. To do that, introduce a XFS_BMAPI_STACK_SWITCH flag that the caller can pass xfs_bmapi_write() to indicate it should switch stacks if it needs to do allocation.

---

Looks good.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] xfs: move allocation stack switch up to xfs_bmapi_allocate

In reply to Dave Chinner
Quoted 14 lines · Dave Chinner

From: Dave Chinner<[email protected]>

Switching stacks are xfs_alloc_vextent can cause deadlocks when we run out of worker threads on the allocation workqueue. This can occur because xfs_bmap_btalloc can make multiple calls to xfs_alloc_vextent() and even if xfs_alloc_vextent() fails it can return with the AGF locked in the current allocation transaction.

If we then need to make another allocation, and all the allocation worker contexts are exhausted because the are blocked waiting for the AGF lock, holder of the AGF cannot get it's xfs-alloc_vextent work completed to release the AGF. Hence allocation effectively deadlocks.

Quoted 7 lines

To avoid this, move the stack switch one layer up to xfs_bmapi_allocate() so that all of the allocation attempts in a single switched stack transaction occur in a single worker context. This avoids the problem of an allocation being blocked waiting for a worker thread whilst holding the AGF.

Looks good.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#5 lore raw
In reply to Dave Chinner

Hi Dave,

Quoted 12 lines · Dave Chinner

This is a followup from the last conversation with Mark about this deadlock. I haven't heard anything in the last coupl eof days, so I figured I'd just write the patches that did what I thought is needed.

Basically, the stack switch is only needed for userdata in the delayed allocation path, and nowhere else at this point in time. To make it so we switch stacks only at this point in time, I introduced a flag to pass to xfs_bmapi_write(). This means we don't switch stacks during direct IO, unwritten extent conversion or preallocation via fallocate/XFS_IOC_RESVSP.

Quoted 8 lines

I then moved the stack switch to xfs_bmapi_allocate(), which is where allocation happens. All the allocation calls within a single userdata extent allocation occur in this function (both data extent and bmap btree blocks), so switching stacks here will ensure that only mapping calls that require allocation will have the stack switched.

Comments welcome...

These two patches have been committed to git://oss.sgi.com/xfs/xfs.git, master and for-next branches.

Regards,
Ben

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs