Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2155982pxb; Fri, 25 Mar 2022 12:05:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwl/SB3iVm9ZMkxIPoKXDJ5/J4OQdRk1iJItIRndN6uBsz/Vljtp5o8XMI1BkcrjFxKdqma X-Received: by 2002:a63:1945:0:b0:382:30a3:d778 with SMTP id 5-20020a631945000000b0038230a3d778mr790225pgz.111.1648235122370; Fri, 25 Mar 2022 12:05:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648235122; cv=none; d=google.com; s=arc-20160816; b=rWNlHpmao46s0+o/YKiYFeFmYGoN1FtNJtwk70dfEfKhCV6YHe3gGYcp2ninZ7FTHn X/A8UhnBVsSouW9ERU7Zv2F5mtbT4NQ3HeDyWgYVQOPF5fSmL4ODhz7y8G0yNoJ4+J2P VEqL/RhuKBnNqJn1pgex1aAoWeRGjoIKK7DOZJzPENBOnXi+JiyUUxHTQUrEtxb/dJtB vsPZtSFBMaBH/a5zMhnGX8wanNcAwzz3Z1zs6fOjj+cN8tUDF8T0IoPFMtnIRpZNaIwq I3aP+0gTdvC8sm11JZVp5RgH96VzhXZ+HovUDNZfugm5UokoaeNU1dO5HPpAGZ76VFsL FV3w== 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:date :subject:cc:to:from:message-id:dkim-signature; bh=IPtd5NBJKkBEY0L1pQtW4blGnh3srn/WBBwWRTexlrw=; b=acPDQ/AuDCpz3ZU/CuSpV0h9NSUfYZNJM7AZM7Fah0CfS2NpmTqJGShd7kSP+Z/NHH ZtnwIgSb3crB9uz20jS2rvaKMrsdTMcez5R4wgtGKw18R7osAKOTmDa+GnbgquDTKHTl LAaCV/TZY3Xo1eOEuwZtcBDhYhLdsFU5dnodxgi/WiMmpaftjXK4WHPYDZSxe+z/M/6Y Cl/lXNtiD8FaR3ox8IeWFfZ9Iwtbao7e+ej1Pd87+GollUs5SEowzAKVPQh8SpiPJzKb 8o1a4l1Grt6kfUcfInvOX540foGrSIbgIPJOPqIydnY4tXIo/9x2P8YXJWXEqakPXiyo nVHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foxmail.com header.s=s201512 header.b=r+WJMRLG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id q22-20020a170902789600b00153b2d165f1si2756042pll.505.2022.03.25.12.05.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 12:05:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@foxmail.com header.s=s201512 header.b=r+WJMRLG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EB54F1F42CF; Fri, 25 Mar 2022 11:13:08 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357182AbiCYLvf (ORCPT + 99 others); Fri, 25 Mar 2022 07:51:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345320AbiCYLve (ORCPT ); Fri, 25 Mar 2022 07:51:34 -0400 Received: from out203-205-221-190.mail.qq.com (out203-205-221-190.mail.qq.com [203.205.221.190]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15C52D3AFC; Fri, 25 Mar 2022 04:49:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1648208996; bh=IPtd5NBJKkBEY0L1pQtW4blGnh3srn/WBBwWRTexlrw=; h=From:To:Cc:Subject:Date; b=r+WJMRLGw2sGKSgekix4f2uFMbge+HYA0TlkB4dDAiBWo+DDAjTRmgE+NGvz+/9X5 rsPVWUq9HeH0xNEFQWW/W47Mx7gSZT05ui5IOecyz+zAI/kpRsbxaZ9ZrABUP04qRl qxhijTneYrdHD0kosTf06f2NRhRko8uCBaBg8LF4= Received: from localhost.localdomain ([218.197.153.188]) by newxmesmtplogicsvrsza5.qq.com (NewEsmtp) with SMTP id C732ECB1; Fri, 25 Mar 2022 19:49:51 +0800 X-QQ-mid: xmsmtpt1648208991turlnigp0 Message-ID: X-QQ-XMAILINFO: MZ7OTbK+3aE5SaqPAIQeWKjDXnRL7UbB0WYL4yx1oTafA5+9iJBprA7gZQLM/c Px2EHDjur6vcGFMz1zZzST7o0PemfNlrw3+G4fbJXUitmxn0SpQ/QpCOkDsQnQ2ZQEO4gU/Wx5sv CnOncfyRJDWFilZxE//u+3/HLjPrZFDjV5pqDuPj6GELM61xA6Z9wiowoR/TdBAzs+++ydswqPlS tuqxGPe5x/gnjAEWoqR/QU5+gk1IPmMeFh+7mZJ1fQ/on3TqG6qAp6hMjckhSbKC523IHyZzPYb+ anKXUKNVssZ2EjpQ849qAf+NBEmJfocABJlFATBspj+bfdCpQuOlcNdjrTJf5l6vrWxJRWW6SkKl lwZGFfixdFw3kLvUhorn0ypHU6h0NQZ/3SOfSHfSSpSnxtyowWqnXVK9v37B2sU6r6NtaATEHqtF qqucgY96CvGchHorApMW2vJfCLgiYHTf6srOH1/GvclsUNfw8CL9+iUaqt922zvRUsXXSrWDlhW/ 67V1Qi7rqJR39VST2GOnHuYq+/ac8qzUCEKGJuLfdKJtnPbgixdN3D+3ga4z2uts/4w/ouuOlxpW 4Zf0Ii3a9dJiBJQDjtiU2bJVPFtw3ZLj1KPiqqjR+3kPAhxXiQJDKoMeqYDAbMru6gll4DYET1W+ 5aFLW5BGIkMGvkfoWWN8OfHyL/rHhNpqdrpOyv+KmOXA3fv1+GiMh7H6FUaFh+sRx2mLt7+mbfl3 6kS+t/T6UjnRiGiEfemzc8e95Ccrq6we4xOGLO3Dd7K8CRyCW9bEzRpR6D3gOF/gXEE54pA017/G qbYgvaE1Gz/Sc2PHk/5mSR+LZKYrfJcpDx5/5NiXOPzlQlMWhDutduEke4pQLJY4a1HvmPHmv7XD XcpSb1G3pF8b7LRc/YxNUxQ9W8MtW+kVY0y5LAhkZ7Tw/yE+cFNL5ecPyUMuL16nwj5qMQvL3buB pXgfYzh141TIT1StdOog== From: xkernel.wang@foxmail.com To: john@phrozen.org, tsbogend@alpha.franken.de Cc: linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoke Wang Subject: [PATCH] MIPS: lantiq: check the return value of kzalloc() Date: Fri, 25 Mar 2022 19:49:41 +0800 X-OQ-MSGID: <20220325114941.4648-1-xkernel.wang@foxmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 From: Xiaoke Wang kzalloc() is a memory allocation function which can return NULL when some internal memory errors happen. So it is better to check the return value of it to prevent potential wrong memory access or memory leak. Signed-off-by: Xiaoke Wang --- arch/mips/lantiq/falcon/sysctrl.c | 2 ++ arch/mips/lantiq/xway/gptu.c | 2 ++ arch/mips/lantiq/xway/sysctrl.c | 46 ++++++++++++++++++++----------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/arch/mips/lantiq/falcon/sysctrl.c b/arch/mips/lantiq/falcon/sysctrl.c index 42222f8..446a253 100644 --- a/arch/mips/lantiq/falcon/sysctrl.c +++ b/arch/mips/lantiq/falcon/sysctrl.c @@ -167,6 +167,8 @@ static inline void clkdev_add_sys(const char *dev, unsigned int module, { struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL); + if (!clk) + return; clk->cl.dev_id = dev; clk->cl.con_id = NULL; clk->cl.clk = clk; diff --git a/arch/mips/lantiq/xway/gptu.c b/arch/mips/lantiq/xway/gptu.c index 3d5683e..200fe9f 100644 --- a/arch/mips/lantiq/xway/gptu.c +++ b/arch/mips/lantiq/xway/gptu.c @@ -122,6 +122,8 @@ static inline void clkdev_add_gptu(struct device *dev, const char *con, { struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL); + if (!clk) + return; clk->cl.dev_id = dev_name(dev); clk->cl.con_id = con; clk->cl.clk = clk; diff --git a/arch/mips/lantiq/xway/sysctrl.c b/arch/mips/lantiq/xway/sysctrl.c index 917fac1..084f6ca 100644 --- a/arch/mips/lantiq/xway/sysctrl.c +++ b/arch/mips/lantiq/xway/sysctrl.c @@ -315,6 +315,8 @@ static void clkdev_add_pmu(const char *dev, const char *con, bool deactivate, { struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL); + if (!clk) + return; clk->cl.dev_id = dev; clk->cl.con_id = con; clk->cl.clk = clk; @@ -338,6 +340,8 @@ static void clkdev_add_cgu(const char *dev, const char *con, { struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL); + if (!clk) + return; clk->cl.dev_id = dev; clk->cl.con_id = con; clk->cl.clk = clk; @@ -356,24 +360,28 @@ static void clkdev_add_pci(void) struct clk *clk_ext = kzalloc(sizeof(struct clk), GFP_KERNEL); /* main pci clock */ - clk->cl.dev_id = "17000000.pci"; - clk->cl.con_id = NULL; - clk->cl.clk = clk; - clk->rate = CLOCK_33M; - clk->rates = valid_pci_rates; - clk->enable = pci_enable; - clk->disable = pmu_disable; - clk->module = 0; - clk->bits = PMU_PCI; - clkdev_add(&clk->cl); + if (clk) { + clk->cl.dev_id = "17000000.pci"; + clk->cl.con_id = NULL; + clk->cl.clk = clk; + clk->rate = CLOCK_33M; + clk->rates = valid_pci_rates; + clk->enable = pci_enable; + clk->disable = pmu_disable; + clk->module = 0; + clk->bits = PMU_PCI; + clkdev_add(&clk->cl); + } /* use internal/external bus clock */ - clk_ext->cl.dev_id = "17000000.pci"; - clk_ext->cl.con_id = "external"; - clk_ext->cl.clk = clk_ext; - clk_ext->enable = pci_ext_enable; - clk_ext->disable = pci_ext_disable; - clkdev_add(&clk_ext->cl); + if (clk_ext) { + clk_ext->cl.dev_id = "17000000.pci"; + clk_ext->cl.con_id = "external"; + clk_ext->cl.clk = clk_ext; + clk_ext->enable = pci_ext_enable; + clk_ext->disable = pci_ext_disable; + clkdev_add(&clk_ext->cl); + } } /* xway socs can generate clocks on gpio pins */ @@ -393,9 +401,15 @@ static void clkdev_add_clkout(void) char *name; name = kzalloc(sizeof("clkout0"), GFP_KERNEL); + if (!name) + continue; sprintf(name, "clkout%d", i); clk = kzalloc(sizeof(struct clk), GFP_KERNEL); + if (!clk) { + kfree(name); + continue; + } clk->cl.dev_id = "1f103000.cgu"; clk->cl.con_id = name; clk->cl.clk = clk; --