From: Jonathan Cameron <Jonathan.Cameron@Huawei.com>
To: Robert Richter <rrichter@amd.com>
Cc: Dan Williams <dan.j.williams@intel.com>,
Dave Jiang <dave.jiang@intel.com>, <linux-cxl@vger.kernel.org>,
<ira.weiny@intel.com>, <vishal.l.verma@intel.com>,
<alison.schofield@intel.com>, <dave@stgolabs.net>
Subject: Re: [PATCH] cxl: Clarify root_port cleanup routine for cxl_qos_class_verify()
Date: Mon, 8 Jan 2024 12:49:34 +0000 [thread overview]
Message-ID: <20240108124934.00007e6e@Huawei.com> (raw)
In-Reply-To: <ZZvjHJFELV-tiasa@rric.localdomain>
On Mon, 8 Jan 2024 12:57:16 +0100
Robert Richter <rrichter@amd.com> wrote:
> On 08.01.24 11:45:57, Jonathan Cameron wrote:
> > On Thu, 4 Jan 2024 11:52:55 -0800
> > Dan Williams <dan.j.williams@intel.com> wrote:
> >
> > > Dave Jiang wrote:
> > > > The current __free() call for root_port in cxl_qos_class_verify() depends
> > > > on 'struct device' to be the first member of 'struct cxl_port' and calls
> > > > put_device() directly with the root_port pointer passed in. Add a helper
> > > > function put_cxl_port() to handle the put_device() properly and avoid
> > > > future issues if the 'struct device' member moves.
> > > >
> > > > Suggested-by: Robert Richter <rrichter@amd.com>
> > > > Signed-off-by: Dave Jiang <dave.jiang@intel.com>
> > > > ---
> > > > drivers/cxl/core/cdat.c | 12 +++++++++++-
> > > > 1 file changed, 11 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/drivers/cxl/core/cdat.c b/drivers/cxl/core/cdat.c
> > > > index cd84d87f597a..d6e64570032f 100644
> > > > --- a/drivers/cxl/core/cdat.c
> > > > +++ b/drivers/cxl/core/cdat.c
> > > > @@ -345,11 +345,21 @@ static void discard_dpa_perf(struct list_head *list)
> > > > }
> > > > DEFINE_FREE(dpa_perf, struct list_head *, if (!list_empty(_T)) discard_dpa_perf(_T))
> > > >
> > > > +static void put_cxl_port(struct cxl_port *port)
> > > > +{
> > > > + if (!port)
> > > > + return;
> > > > +
> > > > + put_device(&port->dev);
> > > > +}
> > > > +
> > > > +DEFINE_FREE(cxl_port, struct cxl_port *, put_cxl_port(_T))
> > >
> > > I don't think there are other cases where a port reference is acquired
> > > at runtime, so this should be root specific, i.e. put_cxl_root().
> > >
> > > This also merits a NULL check to skip calling put_cxl_root() if the
> > > pointer is already NULL:
> > >
> > > DEFINE_FREE(put_cxl_root, struct cxl_port *, if (_T) put_cxl_root(_T))
> > >
> > > ...for example if someone used no_free_ptr() to return the found root
> > > port.
> > Hi Dan,
> >
> > Sorry for late reply - been distracted and only now playing catch up.
> >
> >
> > I'm curious on this mostly because I got similar review feedback on another
> > case without the if (_T) and conversely yet another review asking me
> > to drop it as pointless (totally unrelated bits of the kernel ;)
> > Why do we care given put_cxl_port() has that check? It's clearly harmless
> > but also at first glance pointless.
>
> There is some description in include/linux/cleanup.h:
>
> * NOTE: the DEFINE_FREE()'s @free expression includes a NULL test even though
> * kfree() is fine to be called with a NULL value. This is on purpose. This way
> * the compiler sees the end of our alloc_obj() function as:
> *
> * tmp = p;
> * p = NULL;
> * if (p)
> * kfree(p);
> * return tmp;
> *
> * And through the magic of value-propagation and dead-code-elimination, it
> * eliminates the actual cleanup call and compiles into:
> *
> * return p;
> *
> * Without the NULL test it turns into a mess and the compiler can't help us.
>
> So afaiu if the check is local the compiler optimizes to not call the
> function at all when using return_ptr(p) (end maybe if NULL
> preinitialized).
>
Thanks! I just saw that Dan also referenced an email from Peter Z
that said the same in the PCI discussion.
Re: [PATCH v5 8/9] PCI: Define scoped based management functions
Jonathan
> -Robert
prev parent reply other threads:[~2024-01-08 12:49 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-04 16:08 [PATCH] cxl: Clarify root_port cleanup routine for cxl_qos_class_verify() Dave Jiang
2024-01-04 17:05 ` Alison Schofield
2024-01-04 18:22 ` Robert Richter
2024-01-04 18:26 ` Dave Jiang
2024-01-04 19:52 ` Dan Williams
2024-01-08 11:45 ` Jonathan Cameron
2024-01-08 11:57 ` Robert Richter
2024-01-08 12:49 ` Jonathan Cameron [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240108124934.00007e6e@Huawei.com \
--to=jonathan.cameron@huawei.com \
--cc=alison.schofield@intel.com \
--cc=dan.j.williams@intel.com \
--cc=dave.jiang@intel.com \
--cc=dave@stgolabs.net \
--cc=ira.weiny@intel.com \
--cc=linux-cxl@vger.kernel.org \
--cc=rrichter@amd.com \
--cc=vishal.l.verma@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox