57
57
#define DM_BUFIO_INLINE_VECS 16
58
58
59
59
/*
60
- * Don't try to use kmem_cache_alloc for blocks larger than this.
60
+ * Don't try to use alloc_pages for blocks larger than this.
61
61
* For explanation, see alloc_buffer_data below.
62
62
*/
63
- #define DM_BUFIO_BLOCK_SIZE_SLAB_LIMIT (PAGE_SIZE >> 1)
64
63
#define DM_BUFIO_BLOCK_SIZE_GFP_LIMIT (PAGE_SIZE << (MAX_ORDER - 1))
65
64
66
65
/*
@@ -101,11 +100,11 @@ struct dm_bufio_client {
101
100
unsigned block_size ;
102
101
unsigned char sectors_per_block_bits ;
103
102
unsigned char pages_per_block_bits ;
104
- unsigned char blocks_per_page_bits ;
105
103
unsigned aux_size ;
106
104
void (* alloc_callback )(struct dm_buffer * );
107
105
void (* write_callback )(struct dm_buffer * );
108
106
107
+ struct kmem_cache * slab_cache ;
109
108
struct dm_io_client * dm_io ;
110
109
111
110
struct list_head reserved_buffers ;
@@ -172,19 +171,6 @@ struct dm_buffer {
172
171
173
172
/*----------------------------------------------------------------*/
174
173
175
- static struct kmem_cache * dm_bufio_caches [PAGE_SHIFT - SECTOR_SHIFT ];
176
-
177
- static inline int dm_bufio_cache_index (struct dm_bufio_client * c )
178
- {
179
- unsigned ret = c -> blocks_per_page_bits - 1 ;
180
-
181
- BUG_ON (ret >= ARRAY_SIZE (dm_bufio_caches ));
182
-
183
- return ret ;
184
- }
185
-
186
- #define DM_BUFIO_CACHE (c ) (dm_bufio_caches[dm_bufio_cache_index(c)])
187
-
188
174
#define dm_bufio_in_request () (!!current->bio_list)
189
175
190
176
static void dm_bufio_lock (struct dm_bufio_client * c )
@@ -384,9 +370,9 @@ static void __cache_size_refresh(void)
384
370
static void * alloc_buffer_data (struct dm_bufio_client * c , gfp_t gfp_mask ,
385
371
enum data_mode * data_mode )
386
372
{
387
- if (c -> block_size <= DM_BUFIO_BLOCK_SIZE_SLAB_LIMIT ) {
373
+ if (unlikely ( c -> slab_cache != NULL ) ) {
388
374
* data_mode = DATA_MODE_SLAB ;
389
- return kmem_cache_alloc (DM_BUFIO_CACHE ( c ) , gfp_mask );
375
+ return kmem_cache_alloc (c -> slab_cache , gfp_mask );
390
376
}
391
377
392
378
if (c -> block_size <= DM_BUFIO_BLOCK_SIZE_GFP_LIMIT &&
@@ -426,7 +412,7 @@ static void free_buffer_data(struct dm_bufio_client *c,
426
412
{
427
413
switch (data_mode ) {
428
414
case DATA_MODE_SLAB :
429
- kmem_cache_free (DM_BUFIO_CACHE ( c ) , data );
415
+ kmem_cache_free (c -> slab_cache , data );
430
416
break ;
431
417
432
418
case DATA_MODE_GET_FREE_PAGES :
@@ -1672,8 +1658,6 @@ struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsign
1672
1658
c -> sectors_per_block_bits = __ffs (block_size ) - SECTOR_SHIFT ;
1673
1659
c -> pages_per_block_bits = (__ffs (block_size ) >= PAGE_SHIFT ) ?
1674
1660
__ffs (block_size ) - PAGE_SHIFT : 0 ;
1675
- c -> blocks_per_page_bits = (__ffs (block_size ) < PAGE_SHIFT ?
1676
- PAGE_SHIFT - __ffs (block_size ) : 0 );
1677
1661
1678
1662
c -> aux_size = aux_size ;
1679
1663
c -> alloc_callback = alloc_callback ;
@@ -1699,20 +1683,15 @@ struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsign
1699
1683
goto bad_dm_io ;
1700
1684
}
1701
1685
1702
- mutex_lock (& dm_bufio_clients_lock );
1703
- if (c -> blocks_per_page_bits ) {
1704
- if (!DM_BUFIO_CACHE (c )) {
1705
- char name [26 ];
1706
- snprintf (name , sizeof name , "dm_bufio_cache-%u" , c -> block_size );
1707
- DM_BUFIO_CACHE (c ) = kmem_cache_create (name , c -> block_size , c -> block_size , 0 , NULL );
1708
- if (!DM_BUFIO_CACHE (c )) {
1709
- r = - ENOMEM ;
1710
- mutex_unlock (& dm_bufio_clients_lock );
1711
- goto bad ;
1712
- }
1686
+ if (block_size < PAGE_SIZE ) {
1687
+ char name [26 ];
1688
+ snprintf (name , sizeof name , "dm_bufio_cache-%u" , c -> block_size );
1689
+ c -> slab_cache = kmem_cache_create (name , c -> block_size , c -> block_size , 0 , NULL );
1690
+ if (!c -> slab_cache ) {
1691
+ r = - ENOMEM ;
1692
+ goto bad ;
1713
1693
}
1714
1694
}
1715
- mutex_unlock (& dm_bufio_clients_lock );
1716
1695
1717
1696
while (c -> need_reserved_buffers ) {
1718
1697
struct dm_buffer * b = alloc_buffer (c , GFP_KERNEL );
@@ -1747,6 +1726,7 @@ struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsign
1747
1726
list_del (& b -> lru_list );
1748
1727
free_buffer (b );
1749
1728
}
1729
+ kmem_cache_destroy (c -> slab_cache );
1750
1730
dm_io_client_destroy (c -> dm_io );
1751
1731
bad_dm_io :
1752
1732
mutex_destroy (& c -> lock );
@@ -1793,6 +1773,7 @@ void dm_bufio_client_destroy(struct dm_bufio_client *c)
1793
1773
for (i = 0 ; i < LIST_SIZE ; i ++ )
1794
1774
BUG_ON (c -> n_buffers [i ]);
1795
1775
1776
+ kmem_cache_destroy (c -> slab_cache );
1796
1777
dm_io_client_destroy (c -> dm_io );
1797
1778
mutex_destroy (& c -> lock );
1798
1779
kfree (c );
@@ -1896,8 +1877,6 @@ static int __init dm_bufio_init(void)
1896
1877
dm_bufio_allocated_vmalloc = 0 ;
1897
1878
dm_bufio_current_allocated = 0 ;
1898
1879
1899
- memset (& dm_bufio_caches , 0 , sizeof dm_bufio_caches );
1900
-
1901
1880
mem = (__u64 )mult_frac (totalram_pages - totalhigh_pages ,
1902
1881
DM_BUFIO_MEMORY_PERCENT , 100 ) << PAGE_SHIFT ;
1903
1882
@@ -1932,14 +1911,10 @@ static int __init dm_bufio_init(void)
1932
1911
static void __exit dm_bufio_exit (void )
1933
1912
{
1934
1913
int bug = 0 ;
1935
- int i ;
1936
1914
1937
1915
cancel_delayed_work_sync (& dm_bufio_work );
1938
1916
destroy_workqueue (dm_bufio_wq );
1939
1917
1940
- for (i = 0 ; i < ARRAY_SIZE (dm_bufio_caches ); i ++ )
1941
- kmem_cache_destroy (dm_bufio_caches [i ]);
1942
-
1943
1918
if (dm_bufio_client_count ) {
1944
1919
DMCRIT ("%s: dm_bufio_client_count leaked: %d" ,
1945
1920
__func__ , dm_bufio_client_count );
0 commit comments