Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5842778rdb; Thu, 14 Dec 2023 00:40:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IGoGlmUvK0XSYeUJUb1yrDO28UzLBcB8fyzwh9K+ri35i1VIxC7/7F1LqUUgRSWuqoV6XVs X-Received: by 2002:a05:6808:3a0d:b0:3b9:dbc5:2fe8 with SMTP id gr13-20020a0568083a0d00b003b9dbc52fe8mr12940088oib.47.1702543250559; Thu, 14 Dec 2023 00:40:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702543250; cv=none; d=google.com; s=arc-20160816; b=ze1jbcrpQLyn6LeWNnlqBjJIQ3JmTaC0nPIOS4h1TlqSHXStKFPVZ20XdY06TiJn3v x4wO9Ao+morBJ8uZmfeswSCbAdqJXf5Wmmv33cU1kAInJFIiH+tc6YJCVSjsSg73hAC4 pUlSE4XYONzXPZLxLEE9LRJXBInh3wAFqAyY1JGaxac5bXdr5vhHB7oRHMRWGWWlkLY5 qNi/0UwAJB9BoQU8pYFwc0G2yzbKmnoychybGEwA3WHrjg3MAdsMDAey2k4pBTxu9wEx MHIND0JbAnkmboy9/GPdQAOjeJiJknSD9Zh1kNbCSkFwIp77f1HiGJiDto18inIgLPCD ex8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=Vdfmtqpl6+z8AKOK2CqI57RfC71PjxknUkY/QD/IhWk=; fh=+IL3KclI422sl6UODC4niLBdQJ0X0rSxjvjm0nyS3ME=; b=APPWkLPfk8F79Raf7MAoMYPDsQl9WJG51wjEBjXuj2FdwCmoqMZctnMosJVCQa+WKT DMn6B2KMJo+XuHNf/+vjofY9HAAFDQdhbv7Bmg0JHrB+EGdJJMeOYklnhV7uit+mMbDO y6X/gP4hR6I/FkYwtnXEPrSZ5AHu945pMol7lfrAwiZFEh/LVofhe0HusRedPmOksPud A1PqlmYNDtC4iA6wH4mCuF3FqmGlrcjnH8SC4DB4X5p2id0qmfVl6kf0OQifOGHHnDK0 YwvToti17O+Lk8oJqAXsULsmi3ktjXEvTomVmqbPfbG/bSB+5+v1nAwOt/c6DN1XQPDi SeTw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id w69-20020a638248000000b005c6ed253558si10125451pgd.195.2023.12.14.00.40.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 00:40:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id C52DE8076D07; Thu, 14 Dec 2023 00:40:12 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234472AbjLNIj5 (ORCPT + 99 others); Thu, 14 Dec 2023 03:39:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234418AbjLNIj4 (ORCPT ); Thu, 14 Dec 2023 03:39:56 -0500 Received: from zju.edu.cn (mail.zju.edu.cn [61.164.42.155]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E2616E3 for ; Thu, 14 Dec 2023 00:40:00 -0800 (PST) Received: from localhost.localdomain (unknown [10.190.71.46]) by mail-app3 (Coremail) with SMTP id cC_KCgCHj_JMv3plIqvdAA--.15533S4; Thu, 14 Dec 2023 16:39:57 +0800 (CST) From: Dinghao Liu To: dinghao.liu@zju.edu.cn Cc: Dan Williams , Vishal Verma , Dave Jiang , Ira Weiny , nvdimm@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH] [v2] nvdimm-btt: simplify code with the scope based resource management Date: Thu, 14 Dec 2023 16:39:19 +0800 Message-Id: <20231214083919.22218-1-dinghao.liu@zju.edu.cn> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: cC_KCgCHj_JMv3plIqvdAA--.15533S4 X-Coremail-Antispam: 1UD129KBjvJXoW7uF45Gr1xZw4DArWfGw13CFg_yoW8Wr1xpF s5A34kArWDJF1xuFyDAw4xZry3Ga1fAa4UKryj9393ZrWaqw1jqrZYyFyS9rykuFWxZryj g3yUtwnIkFW5Ar7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkI1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l1IIY67AE w4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2 IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVWxJr0_GcWl84ACjcxK6I8E 87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1le2I262IYc4CY6c 8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_ Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwI xGrwACjI8F5VA0II8E6IAqYI8I648v4I1l42xK82IYc2Ij64vIr41l42xK82IY6x8ErcxF aVAv8VW8uw4UJr1UMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr 4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxG rwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8Jw CI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2 z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUbXdbUUUUUU== X-CM-SenderInfo: qrrzjiaqtzq6lmxovvfxof0/1tbiAgIIBmV4LpY5mAAKsA X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Thu, 14 Dec 2023 00:40:13 -0800 (PST) Use the scope based resource management (defined in linux/cleanup.h) to automate resource lifetime control on struct btt_sb *super in discover_arenas(). Signed-off-by: Dinghao Liu --- Changelog: v2: Set the __free attribute before kzalloc. --- drivers/nvdimm/btt.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c index d5593b0dc700..32a9e2f543c5 100644 --- a/drivers/nvdimm/btt.c +++ b/drivers/nvdimm/btt.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "btt.h" #include "nd.h" @@ -847,23 +848,20 @@ static int discover_arenas(struct btt *btt) { int ret = 0; struct arena_info *arena; - struct btt_sb *super; size_t remaining = btt->rawsize; u64 cur_nlba = 0; size_t cur_off = 0; int num_arenas = 0; - super = kzalloc(sizeof(*super), GFP_KERNEL); + struct btt_sb *super __free(kfree) = kzalloc(sizeof(*super), GFP_KERNEL); if (!super) return -ENOMEM; while (remaining) { /* Alloc memory for arena */ arena = alloc_arena(btt, 0, 0, 0); - if (!arena) { - ret = -ENOMEM; - goto out_super; - } + if (!arena) + return -ENOMEM; arena->infooff = cur_off; ret = btt_info_read(arena, super); @@ -919,14 +917,11 @@ static int discover_arenas(struct btt *btt) btt->nlba = cur_nlba; btt->init_state = INIT_READY; - kfree(super); return ret; out: kfree(arena); free_arenas(btt); - out_super: - kfree(super); return ret; } -- 2.17.1