Commit 0a020d41 authored by Jiri Pirko's avatar Jiri Pirko Committed by David S. Miller
Browse files

lib: introduce initial implementation of object aggregation manager



This lib tracks objects which could be of two types:
1) root object
2) nested object - with a "delta" which differentiates it from
                   the associated root object
The objects are tracked by a hashtable and reference-counted. User is
responsible of implementing callbacks to create/destroy root entity
related to each root object and callback to create/destroy nested object
delta.
Signed-off-by: default avatarJiri Pirko <jiri@mellanox.com>
Signed-off-by: default avatarIdo Schimmel <idosch@mellanox.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 7dc5a0ee
......@@ -10679,6 +10679,14 @@ L: linux-nfc@lists.01.org (moderated for non-subscribers)
S: Supported
F: drivers/nfc/nxp-nci
OBJAGG
M: Jiri Pirko <jiri@mellanox.com>
L: netdev@vger.kernel.org
S: Supported
F: lib/objagg.c
F: lib/test_objagg.c
F: include/linux/objagg.h
OBJTOOL
M: Josh Poimboeuf <jpoimboe@redhat.com>
M: Peter Zijlstra <peterz@infradead.org>
......
/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
/* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
#ifndef _OBJAGG_H
#define _OBJAGG_H
struct objagg_ops {
size_t obj_size;
void * (*delta_create)(void *priv, void *parent_obj, void *obj);
void (*delta_destroy)(void *priv, void *delta_priv);
void * (*root_create)(void *priv, void *obj);
void (*root_destroy)(void *priv, void *root_priv);
};
struct objagg;
struct objagg_obj;
const void *objagg_obj_root_priv(const struct objagg_obj *objagg_obj);
const void *objagg_obj_delta_priv(const struct objagg_obj *objagg_obj);
const void *objagg_obj_raw(const struct objagg_obj *objagg_obj);
struct objagg_obj *objagg_obj_get(struct objagg *objagg, void *obj);
void objagg_obj_put(struct objagg *objagg, struct objagg_obj *objagg_obj);
struct objagg *objagg_create(const struct objagg_ops *ops, void *priv);
void objagg_destroy(struct objagg *objagg);
struct objagg_obj_stats {
unsigned int user_count;
unsigned int delta_user_count; /* includes delta object users */
};
struct objagg_obj_stats_info {
struct objagg_obj_stats stats;
struct objagg_obj *objagg_obj; /* associated object */
bool is_root;
};
struct objagg_stats {
unsigned int stats_info_count;
struct objagg_obj_stats_info stats_info[];
};
const struct objagg_stats *objagg_stats_get(struct objagg *objagg);
void objagg_stats_put(const struct objagg_stats *objagg_stats);
#endif
/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
/* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
#undef TRACE_SYSTEM
#define TRACE_SYSTEM objagg
#if !defined(__TRACE_OBJAGG_H) || defined(TRACE_HEADER_MULTI_READ)
#define __TRACE_OBJAGG_H
#include <linux/tracepoint.h>
struct objagg;
struct objagg_obj;
TRACE_EVENT(objagg_create,
TP_PROTO(const struct objagg *objagg),
TP_ARGS(objagg),
TP_STRUCT__entry(
__field(const void *, objagg)
),
TP_fast_assign(
__entry->objagg = objagg;
),
TP_printk("objagg %p", __entry->objagg)
);
TRACE_EVENT(objagg_destroy,
TP_PROTO(const struct objagg *objagg),
TP_ARGS(objagg),
TP_STRUCT__entry(
__field(const void *, objagg)
),
TP_fast_assign(
__entry->objagg = objagg;
),
TP_printk("objagg %p", __entry->objagg)
);
TRACE_EVENT(objagg_obj_create,
TP_PROTO(const struct objagg *objagg,
const struct objagg_obj *obj),
TP_ARGS(objagg, obj),
TP_STRUCT__entry(
__field(const void *, objagg)
__field(const void *, obj)
),
TP_fast_assign(
__entry->objagg = objagg;
__entry->obj = obj;
),
TP_printk("objagg %p, obj %p", __entry->objagg, __entry->obj)
);
TRACE_EVENT(objagg_obj_destroy,
TP_PROTO(const struct objagg *objagg,
const struct objagg_obj *obj),
TP_ARGS(objagg, obj),
TP_STRUCT__entry(
__field(const void *, objagg)
__field(const void *, obj)
),
TP_fast_assign(
__entry->objagg = objagg;
__entry->obj = obj;
),
TP_printk("objagg %p, obj %p", __entry->objagg, __entry->obj)
);
TRACE_EVENT(objagg_obj_get,
TP_PROTO(const struct objagg *objagg,
const struct objagg_obj *obj,
unsigned int refcount),
TP_ARGS(objagg, obj, refcount),
TP_STRUCT__entry(
__field(const void *, objagg)
__field(const void *, obj)
__field(unsigned int, refcount)
),
TP_fast_assign(
__entry->objagg = objagg;
__entry->obj = obj;
__entry->refcount = refcount;
),
TP_printk("objagg %p, obj %p, refcount %u",
__entry->objagg, __entry->obj, __entry->refcount)
);
TRACE_EVENT(objagg_obj_put,
TP_PROTO(const struct objagg *objagg,
const struct objagg_obj *obj,
unsigned int refcount),
TP_ARGS(objagg, obj, refcount),
TP_STRUCT__entry(
__field(const void *, objagg)
__field(const void *, obj)
__field(unsigned int, refcount)
),
TP_fast_assign(
__entry->objagg = objagg;
__entry->obj = obj;
__entry->refcount = refcount;
),
TP_printk("objagg %p, obj %p, refcount %u",
__entry->objagg, __entry->obj, __entry->refcount)
);
TRACE_EVENT(objagg_obj_parent_assign,
TP_PROTO(const struct objagg *objagg,
const struct objagg_obj *obj,
const struct objagg_obj *parent,
unsigned int parent_refcount),
TP_ARGS(objagg, obj, parent, parent_refcount),
TP_STRUCT__entry(
__field(const void *, objagg)
__field(const void *, obj)
__field(const void *, parent)
__field(unsigned int, parent_refcount)
),
TP_fast_assign(
__entry->objagg = objagg;
__entry->obj = obj;
__entry->parent = parent;
__entry->parent_refcount = parent_refcount;
),
TP_printk("objagg %p, obj %p, parent %p, parent_refcount %u",
__entry->objagg, __entry->obj,
__entry->parent, __entry->parent_refcount)
);
TRACE_EVENT(objagg_obj_parent_unassign,
TP_PROTO(const struct objagg *objagg,
const struct objagg_obj *obj,
const struct objagg_obj *parent,
unsigned int parent_refcount),
TP_ARGS(objagg, obj, parent, parent_refcount),
TP_STRUCT__entry(
__field(const void *, objagg)
__field(const void *, obj)
__field(const void *, parent)
__field(unsigned int, parent_refcount)
),
TP_fast_assign(
__entry->objagg = objagg;
__entry->obj = obj;
__entry->parent = parent;
__entry->parent_refcount = parent_refcount;
),
TP_printk("objagg %p, obj %p, parent %p, parent_refcount %u",
__entry->objagg, __entry->obj,
__entry->parent, __entry->parent_refcount)
);
TRACE_EVENT(objagg_obj_root_create,
TP_PROTO(const struct objagg *objagg,
const struct objagg_obj *obj),
TP_ARGS(objagg, obj),
TP_STRUCT__entry(
__field(const void *, objagg)
__field(const void *, obj)
),
TP_fast_assign(
__entry->objagg = objagg;
__entry->obj = obj;
),
TP_printk("objagg %p, obj %p",
__entry->objagg, __entry->obj)
);
TRACE_EVENT(objagg_obj_root_destroy,
TP_PROTO(const struct objagg *objagg,
const struct objagg_obj *obj),
TP_ARGS(objagg, obj),
TP_STRUCT__entry(
__field(const void *, objagg)
__field(const void *, obj)
),
TP_fast_assign(
__entry->objagg = objagg;
__entry->obj = obj;
),
TP_printk("objagg %p, obj %p",
__entry->objagg, __entry->obj)
);
#endif /* __TRACE_OBJAGG_H */
/* This part must be outside protection */
#include <trace/define_trace.h>
......@@ -624,3 +624,6 @@ config GENERIC_LIB_CMPDI2
config GENERIC_LIB_UCMPDI2
bool
config OBJAGG
tristate "objagg" if COMPILE_TEST
......@@ -1976,6 +1976,16 @@ config TEST_MEMCAT_P
If unsure, say N.
config TEST_OBJAGG
tristate "Perform selftest on object aggreration manager"
default n
depends on OBJAGG
help
Enable this option to test object aggregation manager on boot
(or module load).
If unsure, say N.
endif # RUNTIME_TESTING_MENU
config MEMTEST
......
......@@ -75,6 +75,7 @@ obj-$(CONFIG_TEST_PARMAN) += test_parman.o
obj-$(CONFIG_TEST_KMOD) += test_kmod.o
obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o
obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o
obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o
ifeq ($(CONFIG_DEBUG_KOBJECT),y)
CFLAGS_kobject.o += -DDEBUG
......@@ -274,3 +275,4 @@ obj-$(CONFIG_GENERIC_LIB_LSHRDI3) += lshrdi3.o
obj-$(CONFIG_GENERIC_LIB_MULDI3) += muldi3.o
obj-$(CONFIG_GENERIC_LIB_CMPDI2) += cmpdi2.o
obj-$(CONFIG_GENERIC_LIB_UCMPDI2) += ucmpdi2.o
obj-$(CONFIG_OBJAGG) += objagg.o
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
/* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/rhashtable.h>
#include <linux/list.h>
#include <linux/sort.h>
#include <linux/objagg.h>
#define CREATE_TRACE_POINTS
#include <trace/events/objagg.h>
struct objagg {
const struct objagg_ops *ops;
void *priv;
struct rhashtable obj_ht;
struct rhashtable_params ht_params;
struct list_head obj_list;
unsigned int obj_count;
};
struct objagg_obj {
struct rhash_head ht_node; /* member of objagg->obj_ht */
struct list_head list; /* member of objagg->obj_list */
struct objagg_obj *parent; /* if the object is nested, this
* holds pointer to parent, otherwise NULL
*/
union {
void *delta_priv; /* user delta private */
void *root_priv; /* user root private */
};
unsigned int refcount; /* counts number of users of this object
* including nested objects
*/
struct objagg_obj_stats stats;
unsigned long obj[0];
};
static unsigned int objagg_obj_ref_inc(struct objagg_obj *objagg_obj)
{
return ++objagg_obj->refcount;
}
static unsigned int objagg_obj_ref_dec(struct objagg_obj *objagg_obj)
{
return --objagg_obj->refcount;
}
static void objagg_obj_stats_inc(struct objagg_obj *objagg_obj)
{
objagg_obj->stats.user_count++;
objagg_obj->stats.delta_user_count++;
if (objagg_obj->parent)
objagg_obj->parent->stats.delta_user_count++;
}
static void objagg_obj_stats_dec(struct objagg_obj *objagg_obj)
{
objagg_obj->stats.user_count--;
objagg_obj->stats.delta_user_count--;
if (objagg_obj->parent)
objagg_obj->parent->stats.delta_user_count--;
}
static bool objagg_obj_is_root(const struct objagg_obj *objagg_obj)
{
/* Nesting is not supported, so we can use ->parent
* to figure out if the object is root.
*/
return !objagg_obj->parent;
}
/**
* objagg_obj_root_priv - obtains root private for an object
* @objagg_obj: objagg object instance
*
* Note: all locking must be provided by the caller.
*
* Either the object is root itself when the private is returned
* directly, or the parent is root and its private is returned
* instead.
*
* Returns a user private root pointer.
*/
const void *objagg_obj_root_priv(const struct objagg_obj *objagg_obj)
{
if (objagg_obj_is_root(objagg_obj))
return objagg_obj->root_priv;
WARN_ON(!objagg_obj_is_root(objagg_obj->parent));
return objagg_obj->parent->root_priv;
}
EXPORT_SYMBOL(objagg_obj_root_priv);
/**
* objagg_obj_delta_priv - obtains delta private for an object
* @objagg_obj: objagg object instance
*
* Note: all locking must be provided by the caller.
*
* Returns user private delta pointer or NULL in case the passed
* object is root.
*/
const void *objagg_obj_delta_priv(const struct objagg_obj *objagg_obj)
{
if (objagg_obj_is_root(objagg_obj))
return NULL;
return objagg_obj->delta_priv;
}
EXPORT_SYMBOL(objagg_obj_delta_priv);
/**
* objagg_obj_raw - obtains object user private pointer
* @objagg_obj: objagg object instance
*
* Note: all locking must be provided by the caller.
*
* Returns user private pointer as was passed to objagg_obj_get() by "obj" arg.
*/
const void *objagg_obj_raw(const struct objagg_obj *objagg_obj)
{
return objagg_obj->obj;
}
EXPORT_SYMBOL(objagg_obj_raw);
static struct objagg_obj *objagg_obj_lookup(struct objagg *objagg, void *obj)
{
return rhashtable_lookup_fast(&objagg->obj_ht, obj, objagg->ht_params);
}
static int objagg_obj_parent_assign(struct objagg *objagg,
struct objagg_obj *objagg_obj,
struct objagg_obj *parent)
{
void *delta_priv;
delta_priv = objagg->ops->delta_create(objagg->priv, parent->obj,
objagg_obj->obj);
if (IS_ERR(delta_priv))
return PTR_ERR(delta_priv);
/* User returned a delta private, that means that
* our object can be aggregated into the parent.
*/
objagg_obj->parent = parent;
objagg_obj->delta_priv = delta_priv;
objagg_obj_ref_inc(objagg_obj->parent);
trace_objagg_obj_parent_assign(objagg, objagg_obj,
parent,
parent->refcount);
return 0;
}
static int objagg_obj_parent_lookup_assign(struct objagg *objagg,
struct objagg_obj *objagg_obj)
{
struct objagg_obj *objagg_obj_cur;
int err;
list_for_each_entry(objagg_obj_cur, &objagg->obj_list, list) {
/* Nesting is not supported. In case the object
* is not root, it cannot be assigned as parent.
*/
if (!objagg_obj_is_root(objagg_obj_cur))
continue;
err = objagg_obj_parent_assign(objagg, objagg_obj,
objagg_obj_cur);
if (!err)
return 0;
}
return -ENOENT;
}
static void __objagg_obj_put(struct objagg *objagg,
struct objagg_obj *objagg_obj);
static void objagg_obj_parent_unassign(struct objagg *objagg,
struct objagg_obj *objagg_obj)
{
trace_objagg_obj_parent_unassign(objagg, objagg_obj,
objagg_obj->parent,
objagg_obj->parent->refcount);
objagg->ops->delta_destroy(objagg->priv, objagg_obj->delta_priv);
__objagg_obj_put(objagg, objagg_obj->parent);
}
static int objagg_obj_root_create(struct objagg *objagg,
struct objagg_obj *objagg_obj)
{
objagg_obj->root_priv = objagg->ops->root_create(objagg->priv,
objagg_obj->obj);
if (IS_ERR(objagg_obj->root_priv))
return PTR_ERR(objagg_obj->root_priv);
trace_objagg_obj_root_create(objagg, objagg_obj);
return 0;
}
static void objagg_obj_root_destroy(struct objagg *objagg,
struct objagg_obj *objagg_obj)
{
trace_objagg_obj_root_destroy(objagg, objagg_obj);
objagg->ops->root_destroy(objagg->priv, objagg_obj->root_priv);
}
static int objagg_obj_init(struct objagg *objagg,
struct objagg_obj *objagg_obj)
{
int err;
/* Try to find if the object can be aggregated under an existing one. */
err = objagg_obj_parent_lookup_assign(objagg, objagg_obj);
if (!err)
return 0;
/* If aggregation is not possible, make the object a root. */
return objagg_obj_root_create(objagg, objagg_obj);
}
static void objagg_obj_fini(struct objagg *objagg,
struct objagg_obj *objagg_obj)
{
if (!objagg_obj_is_root(objagg_obj))
objagg_obj_parent_unassign(objagg, objagg_obj);
else
objagg_obj_root_destroy(objagg, objagg_obj);
}
static struct objagg_obj *objagg_obj_create(struct objagg *objagg, void *obj)
{
struct objagg_obj *objagg_obj;
int err;
objagg_obj = kzalloc(sizeof(*objagg_obj) + objagg->ops->obj_size,
GFP_KERNEL);
if (!objagg_obj)
return ERR_PTR(-ENOMEM);
objagg_obj_ref_inc(objagg_obj);
memcpy(objagg_obj->obj, obj, objagg->ops->obj_size);
err = objagg_obj_init(objagg, objagg_obj);
if (err)
goto err_obj_init;
err = rhashtable_insert_fast(&objagg->obj_ht, &objagg_obj->ht_node,
objagg->ht_params);
if (err)
goto err_ht_insert;
list_add(&objagg_obj->list, &objagg->obj_list);
objagg->obj_count++;
trace_objagg_obj_create(objagg, objagg_obj);
return objagg_obj;
err_ht_insert:
objagg_obj_fini(objagg, objagg_obj);
err_obj_init:
kfree(objagg_obj);
return ERR_PTR(err);
}
static struct objagg_obj *__objagg_obj_get(struct objagg *objagg, void *obj)
{
struct objagg_obj *objagg_obj;
/* First, try to find the object exactly as user passed it,
* perhaps it is already in use.
*/
objagg_obj = objagg_obj_lookup(objagg, obj);
if (objagg_obj) {
objagg_obj_ref_inc(objagg_obj);
return objagg_obj;
}
return objagg_obj_create(objagg, obj);
}
/**
* objagg_obj_get - gets an object within objagg instance
* @objagg: objagg instance
* @obj: user-specific private object pointer
*
* Note: all locking must be provided by the caller.
*
* Size of the "obj" memory is specified in "objagg->ops".
*
* There are 3 main options this function wraps:
* 1) The object according to "obj" already exist. In that case
* the reference counter is incrementes and the object is returned.
* 2) The object does not exist, but it can be aggregated within
* another object. In that case, user ops->delta_create() is called
* to obtain delta data and a new object is created with returned
* user-delta private pointer.
* 3) The object does not exist and cannot be aggregated into
* any of the existing objects. In that case, user ops->root_create()
* is called to create the root and a new object is created with
* returned user-root private pointer.
*
* Returns a pointer to objagg object instance in case of success,