From: "Roger Pau Monné" <roger.pau@citrix.com>
To: Volodymyr Babchuk <Volodymyr_Babchuk@epam.com>
Cc: "xen-devel@lists.xenproject.org" <xen-devel@lists.xenproject.org>,
Andrew Cooper <andrew.cooper3@citrix.com>,
George Dunlap <george.dunlap@citrix.com>,
Jan Beulich <jbeulich@suse.com>, Julien Grall <julien@xen.org>,
Stefano Stabellini <sstabellini@kernel.org>, Wei Liu <wl@xen.org>
Subject: Re: [PATCH v3 1/6] xen: add reference counter support
Date: Thu, 16 Mar 2023 17:19:17 +0100 [thread overview]
Message-ID: <ZBNBhS2I4XWkwXr0@Air-de-Roger> (raw)
In-Reply-To: <20230314205612.3703668-2-volodymyr_babchuk@epam.com>
On Tue, Mar 14, 2023 at 08:56:29PM +0000, Volodymyr Babchuk wrote:
> We can use reference counter to ease up object lifetime management.
> This patch adds very basic support for reference counters. refcnt
> should be used in the following way:
>
> 1. Protected structure should have refcnt_t field
>
> 2. This field should be initialized with refcnt_init() during object
> construction.
>
> 3. If code holds a valid pointer to a structure/object it can increase
> refcount with refcnt_get(). No additional locking is required.
>
> 4. Code should call refcnt_put() before dropping pointer to a
> protected structure. `destructor` is a call back function that should
> destruct object and free all resources, including structure protected
> itself. Destructor will be called if reference counter reaches zero.
>
> 5. If code does not hold a valid pointer to a protected structure it
> should use other locking mechanism to obtain a pointer. For example,
> it should lock a list that hold protected objects.
>
> Signed-off-by: Volodymyr Babchuk <volodymyr_babchuk@epam.com>
>
> ---
> v3:
> - moved in from another patch series
> - used structure to encapsulate refcnt_t
> - removed #ifndef NDEBUG in favor of just calling ASSERT
> - added assertion to refcnt_put
> - added saturation support: code catches overflow and underflow
> - added EMACS magic at end of the file
> - fixed formatting
> ---
> xen/include/xen/refcnt.h | 59 ++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 59 insertions(+)
> create mode 100644 xen/include/xen/refcnt.h
>
> diff --git a/xen/include/xen/refcnt.h b/xen/include/xen/refcnt.h
> new file mode 100644
> index 0000000000..1ac05d927c
> --- /dev/null
> +++ b/xen/include/xen/refcnt.h
> @@ -0,0 +1,59 @@
> +#ifndef __XEN_REFCNT_H__
> +#define __XEN_REFCNT_H__
> +
> +#include <asm/atomic.h>
> +#include <asm/bug.h>
> +
> +#define REFCNT_SATURATED (INT_MIN / 2)
> +
> +typedef struct {
> + atomic_t refcnt;
> +} refcnt_t;
> +
> +static inline void refcnt_init(refcnt_t *refcnt)
> +{
> + atomic_set(&refcnt->refcnt, 1);
> +}
> +
> +static inline int refcnt_read(refcnt_t *refcnt)
> +{
> + return atomic_read(&refcnt->refcnt);
> +}
> +
> +static inline void refcnt_get(refcnt_t *refcnt)
> +{
> + int old = atomic_add_unless(&refcnt->refcnt, 1, 0);
Occurred to me while looking at the next patch:
Don't you also need to print a warning (and saturate the counter
maybe?) if old == 0, as that would imply the caller is attempting
to take a reference of an object that should be destroyed? IOW: it
would point to some kind of memory leak.
Thanks, Roger.
next prev parent reply other threads:[~2023-03-16 16:19 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 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é [this message]
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 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é
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 4/6] vpci: restrict unhandled read/write operations for guests Volodymyr Babchuk
2023-03-17 8:37 ` 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 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é
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=ZBNBhS2I4XWkwXr0@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=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.