Received: by 2002:ab2:7a55:0:b0:1f4:4a7d:290d with SMTP id u21csp66562lqp; Thu, 4 Apr 2024 07:17:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXLYofFppDpxlaNCqmNQ+klVTc+/guYW8OHUo70SbjDIZvD4OW0dkPkyKTuy1PFPUU7IuG1NTuQvd9AAAlp1V0F8cgmK07HukJCzXLYYQ== X-Google-Smtp-Source: AGHT+IE8ztrT993s1p0YXXt/dzOFOYEIe50jWim2783W4nPRMAP0JPvV2CnhVbTnpmZ5qgezL6ed X-Received: by 2002:a67:f849:0:b0:478:9943:7a69 with SMTP id b9-20020a67f849000000b0047899437a69mr2355446vsp.32.1712240237298; Thu, 04 Apr 2024 07:17:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712240237; cv=pass; d=google.com; s=arc-20160816; b=EIgEuwcrX6JJIx9SEu0f3yeaIDguHjFbEF+ZQKg30YCCrtlJVV/G59PPVbFkjskrWq aYbGf0CKPjGSgCc3kwO2VJ7GFyckVP+ogtfqOAfZNN3EhV3NZI+fyOcc3BnzV2qZPyc1 4iJ08T6ztO/YWTvzvo4Uxj0I8aHnXXq655Ptxh0Ws3lpNCl9QHqglzMO+7Oyb769bA2D 5oSmjbzZW0T98nxrDp/hJNx3D7NeB6deSBD7GpswG4u3Fktdc8RTBsh9C2QC2fvdEfLb 9xpYmiF8is0c17xiN5930wWbMdicrSZws6ikdpaupgASeRn51cjeOjJg3Fi2Lv+sGCYF JEhg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=qkb73MilbijCgxtqVIIY99oFK9O8mtDqN17saeE4bqY=; fh=qagVGpkIqlGjSaGhv1eI/eItUsgnn+F31xhmQFa+ej8=; b=UwANhb3pImFFAT0y1Gxp9Mp8DJpKrzbelYAyVXrp7ZThqL4PD53L2/Tyv45PsGUEZn ljqFdMZRZaTrbvd+OEH0i2Y3Q8TVr0bGjVE9qPmenwNy0XkKr5V4K6EucKiv+MtNFP+y eq0tJfujhwxSM2U2ulgK5gNvcTB9g7UNWjPeW5N7rmbTaXW14ZHCvgry2EPkSjqJlwwk e/ktmnDTPM2GTNobkU3LqzmJcFmMjPK1y5fF8Ra4b7uQ6nnKb/LQCWTk4PMULXpl2GMC NBfC9Sh01yByVUTTMjfj2KZbeEKUo+YskAdvxOFNqEvA8+WIssH/hmHAEsKtgnlNDdeh I2SA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jM9sLNZD; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-131580-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131580-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id o31-20020a0561023f9f00b00476f116c430si2434315vsv.117.2024.04.04.07.17.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 07:17:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-131580-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jM9sLNZD; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-131580-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131580-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 481871C29A2E for ; Thu, 4 Apr 2024 14:16:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6E24712AAF3; Thu, 4 Apr 2024 14:15:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jM9sLNZD" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 740E21292EB; Thu, 4 Apr 2024 14:15:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712240138; cv=none; b=D3eiWxnJD+OdnzGa5mmLkyza3T0dSKsVAcdGD3FYWbgnZvY8oLh7KeH9pjXjp6ERpBKbW3rk3QYtir3p90EYCgl0IKRB4jajbodx+fooapSONlMAY/tDmnrSFM9PeDvcD667j24eALl2Penwb4jgUPauOBTWjQ9q0X7AQ0/UO+8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712240138; c=relaxed/simple; bh=3WdnQmshomibzByZClEkGia7PnK6CWBXCQshlze4d+I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lMrrNHmBACtU7pIv84o1Mys6SIg52FfYKbcpFufBQMR6lg7KdtszWSZ9P5dA8B4gVE8oyizSlk6pbMq5iYZZOXUhFLgN1mvXmK5SejDu+LnzkDNF9t5aoS34r1Md9dquI134IOzrsWL5UKnkaj1BLHyY9qyBjJenU1TrRNbnJ7I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jM9sLNZD; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D3C55C43390; Thu, 4 Apr 2024 14:15:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712240138; bh=3WdnQmshomibzByZClEkGia7PnK6CWBXCQshlze4d+I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=jM9sLNZDJCyGA8EVCGqMJDWNJurpEUFOlM0x6Am4RGRjjBGFRiRaB9NsO5fFyJ+MX WfUqw+KJneyFVcctw4j3ZS7orXCXOUg5Pu1sdOa8FDJhTvXiU5yDbrCR2eWtNBAKFN bLgJ1T8jicXImpA6g1Lt8o4fGfDK72CDUb4vMAL/nSi/tmG2SloBvicaq0UXivXFy4 DnyG4Ovj24vn6mgTgb09yM5YwVZrtu3V2eLmld+vPuphTDL9zkSojaf+whzZIb0TUv tj/mGPGrRMyq2hsY6A98qJHwa9nkCfQViQnXKfehcxbtBHXLfp3x25lTN7u9JglQ5u qyTiz1YN9aU8g== From: Rob Herring Date: Thu, 04 Apr 2024 09:15:11 -0500 Subject: [PATCH 2/3] of: Use scope based kfree() cleanups Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240404-dt-cleanup-free-v1-2-c60e6cba8da9@kernel.org> References: <20240404-dt-cleanup-free-v1-0-c60e6cba8da9@kernel.org> In-Reply-To: <20240404-dt-cleanup-free-v1-0-c60e6cba8da9@kernel.org> To: Saravana Kannan Cc: Jonathan Cameron , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org X-Mailer: b4 0.13-dev Use the relatively new scope based kfree() cleanup to simplify error handling. Doing so reduces the chances of memory leaks and simplifies error paths by avoiding the need for goto statements. Signed-off-by: Rob Herring --- drivers/of/base.c | 34 ++++++++-------------------------- drivers/of/dynamic.c | 11 ++++------- drivers/of/resolver.c | 35 +++++++++++++---------------------- 3 files changed, 25 insertions(+), 55 deletions(-) diff --git a/drivers/of/base.c b/drivers/of/base.c index 8856c67c466a..20603d3c9931 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -16,6 +16,7 @@ #define pr_fmt(fmt) "OF: " fmt +#include #include #include #include @@ -1393,8 +1394,10 @@ int of_parse_phandle_with_args_map(const struct device_node *np, const char *stem_name, int index, struct of_phandle_args *out_args) { - char *cells_name, *map_name = NULL, *mask_name = NULL; - char *pass_name = NULL; + char *cells_name __free(kfree) = kasprintf(GFP_KERNEL, "#%s-cells", stem_name); + char *map_name __free(kfree) = kasprintf(GFP_KERNEL, "%s-map", stem_name); + char *mask_name __free(kfree) = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name); + char *pass_name __free(kfree) = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name); struct device_node *cur, *new = NULL; const __be32 *map, *mask, *pass; static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = cpu_to_be32(~0) }; @@ -1407,27 +1410,13 @@ int of_parse_phandle_with_args_map(const struct device_node *np, if (index < 0) return -EINVAL; - cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name); - if (!cells_name) + if (!cells_name || !map_name || !mask_name || !pass_name) return -ENOMEM; - ret = -ENOMEM; - map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name); - if (!map_name) - goto free; - - mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name); - if (!mask_name) - goto free; - - pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name); - if (!pass_name) - goto free; - ret = __of_parse_phandle_with_args(np, list_name, cells_name, -1, index, out_args); if (ret) - goto free; + return ret; /* Get the #-cells property */ cur = out_args->np; @@ -1444,8 +1433,7 @@ int of_parse_phandle_with_args_map(const struct device_node *np, /* Get the -map property */ map = of_get_property(cur, map_name, &map_len); if (!map) { - ret = 0; - goto free; + return 0; } map_len /= sizeof(u32); @@ -1521,12 +1509,6 @@ int of_parse_phandle_with_args_map(const struct device_node *np, put: of_node_put(cur); of_node_put(new); -free: - kfree(mask_name); - kfree(map_name); - kfree(cells_name); - kfree(pass_name); - return ret; } EXPORT_SYMBOL(of_parse_phandle_with_args_map); diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c index af7c57a7a25d..43f4e2c93bd2 100644 --- a/drivers/of/dynamic.c +++ b/drivers/of/dynamic.c @@ -9,6 +9,7 @@ #define pr_fmt(fmt) "OF: " fmt +#include #include #include #include @@ -1019,10 +1020,9 @@ int of_changeset_add_prop_u32_array(struct of_changeset *ocs, const u32 *array, size_t sz) { struct property prop; - __be32 *val; - int i, ret; + __be32 *val __free(kfree) = kcalloc(sz, sizeof(__be32), GFP_KERNEL); + int i; - val = kcalloc(sz, sizeof(__be32), GFP_KERNEL); if (!val) return -ENOMEM; @@ -1032,9 +1032,6 @@ int of_changeset_add_prop_u32_array(struct of_changeset *ocs, prop.length = sizeof(u32) * sz; prop.value = (void *)val; - ret = of_changeset_add_prop_helper(ocs, np, &prop); - kfree(val); - - return ret; + return of_changeset_add_prop_helper(ocs, np, &prop); } EXPORT_SYMBOL_GPL(of_changeset_add_prop_u32_array); diff --git a/drivers/of/resolver.c b/drivers/of/resolver.c index b278ab4338ce..2780928764a4 100644 --- a/drivers/of/resolver.c +++ b/drivers/of/resolver.c @@ -8,6 +8,7 @@ #define pr_fmt(fmt) "OF: resolver: " fmt +#include #include #include #include @@ -74,11 +75,11 @@ static int update_usages_of_a_phandle_reference(struct device_node *overlay, { struct device_node *refnode; struct property *prop; - char *value, *cur, *end, *node_path, *prop_name, *s; + char *value __free(kfree) = kmemdup(prop_fixup->value, prop_fixup->length, GFP_KERNEL); + char *cur, *end, *node_path, *prop_name, *s; int offset, len; int err = 0; - value = kmemdup(prop_fixup->value, prop_fixup->length, GFP_KERNEL); if (!value) return -ENOMEM; @@ -89,23 +90,19 @@ static int update_usages_of_a_phandle_reference(struct device_node *overlay, node_path = cur; s = strchr(cur, ':'); - if (!s) { - err = -EINVAL; - goto err_fail; - } + if (!s) + return -EINVAL; *s++ = '\0'; prop_name = s; s = strchr(s, ':'); - if (!s) { - err = -EINVAL; - goto err_fail; - } + if (!s) + return -EINVAL; *s++ = '\0'; err = kstrtoint(s, 10, &offset); if (err) - goto err_fail; + return err; refnode = __of_find_node_by_full_path(of_node_get(overlay), node_path); if (!refnode) @@ -117,22 +114,16 @@ static int update_usages_of_a_phandle_reference(struct device_node *overlay, } of_node_put(refnode); - if (!prop) { - err = -ENOENT; - goto err_fail; - } + if (!prop) + return -ENOENT; - if (offset < 0 || offset + sizeof(__be32) > prop->length) { - err = -EINVAL; - goto err_fail; - } + if (offset < 0 || offset + sizeof(__be32) > prop->length) + return -EINVAL; *(__be32 *)(prop->value + offset) = cpu_to_be32(phandle); } -err_fail: - kfree(value); - return err; + return 0; } /* compare nodes taking into account that 'name' strips out the @ part */ -- 2.43.0