Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp6298199rwn; Tue, 13 Sep 2022 01:59:47 -0700 (PDT) X-Google-Smtp-Source: AA6agR4ClsXGWpTXhCl7tfiD7H131xPtfqU8VBx9bPxi2RHMad9ewgen/9gH9weNGz9lkBkHMG8l X-Received: by 2002:a05:6402:40cb:b0:451:2b71:9940 with SMTP id z11-20020a05640240cb00b004512b719940mr14708380edb.237.1663059587013; Tue, 13 Sep 2022 01:59:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663059587; cv=none; d=google.com; s=arc-20160816; b=w86+1+Bzkk79zaWKLKfgdFhrA0fGw2oi7fl/RsNciBO2MHCmTLPm0Xnhvc89vaAbVj CiUOtDJ7KVoinSVZWJCM8eoM6BzHRmyefwVgpluLdOWiNTSCDNlnEAeOcnuLHXUbjESu BKDIkZ0IsWZEZ4yBNBNJCGbAXpixS3lmeM0O19wTzZnxWIg9Myb/9FD1fZZeKr4XDJnz m9hHaekpGXjQ5eN5qVpFSM8vycxMj7Nf2/hhLbgfmBJ+ZRJwusOVKkzbA7K2eb1oElZd jJrJjHFBabv2Rh/HaYWr6EqxTZ/aLUrrz5iGXfnKkvomD85kTsd+qlNqW+O0UffCVs+E 0IQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=12hz0tpttmw/unlnts/rOvvXWZy1NAgJEPEchdnJEHU=; b=Qv8h3C19Q4cMeMThUFa6XhZwTQAsZbgvLfMJrlxz5FuoiukDMhdcj6SH1G/t117HuW N83d+7+Iq+nfI5MeC6958J9dWMxSmIbtNA485SwyGydAN2iPPCI9nbfQFTCaZb9eO90Z PsVp4JcnUG1f99UYmFFQSK/WT/ShjbskrHyLOqXXmswoMEtmomwj+pmULbNjddhxUNbJ g5VO6SZr9++PbE+3DGIB3Udjhqvd4H4hzKdpRbAfQlcKCdJLqDQyLF8KDJKXUsHvncw2 MXUA/BqkYifMWDMEH/aaDb8aaHZWlD6kdiHJAlrHYxpLOwE+Cjsd9k/HRdvL+gux+pk2 JPIA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f20-20020a056402195400b0045196fd6a9dsi5301637edz.477.2022.09.13.01.59.21; Tue, 13 Sep 2022 01:59:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231334AbiIMIFU (ORCPT + 99 others); Tue, 13 Sep 2022 04:05:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231312AbiIMIFS (ORCPT ); Tue, 13 Sep 2022 04:05:18 -0400 Received: from cstnet.cn (smtp21.cstnet.cn [159.226.251.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BF51344569; Tue, 13 Sep 2022 01:05:14 -0700 (PDT) Received: from localhost.localdomain (unknown [124.16.138.126]) by APP-01 (Coremail) with SMTP id qwCowAAnLhq0OSBj9BMuAg--.30824S2; Tue, 13 Sep 2022 16:05:09 +0800 (CST) From: Jiasheng Jiang To: quic_jjohnson@quicinc.co, agross@kernel.org, bjorn.andersson@linaro.org, konrad.dybcio@somainline.org Cc: linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, Jiasheng Jiang Subject: [PATCH v3] soc: qcom: apr: Add check and kfree Date: Tue, 13 Sep 2022 16:05:06 +0800 Message-Id: <20220913080506.1733403-1-jiasheng@iscas.ac.cn> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: qwCowAAnLhq0OSBj9BMuAg--.30824S2 X-Coremail-Antispam: 1UD129KBjvJXoW7Ar4ruFWrXFyfKrykGFW5Awb_yoW8Zr4kpa 1avas8Ary8JFs3ury3Cr1kWa45Ka1Iy3ykWrZ7J3429rn5XrnayrnrtFy09rWUuFWkAa1U Xr13XF95CF4UWFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUyE14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26r4j6ryUM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I8E87Iv6xkF7I0E14v26r xl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj 6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr 0_Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxAIw28IcxkI7VAK I48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7 xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xII jxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAIw2 0EY4v20xvaj40_Wr1j6rW3Jr1lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY 1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUbXdbUUUUUU== X-Originating-IP: [124.16.138.126] X-CM-SenderInfo: pmld2xxhqjqxpvfd2hldfou0/ X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org As idr_alloc() and of_property_read_string_index can return negative numbers, it should be better to check the return value and deal with the exception. Moreover, if device_register() fails, the adev should also be freed in order to avoid memory leak. Therefore, it should be better to use goto statement to deal with the exception. Fixes: 6adba21eb434 ("soc: qcom: Add APR bus driver") Signed-off-by: Jiasheng Jiang --- Changelog: v2 -> v3: 1. Change the title and use goto statement to deal with the exception. v1 -> v2: 1. Add dev_err and put_device in order to maintain the code consistency. --- drivers/soc/qcom/apr.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/drivers/soc/qcom/apr.c b/drivers/soc/qcom/apr.c index b4046f393575..b1a197155b2d 100644 --- a/drivers/soc/qcom/apr.c +++ b/drivers/soc/qcom/apr.c @@ -454,20 +454,34 @@ static int apr_add_device(struct device *dev, struct device_node *np, adev->dev.driver = NULL; spin_lock(&apr->svcs_lock); - idr_alloc(&apr->svcs_idr, svc, svc_id, svc_id + 1, GFP_ATOMIC); + ret = idr_alloc(&apr->svcs_idr, svc, svc_id, svc_id + 1, GFP_ATOMIC); spin_unlock(&apr->svcs_lock); + if (ret < 0) { + dev_err(dev, "idr_alloc failed: %d\n", ret); + goto error; + } - of_property_read_string_index(np, "qcom,protection-domain", - 1, &adev->service_path); + ret = of_property_read_string_index(np, "qcom,protection-domain", + 1, &adev->service_path); + if (ret < 0) { + dev_err(dev, "of_property_read_string_index failed: %d\n", ret); + goto error; + } dev_info(dev, "Adding APR/GPR dev: %s\n", dev_name(&adev->dev)); ret = device_register(&adev->dev); if (ret) { dev_err(dev, "device_register failed: %d\n", ret); - put_device(&adev->dev); + goto error; } + goto end; + +error: + put_device(&adev->dev); + kfree(adev); +end: return ret; } -- 2.25.1