Skip to contentSkip to conversation

Fix for VM deadlock in 2.2.1

PATCH In review Stephen C. Tweedie 2 participants 2 messages Feb 5, 1999 – Feb 6, 1999 Memory management

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix for VM deadlock in 2.2.1

Hi,

The VM deadlock problems we still have in 2.2 are triggered on both inode and superblock locks, but the underlying cause is not the filesystem locking but the VM reentrancy involved. Any file IO operation (write, msync or whatever) which holds a critical vfs lock while allocating memory can recurse if the file is mmap()ed writably anywhere, as we go down the path get_free_pages() -> try_to_free_pages() -> try_to_swap_out() -> filemap_write_page().

Even with the recursive inode semaphore, it is trivial to reproduce this using two or more processes each doing mmap + write to their own files.

One the way to hack this problem out bit by bit is to redo the locking on all filesystems, and the recursive semaphore code is a start down this path.

The way to completely eliminate the problem is for the VM to avoid this recursion in the first place. The patch below adds a new kpiod (page IO daemon) thread to augment kswapd. All filemap page writes get queued to this thread for IO rather than being executed in the context of the caller, and the caller never blocks waiting for that IO to complete. In other words, the caller can never fail eventually to release any vfs locks currently held, so the page write is guaranteed to succeed eventually. Even recursive allocations within the kpiod thread are safe, since that just results in a queuing of the recursive page write:
the actual IO is deferred until the kpiod thread loops to its next request.

The downside of the change is that we are limited to a single thread of execution when doing memory mapped writeback, although starting multiple kpiod threads will avoid this if it proves to be a problem. On ext2fs, only the copying of the page to the buffer cache is serialised in this way; the actual disk IO will proceed asynchronously via bdflush as usual.

msync() does not use the new kpiod thread for its page writes, nor does it need to.

A reproducer which stresses writeable mmap and write()s in parallel would reliably lock up within a couple of iterations on 2.2.1, but I got bored after listening to it successfully thrash the disk for a couple of hours with the fix in place.

The second patch below is a tiny one I had queued for post-2.2.1, and just moves the swap_cnt and swap_address task_struct fields to the mm_struct, preventing the VM from making multiple swap passes over a single mm if there are multiple threads using that mm.

--Stephen

----------------------------------------------------------------

main.c renamed from main.c.~1~ +2 -0
@@ -64,6 +64,7 @@
 static int init(void *);
 extern int bdflush(void *);
 extern int kswapd(void *);
+extern int kpiod(void *);
 extern void kswapd_setup(void);
 
 extern void init_IRQ(void);
@@ -1271,6 +1272,7 @@
 	kernel_thread(bdflush, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
 	/* Start the background pageout daemon. */
 	kswapd_setup();
+	kernel_thread(kpiod, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
 	kernel_thread(kswapd, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
 
 #if CONFIG_AP1000
filemap.c renamed from filemap.c.~1~ +154 -4

198 lines not on this page · raw message

----------------------------------------------------------------

linux/sched.h renamed from linux/sched.h.~1~ +4 -4
@@ -174,6 +174,8 @@
 	unsigned long rss, total_vm, locked_vm;
 	unsigned long def_flags;
 	unsigned long cpu_vm_mask;
+	unsigned long swap_cnt;	/* number of pages to swap on next pass */
+	unsigned long swap_address;
 	/*
 	 * This is an architecture-specific pointer: the portable
 	 * part of Linux does not know about any segments.
@@ -191,7 +193,7 @@
 		0, 0, 0, 				\
 		0, 0, 0, 0,				\
 		0, 0, 0,				\
-		0, 0, NULL }
+		0, 0, 0, 0, NULL }
 
 struct signal_struct {
 	atomic_t		count;
@@ -276,8 +278,6 @@
 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
 	unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap;
 	int swappable:1;
-	unsigned long swap_address;
-	unsigned long swap_cnt;		/* number of pages to swap on next pass */
 /* process credentials */
 	uid_t uid,euid,suid,fsuid;
 	gid_t gid,egid,sgid,fsgid;
@@ -361,7 +361,7 @@
 /* utime */	{0,0,0,0},0, \
 /* per CPU times */ {0, }, {0, }, \
 /* flt */	0,0,0,0,0,0, \
-/* swp */	0,0,0, \
+/* swp */	0, \
 /* process credentials */					\
 /* uid etc */	0,0,0,0,0,0,0,0,				\
 /* suppl grps*/ 0, {0,},					\
vmscan.c renamed from vmscan.c.~1~ +7 -7
@@ -202,7 +202,7 @@
 
 	do {
 		int result;
-		tsk->swap_address = address + PAGE_SIZE;
+		tsk->mm->swap_address = address + PAGE_SIZE;
 		result = try_to_swap_out(tsk, vma, address, pte, gfp_mask);
 		if (result)
 			return result;
@@ -274,7 +274,7 @@
 	/*
 	 * Go through process' page directory.
 	 */
-	address = p->swap_address;
+	address = p->mm->swap_address;
 
 	/*
 	 * Find the proper vm-area
@@ -296,8 +296,8 @@
 	}
 
 	/* We didn't find anything for the process */
-	p->swap_cnt = 0;
-	p->swap_address = 0;
+	p->mm->swap_cnt = 0;
+	p->mm->swap_address = 0;
 	return 0;
 }
 
@@ -345,9 +345,9 @@
 				continue;
 			/* Refresh swap_cnt? */
 			if (assign)
-				p->swap_cnt = p->mm->rss;
-			if (p->swap_cnt > max_cnt) {
-				max_cnt = p->swap_cnt;
+				p->mm->swap_cnt = p->mm->rss;
+			if (p->mm->swap_cnt > max_cnt) {
+				max_cnt = p->mm->swap_cnt;
 				pbest = p;
 			}
 		}

--
To unsubscribe, send a message with 'unsubscribe linux-mm my@address' in the body to [email protected]. For more info on Linux MM,
see: http://humbolt.geo.uu.nl/Linux-MM/

#1 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"ST" == Stephen C Tweedie <[email protected]> writes:

ST> The way to completely eliminate the problem is for the VM to avoid this ST> recursion in the first place. The patch below adds a new kpiod (page IO ST> daemon) thread to augment kswapd. All filemap page writes get queued to ST> this thread for IO rather than being executed in the context of the ST> caller, and the caller never blocks waiting for that IO to complete. In ST> other words, the caller can never fail eventually to release any vfs ST> locks currently held, so the page write is guaranteed to succeed ST> eventually. Even recursive allocations within the kpiod thread are ST> safe, since that just results in a queuing of the recursive page write:
ST> the actual IO is deferred until the kpiod thread loops to its next ST> request.

When I was experimenting with something similiar, (but as a much more common case) I had the problem of not waiting on pages that were being
written out if there was no other freable memory. Which caused
programs that should have continued via mapping to die due to memory allocation failure.

This isn't likely to be an issue except for programs that use file backed storage, exclusively, or nearly so.

I solved it with a wait_on_any_page routine that had a timeout, to prevent any chance of deadlock. Andreas work of adding a schedule_yield appears to be a complementary common case fix, of making it likely the i/o gets started.

Unless this becomes an issue we can probably just put this in the queue of things to think about. If the i/o queue was the common case I'd be adapting my code right now.

Eric

--
To unsubscribe, send a message with 'unsubscribe linux-mm my@address' in the body to [email protected]. For more info on Linux MM,
see: http://humbolt.geo.uu.nl/Linux-MM/