From patchwork Thu Jul 14 08:07:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hui Zhu X-Patchwork-Id: 12917435 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2AE34C43334 for ; Thu, 14 Jul 2022 08:08:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 63578940195; Thu, 14 Jul 2022 04:08:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E476940134; Thu, 14 Jul 2022 04:08:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4AC8C940195; Thu, 14 Jul 2022 04:08:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 389C1940134 for ; Thu, 14 Jul 2022 04:08:39 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 04307353EC for ; Thu, 14 Jul 2022 08:08:38 +0000 (UTC) X-FDA: 79684978758.30.58844E0 Received: from out30-54.freemail.mail.aliyun.com (out30-54.freemail.mail.aliyun.com [115.124.30.54]) by imf15.hostedemail.com (Postfix) with ESMTP id 1DFAFA0078 for ; Thu, 14 Jul 2022 08:08:34 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R131e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046060;MF=teawaterz@linux.alibaba.com;NM=1;PH=DS;RN=10;SR=0;TI=SMTPD_---0VJIAcaG_1657786099; Received: from localhost(mailfrom:teawaterz@linux.alibaba.com fp:SMTPD_---0VJIAcaG_1657786099) by smtp.aliyun-inc.com; Thu, 14 Jul 2022 16:08:26 +0800 From: Hui Zhu To: minchan@kernel.org, ngupta@vflare.org, senozhatsky@chromium.org, axboe@kernel.dk, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-mm@kvack.org Cc: teawater@gmail.com, Hui Zhu Subject: [PATCH] zsmalloc: zs_malloc: Return ERR_PTR if fail Date: Thu, 14 Jul 2022 16:07:57 +0800 Message-Id: <20220714080757.12161-1-teawater@gmail.com> X-Mailer: git-send-email 2.17.1 ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of teawaterz@linux.alibaba.com designates 115.124.30.54 as permitted sender) smtp.mailfrom=teawaterz@linux.alibaba.com; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=gmail.com (policy=none) ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657786118; a=rsa-sha256; cv=none; b=okdZgfrye3Q+irJ4WEGUV008FZPBTPMg3AWiuhyF6IAa5JJ0dKoUVCtHepMF15FkF2n8QN U9a4rLxwsp/EiYdWm4XCh16EuEU6RPk8k2b8ly3Ree8prQy8qsnEQwj42aH6omTvV9i+8y YGP8mT6NomSotPSCS2Ww1+XMXB1zzCw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657786118; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=Kt0SN3amRDAdVEv3Kjw56WwYz+JYEHM4S7iigLoau5M=; b=mxWR2sptk/EqAIqCDUbaZLo3KaMfVl8Bo6vcza9w5agKxYOlmd/3Q+i04bCPP33qWr8kOd a9FhkZJuepgGi1hjtWYJcCrTbGlHIPUfBqw+Db/BXLUIg4Jp00zNcnEZBWWdfU/rvpcysP tNRwTF2lGIU897bhO6EqwyLxM9lS4EE= X-Rspamd-Queue-Id: 1DFAFA0078 Authentication-Results: imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of teawaterz@linux.alibaba.com designates 115.124.30.54 as permitted sender) smtp.mailfrom=teawaterz@linux.alibaba.com; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=gmail.com (policy=none) X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 1mqygfjmzzx5yam74gqo5z7w3nzpnqnh X-HE-Tag: 1657786114-832855 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Hui Zhu zs_malloc return 0 if it fail. zs_zpool_malloc will return -1 when zs_malloc return 0. But -1 make the return value unclear. For example: when zswap_frontswap_store call 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 change zs_malloc to return ERR_PTR if fail. It didn't just let zs_zpool_malloc -ENOMEM becaue zs_malloc has two types of failures. size is not OK return -EINVAL and memory alloc fail return -ENOMEM. Signed-off-by: Hui Zhu --- drivers/block/zram/zram_drv.c | 4 ++-- mm/zsmalloc.c | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index b8549c6..e1f10b5 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1387,9 +1387,9 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec, __GFP_HIGHMEM | __GFP_MOVABLE); - if (unlikely(!handle)) { + if (IS_ERR((void *)handle)) { zcomp_stream_put(zram->comp); - return -ENOMEM; + return PTR_ERR((void *)handle); } alloced_pages = zs_get_total_pages(zram->mem_pool); diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 5d5fc043..e70952f 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -399,7 +399,10 @@ static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp, unsigned long *handle) { *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) { @@ -1400,7 +1403,7 @@ static unsigned long obj_malloc(struct zs_pool *pool, * @gfp: gfp flags when allocating object * * 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. */ 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; if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE)) - return 0; + return (unsigned long)ERR_PTR(-EINVAL); handle = cache_alloc_handle(pool, gfp); if (!handle) - return 0; + return (unsigned long)ERR_PTR(-ENOMEM); /* extra space in chunk to keep the handle */ size += ZS_HANDLE_SIZE; @@ -1440,7 +1443,7 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp) zspage = alloc_zspage(pool, class, gfp); if (!zspage) { cache_free_handle(pool, handle); - return 0; + return (unsigned long)ERR_PTR(-ENOMEM); } spin_lock(&class->lock);