From mboxrd@z Thu Jan 1 00:00:00 1970 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 70A2C34550; Sun, 21 Jan 2024 16:39:00 +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=1705855140; cv=none; b=Z203qOX4aY3YTCpvFpmJXiKg/7m+bXi6W7Hhz5dCb8pCZSJycaF9Jk86gZXzLVHUJ8oXiBF1QWeMT88wEHfOu/BQQS+HWfWy6bDm4YncbISOxLl+CZi1qc/+Ikug4VkxQGYx9hRUi5xvWhG+IZyD9lGgfuYCRX7WNCIWi6kvaC0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705855140; c=relaxed/simple; bh=IveSG9w2cySCDzTVB6sh7mkjc06OisQW+mSDCMXOLTQ=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dkiYzMSJVVm1raekvPiJILgnsPPT504uY5bO8C83uM/Xs3LApvaLUP4N9ld7nz+gUvGuikrRLKr74buYPETMxYz0/FmTqK8PKDPFba7BmwPlrLMvDTRNT7goD/NQxG7R/j6IEiUFFDaGwIhPJPuCYZgLRc+mSIYEnQ8DB61mOKM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CpBc+JVJ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CpBc+JVJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 70280C43390; Sun, 21 Jan 2024 16:38:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705855140; bh=IveSG9w2cySCDzTVB6sh7mkjc06OisQW+mSDCMXOLTQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=CpBc+JVJSlhBQ++UpKAxuK3aWFzgongPgBPheyLZpm0Gr/lmsMP8smI/rWHnFWln9 gXP5B35l3xiIIG3sv7Ac+ANutHxTX92wBLzYBT0OoTwg7PgACeylFVF4ibGglWWSpT teOS5olEAfx/CAtYQ6+NAh2YLpzYz0EN63TorxqPHoNxPbil06SylFvChS1dSAIkWy lM9NGtBv17BQbIuf6sIOzXHspzC8iHQ0Quu+Idi5puUAonoboi+CGLBWd0z85MUSKH 6Rt1+8M7aMzfqsfUmu7bWxbc5AQvtGkrtcre2zUY/gsLW0SBkiNEDx+7uOA5dCzdvO Rph/YbH0FRnqQ== Date: Sun, 21 Jan 2024 16:38:29 +0000 From: Jonathan Cameron To: Andy Shevchenko Cc: linux-iio@vger.kernel.org, "Rafael J . Wysocki" , Len Brown , linux-acpi@vger.kernel.org, Greg Kroah-Hartman , Daniel Scally , Heikki Krogerus , Sakari Ailus , Nuno =?UTF-8?B?U8Oh?= , Cosmin Tanislav , Mihail Chindris , Rasmus Villemoes , Tomislav Denis , Marek Vasut , Olivier Moysan , Fabrice Gasnier , Lad Prabhakar , Dmitry Baryshkov , Marijn Suijten , Marius Cristea , Ibrahim Tilki , Jonathan Cameron , Rob Herring Subject: Re: [PATCH 01/13] device property: Add cleanup.h based fwnode_handle_put() scope based cleanup. Message-ID: <20240121163714.3670498f@jic23-huawei> In-Reply-To: References: <20240114172009.179893-1-jic23@kernel.org> <20240114172009.179893-2-jic23@kernel.org> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.40; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sun, 21 Jan 2024 14:28:47 +0200 Andy Shevchenko wrote: > On Sun, Jan 14, 2024 at 05:19:57PM +0000, Jonathan Cameron wrote: > > From: Jonathan Cameron > > > > This allows the following > > > > struct fwnode_handle *child __free(kfree) = NULL; That's garbage. Should be __free(fwnode_handle)! > > > > device_for_each_child_node(dev, child) { > > if (false) > > return -EINVAL; > > } > > > > without the fwnode_handle_put() call which tends to complicate early > > exits from such loops and lead to resource leak bugs. > > > > Can also be used where the fwnode_handle was obtained from a call > > such as fwnode_find_reference() as it will safely do nothing if > > IS_ERR() is true. > > ... > > > struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode); > > void fwnode_handle_put(struct fwnode_handle *fwnode); > > I would add a blank line here > > > +DEFINE_FREE(fwnode_handle, struct fwnode_handle *, > > + if (!IS_ERR_OR_NULL(_T)) fwnode_handle_put(_T)) > > > > int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index); > > int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name); > > With the above, > Reviewed-by: Andy Shevchenko > Thanks Andy - however.. The discussion with Rob about the DT equivalent took an interesting turn. He raised the concern that the __free was not always tightly coupled with the equivalent of device_for_each_child_node() which as per similar discussions elsewhere results in: a) Potentially wrong ordering if there is other cleanup.h based stuff going on in the same function. b) A lack of association between the setup of the free and what it is undoing. (this was the one Rob pointed at). I proposed two options that here map to 1) Always drag the declaration next to the device_for_each_child_node() and intentionally don't set it to NULL. { .... stuff.... struct fwnode_handle *child __free(fwnode); device_for_each_child_node(dev, child) { } 2) Scoped version of the loops themselves. #define device_for_each_child_node_scoped(dev, child) \ for (struct fw_node_handle *child __free(fwnode_handle) \ = device_get_next_child_node(dev, NULL); \ child; child = device_get_next_child_node(dev, child)) So that the child only exists at all in the scope of the loop. What do you think of the options? DT thread is here: https://lore.kernel.org/linux-iio/20240114165358.119916-1-jic23@kernel.org/T/#t Jonathan