Re: [PATCH v8 08/14] rv: Support embedded target types in per-object DA storage

From: Gabriele Monaco

Date: Fri Nov 20 2026 - 03:12:48 EST


On Fri, 2026-11-06 at 01:49 +0800, wen.yang@xxxxxxxxx wrote:
> From: Wen Yang <wen.yang@xxxxxxxxx>
>
> A PER_OBJ monitor usually typedefs monitor_target itself, as a pointer to
> the monitored object (borrowed: the object exists independently of the
> monitor and outlives the storage).
>
> A monitor whose target is monitor-owned data can instead define
> DA_OBJ_EMBEDDED_TYPE with a complete struct type: the framework then
> derives monitor_target as a pointer to it, embeds one instance in each
> per-object storage, and allocates and frees it together with the storage.
> On storage creation the caller-supplied target is shallow-copied into
> the storage before it is published, so it may be a fully initialised
> stack object.
>
> This allows per-object monitors to manage custom per-instance data without
> maintaining private mempools, complex multi-stage reference counting, or
> out-of-band teardown state machines.
>
> Suggested-by: Gabriele Monaco <gmonaco@xxxxxxxxxx>
> Signed-off-by: Wen Yang <wen.yang@xxxxxxxxx>
> ---

Looks good, just a minor comment.

>  /*
>   * da_get_target - return the object associated to the monitor
> + *
> + * NULL for an embedded target whose storage was pre-allocated but not filled
> + * yet: a manual-allocation monitor publishes its storage before it has one.
>   */
>  static inline monitor_target da_get_target(struct da_monitor *da_mon)
>  {
> - return container_of(da_mon, struct da_monitor_storage, rv.da_mon)-
> >target;
> + struct da_monitor_storage *mon_storage =
> + container_of(da_mon, struct da_monitor_storage, rv.da_mon);
> +
> +#ifdef DA_OBJ_EMBEDDED_TYPE
> + if (unlikely(da_storage_is_empty(mon_storage)))
> + return NULL;
> +#endif

We usually avoid ifdeffery inside functions like plague..
Could you embed this in TARGET_PTR?

#define TARGET_PTR(storage) (unlikely(da_storage_is_empty(mon_storage)) ? NULL : &(storage)->target)

> + return TARGET_PTR(mon_storage);
>  }
>  
>  /*
> @@ -572,8 +635,11 @@ static inline struct da_monitor
> *da_fill_empty_storage(da_id_type id,
>          monitor_target target,
>          struct da_monitor
> *da_mon)
>  {
> - if (unlikely(da_mon && !da_get_target(da_mon)))
> - container_of(da_mon, struct da_monitor_storage, rv.da_mon)-
> >target = target;
> + struct da_monitor_storage *mon_storage =
> + container_of(da_mon, struct da_monitor_storage, rv.da_mon);
> +
> + if (unlikely(da_mon && da_storage_is_empty(mon_storage)))
> + da_storage_set_target(mon_storage, target);
>   return da_mon;
>  }
>  
> @@ -589,7 +655,11 @@ static inline monitor_target
> da_get_target_by_id(da_id_type id)
>  
>   if (unlikely(!mon_storage))
>   return NULL;
> - return mon_storage->target;
> +#ifdef DA_OBJ_EMBEDDED_TYPE
> + if (unlikely(da_storage_is_empty(mon_storage)))
> + return NULL;
> +#endif

Same here.

> + return TARGET_PTR(mon_storage);
>  }

Thanks,
Gabriele

>  
>  static void da_kfree_cb(struct rcu_head *head)
> @@ -635,7 +705,7 @@ da_create_pool_storage(da_id_type id, monitor_target
> target,
>   memset(mon_storage, 0, sizeof(*mon_storage));
>  
>   mon_storage->id = id;
> - mon_storage->target = target;
> + da_storage_set_target(mon_storage, target);
>  
>   hash_add_rcu(da_monitor_ht, &mon_storage->node, id);
>   return &mon_storage->rv.da_mon;