From: "Roger Pau Monné" <roger.pau@citrix.com>
To: Jan Beulich <jbeulich@suse.com>
Cc: Volodymyr Babchuk <Volodymyr_Babchuk@epam.com>,
"xen-devel@lists.xenproject.org" <xen-devel@lists.xenproject.org>,
Andrew Cooper <andrew.cooper3@citrix.com>, Wei Liu <wl@xen.org>,
George Dunlap <george.dunlap@citrix.com>,
Julien Grall <julien@xen.org>,
Stefano Stabellini <sstabellini@kernel.org>,
Paul Durrant <paul@xen.org>, Kevin Tian <kevin.tian@intel.com>
Subject: Re: [PATCH v3 2/6] xen: pci: introduce reference counting for pdev
Date: Wed, 29 Mar 2023 12:48:06 +0200 [thread overview]
Message-ID: <ZCQXZu8AqZtLGCUB@Air-de-Roger> (raw)
In-Reply-To: <c175d1b5-972c-e311-d634-e7a68e919ece@suse.com>
On Wed, Mar 29, 2023 at 11:55:26AM +0200, Jan Beulich wrote:
> On 16.03.2023 17:16, Roger Pau Monné wrote:
> > On Tue, Mar 14, 2023 at 08:56:29PM +0000, Volodymyr Babchuk wrote:
> >> Prior to this change, lifetime of pci_dev objects was protected by global
> >> pcidevs_lock(). Long-term plan is to remove this log, so we need some
> > ^ lock
> >
> > I wouldn't say remove, as one way or another we need a lock to protect
> > concurrent accesses.
> >
> >> other mechanism to ensure that those objects will not disappear under
> >> feet of code that access them. Reference counting is a good choice as
> >> it provides easy to comprehend way to control object lifetime.
> >>
> >> This patch adds two new helper functions: pcidev_get() and
> >> pcidev_put(). pcidev_get() will increase reference counter, while
> >> pcidev_put() will decrease it, destroying object when counter reaches
> >> zero.
> >>
> >> pcidev_get() should be used only when you already have a valid pointer
> >> to the object or you are holding lock that protects one of the
> >> lists (domain, pseg or ats) that store pci_dev structs.
> >>
> >> pcidev_get() is rarely used directly, because there already are
> >> functions that will provide valid pointer to pci_dev struct:
> >> pci_get_pdev(), pci_get_real_pdev(). They will lock appropriate list,
> >> find needed object and increase its reference counter before returning
> >> to the caller.
> >>
> >> Naturally, pci_put() should be called after finishing working with a
> >> received object. This is the reason why this patch have so many
> >> pcidev_put()s and so little pcidev_get()s: existing calls to
> >> pci_get_*() functions now will increase reference counter
> >> automatically, we just need to decrease it back when we finished.
> >
> > After looking a bit into this, I would like to ask whether it's been
> > considered the need to increase the refcount for each use of a pdev.
> >
> > For example I would consider the initial alloc_pdev() to take a
> > refcount, and then pci_remove_device() _must_ be the function that
> > removes the last refcount, so that it can return -EBUSY otherwise (see
> > my comment below).
>
> I thought I had replied to this, but couldn't find any record thereof;
> apologies for a possible duplicate.
>
> In a get-/put-ref model, much like we have it for domheap pages, the
> last put should trigger whatever is needed for "freeing" (here:
> removing) the item. Therefore I think in this new model all
> PHYSDEVOP_{pci_device_remove,manage_pci_remove} should cause is the
> dropping of the ref that alloc_pdev() has put in place (plus some
> marking of the device, so that another PHYSDEVOP_{pci_device_remove,
> manage_pci_remove} can be properly ignored rather than dropping one
> ref too many; this marking may then also prevent the obtaining of new
> references, if such can be arranged for without breaking [cleanup]
> functionality elsewhere). Whenever the last reference is put, that
> would trigger the operations that pci_remove_device() presently
> carries out.
Right, this all seems sensible.
>
> Of course this would mean that if PHYSDEVOP_{pci_device_remove,
> manage_pci_remove} didn't drop the last reference, it would need to
> signal this to its caller, for it to be aware that the device is not
> yet ready for (e.g.) hot-unplug. There'll then also need to be a way
> for the caller to figure out when that situation has changed (which
> might be via repeated invocations of the same hypercall sub-op, or
> some new sub-op).
Returning -EBUSY and expecting the caller to repeat the call would
likely be the easier one to implement and likely fine for our
purposes. There's a risk that the toolstack/kernel enters an infinite
loop if there's a dangling extra ref somewhere, but that would be a
bug anyway.
So device creation would take a reference, and device assignation would
take another one. Devices assigned are safe against removal, so there
should be no need to take an extra reference in that case.
There are however a number of cases that use pci_get_pdev(NULL, ...)
for example, at which point we would need to take an extra reference
on those cases if the device is not assigned to a domain?
Or would we just keep those under pcidevs_locked regions as-is?
(as PHYSDEVOP_{pci_device_remove, manage_pci_remove} will still take
the pci_lock).
Thanks, Roger.
next prev parent reply other threads:[~2023-03-29 10:48 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-14 20:56 [PATCH v3 0/6] vpci: first series in preparation for vpci on ARM Volodymyr Babchuk
2023-03-14 20:56 ` [PATCH v3 2/6] xen: pci: introduce reference counting for pdev Volodymyr Babchuk
2023-03-16 16:16 ` Roger Pau Monné
2023-03-29 9:55 ` Jan Beulich
2023-03-29 10:48 ` Roger Pau Monné [this message]
2023-03-29 11:58 ` Jan Beulich
2023-04-11 23:41 ` Volodymyr Babchuk
2023-04-12 9:13 ` Roger Pau Monné
2023-04-12 21:54 ` Volodymyr Babchuk
2023-04-13 15:00 ` Roger Pau Monné
2023-04-14 1:30 ` Volodymyr Babchuk
2023-04-17 10:17 ` Roger Pau Monné
2023-04-17 10:34 ` Jan Beulich
2023-04-17 10:51 ` Roger Pau Monné
2023-04-17 11:02 ` Jan Beulich
2023-04-21 11:00 ` Volodymyr Babchuk
2023-04-21 12:24 ` Jan Beulich
2023-04-21 13:02 ` Volodymyr Babchuk
2023-04-21 13:10 ` Roger Pau Monné
2023-04-21 14:13 ` Volodymyr Babchuk
2023-04-24 7:46 ` Jan Beulich
2023-04-24 14:15 ` Volodymyr Babchuk
2023-04-24 14:27 ` Jan Beulich
2023-03-29 10:04 ` Jan Beulich
2023-03-14 20:56 ` [PATCH v3 1/6] xen: add reference counter support Volodymyr Babchuk
2023-03-16 13:54 ` Roger Pau Monné
2023-03-16 14:03 ` Jan Beulich
2023-03-16 16:21 ` Roger Pau Monné
2023-04-11 22:27 ` Volodymyr Babchuk
2023-04-12 10:12 ` Roger Pau Monné
2023-03-16 16:19 ` Roger Pau Monné
2023-03-16 16:32 ` Jan Beulich
2023-03-16 16:39 ` Roger Pau Monné
2023-03-16 16:43 ` Jan Beulich
2023-03-16 16:48 ` Roger Pau Monné
2023-03-16 16:56 ` Jan Beulich
2023-03-17 10:05 ` Roger Pau Monné
2023-03-17 14:46 ` Jan Beulich
2023-03-16 17:01 ` Jan Beulich
2023-04-11 22:38 ` Volodymyr Babchuk
2023-04-17 6:47 ` Jan Beulich
2023-03-14 20:56 ` [PATCH v3 6/6] xen: pci: print reference counter when dumping pci_devs Volodymyr Babchuk
2023-03-17 8:46 ` Roger Pau Monné
2023-03-14 20:56 ` [PATCH v3 3/6] vpci: crash domain if we wasn't able to (un) map vPCI regions Volodymyr Babchuk
2023-03-16 16:32 ` Roger Pau Monné
2023-03-14 20:56 ` [PATCH v3 5/6] vpci: use reference counter to protect vpci state Volodymyr Babchuk
2023-03-17 8:43 ` Roger Pau Monné
2023-03-29 9:31 ` Jan Beulich
2023-03-14 20:56 ` [PATCH v3 4/6] vpci: restrict unhandled read/write operations for guests Volodymyr Babchuk
2023-03-17 8:37 ` Roger Pau Monné
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=ZCQXZu8AqZtLGCUB@Air-de-Roger \
--to=roger.pau@citrix.com \
--cc=Volodymyr_Babchuk@epam.com \
--cc=andrew.cooper3@citrix.com \
--cc=george.dunlap@citrix.com \
--cc=jbeulich@suse.com \
--cc=julien@xen.org \
--cc=kevin.tian@intel.com \
--cc=paul@xen.org \
--cc=sstabellini@kernel.org \
--cc=wl@xen.org \
--cc=xen-devel@lists.xenproject.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.