Skip to content

Commit d0d8da2

Browse files
sergey-senozhatskytorvalds
authored andcommitted
zsmalloc: require GFP in zs_malloc()
Pass GFP flags to zs_malloc() instead of using a fixed mask supplied to zs_create_pool(), so we can be more flexible, but, more importantly, we need this to switch zram to per-cpu compression streams -- zram will try to allocate handle with preemption disabled in a fast path and switch to a slow path (using different gfp mask) if the fast one has failed. Apart from that, this also align zs_malloc() interface with zspool/zbud. [[email protected]: pass GFP flags to zs_malloc() instead of using a fixed mask] Link: http://lkml.kernel.org/r/20160429150942.GA637@swordfish Link: http://lkml.kernel.org/r/20160429150942.GA637@swordfish Signed-off-by: Sergey Senozhatsky <[email protected]> Acked-by: Minchan Kim <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 1ee4716 commit d0d8da2

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

drivers/block/zram/zram_drv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ static struct zram_meta *zram_meta_alloc(char *pool_name, u64 disksize)
514514
goto out_error;
515515
}
516516

517-
meta->mem_pool = zs_create_pool(pool_name, GFP_NOIO | __GFP_HIGHMEM);
517+
meta->mem_pool = zs_create_pool(pool_name);
518518
if (!meta->mem_pool) {
519519
pr_err("Error creating memory pool\n");
520520
goto out_error;
@@ -717,7 +717,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
717717
src = uncmem;
718718
}
719719

720-
handle = zs_malloc(meta->mem_pool, clen);
720+
handle = zs_malloc(meta->mem_pool, clen, GFP_NOIO | __GFP_HIGHMEM);
721721
if (!handle) {
722722
pr_err("Error allocating memory for compressed page: %u, size=%zu\n",
723723
index, clen);

include/linux/zsmalloc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ struct zs_pool_stats {
4141

4242
struct zs_pool;
4343

44-
struct zs_pool *zs_create_pool(const char *name, gfp_t flags);
44+
struct zs_pool *zs_create_pool(const char *name);
4545
void zs_destroy_pool(struct zs_pool *pool);
4646

47-
unsigned long zs_malloc(struct zs_pool *pool, size_t size);
47+
unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t flags);
4848
void zs_free(struct zs_pool *pool, unsigned long obj);
4949

5050
void *zs_map_object(struct zs_pool *pool, unsigned long handle,

mm/zsmalloc.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ struct zs_pool {
247247
struct size_class **size_class;
248248
struct kmem_cache *handle_cachep;
249249

250-
gfp_t flags; /* allocation flags used when growing pool */
251250
atomic_long_t pages_allocated;
252251

253252
struct zs_pool_stats stats;
@@ -295,10 +294,10 @@ static void destroy_handle_cache(struct zs_pool *pool)
295294
kmem_cache_destroy(pool->handle_cachep);
296295
}
297296

298-
static unsigned long alloc_handle(struct zs_pool *pool)
297+
static unsigned long alloc_handle(struct zs_pool *pool, gfp_t gfp)
299298
{
300299
return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
301-
pool->flags & ~__GFP_HIGHMEM);
300+
gfp & ~__GFP_HIGHMEM);
302301
}
303302

304303
static void free_handle(struct zs_pool *pool, unsigned long handle)
@@ -324,7 +323,12 @@ static void *zs_zpool_create(const char *name, gfp_t gfp,
324323
const struct zpool_ops *zpool_ops,
325324
struct zpool *zpool)
326325
{
327-
return zs_create_pool(name, gfp);
326+
/*
327+
* Ignore global gfp flags: zs_malloc() may be invoked from
328+
* different contexts and its caller must provide a valid
329+
* gfp mask.
330+
*/
331+
return zs_create_pool(name);
328332
}
329333

330334
static void zs_zpool_destroy(void *pool)
@@ -335,7 +339,7 @@ static void zs_zpool_destroy(void *pool)
335339
static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
336340
unsigned long *handle)
337341
{
338-
*handle = zs_malloc(pool, size);
342+
*handle = zs_malloc(pool, size, gfp);
339343
return *handle ? 0 : -1;
340344
}
341345
static void zs_zpool_free(void *pool, unsigned long handle)
@@ -1391,7 +1395,7 @@ static unsigned long obj_malloc(struct size_class *class,
13911395
* otherwise 0.
13921396
* Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
13931397
*/
1394-
unsigned long zs_malloc(struct zs_pool *pool, size_t size)
1398+
unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
13951399
{
13961400
unsigned long handle, obj;
13971401
struct size_class *class;
@@ -1400,7 +1404,7 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size)
14001404
if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
14011405
return 0;
14021406

1403-
handle = alloc_handle(pool);
1407+
handle = alloc_handle(pool, gfp);
14041408
if (!handle)
14051409
return 0;
14061410

@@ -1413,7 +1417,7 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size)
14131417

14141418
if (!first_page) {
14151419
spin_unlock(&class->lock);
1416-
first_page = alloc_zspage(class, pool->flags);
1420+
first_page = alloc_zspage(class, gfp);
14171421
if (unlikely(!first_page)) {
14181422
free_handle(pool, handle);
14191423
return 0;
@@ -1878,7 +1882,7 @@ static int zs_register_shrinker(struct zs_pool *pool)
18781882
* On success, a pointer to the newly created pool is returned,
18791883
* otherwise NULL.
18801884
*/
1881-
struct zs_pool *zs_create_pool(const char *name, gfp_t flags)
1885+
struct zs_pool *zs_create_pool(const char *name)
18821886
{
18831887
int i;
18841888
struct zs_pool *pool;
@@ -1948,8 +1952,6 @@ struct zs_pool *zs_create_pool(const char *name, gfp_t flags)
19481952
prev_class = class;
19491953
}
19501954

1951-
pool->flags = flags;
1952-
19531955
if (zs_pool_stat_create(pool, name))
19541956
goto err;
19551957

0 commit comments

Comments
 (0)