linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] cxl/port: automate cleanup with __free()
@ 2025-06-23  9:19 Pranav Tyagi
  2025-06-23  9:27 ` Greg KH
  2025-06-24 17:11 ` Dan Williams
  0 siblings, 2 replies; 5+ messages in thread
From: Pranav Tyagi @ 2025-06-23  9:19 UTC (permalink / raw)
  To: dave, jonathan.cameron, dave.jiang, alison.schofield,
	vishal.l.verma, ira.weiny, dan.j.williams
  Cc: ming.li, rrichter, jeff.johnson, peterz, linux-cxl, linux-kernel,
	skhan, linux-kernel-mentees, Pranav Tyagi

Use the scope based resource management (defined in linux/cleanup.h) to
automate the lifetime control of struct cxl_endpoint_decoder. This
eliminates the explicit kfree() call and makes the code more robust and
maintainable in presence of early returns.

Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>
---
 drivers/cxl/core/port.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
index eb46c6764d20..c35946882b20 100644
--- a/drivers/cxl/core/port.c
+++ b/drivers/cxl/core/port.c
@@ -10,6 +10,7 @@
 #include <linux/slab.h>
 #include <linux/idr.h>
 #include <linux/node.h>
+#include <linux/cleanup.h>
 #include <cxl/einj.h>
 #include <cxlmem.h>
 #include <cxlpci.h>
@@ -1888,14 +1889,14 @@ EXPORT_SYMBOL_NS_GPL(cxl_switch_decoder_alloc, "CXL");
  */
 struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port)
 {
-	struct cxl_endpoint_decoder *cxled;
 	struct cxl_decoder *cxld;
 	int rc;
 
 	if (!is_cxl_endpoint(port))
 		return ERR_PTR(-EINVAL);
 
-	cxled = kzalloc(sizeof(*cxled), GFP_KERNEL);
+	struct cxl_endpoint_decoder *cxled __free(kfree) =
+		kzalloc(sizeof(*cxled), GFP_KERNEL);
 	if (!cxled)
 		return ERR_PTR(-ENOMEM);
 
@@ -1904,7 +1905,6 @@ struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port)
 	cxld = &cxled->cxld;
 	rc = cxl_decoder_init(port, cxld);
 	if (rc)	 {
-		kfree(cxled);
 		return ERR_PTR(rc);
 	}
 
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] cxl/port: automate cleanup with __free()
  2025-06-23  9:19 [PATCH] cxl/port: automate cleanup with __free() Pranav Tyagi
@ 2025-06-23  9:27 ` Greg KH
  2025-06-24 13:56   ` Pranav Tyagi
  2025-06-24 17:11 ` Dan Williams
  1 sibling, 1 reply; 5+ messages in thread
From: Greg KH @ 2025-06-23  9:27 UTC (permalink / raw)
  To: Pranav Tyagi
  Cc: dave, jonathan.cameron, dave.jiang, alison.schofield,
	vishal.l.verma, ira.weiny, dan.j.williams, ming.li, rrichter,
	jeff.johnson, peterz, linux-cxl, linux-kernel, skhan,
	linux-kernel-mentees

On Mon, Jun 23, 2025 at 02:49:29PM +0530, Pranav Tyagi wrote:
> Use the scope based resource management (defined in linux/cleanup.h) to
> automate the lifetime control of struct cxl_endpoint_decoder. This
> eliminates the explicit kfree() call and makes the code more robust and
> maintainable in presence of early returns.
> 
> Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>
> ---
>  drivers/cxl/core/port.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
> index eb46c6764d20..c35946882b20 100644
> --- a/drivers/cxl/core/port.c
> +++ b/drivers/cxl/core/port.c
> @@ -10,6 +10,7 @@
>  #include <linux/slab.h>
>  #include <linux/idr.h>
>  #include <linux/node.h>
> +#include <linux/cleanup.h>
>  #include <cxl/einj.h>
>  #include <cxlmem.h>
>  #include <cxlpci.h>
> @@ -1888,14 +1889,14 @@ EXPORT_SYMBOL_NS_GPL(cxl_switch_decoder_alloc, "CXL");
>   */
>  struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port)
>  {
> -	struct cxl_endpoint_decoder *cxled;
>  	struct cxl_decoder *cxld;
>  	int rc;
>  
>  	if (!is_cxl_endpoint(port))
>  		return ERR_PTR(-EINVAL);
>  
> -	cxled = kzalloc(sizeof(*cxled), GFP_KERNEL);
> +	struct cxl_endpoint_decoder *cxled __free(kfree) =
> +		kzalloc(sizeof(*cxled), GFP_KERNEL);
>  	if (!cxled)
>  		return ERR_PTR(-ENOMEM);
>  
> @@ -1904,7 +1905,6 @@ struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port)
>  	cxld = &cxled->cxld;
>  	rc = cxl_decoder_init(port, cxld);
>  	if (rc)	 {
> -		kfree(cxled);
>  		return ERR_PTR(rc);
>  	}
>  
> -- 
> 2.49.0

Note, I can't speak for the maintainers of this subsystem, but
generally, making changes like this for no real good reason, for code
that has been around for years, is really not needed at all.

If you fix a bug with it, sure, but changes for the sake of "let's use
this new feature" in here really might not be necessary.

Why not add cleanup.h support to code paths that actually fix existing
bugs instead?

Also, you have added some coding style errors to the code now with this
patch, which also is generally not considered a good idea :)

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cxl/port: automate cleanup with __free()
  2025-06-23  9:27 ` Greg KH
@ 2025-06-24 13:56   ` Pranav Tyagi
  0 siblings, 0 replies; 5+ messages in thread
From: Pranav Tyagi @ 2025-06-24 13:56 UTC (permalink / raw)
  To: Greg KH
  Cc: dave, jonathan.cameron, dave.jiang, alison.schofield,
	vishal.l.verma, ira.weiny, dan.j.williams, ming.li, rrichter,
	jeff.johnson, peterz, linux-cxl, linux-kernel, skhan,
	linux-kernel-mentees

On Mon, Jun 23, 2025 at 2:57 PM Greg KH <gregkh@linuxfoundation.org> wrote:
>
> On Mon, Jun 23, 2025 at 02:49:29PM +0530, Pranav Tyagi wrote:
> > Use the scope based resource management (defined in linux/cleanup.h) to
> > automate the lifetime control of struct cxl_endpoint_decoder. This
> > eliminates the explicit kfree() call and makes the code more robust and
> > maintainable in presence of early returns.
> >
> > Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>
> > ---
> >  drivers/cxl/core/port.c | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
> > index eb46c6764d20..c35946882b20 100644
> > --- a/drivers/cxl/core/port.c
> > +++ b/drivers/cxl/core/port.c
> > @@ -10,6 +10,7 @@
> >  #include <linux/slab.h>
> >  #include <linux/idr.h>
> >  #include <linux/node.h>
> > +#include <linux/cleanup.h>
> >  #include <cxl/einj.h>
> >  #include <cxlmem.h>
> >  #include <cxlpci.h>
> > @@ -1888,14 +1889,14 @@ EXPORT_SYMBOL_NS_GPL(cxl_switch_decoder_alloc, "CXL");
> >   */
> >  struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port)
> >  {
> > -     struct cxl_endpoint_decoder *cxled;
> >       struct cxl_decoder *cxld;
> >       int rc;
> >
> >       if (!is_cxl_endpoint(port))
> >               return ERR_PTR(-EINVAL);
> >
> > -     cxled = kzalloc(sizeof(*cxled), GFP_KERNEL);
> > +     struct cxl_endpoint_decoder *cxled __free(kfree) =
> > +             kzalloc(sizeof(*cxled), GFP_KERNEL);
> >       if (!cxled)
> >               return ERR_PTR(-ENOMEM);
> >
> > @@ -1904,7 +1905,6 @@ struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port)
> >       cxld = &cxled->cxld;
> >       rc = cxl_decoder_init(port, cxld);
> >       if (rc)  {
> > -             kfree(cxled);
> >               return ERR_PTR(rc);
> >       }
> >
> > --
> > 2.49.0
>
> Note, I can't speak for the maintainers of this subsystem, but
> generally, making changes like this for no real good reason, for code
> that has been around for years, is really not needed at all.
>
> If you fix a bug with it, sure, but changes for the sake of "let's use
> this new feature" in here really might not be necessary.
>
> Why not add cleanup.h support to code paths that actually fix existing
> bugs instead?
>
> Also, you have added some coding style errors to the code now with this
> patch, which also is generally not considered a good idea :)
>
> thanks,
>
> greg k-h

Hi Greg,

Thanks for the feedback.

This patch was intended as a cleanup to improve consistency and
readability, particularly around handling early returns using
cleanup.h. I followed up on similar prior patches that introduced
automatic cleanup in this subsystem and aimed to extend that style
more consistently.

That said, I understand your concern about changing long-standing
code without a strong functional reason. I’ll be more careful going
forward and focus such changes around actual bug fixes.

Regards
Pranav Tyagi

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cxl/port: automate cleanup with __free()
  2025-06-23  9:19 [PATCH] cxl/port: automate cleanup with __free() Pranav Tyagi
  2025-06-23  9:27 ` Greg KH
@ 2025-06-24 17:11 ` Dan Williams
  2025-06-26 14:28   ` Pranav Tyagi
  1 sibling, 1 reply; 5+ messages in thread
From: Dan Williams @ 2025-06-24 17:11 UTC (permalink / raw)
  To: Pranav Tyagi, dave, jonathan.cameron, dave.jiang,
	alison.schofield, vishal.l.verma, ira.weiny, dan.j.williams
  Cc: ming.li, rrichter, jeff.johnson, peterz, linux-cxl, linux-kernel,
	skhan, linux-kernel-mentees, Pranav Tyagi

Pranav Tyagi wrote:
> Use the scope based resource management (defined in linux/cleanup.h) to
> automate the lifetime control of struct cxl_endpoint_decoder. This
> eliminates the explicit kfree() call and makes the code more robust and
> maintainable in presence of early returns.

I do not want to review small standalone conversions of individual
functions for no other reason than vague claims of improvement. The
reasons to convert an existing function to cleanup.h helpers are:

1/ to fix an actual bug

2/ in the course of refactoring the code for other reasons

3/ to eliminate goto trickiness and bulk

This patch does not make the code "more robust and maintainable", it is
just churn given how easy it is to verify that the kfree() is correctly
paired.

One quick way to identify code churn is when the diffstat does not
result in a net reduction in code lines:

This patch is "3 insertions(+), 3 deletions(-)" == "churn".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cxl/port: automate cleanup with __free()
  2025-06-24 17:11 ` Dan Williams
@ 2025-06-26 14:28   ` Pranav Tyagi
  0 siblings, 0 replies; 5+ messages in thread
From: Pranav Tyagi @ 2025-06-26 14:28 UTC (permalink / raw)
  To: Dan Williams
  Cc: dave, jonathan.cameron, dave.jiang, alison.schofield,
	vishal.l.verma, ira.weiny, ming.li, rrichter, jeff.johnson,
	peterz, linux-cxl, linux-kernel, skhan, linux-kernel-mentees

On Tue, Jun 24, 2025 at 10:42 PM Dan Williams <dan.j.williams@intel.com> wrote:
>
> Pranav Tyagi wrote:
> > Use the scope based resource management (defined in linux/cleanup.h) to
> > automate the lifetime control of struct cxl_endpoint_decoder. This
> > eliminates the explicit kfree() call and makes the code more robust and
> > maintainable in presence of early returns.
>
> I do not want to review small standalone conversions of individual
> functions for no other reason than vague claims of improvement. The
> reasons to convert an existing function to cleanup.h helpers are:
>
> 1/ to fix an actual bug
>
> 2/ in the course of refactoring the code for other reasons
>
> 3/ to eliminate goto trickiness and bulk
>
> This patch does not make the code "more robust and maintainable", it is
> just churn given how easy it is to verify that the kfree() is correctly
> paired.
>
> One quick way to identify code churn is when the diffstat does not
> result in a net reduction in code lines:
>
> This patch is "3 insertions(+), 3 deletions(-)" == "churn".

Thank you for the feedback. I understand your concerns and completely
agree with your reasoning. Please pardon my misjudgment in sending this
patch. I am still a beginner with kernel development and learning to
better assess what makes a meaningful contribution.

Regards
Pranav Tyagi

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-06-26 14:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-23  9:19 [PATCH] cxl/port: automate cleanup with __free() Pranav Tyagi
2025-06-23  9:27 ` Greg KH
2025-06-24 13:56   ` Pranav Tyagi
2025-06-24 17:11 ` Dan Williams
2025-06-26 14:28   ` Pranav Tyagi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).