dm crypt: fix mempool deadlock

This patch fixes a possible deadlock in dm-crypt's mempool use.

Currently, dm-crypt reserves a mempool of MIN_BIO_PAGES reserved pages.
It allocates first MIN_BIO_PAGES with non-failing allocation (the allocation
cannot fail and waits until the mempool is refilled). Further pages are
allocated with different gfp flags that allow failing.

Because allocations may be done in parallel, this code can deadlock. Example:
There are two processes, each tries to allocate MIN_BIO_PAGES and the processes
run simultaneously.
It may end up in a situation where each process allocates (MIN_BIO_PAGES / 2)
pages. The mempool is exhausted. Each process waits for more pages to be freed
to the mempool, which never happens.

To avoid this deadlock scenario, this patch changes the code so that only
the first page is allocated with non-failing gfp mask. Allocation of further
pages may fail.

Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Cc: stable@kernel.org
Signed-off-by: Milan Broz <mbroz@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
This commit is contained in:
Mikulas Patocka 2012-03-28 18:41:22 +01:00 committed by Alasdair G Kergon
parent aadbe266f2
commit aeb2deae26
1 changed files with 4 additions and 6 deletions

View File

@ -176,7 +176,6 @@ struct crypt_config {
#define MIN_IOS 16 #define MIN_IOS 16
#define MIN_POOL_PAGES 32 #define MIN_POOL_PAGES 32
#define MIN_BIO_PAGES 8
static struct kmem_cache *_crypt_io_pool; static struct kmem_cache *_crypt_io_pool;
@ -848,12 +847,11 @@ static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
} }
/* /*
* if additional pages cannot be allocated without waiting, * If additional pages cannot be allocated without waiting,
* return a partially allocated bio, the caller will then try * return a partially-allocated bio. The caller will then try
* to allocate additional bios while submitting this partial bio * to allocate more bios while submitting this partial bio.
*/ */
if (i == (MIN_BIO_PAGES - 1)) gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
len = (size > PAGE_SIZE) ? PAGE_SIZE : size; len = (size > PAGE_SIZE) ? PAGE_SIZE : size;