Skip to contentSkip to conversation

xfs: add readahead bufs to lru early to prevent post-unmount panic

PATCH In review Brian Foster 2 participants 18 messages Jun 30, 2016 – Jul 13, 2016 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: add readahead bufs to lru early to prevent post-unmount panic

Newly allocated XFS metadata buffers are added to the LRU once the hold count is released, which typically occurs after I/O completion. There is no other mechanism at current that tracks the existence or I/O state of a new buffer. Further, readahead I/O tends to be submitted asynchronously by nature, which means the I/O can remain in flight and actually complete long after the calling context is gone. This means that file descriptors or any other holds on the filesystem can be released, allowing the filesystem to be unmounted while I/O is still in flight. When I/O completion occurs, core data structures may have been freed, causing completion to run into invalid memory accesses and likely to panic.

This problem is reproduced on XFS via directory readahead. A filesystem is mounted, a directory is opened/closed and the filesystem immediately unmounted. The open/close cycle triggers a directory readahead that if delayed long enough, runs buffer I/O completion after the unmount has completed.

To work around this problem, add readahead buffers to the LRU earlier than other buffers (when the buffer is allocated, specifically). The buffer hold count will ultimately remain until I/O completion, which means any shrinker activity will skip the buffer until then. This makes the buffer visible to xfs_wait_buftarg(), however, which ensures that an unmount or quiesce waits for I/O completion appropriately.

---

This addresses the problem reproduced by the recently posted xfstests test:

http://thread.gmane.org/gmane.comp.file-systems.fstests/2740

This could probably be made more involved, i.e., to create another list of buffers in flight or some such. This seems more simple/sane to me, however, and survives my testing so far...

Brian

 fs/xfs/xfs_buf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_buf.c +12 -0
index 4665ff6..3f03df9 100644
@@ -590,8 +590,20 @@ xfs_buf_get_map(
 		return NULL;
 	}
 
+	/*
+	 * If the buffer found doesn't match the one allocated above, somebody
+	 * else beat us to insertion and we can toss the new one.
+	 *
+	 * If we did add the buffer and it happens to be readahead, add to the
+	 * LRU now rather than waiting until the hold is released. Otherwise,
+	 * the buffer is not visible to xfs_wait_buftarg() while in flight and
+	 * nothing else prevents an unmount before I/O completion.
+	 */
 	if (bp != new_bp)
 		xfs_buf_free(new_bp);
+	else if (flags & XBF_READ_AHEAD &&
+		 list_lru_add(&bp->b_target->bt_lru, &bp->b_lru))
+		atomic_inc(&bp->b_hold);
 
 found:
 	if (!bp->b_addr) {
Signature · 6 lines
2.5.5

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw
Quoted 54 lines · Brian Foster

Newly allocated XFS metadata buffers are added to the LRU once the hold count is released, which typically occurs after I/O completion. There is no other mechanism at current that tracks the existence or I/O state of a new buffer. Further, readahead I/O tends to be submitted asynchronously by nature, which means the I/O can remain in flight and actually complete long after the calling context is gone. This means that file descriptors or any other holds on the filesystem can be released, allowing the filesystem to be unmounted while I/O is still in flight. When I/O completion occurs, core data structures may have been freed, causing completion to run into invalid memory accesses and likely to panic.

This problem is reproduced on XFS via directory readahead. A filesystem is mounted, a directory is opened/closed and the filesystem immediately unmounted. The open/close cycle triggers a directory readahead that if delayed long enough, runs buffer I/O completion after the unmount has completed.

To work around this problem, add readahead buffers to the LRU earlier than other buffers (when the buffer is allocated, specifically). The buffer hold count will ultimately remain until I/O completion, which means any shrinker activity will skip the buffer until then. This makes the buffer visible to xfs_wait_buftarg(), however, which ensures that an unmount or quiesce waits for I/O completion appropriately.

---

This addresses the problem reproduced by the recently posted xfstests test:

http://thread.gmane.org/gmane.comp.file-systems.fstests/2740

This could probably be made more involved, i.e., to create another list of buffers in flight or some such. This seems more simple/sane to me, however, and survives my testing so far...

Brian

 fs/xfs/xfs_buf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_buf.c +4 -0
index 4665ff6..3f03df9 100644
@@ -590,8 +590,20 @@ xfs_buf_get_map(
 		return NULL;
 	}
 
+	/*
+	 * If the buffer found doesn't match the one allocated above, somebody
+	 * else beat us to insertion and we can toss the new one.
+	 *

fs/xfs/xfs_buf.c xfs_buf_get_map( · line 606

Quoted 10 lines
fs/xfs/xfs_buf.c +8 -0
+	 * If we did add the buffer and it happens to be readahead, add to the
+	 * LRU now rather than waiting until the hold is released. Otherwise,
+	 * the buffer is not visible to xfs_wait_buftarg() while in flight and
+	 * nothing else prevents an unmount before I/O completion.
+	 */
 	if (bp != new_bp)
 		xfs_buf_free(new_bp);
+	else if (flags & XBF_READ_AHEAD &&
+		 list_lru_add(&bp->b_target->bt_lru, &bp->b_lru))
+		atomic_inc(&bp->b_hold);

This doesn't sit right with me. The LRU is for "unused" objects, and readahead objects are not unused until IO completes and nobody is waiting on them.

As it is, this points out another problem with readahead buffers - they aren't actually being cached properly because b_lru_ref == 0, which means they are immediately reclaimed on IO completion rather than being added to the LRU....

I also think that it's not sufficient to cover the generic case of async IO that has no waiter. i.e. we could do get_buf, submit async write, drop submitter reference, and now we have the same problem but on a write. i.e. this problem is and async IO issue, not a readahead issue.

I think that it might be better to fix it by doing this:

1. ensure async IO submission always has b_lru_ref set, and if it isn't, set it to 1. This ensures the buffer will be added to the LRU on completion if it isn't already there.

2. keep a count of async buffer IO in progress. A per-cpu counter in the buftarg will be fine for this. Increment in xfs_buf_submit(), decrement in the xfs_buf_rele() call from xfs_buf_iodone() once we've determined if the buffer needs adding to the LRU or not.

3. make xfs_wait_buftarg() wait until the async IO count goes to zero before it gives up trying to release buffers on the LRU.

That will ensure readahead buffers are cached, and we capture both async read and async write buffers in xfs_wait_buftarg().

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#2 lore raw
Quoted 61 lines · Dave Chinner
Quoted 60 lines · Brian Foster

Newly allocated XFS metadata buffers are added to the LRU once the hold count is released, which typically occurs after I/O completion. There is no other mechanism at current that tracks the existence or I/O state of a new buffer. Further, readahead I/O tends to be submitted asynchronously by nature, which means the I/O can remain in flight and actually complete long after the calling context is gone. This means that file descriptors or any other holds on the filesystem can be released, allowing the filesystem to be unmounted while I/O is still in flight. When I/O completion occurs, core data structures may have been freed, causing completion to run into invalid memory accesses and likely to panic.

This problem is reproduced on XFS via directory readahead. A filesystem is mounted, a directory is opened/closed and the filesystem immediately unmounted. The open/close cycle triggers a directory readahead that if delayed long enough, runs buffer I/O completion after the unmount has completed.

To work around this problem, add readahead buffers to the LRU earlier than other buffers (when the buffer is allocated, specifically). The buffer hold count will ultimately remain until I/O completion, which means any shrinker activity will skip the buffer until then. This makes the buffer visible to xfs_wait_buftarg(), however, which ensures that an unmount or quiesce waits for I/O completion appropriately.

---

This addresses the problem reproduced by the recently posted xfstests test:

http://thread.gmane.org/gmane.comp.file-systems.fstests/2740

This could probably be made more involved, i.e., to create another list of buffers in flight or some such. This seems more simple/sane to me, however, and survives my testing so far...

Brian

 fs/xfs/xfs_buf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_buf.c +9 -0
index 4665ff6..3f03df9 100644
@@ -590,8 +590,20 @@ xfs_buf_get_map(
 		return NULL;
 	}
 
+	/*
+	 * If the buffer found doesn't match the one allocated above, somebody
+	 * else beat us to insertion and we can toss the new one.
+	 *
+	 * If we did add the buffer and it happens to be readahead, add to the
+	 * LRU now rather than waiting until the hold is released. Otherwise,
+	 * the buffer is not visible to xfs_wait_buftarg() while in flight and
+	 * nothing else prevents an unmount before I/O completion.
+	 */
 	if (bp != new_bp)
Quoted 9 lines

fs/xfs/xfs_buf.c xfs_buf_get_map( · line 606

Quoted 4 lines
fs/xfs/xfs_buf.c +3 -0
 		xfs_buf_free(new_bp);
+	else if (flags & XBF_READ_AHEAD &&
+		 list_lru_add(&bp->b_target->bt_lru, &bp->b_lru))
+		atomic_inc(&bp->b_hold);

This doesn't sit right with me. The LRU is for "unused" objects, and readahead objects are not unused until IO completes and nobody is waiting on them.

Sure, but don't buffers remain on the LRU once they are reused? My impression is that this patch really doesn't change behavior in that regard. IOWs, the buffer is going to end up on the LRU either way and either end up disposed if it is never actually accessed or "used" otherwise and the hold count is bumped. Further, isn't the hold count mechanism precisely to handle "used" buffers on the LRU?

Quoted 5 lines

As it is, this points out another problem with readahead buffers - they aren't actually being cached properly because b_lru_ref == 0, which means they are immediately reclaimed on IO completion rather than being added to the LRU....

Not following... _xfs_buf_alloc() sets b_lru_ref to 1 and I don't see it set/decremented unless we stale or dispose it. What am I missing?

Quoted 6 lines

I also think that it's not sufficient to cover the generic case of async IO that has no waiter. i.e. we could do get_buf, submit async write, drop submitter reference, and now we have the same problem but on a write. i.e. this problem is and async IO issue, not a readahead issue.

Indeed. I thought about making the current patch check for ASYNC, but opted to try and make it more isolated (I suppose ASYNC|READAHEAD would have been moreso).

The other thought I had was to change where buffers are added to the LRU altogether, but didn't want to jump right to that. Is there any issue with populating the LRU with initially held buffers as such, or any particular reason LRU addition was deferred to I/O completion in the first place?

Quoted 6 lines

I think that it might be better to fix it by doing this:

1. ensure async IO submission always has b_lru_ref set, and if it isn't, set it to 1. This ensures the buffer will be added to the LRU on completion if it isn't already there.

See above. I'm not following why this is necessary (if it is, it seems indicative of a bug).

Quoted 10 lines

2. keep a count of async buffer IO in progress. A per-cpu counter in the buftarg will be fine for this. Increment in xfs_buf_submit(), decrement in the xfs_buf_rele() call from xfs_buf_iodone() once we've determined if the buffer needs adding to the LRU or not.

3. make xfs_wait_buftarg() wait until the async IO count goes to zero before it gives up trying to release buffers on the LRU.

This is along the lines of what I was thinking wrt to a list (explicit I/O tracking one way or another). The count is more simple and does cover the arbitrary read/write case, but is still more code and seems slightly duplicative to me because in most cases the LRU wait already handles this. I'm also not totally sure we need to handle the write case. Are we susceptible to this issue anywhere that isn't already protected by the sb write protection mechanism, for example?

OTOH, explicit tracking might provide a more generic way to deal with uncached buffers as opposed to lock cycling.

Brian

Quoted 9 lines

That will ensure readahead buffers are cached, and we capture both async read and async write buffers in xfs_wait_buftarg().

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]

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

#3 lore raw
Quoted 72 lines · Brian Foster
Quoted 61 lines · Dave Chinner
Quoted 60 lines · Brian Foster

Newly allocated XFS metadata buffers are added to the LRU once the hold count is released, which typically occurs after I/O completion. There is no other mechanism at current that tracks the existence or I/O state of a new buffer. Further, readahead I/O tends to be submitted asynchronously by nature, which means the I/O can remain in flight and actually complete long after the calling context is gone. This means that file descriptors or any other holds on the filesystem can be released, allowing the filesystem to be unmounted while I/O is still in flight. When I/O completion occurs, core data structures may have been freed, causing completion to run into invalid memory accesses and likely to panic.

This problem is reproduced on XFS via directory readahead. A filesystem is mounted, a directory is opened/closed and the filesystem immediately unmounted. The open/close cycle triggers a directory readahead that if delayed long enough, runs buffer I/O completion after the unmount has completed.

To work around this problem, add readahead buffers to the LRU earlier than other buffers (when the buffer is allocated, specifically). The buffer hold count will ultimately remain until I/O completion, which means any shrinker activity will skip the buffer until then. This makes the buffer visible to xfs_wait_buftarg(), however, which ensures that an unmount or quiesce waits for I/O completion appropriately.

---

This addresses the problem reproduced by the recently posted xfstests test:

http://thread.gmane.org/gmane.comp.file-systems.fstests/2740

This could probably be made more involved, i.e., to create another list of buffers in flight or some such. This seems more simple/sane to me, however, and survives my testing so far...

Brian

 fs/xfs/xfs_buf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_buf.c +9 -0
index 4665ff6..3f03df9 100644
@@ -590,8 +590,20 @@ xfs_buf_get_map(
 		return NULL;
 	}
 
+	/*
+	 * If the buffer found doesn't match the one allocated above, somebody
+	 * else beat us to insertion and we can toss the new one.
+	 *
+	 * If we did add the buffer and it happens to be readahead, add to the
+	 * LRU now rather than waiting until the hold is released. Otherwise,
+	 * the buffer is not visible to xfs_wait_buftarg() while in flight and
+	 * nothing else prevents an unmount before I/O completion.
+	 */
 	if (bp != new_bp)
Quoted 9 lines

fs/xfs/xfs_buf.c xfs_buf_get_map( · line 606

Quoted 4 lines
fs/xfs/xfs_buf.c +3 -0
 		xfs_buf_free(new_bp);
+	else if (flags & XBF_READ_AHEAD &&
+		 list_lru_add(&bp->b_target->bt_lru, &bp->b_lru))
+		atomic_inc(&bp->b_hold);

This doesn't sit right with me. The LRU is for "unused" objects, and readahead objects are not unused until IO completes and nobody is waiting on them.

Quoted 1 line

Sure, but don't buffers remain on the LRU once they are reused?

Yes, but that's once the contents have been used.

Quoted 3 lines

My
impression is that this patch really doesn't change behavior in that regard.

Not really - but it makes the readahead buffers completely different to the rest of the buffers on the LRU in that they don't contain valid data.

For the same reason we also don't add buffers at creation time because they don't hold valid data until after the code that has created/read in the data has released it.....

Quoted 4 lines

IOWs, the buffer is going to end up on the LRU either way and either end up disposed if it is never actually accessed or "used" otherwise and the hold count is bumped. Further, isn't the hold count mechanism precisely to handle "used" buffers on the LRU?

... hence we only add the buffer to the LRU once the initial hold count reaches zero and the owner has indicated that the buffer should be moved to the LRU by setting b_lru_ref. Once we've added the buffer to the LRU, we add a reference count that persists untile b_lru_ref is decremented to zero. At that point, we remove the euffer from the LRU and release it. That (b_lru_ref == 0) then triggers the buffer to be freed....

Note also that buffers that contain stale data (i.e. XBF_STALE) are not added to the LRU and xfs_buf_stale() removes buffers from the LRU, so shortcutting the above reclaim lifecycle in the case where a user invalidates the contents of the buffer. The end result is the same - there are no buffers with stale contents on the LRU.

readahead is a special case - there is no accessor to say "cache this buffer for N references", but we have to keep it around for some time so that readahead is effective. We don't want to add it before we've done IO on it, and realistically we should only add it to the LRU if there was no IO error. We've had to work around bugs introduced by caching failed readahead buffers on the LRU in the past....

Quoted 8 lines
Quoted 5 lines

As it is, this points out another problem with readahead buffers - they aren't actually being cached properly because b_lru_ref == 0, which means they are immediately reclaimed on IO completion rather than being added to the LRU....

Not following... _xfs_buf_alloc() sets b_lru_ref to 1 and I don't see it set/decremented unless we stale or dispose it. What am I missing?

Sorry, I misread the cscope output I was looking at to quickly remind me of how it all worked - xfs_buf_stale() sets it to zero, not _xfs_buf_alloc().

Quoted 11 lines
Quoted 6 lines

I also think that it's not sufficient to cover the generic case of async IO that has no waiter. i.e. we could do get_buf, submit async write, drop submitter reference, and now we have the same problem but on a write. i.e. this problem is and async IO issue, not a readahead issue.

Indeed. I thought about making the current patch check for ASYNC, but opted to try and make it more isolated (I suppose ASYNC|READAHEAD would have been moreso).

Quoted 5 lines

The other thought I had was to change where buffers are added to the LRU altogether, but didn't want to jump right to that. Is there any issue with populating the LRU with initially held buffers as such, or any particular reason LRU addition was deferred to I/O completion in the first place?

Yes, because then we have to deal with buffers that fail memory allocation, read IO or are allocated just to invalidate a range of disk blocks during a transaction (e.g. removing a remote attribute). There are probably other cases where we don't want to put buffers we allocate onto the LRU, but I can't think of any more right now.

Quoted 21 lines
Quoted 6 lines

I think that it might be better to fix it by doing this:

1. ensure async IO submission always has b_lru_ref set, and if it isn't, set it to 1. This ensures the buffer will be added to the LRU on completion if it isn't already there.

See above. I'm not following why this is necessary (if it is, it seems indicative of a bug).

Quoted 10 lines

2. keep a count of async buffer IO in progress. A per-cpu counter in the buftarg will be fine for this. Increment in xfs_buf_submit(), decrement in the xfs_buf_rele() call from xfs_buf_iodone() once we've determined if the buffer needs adding to the LRU or not.

3. make xfs_wait_buftarg() wait until the async IO count goes to zero before it gives up trying to release buffers on the LRU.

Quoted 5 lines

This is along the lines of what I was thinking wrt to a list (explicit I/O tracking one way or another). The count is more simple and does cover the arbitrary read/write case, but is still more code and seems slightly duplicative to me because in most cases the LRU wait already handles this.

Sure, but adding a list means you need global locks and when you have lots of concurrent readahead going on that will be a problem. readahead is supposed to have minimal overhead, so anything that adds submission serialisation is not the best idea... The counter has almost no overhead for the async IO case, read or write, and that's all we need to allow the IO to complete safely during unmount.

Quoted 3 lines

I'm also not totally sure we need to handle the write case. Are we susceptible to this issue anywhere that isn't already protected by the sb write protection mechanism, for example?

We can do metadata buffer writes outside the superblock write protection context. A good example of that is log recovery on read-only filesystems....

Quoted 2 lines

OTOH, explicit tracking might provide a more generic way to deal with uncached buffers as opposed to lock cycling.

Well, only if those uncached buffers are using async IO....

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#4 lore raw
Quoted 77 lines · Dave Chinner
Quoted 72 lines · Brian Foster
Quoted 61 lines · Dave Chinner
Quoted 60 lines · Brian Foster

Newly allocated XFS metadata buffers are added to the LRU once the hold count is released, which typically occurs after I/O completion. There is no other mechanism at current that tracks the existence or I/O state of a new buffer. Further, readahead I/O tends to be submitted asynchronously by nature, which means the I/O can remain in flight and actually complete long after the calling context is gone. This means that file descriptors or any other holds on the filesystem can be released, allowing the filesystem to be unmounted while I/O is still in flight. When I/O completion occurs, core data structures may have been freed, causing completion to run into invalid memory accesses and likely to panic.

This problem is reproduced on XFS via directory readahead. A filesystem is mounted, a directory is opened/closed and the filesystem immediately unmounted. The open/close cycle triggers a directory readahead that if delayed long enough, runs buffer I/O completion after the unmount has completed.

To work around this problem, add readahead buffers to the LRU earlier than other buffers (when the buffer is allocated, specifically). The buffer hold count will ultimately remain until I/O completion, which means any shrinker activity will skip the buffer until then. This makes the buffer visible to xfs_wait_buftarg(), however, which ensures that an unmount or quiesce waits for I/O completion appropriately.

---

This addresses the problem reproduced by the recently posted xfstests test:

http://thread.gmane.org/gmane.comp.file-systems.fstests/2740

This could probably be made more involved, i.e., to create another list of buffers in flight or some such. This seems more simple/sane to me, however, and survives my testing so far...

Brian

 fs/xfs/xfs_buf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_buf.c +9 -0
index 4665ff6..3f03df9 100644
@@ -590,8 +590,20 @@ xfs_buf_get_map(
 		return NULL;
 	}
 
+	/*
+	 * If the buffer found doesn't match the one allocated above, somebody
+	 * else beat us to insertion and we can toss the new one.
+	 *
+	 * If we did add the buffer and it happens to be readahead, add to the
+	 * LRU now rather than waiting until the hold is released. Otherwise,
+	 * the buffer is not visible to xfs_wait_buftarg() while in flight and
+	 * nothing else prevents an unmount before I/O completion.
+	 */
 	if (bp != new_bp)
Quoted 9 lines

fs/xfs/xfs_buf.c xfs_buf_get_map( · line 606

Quoted 4 lines
fs/xfs/xfs_buf.c +3 -0
 		xfs_buf_free(new_bp);
+	else if (flags & XBF_READ_AHEAD &&
+		 list_lru_add(&bp->b_target->bt_lru, &bp->b_lru))
+		atomic_inc(&bp->b_hold);

This doesn't sit right with me. The LRU is for "unused" objects, and readahead objects are not unused until IO completes and nobody is waiting on them.

Quoted 1 line

Sure, but don't buffers remain on the LRU once they are reused?

Yes, but that's once the contents have been used.

Quoted 8 lines
Quoted 3 lines

My
impression is that this patch really doesn't change behavior in that regard.

Not really - but it makes the readahead buffers completely different to the rest of the buffers on the LRU in that they don't contain valid data.

Yes, this does specially handle readahead buffers in terms of implementation, which is unfortunate...

Quoted 4 lines

For the same reason we also don't add buffers at creation time because they don't hold valid data until after the code that has created/read in the data has released it.....

... but buffers are added to the LRU at I/O completion regardless of whether they have valid data or not (i.e., even on I/O error).

Quoted 13 lines
Quoted 4 lines

IOWs, the buffer is going to end up on the LRU either way and either end up disposed if it is never actually accessed or "used" otherwise and the hold count is bumped. Further, isn't the hold count mechanism precisely to handle "used" buffers on the LRU?

... hence we only add the buffer to the LRU once the initial hold count reaches zero and the owner has indicated that the buffer should be moved to the LRU by setting b_lru_ref. Once we've added the buffer to the LRU, we add a reference count that persists untile b_lru_ref is decremented to zero. At that point, we remove the euffer from the LRU and release it. That (b_lru_ref == 0) then triggers the buffer to be freed....

Quoted 6 lines

Note also that buffers that contain stale data (i.e. XBF_STALE) are not added to the LRU and xfs_buf_stale() removes buffers from the LRU, so shortcutting the above reclaim lifecycle in the case where a user invalidates the contents of the buffer. The end result is the same - there are no buffers with stale contents on the LRU.

Yes, but this seems arbitrary. The buffer may or may not be on the LRU when it is marked stale.

Quoted 8 lines

readahead is a special case - there is no accessor to say "cache this buffer for N references", but we have to keep it around for some time so that readahead is effective. We don't want to add it before we've done IO on it, and realistically we should only add it to the LRU if there was no IO error. We've had to work around bugs introduced by caching failed readahead buffers on the LRU in the past....

Ok, that makes sense. At least, I get the sense that you have a view of the LRU that is slightly different from what the code actually does (e.g., buffers w/ I/O errors on the LRU when they shouldn't be), which is why I'm trying to reconcile some of the reasoning here.

In other words, if the LRU was an unused only buffer list where used or stale buffers were pulled off and later reinserted/reprioritized (in terms of reclaim), then this would all make complete sense to me. In actuality, we have an LRU list that has buffers with I/O errors, buffers in use, buffers that might or might not be stale, etc.

Quoted 30 lines
Quoted 8 lines
Quoted 5 lines

As it is, this points out another problem with readahead buffers - they aren't actually being cached properly because b_lru_ref == 0, which means they are immediately reclaimed on IO completion rather than being added to the LRU....

Not following... _xfs_buf_alloc() sets b_lru_ref to 1 and I don't see it set/decremented unless we stale or dispose it. What am I missing?

Sorry, I misread the cscope output I was looking at to quickly remind me of how it all worked - xfs_buf_stale() sets it to zero, not _xfs_buf_alloc().

Quoted 11 lines
Quoted 6 lines

I also think that it's not sufficient to cover the generic case of async IO that has no waiter. i.e. we could do get_buf, submit async write, drop submitter reference, and now we have the same problem but on a write. i.e. this problem is and async IO issue, not a readahead issue.

Indeed. I thought about making the current patch check for ASYNC, but opted to try and make it more isolated (I suppose ASYNC|READAHEAD would have been moreso).

Quoted 5 lines

The other thought I had was to change where buffers are added to the LRU altogether, but didn't want to jump right to that. Is there any issue with populating the LRU with initially held buffers as such, or any particular reason LRU addition was deferred to I/O completion in the first place?

Quoted 6 lines

Yes, because then we have to deal with buffers that fail memory allocation, read IO or are allocated just to invalidate a range of disk blocks during a transaction (e.g. removing a remote attribute). There are probably other cases where we don't want to put buffers we allocate onto the LRU, but I can't think of any more right now.

I was thinking more along the lines of insertion on I/O submission rather than allocation. I.e., similar the proposed #1 below, but to actually insert to the list such that we never lose track of the buffer. My understanding is that for an async buffer, LRU insertion is imminent at this point as it is (unless perhaps it is looked up and marked stale by somebody else before I/O completion, but I doubt that's common).

Quoted 27 lines
Quoted 21 lines
Quoted 6 lines

I think that it might be better to fix it by doing this:

1. ensure async IO submission always has b_lru_ref set, and if it isn't, set it to 1. This ensures the buffer will be added to the LRU on completion if it isn't already there.

See above. I'm not following why this is necessary (if it is, it seems indicative of a bug).

Quoted 10 lines

2. keep a count of async buffer IO in progress. A per-cpu counter in the buftarg will be fine for this. Increment in xfs_buf_submit(), decrement in the xfs_buf_rele() call from xfs_buf_iodone() once we've determined if the buffer needs adding to the LRU or not.

3. make xfs_wait_buftarg() wait until the async IO count goes to zero before it gives up trying to release buffers on the LRU.

Quoted 5 lines

This is along the lines of what I was thinking wrt to a list (explicit I/O tracking one way or another). The count is more simple and does cover the arbitrary read/write case, but is still more code and seems slightly duplicative to me because in most cases the LRU wait already handles this.

Quoted 8 lines

Sure, but adding a list means you need global locks and when you have lots of concurrent readahead going on that will be a problem. readahead is supposed to have minimal overhead, so anything that adds submission serialisation is not the best idea... The counter has almost no overhead for the async IO case, read or write, and that's all we need to allow the IO to complete safely during unmount.

Indeed, I'm not advocating a list over counters. Just pointing out that either seem potentially unnecessary or more complexity than really necessary.

I guess my initial approach was to make the fix as isolated as possible because this code is hairy and it seems we have a lot of little hacks around to deal with other corner cases such as this. Surely this adds yet another one, but afaics, so does a counter mechanism. At the end of the day, the only real issue we know it fixes is this readahead corner case.

I think that if we're going to go as far as adding a list/counter mechanism, we should try to fix this in a way that is more broadly useful. By that I mean fix up the existing mechanism or add something that allows us to start unwinding some of the preexisting hacks such as cycling buffer locks to ensure I/O completion (perhaps alleviating the need to hold locks across I/O in the first place), etc.

Perhaps the counter approach opens the door for that, I have to think about it some more..

Quoted 8 lines
Quoted 3 lines

I'm also not totally sure we need to handle the write case. Are we susceptible to this issue anywhere that isn't already protected by the sb write protection mechanism, for example?

We can do metadata buffer writes outside the superblock write protection context. A good example of that is log recovery on read-only filesystems....

This waits for I/O to complete so is unaffected by this problem. Now that I take a look at that, I'm not sure we'd be able to replace that with an I/O counter wait since technically that would wait for all in-flight I/O to complete. This context (xfs_buf_delwri_submit()) is only looking to wait for the locally submitted I/O to complete. Ugh.

Brian

Quoted 16 lines
Quoted 2 lines

OTOH, explicit tracking might provide a more generic way to deal with uncached buffers as opposed to lock cycling.

Well, only if those uncached buffers are using async IO....

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

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

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

#5 lore raw
Quoted 11 lines · Brian Foster
Quoted 7 lines · Dave Chinner

On Thu, Jun 30, 2016 at 07:56:21PM -0400, Brian Foster wrote:
Note also that buffers that contain stale data (i.e. XBF_STALE) are not added to the LRU and xfs_buf_stale() removes buffers from the LRU, so shortcutting the above reclaim lifecycle in the case where a user invalidates the contents of the buffer. The end result is the same - there are no buffers with stale contents on the LRU.

Yes, but this seems arbitrary. The buffer may or may not be on the LRU when it is marked stale.

Yes, because (as I've already explained) we can mark buffers stale without needing to fill them with data. But whether or not the buffer is in the LRU when we mark it stale is irrelevant, because we only have a requirement that stale buffers are removed from the LRU so that they are torn down immediately on release.

This is actually very important because this is how we prevent duplicate buffers from entering the cache when we free metadata. Once we free a metadata buffer and finish committing the transaction, the buffer must not be kept in the cache because the extent it covers could be reallocated at any time, and in a different shape. It could even be user data. In all cases, the buffer needs to be removed from the cache immediately, and that's why xfs_buf_stale() behaves like this.

Quoted 14 lines
Quoted 8 lines

readahead is a special case - there is no accessor to say "cache this buffer for N references", but we have to keep it around for some time so that readahead is effective. We don't want to add it before we've done IO on it, and realistically we should only add it to the LRU if there was no IO error. We've had to work around bugs introduced by caching failed readahead buffers on the LRU in the past....

Ok, that makes sense. At least, I get the sense that you have a view of the LRU that is slightly different from what the code actually does (e.g., buffers w/ I/O errors on the LRU when they shouldn't be), which is why I'm trying to reconcile some of the reasoning here.

Quoted 5 lines

In other words, if the LRU was an unused only buffer list where used or stale buffers were pulled off and later reinserted/reprioritized (in terms of reclaim), then this would all make complete sense to me. In actuality, we have an LRU list that has buffers with I/O errors, buffers in use, buffers that might or might not be stale, etc.

Nothing is as simple as it seems - clean design never remains that
way once reality intrudes. However, the general rule is that buffers are not added to the LRU until the initial user has finished and release them. THis is done for many reasons.

Firstly, We do not add them a lookup time because we do not know if the caller wants them to be added to the LRU.

Secondly, we also don't add at lookup time because buffer lookup is the single hottest path in the XFS codebase. i.e. xfs_buf_find() and the struct xfs_buf are one of the few places where we've actually optimised the code to minimise cache footprint and CPU overhead.

Currently, all of th einformation that a buffer lookup accesses is in the first cacheline of the struct xfs_buf, ensuring that we only take one cache miss penalty per buffer we walk through the rb-tree. If we now add a check to see if the buffer is on the LRU into the lookup path, we are - at minimum - adding an extra cacheline miss to the hot path. If we have to add the buffer to the LRU, then we're adding another couple of cacheline misses and another lock, all of which will have an impact on performance.

THird, we add/remove buffers to/from the LRU lazily in paths that are not performance critical as a result. This is exactly the same algorithms the VFS uses for the inode and dentry cache LRUs, and we also use this for the XFS dquot LRU, too. The b_lru_ref count replaces the single referenced flag that the inode/dentry caches use so we can prioritise reclaim to match typical btree and directory access patterns (i.e. reclaim data before leaf before node before root) but otherwise the buffer LRU is no different to the VFS cache algorithms....

Finally, and perhaps the most important, is behaviour under low memory conditions. If we add the buffer to the LRU when we first look it up in the cache, the shrinker can now see it and decrement the b_lru_ref count while we are doing the first operation on that buffer. Say this transaction generates sufficient memory pressure that it causes the shrinker to completely decrement b_lru_ref - it will then remove the buffer from the LRU *before we've even finished with it*. This will cause the buffer to be freed immediately after the caller releases it (because b_lru_ref is zero), and so the next access requires the buffer to be read from disk again. What we end up with here is a situation where the buffer cache cannot maintian a working set of buffers between operations because the memory pressure within an operation causes the buffers to be removed from the LRU before the operation completes.

So, yeah, there's lots of "assumed knowledge" in why the buffer cache LRU is structured the way it is. That, unfortunately, is pretty much typical of all the linux slab caches that are controlled by shrinkers....

FWIW, readahead is often not complete before the followup read comes in, which means there's a caller blocked on the buffer with an active reference by the time IO completion runs xfs_buf_relse() to unlock the buffer. In these cases, the readahead buffer does not go onto the LRU until after the blocked caller has finished with the buffer and called xfs_buf_rele() itself (i.e. after the transaction subsystem is done with it).

Quoted 13 lines
Quoted 6 lines
Quoted 5 lines

The other thought I had was to change where buffers are added to the LRU altogether, but didn't want to jump right to that. Is there any issue with populating the LRU with initially held buffers as such, or any particular reason LRU addition was deferred to I/O completion in the first place?

Quoted 6 lines

Yes, because then we have to deal with buffers that fail memory allocation, read IO or are allocated just to invalidate a range of disk blocks during a transaction (e.g. removing a remote attribute). There are probably other cases where we don't want to put buffers we allocate onto the LRU, but I can't think of any more right now.

Quoted 4 lines

I was thinking more along the lines of insertion on I/O submission rather than allocation. I.e., similar the proposed #1 below, but to actually insert to the list such that we never lose track of the buffer.

Why do we need a list?

It's a similar situation to inode_dio_wait(): we don't track direct IO because of the per-IO overhead of doing so, especially as we only need to implement a "drain barrier". i.e. it only needs a counter to implement the barrier needed for truncate/hole punch operations. That's exactly the same case here xfs_wait_buftarg() is a drain barrier; we could simply count every single IO in flight and wait for that to return to zero before walking the LRU, and it would solve the problem for everything, regardless of the type of buffer or whether it is in or going to be put in the LRU....

Also, remember that xfs_wait_buftarg() is run with the assumption that all high level processing has been stopped, and we are only waiting for remaining users to drop their locks and go away (e.g. complete async IO from AIL pushing). It's not a general use case we are working on here...

Quoted 14 lines

I guess my initial approach was to make the fix as isolated as possible because this code is hairy and it seems we have a lot of little hacks around to deal with other corner cases such as this. Surely this adds yet another one, but afaics, so does a counter mechanism. At the end of the day, the only real issue we know it fixes is this readahead corner case.

I think that if we're going to go as far as adding a list/counter mechanism, we should try to fix this in a way that is more broadly useful. By that I mean fix up the existing mechanism or add something that allows us to start unwinding some of the preexisting hacks such as cycling buffer locks to ensure I/O completion (perhaps alleviating the need to hold locks across I/O in the first place), etc.

Quoted 2 lines

Perhaps the counter approach opens the door for that, I have to think about it some more..

I don't think we can get rid of lock cycling - the counter can only exist within the IO context which is within the lock context. Hence to guarantee that all users of a buffer have finished, lock cycling is still necessary. i.e. lock cycling is a life cycle barrier, not an IO barrier...

Quoted 6 lines
Quoted 4 lines

We can do metadata buffer writes outside the superblock write protection context. A good example of that is log recovery on read-only filesystems....

This waits for I/O to complete so is unaffected by this problem.

Sure, but that's not what you were asking about. My point was that there is no specific high level superblock protection against async metadata IO being issued from non-VFS interfacing, internal filesystem subsystems.

Quoted 5 lines

Now
that I take a look at that, I'm not sure we'd be able to replace that with an I/O counter wait since technically that would wait for all in-flight I/O to complete. This context (xfs_buf_delwri_submit()) is only looking to wait for the locally submitted I/O to complete. Ugh.

I'm not advocating doing that at all.

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#6 lore raw
Quoted 19 lines · Dave Chinner
Quoted 11 lines · Brian Foster
Quoted 7 lines · Dave Chinner

On Thu, Jun 30, 2016 at 07:56:21PM -0400, Brian Foster wrote:
Note also that buffers that contain stale data (i.e. XBF_STALE) are not added to the LRU and xfs_buf_stale() removes buffers from the LRU, so shortcutting the above reclaim lifecycle in the case where a user invalidates the contents of the buffer. The end result is the same - there are no buffers with stale contents on the LRU.

Yes, but this seems arbitrary. The buffer may or may not be on the LRU when it is marked stale.

Yes, because (as I've already explained) we can mark buffers stale without needing to fill them with data. But whether or not the buffer is in the LRU when we mark it stale is irrelevant, because we only have a requirement that stale buffers are removed from the LRU so that they are torn down immediately on release.

Quoted 9 lines

This is actually very important because this is how we prevent duplicate buffers from entering the cache when we free metadata. Once we free a metadata buffer and finish committing the transaction, the buffer must not be kept in the cache because the extent it covers could be reallocated at any time, and in a different shape. It could even be user data. In all cases, the buffer needs to be removed from the cache immediately, and that's why xfs_buf_stale() behaves like this.

Yes, I understand. I think we're talking past eachother on this particular train of thought. Your original point was that the LRU is for "unused" buffers, and/or buffers aren't on the LRU until "their content is used." The example given was that stale buffers are immediately removed from the LRU and thus "buffers with stale data are never on the LRU."

My response to that is that's an implementation detail of XBF_STALE buffers and not necessarily true in other cases. We do actually end up with buffers with "stale" (as opposed to XBF_STALE, perhaps "invalid" is a better term) data in the event of I/O error. If a callsite allocates a buffer, never issues I/O on it (i.e., it is !XBF_DONE) and releases it, it ends up on the LRU. (I don't know that we do that anywhere given this is a filesystem after all ;), but that's the natural lifecycle in that particular case afaict).

So nothing about the LRU suggests to me that the contents must have valid data. AFAICT, it's primarily memory management mechanism to allow priority-ordered releasing of "unused" buffers, where an unused buffer is defined as a buffer without any active references, regardless of what the buffer contains.

I'm sympathetic to the argument that in the ideal case the LRU might not contain buffers actively in use, but IMO that's beyond the scope of the original patch. IOW, if we want to change that, we should approach it separately.

Quoted 20 lines
Quoted 14 lines
Quoted 8 lines

readahead is a special case - there is no accessor to say "cache this buffer for N references", but we have to keep it around for some time so that readahead is effective. We don't want to add it before we've done IO on it, and realistically we should only add it to the LRU if there was no IO error. We've had to work around bugs introduced by caching failed readahead buffers on the LRU in the past....

Ok, that makes sense. At least, I get the sense that you have a view of the LRU that is slightly different from what the code actually does (e.g., buffers w/ I/O errors on the LRU when they shouldn't be), which is why I'm trying to reconcile some of the reasoning here.

Quoted 5 lines

In other words, if the LRU was an unused only buffer list where used or stale buffers were pulled off and later reinserted/reprioritized (in terms of reclaim), then this would all make complete sense to me. In actuality, we have an LRU list that has buffers with I/O errors, buffers in use, buffers that might or might not be stale, etc.

Quoted 4 lines

Nothing is as simple as it seems - clean design never remains that
way once reality intrudes. However, the general rule is that buffers are not added to the LRU until the initial user has finished and release them. THis is done for many reasons.

Agreed on the first point. :) I think that's the only reason for the original patch to exist. ;) (As noted previously, I agree that it is a hack).

Quoted 3 lines

Firstly, We do not add them a lookup time because we do not know if the caller wants them to be added to the LRU.

We set b_lru_ref to 1 on buffer allocation. But it's true that we don't know if it's going to be marked stale before it is released, of course.

Quoted 6 lines

Secondly, we also don't add at lookup time because buffer lookup is the single hottest path in the XFS codebase. i.e. xfs_buf_find() and the struct xfs_buf are one of the few places where we've actually optimised the code to minimise cache footprint and CPU overhead.

Ok...

Quoted 9 lines

Currently, all of th einformation that a buffer lookup accesses is in the first cacheline of the struct xfs_buf, ensuring that we only take one cache miss penalty per buffer we walk through the rb-tree. If we now add a check to see if the buffer is on the LRU into the lookup path, we are - at minimum - adding an extra cacheline miss to the hot path. If we have to add the buffer to the LRU, then we're adding another couple of cacheline misses and another lock, all of which will have an impact on performance.

Performance is a fair point against adding all buffers to the LRU immediately. I take that to mean we'd have to test such a change with that in mind, but it sounds like this path is already optimized without any more room in the first cacheline, thus probably not likely to be an option.

I'm not sure this is as as critical applied solely to read ahead buffers, however...

Quoted 10 lines

THird, we add/remove buffers to/from the LRU lazily in paths that are not performance critical as a result. This is exactly the same algorithms the VFS uses for the inode and dentry cache LRUs, and we also use this for the XFS dquot LRU, too. The b_lru_ref count replaces the single referenced flag that the inode/dentry caches use so we can prioritise reclaim to match typical btree and directory access patterns (i.e. reclaim data before leaf before node before root) but otherwise the buffer LRU is no different to the VFS cache algorithms....

Quoted 15 lines

Finally, and perhaps the most important, is behaviour under low memory conditions. If we add the buffer to the LRU when we first look it up in the cache, the shrinker can now see it and decrement the b_lru_ref count while we are doing the first operation on that buffer. Say this transaction generates sufficient memory pressure that it causes the shrinker to completely decrement b_lru_ref - it will then remove the buffer from the LRU *before we've even finished with it*. This will cause the buffer to be freed immediately after the caller releases it (because b_lru_ref is zero), and so the next access requires the buffer to be read from disk again. What we end up with here is a situation where the buffer cache cannot maintian a working set of buffers between operations because the memory pressure within an operation causes the buffers to be removed from the LRU before the operation completes.

This makes sense, we probably don't want to subject active buffers to deprioritization via the shrinker before their initial use. It sounds like this could technically be a flaw for buffers once they're actually reused, as well. I could see an argument for reused buffers remaining on the LRU such that their b_lru_ref remains consistent (in terms of priority) with associated buffers (e.g., btree node vs. leaf priority ordering) in the event of shrinker activity, but it seems like there are other ways for that to get out of sync just the same. It could also be the case that such buffers tend to be "held" together.

Again, I'm not sure how important this is to readahead buffers since they are released on I/O completion. If we're waiting on the readahead before it completes then the buffer is not fully released, but that's a matter of timing more than anything. E.g., faster storage could disrupt that just as much as this particular change. Otherwise, this just means the readahead buffer could be freed before we try to use it, which is already the case today.

Quoted 26 lines

So, yeah, there's lots of "assumed knowledge" in why the buffer cache LRU is structured the way it is. That, unfortunately, is pretty much typical of all the linux slab caches that are controlled by shrinkers....

FWIW, readahead is often not complete before the followup read comes in, which means there's a caller blocked on the buffer with an active reference by the time IO completion runs xfs_buf_relse() to unlock the buffer. In these cases, the readahead buffer does not go onto the LRU until after the blocked caller has finished with the buffer and called xfs_buf_rele() itself (i.e. after the transaction subsystem is done with it).

Quoted 13 lines
Quoted 12 lines
Quoted 5 lines

The other thought I had was to change where buffers are added to the LRU altogether, but didn't want to jump right to that. Is there any issue with populating the LRU with initially held buffers as such, or any particular reason LRU addition was deferred to I/O completion in the first place?

Yes, because then we have to deal with buffers that fail memory allocation, read IO or are allocated just to invalidate a range of disk blocks during a transaction (e.g. removing a remote attribute). There are probably other cases where we don't want to put buffers we allocate onto the LRU, but I can't think of any more right now.

Quoted 7 lines
Quoted 4 lines

I was thinking more along the lines of insertion on I/O submission rather than allocation. I.e., similar the proposed #1 below, but to actually insert to the list such that we never lose track of the buffer.

Why do we need a list?

I was referring to the LRU.

Quoted 30 lines

It's a similar situation to inode_dio_wait(): we don't track direct IO because of the per-IO overhead of doing so, especially as we only need to implement a "drain barrier". i.e. it only needs a counter to implement the barrier needed for truncate/hole punch operations. That's exactly the same case here xfs_wait_buftarg() is a drain barrier; we could simply count every single IO in flight and wait for that to return to zero before walking the LRU, and it would solve the problem for everything, regardless of the type of buffer or whether it is in or going to be put in the LRU....

Also, remember that xfs_wait_buftarg() is run with the assumption that all high level processing has been stopped, and we are only waiting for remaining users to drop their locks and go away (e.g. complete async IO from AIL pushing). It's not a general use case we are working on here...

Quoted 14 lines

I guess my initial approach was to make the fix as isolated as possible because this code is hairy and it seems we have a lot of little hacks around to deal with other corner cases such as this. Surely this adds yet another one, but afaics, so does a counter mechanism. At the end of the day, the only real issue we know it fixes is this readahead corner case.

I think that if we're going to go as far as adding a list/counter mechanism, we should try to fix this in a way that is more broadly useful. By that I mean fix up the existing mechanism or add something that allows us to start unwinding some of the preexisting hacks such as cycling buffer locks to ensure I/O completion (perhaps alleviating the need to hold locks across I/O in the first place), etc.

Quoted 9 lines
Quoted 2 lines

Perhaps the counter approach opens the door for that, I have to think about it some more..

I don't think we can get rid of lock cycling - the counter can only exist within the IO context which is within the lock context. Hence to guarantee that all users of a buffer have finished, lock cycling is still necessary. i.e. lock cycling is a life cycle barrier, not an IO barrier...

Ok, but we apparently use it as such in places like xfs_buf_delwri_submit().

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

We can do metadata buffer writes outside the superblock write protection context. A good example of that is log recovery on read-only filesystems....

Quoted 7 lines
Quoted 1 line

This waits for I/O to complete so is unaffected by this problem.

Sure, but that's not what you were asking about. My point was that there is no specific high level superblock protection against async metadata IO being issued from non-VFS interfacing, internal filesystem subsystems.

Ok, I wasn't being clear... the sb protection was just an example. The fundamental question was whether we have to handle the write case due to not otherwise having some kind of synchronization or protection against tearing down infrastructure before I/Os complete. IOW, I don't care as much about the sb write protection specifically as whether we can identify any places where this is an actual, real problem that needs to be solved.

(Again, I'm not against stepping back and taking a larger look at the current buffer mechanisms to try and resolve such problems in theory, but I would prefer to do that separately from this fix).

Quoted 8 lines
Quoted 5 lines

Now
that I take a look at that, I'm not sure we'd be able to replace that with an I/O counter wait since technically that would wait for all in-flight I/O to complete. This context (xfs_buf_delwri_submit()) is only looking to wait for the locally submitted I/O to complete. Ugh.

I'm not advocating doing that at all.

I know, I was. I was hoping that if we go down the path of a more generic mechanism we might come up with something more useful beyond this particular readahead problem.

Brian

Quoted 11 lines

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

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

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

#7 lore raw
In reply to Dave Chinner
Quoted 93 lines · Dave Chinner
Quoted 54 lines · Brian Foster

Newly allocated XFS metadata buffers are added to the LRU once the hold count is released, which typically occurs after I/O completion. There is no other mechanism at current that tracks the existence or I/O state of a new buffer. Further, readahead I/O tends to be submitted asynchronously by nature, which means the I/O can remain in flight and actually complete long after the calling context is gone. This means that file descriptors or any other holds on the filesystem can be released, allowing the filesystem to be unmounted while I/O is still in flight. When I/O completion occurs, core data structures may have been freed, causing completion to run into invalid memory accesses and likely to panic.

This problem is reproduced on XFS via directory readahead. A filesystem is mounted, a directory is opened/closed and the filesystem immediately unmounted. The open/close cycle triggers a directory readahead that if delayed long enough, runs buffer I/O completion after the unmount has completed.

To work around this problem, add readahead buffers to the LRU earlier than other buffers (when the buffer is allocated, specifically). The buffer hold count will ultimately remain until I/O completion, which means any shrinker activity will skip the buffer until then. This makes the buffer visible to xfs_wait_buftarg(), however, which ensures that an unmount or quiesce waits for I/O completion appropriately.

---

This addresses the problem reproduced by the recently posted xfstests test:

http://thread.gmane.org/gmane.comp.file-systems.fstests/2740

This could probably be made more involved, i.e., to create another list of buffers in flight or some such. This seems more simple/sane to me, however, and survives my testing so far...

Brian

 fs/xfs/xfs_buf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_buf.c +4 -0
index 4665ff6..3f03df9 100644
@@ -590,8 +590,20 @@ xfs_buf_get_map(
 		return NULL;
 	}
 
+	/*
+	 * If the buffer found doesn't match the one allocated above, somebody
+	 * else beat us to insertion and we can toss the new one.
+	 *

fs/xfs/xfs_buf.c xfs_buf_get_map( · line 606

Quoted 10 lines
fs/xfs/xfs_buf.c +8 -0
+	 * If we did add the buffer and it happens to be readahead, add to the
+	 * LRU now rather than waiting until the hold is released. Otherwise,
+	 * the buffer is not visible to xfs_wait_buftarg() while in flight and
+	 * nothing else prevents an unmount before I/O completion.
+	 */
 	if (bp != new_bp)
 		xfs_buf_free(new_bp);
+	else if (flags & XBF_READ_AHEAD &&
+		 list_lru_add(&bp->b_target->bt_lru, &bp->b_lru))
+		atomic_inc(&bp->b_hold);

This doesn't sit right with me. The LRU is for "unused" objects, and readahead objects are not unused until IO completes and nobody is waiting on them.

As it is, this points out another problem with readahead buffers - they aren't actually being cached properly because b_lru_ref == 0, which means they are immediately reclaimed on IO completion rather than being added to the LRU....

I also think that it's not sufficient to cover the generic case of async IO that has no waiter. i.e. we could do get_buf, submit async write, drop submitter reference, and now we have the same problem but on a write. i.e. this problem is and async IO issue, not a readahead issue.

I think that it might be better to fix it by doing this:

1. ensure async IO submission always has b_lru_ref set, and if it isn't, set it to 1. This ensures the buffer will be added to the LRU on completion if it isn't already there.

2. keep a count of async buffer IO in progress. A per-cpu counter in the buftarg will be fine for this. Increment in xfs_buf_submit(), decrement in the xfs_buf_rele() call from xfs_buf_iodone() once we've determined if the buffer needs adding to the LRU or not.

Quoted 4 lines

3. make xfs_wait_buftarg() wait until the async IO count goes to zero before it gives up trying to release buffers on the LRU.

After playing with this a bit this afternoon, I don't think it is so straightforward to maintain consistency between xfs_buf_submit() and xfs_buf_rele(). Some buffers are actually never released (superblock, log buffers). Other buffers can actually be submitted for I/O multiple times before they are ultimately released (e.g., log recovery buffer read -> delwri submission).

I have a semi-functional patch that holds more of a pure I/O count, which means the count is decremented immediately in xfs_buf_ioend() rather than deferred to release. One downside is that while this technically still resolves the original problem, it's racy in that the count is dropped before the buffer is added to the LRU. This still works for the original problem because we also drain the ioend workqueue in xfs_wait_buftarg(), but it's not correct because we allow for non-deferred completion in the event of I/O errors (i.e., xfs_buf_ioend() called directly from xfs_buf_submit()).

Brian

Quoted 14 lines

That will ensure readahead buffers are cached, and we capture both async read and async write buffers in xfs_wait_buftarg().

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

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

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

#8 lore raw
Quoted 99 lines · Brian Foster
Quoted 93 lines · Dave Chinner
Quoted 54 lines · Brian Foster

Newly allocated XFS metadata buffers are added to the LRU once the hold count is released, which typically occurs after I/O completion. There is no other mechanism at current that tracks the existence or I/O state of a new buffer. Further, readahead I/O tends to be submitted asynchronously by nature, which means the I/O can remain in flight and actually complete long after the calling context is gone. This means that file descriptors or any other holds on the filesystem can be released, allowing the filesystem to be unmounted while I/O is still in flight. When I/O completion occurs, core data structures may have been freed, causing completion to run into invalid memory accesses and likely to panic.

This problem is reproduced on XFS via directory readahead. A filesystem is mounted, a directory is opened/closed and the filesystem immediately unmounted. The open/close cycle triggers a directory readahead that if delayed long enough, runs buffer I/O completion after the unmount has completed.

To work around this problem, add readahead buffers to the LRU earlier than other buffers (when the buffer is allocated, specifically). The buffer hold count will ultimately remain until I/O completion, which means any shrinker activity will skip the buffer until then. This makes the buffer visible to xfs_wait_buftarg(), however, which ensures that an unmount or quiesce waits for I/O completion appropriately.

---

This addresses the problem reproduced by the recently posted xfstests test:

http://thread.gmane.org/gmane.comp.file-systems.fstests/2740

This could probably be made more involved, i.e., to create another list of buffers in flight or some such. This seems more simple/sane to me, however, and survives my testing so far...

Brian

 fs/xfs/xfs_buf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_buf.c +4 -0
index 4665ff6..3f03df9 100644
@@ -590,8 +590,20 @@ xfs_buf_get_map(
 		return NULL;
 	}
 
+	/*
+	 * If the buffer found doesn't match the one allocated above, somebody
+	 * else beat us to insertion and we can toss the new one.
+	 *

fs/xfs/xfs_buf.c xfs_buf_get_map( · line 606

Quoted 10 lines
fs/xfs/xfs_buf.c +8 -0
+	 * If we did add the buffer and it happens to be readahead, add to the
+	 * LRU now rather than waiting until the hold is released. Otherwise,
+	 * the buffer is not visible to xfs_wait_buftarg() while in flight and
+	 * nothing else prevents an unmount before I/O completion.
+	 */
 	if (bp != new_bp)
 		xfs_buf_free(new_bp);
+	else if (flags & XBF_READ_AHEAD &&
+		 list_lru_add(&bp->b_target->bt_lru, &bp->b_lru))
+		atomic_inc(&bp->b_hold);

This doesn't sit right with me. The LRU is for "unused" objects, and readahead objects are not unused until IO completes and nobody is waiting on them.

As it is, this points out another problem with readahead buffers - they aren't actually being cached properly because b_lru_ref == 0, which means they are immediately reclaimed on IO completion rather than being added to the LRU....

I also think that it's not sufficient to cover the generic case of async IO that has no waiter. i.e. we could do get_buf, submit async write, drop submitter reference, and now we have the same problem but on a write. i.e. this problem is and async IO issue, not a readahead issue.

I think that it might be better to fix it by doing this:

1. ensure async IO submission always has b_lru_ref set, and if it isn't, set it to 1. This ensures the buffer will be added to the LRU on completion if it isn't already there.

2. keep a count of async buffer IO in progress. A per-cpu counter in the buftarg will be fine for this. Increment in xfs_buf_submit(), decrement in the xfs_buf_rele() call from xfs_buf_iodone() once we've determined if the buffer needs adding to the LRU or not.

Quoted 4 lines

3. make xfs_wait_buftarg() wait until the async IO count goes to zero before it gives up trying to release buffers on the LRU.

Quoted 7 lines

After playing with this a bit this afternoon, I don't think it is so straightforward to maintain consistency between xfs_buf_submit() and xfs_buf_rele(). Some buffers are actually never released (superblock, log buffers). Other buffers can actually be submitted for I/O multiple times before they are ultimately released (e.g., log recovery buffer read -> delwri submission).

I think I can get around these problems by skipping all uncached I/O and maintaining a per-buffer I/O count that is sunk into the global buftarg count once the buffer is released. E.g., something like the following patch. Not fully tested, but works on some quick tests...

fs/xfs/xfs_buf.c +42 -1
index 4665ff6..8a04b66 100644
@@ -177,6 +177,7 @@ _xfs_buf_alloc(
 	XB_SET_OWNER(bp);
 	bp->b_target = target;
 	bp->b_flags = flags;
+	atomic_set(&bp->b_io_count, 0);
 
 	/*
 	 * Set length and io_length to the same value initially.
@@ -865,6 +866,21 @@ xfs_buf_hold(
 	atomic_inc(&bp->b_hold);
 }
 
+static inline void
+xfs_buf_rele_iocount(
+	struct xfs_buf	*bp)
+{
+	int		val;
+
+	val = atomic_read(&bp->b_io_count);
+	if (!val)
+		return;
+
+	atomic_sub(val, &bp->b_io_count);
+	percpu_counter_add(&bp->b_target->bt_io_count, -val);
+	wake_up(&bp->b_target->bt_io_wait);
+}
+
 /*
  *	Releases a hold on the specified buffer.  If the
  *	the hold count is 1, calls xfs_buf_free.
@@ -880,8 +896,10 @@ xfs_buf_rele(
 	if (!pag) {
 		ASSERT(list_empty(&bp->b_lru));
 		ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
-		if (atomic_dec_and_test(&bp->b_hold))
+		if (atomic_dec_and_test(&bp->b_hold)) {
+			xfs_buf_rele_iocount(bp);
 			xfs_buf_free(bp);
+		}
 		return;
 	}
 
@@ -890,6 +908,9 @@ xfs_buf_rele(
 	ASSERT(atomic_read(&bp->b_hold) > 0);
 	if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
 		spin_lock(&bp->b_lock);
+
+		xfs_buf_rele_iocount(bp);
+
 		if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
 			/*
 			 * If the buffer is added to the LRU take a new
@@ -1277,6 +1298,18 @@ _xfs_buf_ioapply(
 	rw |= REQ_META;
 
 	/*
+	 * XXX: uncached check indirectly filters out the sb buffer and log
+	 * buffers (possibly others?), that are held and never released to the
+	 * LRU
+	 */
+	if (bp->b_flags & XBF_ASYNC &&
+	    bp->b_bn != XFS_BUF_DADDR_NULL &&	/* uncached */
+	    atomic_read(&bp->b_lru_ref) && list_empty(&bp->b_lru)) {
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+		atomic_inc(&bp->b_io_count);
+	}
+
+	/*
 	 * Walk all the vectors issuing IO on them. Set up the initial offset
 	 * into the buffer and the desired IO size before we start -
 	 * _xfs_buf_ioapply_vec() will modify them appropriately for each
@@ -1533,6 +1566,8 @@ xfs_wait_buftarg(
 	 * ensure here that all reference counts have been dropped before we
 	 * start walking the LRU list.
 	 */
+	wait_event(btp->bt_io_wait,
+		   (percpu_counter_sum(&btp->bt_io_count) == 0));
 	drain_workqueue(btp->bt_mount->m_buf_workqueue);
 
 	/* loop until there is nothing left on the lru list. */
@@ -1629,6 +1664,8 @@ xfs_free_buftarg(
 	struct xfs_buftarg	*btp)
 {
 	unregister_shrinker(&btp->bt_shrinker);
+	ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
+	percpu_counter_destroy(&btp->bt_io_count);
 	list_lru_destroy(&btp->bt_lru);
 
 	if (mp->m_flags & XFS_MOUNT_BARRIER)
@@ -1693,6 +1730,10 @@ xfs_alloc_buftarg(
 	if (list_lru_init(&btp->bt_lru))
 		goto error;
 
+	if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
+		goto error;
+	init_waitqueue_head(&btp->bt_io_wait);
+
 	btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
 	btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
 	btp->bt_shrinker.seeks = DEFAULT_SEEKS;
fs/xfs/xfs_buf.h +5 -0
index 8bfb974..2518d09 100644
@@ -115,6 +115,10 @@ typedef struct xfs_buftarg {
 	/* LRU control structures */
 	struct shrinker		bt_shrinker;
 	struct list_lru		bt_lru;
+
+	/* XXX: would atomic_t suffice? */
+	struct percpu_counter	bt_io_count;
+	wait_queue_head_t	bt_io_wait;
 } xfs_buftarg_t;
 
 struct xfs_buf;
@@ -183,6 +187,7 @@ typedef struct xfs_buf {
 	unsigned int		b_page_count;	/* size of page array */
 	unsigned int		b_offset;	/* page offset in first page */
 	int			b_error;	/* error code on I/O */
+	atomic_t		b_io_count;
 
 	/*
 	 * async write failure retry count. Initialised to zero on the first
Quoted 27 lines

I have a semi-functional patch that holds more of a pure I/O count, which means the count is decremented immediately in xfs_buf_ioend() rather than deferred to release. One downside is that while this technically still resolves the original problem, it's racy in that the count is dropped before the buffer is added to the LRU. This still works for the original problem because we also drain the ioend workqueue in xfs_wait_buftarg(), but it's not correct because we allow for non-deferred completion in the event of I/O errors (i.e., xfs_buf_ioend() called directly from xfs_buf_submit()).

Brian

Quoted 14 lines

That will ensure readahead buffers are cached, and we capture both async read and async write buffers in xfs_wait_buftarg().

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

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

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

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

#9 lore raw
Quoted 112 lines · Brian Foster
Quoted 99 lines · Brian Foster
Quoted 93 lines · Dave Chinner
Quoted 54 lines · Brian Foster

Newly allocated XFS metadata buffers are added to the LRU once the hold count is released, which typically occurs after I/O completion. There is no other mechanism at current that tracks the existence or I/O state of a new buffer. Further, readahead I/O tends to be submitted asynchronously by nature, which means the I/O can remain in flight and actually complete long after the calling context is gone. This means that file descriptors or any other holds on the filesystem can be released, allowing the filesystem to be unmounted while I/O is still in flight. When I/O completion occurs, core data structures may have been freed, causing completion to run into invalid memory accesses and likely to panic.

This problem is reproduced on XFS via directory readahead. A filesystem is mounted, a directory is opened/closed and the filesystem immediately unmounted. The open/close cycle triggers a directory readahead that if delayed long enough, runs buffer I/O completion after the unmount has completed.

To work around this problem, add readahead buffers to the LRU earlier than other buffers (when the buffer is allocated, specifically). The buffer hold count will ultimately remain until I/O completion, which means any shrinker activity will skip the buffer until then. This makes the buffer visible to xfs_wait_buftarg(), however, which ensures that an unmount or quiesce waits for I/O completion appropriately.

---

This addresses the problem reproduced by the recently posted xfstests test:

http://thread.gmane.org/gmane.comp.file-systems.fstests/2740

This could probably be made more involved, i.e., to create another list of buffers in flight or some such. This seems more simple/sane to me, however, and survives my testing so far...

Brian

 fs/xfs/xfs_buf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_buf.c +4 -0
index 4665ff6..3f03df9 100644
@@ -590,8 +590,20 @@ xfs_buf_get_map(
 		return NULL;
 	}
 
+	/*
+	 * If the buffer found doesn't match the one allocated above, somebody
+	 * else beat us to insertion and we can toss the new one.
+	 *

fs/xfs/xfs_buf.c xfs_buf_get_map( · line 606

Quoted 10 lines
fs/xfs/xfs_buf.c +8 -0
+	 * If we did add the buffer and it happens to be readahead, add to the
+	 * LRU now rather than waiting until the hold is released. Otherwise,
+	 * the buffer is not visible to xfs_wait_buftarg() while in flight and
+	 * nothing else prevents an unmount before I/O completion.
+	 */
 	if (bp != new_bp)
 		xfs_buf_free(new_bp);
+	else if (flags & XBF_READ_AHEAD &&
+		 list_lru_add(&bp->b_target->bt_lru, &bp->b_lru))
+		atomic_inc(&bp->b_hold);

This doesn't sit right with me. The LRU is for "unused" objects, and readahead objects are not unused until IO completes and nobody is waiting on them.

As it is, this points out another problem with readahead buffers - they aren't actually being cached properly because b_lru_ref == 0, which means they are immediately reclaimed on IO completion rather than being added to the LRU....

I also think that it's not sufficient to cover the generic case of async IO that has no waiter. i.e. we could do get_buf, submit async write, drop submitter reference, and now we have the same problem but on a write. i.e. this problem is and async IO issue, not a readahead issue.

I think that it might be better to fix it by doing this:

1. ensure async IO submission always has b_lru_ref set, and if it isn't, set it to 1. This ensures the buffer will be added to the LRU on completion if it isn't already there.

2. keep a count of async buffer IO in progress. A per-cpu counter in the buftarg will be fine for this. Increment in xfs_buf_submit(), decrement in the xfs_buf_rele() call from xfs_buf_iodone() once we've determined if the buffer needs adding to the LRU or not.

Quoted 4 lines

3. make xfs_wait_buftarg() wait until the async IO count goes to zero before it gives up trying to release buffers on the LRU.

Quoted 7 lines

After playing with this a bit this afternoon, I don't think it is so straightforward to maintain consistency between xfs_buf_submit() and xfs_buf_rele(). Some buffers are actually never released (superblock, log buffers). Other buffers can actually be submitted for I/O multiple times before they are ultimately released (e.g., log recovery buffer read -> delwri submission).

I think I can get around these problems by skipping all uncached I/O and maintaining a per-buffer I/O count that is sunk into the global buftarg count once the buffer is released. E.g., something like the following patch. Not fully tested, but works on some quick tests...

....

Quoted 44 lines
fs/xfs/xfs_buf.c +21 -1
+static inline void
+xfs_buf_rele_iocount(
+	struct xfs_buf	*bp)
+{
+	int		val;
+
+	val = atomic_read(&bp->b_io_count);
+	if (!val)
+		return;
+
+	atomic_sub(val, &bp->b_io_count);
+	percpu_counter_add(&bp->b_target->bt_io_count, -val);
+	wake_up(&bp->b_target->bt_io_wait);
+}
+
 /*
  *	Releases a hold on the specified buffer.  If the
  *	the hold count is 1, calls xfs_buf_free.
@@ -880,8 +896,10 @@ xfs_buf_rele(
 	if (!pag) {
 		ASSERT(list_empty(&bp->b_lru));
 		ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
-		if (atomic_dec_and_test(&bp->b_hold))
+		if (atomic_dec_and_test(&bp->b_hold)) {
+			xfs_buf_rele_iocount(bp);
 			xfs_buf_free(bp);
+		}
 		return;
 	}
 
@@ -890,6 +908,9 @@ xfs_buf_rele(
 	ASSERT(atomic_read(&bp->b_hold) > 0);
 	if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
 		spin_lock(&bp->b_lock);
+
+		xfs_buf_rele_iocount(bp);
+
 		if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
 			/*
 			 * If the buffer is added to the LRU take a new
@@ -1277,6 +1298,18 @@ _xfs_buf_ioapply(
 	rw |= REQ_META;
 
 	/*

fs/xfs/xfs_buf.c _xfs_buf_ioapply( · line 1310

Quoted 10 lines
fs/xfs/xfs_buf.c +10 -0
+	 * XXX: uncached check indirectly filters out the sb buffer and log
+	 * buffers (possibly others?), that are held and never released to the
+	 * LRU
+	 */
+	if (bp->b_flags & XBF_ASYNC &&
+	    bp->b_bn != XFS_BUF_DADDR_NULL &&	/* uncached */
+	    atomic_read(&bp->b_lru_ref) && list_empty(&bp->b_lru)) {
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+		atomic_inc(&bp->b_io_count);
+	}

This seems rather specific and potentially fragile. Why not just count /all/ IO using the per-cpu counter? If you're worried about overhead, just use a large custom batch value so they are almost never folded back into the global count and so the vast majority of counter updates are single unlocked CPU instructions.

Counting everything is far better than needing to run through 4 branch tests on every buffer we submit IO on. i.e. counting everything with an appropriately configured percpu counter is likely to have a lower fast path overhead testing every buffer to capture only the specific case in question...

fs/xfs/xfs_buf.c xfs_wait_buftarg( · line 1571

Quoted 7 lines
fs/xfs/xfs_buf.c +2 -0
@@ -1533,6 +1566,8 @@ xfs_wait_buftarg(
 	 * ensure here that all reference counts have been dropped before we
 	 * start walking the LRU list.
 	 */
+	wait_event(btp->bt_io_wait,
+		   (percpu_counter_sum(&btp->bt_io_count) == 0));
 	drain_workqueue(btp->bt_mount->m_buf_workqueue);

I also don't think that per-buffer IO accounting is justified for this. This:

	while(percpu_counter_sum(&btp->bt_io_count))
		delay(100);

Will do the same thing without adding an atomic counter inc/dec to every buffer we want to capture here.

The waiting should probably also happen after we drain all the workqueues, because the bt_io_count won't drop to zero until we process all the pending IO completions on the work queues and release the buffers...

fs/xfs/xfs_buf.h typedef struct xfs_buftarg { · line 120

Quoted 2 lines
fs/xfs/xfs_buf.h +2 -0
+	/* XXX: would atomic_t suffice? */
+	struct percpu_counter	bt_io_count;

Too much cacheline contention in large machines to use an atomic_t as a global counter.

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#10 lore raw
Quoted 114 lines · Dave Chinner
Quoted 112 lines · Brian Foster
Quoted 99 lines · Brian Foster
Quoted 93 lines · Dave Chinner
Quoted 54 lines · Brian Foster

Newly allocated XFS metadata buffers are added to the LRU once the hold count is released, which typically occurs after I/O completion. There is no other mechanism at current that tracks the existence or I/O state of a new buffer. Further, readahead I/O tends to be submitted asynchronously by nature, which means the I/O can remain in flight and actually complete long after the calling context is gone. This means that file descriptors or any other holds on the filesystem can be released, allowing the filesystem to be unmounted while I/O is still in flight. When I/O completion occurs, core data structures may have been freed, causing completion to run into invalid memory accesses and likely to panic.

This problem is reproduced on XFS via directory readahead. A filesystem is mounted, a directory is opened/closed and the filesystem immediately unmounted. The open/close cycle triggers a directory readahead that if delayed long enough, runs buffer I/O completion after the unmount has completed.

To work around this problem, add readahead buffers to the LRU earlier than other buffers (when the buffer is allocated, specifically). The buffer hold count will ultimately remain until I/O completion, which means any shrinker activity will skip the buffer until then. This makes the buffer visible to xfs_wait_buftarg(), however, which ensures that an unmount or quiesce waits for I/O completion appropriately.

---

This addresses the problem reproduced by the recently posted xfstests test:

http://thread.gmane.org/gmane.comp.file-systems.fstests/2740

This could probably be made more involved, i.e., to create another list of buffers in flight or some such. This seems more simple/sane to me, however, and survives my testing so far...

Brian

 fs/xfs/xfs_buf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_buf.c +4 -0
index 4665ff6..3f03df9 100644
@@ -590,8 +590,20 @@ xfs_buf_get_map(
 		return NULL;
 	}
 
+	/*
+	 * If the buffer found doesn't match the one allocated above, somebody
+	 * else beat us to insertion and we can toss the new one.
+	 *

fs/xfs/xfs_buf.c xfs_buf_get_map( · line 606

Quoted 10 lines
fs/xfs/xfs_buf.c +8 -0
+	 * If we did add the buffer and it happens to be readahead, add to the
+	 * LRU now rather than waiting until the hold is released. Otherwise,
+	 * the buffer is not visible to xfs_wait_buftarg() while in flight and
+	 * nothing else prevents an unmount before I/O completion.
+	 */
 	if (bp != new_bp)
 		xfs_buf_free(new_bp);
+	else if (flags & XBF_READ_AHEAD &&
+		 list_lru_add(&bp->b_target->bt_lru, &bp->b_lru))
+		atomic_inc(&bp->b_hold);

This doesn't sit right with me. The LRU is for "unused" objects, and readahead objects are not unused until IO completes and nobody is waiting on them.

As it is, this points out another problem with readahead buffers - they aren't actually being cached properly because b_lru_ref == 0, which means they are immediately reclaimed on IO completion rather than being added to the LRU....

I also think that it's not sufficient to cover the generic case of async IO that has no waiter. i.e. we could do get_buf, submit async write, drop submitter reference, and now we have the same problem but on a write. i.e. this problem is and async IO issue, not a readahead issue.

I think that it might be better to fix it by doing this:

1. ensure async IO submission always has b_lru_ref set, and if it isn't, set it to 1. This ensures the buffer will be added to the LRU on completion if it isn't already there.

2. keep a count of async buffer IO in progress. A per-cpu counter in the buftarg will be fine for this. Increment in xfs_buf_submit(), decrement in the xfs_buf_rele() call from xfs_buf_iodone() once we've determined if the buffer needs adding to the LRU or not.

Quoted 4 lines

3. make xfs_wait_buftarg() wait until the async IO count goes to zero before it gives up trying to release buffers on the LRU.

Quoted 7 lines

After playing with this a bit this afternoon, I don't think it is so straightforward to maintain consistency between xfs_buf_submit() and xfs_buf_rele(). Some buffers are actually never released (superblock, log buffers). Other buffers can actually be submitted for I/O multiple times before they are ultimately released (e.g., log recovery buffer read -> delwri submission).

I think I can get around these problems by skipping all uncached I/O and maintaining a per-buffer I/O count that is sunk into the global buftarg count once the buffer is released. E.g., something like the following patch. Not fully tested, but works on some quick tests...

....

...

Quoted 12 lines
Quoted 12 lines
fs/xfs/xfs_buf.c +8 -0
@@ -1277,6 +1298,18 @@ _xfs_buf_ioapply(
 	rw |= REQ_META;
 
 	/*
+	 * XXX: uncached check indirectly filters out the sb buffer and log
+	 * buffers (possibly others?), that are held and never released to the
+	 * LRU
+	 */
+	if (bp->b_flags & XBF_ASYNC &&
+	    bp->b_bn != XFS_BUF_DADDR_NULL &&	/* uncached */
+	    atomic_read(&bp->b_lru_ref) && list_empty(&bp->b_lru)) {
+		percpu_counter_inc(&bp->b_target->bt_io_count);
Quoted 9 lines

fs/xfs/xfs_buf.c _xfs_buf_ioapply( · line 1310

Quoted 2 lines
fs/xfs/xfs_buf.c +2 -0
+		atomic_inc(&bp->b_io_count);
+	}

This seems rather specific and potentially fragile. Why not just count /all/ IO using the per-cpu counter? If you're worried about overhead, just use a large custom batch value so they are almost never folded back into the global count and so the vast majority of counter updates are single unlocked CPU instructions.

I'm not really worried about overhead (yet). I'm just trying to come up with a relatively straightforward scheme that solves the problem. The extra checks here are basically the cost of deferring the I/O count decrement to buffer release.

Quoted 6 lines

Counting everything is far better than needing to run through 4 branch tests on every buffer we submit IO on. i.e. counting everything with an appropriately configured percpu counter is likely to have a lower fast path overhead testing every buffer to capture only the specific case in question...

See my immediately previous reply to this thread. I hadn't posted code, but the initial variant of this patch uses more of a pure I/O count. The primary drawback with that is that it is racy in that the I/O count is decremented before the buffer is added to the LRU. The workqueue drain helps prevent the original problem, but at the same time I'm not sure it makes this approach any more robust than the original one-liner patch.

I also defined a new buffer flag in that version to help simplify dealing with variable behavior between error cases and iodone handlers and whatnot. That trickiness might be avoided by pushing the counter further down to more of a bio count (e.g., a global variant of bp>b_io_remaining count), but I haven't experimented with that as of yet.

Quoted 11 lines

fs/xfs/xfs_buf.c xfs_wait_buftarg( · line 1571

Quoted 7 lines
fs/xfs/xfs_buf.c +2 -0
@@ -1533,6 +1566,8 @@ xfs_wait_buftarg(
 	 * ensure here that all reference counts have been dropped before we
 	 * start walking the LRU list.
 	 */
+	wait_event(btp->bt_io_wait,
+		   (percpu_counter_sum(&btp->bt_io_count) == 0));
 	drain_workqueue(btp->bt_mount->m_buf_workqueue);

I also don't think that per-buffer IO accounting is justified for this. This:

Quoted 6 lines
	while(percpu_counter_sum(&btp->bt_io_count))
		delay(100);

Will do the same thing without adding an atomic counter inc/dec to every buffer we want to capture here.

The purpose of the per-buffer I/O accounting is not to drive the synchronization mechanism. The purpose is to cover the cases where buffers are submitted for I/O multiple times before they are released (also mentioned in my immediately previous reply). Otherwise, the counter gets out of whack and never returns to 0.

This is required for an implementation that defers decrementing the I/O count to buffer release time (when the buffer is added to the LRU). I actually think this patch as posted doesn't quite order things correctly in that regard, but the broader point holds I think. This additional accounting isn't required for a pure I/O counter (inc on submit, dec on complete).

Quoted 5 lines

The waiting should probably also happen after we drain all the workqueues, because the bt_io_count won't drop to zero until we process all the pending IO completions on the work queues and release the buffers...

I'm not sure it matters much for this implementation. I figured it more natural to wait for pending I/O followed by completions. Note that this order might be more critical with a pure I/O count approach because the drain_workqueue() is what ends up serializing the addition to the LRU, and that only happens if we wait for the I/O to complete first.

But I'll look at this more closely once we've established a direction...

Quoted 6 lines

fs/xfs/xfs_buf.h typedef struct xfs_buftarg { · line 120

Quoted 2 lines
fs/xfs/xfs_buf.h +2 -0
+	/* XXX: would atomic_t suffice? */
+	struct percpu_counter	bt_io_count;

Too much cacheline contention in large machines to use an atomic_t as a global counter.

Ok.

So what is your preference out of the possible approaches here? AFAICS, we have the following options:

1.) The original "add readahead to LRU early" approach.
Pros: simple one-liner
Cons: bit of a hack, only covers readahead scenario
2.) Defer I/O count decrement to buffer release (this patch).
Pros: should cover all cases (reads/writes)
Cons: more complex (requires per-buffer accounting, etc.)
3.) Raw (buffer or bio?) I/O count (no defer to buffer release)
Pros: eliminates some complexity from #2
Cons: still more complex than #1, racy in that decrement does not serialize against LRU addition (requires drain_workqueue(), which still doesn't cover error conditions)

As noted above, option #3 also allows for either a buffer based count or bio based count, the latter of which might simplify things a bit further (TBD). Thoughts?

Brian

Quoted 11 lines

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

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

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

#11 lore raw
Quoted 101 lines · Brian Foster
Quoted 100 lines · Dave Chinner
Quoted 99 lines · Brian Foster
Quoted 98 lines · Brian Foster
Quoted 97 lines · Dave Chinner
Quoted 54 lines · Brian Foster

Newly allocated XFS metadata buffers are added to the LRU once the hold count is released, which typically occurs after I/O completion. There is no other mechanism at current that tracks the existence or I/O state of a new buffer. Further, readahead I/O tends to be submitted asynchronously by nature, which means the I/O can remain in flight and actually complete long after the calling context is gone. This means that file descriptors or any other holds on the filesystem can be released, allowing the filesystem to be unmounted while I/O is still in flight. When I/O completion occurs, core data structures may have been freed, causing completion to run into invalid memory accesses and likely to panic.

This problem is reproduced on XFS via directory readahead. A filesystem is mounted, a directory is opened/closed and the filesystem immediately unmounted. The open/close cycle triggers a directory readahead that if delayed long enough, runs buffer I/O completion after the unmount has completed.

To work around this problem, add readahead buffers to the LRU earlier than other buffers (when the buffer is allocated, specifically). The buffer hold count will ultimately remain until I/O completion, which means any shrinker activity will skip the buffer until then. This makes the buffer visible to xfs_wait_buftarg(), however, which ensures that an unmount or quiesce waits for I/O completion appropriately.

---

This addresses the problem reproduced by the recently posted xfstests test:

http://thread.gmane.org/gmane.comp.file-systems.fstests/2740

This could probably be made more involved, i.e., to create another list of buffers in flight or some such. This seems more simple/sane to me, however, and survives my testing so far...

Brian

 fs/xfs/xfs_buf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_buf.c +4 -0
index 4665ff6..3f03df9 100644
@@ -590,8 +590,20 @@ xfs_buf_get_map(
 		return NULL;
 	}
 
+	/*
+	 * If the buffer found doesn't match the one allocated above, somebody
+	 * else beat us to insertion and we can toss the new one.
+	 *

fs/xfs/xfs_buf.c xfs_buf_get_map( · line 606

Quoted 10 lines
fs/xfs/xfs_buf.c +8 -0
+	 * If we did add the buffer and it happens to be readahead, add to the
+	 * LRU now rather than waiting until the hold is released. Otherwise,
+	 * the buffer is not visible to xfs_wait_buftarg() while in flight and
+	 * nothing else prevents an unmount before I/O completion.
+	 */
 	if (bp != new_bp)
 		xfs_buf_free(new_bp);
+	else if (flags & XBF_READ_AHEAD &&
+		 list_lru_add(&bp->b_target->bt_lru, &bp->b_lru))
+		atomic_inc(&bp->b_hold);

This doesn't sit right with me. The LRU is for "unused" objects, and readahead objects are not unused until IO completes and nobody is waiting on them.

As it is, this points out another problem with readahead buffers - they aren't actually being cached properly because b_lru_ref == 0, which means they are immediately reclaimed on IO completion rather than being added to the LRU....

I also think that it's not sufficient to cover the generic case of async IO that has no waiter. i.e. we could do get_buf, submit async write, drop submitter reference, and now we have the same problem but on a write. i.e. this problem is and async IO issue, not a readahead issue.

I think that it might be better to fix it by doing this:

1. ensure async IO submission always has b_lru_ref set, and if it isn't, set it to 1. This ensures the buffer will be added to the LRU on completion if it isn't already there.

2. keep a count of async buffer IO in progress. A per-cpu counter in the buftarg will be fine for this. Increment in xfs_buf_submit(), decrement in the xfs_buf_rele() call from xfs_buf_iodone() once we've determined if the buffer needs adding to the LRU or not.

3. make xfs_wait_buftarg() wait until the async IO count goes to zero before it gives up trying to release buffers on the LRU.

...

Quoted 15 lines

So what is your preference out of the possible approaches here? AFAICS, we have the following options:

1.) The original "add readahead to LRU early" approach.
Pros: simple one-liner
Cons: bit of a hack, only covers readahead scenario
2.) Defer I/O count decrement to buffer release (this patch).
Pros: should cover all cases (reads/writes)
Cons: more complex (requires per-buffer accounting, etc.)
3.) Raw (buffer or bio?) I/O count (no defer to buffer release)
Pros: eliminates some complexity from #2
Cons: still more complex than #1, racy in that decrement does not serialize against LRU addition (requires drain_workqueue(), which still doesn't cover error conditions)

Quoted 4 lines

As noted above, option #3 also allows for either a buffer based count or bio based count, the latter of which might simplify things a bit further (TBD). Thoughts?

FWIW, the following is a slightly cleaned up version of my initial approach (option #3 above). Note that the flag is used to help deal with varying ioend behavior. E.g., xfs_buf_ioend() is called once for some buffers, multiple times for others with an iodone callback, that behavior changes in some cases when an error is set, etc. (I'll add comments before an official post.)

Brian

fs/xfs/xfs_buf.c +16 -1
index 4665ff6..45d3ddd 100644
@@ -1018,7 +1018,10 @@ xfs_buf_ioend(
 
 	trace_xfs_buf_iodone(bp, _RET_IP_);
 
-	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
+	if (bp->b_flags & XBF_IN_FLIGHT)
+		percpu_counter_dec(&bp->b_target->bt_io_count);
+
+	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD | XBF_IN_FLIGHT);
 
 	/*
 	 * Pull in IO completion errors now. We are guaranteed to be running
@@ -1341,6 +1344,11 @@ xfs_buf_submit(
 	 * xfs_buf_ioend too early.
 	 */
 	atomic_set(&bp->b_io_remaining, 1);
+	if (bp->b_flags & XBF_ASYNC) {
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+		bp->b_flags |= XBF_IN_FLIGHT;
+	}
+
 	_xfs_buf_ioapply(bp);
 
 	/*
@@ -1533,6 +1541,8 @@ xfs_wait_buftarg(
 	 * ensure here that all reference counts have been dropped before we
 	 * start walking the LRU list.
 	 */
+	while (percpu_counter_sum(&btp->bt_io_count))
+		delay(100);
 	drain_workqueue(btp->bt_mount->m_buf_workqueue);
 
 	/* loop until there is nothing left on the lru list. */
@@ -1629,6 +1639,8 @@ xfs_free_buftarg(
 	struct xfs_buftarg	*btp)
 {
 	unregister_shrinker(&btp->bt_shrinker);
+	ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
+	percpu_counter_destroy(&btp->bt_io_count);
 	list_lru_destroy(&btp->bt_lru);
 
 	if (mp->m_flags & XFS_MOUNT_BARRIER)
@@ -1693,6 +1705,9 @@ xfs_alloc_buftarg(
 	if (list_lru_init(&btp->bt_lru))
 		goto error;
 
+	if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
+		goto error;
+
 	btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
 	btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
 	btp->bt_shrinker.seeks = DEFAULT_SEEKS;
fs/xfs/xfs_buf.h +3 -0
index 8bfb974..e1f95e0 100644
@@ -43,6 +43,7 @@ typedef enum {
 #define XBF_READ	 (1 << 0) /* buffer intended for reading from device */
 #define XBF_WRITE	 (1 << 1) /* buffer intended for writing to device */
 #define XBF_READ_AHEAD	 (1 << 2) /* asynchronous read-ahead */
+#define XBF_IN_FLIGHT	 (1 << 3)
 #define XBF_ASYNC	 (1 << 4) /* initiator will not wait for completion */
 #define XBF_DONE	 (1 << 5) /* all pages in the buffer uptodate */
 #define XBF_STALE	 (1 << 6) /* buffer has been staled, do not find it */
@@ -115,6 +116,8 @@ typedef struct xfs_buftarg {
 	/* LRU control structures */
 	struct shrinker		bt_shrinker;
 	struct list_lru		bt_lru;
+
+	struct percpu_counter	bt_io_count;
 } xfs_buftarg_t;
 
 struct xfs_buf;
Quoted 14 lines

Brian

Quoted 11 lines

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

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

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

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

#12 lore raw
Quoted 20 lines · Brian Foster

On Mon, Jul 11, 2016 at 09:52:52AM -0400, Brian Foster wrote:
...

Quoted 18 lines

So what is your preference out of the possible approaches here? AFAICS, we have the following options:

1.) The original "add readahead to LRU early" approach.
Pros: simple one-liner
Cons: bit of a hack, only covers readahead scenario
2.) Defer I/O count decrement to buffer release (this patch).
Pros: should cover all cases (reads/writes)
Cons: more complex (requires per-buffer accounting, etc.)
3.) Raw (buffer or bio?) I/O count (no defer to buffer release)
Pros: eliminates some complexity from #2
Cons: still more complex than #1, racy in that decrement does not serialize against LRU addition (requires drain_workqueue(), which still doesn't cover error conditions)

As noted above, option #3 also allows for either a buffer based count or bio based count, the latter of which might simplify things a bit further (TBD). Thoughts?

Pretty good summary :P

Quoted 6 lines

FWIW, the following is a slightly cleaned up version of my initial approach (option #3 above). Note that the flag is used to help deal with varying ioend behavior. E.g., xfs_buf_ioend() is called once for some buffers, multiple times for others with an iodone callback, that behavior changes in some cases when an error is set, etc. (I'll add comments before an official post.)

The approach looks good - I think there's a couple of things we can do to clean it up and make it robust. Comments inline.

Quoted 6 lines
fs/xfs/xfs_buf.c +0 -0
index 4665ff6..45d3ddd 100644
@@ -1018,7 +1018,10 @@ xfs_buf_ioend(
 

fs/xfs/xfs_buf.c xfs_buf_ioend( · line 1027

Quoted 10 lines
fs/xfs/xfs_buf.c +4 -1
 	trace_xfs_buf_iodone(bp, _RET_IP_);
 
-	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
+	if (bp->b_flags & XBF_IN_FLIGHT)
+		percpu_counter_dec(&bp->b_target->bt_io_count);
+
+	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD | XBF_IN_FLIGHT);
 
 	/*
 	 * Pull in IO completion errors now. We are guaranteed to be running

I think the XBF_IN_FLIGHT can be moved to the final xfs_buf_rele() processing if:

fs/xfs/xfs_buf.c xfs_buf_submit( · line 1350

Quoted 8 lines
fs/xfs/xfs_buf.c +4 -0
@@ -1341,6 +1344,11 @@ xfs_buf_submit(
 	 * xfs_buf_ioend too early.
 	 */
 	atomic_set(&bp->b_io_remaining, 1);
+	if (bp->b_flags & XBF_ASYNC) {
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+		bp->b_flags |= XBF_IN_FLIGHT;
+	}

You change this to:

	if (!(bp->b_flags & XBF_IN_FLIGHT)) {
		percpu_counter_inc(&bp->b_target->bt_io_count);
		bp->b_flags |= XBF_IN_FLIGHT;
	}

We shouldn't have to check for XBF_ASYNC in xfs_buf_submit() - it is the path taken for async IO submission, so we should probably ASSERT(bp->b_flags & XBF_ASYNC) in this function to ensure that is the case.

[Thinking aloud - __test_and_set_bit() might make this code a bit cleaner]

fs/xfs/xfs_buf.h typedef enum { · line 46

Quoted 9 lines
fs/xfs/xfs_buf.h +1 -0
index 8bfb974..e1f95e0 100644
@@ -43,6 +43,7 @@ typedef enum {
 #define XBF_READ	 (1 << 0) /* buffer intended for reading from device */
 #define XBF_WRITE	 (1 << 1) /* buffer intended for writing to device */
 #define XBF_READ_AHEAD	 (1 << 2) /* asynchronous read-ahead */
+#define XBF_IN_FLIGHT	 (1 << 3)

Hmmm - it's an internal flag, so probably should be prefixed with an "_" and moved down to the section with _XBF_KMEM and friends.

Thoughts?

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#13 lore raw
Quoted 65 lines · Dave Chinner
Quoted 20 lines · Brian Foster

On Mon, Jul 11, 2016 at 09:52:52AM -0400, Brian Foster wrote:
...

Quoted 18 lines

So what is your preference out of the possible approaches here? AFAICS, we have the following options:

1.) The original "add readahead to LRU early" approach.
Pros: simple one-liner
Cons: bit of a hack, only covers readahead scenario
2.) Defer I/O count decrement to buffer release (this patch).
Pros: should cover all cases (reads/writes)
Cons: more complex (requires per-buffer accounting, etc.)
3.) Raw (buffer or bio?) I/O count (no defer to buffer release)
Pros: eliminates some complexity from #2
Cons: still more complex than #1, racy in that decrement does not serialize against LRU addition (requires drain_workqueue(), which still doesn't cover error conditions)

As noted above, option #3 also allows for either a buffer based count or bio based count, the latter of which might simplify things a bit further (TBD). Thoughts?

Pretty good summary :P

Quoted 6 lines

FWIW, the following is a slightly cleaned up version of my initial approach (option #3 above). Note that the flag is used to help deal with varying ioend behavior. E.g., xfs_buf_ioend() is called once for some buffers, multiple times for others with an iodone callback, that behavior changes in some cases when an error is set, etc. (I'll add comments before an official post.)

The approach looks good - I think there's a couple of things we can do to clean it up and make it robust. Comments inline.

Quoted 6 lines
fs/xfs/xfs_buf.c +0 -0
index 4665ff6..45d3ddd 100644
@@ -1018,7 +1018,10 @@ xfs_buf_ioend(
 

fs/xfs/xfs_buf.c xfs_buf_ioend( · line 1027

Quoted 10 lines
fs/xfs/xfs_buf.c +4 -1
 	trace_xfs_buf_iodone(bp, _RET_IP_);
 
-	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
+	if (bp->b_flags & XBF_IN_FLIGHT)
+		percpu_counter_dec(&bp->b_target->bt_io_count);
+
+	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD | XBF_IN_FLIGHT);
 
 	/*
 	 * Pull in IO completion errors now. We are guaranteed to be running

I think the XBF_IN_FLIGHT can be moved to the final xfs_buf_rele() processing if:

fs/xfs/xfs_buf.c xfs_buf_submit( · line 1350

Quoted 8 lines
fs/xfs/xfs_buf.c +4 -0
@@ -1341,6 +1344,11 @@ xfs_buf_submit(
 	 * xfs_buf_ioend too early.
 	 */
 	atomic_set(&bp->b_io_remaining, 1);
+	if (bp->b_flags & XBF_ASYNC) {
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+		bp->b_flags |= XBF_IN_FLIGHT;
+	}

You change this to:

Quoted 5 lines
	if (!(bp->b_flags & XBF_IN_FLIGHT)) {
		percpu_counter_inc(&bp->b_target->bt_io_count);
		bp->b_flags |= XBF_IN_FLIGHT;
	}

Ok, so use the flag to cap the I/O count and defer the decrement to release. I think that should work and addresses the raciness issue. I'll give it a try.

Quoted 5 lines

We shouldn't have to check for XBF_ASYNC in xfs_buf_submit() - it is the path taken for async IO submission, so we should probably ASSERT(bp->b_flags & XBF_ASYNC) in this function to ensure that is the case.

Yeah, that's unnecessary. There's already such an assert in xfs_buf_submit(), actually.

Quoted 3 lines

[Thinking aloud - __test_and_set_bit() might make this code a bit cleaner]

On a quick try, this complains about b_flags being an unsigned int. I think I'll leave the set bit as is and use a helper for the release, which also provides a location to explain how the count works.

Quoted 4 lines
Quoted 4 lines
fs/xfs/xfs_buf.h +0 -0
index 8bfb974..e1f95e0 100644
Quoted 9 lines

fs/xfs/xfs_buf.h typedef enum { · line 46

Quoted 5 lines
fs/xfs/xfs_buf.h +1 -0
@@ -43,6 +43,7 @@ typedef enum {
 #define XBF_READ	 (1 << 0) /* buffer intended for reading from device */
 #define XBF_WRITE	 (1 << 1) /* buffer intended for writing to device */
 #define XBF_READ_AHEAD	 (1 << 2) /* asynchronous read-ahead */
+#define XBF_IN_FLIGHT	 (1 << 3)

Hmmm - it's an internal flag, so probably should be prefixed with an "_" and moved down to the section with _XBF_KMEM and friends.

Indeed, thanks.

Brian

Quoted 13 lines

Thoughts?

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

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

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

#14 lore raw
Quoted 72 lines · Brian Foster
Quoted 65 lines · Dave Chinner
Quoted 20 lines · Brian Foster

On Mon, Jul 11, 2016 at 09:52:52AM -0400, Brian Foster wrote:
...

Quoted 18 lines

So what is your preference out of the possible approaches here? AFAICS, we have the following options:

1.) The original "add readahead to LRU early" approach.
Pros: simple one-liner
Cons: bit of a hack, only covers readahead scenario
2.) Defer I/O count decrement to buffer release (this patch).
Pros: should cover all cases (reads/writes)
Cons: more complex (requires per-buffer accounting, etc.)
3.) Raw (buffer or bio?) I/O count (no defer to buffer release)
Pros: eliminates some complexity from #2
Cons: still more complex than #1, racy in that decrement does not serialize against LRU addition (requires drain_workqueue(), which still doesn't cover error conditions)

As noted above, option #3 also allows for either a buffer based count or bio based count, the latter of which might simplify things a bit further (TBD). Thoughts?

Pretty good summary :P

Quoted 6 lines

FWIW, the following is a slightly cleaned up version of my initial approach (option #3 above). Note that the flag is used to help deal with varying ioend behavior. E.g., xfs_buf_ioend() is called once for some buffers, multiple times for others with an iodone callback, that behavior changes in some cases when an error is set, etc. (I'll add comments before an official post.)

The approach looks good - I think there's a couple of things we can do to clean it up and make it robust. Comments inline.

Quoted 6 lines
fs/xfs/xfs_buf.c +0 -0
index 4665ff6..45d3ddd 100644
@@ -1018,7 +1018,10 @@ xfs_buf_ioend(
 

fs/xfs/xfs_buf.c xfs_buf_ioend( · line 1027

Quoted 10 lines
fs/xfs/xfs_buf.c +4 -1
 	trace_xfs_buf_iodone(bp, _RET_IP_);
 
-	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
+	if (bp->b_flags & XBF_IN_FLIGHT)
+		percpu_counter_dec(&bp->b_target->bt_io_count);
+
+	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD | XBF_IN_FLIGHT);
 
 	/*
 	 * Pull in IO completion errors now. We are guaranteed to be running

I think the XBF_IN_FLIGHT can be moved to the final xfs_buf_rele() processing if:

fs/xfs/xfs_buf.c xfs_buf_submit( · line 1350

Quoted 8 lines
fs/xfs/xfs_buf.c +4 -0
@@ -1341,6 +1344,11 @@ xfs_buf_submit(
 	 * xfs_buf_ioend too early.
 	 */
 	atomic_set(&bp->b_io_remaining, 1);
+	if (bp->b_flags & XBF_ASYNC) {
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+		bp->b_flags |= XBF_IN_FLIGHT;
+	}

You change this to:

Quoted 5 lines
	if (!(bp->b_flags & XBF_IN_FLIGHT)) {
		percpu_counter_inc(&bp->b_target->bt_io_count);
		bp->b_flags |= XBF_IN_FLIGHT;
	}
Quoted 4 lines

Ok, so use the flag to cap the I/O count and defer the decrement to release. I think that should work and addresses the raciness issue. I'll give it a try.

This appears to be doable, but it reintroduces some ugliness from the previous approach. For example, we have to start filtering out uncached buffers again (if we defer the decrement to release, we must handle never-released buffers one way or another). Also, given the feedback on the previous patch with regard to filtering out non-new buffers from the I/O count, I've dropped that and replaced it with updates to xfs_buf_rele() to decrement when the buffer is returned to the LRU (we either have to filter out buffers already on the LRU at submit time or make sure that they are decremented when released back to the LRU).

Code follows...

Brian

fs/xfs/xfs_buf.c +71 -14
index 4665ff6..b7afbac 100644
@@ -80,6 +80,25 @@ xfs_buf_vmap_len(
 }
 
 /*
+ * Clear the in-flight state on a buffer about to be released to the LRU or
+ * freed and unaccount from the buftarg. The buftarg I/O count maintains a count
+ * of held buffers that have undergone at least one I/O in the current hold
+ * cycle (e.g., not a total I/O count). This provides protection against unmount
+ * for buffer I/O completion (see xfs_wait_buftarg()) processing.
+ */
+static inline void
+xfs_buf_rele_in_flight(
+	struct xfs_buf	*bp)
+{
+	if (!(bp->b_flags & _XBF_IN_FLIGHT))
+		return;
+
+	ASSERT(bp->b_flags & XBF_ASYNC);
+	bp->b_flags &= ~_XBF_IN_FLIGHT;
+	percpu_counter_dec(&bp->b_target->bt_io_count);
+}
+
+/*
  * When we mark a buffer stale, we remove the buffer from the LRU and clear the
  * b_lru_ref count so that the buffer is freed immediately when the buffer
  * reference count falls to zero. If the buffer is already on the LRU, we need
@@ -866,30 +885,37 @@ xfs_buf_hold(
 }
 
 /*
- *	Releases a hold on the specified buffer.  If the
- *	the hold count is 1, calls xfs_buf_free.
+ * Release a hold on the specified buffer. If the hold count is 1, the buffer is
+ * placed on LRU or freed (depending on b_lru_ref).
  */
 void
 xfs_buf_rele(
 	xfs_buf_t		*bp)
 {
 	struct xfs_perag	*pag = bp->b_pag;
+	bool			release;
+	bool			freebuf = false;
 
 	trace_xfs_buf_rele(bp, _RET_IP_);
 
 	if (!pag) {
 		ASSERT(list_empty(&bp->b_lru));
 		ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
-		if (atomic_dec_and_test(&bp->b_hold))
+		if (atomic_dec_and_test(&bp->b_hold)) {
+			xfs_buf_rele_in_flight(bp);
 			xfs_buf_free(bp);
+		}
 		return;
 	}
 
 	ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
 
 	ASSERT(atomic_read(&bp->b_hold) > 0);
-	if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
-		spin_lock(&bp->b_lock);
+
+	release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
+	spin_lock(&bp->b_lock);
+	if (release) {
+		xfs_buf_rele_in_flight(bp);
 		if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
 			/*
 			 * If the buffer is added to the LRU take a new
@@ -900,7 +926,6 @@ xfs_buf_rele(
 				bp->b_state &= ~XFS_BSTATE_DISPOSE;
 				atomic_inc(&bp->b_hold);
 			}
-			spin_unlock(&bp->b_lock);
 			spin_unlock(&pag->pag_buf_lock);
 		} else {
 			/*
@@ -914,15 +939,24 @@ xfs_buf_rele(
 			} else {
 				ASSERT(list_empty(&bp->b_lru));
 			}
-			spin_unlock(&bp->b_lock);
 
 			ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
 			rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
 			spin_unlock(&pag->pag_buf_lock);
 			xfs_perag_put(pag);
-			xfs_buf_free(bp);
+			freebuf = true;
 		}
+	} else if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru)) {
+		/*
+		 * The buffer is already on the LRU and it holds the only
+		 * reference. Drop the in flight state.
+		 */
+		xfs_buf_rele_in_flight(bp);
 	}
+	spin_unlock(&bp->b_lock);
+
+	if (freebuf)
+		xfs_buf_free(bp);
 }
 
 
@@ -1341,6 +1375,18 @@ xfs_buf_submit(
 	 * xfs_buf_ioend too early.
 	 */
 	atomic_set(&bp->b_io_remaining, 1);
+
+	/*
+	 * Bump the I/O in flight count on the buftarg if we haven't yet done
+	 * so for this buffer. Skip uncached buffers because many of those
+	 * (e.g., superblock, log buffers) are never released.
+	 */
+	if ((bp->b_bn != XFS_BUF_DADDR_NULL) &&
+	    !(bp->b_flags & _XBF_IN_FLIGHT)) {
+		bp->b_flags |= _XBF_IN_FLIGHT;
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+	}
+
 	_xfs_buf_ioapply(bp);
 
 	/*
@@ -1526,13 +1572,19 @@ xfs_wait_buftarg(
 	int loop = 0;
 
 	/*
-	 * We need to flush the buffer workqueue to ensure that all IO
-	 * completion processing is 100% done. Just waiting on buffer locks is
-	 * not sufficient for async IO as the reference count held over IO is
-	 * not released until after the buffer lock is dropped. Hence we need to
-	 * ensure here that all reference counts have been dropped before we
-	 * start walking the LRU list.
+	 * First wait on the buftarg I/O count for all in-flight buffers to be
+	 * released. This is critical as new buffers do not make the LRU until
+	 * they are released.
+	 *
+	 * Next, flush the buffer workqueue to ensure all completion processing
+	 * has finished. Just waiting on buffer locks is not sufficient for
+	 * async IO as the reference count held over IO is not released until
+	 * after the buffer lock is dropped. Hence we need to ensure here that
+	 * all reference counts have been dropped before we start walking the
+	 * LRU list.
 	 */
+	while (percpu_counter_sum(&btp->bt_io_count))
+		delay(100);
 	drain_workqueue(btp->bt_mount->m_buf_workqueue);
 
 	/* loop until there is nothing left on the lru list. */
@@ -1629,6 +1681,8 @@ xfs_free_buftarg(
 	struct xfs_buftarg	*btp)
 {
 	unregister_shrinker(&btp->bt_shrinker);
+	ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
+	percpu_counter_destroy(&btp->bt_io_count);
 	list_lru_destroy(&btp->bt_lru);
 
 	if (mp->m_flags & XFS_MOUNT_BARRIER)
@@ -1693,6 +1747,9 @@ xfs_alloc_buftarg(
 	if (list_lru_init(&btp->bt_lru))
 		goto error;
 
+	if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
+		goto error;
+
 	btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
 	btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
 	btp->bt_shrinker.seeks = DEFAULT_SEEKS;
fs/xfs/xfs_buf.h +3 -0
index 8bfb974..19f70e2 100644
@@ -62,6 +62,7 @@ typedef enum {
 #define _XBF_KMEM	 (1 << 21)/* backed by heap memory */
 #define _XBF_DELWRI_Q	 (1 << 22)/* buffer on a delwri queue */
 #define _XBF_COMPOUND	 (1 << 23)/* compound buffer */
+#define _XBF_IN_FLIGHT	 (1 << 25) /* I/O in flight, for accounting purposes */
 
 typedef unsigned int xfs_buf_flags_t;
 
@@ -115,6 +116,8 @@ typedef struct xfs_buftarg {
 	/* LRU control structures */
 	struct shrinker		bt_shrinker;
 	struct list_lru		bt_lru;
+
+	struct percpu_counter	bt_io_count;
 } xfs_buftarg_t;
 
 struct xfs_buf;
Quoted 49 lines
Quoted 5 lines

We shouldn't have to check for XBF_ASYNC in xfs_buf_submit() - it is the path taken for async IO submission, so we should probably ASSERT(bp->b_flags & XBF_ASYNC) in this function to ensure that is the case.

Yeah, that's unnecessary. There's already such an assert in xfs_buf_submit(), actually.

Quoted 3 lines

[Thinking aloud - __test_and_set_bit() might make this code a bit cleaner]

On a quick try, this complains about b_flags being an unsigned int. I think I'll leave the set bit as is and use a helper for the release, which also provides a location to explain how the count works.

Quoted 4 lines
Quoted 4 lines
fs/xfs/xfs_buf.h +0 -0
index 8bfb974..e1f95e0 100644
Quoted 9 lines

fs/xfs/xfs_buf.h typedef enum { · line 46

Quoted 5 lines
fs/xfs/xfs_buf.h +1 -0
@@ -43,6 +43,7 @@ typedef enum {
 #define XBF_READ	 (1 << 0) /* buffer intended for reading from device */
 #define XBF_WRITE	 (1 << 1) /* buffer intended for writing to device */
 #define XBF_READ_AHEAD	 (1 << 2) /* asynchronous read-ahead */
+#define XBF_IN_FLIGHT	 (1 << 3)

Hmmm - it's an internal flag, so probably should be prefixed with an "_" and moved down to the section with _XBF_KMEM and friends.

Indeed, thanks.

Brian

Quoted 13 lines

Thoughts?

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

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

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

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

#15 lore raw
Quoted 73 lines · Brian Foster
Quoted 72 lines · Brian Foster
Quoted 70 lines · Dave Chinner
Quoted 20 lines · Brian Foster

On Mon, Jul 11, 2016 at 09:52:52AM -0400, Brian Foster wrote:
...

Quoted 18 lines

So what is your preference out of the possible approaches here? AFAICS, we have the following options:

1.) The original "add readahead to LRU early" approach.
Pros: simple one-liner
Cons: bit of a hack, only covers readahead scenario
2.) Defer I/O count decrement to buffer release (this patch).
Pros: should cover all cases (reads/writes)
Cons: more complex (requires per-buffer accounting, etc.)
3.) Raw (buffer or bio?) I/O count (no defer to buffer release)
Pros: eliminates some complexity from #2
Cons: still more complex than #1, racy in that decrement does not serialize against LRU addition (requires drain_workqueue(), which still doesn't cover error conditions)

As noted above, option #3 also allows for either a buffer based count or bio based count, the latter of which might simplify things a bit further (TBD). Thoughts?

Pretty good summary :P

Quoted 6 lines

FWIW, the following is a slightly cleaned up version of my initial approach (option #3 above). Note that the flag is used to help deal with varying ioend behavior. E.g., xfs_buf_ioend() is called once for some buffers, multiple times for others with an iodone callback, that behavior changes in some cases when an error is set, etc. (I'll add comments before an official post.)

The approach looks good - I think there's a couple of things we can do to clean it up and make it robust. Comments inline.

Quoted 6 lines
fs/xfs/xfs_buf.c +0 -0
index 4665ff6..45d3ddd 100644
@@ -1018,7 +1018,10 @@ xfs_buf_ioend(
 

fs/xfs/xfs_buf.c xfs_buf_ioend( · line 1027

Quoted 10 lines
fs/xfs/xfs_buf.c +4 -1
 	trace_xfs_buf_iodone(bp, _RET_IP_);
 
-	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
+	if (bp->b_flags & XBF_IN_FLIGHT)
+		percpu_counter_dec(&bp->b_target->bt_io_count);
+
+	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD | XBF_IN_FLIGHT);
 
 	/*
 	 * Pull in IO completion errors now. We are guaranteed to be running

I think the XBF_IN_FLIGHT can be moved to the final xfs_buf_rele() processing if:

fs/xfs/xfs_buf.c xfs_buf_submit( · line 1350

Quoted 8 lines
fs/xfs/xfs_buf.c +4 -0
@@ -1341,6 +1344,11 @@ xfs_buf_submit(
 	 * xfs_buf_ioend too early.
 	 */
 	atomic_set(&bp->b_io_remaining, 1);
+	if (bp->b_flags & XBF_ASYNC) {
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+		bp->b_flags |= XBF_IN_FLIGHT;
+	}

You change this to:

	if (!(bp->b_flags & XBF_IN_FLIGHT)) {
		percpu_counter_inc(&bp->b_target->bt_io_count);
		bp->b_flags |= XBF_IN_FLIGHT;
	}
Quoted 7 lines
Quoted 4 lines

Ok, so use the flag to cap the I/O count and defer the decrement to release. I think that should work and addresses the raciness issue. I'll give it a try.

This appears to be doable, but it reintroduces some ugliness from the previous approach.

Ah, so it does. Bugger.

Quoted 3 lines

For example, we have to start filtering out uncached buffers again (if we defer the decrement to release, we must handle never-released buffers one way or another).

So the problem is limited to the superblock buffer and the iclog buffers, right? How about making that special case explicit via a flag set on the buffer? e.g. XBF_NO_IOCOUNT. THat way the exceptions are clearly spelt out, rather than avoiding all uncached buffers?

Quoted 18 lines

Also, given the feedback on
the previous patch with regard to filtering out non-new buffers from the I/O count, I've dropped that and replaced it with updates to xfs_buf_rele() to decrement when the buffer is returned to the LRU (we either have to filter out buffers already on the LRU at submit time or make sure that they are decremented when released back to the LRU).

Code follows...

Brian

fs/xfs/xfs_buf.c +0 -0
index 4665ff6..b7afbac 100644
@@ -80,6 +80,25 @@ xfs_buf_vmap_len(
 }
 

fs/xfs/xfs_buf.c xfs_buf_vmap_len( · line 91

Quoted 10 lines
fs/xfs/xfs_buf.c +9 -0
 /*
+ * Clear the in-flight state on a buffer about to be released to the LRU or
+ * freed and unaccount from the buftarg. The buftarg I/O count maintains a count
+ * of held buffers that have undergone at least one I/O in the current hold
+ * cycle (e.g., not a total I/O count). This provides protection against unmount
+ * for buffer I/O completion (see xfs_wait_buftarg()) processing.
+ */
+static inline void
+xfs_buf_rele_in_flight(
+	struct xfs_buf	*bp)

Not sure about the name: xfs_buf_ioacct_dec()?

Quoted 74 lines
fs/xfs/xfs_buf.c +22 -7
+{
+	if (!(bp->b_flags & _XBF_IN_FLIGHT))
+		return;
+
+	ASSERT(bp->b_flags & XBF_ASYNC);
+	bp->b_flags &= ~_XBF_IN_FLIGHT;
+	percpu_counter_dec(&bp->b_target->bt_io_count);
+}
+
+/*
  * When we mark a buffer stale, we remove the buffer from the LRU and clear the
  * b_lru_ref count so that the buffer is freed immediately when the buffer
  * reference count falls to zero. If the buffer is already on the LRU, we need
@@ -866,30 +885,37 @@ xfs_buf_hold(
 }
 
 /*
- *	Releases a hold on the specified buffer.  If the
- *	the hold count is 1, calls xfs_buf_free.
+ * Release a hold on the specified buffer. If the hold count is 1, the buffer is
+ * placed on LRU or freed (depending on b_lru_ref).
  */
 void
 xfs_buf_rele(
 	xfs_buf_t		*bp)
 {
 	struct xfs_perag	*pag = bp->b_pag;
+	bool			release;
+	bool			freebuf = false;
 
 	trace_xfs_buf_rele(bp, _RET_IP_);
 
 	if (!pag) {
 		ASSERT(list_empty(&bp->b_lru));
 		ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
-		if (atomic_dec_and_test(&bp->b_hold))
+		if (atomic_dec_and_test(&bp->b_hold)) {
+			xfs_buf_rele_in_flight(bp);
 			xfs_buf_free(bp);
+		}
 		return;
 	}
 
 	ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
 
 	ASSERT(atomic_read(&bp->b_hold) > 0);
-	if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
-		spin_lock(&bp->b_lock);
+
+	release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
+	spin_lock(&bp->b_lock);
+	if (release) {
+		xfs_buf_rele_in_flight(bp);
 		if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
 			/*
 			 * If the buffer is added to the LRU take a new
@@ -900,7 +926,6 @@ xfs_buf_rele(
 				bp->b_state &= ~XFS_BSTATE_DISPOSE;
 				atomic_inc(&bp->b_hold);
 			}
-			spin_unlock(&bp->b_lock);
 			spin_unlock(&pag->pag_buf_lock);
 		} else {
 			/*
@@ -914,15 +939,24 @@ xfs_buf_rele(
 			} else {
 				ASSERT(list_empty(&bp->b_lru));
 			}
-			spin_unlock(&bp->b_lock);
 
 			ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
 			rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
 			spin_unlock(&pag->pag_buf_lock);
 			xfs_perag_put(pag);

fs/xfs/xfs_buf.c xfs_buf_rele( · line 955

Quoted 10 lines
fs/xfs/xfs_buf.c +7 -1
-			xfs_buf_free(bp);
+			freebuf = true;
 		}
+	} else if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru)) {
+		/*
+		 * The buffer is already on the LRU and it holds the only
+		 * reference. Drop the in flight state.
+		 */
+		xfs_buf_rele_in_flight(bp);
 	}

This b_hold check is racy - bp->b_lock is not enough to stabilise the b_hold count. Because we don't hold the buffer semaphore any more, another buffer reference holder can successfully run the above atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock). New references can be taken in xfs_buf_find() so the count could go up, but I think that's fine given the eventual case we care about here is draining references on unmount.

I think this is still ok for draining references, too, because of the flag check inside xfs_buf_rele_in_flight(). If we race on a transition a value of 1, then we end running the branch in each caller. If we race on transition to zero, then the caller that is releasing the buffer will execute xfs_buf_rele_in_flight() and all will be well.

Needs comments, and maybe restructing the code to handle the xfs_buf_rele_in_flight() call up front so it's clear that io accounting is clearly a separate case from the rest of release handling. e.g.

	release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
	spin_lock(&bp->b_lock);
	if (!release) {
		if (!(atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
			xfs_buf_ioacct_dec(bp);
		goto out_unlock;
	}
	xfs_buf_ioacct_dec(bp);

	/* rest of release code, one level of indentation removed */

out_unlock:

	spin_unlock(&bp->b_lock);

	if (freebuf)
		xfs_buf_free(bp);
Quoted 5 lines
fs/xfs/xfs_buf.c +1 -0
@@ -1341,6 +1375,18 @@ xfs_buf_submit(
 	 * xfs_buf_ioend too early.
 	 */
 	atomic_set(&bp->b_io_remaining, 1);
+

fs/xfs/xfs_buf.c xfs_buf_submit( · line 1388

Quoted 10 lines
fs/xfs/xfs_buf.c +10 -0
+	/*
+	 * Bump the I/O in flight count on the buftarg if we haven't yet done
+	 * so for this buffer. Skip uncached buffers because many of those
+	 * (e.g., superblock, log buffers) are never released.
+	 */
+	if ((bp->b_bn != XFS_BUF_DADDR_NULL) &&
+	    !(bp->b_flags & _XBF_IN_FLIGHT)) {
+		bp->b_flags |= _XBF_IN_FLIGHT;
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+	}

xfs_buf_ioacct_inc()

{
	if (bp->b_flags & (XBF_NO_IOACCT | _XBF_IN_FLIGHT))
		return;
	percpu_counter_inc(&bp->b_target->bt_io_count);
	bp->b_flags |= _XBF_IN_FLIGHT;
}

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#16 lore raw
Quoted 84 lines · Dave Chinner
Quoted 73 lines · Brian Foster
Quoted 72 lines · Brian Foster
Quoted 70 lines · Dave Chinner
Quoted 20 lines · Brian Foster

On Mon, Jul 11, 2016 at 09:52:52AM -0400, Brian Foster wrote:
...

Quoted 18 lines

So what is your preference out of the possible approaches here? AFAICS, we have the following options:

1.) The original "add readahead to LRU early" approach.
Pros: simple one-liner
Cons: bit of a hack, only covers readahead scenario
2.) Defer I/O count decrement to buffer release (this patch).
Pros: should cover all cases (reads/writes)
Cons: more complex (requires per-buffer accounting, etc.)
3.) Raw (buffer or bio?) I/O count (no defer to buffer release)
Pros: eliminates some complexity from #2
Cons: still more complex than #1, racy in that decrement does not serialize against LRU addition (requires drain_workqueue(), which still doesn't cover error conditions)

As noted above, option #3 also allows for either a buffer based count or bio based count, the latter of which might simplify things a bit further (TBD). Thoughts?

Pretty good summary :P

Quoted 6 lines

FWIW, the following is a slightly cleaned up version of my initial approach (option #3 above). Note that the flag is used to help deal with varying ioend behavior. E.g., xfs_buf_ioend() is called once for some buffers, multiple times for others with an iodone callback, that behavior changes in some cases when an error is set, etc. (I'll add comments before an official post.)

The approach looks good - I think there's a couple of things we can do to clean it up and make it robust. Comments inline.

Quoted 6 lines
fs/xfs/xfs_buf.c +0 -0
index 4665ff6..45d3ddd 100644
@@ -1018,7 +1018,10 @@ xfs_buf_ioend(
 

fs/xfs/xfs_buf.c xfs_buf_ioend( · line 1027

Quoted 10 lines
fs/xfs/xfs_buf.c +4 -1
 	trace_xfs_buf_iodone(bp, _RET_IP_);
 
-	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
+	if (bp->b_flags & XBF_IN_FLIGHT)
+		percpu_counter_dec(&bp->b_target->bt_io_count);
+
+	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD | XBF_IN_FLIGHT);
 
 	/*
 	 * Pull in IO completion errors now. We are guaranteed to be running

I think the XBF_IN_FLIGHT can be moved to the final xfs_buf_rele() processing if:

fs/xfs/xfs_buf.c xfs_buf_submit( · line 1350

Quoted 8 lines
fs/xfs/xfs_buf.c +4 -0
@@ -1341,6 +1344,11 @@ xfs_buf_submit(
 	 * xfs_buf_ioend too early.
 	 */
 	atomic_set(&bp->b_io_remaining, 1);
+	if (bp->b_flags & XBF_ASYNC) {
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+		bp->b_flags |= XBF_IN_FLIGHT;
+	}

You change this to:

	if (!(bp->b_flags & XBF_IN_FLIGHT)) {
		percpu_counter_inc(&bp->b_target->bt_io_count);
		bp->b_flags |= XBF_IN_FLIGHT;
	}
Quoted 7 lines
Quoted 4 lines

Ok, so use the flag to cap the I/O count and defer the decrement to release. I think that should work and addresses the raciness issue. I'll give it a try.

This appears to be doable, but it reintroduces some ugliness from the previous approach.

Ah, so it does. Bugger.

Quoted 9 lines
Quoted 3 lines

For example, we have to start filtering out uncached buffers again (if we defer the decrement to release, we must handle never-released buffers one way or another).

So the problem is limited to the superblock buffer and the iclog buffers, right? How about making that special case explicit via a flag set on the buffer? e.g. XBF_NO_IOCOUNT. THat way the exceptions are clearly spelt out, rather than avoiding all uncached buffers?

I think so. I considered a similar approach earlier, but I didn't want to spend time tracking down the associated users until the broader approach was nailed down. More specifically, I think we could set b_lru_ref to 0 on those buffers and use that to bypass the accounting. That makes it clear that these buffers are not destined for the LRU and alternative synchronization is required (which already exists in the form of lock cycles).

The rest of the feedback makes sense, so I'll incorporate that and give the above a try... thanks.

Brian

Quoted 179 lines
Quoted 18 lines

Also, given the feedback on
the previous patch with regard to filtering out non-new buffers from the I/O count, I've dropped that and replaced it with updates to xfs_buf_rele() to decrement when the buffer is returned to the LRU (we either have to filter out buffers already on the LRU at submit time or make sure that they are decremented when released back to the LRU).

Code follows...

Brian

fs/xfs/xfs_buf.c +0 -0
index 4665ff6..b7afbac 100644
@@ -80,6 +80,25 @@ xfs_buf_vmap_len(
 }
 

fs/xfs/xfs_buf.c xfs_buf_vmap_len( · line 91

Quoted 10 lines
fs/xfs/xfs_buf.c +9 -0
 /*
+ * Clear the in-flight state on a buffer about to be released to the LRU or
+ * freed and unaccount from the buftarg. The buftarg I/O count maintains a count
+ * of held buffers that have undergone at least one I/O in the current hold
+ * cycle (e.g., not a total I/O count). This provides protection against unmount
+ * for buffer I/O completion (see xfs_wait_buftarg()) processing.
+ */
+static inline void
+xfs_buf_rele_in_flight(
+	struct xfs_buf	*bp)

Not sure about the name: xfs_buf_ioacct_dec()?

Quoted 74 lines
fs/xfs/xfs_buf.c +22 -7
+{
+	if (!(bp->b_flags & _XBF_IN_FLIGHT))
+		return;
+
+	ASSERT(bp->b_flags & XBF_ASYNC);
+	bp->b_flags &= ~_XBF_IN_FLIGHT;
+	percpu_counter_dec(&bp->b_target->bt_io_count);
+}
+
+/*
  * When we mark a buffer stale, we remove the buffer from the LRU and clear the
  * b_lru_ref count so that the buffer is freed immediately when the buffer
  * reference count falls to zero. If the buffer is already on the LRU, we need
@@ -866,30 +885,37 @@ xfs_buf_hold(
 }
 
 /*
- *	Releases a hold on the specified buffer.  If the
- *	the hold count is 1, calls xfs_buf_free.
+ * Release a hold on the specified buffer. If the hold count is 1, the buffer is
+ * placed on LRU or freed (depending on b_lru_ref).
  */
 void
 xfs_buf_rele(
 	xfs_buf_t		*bp)
 {
 	struct xfs_perag	*pag = bp->b_pag;
+	bool			release;
+	bool			freebuf = false;
 
 	trace_xfs_buf_rele(bp, _RET_IP_);
 
 	if (!pag) {
 		ASSERT(list_empty(&bp->b_lru));
 		ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
-		if (atomic_dec_and_test(&bp->b_hold))
+		if (atomic_dec_and_test(&bp->b_hold)) {
+			xfs_buf_rele_in_flight(bp);
 			xfs_buf_free(bp);
+		}
 		return;
 	}
 
 	ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
 
 	ASSERT(atomic_read(&bp->b_hold) > 0);
-	if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
-		spin_lock(&bp->b_lock);
+
+	release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
+	spin_lock(&bp->b_lock);
+	if (release) {
+		xfs_buf_rele_in_flight(bp);
 		if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
 			/*
 			 * If the buffer is added to the LRU take a new
@@ -900,7 +926,6 @@ xfs_buf_rele(
 				bp->b_state &= ~XFS_BSTATE_DISPOSE;
 				atomic_inc(&bp->b_hold);
 			}
-			spin_unlock(&bp->b_lock);
 			spin_unlock(&pag->pag_buf_lock);
 		} else {
 			/*
@@ -914,15 +939,24 @@ xfs_buf_rele(
 			} else {
 				ASSERT(list_empty(&bp->b_lru));
 			}
-			spin_unlock(&bp->b_lock);
 
 			ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
 			rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
 			spin_unlock(&pag->pag_buf_lock);
 			xfs_perag_put(pag);

fs/xfs/xfs_buf.c xfs_buf_rele( · line 955

Quoted 10 lines
fs/xfs/xfs_buf.c +7 -1
-			xfs_buf_free(bp);
+			freebuf = true;
 		}
+	} else if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru)) {
+		/*
+		 * The buffer is already on the LRU and it holds the only
+		 * reference. Drop the in flight state.
+		 */
+		xfs_buf_rele_in_flight(bp);
 	}

This b_hold check is racy - bp->b_lock is not enough to stabilise the b_hold count. Because we don't hold the buffer semaphore any more, another buffer reference holder can successfully run the above atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock). New references can be taken in xfs_buf_find() so the count could go up, but I think that's fine given the eventual case we care about here is draining references on unmount.

I think this is still ok for draining references, too, because of the flag check inside xfs_buf_rele_in_flight(). If we race on a transition a value of 1, then we end running the branch in each caller. If we race on transition to zero, then the caller that is releasing the buffer will execute xfs_buf_rele_in_flight() and all will be well.

Needs comments, and maybe restructing the code to handle the xfs_buf_rele_in_flight() call up front so it's clear that io accounting is clearly a separate case from the rest of release handling. e.g.

	release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
	spin_lock(&bp->b_lock);
	if (!release) {
		if (!(atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
			xfs_buf_ioacct_dec(bp);
		goto out_unlock;
	}
	xfs_buf_ioacct_dec(bp);

	/* rest of release code, one level of indentation removed */

out_unlock:

	spin_unlock(&bp->b_lock);

	if (freebuf)
		xfs_buf_free(bp);
Quoted 5 lines
fs/xfs/xfs_buf.c +1 -0
@@ -1341,6 +1375,18 @@ xfs_buf_submit(
 	 * xfs_buf_ioend too early.
 	 */
 	atomic_set(&bp->b_io_remaining, 1);
+

fs/xfs/xfs_buf.c xfs_buf_submit( · line 1388

Quoted 10 lines
fs/xfs/xfs_buf.c +10 -0
+	/*
+	 * Bump the I/O in flight count on the buftarg if we haven't yet done
+	 * so for this buffer. Skip uncached buffers because many of those
+	 * (e.g., superblock, log buffers) are never released.
+	 */
+	if ((bp->b_bn != XFS_BUF_DADDR_NULL) &&
+	    !(bp->b_flags & _XBF_IN_FLIGHT)) {
+		bp->b_flags |= _XBF_IN_FLIGHT;
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+	}

xfs_buf_ioacct_inc()

{
	if (bp->b_flags & (XBF_NO_IOACCT | _XBF_IN_FLIGHT))
		return;
	percpu_counter_inc(&bp->b_target->bt_io_count);
	bp->b_flags |= _XBF_IN_FLIGHT;
}
Quoted 11 lines

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

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

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

#17 lore raw
Quoted 95 lines · Brian Foster
Quoted 84 lines · Dave Chinner
Quoted 73 lines · Brian Foster
Quoted 72 lines · Brian Foster
Quoted 70 lines · Dave Chinner
Quoted 20 lines · Brian Foster

On Mon, Jul 11, 2016 at 09:52:52AM -0400, Brian Foster wrote:
...

Quoted 18 lines

So what is your preference out of the possible approaches here? AFAICS, we have the following options:

1.) The original "add readahead to LRU early" approach.
Pros: simple one-liner
Cons: bit of a hack, only covers readahead scenario
2.) Defer I/O count decrement to buffer release (this patch).
Pros: should cover all cases (reads/writes)
Cons: more complex (requires per-buffer accounting, etc.)
3.) Raw (buffer or bio?) I/O count (no defer to buffer release)
Pros: eliminates some complexity from #2
Cons: still more complex than #1, racy in that decrement does not serialize against LRU addition (requires drain_workqueue(), which still doesn't cover error conditions)

As noted above, option #3 also allows for either a buffer based count or bio based count, the latter of which might simplify things a bit further (TBD). Thoughts?

Pretty good summary :P

Quoted 6 lines

FWIW, the following is a slightly cleaned up version of my initial approach (option #3 above). Note that the flag is used to help deal with varying ioend behavior. E.g., xfs_buf_ioend() is called once for some buffers, multiple times for others with an iodone callback, that behavior changes in some cases when an error is set, etc. (I'll add comments before an official post.)

The approach looks good - I think there's a couple of things we can do to clean it up and make it robust. Comments inline.

Quoted 6 lines
fs/xfs/xfs_buf.c +0 -0
index 4665ff6..45d3ddd 100644
@@ -1018,7 +1018,10 @@ xfs_buf_ioend(
 

fs/xfs/xfs_buf.c xfs_buf_ioend( · line 1027

Quoted 10 lines
fs/xfs/xfs_buf.c +4 -1
 	trace_xfs_buf_iodone(bp, _RET_IP_);
 
-	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
+	if (bp->b_flags & XBF_IN_FLIGHT)
+		percpu_counter_dec(&bp->b_target->bt_io_count);
+
+	bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD | XBF_IN_FLIGHT);
 
 	/*
 	 * Pull in IO completion errors now. We are guaranteed to be running

I think the XBF_IN_FLIGHT can be moved to the final xfs_buf_rele() processing if:

fs/xfs/xfs_buf.c xfs_buf_submit( · line 1350

Quoted 8 lines
fs/xfs/xfs_buf.c +4 -0
@@ -1341,6 +1344,11 @@ xfs_buf_submit(
 	 * xfs_buf_ioend too early.
 	 */
 	atomic_set(&bp->b_io_remaining, 1);
+	if (bp->b_flags & XBF_ASYNC) {
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+		bp->b_flags |= XBF_IN_FLIGHT;
+	}

You change this to:

	if (!(bp->b_flags & XBF_IN_FLIGHT)) {
		percpu_counter_inc(&bp->b_target->bt_io_count);
		bp->b_flags |= XBF_IN_FLIGHT;
	}
Quoted 7 lines
Quoted 4 lines

Ok, so use the flag to cap the I/O count and defer the decrement to release. I think that should work and addresses the raciness issue. I'll give it a try.

This appears to be doable, but it reintroduces some ugliness from the previous approach.

Ah, so it does. Bugger.

Quoted 9 lines
Quoted 3 lines

For example, we have to start filtering out uncached buffers again (if we defer the decrement to release, we must handle never-released buffers one way or another).

So the problem is limited to the superblock buffer and the iclog buffers, right? How about making that special case explicit via a flag set on the buffer? e.g. XBF_NO_IOCOUNT. THat way the exceptions are clearly spelt out, rather than avoiding all uncached buffers?

Quoted 8 lines

I think so. I considered a similar approach earlier, but I didn't want to spend time tracking down the associated users until the broader approach was nailed down. More specifically, I think we could set b_lru_ref to 0 on those buffers and use that to bypass the accounting. That makes it clear that these buffers are not destined for the LRU and alternative synchronization is required (which already exists in the form of lock cycles).

It occurs to me that this probably won't work in all cases because b_lru_ref is decremented naturally as part of the LRU shrinker mechanism. Disregard this, I'll look into the flag..

Brian

Quoted 196 lines

The rest of the feedback makes sense, so I'll incorporate that and give the above a try... thanks.

Brian

Quoted 179 lines
Quoted 18 lines

Also, given the feedback on
the previous patch with regard to filtering out non-new buffers from the I/O count, I've dropped that and replaced it with updates to xfs_buf_rele() to decrement when the buffer is returned to the LRU (we either have to filter out buffers already on the LRU at submit time or make sure that they are decremented when released back to the LRU).

Code follows...

Brian

fs/xfs/xfs_buf.c +0 -0
index 4665ff6..b7afbac 100644
@@ -80,6 +80,25 @@ xfs_buf_vmap_len(
 }
 

fs/xfs/xfs_buf.c xfs_buf_vmap_len( · line 91

Quoted 10 lines
fs/xfs/xfs_buf.c +9 -0
 /*
+ * Clear the in-flight state on a buffer about to be released to the LRU or
+ * freed and unaccount from the buftarg. The buftarg I/O count maintains a count
+ * of held buffers that have undergone at least one I/O in the current hold
+ * cycle (e.g., not a total I/O count). This provides protection against unmount
+ * for buffer I/O completion (see xfs_wait_buftarg()) processing.
+ */
+static inline void
+xfs_buf_rele_in_flight(
+	struct xfs_buf	*bp)

Not sure about the name: xfs_buf_ioacct_dec()?

Quoted 74 lines
fs/xfs/xfs_buf.c +22 -7
+{
+	if (!(bp->b_flags & _XBF_IN_FLIGHT))
+		return;
+
+	ASSERT(bp->b_flags & XBF_ASYNC);
+	bp->b_flags &= ~_XBF_IN_FLIGHT;
+	percpu_counter_dec(&bp->b_target->bt_io_count);
+}
+
+/*
  * When we mark a buffer stale, we remove the buffer from the LRU and clear the
  * b_lru_ref count so that the buffer is freed immediately when the buffer
  * reference count falls to zero. If the buffer is already on the LRU, we need
@@ -866,30 +885,37 @@ xfs_buf_hold(
 }
 
 /*
- *	Releases a hold on the specified buffer.  If the
- *	the hold count is 1, calls xfs_buf_free.
+ * Release a hold on the specified buffer. If the hold count is 1, the buffer is
+ * placed on LRU or freed (depending on b_lru_ref).
  */
 void
 xfs_buf_rele(
 	xfs_buf_t		*bp)
 {
 	struct xfs_perag	*pag = bp->b_pag;
+	bool			release;
+	bool			freebuf = false;
 
 	trace_xfs_buf_rele(bp, _RET_IP_);
 
 	if (!pag) {
 		ASSERT(list_empty(&bp->b_lru));
 		ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
-		if (atomic_dec_and_test(&bp->b_hold))
+		if (atomic_dec_and_test(&bp->b_hold)) {
+			xfs_buf_rele_in_flight(bp);
 			xfs_buf_free(bp);
+		}
 		return;
 	}
 
 	ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
 
 	ASSERT(atomic_read(&bp->b_hold) > 0);
-	if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
-		spin_lock(&bp->b_lock);
+
+	release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
+	spin_lock(&bp->b_lock);
+	if (release) {
+		xfs_buf_rele_in_flight(bp);
 		if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
 			/*
 			 * If the buffer is added to the LRU take a new
@@ -900,7 +926,6 @@ xfs_buf_rele(
 				bp->b_state &= ~XFS_BSTATE_DISPOSE;
 				atomic_inc(&bp->b_hold);
 			}
-			spin_unlock(&bp->b_lock);
 			spin_unlock(&pag->pag_buf_lock);
 		} else {
 			/*
@@ -914,15 +939,24 @@ xfs_buf_rele(
 			} else {
 				ASSERT(list_empty(&bp->b_lru));
 			}
-			spin_unlock(&bp->b_lock);
 
 			ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
 			rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
 			spin_unlock(&pag->pag_buf_lock);
 			xfs_perag_put(pag);

fs/xfs/xfs_buf.c xfs_buf_rele( · line 955

Quoted 10 lines
fs/xfs/xfs_buf.c +7 -1
-			xfs_buf_free(bp);
+			freebuf = true;
 		}
+	} else if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru)) {
+		/*
+		 * The buffer is already on the LRU and it holds the only
+		 * reference. Drop the in flight state.
+		 */
+		xfs_buf_rele_in_flight(bp);
 	}

This b_hold check is racy - bp->b_lock is not enough to stabilise the b_hold count. Because we don't hold the buffer semaphore any more, another buffer reference holder can successfully run the above atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock). New references can be taken in xfs_buf_find() so the count could go up, but I think that's fine given the eventual case we care about here is draining references on unmount.

I think this is still ok for draining references, too, because of the flag check inside xfs_buf_rele_in_flight(). If we race on a transition a value of 1, then we end running the branch in each caller. If we race on transition to zero, then the caller that is releasing the buffer will execute xfs_buf_rele_in_flight() and all will be well.

Needs comments, and maybe restructing the code to handle the xfs_buf_rele_in_flight() call up front so it's clear that io accounting is clearly a separate case from the rest of release handling. e.g.

	release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
	spin_lock(&bp->b_lock);
	if (!release) {
		if (!(atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
			xfs_buf_ioacct_dec(bp);
		goto out_unlock;
	}
	xfs_buf_ioacct_dec(bp);

	/* rest of release code, one level of indentation removed */

out_unlock:

	spin_unlock(&bp->b_lock);

	if (freebuf)
		xfs_buf_free(bp);
Quoted 5 lines
fs/xfs/xfs_buf.c +1 -0
@@ -1341,6 +1375,18 @@ xfs_buf_submit(
 	 * xfs_buf_ioend too early.
 	 */
 	atomic_set(&bp->b_io_remaining, 1);
+

fs/xfs/xfs_buf.c xfs_buf_submit( · line 1388

Quoted 10 lines
fs/xfs/xfs_buf.c +10 -0
+	/*
+	 * Bump the I/O in flight count on the buftarg if we haven't yet done
+	 * so for this buffer. Skip uncached buffers because many of those
+	 * (e.g., superblock, log buffers) are never released.
+	 */
+	if ((bp->b_bn != XFS_BUF_DADDR_NULL) &&
+	    !(bp->b_flags & _XBF_IN_FLIGHT)) {
+		bp->b_flags |= _XBF_IN_FLIGHT;
+		percpu_counter_inc(&bp->b_target->bt_io_count);
+	}

xfs_buf_ioacct_inc()

{
	if (bp->b_flags & (XBF_NO_IOACCT | _XBF_IN_FLIGHT))
		return;
	percpu_counter_inc(&bp->b_target->bt_io_count);
	bp->b_flags |= _XBF_IN_FLIGHT;
}
Quoted 11 lines

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

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

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

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