Re: [PATCH V2 1/3] drivers/staging: zcache: in-kernel tmem code

From: Minchan Kim
Date: Sat Feb 12 2011 - 21:05:44 EST


Hi Dan,

On Mon, Feb 7, 2011 at 12:25 PM, Dan Magenheimer
<dan.magenheimer@xxxxxxxxxx> wrote:
> [PATCH V2 1/3] drivers/staging: zcache: in-kernel tmem code
>
> Transcendent memory ("tmem") is a clean API/ABI that provides
> for an efficient address translation and a set of highly
> concurrent access methods to copy data between a page-oriented
> data source (e.g. cleancache or frontswap) and a page-addressable
> memory ("PAM") data store. ÂOf critical importance, the PAM data
> store is of unknown (and possibly varying) size so any individual
> access may succeed or fail as defined by the API/ABI.
>
> Tmem exports a basic set of access methods (e.g. put, get,
> flush, flush object, new pool, and destroy pool) which are
> normally called from a "host" (e.g. zcache).
>
> To be functional, two sets of "ops" must be registered by the
> host, one to provide "host services" (memory allocation) and
> one to provide page-addressable memory ("PAM") hooks.
>
> Tmem supports one or more "clients", each which can provide
> a set of "pools" to partition pages. ÂEach pool contains
> a set of "objects"; each object holds pointers to some number
> of PAM page descriptors ("pampd"), indexed by an "index" number.
> This triple <pool id, object id, index> is sometimes referred
> to as a "handle". ÂTmem's primary function is to essentially
> provide address translation of handles into pampds and move
> data appropriately.
>
> As an example, for cleancache, a pool maps to a filesystem,
> an object maps to a file, and the index is the page offset
> into the file. ÂAnd in this patch, zcache is the host and
> each PAM descriptor points to a compressed page of data.
>
> Tmem supports two kinds of pages: "ephemeral" and "persistent".
> Ephemeral pages may be asynchronously reclaimed "bottoms up"
> so the data structures and concurrency model must allow for
> this. ÂFor example, each pampd must retain sufficient information
> to invalidate tmem's handle-to-pampd translation.
> its containing object so that, on reclaim, all tmem data
> structures can be made consistent.
>
> Signed-off-by: Dan Magenheimer <dan.magenheimer@xxxxxxxxxx>
>
> ---
>
> Diffstat:
> Âdrivers/staging/zcache/tmem.c      Â| Â710 +++++++++++++++++++++
> Âdrivers/staging/zcache/tmem.h      Â| Â195 +++++
> Â2 files changed, 905 insertions(+)
> --- linux-2.6.37/drivers/staging/zcache/tmem.c Â1969-12-31 17:00:00.000000000 -0700
> +++ linux-2.6.37-zcache/drivers/staging/zcache/tmem.c  2011-02-05 15:44:19.000000000 -0700
> @@ -0,0 +1,710 @@
> +/*
> + * In-kernel transcendent memory (generic implementation)
> + *
> + * Copyright (c) 2009-2011, Dan Magenheimer, Oracle Corp.
> + *
> + * The primary purpose of Transcedent Memory ("tmem") is to map object-oriented
> + * "handles" (triples containing a pool id, and object id, and an index), to
> + * pages in a page-accessible memory (PAM). ÂTmem references the PAM pages via
> + * an abstract "pampd" (PAM page-descriptor), which can be operated on by a
> + * set of functions (pamops). ÂEach pampd contains some representation of
> + * PAGE_SIZE bytes worth of data. Tmem must support potentially millions of
> + * pages and must be able to insert, find, and delete these pages at a
> + * potential frequency of thousands per second concurrently across many CPUs,
> + * (and, if used with KVM, across many vcpus across many guests).
> + * Tmem is tracked with a hierarchy of data structures, organized by
> + * the elements in a handle-tuple: pool_id, object_id, and page index.
> + * One or more "clients" (e.g. guests) each provide one or more tmem_pools.
> + * Each pool, contains a hash table of rb_trees of tmem_objs. ÂEach
> + * tmem_obj contains a radix-tree-like tree of pointers, with intermediate
> + * nodes called tmem_objnodes. ÂEach leaf pointer in this tree points to
> + * a pampd, which is accessible only through a small set of callbacks
> + * registered by the PAM implementation (see tmem_register_pamops). Tmem
> + * does all memory allocation via a set of callbacks registered by the tmem
> + * host implementation (e.g. see tmem_register_hostops).
> + */
> +
> +#include <linux/list.h>
> +#include <linux/spinlock.h>
> +#include <linux/atomic.h>
> +
> +#include "tmem.h"
> +
> +/* data structure sentinels used for debugging... see tmem.h */
> +#define POOL_SENTINEL 0x87658765
> +#define OBJ_SENTINEL 0x12345678
> +#define OBJNODE_SENTINEL 0xfedcba09
> +
> +/*
> + * A tmem host implementation must use this function to register callbacks
> + * for memory allocation.
> + */

I think it would better to use "object management(ex, allocation,
free) " rather than vague "memory allocation".

And I am not sure it's good that support allocation flexibility.
(The flexibility is rather limited since user should implement it as
considering rb tree. We don't need to export policy to user)
I think we can implement general obj/objnode allocation in tmem to
hide it from host.
It can make client simple to use tmem but lost flexibility.
Do we really need the flexibility?

> +static struct tmem_hostops tmem_hostops;
> +
> +static void tmem_objnode_tree_init(void);
> +
> +void tmem_register_hostops(struct tmem_hostops *m)
> +{
> + Â Â Â tmem_objnode_tree_init();
> + Â Â Â tmem_hostops = *m;
> +}
> +
> +/*
> + * A tmem host implementation must use this function to register
> + * callbacks for a page-accessible memory (PAM) implementation
> + */

You said tmem_hostops is for memory allocation.
But said tmem_pamops is for PAM implementation?
It's not same level explanation.
I hope you write down it more clearly by same level.
(Ex, is for add/delete/get the page into PAM)

> +static struct tmem_pamops tmem_pamops;
> +
> +void tmem_register_pamops(struct tmem_pamops *m)
> +{
> + Â Â Â tmem_pamops = *m;
> +}
> +
> +/*
> + * Oid's are potentially very sparse and tmem_objs may have an indeterminately
> + * short life, being added and deleted at a relatively high frequency.
> + * So an rb_tree is an ideal data structure to manage tmem_objs. ÂBut because
> + * of the potentially huge number of tmem_objs, each pool manages a hashtable
> + * of rb_trees to reduce search, insert, delete, and rebalancing time.
> + * Each hashbucket also has a lock to manage concurrent access.
> + *
> + * The following routines manage tmem_objs. ÂWhen any tmem_obj is accessed,
> + * the hashbucket lock must be held.
> + */
> +
> +/* searches for object==oid in pool, returns locked object if found */

Returns locked object if found?
I can't find it in the code and merge the comment above, not separate phrase.

> +static struct tmem_obj *tmem_obj_find(struct tmem_hashbucket *hb,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct tmem_oid *oidp)
> +{
> + Â Â Â struct rb_node *rbnode;
> + Â Â Â struct tmem_obj *obj;
> +
> + Â Â Â rbnode = hb->obj_rb_root.rb_node;
> + Â Â Â while (rbnode) {
> + Â Â Â Â Â Â Â BUG_ON(RB_EMPTY_NODE(rbnode));
> + Â Â Â Â Â Â Â obj = rb_entry(rbnode, struct tmem_obj, rb_tree_node);
> + Â Â Â Â Â Â Â switch (tmem_oid_compare(oidp, &obj->oid)) {
> + Â Â Â Â Â Â Â case 0: /* equal */
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â case -1:
> + Â Â Â Â Â Â Â Â Â Â Â rbnode = rbnode->rb_left;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â case 1:
> + Â Â Â Â Â Â Â Â Â Â Â rbnode = rbnode->rb_right;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> + Â Â Â obj = NULL;
> +out:
> + Â Â Â return obj;
> +}
> +
> +static void tmem_pampd_destroy_all_in_obj(struct tmem_obj *);
> +
> +/* free an object that has no more pampds in it */
> +static void tmem_obj_free(struct tmem_obj *obj, struct tmem_hashbucket *hb)
> +{
> + Â Â Â struct tmem_pool *pool;
> +
> + Â Â Â BUG_ON(obj == NULL);

We don't need this BUG_ON. If obj is NULL, obj->pool is crashed then
we can know it.

> + Â Â Â ASSERT_SENTINEL(obj, OBJ);
> + Â Â Â BUG_ON(obj->pampd_count > 0);
> + Â Â Â pool = obj->pool;
> + Â Â Â BUG_ON(pool == NULL);

Ditto. it is crashed at pool->obj_count.

> + Â Â Â if (obj->objnode_tree_root != NULL) /* may be "stump" with no leaves */
> + Â Â Â Â Â Â Â tmem_pampd_destroy_all_in_obj(obj);
> + Â Â Â BUG_ON(obj->objnode_tree_root != NULL);
> + Â Â Â BUG_ON((long)obj->objnode_count != 0);
> + Â Â Â atomic_dec(&pool->obj_count);

Does we really need the atomic operation?
It seems it's protected by hash bucket lock.

Another topic.
I think hb->lock is very coarse-grained.
Maybe we need more fine-grained lock design to emphasis on your
concurrent benefit.

> + Â Â Â BUG_ON(atomic_read(&pool->obj_count) < 0);
> + Â Â Â INVERT_SENTINEL(obj, OBJ);
> + Â Â Â obj->pool = NULL;
> + Â Â Â tmem_oid_set_invalid(&obj->oid);
> + Â Â Â rb_erase(&obj->rb_tree_node, &hb->obj_rb_root);

For example, we can remove obj in rb tree and then we can clean up the object.
It can reduce lock hold time.

> +}
> +
> +/*
> + * initialize, and insert an tmem_object_root (called only if find failed)
> + */
> +static void tmem_obj_init(struct tmem_obj *obj, struct tmem_hashbucket *hb,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct tmem_pool *pool,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct tmem_oid *oidp)
> +{
> + Â Â Â struct rb_root *root = &hb->obj_rb_root;
> + Â Â Â struct rb_node **new = &(root->rb_node), *parent = NULL;
> + Â Â Â struct tmem_obj *this;
> +
> + Â Â Â BUG_ON(pool == NULL);
> + Â Â Â atomic_inc(&pool->obj_count);
> + Â Â Â obj->objnode_tree_height = 0;
> + Â Â Â obj->objnode_tree_root = NULL;
> + Â Â Â obj->pool = pool;
> + Â Â Â obj->oid = *oidp;
> + Â Â Â obj->objnode_count = 0;
> + Â Â Â obj->pampd_count = 0;
> + Â Â Â SET_SENTINEL(obj, OBJ);
> + Â Â Â while (*new) {
> + Â Â Â Â Â Â Â BUG_ON(RB_EMPTY_NODE(*new));
> + Â Â Â Â Â Â Â this = rb_entry(*new, struct tmem_obj, rb_tree_node);
> + Â Â Â Â Â Â Â parent = *new;
> + Â Â Â Â Â Â Â switch (tmem_oid_compare(oidp, &this->oid)) {
> + Â Â Â Â Â Â Â case 0:
> + Â Â Â Â Â Â Â Â Â Â Â BUG(); /* already present; should never happen! */
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â case -1:
> + Â Â Â Â Â Â Â Â Â Â Â new = &(*new)->rb_left;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â case 1:
> + Â Â Â Â Â Â Â Â Â Â Â new = &(*new)->rb_right;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> + Â Â Â rb_link_node(&obj->rb_tree_node, parent, new);
> + Â Â Â rb_insert_color(&obj->rb_tree_node, root);
> +}
> +
> +/*
> + * Tmem is managed as a set of tmem_pools with certain attributes, such as
> + * "ephemeral" vs "persistent". ÂThese attributes apply to all tmem_objs
> + * and all pampds that belong to a tmem_pool. ÂA tmem_pool is created
> + * or deleted relatively rarely (for example, when a filesystem is
> + * mounted or unmounted.

Although it's rare, it might take a long time to clear all object.
We can use cond_resched when it doesn't hold spin_lock.

> + */
> +
> +/* flush all data from a pool and, optionally, free it */

I think it would be better to use term "object" rather than "data".

> +static void tmem_pool_flush(struct tmem_pool *pool, bool destroy)
> +{
> + Â Â Â struct rb_node *rbnode;
> + Â Â Â struct tmem_obj *obj;
> + Â Â Â struct tmem_hashbucket *hb = &pool->hashbucket[0];
> + Â Â Â int i;
> +
> + Â Â Â BUG_ON(pool == NULL);
> + Â Â Â for (i = 0; i < TMEM_HASH_BUCKETS; i++, hb++) {
> + Â Â Â Â Â Â Â spin_lock(&hb->lock);
> + Â Â Â Â Â Â Â rbnode = rb_first(&hb->obj_rb_root);
> + Â Â Â Â Â Â Â while (rbnode != NULL) {
> + Â Â Â Â Â Â Â Â Â Â Â obj = rb_entry(rbnode, struct tmem_obj, rb_tree_node);
> + Â Â Â Â Â Â Â Â Â Â Â rbnode = rb_next(rbnode);
> + Â Â Â Â Â Â Â Â Â Â Â tmem_pampd_destroy_all_in_obj(obj);
> + Â Â Â Â Â Â Â Â Â Â Â tmem_obj_free(obj, hb);
> + Â Â Â Â Â Â Â Â Â Â Â (*tmem_hostops.obj_free)(obj, pool);
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â spin_unlock(&hb->lock);
> + Â Â Â }
> + Â Â Â if (destroy)

I don't see any use case of not-destroy.
What do you have in your mind?

> + Â Â Â Â Â Â Â list_del(&pool->pool_list);
> +}
> +
> +/*
> + * A tmem_obj contains a radix-tree-like tree in which the intermediate
> + * nodes are called tmem_objnodes. Â(The kernel lib/radix-tree.c implementation
> + * is very specialized and tuned for specific uses and is not particularly
> + * suited for use from this code, though some code from the core algorithms has
> + * been reused, thus the copyright notices below). ÂEach tmem_objnode contains
> + * a set of pointers which point to either a set of intermediate tmem_objnodes
> + * or a set of of pampds.

I remember you sent the patch which point out current radix-tree problem.
Sorry for not follow that.

Anyway, I think it would be better to separate this patch into another
tmem-radix-tree.c and write down the description in the patch why we
need new radix-tree in detail.
Sorry for bothering you.

> + *
> + * Portions Copyright (C) 2001 Momchil Velikov
> + * Portions Copyright (C) 2001 Christoph Hellwig
> + * Portions Copyright (C) 2005 SGI, Christoph Lameter <clameter@xxxxxxx>
> + */
> +
> +struct tmem_objnode_tree_path {
> + Â Â Â struct tmem_objnode *objnode;
> + Â Â Â int offset;
> +};
> +
> +/* objnode height_to_maxindex translation */
> +static unsigned long tmem_objnode_tree_h2max[OBJNODE_TREE_MAX_PATH + 1];
> +
> +static void tmem_objnode_tree_init(void)
> +{
> + Â Â Â unsigned int ht, tmp;
> +
> + Â Â Â for (ht = 0; ht < ARRAY_SIZE(tmem_objnode_tree_h2max); ht++) {
> + Â Â Â Â Â Â Â tmp = ht * OBJNODE_TREE_MAP_SHIFT;
> + Â Â Â Â Â Â Â if (tmp >= OBJNODE_TREE_INDEX_BITS)
> + Â Â Â Â Â Â Â Â Â Â Â tmem_objnode_tree_h2max[ht] = ~0UL;
> + Â Â Â Â Â Â Â else
> + Â Â Â Â Â Â Â Â Â Â Â tmem_objnode_tree_h2max[ht] =
> + Â Â Â Â Â Â Â Â Â Â Â Â Â (~0UL >> (OBJNODE_TREE_INDEX_BITS - tmp - 1)) >> 1;
> + Â Â Â }
> +}
> +
> +static struct tmem_objnode *tmem_objnode_alloc(struct tmem_obj *obj)
> +{
> + Â Â Â struct tmem_objnode *objnode;
> +
> + Â Â Â ASSERT_SENTINEL(obj, OBJ);
> + Â Â Â BUG_ON(obj->pool == NULL);
> + Â Â Â ASSERT_SENTINEL(obj->pool, POOL);
> + Â Â Â objnode = (*tmem_hostops.objnode_alloc)(obj->pool);
> + Â Â Â if (unlikely(objnode == NULL))
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â objnode->obj = obj;
> + Â Â Â SET_SENTINEL(objnode, OBJNODE);
> + Â Â Â memset(&objnode->slots, 0, sizeof(objnode->slots));
> + Â Â Â objnode->slots_in_use = 0;
> + Â Â Â obj->objnode_count++;
> +out:
> + Â Â Â return objnode;
> +}
> +
> +static void tmem_objnode_free(struct tmem_objnode *objnode)
> +{
> + Â Â Â struct tmem_pool *pool;
> + Â Â Â int i;
> +
> + Â Â Â BUG_ON(objnode == NULL);
> + Â Â Â for (i = 0; i < OBJNODE_TREE_MAP_SIZE; i++)
> + Â Â Â Â Â Â Â BUG_ON(objnode->slots[i] != NULL);
> + Â Â Â ASSERT_SENTINEL(objnode, OBJNODE);
> + Â Â Â INVERT_SENTINEL(objnode, OBJNODE);
> + Â Â Â BUG_ON(objnode->obj == NULL);
> + Â Â Â ASSERT_SENTINEL(objnode->obj, OBJ);
> + Â Â Â pool = objnode->obj->pool;
> + Â Â Â BUG_ON(pool == NULL);
> + Â Â Â ASSERT_SENTINEL(pool, POOL);
> + Â Â Â objnode->obj->objnode_count--;
> + Â Â Â objnode->obj = NULL;
> + Â Â Â (*tmem_hostops.objnode_free)(objnode, pool);
> +}
> +
> +/*
> + * lookup index in object and return associated pampd (or NULL if not found)
> + */
> +static void *tmem_pampd_lookup_in_obj(struct tmem_obj *obj, uint32_t index)
> +{
> + Â Â Â unsigned int height, shift;
> + Â Â Â struct tmem_objnode **slot = NULL;
> +
> + Â Â Â BUG_ON(obj == NULL);
> + Â Â Â ASSERT_SENTINEL(obj, OBJ);
> + Â Â Â BUG_ON(obj->pool == NULL);
> + Â Â Â ASSERT_SENTINEL(obj->pool, POOL);
> +
> + Â Â Â height = obj->objnode_tree_height;
> + Â Â Â if (index > tmem_objnode_tree_h2max[obj->objnode_tree_height])
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â if (height == 0 && obj->objnode_tree_root) {
> + Â Â Â Â Â Â Â slot = &obj->objnode_tree_root;
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> + Â Â Â shift = (height-1) * OBJNODE_TREE_MAP_SHIFT;
> + Â Â Â slot = &obj->objnode_tree_root;
> + Â Â Â while (height > 0) {
> + Â Â Â Â Â Â Â if (*slot == NULL)
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â slot = (struct tmem_objnode **)
> + Â Â Â Â Â Â Â Â Â Â Â ((*slot)->slots +
> + Â Â Â Â Â Â Â Â Â Â Â Â((index >> shift) & OBJNODE_TREE_MAP_MASK));
> + Â Â Â Â Â Â Â shift -= OBJNODE_TREE_MAP_SHIFT;
> + Â Â Â Â Â Â Â height--;
> + Â Â Â }
> +out:
> + Â Â Â return slot != NULL ? *slot : NULL;
> +}
> +
> +static int tmem_pampd_add_to_obj(struct tmem_obj *obj, uint32_t index,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â void *pampd)
> +{
> + Â Â Â int ret = 0;
> + Â Â Â struct tmem_objnode *objnode = NULL, *newnode, *slot;
> + Â Â Â unsigned int height, shift;
> + Â Â Â int offset = 0;
> +
> + Â Â Â /* if necessary, extend the tree to be higher Â*/
> + Â Â Â if (index > tmem_objnode_tree_h2max[obj->objnode_tree_height]) {
> + Â Â Â Â Â Â Â height = obj->objnode_tree_height + 1;
> + Â Â Â Â Â Â Â if (index > tmem_objnode_tree_h2max[height])
> + Â Â Â Â Â Â Â Â Â Â Â while (index > tmem_objnode_tree_h2max[height])
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â height++;
> + Â Â Â Â Â Â Â if (obj->objnode_tree_root == NULL) {
> + Â Â Â Â Â Â Â Â Â Â Â obj->objnode_tree_height = height;
> + Â Â Â Â Â Â Â Â Â Â Â goto insert;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â do {
> + Â Â Â Â Â Â Â Â Â Â Â newnode = tmem_objnode_alloc(obj);
> + Â Â Â Â Â Â Â Â Â Â Â if (!newnode) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ret = -ENOMEM;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â newnode->slots[0] = obj->objnode_tree_root;
> + Â Â Â Â Â Â Â Â Â Â Â newnode->slots_in_use = 1;
> + Â Â Â Â Â Â Â Â Â Â Â obj->objnode_tree_root = newnode;
> + Â Â Â Â Â Â Â Â Â Â Â obj->objnode_tree_height++;
> + Â Â Â Â Â Â Â } while (height > obj->objnode_tree_height);
> + Â Â Â }
> +insert:
> + Â Â Â slot = obj->objnode_tree_root;
> + Â Â Â height = obj->objnode_tree_height;
> + Â Â Â shift = (height-1) * OBJNODE_TREE_MAP_SHIFT;
> + Â Â Â while (height > 0) {
> + Â Â Â Â Â Â Â if (slot == NULL) {
> + Â Â Â Â Â Â Â Â Â Â Â /* add a child objnode. Â*/
> + Â Â Â Â Â Â Â Â Â Â Â slot = tmem_objnode_alloc(obj);
> + Â Â Â Â Â Â Â Â Â Â Â if (!slot) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ret = -ENOMEM;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â if (objnode) {
> +
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â objnode->slots[offset] = slot;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â objnode->slots_in_use++;
> + Â Â Â Â Â Â Â Â Â Â Â } else
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â obj->objnode_tree_root = slot;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â /* go down a level */
> + Â Â Â Â Â Â Â offset = (index >> shift) & OBJNODE_TREE_MAP_MASK;
> + Â Â Â Â Â Â Â objnode = slot;
> + Â Â Â Â Â Â Â slot = objnode->slots[offset];
> + Â Â Â Â Â Â Â shift -= OBJNODE_TREE_MAP_SHIFT;
> + Â Â Â Â Â Â Â height--;
> + Â Â Â }
> + Â Â Â BUG_ON(slot != NULL);
> + Â Â Â if (objnode) {
> + Â Â Â Â Â Â Â objnode->slots_in_use++;
> + Â Â Â Â Â Â Â objnode->slots[offset] = pampd;
> + Â Â Â } else
> + Â Â Â Â Â Â Â obj->objnode_tree_root = pampd;
> + Â Â Â obj->pampd_count++;
> +out:
> + Â Â Â return ret;
> +}
> +
> +static void *tmem_pampd_delete_from_obj(struct tmem_obj *obj, uint32_t index)
> +{
> + Â Â Â struct tmem_objnode_tree_path path[OBJNODE_TREE_MAX_PATH + 1];
> + Â Â Â struct tmem_objnode_tree_path *pathp = path;
> + Â Â Â struct tmem_objnode *slot = NULL;
> + Â Â Â unsigned int height, shift;
> + Â Â Â int offset;
> +
> + Â Â Â BUG_ON(obj == NULL);
> + Â Â Â ASSERT_SENTINEL(obj, OBJ);
> + Â Â Â BUG_ON(obj->pool == NULL);
> + Â Â Â ASSERT_SENTINEL(obj->pool, POOL);
> + Â Â Â height = obj->objnode_tree_height;
> + Â Â Â if (index > tmem_objnode_tree_h2max[height])
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â slot = obj->objnode_tree_root;
> + Â Â Â if (height == 0 && obj->objnode_tree_root) {
> + Â Â Â Â Â Â Â obj->objnode_tree_root = NULL;
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> + Â Â Â shift = (height - 1) * OBJNODE_TREE_MAP_SHIFT;
> + Â Â Â pathp->objnode = NULL;
> + Â Â Â do {
> + Â Â Â Â Â Â Â if (slot == NULL)
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â pathp++;
> + Â Â Â Â Â Â Â offset = (index >> shift) & OBJNODE_TREE_MAP_MASK;
> + Â Â Â Â Â Â Â pathp->offset = offset;
> + Â Â Â Â Â Â Â pathp->objnode = slot;
> + Â Â Â Â Â Â Â slot = slot->slots[offset];
> + Â Â Â Â Â Â Â shift -= OBJNODE_TREE_MAP_SHIFT;
> + Â Â Â Â Â Â Â height--;
> + Â Â Â } while (height > 0);
> + Â Â Â if (slot == NULL)
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â while (pathp->objnode) {
> + Â Â Â Â Â Â Â pathp->objnode->slots[pathp->offset] = NULL;
> + Â Â Â Â Â Â Â pathp->objnode->slots_in_use--;
> + Â Â Â Â Â Â Â if (pathp->objnode->slots_in_use) {
> + Â Â Â Â Â Â Â Â Â Â Â if (pathp->objnode == obj->objnode_tree_root) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â while (obj->objnode_tree_height > 0 &&
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â obj->objnode_tree_root->slots_in_use == 1 &&
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â obj->objnode_tree_root->slots[0]) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct tmem_objnode *to_free =
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â obj->objnode_tree_root;
> +
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â obj->objnode_tree_root =
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â to_free->slots[0];
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â obj->objnode_tree_height--;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â to_free->slots[0] = NULL;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â to_free->slots_in_use = 0;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â tmem_objnode_free(to_free);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â tmem_objnode_free(pathp->objnode); /* 0 slots used, free it */
> + Â Â Â Â Â Â Â pathp--;
> + Â Â Â }
> + Â Â Â obj->objnode_tree_height = 0;
> + Â Â Â obj->objnode_tree_root = NULL;
> +
> +out:
> + Â Â Â if (slot != NULL)
> + Â Â Â Â Â Â Â obj->pampd_count--;
> + Â Â Â BUG_ON(obj->pampd_count < 0);
> + Â Â Â return slot;
> +}
> +
> +/* recursively walk the objnode_tree destroying pampds and objnodes */
> +static void tmem_objnode_node_destroy(struct tmem_obj *obj,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct tmem_objnode *objnode,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned int ht)
> +{
> + Â Â Â int i;
> +
> + Â Â Â if (ht == 0)
> + Â Â Â Â Â Â Â return;
> + Â Â Â for (i = 0; i < OBJNODE_TREE_MAP_SIZE; i++) {
> + Â Â Â Â Â Â Â if (objnode->slots[i]) {
> + Â Â Â Â Â Â Â Â Â Â Â if (ht == 1) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â obj->pampd_count--;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (*tmem_pamops.free)(objnode->slots[i],
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â obj->pool);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â objnode->slots[i] = NULL;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â continue;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â tmem_objnode_node_destroy(obj, objnode->slots[i], ht-1);
> + Â Â Â Â Â Â Â Â Â Â Â tmem_objnode_free(objnode->slots[i]);
> + Â Â Â Â Â Â Â Â Â Â Â objnode->slots[i] = NULL;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +}
> +
> +static void tmem_pampd_destroy_all_in_obj(struct tmem_obj *obj)
> +{
> + Â Â Â if (obj->objnode_tree_root == NULL)
> + Â Â Â Â Â Â Â return;
> + Â Â Â if (obj->objnode_tree_height == 0) {
> + Â Â Â Â Â Â Â obj->pampd_count--;
> + Â Â Â Â Â Â Â (*tmem_pamops.free)(obj->objnode_tree_root, obj->pool);
> + Â Â Â } else {
> + Â Â Â Â Â Â Â tmem_objnode_node_destroy(obj, obj->objnode_tree_root,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â obj->objnode_tree_height);
> + Â Â Â Â Â Â Â tmem_objnode_free(obj->objnode_tree_root);
> + Â Â Â Â Â Â Â obj->objnode_tree_height = 0;
> + Â Â Â }
> + Â Â Â obj->objnode_tree_root = NULL;
> +}
> +
> +/*
> + * Tmem is operated on by a set of well-defined actions:
> + * "put", "get", "flush", "flush_object", "new pool" and "destroy pool".
> + * (The tmem ABI allows for subpages and exchanges but these operations
> + * are not included in this implementation.)
> + *
> + * These "tmem core" operations are implemented in the following functions.
> + */
> +
> +/*
> + * "Put" a page, e.g. copy a page from the kernel into newly allocated
> + * PAM space (if such space is available). ÂTmem_put is complicated by
> + * a corner case: What if a page with matching handle already exists in
> + * tmem? ÂTo guarantee coherency, one of two actions is necessary: Either
> + * the data for the page must be overwritten, or the page must be
> + * "flushed" so that the data is not accessible to a subsequent "get".
> + * Since these "duplicate puts" are relatively rare, this implementation
> + * always flushes for simplicity.
> + */
> +int tmem_put(struct tmem_pool *pool, struct tmem_oid *oidp, uint32_t index,
> + Â Â Â Â Â Â Â struct page *page)
> +{
> + Â Â Â struct tmem_obj *obj = NULL, *objfound = NULL, *objnew = NULL;
> + Â Â Â void *pampd = NULL, *pampd_del = NULL;
> + Â Â Â int ret = -ENOMEM;
> + Â Â Â bool ephemeral;
> + Â Â Â struct tmem_hashbucket *hb;
> +
> + Â Â Â ephemeral = is_ephemeral(pool);
> + Â Â Â hb = &pool->hashbucket[tmem_oid_hash(oidp)];
> + Â Â Â spin_lock(&hb->lock);

This spin_lock means we can't call *put* in interrupt context.
But now we can call put_page in intrrupt context.
I see zcache_put_page checks irqs_disabled so now it's okay.
But zcache is just only one client of tmem. In future, another client
can call it in interrupt context.

Do you intent to limit calling it in only not-interrupt context by design?

> + Â Â Â obj = objfound = tmem_obj_find(hb, oidp);
> + Â Â Â if (obj != NULL) {
> + Â Â Â Â Â Â Â pampd = tmem_pampd_lookup_in_obj(objfound, index);
> + Â Â Â Â Â Â Â if (pampd != NULL) {
> + Â Â Â Â Â Â Â Â Â Â Â /* if found, is a dup put, flush the old one */
> + Â Â Â Â Â Â Â Â Â Â Â pampd_del = tmem_pampd_delete_from_obj(obj, index);
> + Â Â Â Â Â Â Â Â Â Â Â BUG_ON(pampd_del != pampd);
> + Â Â Â Â Â Â Â Â Â Â Â (*tmem_pamops.free)(pampd, pool);
> + Â Â Â Â Â Â Â Â Â Â Â if (obj->pampd_count == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â objnew = obj;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â objfound = NULL;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â pampd = NULL;
> + Â Â Â Â Â Â Â }
> + Â Â Â } else {
> + Â Â Â Â Â Â Â obj = objnew = (*tmem_hostops.obj_alloc)(pool);
> + Â Â Â Â Â Â Â if (unlikely(obj == NULL)) {
> + Â Â Â Â Â Â Â Â Â Â Â ret = -ENOMEM;
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â tmem_obj_init(obj, hb, pool, oidp);
> + Â Â Â }
> + Â Â Â BUG_ON(obj == NULL);
> + Â Â Â BUG_ON(((objnew != obj) && (objfound != obj)) || (objnew == objfound));
> + Â Â Â pampd = (*tmem_pamops.create)(obj->pool, &obj->oid, index, page);
> + Â Â Â if (unlikely(pampd == NULL))
> + Â Â Â Â Â Â Â goto free;
> + Â Â Â ret = tmem_pampd_add_to_obj(obj, index, pampd);
> + Â Â Â if (unlikely(ret == -ENOMEM))
> + Â Â Â Â Â Â Â /* may have partially built objnode tree ("stump") */
> + Â Â Â Â Â Â Â goto delete_and_free;
> + Â Â Â goto out;
> +
> +delete_and_free:
> + Â Â Â (void)tmem_pampd_delete_from_obj(obj, index);
> +free:
> + Â Â Â if (pampd)
> + Â Â Â Â Â Â Â (*tmem_pamops.free)(pampd, pool);
> + Â Â Â if (objnew) {
> + Â Â Â Â Â Â Â tmem_obj_free(objnew, hb);
> + Â Â Â Â Â Â Â (*tmem_hostops.obj_free)(objnew, pool);
> + Â Â Â }
> +out:
> + Â Â Â spin_unlock(&hb->lock);
> + Â Â Â return ret;
> +}
> +
> +/*
> + * "Get" a page, e.g. if one can be found, copy the tmem page with the
> + * matching handle from PAM space to the kernel. ÂBy tmem definition,
> + * when a "get" is successful on an ephemeral page, the page is "flushed",
> + * and when a "get" is successful on a persistent page, the page is retained
> + * in tmem. ÂNote that to preserve
> + * coherency, "get" can never be skipped if tmem contains the data.
> + * That is, if a get is done with a certain handle and fails, any
> + * subsequent "get" must also fail (unless of course there is a
> + * "put" done with the same handle).
> +
> + */
> +int tmem_get(struct tmem_pool *pool, struct tmem_oid *oidp,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â uint32_t index, struct page *page)
> +{
> + Â Â Â struct tmem_obj *obj;
> + Â Â Â void *pampd;
> + Â Â Â bool ephemeral = is_ephemeral(pool);
> + Â Â Â uint32_t ret = -1;
> + Â Â Â struct tmem_hashbucket *hb;
> +
> + Â Â Â hb = &pool->hashbucket[tmem_oid_hash(oidp)];
> + Â Â Â spin_lock(&hb->lock);
> + Â Â Â obj = tmem_obj_find(hb, oidp);
> + Â Â Â if (obj == NULL)
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â ephemeral = is_ephemeral(pool);
> + Â Â Â if (ephemeral)
> + Â Â Â Â Â Â Â pampd = tmem_pampd_delete_from_obj(obj, index);

I hope you write down about this exclusive characteristic of ephemeral
in description.

> + Â Â Â else
> + Â Â Â Â Â Â Â pampd = tmem_pampd_lookup_in_obj(obj, index);
> + Â Â Â if (pampd == NULL)
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â ret = (*tmem_pamops.get_data)(page, pampd, pool);
> + Â Â Â if (ret < 0)
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â if (ephemeral) {
> + Â Â Â Â Â Â Â (*tmem_pamops.free)(pampd, pool);
> + Â Â Â Â Â Â Â if (obj->pampd_count == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â tmem_obj_free(obj, hb);
> + Â Â Â Â Â Â Â Â Â Â Â (*tmem_hostops.obj_free)(obj, pool);
> + Â Â Â Â Â Â Â Â Â Â Â obj = NULL;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> + Â Â Â ret = 0;
> +out:
> + Â Â Â spin_unlock(&hb->lock);
> + Â Â Â return ret;
> +}
> +
> +/*
> + * If a page in tmem matches the handle, "flush" this page from tmem such
> + * that any subsequent "get" does not succeed (unless, of course, there
> + * was another "put" with the same handle).
> + */
> +int tmem_flush_page(struct tmem_pool *pool,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct tmem_oid *oidp, uint32_t index)
> +{
> + Â Â Â struct tmem_obj *obj;
> + Â Â Â void *pampd;
> + Â Â Â int ret = -1;
> + Â Â Â struct tmem_hashbucket *hb;
> +
> + Â Â Â hb = &pool->hashbucket[tmem_oid_hash(oidp)];
> + Â Â Â spin_lock(&hb->lock);
> + Â Â Â obj = tmem_obj_find(hb, oidp);
> + Â Â Â if (obj == NULL)
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â pampd = tmem_pampd_delete_from_obj(obj, index);
> + Â Â Â if (pampd == NULL)
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â (*tmem_pamops.free)(pampd, pool);
> + Â Â Â if (obj->pampd_count == 0) {
> + Â Â Â Â Â Â Â tmem_obj_free(obj, hb);
> + Â Â Â Â Â Â Â (*tmem_hostops.obj_free)(obj, pool);
> + Â Â Â }
> + Â Â Â ret = 0;
> +
> +out:
> + Â Â Â spin_unlock(&hb->lock);
> + Â Â Â return ret;
> +}
> +
> +/*
> + * "Flush" all pages in tmem matching this oid.
> + */
> +int tmem_flush_object(struct tmem_pool *pool, struct tmem_oid *oidp)
> +{
> + Â Â Â struct tmem_obj *obj;
> + Â Â Â struct tmem_hashbucket *hb;
> + Â Â Â int ret = -1;
> +
> + Â Â Â hb = &pool->hashbucket[tmem_oid_hash(oidp)];
> + Â Â Â spin_lock(&hb->lock);
> + Â Â Â obj = tmem_obj_find(hb, oidp);
> + Â Â Â if (obj == NULL)
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â tmem_pampd_destroy_all_in_obj(obj);
> + Â Â Â tmem_obj_free(obj, hb);
> + Â Â Â (*tmem_hostops.obj_free)(obj, pool);
> + Â Â Â ret = 0;
> +
> +out:
> + Â Â Â spin_unlock(&hb->lock);

Couldn't we make hb->lock by fine-grained with a new design?

spin_lock
obj = tmem_obj_find
tmem_obj_free
spin_unlock
tmem_pampd_destroy_all_in_obj
(*tmem_hostops.obj_free)



> + Â Â Â return ret;
> +}
> +
> +/*
> + * "Flush" all pages (and tmem_objs) from this tmem_pool and disable
> + * all subsequent access to this tmem_pool.
> + */
> +int tmem_destroy_pool(struct tmem_pool *pool)
> +{
> + Â Â Â int ret = -1;
> +
> + Â Â Â if (pool == NULL)
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â tmem_pool_flush(pool, 1);
> + Â Â Â ret = 0;
> +out:
> + Â Â Â return ret;
> +}
> +
> +static LIST_HEAD(tmem_global_pool_list);
> +
> +/*
> + * Create a new tmem_pool with the provided flag and return
> + * a pool id provided by the tmem host implementation.
> + */
> +void tmem_new_pool(struct tmem_pool *pool, uint32_t flags)
> +{
> + Â Â Â int persistent = flags & TMEM_POOL_PERSIST;
> + Â Â Â int shared = flags & TMEM_POOL_SHARED;
> + Â Â Â struct tmem_hashbucket *hb = &pool->hashbucket[0];
> + Â Â Â int i;
> +
> + Â Â Â for (i = 0; i < TMEM_HASH_BUCKETS; i++, hb++) {
> + Â Â Â Â Â Â Â hb->obj_rb_root = RB_ROOT;
> + Â Â Â Â Â Â Â spin_lock_init(&hb->lock);
> + Â Â Â }
> + Â Â Â INIT_LIST_HEAD(&pool->pool_list);
> + Â Â Â atomic_set(&pool->obj_count, 0);
> + Â Â Â SET_SENTINEL(pool, POOL);
> + Â Â Â list_add_tail(&pool->pool_list, &tmem_global_pool_list);
> + Â Â Â pool->persistent = persistent;
> + Â Â Â pool->shared = shared;
> +}
> --- linux-2.6.37/drivers/staging/zcache/tmem.h Â1969-12-31 17:00:00.000000000 -0700
> +++ linux-2.6.37-zcache/drivers/staging/zcache/tmem.h  2011-02-05 15:44:19.000000000 -0700
> @@ -0,0 +1,195 @@
> +/*
> + * tmem.h
> + *
> + * Transcendent memory
> + *
> + * Copyright (c) 2009-2011, Dan Magenheimer, Oracle Corp.
> + */
> +
> +#ifndef _TMEM_H_
> +#define _TMEM_H_
> +
> +#include <linux/types.h>
> +#include <linux/highmem.h>
> +#include <linux/hash.h>
> +#include <linux/atomic.h>
> +
> +/*
> + * These are pre-defined by the Xen<->Linux ABI
> + */
> +#define TMEM_PUT_PAGE Â Â Â Â Â Â Â Â Â4
> +#define TMEM_GET_PAGE Â Â Â Â Â Â Â Â Â5
> +#define TMEM_FLUSH_PAGE Â Â Â Â Â Â Â Â Â Â Â Â6
> +#define TMEM_FLUSH_OBJECT Â Â Â Â Â Â Â7
> +#define TMEM_POOL_PERSIST Â Â Â Â Â Â Â1
> +#define TMEM_POOL_SHARED Â Â Â Â Â Â Â 2
> +#define TMEM_POOL_PRECOMPRESSED Â Â Â Â Â Â Â Â4
> +#define TMEM_POOL_PAGESIZE_SHIFT Â Â Â 4
> +#define TMEM_POOL_PAGESIZE_MASK Â Â Â Â Â Â Â Â0xf
> +#define TMEM_POOL_RESERVED_BITS Â Â Â Â Â Â Â Â0x00ffff00
> +
> +/*
> + * sentinels have proven very useful for debugging but can be removed
> + * or disabled before final merge.
> + */
> +#define SENTINELS
> +#ifdef SENTINELS
> +#define DECL_SENTINEL uint32_t sentinel;
> +#define SET_SENTINEL(_x, _y) (_x->sentinel = _y##_SENTINEL)
> +#define INVERT_SENTINEL(_x, _y) (_x->sentinel = ~_y##_SENTINEL)
> +#define ASSERT_SENTINEL(_x, _y) WARN_ON(_x->sentinel != _y##_SENTINEL)
> +#define ASSERT_INVERTED_SENTINEL(_x, _y) WARN_ON(_x->sentinel != ~_y##_SENTINEL)
> +#else
> +#define DECL_SENTINEL
> +#define SET_SENTINEL(_x, _y) do { } while (0)
> +#define INVERT_SENTINEL(_x, _y) do { } while (0)
> +#define ASSERT_SENTINEL(_x, _y) do { } while (0)
> +#define ASSERT_INVERTED_SENTINEL(_x, _y) do { } while (0)
> +#endif
> +
> +#define ASSERT_SPINLOCK(_l) Â ÂWARN_ON(!spin_is_locked(_l))
> +
> +/*
> + * A pool is the highest-level data structure managed by tmem and
> + * usually corresponds to a large independent set of pages such as
> + * a filesystem. ÂEach pool has an id, and certain attributes and counters.
> + * It also contains a set of hash buckets, each of which contains an rbtree
> + * of objects and a lock to manage concurrency within the pool.
> + */
> +
> +#define TMEM_HASH_BUCKET_BITS Â8
> +#define TMEM_HASH_BUCKETS Â Â Â(1<<TMEM_HASH_BUCKET_BITS)
> +
> +struct tmem_hashbucket {
> + Â Â Â struct rb_root obj_rb_root;
> + Â Â Â spinlock_t lock;
> +};
> +
> +struct tmem_pool {
> + Â Â Â void *client; /* "up" for some clients, avoids table lookup */
> + Â Â Â struct list_head pool_list;
> + Â Â Â uint32_t pool_id;
> + Â Â Â bool persistent;
> + Â Â Â bool shared;

Just nitpick.
Do we need two each variable for persist and shared?
Couldn't we merge it into just one "flag variable"?

> + Â Â Â atomic_t obj_count;
> + Â Â Â atomic_t refcount;
> + Â Â Â struct tmem_hashbucket hashbucket[TMEM_HASH_BUCKETS];
> + Â Â Â DECL_SENTINEL
> +};
> +
> +#define is_persistent(_p) Â(_p->persistent)
> +#define is_ephemeral(_p) Â (!(_p->persistent))
> +
> +/*
> + * An object id ("oid") is large: 192-bits (to ensure, for example, files
> + * in a modern filesystem can be uniquely identified).
> + */
> +
> +struct tmem_oid {
> + Â Â Â uint64_t oid[3];
> +};
> +
> +static inline void tmem_oid_set_invalid(struct tmem_oid *oidp)
> +{
> + Â Â Â oidp->oid[0] = oidp->oid[1] = oidp->oid[2] = -1UL;
> +}
> +
> +static inline bool tmem_oid_valid(struct tmem_oid *oidp)
> +{
> + Â Â Â return oidp->oid[0] != -1UL || oidp->oid[1] != -1UL ||
> + Â Â Â Â Â Â Â oidp->oid[2] != -1UL;
> +}
> +
> +static inline int tmem_oid_compare(struct tmem_oid *left,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct tmem_oid *right)
> +{
> + Â Â Â int ret;
> +
> + Â Â Â if (left->oid[2] == right->oid[2]) {
> + Â Â Â Â Â Â Â if (left->oid[1] == right->oid[1]) {
> + Â Â Â Â Â Â Â Â Â Â Â if (left->oid[0] == right->oid[0])
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ret = 0;
> + Â Â Â Â Â Â Â Â Â Â Â else if (left->oid[0] < right->oid[0])
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ret = -1;
> + Â Â Â Â Â Â Â Â Â Â Â else
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return 1;
> + Â Â Â Â Â Â Â } else if (left->oid[1] < right->oid[1])
> + Â Â Â Â Â Â Â Â Â Â Â ret = -1;
> + Â Â Â Â Â Â Â else
> + Â Â Â Â Â Â Â Â Â Â Â ret = 1;
> + Â Â Â } else if (left->oid[2] < right->oid[2])
> + Â Â Â Â Â Â Â ret = -1;
> + Â Â Â else
> + Â Â Â Â Â Â Â ret = 1;
> + Â Â Â return ret;
> +}
> +
> +static inline unsigned tmem_oid_hash(struct tmem_oid *oidp)
> +{
> + Â Â Â return hash_long(oidp->oid[0] ^ oidp->oid[1] ^ oidp->oid[2],
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â TMEM_HASH_BUCKET_BITS);
> +}
> +
> +/*
> + * A tmem_obj contains an identifier (oid), pointers to the parent
> + * pool and the rb_tree to which it belongs, counters, and an ordered
> + * set of pampds, structured in a radix-tree-like tree. ÂThe intermediate
> + * nodes of the tree are called tmem_objnodes.
> + */
> +
> +struct tmem_objnode;
> +
> +struct tmem_obj {
> + Â Â Â struct tmem_oid oid;
> + Â Â Â struct tmem_pool *pool;
> + Â Â Â struct rb_node rb_tree_node;
> + Â Â Â struct tmem_objnode *objnode_tree_root;
> + Â Â Â unsigned int objnode_tree_height;
> + Â Â Â unsigned long objnode_count;
> + Â Â Â long pampd_count;
> + Â Â Â DECL_SENTINEL
> +};
> +
> +#define OBJNODE_TREE_MAP_SHIFT 6
> +#define OBJNODE_TREE_MAP_SIZE (1UL << OBJNODE_TREE_MAP_SHIFT)
> +#define OBJNODE_TREE_MAP_MASK (OBJNODE_TREE_MAP_SIZE-1)
> +#define OBJNODE_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
> +#define OBJNODE_TREE_MAX_PATH \
> + Â Â Â Â Â Â Â (OBJNODE_TREE_INDEX_BITS/OBJNODE_TREE_MAP_SHIFT + 2)
> +
> +struct tmem_objnode {
> + Â Â Â struct tmem_obj *obj;
> + Â Â Â DECL_SENTINEL
> + Â Â Â void *slots[OBJNODE_TREE_MAP_SIZE];
> + Â Â Â unsigned int slots_in_use;
> +};
> +
> +/* pampd abstract datatype methods provided by the PAM implementation */
> +struct tmem_pamops {
> + Â Â Â void *(*create)(struct tmem_pool *, struct tmem_oid *, uint32_t,
> + Â Â Â Â Â Â Â Â Â Â Â struct page *);
> + Â Â Â int (*get_data)(struct page *, void *, struct tmem_pool *);
> + Â Â Â void (*free)(void *, struct tmem_pool *);
> +};

Hmm.. create/get_data/free isn't good naming, I think.
How about add/get/delete like page/swap cache operation?

> +extern void tmem_register_pamops(struct tmem_pamops *m);
> +
> +/* memory allocation methods provided by the host implementation */
> +struct tmem_hostops {
> + Â Â Â struct tmem_obj *(*obj_alloc)(struct tmem_pool *);
> + Â Â Â void (*obj_free)(struct tmem_obj *, struct tmem_pool *);
> + Â Â Â struct tmem_objnode *(*objnode_alloc)(struct tmem_pool *);
> + Â Â Â void (*objnode_free)(struct tmem_objnode *, struct tmem_pool *);
> +};

As I said, I am not sure the benefit of hostop.
If we can do, I want to hide it from host.

> +extern void tmem_register_hostops(struct tmem_hostops *m);
> +
> +/* core tmem accessor functions */
> +extern int tmem_put(struct tmem_pool *, struct tmem_oid *, uint32_t index,
> + Â Â Â Â Â Â Â Â Â Â Â struct page *page);
> +extern int tmem_get(struct tmem_pool *, struct tmem_oid *, uint32_t index,
> + Â Â Â Â Â Â Â Â Â Â Â struct page *page);
> +extern int tmem_flush_page(struct tmem_pool *, struct tmem_oid *,
> + Â Â Â Â Â Â Â Â Â Â Â uint32_t index);
> +extern int tmem_flush_object(struct tmem_pool *, struct tmem_oid *);
> +extern int tmem_destroy_pool(struct tmem_pool *);
> +extern void tmem_new_pool(struct tmem_pool *, uint32_t);
> +#endif /* _TMEM_H */
>


It's very quick review so maybe I miss your design/goal.
Sorry if I am doing such a thing.

--
Kind regards,
Minchan Kim
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/