1056 lines · 28.3 KB · C
RawDownload
// 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/idr.h>#include <linux/list.h>#include <linux/sort.h>#include <linux/objagg.h> #define CREATE_TRACE_POINTS#include <trace/events/objagg.h> struct objagg_hints {	struct rhashtable node_ht;	struct rhashtable_params ht_params;	struct list_head node_list;	unsigned int node_count;	unsigned int root_count;	unsigned int refcount;	const struct objagg_ops *ops;}; struct objagg_hints_node {	struct rhash_head ht_node; /* member of objagg_hints->node_ht */	struct list_head list; /* member of objagg_hints->node_list */	struct objagg_hints_node *parent;	unsigned int root_id;	struct objagg_obj_stats_info stats_info;	unsigned long obj[0];}; static struct objagg_hints_node *objagg_hints_lookup(struct objagg_hints *objagg_hints, void *obj){	if (!objagg_hints)		return NULL;	return rhashtable_lookup_fast(&objagg_hints->node_ht, obj,				      objagg_hints->ht_params);} 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 ida root_ida;	struct objagg_hints *hints;}; 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 root_id;	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,				    bool take_parent_ref){	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;	if (take_parent_ref)		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, true);		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_id_alloc(struct objagg *objagg,				    struct objagg_obj *objagg_obj,				    struct objagg_hints_node *hnode){	unsigned int min, max;	int root_id; 	/* In case there are no hints available, the root id is invalid. */	if (!objagg->hints) {		objagg_obj->root_id = OBJAGG_OBJ_ROOT_ID_INVALID;		return 0;	} 	if (hnode) {		min = hnode->root_id;		max = hnode->root_id;	} else {		/* For objects with no hint, start after the last		 * hinted root_id.		 */		min = objagg->hints->root_count;		max = ~0;	} 	root_id = ida_alloc_range(&objagg->root_ida, min, max, GFP_KERNEL); 	if (root_id < 0)		return root_id;	objagg_obj->root_id = root_id;	return 0;} static void objagg_obj_root_id_free(struct objagg *objagg,				    struct objagg_obj *objagg_obj){	if (!objagg->hints)		return;	ida_free(&objagg->root_ida, objagg_obj->root_id);} static int objagg_obj_root_create(struct objagg *objagg,				  struct objagg_obj *objagg_obj,				  struct objagg_hints_node *hnode){	int err; 	err = objagg_obj_root_id_alloc(objagg, objagg_obj, hnode);	if (err)		return err;	objagg_obj->root_priv = objagg->ops->root_create(objagg->priv,							 objagg_obj->obj,							 objagg_obj->root_id);	if (IS_ERR(objagg_obj->root_priv)) {		err = PTR_ERR(objagg_obj->root_priv);		goto err_root_create;	}	trace_objagg_obj_root_create(objagg, objagg_obj);	return 0; err_root_create:	objagg_obj_root_id_free(objagg, objagg_obj);	return err;} 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);	objagg_obj_root_id_free(objagg, objagg_obj);} static struct objagg_obj *__objagg_obj_get(struct objagg *objagg, void *obj); static int objagg_obj_init_with_hints(struct objagg *objagg,				      struct objagg_obj *objagg_obj,				      bool *hint_found){	struct objagg_hints_node *hnode;	struct objagg_obj *parent;	int err; 	hnode = objagg_hints_lookup(objagg->hints, objagg_obj->obj);	if (!hnode) {		*hint_found = false;		return 0;	}	*hint_found = true; 	if (!hnode->parent)		return objagg_obj_root_create(objagg, objagg_obj, hnode); 	parent = __objagg_obj_get(objagg, hnode->parent->obj);	if (IS_ERR(parent))		return PTR_ERR(parent); 	err = objagg_obj_parent_assign(objagg, objagg_obj, parent, false);	if (err) {		*hint_found = false;		err = 0;		goto err_parent_assign;	} 	return 0; err_parent_assign:	objagg_obj_put(objagg, parent);	return err;} static int objagg_obj_init(struct objagg *objagg,			   struct objagg_obj *objagg_obj){	bool hint_found;	int err; 	/* First, try to use hints if they are available and	 * if they provide result.	 */	err = objagg_obj_init_with_hints(objagg, objagg_obj, &hint_found);	if (err)		return err; 	if (hint_found)		return 0; 	/* 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, NULL);} 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, * otherwise it returns pointer error using ERR_PTR macro. */struct objagg_obj *objagg_obj_get(struct objagg *objagg, void *obj){	struct objagg_obj *objagg_obj; 	objagg_obj = __objagg_obj_get(objagg, obj);	if (IS_ERR(objagg_obj))		return objagg_obj;	objagg_obj_stats_inc(objagg_obj);	trace_objagg_obj_get(objagg, objagg_obj, objagg_obj->refcount);	return objagg_obj;}EXPORT_SYMBOL(objagg_obj_get); static void objagg_obj_destroy(struct objagg *objagg,			       struct objagg_obj *objagg_obj){	trace_objagg_obj_destroy(objagg, objagg_obj);	--objagg->obj_count;	list_del(&objagg_obj->list);	rhashtable_remove_fast(&objagg->obj_ht, &objagg_obj->ht_node,			       objagg->ht_params);	objagg_obj_fini(objagg, objagg_obj);	kfree(objagg_obj);} static void __objagg_obj_put(struct objagg *objagg,			     struct objagg_obj *objagg_obj){	if (!objagg_obj_ref_dec(objagg_obj))		objagg_obj_destroy(objagg, objagg_obj);} /** * objagg_obj_put - puts an object within objagg instance * @objagg:	objagg instance * @objagg_obj:	objagg object instance * * Note: all locking must be provided by the caller. * * Symmetric to objagg_obj_get(). */void objagg_obj_put(struct objagg *objagg, struct objagg_obj *objagg_obj){	trace_objagg_obj_put(objagg, objagg_obj, objagg_obj->refcount);	objagg_obj_stats_dec(objagg_obj);	__objagg_obj_put(objagg, objagg_obj);}EXPORT_SYMBOL(objagg_obj_put); /** * objagg_create - creates a new objagg instance * @ops:		user-specific callbacks * @objagg_hints:	hints, can be NULL * @priv:		pointer to a private data passed to the ops * * Note: all locking must be provided by the caller. * * The purpose of the library is to provide an infrastructure to * aggregate user-specified objects. Library does not care about the type * of the object. User fills-up ops which take care of the specific * user object manipulation. * * As a very stupid example, consider integer numbers. For example * number 8 as a root object. That can aggregate number 9 with delta 1,

Lines 1–500 of 1056.