|
18 | 18 | #include <linux/module.h> |
19 | 19 | #include <linux/rbtree.h> |
20 | 20 | #include <linux/stacktrace.h> |
| 21 | +#include <linux/jump_label.h> |
21 | 22 |
|
22 | 23 | #define DM_MSG_PREFIX "bufio" |
23 | 24 |
|
@@ -164,29 +165,31 @@ struct dm_buffer { |
164 | 165 | #endif |
165 | 166 | }; |
166 | 167 |
|
| 168 | +static DEFINE_STATIC_KEY_FALSE(no_sleep_enabled); |
| 169 | + |
167 | 170 | /*----------------------------------------------------------------*/ |
168 | 171 |
|
169 | 172 | #define dm_bufio_in_request() (!!current->bio_list) |
170 | 173 |
|
171 | 174 | static void dm_bufio_lock(struct dm_bufio_client *c) |
172 | 175 | { |
173 | | - if (c->no_sleep) |
| 176 | + if (static_branch_unlikely(&no_sleep_enabled) && c->no_sleep) |
174 | 177 | spin_lock_irqsave_nested(&c->spinlock, c->spinlock_flags, dm_bufio_in_request()); |
175 | 178 | else |
176 | 179 | mutex_lock_nested(&c->lock, dm_bufio_in_request()); |
177 | 180 | } |
178 | 181 |
|
179 | 182 | static int dm_bufio_trylock(struct dm_bufio_client *c) |
180 | 183 | { |
181 | | - if (c->no_sleep) |
| 184 | + if (static_branch_unlikely(&no_sleep_enabled) && c->no_sleep) |
182 | 185 | return spin_trylock_irqsave(&c->spinlock, c->spinlock_flags); |
183 | 186 | else |
184 | 187 | return mutex_trylock(&c->lock); |
185 | 188 | } |
186 | 189 |
|
187 | 190 | static void dm_bufio_unlock(struct dm_bufio_client *c) |
188 | 191 | { |
189 | | - if (c->no_sleep) |
| 192 | + if (static_branch_unlikely(&no_sleep_enabled) && c->no_sleep) |
190 | 193 | spin_unlock_irqrestore(&c->spinlock, c->spinlock_flags); |
191 | 194 | else |
192 | 195 | mutex_unlock(&c->lock); |
@@ -1760,8 +1763,10 @@ struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsign |
1760 | 1763 | c->alloc_callback = alloc_callback; |
1761 | 1764 | c->write_callback = write_callback; |
1762 | 1765 |
|
1763 | | - if (flags & DM_BUFIO_CLIENT_NO_SLEEP) |
| 1766 | + if (flags & DM_BUFIO_CLIENT_NO_SLEEP) { |
1764 | 1767 | c->no_sleep = true; |
| 1768 | + static_branch_inc(&no_sleep_enabled); |
| 1769 | + } |
1765 | 1770 |
|
1766 | 1771 | for (i = 0; i < LIST_SIZE; i++) { |
1767 | 1772 | INIT_LIST_HEAD(&c->lru[i]); |
@@ -1895,6 +1900,8 @@ void dm_bufio_client_destroy(struct dm_bufio_client *c) |
1895 | 1900 | kmem_cache_destroy(c->slab_buffer); |
1896 | 1901 | dm_io_client_destroy(c->dm_io); |
1897 | 1902 | mutex_destroy(&c->lock); |
| 1903 | + if (c->no_sleep) |
| 1904 | + static_branch_dec(&no_sleep_enabled); |
1898 | 1905 | kfree(c); |
1899 | 1906 | } |
1900 | 1907 | EXPORT_SYMBOL_GPL(dm_bufio_client_destroy); |
|
0 commit comments