Commit c7e6f17b authored by Hui Zhu's avatar Hui Zhu Committed by akpm

zsmalloc: zs_malloc: return ERR_PTR on failure

zs_malloc returns 0 if it fails.  zs_zpool_malloc will return -1 when
zs_malloc return 0.  But -1 makes the return value unclear.

For example, when zswap_frontswap_store calls zs_malloc through
zs_zpool_malloc, it will return -1 to its caller.  The other return value
is -EINVAL, -ENODEV or something else.

This commit changes zs_malloc to return ERR_PTR on failure.  It didn't
just let zs_zpool_malloc return -ENOMEM becaue zs_malloc has two types of
failure:

- size is not OK return -EINVAL
- memory alloc fail return -ENOMEM.

Link: https://lkml.kernel.org/r/20220714080757.12161-1-teawater@gmail.comSigned-off-by: default avatarHui Zhu <teawater@antgroup.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Nitin Gupta <ngupta@vflare.org>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Jens Axboe <axboe@kernel.dk>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent fef3e906
...@@ -1389,9 +1389,9 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec, ...@@ -1389,9 +1389,9 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
__GFP_HIGHMEM | __GFP_HIGHMEM |
__GFP_MOVABLE); __GFP_MOVABLE);
if (unlikely(!handle)) { if (IS_ERR((void *)handle)) {
zcomp_stream_put(zram->comp); zcomp_stream_put(zram->comp);
return -ENOMEM; return PTR_ERR((void *)handle);
} }
alloced_pages = zs_get_total_pages(zram->mem_pool); alloced_pages = zs_get_total_pages(zram->mem_pool);
......
...@@ -399,7 +399,10 @@ static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp, ...@@ -399,7 +399,10 @@ static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
unsigned long *handle) unsigned long *handle)
{ {
*handle = zs_malloc(pool, size, gfp); *handle = zs_malloc(pool, size, gfp);
return *handle ? 0 : -1;
if (IS_ERR((void *)(*handle)))
return PTR_ERR((void *)*handle);
return 0;
} }
static void zs_zpool_free(void *pool, unsigned long handle) static void zs_zpool_free(void *pool, unsigned long handle)
{ {
...@@ -1400,7 +1403,7 @@ static unsigned long obj_malloc(struct zs_pool *pool, ...@@ -1400,7 +1403,7 @@ static unsigned long obj_malloc(struct zs_pool *pool,
* @gfp: gfp flags when allocating object * @gfp: gfp flags when allocating object
* *
* On success, handle to the allocated object is returned, * On success, handle to the allocated object is returned,
* otherwise 0. * otherwise an ERR_PTR().
* Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail. * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
*/ */
unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp) unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
...@@ -1411,11 +1414,11 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp) ...@@ -1411,11 +1414,11 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
struct zspage *zspage; struct zspage *zspage;
if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE)) if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
return 0; return (unsigned long)ERR_PTR(-EINVAL);
handle = cache_alloc_handle(pool, gfp); handle = cache_alloc_handle(pool, gfp);
if (!handle) if (!handle)
return 0; return (unsigned long)ERR_PTR(-ENOMEM);
/* extra space in chunk to keep the handle */ /* extra space in chunk to keep the handle */
size += ZS_HANDLE_SIZE; size += ZS_HANDLE_SIZE;
...@@ -1440,7 +1443,7 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp) ...@@ -1440,7 +1443,7 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
zspage = alloc_zspage(pool, class, gfp); zspage = alloc_zspage(pool, class, gfp);
if (!zspage) { if (!zspage) {
cache_free_handle(pool, handle); cache_free_handle(pool, handle);
return 0; return (unsigned long)ERR_PTR(-ENOMEM);
} }
spin_lock(&class->lock); spin_lock(&class->lock);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment