fix deadlock in balance_dirty_pages
Subsystems (5)
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
Andrew Morton
maintainerQuoted 64 lines · On Tue, 27 Feb 2007 23:38:12 +0100 Miklos Szeredi <[email protected]> wrote:
From: Miklos Szeredi <[email protected]>
This deadlock happens, when dirty pages from one filesystem are written back through another filesystem. It easiest to demonstrate with fuse although it could affect looback mounts as well (see following patches).
Let's call the filesystems A(bove) and B(elow). Process
Pr_ais writing to A, and processPr_bis writing to B.
Pr_ais bash-shared-mapping.Pr_bis the fuse filesystem daemon (fusexmp_fh), for simplicity let's assume thatPr_bis single threaded.These are the simplified stack traces of these processes after the deadlock:
Pr_a(bash-shared-mapping):(block on queue)
fuse_writepage
generic_writepages
writeback_inodes
balance_dirty_pages
balance_dirty_pages_ratelimited_nr
set_page_dirty_mapping_balance
do_no_page
Pr_b(fusexmp_fh):
io_schedule_timeout
congestion_wait
balance_dirty_pages
balance_dirty_pages_ratelimited_nr
generic_file_buffered_write
generic_file_aio_write
ext3_file_write
do_sync_write
vfs_write
sys_pwrite64Thanks to the aggressive nature of
Pr_a, it can happen, that
nr_file_dirty>dirty_thresh+ marginThis is due to both
nr_dirtygrowing anddirty_threshshrinking, which in turn is due tonr_file_mappedrapidly growing. The exact size of the margin at which the deadlock happens is not known, but it's around 100 pages.At this point
Pr_aentersbalance_dirty_pagesand starts to write back some if it's dirty pages. After submitting some requests, it blocks on the request queue.The first write request will trigger
Pr_bto perform awrite()syscall. This will submit a write request to the block device and then may enterbalance_dirty_pages().The condition for exiting
balance_dirty_pages()is- either that
write_chunkpages have been written
Quoted 7 lines
- or
nr_file_dirty+nr_writeback<dirty_threshIt is entirely possible that less than
write_chunkpages were written, in which casebalance_dirty_pages()will not exit even after all the submitted requests have been succesfully completed.Which means that the
write()syscall does not return.
But the balance_dirty_pages() loop does more than just wait for those two conditions. It will also submit _more_ dirty pages for writeout. ie: it should be feeding more of file A's pages into writepage.
Why isn't that happening?
Quoted 47 lines
Which means, that no more dirty pages from A will be written back, and neither
nr_writebacknornr_file_dirtywill decrease.Which means, that
balance_dirty_pages()will loop forever.Q.E.D.
The solution is to exit
balance_dirty_pages()on the condition, that there are only a few dirty + writeback pages for this backing dev. This makes sure, that there is always some progress with this setup.The number of outstanding dirty + written pages is limited to 8, which means that when over the threshold (
dirty_exceeded== 1), each filesystem may only effectively pin a maximum of 16 (+8 because of ratelimiting) extra pages.Note: a similar safety vent is always needed if there's a global limit for the dirty+writeback pages, even if in the future there will be some per-queue (or other) soft limit.
Signed-off-by Miklos Szeredi <[email protected]>---
Index:
linux/mm/page-writeback.c
===================================================================mm/page-writeback.c +11
-1@@ -201,6 +201,17 @@ static void balance_dirty_pages(struct aif (!dirty_exceeded) dirty_exceeded =1 ; +/* +* Acquit producer of dirty pages if there's little or +* nothing to write back to this particular queue. +* +* Without this check a deadlock is possible for if +* one filesystem is writing data through another. +*/ +if (atomic_long_read (&bdi->nr_dirty) + +atomic_long_read (&bdi->nr_writeback) <8 ) +break ; +/* Note: nr_reclaimable denotes nr_dirty + nr_unstable. * Unstable writes are a feature of certain networked * filesystems (i.e. NFS) in which data may have been --
Quoted 68 lines
Quoted 68 lines
This deadlock happens, when dirty pages from one filesystem are written back through another filesystem. It easiest to demonstrate with fuse although it could affect looback mounts as well (see following patches).
Let's call the filesystems A(bove) and B(elow). Process
Pr_ais writing to A, and processPr_bis writing to B.
Pr_ais bash-shared-mapping.Pr_bis the fuse filesystem daemon (fusexmp_fh), for simplicity let's assume thatPr_bis single threaded.These are the simplified stack traces of these processes after the deadlock:
Pr_a(bash-shared-mapping):(block on queue)
fuse_writepage
generic_writepages
writeback_inodes
balance_dirty_pages
balance_dirty_pages_ratelimited_nr
set_page_dirty_mapping_balance
do_no_page
Pr_b(fusexmp_fh):
io_schedule_timeout
congestion_wait
balance_dirty_pages
balance_dirty_pages_ratelimited_nr
generic_file_buffered_write
generic_file_aio_write
ext3_file_write
do_sync_write
vfs_write
sys_pwrite64Thanks to the aggressive nature of
Pr_a, it can happen, that
nr_file_dirty>dirty_thresh+ marginThis is due to both
nr_dirtygrowing anddirty_threshshrinking, which in turn is due tonr_file_mappedrapidly growing. The exact size of the margin at which the deadlock happens is not known, but it's around 100 pages.At this point
Pr_aentersbalance_dirty_pagesand starts to write back some if it's dirty pages. After submitting some requests, it blocks on the request queue.The first write request will trigger
Pr_bto perform awrite()syscall. This will submit a write request to the block device and then may enterbalance_dirty_pages().The condition for exiting
balance_dirty_pages()is- either that
write_chunkpages have been written- or
nr_file_dirty+nr_writeback<dirty_threshIt is entirely possible that less than
write_chunkpages were written, in which casebalance_dirty_pages()will not exit even after all the submitted requests have been succesfully completed.
Quoted 7 lines
Quoted 1 line
Which means that the
write()syscall does not return.But the
balance_dirty_pages()loop does more than just wait for those two conditions. It will also submit _more_ dirty pages for writeout. ie: it should be feeding more of file A's pages into writepage.Why isn't that happening?
All of A's data is actually written by B. So just submitting more pages to some queue doesn't help, it will just make the queue longer.
If the queue length were not limited, and B would have limitless threads, and the write() wouldn't exclude other writes to the same file (i_mutex), then there would be no deadlock.
But for fuse the first and the last condition isn't met.
For the loop device the second condition isn't met, loop is single threaded.
Thanks,
Miklos
Andrew Morton
maintainerQuoted 79 lines · On Thu, 01 Mar 2007 08:35:28 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 68 lines
Quoted 68 lines
This deadlock happens, when dirty pages from one filesystem are written back through another filesystem. It easiest to demonstrate with fuse although it could affect looback mounts as well (see following patches).
Let's call the filesystems A(bove) and B(elow). Process
Pr_ais writing to A, and processPr_bis writing to B.
Pr_ais bash-shared-mapping.Pr_bis the fuse filesystem daemon (fusexmp_fh), for simplicity let's assume thatPr_bis single threaded.These are the simplified stack traces of these processes after the deadlock:
Pr_a(bash-shared-mapping):(block on queue)
fuse_writepage
generic_writepages
writeback_inodes
balance_dirty_pages
balance_dirty_pages_ratelimited_nr
set_page_dirty_mapping_balance
do_no_page
Pr_b(fusexmp_fh):
io_schedule_timeout
congestion_wait
balance_dirty_pages
balance_dirty_pages_ratelimited_nr
generic_file_buffered_write
generic_file_aio_write
ext3_file_write
do_sync_write
vfs_write
sys_pwrite64Thanks to the aggressive nature of
Pr_a, it can happen, that
nr_file_dirty>dirty_thresh+ marginThis is due to both
nr_dirtygrowing anddirty_threshshrinking, which in turn is due tonr_file_mappedrapidly growing. The exact size of the margin at which the deadlock happens is not known, but it's around 100 pages.At this point
Pr_aentersbalance_dirty_pagesand starts to write back some if it's dirty pages. After submitting some requests, it blocks on the request queue.The first write request will trigger
Pr_bto perform awrite()syscall. This will submit a write request to the block device and then may enterbalance_dirty_pages().The condition for exiting
balance_dirty_pages()is- either that
write_chunkpages have been written- or
nr_file_dirty+nr_writeback<dirty_threshIt is entirely possible that less than
write_chunkpages were written, in which casebalance_dirty_pages()will not exit even after all the submitted requests have been succesfully completed.Quoted 7 lines
Quoted 1 line
Which means that the
write()syscall does not return.But the
balance_dirty_pages()loop does more than just wait for those two conditions. It will also submit _more_ dirty pages for writeout. ie: it should be feeding more of file A's pages into writepage.Why isn't that happening?
All of A's data is actually written by B. So just submitting more pages to some queue doesn't help, it will just make the queue longer.
Quoted 8 lines
If the queue length were not limited, and B would have limitless threads, and the
write()wouldn't exclude other writes to the same file (i_mutex), then there would be no deadlock.But for fuse the first and the last condition isn't met.
For the loop device the second condition isn't met, loop is single threaded.
Sigh. What's this about i_mutex? That appears to be some critical information which _still_ isn't being communicated.
Quoted 83 lines
Quoted 83 lines
Quoted 68 lines
Quoted 68 lines
This deadlock happens, when dirty pages from one filesystem are written back through another filesystem. It easiest to demonstrate with fuse although it could affect looback mounts as well (see following patches).
Let's call the filesystems A(bove) and B(elow). Process
Pr_ais writing to A, and processPr_bis writing to B.
Pr_ais bash-shared-mapping.Pr_bis the fuse filesystem daemon (fusexmp_fh), for simplicity let's assume thatPr_bis single threaded.These are the simplified stack traces of these processes after the deadlock:
Pr_a(bash-shared-mapping):(block on queue)
fuse_writepage
generic_writepages
writeback_inodes
balance_dirty_pages
balance_dirty_pages_ratelimited_nr
set_page_dirty_mapping_balance
do_no_page
Pr_b(fusexmp_fh):
io_schedule_timeout
congestion_wait
balance_dirty_pages
balance_dirty_pages_ratelimited_nr
generic_file_buffered_write
generic_file_aio_write
ext3_file_write
do_sync_write
vfs_write
sys_pwrite64Thanks to the aggressive nature of
Pr_a, it can happen, that
nr_file_dirty>dirty_thresh+ marginThis is due to both
nr_dirtygrowing anddirty_threshshrinking, which in turn is due tonr_file_mappedrapidly growing. The exact size of the margin at which the deadlock happens is not known, but it's around 100 pages.At this point
Pr_aentersbalance_dirty_pagesand starts to write back some if it's dirty pages. After submitting some requests, it blocks on the request queue.The first write request will trigger
Pr_bto perform awrite()syscall. This will submit a write request to the block device and then may enterbalance_dirty_pages().The condition for exiting
balance_dirty_pages()is- either that
write_chunkpages have been written- or
nr_file_dirty+nr_writeback<dirty_threshIt is entirely possible that less than
write_chunkpages were written, in which casebalance_dirty_pages()will not exit even after all the submitted requests have been succesfully completed.Quoted 7 lines
Quoted 1 line
Which means that the
write()syscall does not return.But the
balance_dirty_pages()loop does more than just wait for those two conditions. It will also submit _more_ dirty pages for writeout. ie: it should be feeding more of file A's pages into writepage.Why isn't that happening?
All of A's data is actually written by B. So just submitting more pages to some queue doesn't help, it will just make the queue longer.
If the queue length were not limited, and B would have limitless threads, and the
write()wouldn't exclude other writes to the same file (i_mutex), then there would be no deadlock.
Quoted 8 lines
Quoted 4 lines
But for fuse the first and the last condition isn't met.
For the loop device the second condition isn't met, loop is single threaded.
Sigh. What's this about
i_mutex? That appears to be some critical information which _still_ isn't being communicated.
This:
ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
unsigned long nr_segs, loff_t pos)
{
struct file *file = iocb->ki_filp;
struct address_space *mapping = file->f_mapping;
struct inode *inode = mapping->host;
ssize_t ret;
BUG_ON(iocb->ki_pos != pos);
mutex_lock(&inode->i_mutex);
ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
&iocb->ki_pos);
mutex_unlock(&inode->i_mutex);It's in the stack trace. I thought it was obvious.
Miklos
Andrew Morton
maintainerQuoted 22 lines · On Thu, 01 Mar 2007 09:37:06 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 3 lines
Sigh. What's this about
i_mutex? That appears to be some critical information which _still_ isn't being communicated.This:
ssize_tgeneric_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
unsigned longnr_segs,loff_tpos){ struct file *file = iocb->ki_filp; struct address_space *mapping = file->f_mapping; struct inode *inode = mapping->host; ssize_t ret; BUG_ON(iocb->ki_pos != pos); mutex_lock(&inode->i_mutex); ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, &iocb->ki_pos); mutex_unlock(&inode->i_mutex);
Quoted 1 line
It's in the stack trace. I thought it was obvious.
No, it is not obvious.
I'm just not going to apply weird hacks to work around a bug which I do not understand, and I have spent way too much time trying to understand this one.
So let us persist.
Please fully describe the role of i_mutex in this hang.
Quoted 3 lines
I'm just not going to apply weird hacks to work around a bug which I do not understand, and I have spent way too much time trying to understand this one.
I suggest you apply patch #5 "balance dirty pages from loop device" and see for yourself the same deadlock with a simple loopback mount.
I beleive _not_ doing balance_dirty_pages() in loop is rather a bigger hack, then mine ;)
Quoted 3 lines
So let us persist.
Please fully describe the role of
i_mutexin this hang.
OK. Added description of the multithreaded case, with i_mutex's role:
+ What if Pr_b is multithreaded? The first thread will enter
+ balance_dirty_pages() and loop there as shown above. It will hold
+ i_mutex for the inode, taken in generic_file_aio_write(). +
+ The other theads now try to write back more data into the same file,
+ but will block on i_mutex. So even with unlimited number of threads
+ no progress is made.
Thanks,
Miklos
From: Miklos Szeredi <[email protected]>
This deadlock happens, when dirty pages from one filesystem are written back through another filesystem. It easiest to demonstrate with fuse although it could affect looback mounts as well (see following patches).
Let's call the filesystems A(bove) and B(elow). Process Pr_a is writing to A, and process Pr_b is writing to B.
Pr_a is bash-shared-mapping. Pr_b is the fuse filesystem daemon (fusexmp_fh), for simplicity let's assume that Pr_b is single threaded.
These are the simplified stack traces of these processes after the deadlock:
Pr_a (bash-shared-mapping):
(block on queue)
fuse_writepage
generic_writepages
writeback_inodes
balance_dirty_pages
balance_dirty_pages_ratelimited_nr
set_page_dirty_mapping_balance
do_no_page
Pr_b (fusexmp_fh):
io_schedule_timeout
congestion_wait
balance_dirty_pages
balance_dirty_pages_ratelimited_nr
generic_file_buffered_write
generic_file_aio_write
ext3_file_write
do_sync_write
vfs_write
sys_pwrite64
Thanks to the aggressive nature of Pr_a, it can happen, that
nr_file_dirty > dirty_thresh + margin
This is due to both nr_dirty growing and dirty_thresh shrinking, which in turn is due to nr_file_mapped rapidly growing. The exact size of the margin at which the deadlock happens is not known, but it's around 100 pages.
At this point Pr_a enters balance_dirty_pages and starts to write back some if it's dirty pages. After submitting some requests, it blocks on the request queue.
The first write request will trigger Pr_b to perform a write() syscall. This will submit a write request to the block device and then may enter balance_dirty_pages().
The condition for exiting balance_dirty_pages() is
- either that write_chunk pages have been written
- or nr_file_dirty + nr_writeback < dirty_thresh
It is entirely possible that less than write_chunk pages were written, in which case balance_dirty_pages() will not exit even after all the submitted requests have been succesfully completed.
Which means that the write() syscall does not return.
Which means, that no more dirty pages from A will be written back, and neither nr_writeback nor nr_file_dirty will decrease.
Which means, that balance_dirty_pages() will loop forever.
What if Pr_b is multithreaded? The first thread will enter balance_dirty_pages() and loop there as shown above. It will hold i_mutex for the inode, taken in generic_file_aio_write().
The other theads now try to write back more data into the same file, but will block on i_mutex. So even with unlimited number of threads no progress is made.
Q.E.D.
The solution is to exit balance_dirty_pages() on the condition, that there are only a few dirty + writeback pages for this backing dev. This makes sure, that there is always some progress with this setup.
The number of outstanding dirty + written pages is limited to 8, which means that when over the threshold (dirty_exceeded == 1), each filesystem may only effectively pin a maximum of 16 (+8 because of ratelimiting) extra pages.
Note: a similar safety vent is always needed if there's a global limit for the dirty+writeback pages, even if in the future there will be some per-queue (or other) soft limit.
---
mm/page-writeback.c +11 -0
@@ -201,6 +201,17 @@ static void balance_dirty_pages(struct aif (!dirty_exceeded) dirty_exceeded =1 ; +/* +* Acquit producer of dirty pages if there's little or +* nothing to write back to this particular queue. +* +* Without this check a deadlock is possible for if +* one filesystem is writing data through another. +*/ +if (atomic_long_read (&bdi->nr_dirty) + +atomic_long_read (&bdi->nr_writeback) <8 ) +break ; +/* Note: nr_reclaimable denotes nr_dirty + nr_unstable. * Unstable writes are a feature of certain networked * filesystems (i.e. NFS) in which data may have been
A message that is not in the archive (sent off-list or not yet received): [email protected]
Andrew Morton
maintainerRe: [patch 04/22] fix deadlock in throttle_vm_writeout
Quoted 55 lines · On Tue, 27 Feb 2007 23:38:13 +0100 Miklos Szeredi <[email protected]> wrote:
From: Miklos Szeredi <[email protected]>
This deadlock is similar to the one in
balance_dirty_pages, but instead of waiting inbalance_dirty_pagesafter submitting a write request, it happens during a memory allocation for filesystem B before submitting a write request.It is easy to reproduce on a machine with not too much memory. E.g. try this on 2.6.21-rc1 UML with 32MB (works on physical hw as well):
dd if=/dev/zero of=/tmp/tmp.img bs=1048576 count=40 mke2fs -j -F
/tmp/tmp.img
mkdir/tmp/img
mount -oloop/tmp/tmp.img/tmp/imgbash-shared-mapping/tmp/img/foo30000000The deadlock doesn't happen immediately, sometimes only after a few minutes.
Simplified stack trace for bash-shared-mapping after the deadlock:
io_schedule_timeout
congestion_wait
balance_dirty_pages
balance_dirty_pages_ratelimited_nr
generic_file_buffered_write
__generic_file_aio_write_nolock
generic_file_aio_write
ext3_file_write
do_sync_write
vfs_write
sys_pwrite64and for [loop0]:
io_schedule_timeout
congestion_wait
throttle_vm_writeout
shrink_zone
shrink_zones
try_to_free_pages
__alloc_pages
find_or_create_page
do_lo_send_aops
lo_send
do_bio_filebacked
loop_threadThe requirement for the deadlock is that
nr_writeback>dirty_thresh* 1.1 + marginAgain margin seems to be in the 100 page range.
Quoted 8 lines
The task of
throttle_vm_writeoutis to limit the rate at which under-writeback pages are created due to swapping. There's no other way direct reclaim can increase thenr_writeback+nr_file_dirty.So when there are few or no under-swap pages, it is safe for this function to return. This ensures, that there's progress with writing back dirty pages.
Would this also be solved by the below just-submitted bugfix? I guess not.
I think the basic problem here is that the loop thread is reponsible for cleaning memory, but in throttle_vm_writeout(), the loop thread can get stuck waiting for some other thread to clean memory, but that ain't going to happen.
throttle_vm_writeout() wasn't very well thought through, I suspect.
I suspect we can just delete throttle_vm_writeout() now. The original rationale was:
[PATCH] vm: pageout throttling
With silly pageout testcases it is possible to place huge amounts of memory under I/O. With a large request queue (CFQ uses 8192 requests) it is possible to place _all_ memory under I/O at the same time.
This means that all memory is pinned and unreclaimable and the VM gets upset and goes oom.
The patch limits the amount of memory which is under pageout writeout to be a little more than the amount of memory at which balance_dirty_pages() callers will synchronously throttle.
This means that heavy pageout activity can starve heavy writeback activity completely, but heavy writeback activity will not cause starvation of pageout. Because we don't want a simple `dd' to be causing excessive latencies in page reclaim.
but now that we limit the amount of dirty MAP_SHARED memory, and given that the various pieces of the dirty-memory limiting puzzle also take the number of under-writeback pages into account, we should no longer be able to get in the situation where the total number of writeback+dirty pages exceeds dirty_ratio.
From: Andrew Morton <[email protected]>
throttle_vm_writeout() is designed to wait for the dirty levels to subside. But if the caller holds IO or FS locks, we might be holding up that writeout.
So change it to take a single nap to give other devices a chance to clean some memory, then return.
---
include/linux/writeback.h | 2 +- mm/page-writeback.c | 13 +++++++++++-- mm/vmscan.c | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-)
include/linux/writeback.h renamed from include/linux/writeback.h~throttle_vm_writeout-dont-loop-on-gfp_nofs-and-gfp_noio-allocations +1 -1
@@ -84,7 +84,7 @@ static inline void wait_on_inode(structint wakeup_pdflush (long nr_pages);void laptop_io_completion (void );void laptop_sync_completion (void ); -void throttle_vm_writeout (void ); +void throttle_vm_writeout (gfp_t gfp_mask);/* These are exported to sysctl. */ extern int dirty_background_ratio;
mm/page-writeback.c renamed from mm/page-writeback.c~throttle_vm_writeout-dont-loop-on-gfp_nofs-and-gfp_noio-allocations +11 -2
@@ -296,11 +296,21 @@ void balance_dirty_pages_ratelimited_nr( }EXPORT_SYMBOL (balance_dirty_pages_ratelimited_nr); -void throttle_vm_writeout (void ) +void throttle_vm_writeout (gfp_t gfp_mask) {long background_thresh;long dirty_thresh; +if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO)) { +/* +* The caller might hold locks which can prevent IO completion +* or progress in the filesystem. So we cannot just sit here +* waiting for IO to complete. +*/ +congestion_wait (WRITE ,HZ /10 ); +return ; + } +for ( ; ; ) {get_dirty_limits (&background_thresh, &dirty_thresh,NULL );
@@ -317,7 +327,6 @@ void throttle_vm_writeout(void) } } -/* * writeback at least _min_pages, and keep writing until the amount of dirty * memory is less than the background threshold, or until we're all clean.
mm/vmscan.c renamed from mm/vmscan.c~throttle_vm_writeout-dont-loop-on-gfp_nofs-and-gfp_noio-allocations +1 -1
@@ -952,7 +952,7 @@ static unsigned long shrink_zone(int pri } } -throttle_vm_writeout (); +throttle_vm_writeout (sc->gfp_mask);atomic_dec (&zone->reclaim_in_progress);return nr_reclaimed;
_
Re: [patch 04/22] fix deadlock in throttle_vm_writeout
Quoted 94 lines
Quoted 55 lines
From: Miklos Szeredi <[email protected]>
This deadlock is similar to the one in
balance_dirty_pages, but instead of waiting inbalance_dirty_pagesafter submitting a write request, it happens during a memory allocation for filesystem B before submitting a write request.It is easy to reproduce on a machine with not too much memory. E.g. try this on 2.6.21-rc1 UML with 32MB (works on physical hw as well):
dd if=/dev/zero of=/tmp/tmp.img bs=1048576 count=40 mke2fs -j -F
/tmp/tmp.img
mkdir/tmp/img
mount -oloop/tmp/tmp.img/tmp/imgbash-shared-mapping/tmp/img/foo30000000The deadlock doesn't happen immediately, sometimes only after a few minutes.
Simplified stack trace for bash-shared-mapping after the deadlock:
io_schedule_timeout
congestion_wait
balance_dirty_pages
balance_dirty_pages_ratelimited_nr
generic_file_buffered_write
__generic_file_aio_write_nolock
generic_file_aio_write
ext3_file_write
do_sync_write
vfs_write
sys_pwrite64and for [loop0]:
io_schedule_timeout
congestion_wait
throttle_vm_writeout
shrink_zone
shrink_zones
try_to_free_pages
__alloc_pages
find_or_create_page
do_lo_send_aops
lo_send
do_bio_filebacked
loop_threadThe requirement for the deadlock is that
nr_writeback>dirty_thresh* 1.1 + marginAgain margin seems to be in the 100 page range.
Quoted 8 lines
The task of
throttle_vm_writeoutis to limit the rate at which under-writeback pages are created due to swapping. There's no other way direct reclaim can increase thenr_writeback+nr_file_dirty.So when there are few or no under-swap pages, it is safe for this function to return. This ensures, that there's progress with writing back dirty pages.
Would this also be solved by the below just-submitted bugfix? I guess not.
I think the basic problem here is that the loop thread is reponsible for cleaning memory, but in
throttle_vm_writeout(), the loop thread can get stuck waiting for some other thread to clean memory, but that ain't going to happen.
throttle_vm_writeout()wasn't very well thought through, I suspect.I suspect we can just delete
throttle_vm_writeout()now. The original rationale was:[PATCH] vm: pageout throttling
With silly pageout testcases it is possible to place huge amounts of memory under I/O. With a large request queue (CFQ uses 8192 requests) it is possible to place _all_ memory under I/O at the same time.
This means that all memory is pinned and unreclaimable and the VM gets upset and goes oom.
The patch limits the amount of memory which is under pageout writeout to be a little more than the amount of memory at which
balance_dirty_pages()callers will synchronously throttle.This means that heavy pageout activity can starve heavy writeback activity completely, but heavy writeback activity will not cause starvation of pageout. Because we don't want a simple `dd' to be causing excessive latencies in page reclaim.
Quoted 5 lines
but now that we limit the amount of dirty
MAP_SHAREDmemory, and given that the various pieces of the dirty-memory limiting puzzle also take the number of under-writeback pages into account, we should no longer be able to get in the situation where the total number of writeback+dirty pages exceedsdirty_ratio.
Only with swap, which can generate a lot of writeback pages, without limiting. Does this matter? I guess that depends on the queue length. If a lot of swap requests are using up memory, than that's bad.
Quoted 8 lines
From: Andrew Morton <[email protected]>
throttle_vm_writeout()is designed to wait for the dirty levels to subside. But if the caller holds IO or FS locks, we might be holding up that writeout.So change it to take a single nap to give other devices a chance to clean some memory, then return.
Why does it nap unconditionally for GFP_NOFS/NOIO? That seems to be a waste of time.
Btw, I did try not calling throttle_vm_writeout() for GFP_NOFS/NOIO and IIRC it didn't help. I'll check again.
Thanks,
Miklos
Quoted 71 lines
Cc Nick Piggin <[email protected]>Cc OGAWA Hirofumi <[email protected]>Cc Kumar Gala <[email protected]>Cc Pete Zaitcev <[email protected]>Cc <[email protected]>Signed-off-by Andrew Morton <[email protected]>---
include/linux/writeback.h | 2 +- mm/page-writeback.c | 13 +++++++++++-- mm/vmscan.c | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-)diff -puN
include/linux/writeback.h~throttle_vm_writeout-dont-loop-on-gfp_nofs-and-gfp_noio-allocationsinclude/linux/writeback.hinclude/linux/writeback.h renamed from include/linux/writeback.h~throttle_vm_writeout-dont-loop-on-gfp_nofs-and-gfp_noio-allocations +1
-1@@ -84,7 +84,7 @@ static inline void wait_on_inode(structint wakeup_pdflush (long nr_pages);void laptop_io_completion (void );void laptop_sync_completion (void ); -void throttle_vm_writeout (void ); +void throttle_vm_writeout (gfp_t gfp_mask);/* These are exported to sysctl. */ extern int dirty_background_ratio;diff -puN
mm/page-writeback.c~throttle_vm_writeout-dont-loop-on-gfp_nofs-and-gfp_noio-allocationsmm/page-writeback.cmm/page-writeback.c renamed from mm/page-writeback.c~throttle_vm_writeout-dont-loop-on-gfp_nofs-and-gfp_noio-allocations +11
-2@@ -296,11 +296,21 @@ void balance_dirty_pages_ratelimited_nr( }EXPORT_SYMBOL (balance_dirty_pages_ratelimited_nr); -void throttle_vm_writeout (void ) +void throttle_vm_writeout (gfp_t gfp_mask) {long background_thresh;long dirty_thresh; +if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO)) { +/* +* The caller might hold locks which can prevent IO completion +* or progress in the filesystem. So we cannot just sit here +* waiting for IO to complete. +*/ +congestion_wait (WRITE ,HZ /10 ); +return ; + } +for ( ; ; ) {get_dirty_limits (&background_thresh, &dirty_thresh,NULL );@@ -317,7 +327,6 @@ void throttle_vm_writeout(void) } } -/* * writeback at least _min_pages, and keep writing until the amount of dirty * memory is less than the background threshold, or until we're all clean. diff -puN
mm/vmscan.c~throttle_vm_writeout-dont-loop-on-gfp_nofs-and-gfp_noio-allocationsmm/vmscan.cmm/vmscan.c renamed from mm/vmscan.c~throttle_vm_writeout-dont-loop-on-gfp_nofs-and-gfp_noio-allocations +1
-1@@ -952,7 +952,7 @@ static unsigned long shrink_zone(int pri } } -throttle_vm_writeout (); +throttle_vm_writeout (sc->gfp_mask);atomic_dec (&zone->reclaim_in_progress);return nr_reclaimed;