[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v3 1/6] xen: add reference counter support


  • To: Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>
  • From: Roger Pau Monné <roger.pau@xxxxxxxxxx>
  • Date: Thu, 16 Mar 2023 17:19:17 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=IJRdktpNfP/hPtrS7zLScKntJ5YCXTuw/OzOXfVVNkU=; b=eCFPilRLz+YOuxhYahV+h5vIwZ27Q37FgAB7h+84oKE4t/FYUByTbWbH5yHsepA+pYNw2TOzgbFPEgY/eOuqc0aPXFEo69V3W5b/pxI/cqLRMbJsH1ce9HpqltOY5+sJDfdIDflpc5Y9Xf4Y5Dti8TqStgcjPjlMh8oPImYWq8ZlcCoTBwXgZyNdKCZXI5Bk04GNJ1OOIl4r/rY6abz1oTVWKnHF887H2h9x0eLGW7yr2vIRVHW3sXG83Lw0r+VWsmiGbJgbxfisOTZeAfifZFXiYTxJvaGSaRnfUUNkuxCfNrIqcOsEG+1BDdcX371WkbTTgJwqZH7RPdeVrG/7BQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oR5xutBHJ0mFE2640Bce6GREINtr2LjmKo6UNRbM1fcUlFBVPUn4J+G0G7alta4WNhOiDMqUeLKTzH2hikZhnGptIL98tcGxbFos8U9iRtqj1gv8zlgQOW0opaN4rBEfYIl/0WCKpv8C1R+pF+NnaVNhRxtZnAw+QKDHd42KARblHEb/rcFYH4lcT2cufohjFAFvnIg18LeoTCCSyaMyZGRLzNnJW1aqxdmVauwK8WR6TKe+saGjpvH7c7j5xLJpskn5dIX4JaLwhlQ8HxhdeTiXU67uOnpLw63xxaiDClw6rIgoQI63gkzOSn2DB1EQ6aCv3aJxiLv4cltFYD+Vdw==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Cc: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, George Dunlap <george.dunlap@xxxxxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Julien Grall <julien@xxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>
  • Delivery-date: Thu, 16 Mar 2023 16:19:35 +0000
  • Ironport-data: A9a23:a0oCVq9tHnAW1nONpR5/DrUDYn+TJUtcMsCJ2f8bNWPcYEJGY0x3x jdLCjqBa/yIN2v8Ldp2a9i28BgA75/dx4IySFZo/3o8E34SpcT7XtnIdU2Y0wF+jCHgZBk+s 5hBMImowOQcFCK0SsKFa+C5xZVE/fjUAOG6UKicYXoZqTZMEE8JkQhkl/MynrlmiN24BxLlk d7pqojUNUTNNwRcawr40Ire7kI/1BjOkGlA5AdmPqkV5AC2e0Q9V/rzG4ngdxMUfaEMdgKKb 76r5K20+Grf4yAsBruN+losWhRXKlJ6FVHmZkt+A8BOsDAbzsAB+v9T2M4nQVVWk120c+VZk 72hg3ASpTABZcUgkMxFO/VR/roX0aduoNcrKlDn2SCfItGvn9IBDJyCAWlvVbD09NqbDkliy /UqcAw8cCvduL6K5L6aY/lcuOg8eZyD0IM34hmMzBn/JNN/GNXoZPyP4tVVmjAtmspJAPDSI dIDbiZiZwjBZBsJPUoLDJU5n6GjgXyXnz9w8QrJ4/ZopTWNilUuitABM/KMEjCObd9SkUuC4 HrP4kzyAw0ANczZwj2Amp6prraXxHumBtxCRdVU8NZAvl7P2XBIBSZJdlyFj+OV0HKaX9R2f hl8Fi0G6PJaGFaQZuf6Wxq0sXuVpCk2UtBbE/A5wAyVw6+S6AGcbkAUQzgEZNE4ucseQT0xy kTPj97vHSZosrCeVTSa7Lj8hTG4NDURLGQCTTQZVgZD6N7myKkxkxbOQ9BLAKOzyNrvFlnY2 CuWpSIzg7ESi88j1Kih+13DxTW2qfDhUQod9gjRGGW/4WtEiJWNYoWp7R3Q6q9GJYPAF12Z5 iFbwY6Z8fwECoyLmGqVWuIREbq15vGDdjrBnVpoGJpn/DOok5K+Qb1tDPhFDB8BGq45lfXBO Sc/ZSs5CEdvAUaX
  • Ironport-hdrordr: A9a23:FLoUmatzu1+FeVIzl8ch35hX7skDctV00zEX/kB9WHVpm62j+/ xG+c5x6faaslkssR0b9+xoQZPwJk80l6QU3WBhB9aftWDd0QPDQb2KhrGSoAEIdReOk9J15O NNdLV/Fc21LXUSt7ef3OBgKadD/OW6
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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@xxxxxxxx>
> 
> ---
> 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.



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.