Deadlock
Outline Deadlock Examples, Conditions, Strategies Deadlock Prevention Deadlock Avoidance Banker s algorithm
Deadlocks Thread 1: lock(l1); lock(l2);! Thread 2: lock(l2); lock(l1);
Examples?
Examples? GFP_IO flag in Linux Kernel Memory allocator Causes get_free_page() calls shm_swap() to pageout. What if shm_swap() needs memory?
Examples? GFP_IO flag in Linux Kernel Memory allocator Causes get_free_page() calls shm_swap() to pageout. What if shm_swap() needs memory? Vector v1, v2; v1.addall(v2); addall() needs to be thread safe. what happens when v2.addall(v1) is called too?
Conditions
Conditions Mutual Exclusion threads claim exclusive control
Conditions Mutual Exclusion threads claim exclusive control Hold-and-Wait resources acquired progressively
Conditions Mutual Exclusion threads claim exclusive control Hold-and-Wait resources acquired progressively No preemption resources cannot be forcibly taken away
Conditions Mutual Exclusion threads claim exclusive control Hold-and-Wait resources acquired progressively No preemption resources cannot be forcibly taken away Circular Wait circular chain such that threads wait on resources held by another
Dining Philosophers http://upload.wikimedia.org/wikipedia/commons/6/6a/dining_philosophers.png
Dining Philosophers Philosopher has sole control over picked fork (mutual exclusion) http://upload.wikimedia.org/wikipedia/commons/6/6a/dining_philosophers.png
Dining Philosophers Philosopher has sole control over picked fork (mutual exclusion) Philosopher picks left fork then picks right (hold and wait) http://upload.wikimedia.org/wikipedia/commons/6/6a/dining_philosophers.png
Dining Philosophers Philosopher has sole control over picked fork (mutual exclusion) Philosopher picks left fork then picks right (hold and wait) Fork cannot be taken away from a philosopher (no preemption) http://upload.wikimedia.org/wikipedia/commons/6/6a/dining_philosophers.png
Dining Philosophers Philosopher has sole control over picked fork (mutual exclusion) Philosopher picks left fork then picks right (hold and wait) Fork cannot be taken away from a philosopher (no preemption) Once all philosophers pick a fork, each of them wait on the philosopher at his right to release the fork (circular wait) http://upload.wikimedia.org/wikipedia/commons/6/6a/dining_philosophers.png
Strategies
Ignore - Ostrich algorithm Strategies
Strategies Ignore - Ostrich algorithm Detection & Recovery
Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph
Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph
Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph
Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph Identify deadlocked processes
Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph Identify deadlocked processes rollback, preempt, kill
Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph Identify deadlocked processes rollback, preempt, kill Prevention
Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph Identify deadlocked processes rollback, preempt, kill Prevention Negate any one of the deadlock conditions
Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph Identify deadlocked processes rollback, preempt, kill Prevention Negate any one of the deadlock conditions Avoidance
Strategies Ignore - Ostrich algorithm Detection & Recovery Build Dependency graph Identify deadlocked processes rollback, preempt, kill Prevention Negate any one of the deadlock conditions Avoidance Carefully allocate resources
Prevention - Mutual Exclusion Never assign resource exclusively to a process Not really realistic. ex. printers can t print two documents simultaneously Use daemon to exclusively access printer? Deadlock free? Use hardware instructions, compare and swap
Prevention - Hold & Wait down(global_sem); down(get_left(i)); down(get_right(i)); up(global_sem);! -! down(global_sem); up(get_left(i)); up(get_right(i)); up(global_sem);
Prevention - Hold & Wait Ensure that all resources are acquired at once down(global_sem); down(get_left(i)); down(get_right(i)); up(global_sem);! -! down(global_sem); up(get_left(i)); up(get_right(i)); up(global_sem);
Prevention - Hold & Wait Ensure that all resources are acquired at once Is this feasible? down(global_sem); down(get_left(i)); down(get_right(i)); up(global_sem);! -! down(global_sem); up(get_left(i)); up(get_right(i)); up(global_sem);
Prevention - Hold & Wait Ensure that all resources are acquired at once Is this feasible? Know all resources in advance? down(global_sem); down(get_left(i)); down(get_right(i)); up(global_sem);! -! down(global_sem); up(get_left(i)); up(get_right(i)); up(global_sem);
Prevention - No Preemption
Prevention - No Preemption Take away resources owned by thread
Prevention - No Preemption Take away resources owned by thread Use trylock(), non blocking call in getting the lock.
Prevention - No Preemption Take away resources owned by thread Use trylock(), non blocking call in getting the lock. top: lock(get_left(i); if (trylock(get_right(i) == -1) { unlock(get_left(i)); goto top; }
Prevention - No Preemption Take away resources owned by thread Use trylock(), non blocking call in getting the lock. top: lock(get_left(i); if (trylock(get_right(i) == -1) { unlock(get_left(i)); goto top; } Lifelock?
Prevention - Circular Wait http://lxr.free-electrons.com/source/mm/filemap.c
Prevention - Circular Wait Enforce ordering in Locking http://lxr.free-electrons.com/source/mm/filemap.c
Prevention - Circular Wait Enforce ordering in Locking enumerate all locks in the system? http://lxr.free-electrons.com/source/mm/filemap.c
Prevention - Circular Wait Enforce ordering in Locking enumerate all locks in the system? 62 /* 63 * Lock ordering: 64 * 65 * ->i_mmap_rwsem (truncate_pagecache) 66 * ->private_lock ( free_pte-> set_page_dirty_buffers) 67 * ->swap_lock (exclusive_swap_page, others) 68 * ->mapping->tree_lock 69 * 70 * ->i_mutex 71 * ->i_mmap_rwsem (truncate->unmap_mapping_range) 72 * 73 * ->mmap_sen 74 * ->i_mmap_rwsem 75 * ->page_table_lock or pte_lock (various, mainly in memory.c) 76 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock) 77 * 78 * ->mmap_sem 79 * ->lock_page (access_process_vm) 80 * 81 * ->i_mutex (generic_perform_write) 82 * ->mmap_sem (fault_in_pages_readable->do_page_fault) 83 * 84 * bdi->wb.list_lock 85 * sb_lock (fs/fs-writeback.c) 86 * ->mapping->tree_lock ( sync_single_inode) 87 * 88 * ->i_mmap_rwsem 89 * ->anon_vma.lock (vma_adjust) 90 * 91 * ->anon_vma.lock 92 * ->page_table_lock or pte_lock (anon_vma_prepare and various) 93 * 94 * ->page_table_lock or pte_lock 95 * ->swap_lock (try_to_unmap_one) http://lxr.free-electrons.com/source/mm/filemap.c
Prevention - Circular Wait Enforce ordering in Locking enumerate all locks in the system? Is it feasible? 62 /* 63 * Lock ordering: 64 * 65 * ->i_mmap_rwsem (truncate_pagecache) 66 * ->private_lock ( free_pte-> set_page_dirty_buffers) 67 * ->swap_lock (exclusive_swap_page, others) 68 * ->mapping->tree_lock 69 * 70 * ->i_mutex 71 * ->i_mmap_rwsem (truncate->unmap_mapping_range) 72 * 73 * ->mmap_sen 74 * ->i_mmap_rwsem 75 * ->page_table_lock or pte_lock (various, mainly in memory.c) 76 * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock) 77 * 78 * ->mmap_sem 79 * ->lock_page (access_process_vm) 80 * 81 * ->i_mutex (generic_perform_write) 82 * ->mmap_sem (fault_in_pages_readable->do_page_fault) 83 * 84 * bdi->wb.list_lock 85 * sb_lock (fs/fs-writeback.c) 86 * ->mapping->tree_lock ( sync_single_inode) 87 * 88 * ->i_mmap_rwsem 89 * ->anon_vma.lock (vma_adjust) 90 * 91 * ->anon_vma.lock 92 * ->page_table_lock or pte_lock (anon_vma_prepare and various) 93 * 94 * ->page_table_lock or pte_lock 95 * ->swap_lock (try_to_unmap_one) http://lxr.free-electrons.com/source/mm/filemap.c
Deadlock Avoidance Banker s algorithm - Dijkstra (1965) Safe and unsafe states Checks to see if any request would yield a safe state If not, the request is denied
Banker s algorithm
Banker s algorithm Using Vector E (Existing Resources) and P (Possessed Resources) build
Banker s algorithm Using Vector E (Existing Resources) and P (Possessed Resources) build A: Available resources
Banker s algorithm Using Vector E (Existing Resources) and P (Possessed Resources) build A: Available resources Pick a row whose resource needs are smaller or equal to A. If not found, system would eventually deadlock as resources cannot be found.
Banker s algorithm Using Vector E (Existing Resources) and P (Possessed Resources) build A: Available resources Pick a row whose resource needs are smaller or equal to A. If not found, system would eventually deadlock as resources cannot be found. Assuming the process makes a maximum allocation of its resources, consider the process as terminated and add its resources to the A vector
Banker s algorithm Using Vector E (Existing Resources) and P (Possessed Resources) build A: Available resources Pick a row whose resource needs are smaller or equal to A. If not found, system would eventually deadlock as resources cannot be found. Assuming the process makes a maximum allocation of its resources, consider the process as terminated and add its resources to the A vector Repeat until all processes are terminated.
Problem #1 Is the State Safe? E = (6, 3, 4, 2) Allocated: A B C D P0 3 0 1 1 P1 0 1 0 0 P2 1 1 1 0 P3 1 1 0 1 P4 0 0 0 0 Needed: A B C D P0 1 1 0 0 P1 0 1 1 2 P2 3 1 0 0 P3 0 0 1 0 P4 2 1 1 0
Problem #2 Is the state safe? E = (10, 5, 7) Allocated: A B C P0 0 1 0 P1 2 0 0 P2 3 0 2 P3 2 1 1 P4 0 0 2 Max: A B C P0 7 5 3 P1 3 2 2 P2 9 0 2 P3 2 2 2 P4 4 3 3
Problem #3 Is the following state safe? E = (6, 5, 7, 6) Allocated: A B C D Max: A B C D P0 1 2 2 1 P0 3 3 2 2 P1 1 0 3 3 P1 1 2 3 4 P2 1 2 1 0 P2 1 3 5 0
Done!!!