Skip to content

Commit b32d458

Browse files
nhukcMike Snitzer
authored andcommitted
dm bufio: Add DM_BUFIO_CLIENT_NO_SLEEP flag
Add an optional flag that ensures dm_bufio_client does not sleep (primary focus is to service dm_bufio_get without sleeping). This allows the dm-bufio cache to be queried from interrupt context. To ensure that dm-bufio does not sleep, dm-bufio must use a spinlock instead of a mutex. Additionally, to avoid deadlocks, special care must be taken so that dm-bufio does not sleep while holding the spinlock. But again: the scope of this no_sleep is initially confined to dm_bufio_get, so __alloc_buffer_wait_no_callback is _not_ changed to avoid sleeping because __bufio_new avoids allocation for NF_GET. Signed-off-by: Nathan Huckleberry <[email protected]> Signed-off-by: Mike Snitzer <[email protected]>
1 parent 0fcb100 commit b32d458

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

drivers/md/dm-bufio.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
*/
8282
struct dm_bufio_client {
8383
struct mutex lock;
84+
spinlock_t spinlock;
85+
unsigned long spinlock_flags;
8486

8587
struct list_head lru[LIST_SIZE];
8688
unsigned long n_buffers[LIST_SIZE];
@@ -90,6 +92,7 @@ struct dm_bufio_client {
9092
s8 sectors_per_block_bits;
9193
void (*alloc_callback)(struct dm_buffer *);
9294
void (*write_callback)(struct dm_buffer *);
95+
bool no_sleep;
9396

9497
struct kmem_cache *slab_buffer;
9598
struct kmem_cache *slab_cache;
@@ -167,17 +170,26 @@ struct dm_buffer {
167170

168171
static void dm_bufio_lock(struct dm_bufio_client *c)
169172
{
170-
mutex_lock_nested(&c->lock, dm_bufio_in_request());
173+
if (c->no_sleep)
174+
spin_lock_irqsave_nested(&c->spinlock, c->spinlock_flags, dm_bufio_in_request());
175+
else
176+
mutex_lock_nested(&c->lock, dm_bufio_in_request());
171177
}
172178

173179
static int dm_bufio_trylock(struct dm_bufio_client *c)
174180
{
175-
return mutex_trylock(&c->lock);
181+
if (c->no_sleep)
182+
return spin_trylock_irqsave(&c->spinlock, c->spinlock_flags);
183+
else
184+
return mutex_trylock(&c->lock);
176185
}
177186

178187
static void dm_bufio_unlock(struct dm_bufio_client *c)
179188
{
180-
mutex_unlock(&c->lock);
189+
if (c->no_sleep)
190+
spin_unlock_irqrestore(&c->spinlock, c->spinlock_flags);
191+
else
192+
mutex_unlock(&c->lock);
181193
}
182194

183195
/*----------------------------------------------------------------*/
@@ -1748,12 +1760,16 @@ struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsign
17481760
c->alloc_callback = alloc_callback;
17491761
c->write_callback = write_callback;
17501762

1763+
if (flags & DM_BUFIO_CLIENT_NO_SLEEP)
1764+
c->no_sleep = true;
1765+
17511766
for (i = 0; i < LIST_SIZE; i++) {
17521767
INIT_LIST_HEAD(&c->lru[i]);
17531768
c->n_buffers[i] = 0;
17541769
}
17551770

17561771
mutex_init(&c->lock);
1772+
spin_lock_init(&c->spinlock);
17571773
INIT_LIST_HEAD(&c->reserved_buffers);
17581774
c->need_reserved_buffers = reserved_buffers;
17591775

include/linux/dm-bufio.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
struct dm_bufio_client;
1818
struct dm_buffer;
1919

20+
/*
21+
* Flags for dm_bufio_client_create
22+
*/
23+
#define DM_BUFIO_CLIENT_NO_SLEEP 0x1
24+
2025
/*
2126
* Create a buffered IO cache on a given device
2227
*/

0 commit comments

Comments
 (0)