Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp915202rdb; Tue, 30 Jan 2024 02:27:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IGJbEnYWKJgRhGX30W/bcZ1HCcTH8lPeKazVOrMtdtHgdPBzJJ0eZXITV7BM3RhYYPbIWuG X-Received: by 2002:a17:906:46d2:b0:a35:dad6:e241 with SMTP id k18-20020a17090646d200b00a35dad6e241mr2880565ejs.11.1706610423277; Tue, 30 Jan 2024 02:27:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706610423; cv=pass; d=google.com; s=arc-20160816; b=azr+h3DSWIO2Igb3rhP+0D7RvXHzZC3oI8pkhrdDRq0BRX4S1VLUQC0i97FtOKMeE1 wZcmPV62eDB8URZkwg3PxiJZSvp450UYXOjegxCV3jBN1y7hJZ54AxuYCXchIdzRWw/4 MNTF5YXqTUQywSg1p+ArNIUjd+rS6+0dHNDHY9vRGSz3kFrRlh2Bu26gifn4lv8EfiQH /AbumIOF2/Y0NAvfu2AOfDKzFd76yi9Wnm0uJ+t7ASEQ1LD52+et1XeTxAn4uO20Tgst RTa+bIhl1xQRHuvB5U1ZjquSiyofLdZ94G6z0ZpmtgETydeSUFnY15kMdym4vXF5/3b5 TFTQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:references:message-id:in-reply-to:subject:cc:to:from :date:dkim-signature; bh=B0tGjYMn1Bw0c4FGimg8I6GIokoYVNuvmtpt3/P3hkU=; fh=/8VGipN3TVHWbPd1+ihFSSbRIskkDp7WiGaYBHwIA4M=; b=rxpasfEhkmIhbp0CZuY0hUu1MeQb4ZWdkOE8uYZuTra2tfHry7Kea0mlDjdJBZCn2I KaVPos2Z5eBT0nSZrzb3OUfXQakwpKaenqbH71xlphz24wiM3v0mmiANZLOisbBNHFHC n7bxSpYU5euDis0vgFZdgMDiZA2vvvLnjlYChn5zgajBC1bIFzphk6lcpj0iyOMwODT3 1hrzCDxIgWRdhJkn+ulT4Xeb9fTQxIu2d7LisjS4gS7k+gUm4NXr17RjKyvzIGPv7Iur 2VRD+0gveTT8yrHG0ALLXWnLPwcWh0EQ8S9aJK+kR1fs9ri0/qpR6m3zyU3rKVwDPBOm nabA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@inria.fr header.s=dc header.b=Yn5WoZTm; arc=pass (i=1 spf=pass spfdomain=inria.fr dkim=pass dkdomain=inria.fr dmarc=pass fromdomain=inria.fr); spf=pass (google.com: domain of linux-kernel+bounces-44380-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44380-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=inria.fr Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id n8-20020a170906b30800b00a3523d7d31esi3712733ejz.151.2024.01.30.02.27.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 02:27:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-44380-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@inria.fr header.s=dc header.b=Yn5WoZTm; arc=pass (i=1 spf=pass spfdomain=inria.fr dkim=pass dkdomain=inria.fr dmarc=pass fromdomain=inria.fr); spf=pass (google.com: domain of linux-kernel+bounces-44380-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44380-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=inria.fr 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 12BBD1F245C3 for ; Tue, 30 Jan 2024 10:26:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D6EB060EDC; Tue, 30 Jan 2024 10:26:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=inria.fr header.i=@inria.fr header.b="Yn5WoZTm" Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) (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 6105060DC7; Tue, 30 Jan 2024 10:26:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.134.164.104 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706610376; cv=none; b=fXfGAo/pOfMEeQxT1OigRGPzaTkr9G/rk8d2gKPGk7v0/6wAbv3ZrbRAhS7xH0SNPWZyjjZ6a54Sjh9cil67I0c9UoS1dOoyqvhyhICRlXOJAWSopo6bR+TZBbLL0lnGYIPr5BOH6J1zAmlV2xdT5lp3qQuBYwgQ6o5UQcHU6N0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706610376; c=relaxed/simple; bh=NNXENF2QPPdXpgQAXbcfd5gidZFE0d1VXALQ6s8OWPQ=; h=Date:From:To:cc:Subject:In-Reply-To:Message-ID:References: MIME-Version:Content-Type; b=b1QxPB3dbsPLL54LWaB7p5YlaosVLblD223ZJeTQpYtAW+N0dCKjPoOxKb0GV6Y9nOnDPIsx34SNbHwIhc7CLBxr0G+2du7GM77kAfQgX0uZCHrLDU3WeOeUUO5z1yFR13mQggKBjfZ1b68dZU40djiVkhAKEFtPR8Guhw1s0RY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=inria.fr; spf=pass smtp.mailfrom=inria.fr; dkim=pass (1024-bit key) header.d=inria.fr header.i=@inria.fr header.b=Yn5WoZTm; arc=none smtp.client-ip=192.134.164.104 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=inria.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=inria.fr DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=inria.fr; s=dc; h=date:from:to:cc:subject:in-reply-to:message-id: references:mime-version; bh=B0tGjYMn1Bw0c4FGimg8I6GIokoYVNuvmtpt3/P3hkU=; b=Yn5WoZTmVL0M2USwDDW7k11cq0ASbjTpGYfRwE+jV5TwMwBEsGJ3+BSr u4D1AYeCHAu5VRJc0mpdfTGVCGgQGDlAnOqgZyryDXo4QYW3LCifYtDE3 a0warqx5+ofQhSyO6H8NS3STEsip/DReO2J/PNm4hRy+pCh2ORD9/x3qc Q=; Authentication-Results: mail3-relais-sop.national.inria.fr; dkim=none (message not signed) header.i=none; spf=SoftFail smtp.mailfrom=julia.lawall@inria.fr; dmarc=fail (p=none dis=none) d=inria.fr X-IronPort-AV: E=Sophos;i="6.05,707,1701126000"; d="scan'208";a="78202663" Received: from 231.85.89.92.rev.sfr.net (HELO hadrien) ([92.89.85.231]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2024 11:26:08 +0100 Date: Tue, 30 Jan 2024 11:26:08 +0100 (CET) From: Julia Lawall X-X-Sender: jll@hadrien To: Jonathan Cameron cc: Jonathan Cameron , linux-iio@vger.kernel.org, Rob Herring , Frank Rowand , linux-kernel@vger.kernel.org, Nicolas Palix , Sumera Priyadarsini , "Rafael J . Wysocki" , Len Brown , linux-acpi@vger.kernel.org, Andy Shevchenko , Greg Kroah-Hartman , =?ISO-8859-15?Q?Nuno_S=E1?= Subject: Re: [RFC PATCH 0/5] of: automate of_node_put() - new approach to loops. In-Reply-To: <20240130093854.00000acc@Huawei.com> Message-ID: References: <20240128160542.178315-1-jic23@kernel.org> <20240129114218.00003c34@Huawei.com> <20240129195227.3c3adae1@jic23-huawei> <20240130093854.00000acc@Huawei.com> User-Agent: Alpine 2.22 (DEB 394 2020-01-19) 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=US-ASCII On Tue, 30 Jan 2024, Jonathan Cameron wrote: > On Mon, 29 Jan 2024 21:29:13 +0100 (CET) > Julia Lawall wrote: > > > On Mon, 29 Jan 2024, Jonathan Cameron wrote: > > > > > On Mon, 29 Jan 2024 15:02:19 +0100 (CET) > > > Julia Lawall wrote: > > > > > > > On Mon, 29 Jan 2024, Jonathan Cameron wrote: > > > > > > > > > On Sun, 28 Jan 2024 19:06:53 +0100 (CET) > > > > > Julia Lawall wrote: > > > > > > > > > > > On Sun, 28 Jan 2024, Jonathan Cameron wrote: > > > > > > > > > > > > > From: Jonathan Cameron > > > > > > > > > > > > > > +CC includes peopleinterested in property.h equivalents to minimize > > > > > > > duplication of discussion. Outcome of this discussion will affect: > > > > > > > https://lore.kernel.org/all/20240114172009.179893-1-jic23@kernel.org/ > > > > > > > [PATCH 00/13] device property / IIO: Use cleanup.h magic for fwnode_handle_put() handling. > > > > > > > > > > > > > > In discussion of previous approach with Rob Herring we talked about various > > > > > > > ways to avoid a disconnect between the declaration of the __free(device_node) > > > > > > > and the first non NULL assignment. Making this connection clear is useful for 2 > > > > > > > reasons: > > > > > > > 1) Avoids out of order cleanup with respect to other cleanup.h usage. > > > > > > > 2) Avoids disconnect between how cleanup is to be done and how the reference > > > > > > > was acquired in the first place. > > > > > > > > > > > > > > https://lore.kernel.org/all/20240117194743.GA2888190-robh@kernel.org/ > > > > > > > > > > > > > > The options we discussed are: > > > > > > > > > > > > > > 1) Ignore this issue and merge original set. > > > > > > > > > > > > > > 2) Always put the declaration just before the for loop and don't set it NULL. > > > > > > > > > > > > > > { > > > > > > > int ret; > > > > > > > > > > > > > > ret = ... and other fun code. > > > > > > > > > > > > > > struct device_node *child __free(device_node); > > > > > > > for_each_child_of_node(np, child) { > > > > > > > } > > > > > > > } > > > > > > > > > > > > > > This works but careful review is needed to ensure that this unusual pattern is > > > > > > > followed. We don't set it to NULL as the loop will do that anyway if there are > > > > > > > no child nodes, or the loop finishes without an early break or return. > > > > > > > > > > > > > > 3) Introduced the pointer to auto put device_node only within the > > > > > > > for loop scope. > > > > > > > > > > > > > > +#define for_each_child_of_node_scoped(parent, child) \ > > > > > > > + for (struct device_node *child __free(device_node) = \ > > > > > > > + of_get_next_child(parent, NULL); \ > > > > > > > + child != NULL; \ > > > > > > > + child = of_get_next_available_child(parent, child)) > > > > > > > + > > > > > > > > > > > > > > This series is presenting option 3. I only implemented this loop out of > > > > > > > all the similar ones and it is only compile tested. > > > > > > > > > > > > > > Disadvantage Rob raised is that it isn't obvious this macro will instantiate > > > > > > > a struct device_node *child. I can't see a way around that other than option 2 > > > > > > > above, but all suggestions welcome. Note that if a conversion leaves an > > > > > > > 'external' struct device_node *child variable, in many cases the compiler > > > > > > > will catch that as an unused variable. We don't currently run shaddow > > > > > > > variable detection in normal kernel builds, but that could also be used > > > > > > > to catch such bugs. > > > > > > > > > > > > > > All comments welcome. > > > > > > > > > > > > It looks promising to get rid of a lot of clunky and error-prone > > > > > > error-handling code. > > > > > > > > > > Absolutely. I think I spotted 2 bugs whilst just looking for places this pattern > > > > > doesn't apply. Will circle back to those once this discussion is resolved. > > > > > I think I've taken dozen's of fixes for cases where these were missed over the years > > > > > so hoping this means I'll never see another one! > > > > > > > > > > > > > > > > > I guess that > > > > > > > > > > > > for_each_child_of_node_scoped(parent, struct device_node *, child) > > > > > > > > > > > > would seem too verbose? > > > > > > > > > > Intent just to make the allocated internal type clear? Sure we can do that if > > > > > it helps with making it clear something is being allocated. > > > > > I can't think of a way this could be used with anything other than > > > > > a struct device_node * as the second parameter but I guess it still helps > > > > > 'hint' at what is going on.. > > > > > > To touch back on this, I'm still not sure what your intent was in suggesting > > > having the explicit struct device_node * > > > > Well, I wanted struct device_node * child, but then there wouldn't be the > > macro argument that would give the variable name. So perhaps even with > > the comma it would look a little more like a declaration. > > > > It does seem problematic to have eg two freestanding occurrences of child > > that are not in the same scope, if there is a child in the iterator > > argument list and a use elsewhere in the function. > Hi Julia, > > > Would be nice to have shadow variable detection enabled (QEMU did > that recently and it caught various bugs). > > I'm not sure if this helps or not with readability and get the feeling > that people will perpetually send patches to remove the pointless > macro argument. I guess a comment on why it is there might avoid that > well enough though. OK, it was a suggestion. Even I was not convinced that it was a good suggestion :) > > > > > > There are a lot of opportunities for device_node loops, but also for some > > > > > > more obscure loops over other types. > > > > > > > > > > > And there are a lot of of_node_puts > > > > > > that could be eliminated independent of loops. > > > > > > > > > > The non loop cases should be handled via the __free(device_node) as provided > > > > > by patch 1. We'll need to keep the declaration local and initial assignment > > > > > together but that is easy enough to do and similar to the many other cleanup.h > > > > > usecases that are surfacing. > > > > > > > > I tried the following semantic patch: > > > > > > > > @@ > > > > identifier x,f; > > > > attribute name __free; > > > > expression list es; > > > > expression e; > > > > statement S; > > > > @@ > > > > > > > > { > > > > ... when != S > > > > struct device_node *x > > > > + __free(device_node) > > > > ; > > > > ... when strict > > > > x = f(es); > > > > <... when any > > > > when != x = e > > > > -of_node_put(x); > > > > ...> > > > > -of_node_put(x); > > > > } > > > > > > > Nice. > > > > It is written defensively in various ways: > > > > > > > > when != S means tha tthe device_node declaration has t be at the top of > > > > the block, perhaps with other declarations before it. > > > > > > > > when strict means that all paths must lead from the declaration to the > > > > initialization. So there is no need to intiialize the variable to NULL, > > > > as far as I understand. > > > > > > > > when != x = e means that the declared variable is not reinitialized, which > > > > would require keeping the previous of_node_put. > > > > > > > > There is an of_node_put at the end of the block, so the use of __free > > > > doesn't change the lifetime. > > > > > > > > An unfortunate aspect of the last constraint is that some functions may > > > > declare multiple device_node variables, and only one of the of_not_puts > > > > can come at the very end of the block. This can be solved by just running > > > > the semantic patch again. > > > > > > > > An alternative would be to move the initialization up to the declaration, > > > > but the result was often a bit ugly, due to the various initialization > > > > function calls having long names and argument lists. > > > > > > You have to do that in order to ensure there is no window for someone to > > > easily insert code that leaves them uninitialized. > > > > > > Linus had some rather specific comments on that being the only right way to > > > do it. > > > > OK, the semantic patch can be changed to do that. It would have to be a > > bit more complex or a bit more defensive, to be sure that no variables are > > used between the declaration and the current call site. > > > > > > > > > > > > > The output is below. I have looked quickly through all of the changes and > > > > they all look reasonable, but have not tried compiling anything (which I > > > > guess wouldn't currently work anyway). > > > > > > > > julia > > > > > > > > > > I picked a couple from the end. They show the sort of things that > > > would want cleaning up. The script is great for finding low hanging > > > fruit but as you've identified there is often some stuff that isn't > > > easy to automate. > > > > > > > diff -u -p a/drivers/clk/clk-nomadik.c b/drivers/clk/clk-nomadik.c > > > > --- a/drivers/clk/clk-nomadik.c > > > > +++ b/drivers/clk/clk-nomadik.c > > > > @@ -87,7 +87,7 @@ static const struct of_device_id nomadik > > > > > > > > static void __init nomadik_src_init(void) > > > > { > > > > - struct device_node *np; > > > > + struct device_node *np __free(device_node); > > > > u32 val; > > > > > > > > np = of_find_matching_node(NULL, nomadik_src_match); > > > > @@ -134,7 +134,6 @@ static void __init nomadik_src_init(void > > > > register_reboot_notifier(&nomadik_clk_reboot_notifier); > > > > > > > > out_put: > > > Can avoid the label given nothing to do any more. > > > > Indeed. > > > > > > - of_node_put(np); > > > > } > > > > > > > > /** > > > > diff -u -p a/arch/powerpc/platforms/chrp/setup.c b/arch/powerpc/platforms/chrp/setup.c > > > > --- a/arch/powerpc/platforms/chrp/setup.c > > > > +++ b/arch/powerpc/platforms/chrp/setup.c > > > > @@ -99,7 +99,7 @@ static void chrp_show_cpuinfo(struct seq > > > > { > > > > int i, sdramen; > > > > unsigned int t; > > > > - struct device_node *root; > > > > + struct device_node *root __free(device_node); > > > > > > Same as next 2. > > > > > > > const char *model = ""; > > > > > > > > root = of_find_node_by_path("/"); > > > > @@ -152,7 +152,6 @@ static void chrp_show_cpuinfo(struct seq > > > > gg2_cachetypes[(t>>2) & 3], > > > > gg2_cachemodes[t & 3]); > > > > } > > > > - of_node_put(root); > > > > } > > > > > > > > /* > > > > @@ -195,7 +194,7 @@ static void __init sio_fixup_irq(const c > > > > > > > > static void __init sio_init(void) > > > > { > > > > - struct device_node *root; > > > > + struct device_node *root __free(device_node); > > > > > > As below needs to be > > > struct device_node *root __free(device_node) = of_find_node_by_path("/"); > > > so there is no space for code to be added inbetween that might return before > > > this is set. > > > > OK > > > > > > const char *model; > > > > > > > > root = of_find_node_by_path("/"); > > > > @@ -209,8 +208,6 @@ static void __init sio_init(void) > > > > /* select logical device 1 (KBC/Mouse) */ > > > > sio_fixup_irq("mouse", 1, 12, 2); > > > > } > > > > - > > > > - of_node_put(root); > > > > } > > > > > > > > > > > > @@ -364,7 +361,7 @@ static void chrp_8259_cascade(struct irq > > > > */ > > > > static void __init chrp_find_openpic(void) > > > > { > > > > - struct device_node *np, *root; > > > > + struct device_node *np __free(device_node), *root; > > > > > > This one looks dangerous because of the chance of other code > > > getting added between here and the point where it is set. > > > > > > Better to move that down so we have > > > > > > struct device_node *np __free(device_node) = of_find_node_by_type(NULL, "open-pic"); > > > > > > Also there is a nicer use in: > > > https://elixir.bootlin.com/linux/v6.8-rc2/source/arch/powerpc/platforms/chrp/setup.c#L217 > > > > So the point is that we now allow declarations at random places, and not > > only at the top of a function? > > yes. > > https://lore.kernel.org/all/CAHk-=wgRHiV5VSxtfXA4S6aLUmcQYEuB67u3BJPJPtuESs1JyA@mail.gmail.com/ > And onwards in that thread. > > Linus was pretty clear that constructor and destructor should come together > and gave an example at the end of that email > > + lines = gpio_sim_count_lines(bank); > + char **line_names __free(kfree) = kcalloc(lines, sizeof(*line_names), GFP_KERNEL); > > Reality was that the cleanup.h macros were doing this anyway it just was > somewhat hidden in guard() and friends. > > > > > > > > > > int len, i, j; > > > > int isu_size; > > > > const unsigned int *iranges, *opprop = NULL; > > > > @@ -438,7 +435,6 @@ static void __init chrp_find_openpic(voi > > > > ppc_md.get_irq = mpic_get_irq; > > > > bail: > > > > of_node_put(root); > > > With root covered this can return early. > > > > Yes, this could be addressed by running the rule more than once. > > > > Thanks for the feedback! > > Thanks for putting together a script so quickly. I have updated it to take into account most of the mentioned issues (gotos, etc). In my version, I move the call up to the original declaration site, when that is safe to do. A negative aspect is that the original call site may have been preceeded by comments, which get removed. It's hard to know what should be done with comments, so that has to be addressed manually. But if we can leave the call site where it is, the comments should naturally stay there too. There remains the problem of very long lines. Coccinelle has some heuristics for dealing with this, but they don't fit well with this case, where something before the function name is already very long. Anyway, I will send a new version later today to illustrate what can be done. No implication that the semantic patch should be sent off to Linus for immediate application across the kernel. This seems like something that should be checked more carefully, especially since there is the opportunity to fix bugs along the way (I saw one case with a missing of_node_put on an error path). julia > > Jonathan > > > > > julia > > > > > > > > > - of_node_put(np); > > > > } > > > > > > > > > > > >