Received: by 2002:ab2:7a55:0:b0:1f4:4a7d:290d with SMTP id u21csp367340lqp; Thu, 4 Apr 2024 16:16:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWdqJZwsPYvKCknUto8pD1h4DX616MeXkKeyA9j3dYiyzDeioq0N3YK+amVWC676QV9vx6WgzMaaQuTgAbkSMYTYlkEXAYH093wQaNK0g== X-Google-Smtp-Source: AGHT+IFThTZK4GDTADJJBAs4/QhlBbOiQ+4AarNYrTTmFRi+haROd7YmumaB0IcNXMFD4olj4Nk/ X-Received: by 2002:a50:f681:0:b0:56e:3034:1d4a with SMTP id d1-20020a50f681000000b0056e30341d4amr15837edn.9.1712272569060; Thu, 04 Apr 2024 16:16:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712272569; cv=pass; d=google.com; s=arc-20160816; b=SdbeL3EInkhPbFJE1H1hEznsrTJBZesvoicrjKmk/n0trXhLrI1bpkCtO9QMr/Fz21 yHP7LvK3SJaFPPHjCFiBzJu8rCz6qeXRTKzKom0ijP/cjZXeZUcawakbBlxq2Otpkyb8 cpaklFgRzZAEajhmP9P5lR+fqkycqaNoN9WLDUNRllPDJCx7MPorHRbgmi3ARihus/aw vUrHzc6SRCEbILIZ2SFZRYowaY34wTVjyqfqdLL2IQcmo78ClpKqa9oH58VU1HKd9raa 4+jsNLH02ZZ4Zd5kmKv1JwzuYT3D3MxB0gztvmU+c3MpTXdRi4+G0US8NW7/ECRTCdQo 0i8w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=sKddqz6smnqmh9exl0udqVZxjHW77Ue+RMv6tUW1JN4=; fh=Wdu2Qbi7zDOdZNKlHy0B+Bi6ThFrxgJ8RyxzNK7+QL8=; b=gpzTuT1j8aNCx5a93Jkkn2e+YKXalJMXPjtyos6ErM/Vp1kEePVNhsYVB7uJxycP4B RIOLc8FYjx5wOR9seciwCxJs8RtZpytRbXCu03PR8VYjIfhpw/gkEW6zLw4i08ALtTYg c4rvtGH/tsXL2EOxexYrqdM1zCPb1NkNHeVVmNmnIXQTLdsmjP/tCDcO42mvCffA+gIA XO/LP7Q7NTSTnuAG84faYoE0ajJakLigmqPMOHWomZZEY3v0p4NJCOkEEWE7iHitlQRZ sBnfn7Kgv8m0kARLSXu7hSTTfrXFNfbfCnxSo3cgZHu/2QrnPmTHzZKBYimXbjZHIOdD 1fbw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1NeFl5Sx; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-132246-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-132246-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id p12-20020a05640243cc00b0056bf30ff6b4si133964edc.122.2024.04.04.16.16.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 16:16:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-132246-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1NeFl5Sx; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-132246-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-132246-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 509EF1F21E0F for ; Thu, 4 Apr 2024 23:16:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F071C13C3F2; Thu, 4 Apr 2024 23:16:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1NeFl5Sx" Received: from mail-qt1-f169.google.com (mail-qt1-f169.google.com [209.85.160.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 236E413BC38 for ; Thu, 4 Apr 2024 23:15:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712272559; cv=none; b=Aqf/lSeDz5GxNfcYGt8HHktt8pDkxOn1yfchywzkRhE+9nIqfmqIJ3jTZfhpDV7s4C2j454egWJfn5X7MCXLTFBFiZp0wxyWpXPQ6Xw472JKSDyC00lr2R8AyW/YtuOlQn0u3qMl12gCj+etXeCyz/Tx1lXyZ0Yi7N1v3B52zD0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712272559; c=relaxed/simple; bh=q7Rmob6dx38Tia7j2342w5w2ow7VgzMTr/CsK1WSk6Y=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=FAcn+qkaGF4uBm4Sc0co72clhXMn1sNNt8vhLJr4lmp7EhEqUEc8+W5hTtdGIPaag0PZVvhU7TotPzJyzrdSQ1Wd9rJ7uaNXP8UYjVAJp9Ac5ekx9IZfSFRt5yjEDT1rF2VydVRSKy+LP0aLvlyu9cvN2PlhrEI6H9kigfsRe4k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1NeFl5Sx; arc=none smtp.client-ip=209.85.160.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-qt1-f169.google.com with SMTP id d75a77b69052e-430d3fcc511so67901cf.1 for ; Thu, 04 Apr 2024 16:15:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712272557; x=1712877357; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=sKddqz6smnqmh9exl0udqVZxjHW77Ue+RMv6tUW1JN4=; b=1NeFl5SxPNDinhtQ6Ty8eE0Sn8E2NmdKOaLffWEUVMq4W79yRCg7dKYtXawDEW7Wvc 6UoYAqmwItyLvtb1H/LxB2T7irJYxF5bDq/qVzHmZMzgZbFU2eH7+JUbvVfwH68MvQsO cipqhy9LcyHPKvd52aAHxn76IFN1dpX6aSoqhthz2WyyN2At1LUUeLvgbZPW4kMivd5p Ojg7ahmnmRbGFQiZDWE+3HtXVx+Mum+AqcR3NQD57w87q052McPEO23CC5kJ6RuQcfht mB/FrN90GNA8dkMw57Qbbh3UtA3D1rySh532E5n3fg2gJAQI34LP4O2KwRnOEj2m/38O WbQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712272557; x=1712877357; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sKddqz6smnqmh9exl0udqVZxjHW77Ue+RMv6tUW1JN4=; b=JnoSYtuIekZI/K/Kyn9x8vQbAg4E0IaxxVkDueJW34dVqrYfassCajjG0jvFW5/Y7P gSKoZYjKdvxZhC6tr3NOrkyBXdpHspNaiJiUjZv5v4mWr3n5HpyowFAfUy32/hLPeF4X EJEEBYVr3PKUgRHiHz3PlgOt9aqvUJZ9OSSEAS+b12KVfz9rX8H/ZnOD7ALq16s4Dk03 SkB5xMWQJY7kbrRWc+5CKL8tZbb2Ea17GGpBSMgLiqV8hkAHIMb9EfJN8DDHNrXRett5 7De0o/CGzkBPTYjWQiuydY3XxeYZCshqjIVj9ekAJXDcubs45H5OW2xGDWsT6pcNBh/H JY2g== X-Forwarded-Encrypted: i=1; AJvYcCULvtnRTfHo3RYUwBHLYf7CKTrmxLs+7AwCn/1h5mLneVQDFSwP+c3GWhqNVd+IE84muRJqv0kWFiHhnpWzdgACzZNTPT4ZSuoJcNC0 X-Gm-Message-State: AOJu0Yyyg30UJSaG4Ryyh0NeKpBtx+FSEiOGId6X27uYuB/gXsRoRaeY lzm6Ft5FtvcOZbfPFvIsWXNLOEJVYLClvKr/OrYvsUxdyv2LCqbpSPv/NNeyiwLZB2hQrIC1piA MqRAfOM15sdk1M2yv4xQPzwTK8L8/3oaDfoXi X-Received: by 2002:a05:622a:1b19:b0:434:36fe:6d3 with SMTP id bb25-20020a05622a1b1900b0043436fe06d3mr354666qtb.0.1712272556757; Thu, 04 Apr 2024 16:15:56 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240404-dt-cleanup-free-v1-0-c60e6cba8da9@kernel.org> <20240404-dt-cleanup-free-v1-2-c60e6cba8da9@kernel.org> In-Reply-To: <20240404-dt-cleanup-free-v1-2-c60e6cba8da9@kernel.org> From: Saravana Kannan Date: Thu, 4 Apr 2024 16:15:20 -0700 Message-ID: Subject: Re: [PATCH 2/3] of: Use scope based kfree() cleanups To: Rob Herring Cc: Jonathan Cameron , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Apr 4, 2024 at 7:15=E2=80=AFAM Rob Herring wrote: > > 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 de= vice_node *np, > const char *stem_name, > int index, struct of_phandle_args *out= _args) > { > - char *cells_name, *map_name =3D NULL, *mask_name =3D NULL; > - char *pass_name =3D NULL; > + char *cells_name __free(kfree) =3D kasprintf(GFP_KERNEL, "#%s-cel= ls", stem_name); > + char *map_name __free(kfree) =3D kasprintf(GFP_KERNEL, "%s-map", = stem_name); > + char *mask_name __free(kfree) =3D kasprintf(GFP_KERNEL, "%s-map-m= ask", stem_name); > + char *pass_name __free(kfree) =3D kasprintf(GFP_KERNEL, "%s-map-p= ass-thru", stem_name); With the scoped stuff, do these function calls need to be in the same line we are defining these variables? If not, I'd rather that the calls remain where they were. It feels like a lote to visually parse and take in from a readability perspective. > struct device_node *cur, *new =3D NULL; > const __be32 *map, *mask, *pass; > static const __be32 dummy_mask[] =3D { [0 ... MAX_PHANDLE_ARGS] = =3D cpu_to_be32(~0) }; > @@ -1407,27 +1410,13 @@ int of_parse_phandle_with_args_map(const struct d= evice_node *np, > if (index < 0) > return -EINVAL; > > - cells_name =3D kasprintf(GFP_KERNEL, "#%s-cells", stem_name); > - if (!cells_name) > + if (!cells_name || !map_name || !mask_name || !pass_name) > return -ENOMEM; > > - ret =3D -ENOMEM; > - map_name =3D kasprintf(GFP_KERNEL, "%s-map", stem_name); > - if (!map_name) > - goto free; > - > - mask_name =3D kasprintf(GFP_KERNEL, "%s-map-mask", stem_name); > - if (!mask_name) > - goto free; > - > - pass_name =3D kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name= ); > - if (!pass_name) > - goto free; > - > ret =3D __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 =3D out_args->np; > @@ -1444,8 +1433,7 @@ int of_parse_phandle_with_args_map(const struct dev= ice_node *np, > /* Get the -map property */ > map =3D of_get_property(cur, map_name, &map_len); > if (!map) { > - ret =3D 0; > - goto free; > + return 0; > } > map_len /=3D sizeof(u32); > > @@ -1521,12 +1509,6 @@ int of_parse_phandle_with_args_map(const struct de= vice_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_chan= geset *ocs, > const u32 *array, size_t sz) > { > struct property prop; > - __be32 *val; > - int i, ret; > + __be32 *val __free(kfree) =3D kcalloc(sz, sizeof(__be32), GFP_KER= NEL); > + int i; > > - val =3D kcalloc(sz, sizeof(__be32), GFP_KERNEL); > if (!val) > return -ENOMEM; > > @@ -1032,9 +1032,6 @@ int of_changeset_add_prop_u32_array(struct of_chang= eset *ocs, > prop.length =3D sizeof(u32) * sz; > prop.value =3D (void *)val; > > - ret =3D 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(struc= t device_node *overlay, > { > struct device_node *refnode; > struct property *prop; > - char *value, *cur, *end, *node_path, *prop_name, *s; > + char *value __free(kfree) =3D kmemdup(prop_fixup->value, prop_fix= up->length, GFP_KERNEL); > + char *cur, *end, *node_path, *prop_name, *s; > int offset, len; > int err =3D 0; > > - value =3D kmemdup(prop_fixup->value, prop_fixup->length, GFP_KERN= EL); > if (!value) > return -ENOMEM; > > @@ -89,23 +90,19 @@ static int update_usages_of_a_phandle_reference(struc= t device_node *overlay, > > node_path =3D cur; > s =3D strchr(cur, ':'); > - if (!s) { > - err =3D -EINVAL; > - goto err_fail; > - } > + if (!s) > + return -EINVAL; > *s++ =3D '\0'; > > prop_name =3D s; > s =3D strchr(s, ':'); > - if (!s) { > - err =3D -EINVAL; > - goto err_fail; > - } > + if (!s) > + return -EINVAL; > *s++ =3D '\0'; > > err =3D kstrtoint(s, 10, &offset); > if (err) > - goto err_fail; > + return err; > > refnode =3D __of_find_node_by_full_path(of_node_get(overl= ay), node_path); > if (!refnode) > @@ -117,22 +114,16 @@ static int update_usages_of_a_phandle_reference(str= uct device_node *overlay, > } > of_node_put(refnode); > > - if (!prop) { > - err =3D -ENOENT; > - goto err_fail; > - } > + if (!prop) > + return -ENOENT; > > - if (offset < 0 || offset + sizeof(__be32) > prop->length)= { > - err =3D -EINVAL; > - goto err_fail; > - } > + if (offset < 0 || offset + sizeof(__be32) > prop->length) > + return -EINVAL; > > *(__be32 *)(prop->value + offset) =3D cpu_to_be32(phandle= ); > } > > -err_fail: > - kfree(value); > - return err; > + return 0; > } > > /* compare nodes taking into account that 'name' strips out the @ part *= / Reviewed-by: Saravana Kannan -Saravana