From: Dan Williams <dan.j.williams@intel.com>
To: "Tian, Kevin" <kevin.tian@intel.com>,
"Williams, Dan J" <dan.j.williams@intel.com>,
"peterz@infradead.org" <peterz@infradead.org>,
"torvalds@linux-foundation.org" <torvalds@linux-foundation.org>
Cc: "Bjorn Helgaas" <bhelgaas@google.com>,
"Weiny, Ira" <ira.weiny@intel.com>,
"Jonathan Cameron" <jonathan.cameron@huawei.com>,
"Brandeburg, Jesse" <jesse.brandeburg@intel.com>,
"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
"Wunner, Lukas" <lukas.wunner@intel.com>,
"Jonathan Corbet" <corbet@lwn.net>,
"linux-pci@vger.kernel.org" <linux-pci@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"gregkh@linuxfoundation.org" <gregkh@linuxfoundation.org>,
"linux-doc@vger.kernel.org" <linux-doc@vger.kernel.org>
Subject: RE: [PATCH] cleanup: Add usage and style documentation
Date: Fri, 22 Mar 2024 17:17:30 -0700 [thread overview]
Message-ID: <65fe1f9aadf51_2690d2948f@dwillia2-mobl3.amr.corp.intel.com.notmuch> (raw)
In-Reply-To: <BN9PR11MB52764D455B465E7C69F3A4C38C312@BN9PR11MB5276.namprd11.prod.outlook.com>
Tian, Kevin wrote:
> > From: Dan Williams <dan.j.williams@intel.com>
> > Sent: Thursday, March 21, 2024 6:05 AM
> > + *
> > + * Note that unwind order is dictated by declaration order. That
> > + * contraindicates a pattern like the following:
> > + *
> > + * .. code-block:: c
> > + *
> > + * int num, ret = 0;
> > + * struct pci_dev *bridge = ctrl->pcie->port;
> > + * struct pci_bus *parent = bridge->subordinate;
> > + * struct pci_dev *dev __free(pci_dev_put) = NULL;
> > + *
> > + * pci_lock_rescan_remove();
> > + *
> > + * dev = pci_get_slot(parent, PCI_DEVFN(0, 0));
> > + *
> > + * In this case @dev is declared in x-mas tree style in a preamble
> > + * declaration block. That is problematic because it destroys the
> > + * compiler's ability to infer proper unwind order. If other cleanup
> > + * helpers appeared in such a function that depended on @dev being live
> > + * to complete their unwind then using the "struct obj_type *obj
> > + * __free(...) = NULL" style is an anti-pattern that potentially causes
> > + * a use-after-free bug. Instead, the expectation is this conversion:
> > + *
>
> an example of dependent cleanup helpers might be helpful to
> better understand this expectation?
The simplest example I can think of to show the danger of the
"__free(...) = NULL" causing cleanup inter-dependency problems is the
following:
---
LIST_HEAD(list);
DEFINE_MUTEX(lock);
struct object {
struct list_head node;
};
static struct object *alloc_add(void)
{
struct object *obj;
lockdep_assert_held(&lock);
obj = kfree(sizeof(*obj), GFP_KERNEL);
if (obj) {
LIST_HEAD_INIT(&obj->node);
list_add(obj->node, &list):
}
return obj;
}
static void remove_free(struct object *obj)
{
lockdep_assert_held(&lock);
list_del(&obj->node);
kfree(obj);
}
DEFINE_FREE(remove_free, struct object *, if (_T) remove_free(_T))
static int init(void)
{
struct object *obj __free(remove_free) = NULL;
int err;
guard(mutex)(lock);
obj = alloc_add();
if (!obj)
return -ENOMEM;
err = other_init(obj);
if (err)
return err; // remove_free() called without the lock!!
no_free_ptr(obj);
return 0;
}
---
The fix for this bug is to replace the "__free(...) = NULL" pattern and
move the assignment to the declaration.
guard(mutex)(lock);
struct object *obj __free(remove_free) = alloc_add();
...so the compiler can observe LIFO order on the unwind. Yes, no one
should write code like this, all of the init should happen before
assigning to a list, but hopefully it illustrates the point.
next prev parent reply other threads:[~2024-03-23 0:17 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-20 22:04 Dan Williams
2024-03-22 5:43 ` Tian, Kevin
2024-03-23 0:17 ` Dan Williams [this message]
2024-03-23 18:01 ` Markus Elfring
2024-03-22 9:06 ` Peter Zijlstra
2024-03-22 19:10 ` Dan Williams
2024-03-23 20:44 ` Matthew Wilcox
2024-03-24 0:57 ` Dan Williams
2024-03-24 6:23 ` Lukas Wunner
2024-03-24 9:08 ` Jonathan Corbet
2024-03-24 20:37 ` Dan Williams
2024-03-22 13:00 ` Markus Elfring
2024-03-22 13:48 ` Greg Kroah-Hartman
2024-03-22 13:34 ` Bjorn Helgaas
2024-03-25 18:52 ` Dan Williams
2024-03-22 13:45 ` Matthew Wilcox
2024-03-25 22:57 ` [PATCH v2] " Dan Williams
2024-03-26 12:06 ` Markus Elfring
2024-03-26 15:35 ` Jonathan Corbet
2024-03-26 16:51 ` Dan Williams
2024-03-26 16:56 ` Jonathan Cameron
2024-03-26 17:49 ` Dan Williams
2024-03-26 17:53 ` Dan Williams
2024-03-29 23:48 ` [PATCH v3] " Dan Williams
2024-03-30 20:23 ` Markus Elfring
2024-04-01 8:19 ` Tian, Kevin
2024-04-02 7:15 ` [v3] " Markus Elfring
2024-08-02 13:58 ` [PATCH v3] " Jonathan Cameron
2024-08-02 15:07 ` Peter Zijlstra
2024-08-06 10:51 ` [tip: locking/core] " tip-bot2 for Dan Williams
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=65fe1f9aadf51_2690d2948f@dwillia2-mobl3.amr.corp.intel.com.notmuch \
--to=dan.j.williams@intel.com \
--cc=bhelgaas@google.com \
--cc=corbet@lwn.net \
--cc=gregkh@linuxfoundation.org \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=ira.weiny@intel.com \
--cc=jesse.brandeburg@intel.com \
--cc=jonathan.cameron@huawei.com \
--cc=kevin.tian@intel.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lukas.wunner@intel.com \
--cc=peterz@infradead.org \
--cc=torvalds@linux-foundation.org \
/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
all inboxes | Powered by JetHome®