6 * Copyright (c) 2009-2012, Dan Magenheimer, Oracle Corp.
12 #include <linux/types.h>
13 #include <linux/highmem.h>
14 #include <linux/hash.h>
15 #include <linux/atomic.h>
18 * These are defined by the Xen<->Linux ABI so should remain consistent
20 #define TMEM_POOL_PERSIST 1
21 #define TMEM_POOL_SHARED 2
22 #define TMEM_POOL_PRECOMPRESSED 4
23 #define TMEM_POOL_PAGESIZE_SHIFT 4
24 #define TMEM_POOL_PAGESIZE_MASK 0xf
25 #define TMEM_POOL_RESERVED_BITS 0x00ffff00
28 * sentinels have proven very useful for debugging but can be removed
29 * or disabled before final merge.
33 #define DECL_SENTINEL uint32_t sentinel;
34 #define SET_SENTINEL(_x, _y) (_x->sentinel = _y##_SENTINEL)
35 #define INVERT_SENTINEL(_x, _y) (_x->sentinel = ~_y##_SENTINEL)
36 #define ASSERT_SENTINEL(_x, _y) WARN_ON(_x->sentinel != _y##_SENTINEL)
37 #define ASSERT_INVERTED_SENTINEL(_x, _y) WARN_ON(_x->sentinel != ~_y##_SENTINEL)
40 #define SET_SENTINEL(_x, _y) do { } while (0)
41 #define INVERT_SENTINEL(_x, _y) do { } while (0)
42 #define ASSERT_SENTINEL(_x, _y) do { } while (0)
43 #define ASSERT_INVERTED_SENTINEL(_x, _y) do { } while (0)
46 #define ASSERT_SPINLOCK(_l) lockdep_assert_held(_l)
49 * A pool is the highest-level data structure managed by tmem and
50 * usually corresponds to a large independent set of pages such as
51 * a filesystem. Each pool has an id, and certain attributes and counters.
52 * It also contains a set of hash buckets, each of which contains an rbtree
53 * of objects and a lock to manage concurrency within the pool.
56 #define TMEM_HASH_BUCKET_BITS 8
57 #define TMEM_HASH_BUCKETS (1<<TMEM_HASH_BUCKET_BITS)
59 struct tmem_hashbucket
{
60 struct rb_root obj_rb_root
;
65 void *client
; /* "up" for some clients, avoids table lookup */
66 struct list_head pool_list
;
72 struct tmem_hashbucket hashbucket
[TMEM_HASH_BUCKETS
];
76 #define is_persistent(_p) (_p->persistent)
77 #define is_ephemeral(_p) (!(_p->persistent))
80 * An object id ("oid") is large: 192-bits (to ensure, for example, files
81 * in a modern filesystem can be uniquely identified).
88 static inline void tmem_oid_set_invalid(struct tmem_oid
*oidp
)
90 oidp
->oid
[0] = oidp
->oid
[1] = oidp
->oid
[2] = -1UL;
93 static inline bool tmem_oid_valid(struct tmem_oid
*oidp
)
95 return oidp
->oid
[0] != -1UL || oidp
->oid
[1] != -1UL ||
99 static inline int tmem_oid_compare(struct tmem_oid
*left
,
100 struct tmem_oid
*right
)
104 if (left
->oid
[2] == right
->oid
[2]) {
105 if (left
->oid
[1] == right
->oid
[1]) {
106 if (left
->oid
[0] == right
->oid
[0])
108 else if (left
->oid
[0] < right
->oid
[0])
112 } else if (left
->oid
[1] < right
->oid
[1])
116 } else if (left
->oid
[2] < right
->oid
[2])
123 static inline unsigned tmem_oid_hash(struct tmem_oid
*oidp
)
125 return hash_long(oidp
->oid
[0] ^ oidp
->oid
[1] ^ oidp
->oid
[2],
126 TMEM_HASH_BUCKET_BITS
);
129 #ifdef CONFIG_RAMSTER
130 struct tmem_xhandle
{
132 uint8_t xh_data_cksum
;
133 uint16_t xh_data_size
;
140 static inline struct tmem_xhandle
tmem_xhandle_fill(uint16_t client_id
,
141 struct tmem_pool
*pool
,
142 struct tmem_oid
*oidp
,
145 struct tmem_xhandle xh
;
146 xh
.client_id
= client_id
;
147 xh
.xh_data_cksum
= (uint8_t)-1;
148 xh
.xh_data_size
= (uint16_t)-1;
149 xh
.pool_id
= pool
->pool_id
;
158 * A tmem_obj contains an identifier (oid), pointers to the parent
159 * pool and the rb_tree to which it belongs, counters, and an ordered
160 * set of pampds, structured in a radix-tree-like tree. The intermediate
161 * nodes of the tree are called tmem_objnodes.
168 struct tmem_pool
*pool
;
169 struct rb_node rb_tree_node
;
170 struct tmem_objnode
*objnode_tree_root
;
171 unsigned int objnode_tree_height
;
172 unsigned long objnode_count
;
174 #ifdef CONFIG_RAMSTER
176 * for current design of ramster, all pages belonging to
177 * an object reside on the same remotenode and extra is
178 * used to record the number of the remotenode so a
179 * flush-object operation can specify it
181 void *extra
; /* for private use by pampd implementation */
186 #define OBJNODE_TREE_MAP_SHIFT 6
187 #define OBJNODE_TREE_MAP_SIZE (1UL << OBJNODE_TREE_MAP_SHIFT)
188 #define OBJNODE_TREE_MAP_MASK (OBJNODE_TREE_MAP_SIZE-1)
189 #define OBJNODE_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
190 #define OBJNODE_TREE_MAX_PATH \
191 (OBJNODE_TREE_INDEX_BITS/OBJNODE_TREE_MAP_SHIFT + 2)
193 struct tmem_objnode
{
194 struct tmem_obj
*obj
;
196 void *slots
[OBJNODE_TREE_MAP_SIZE
];
197 unsigned int slots_in_use
;
201 struct tmem_oid oid
; /* 24 bytes */
208 /* pampd abstract datatype methods provided by the PAM implementation */
210 void (*create_finish
)(void *, bool);
211 int (*get_data
)(char *, size_t *, bool, void *, struct tmem_pool
*,
212 struct tmem_oid
*, uint32_t);
213 int (*get_data_and_free
)(char *, size_t *, bool, void *,
214 struct tmem_pool
*, struct tmem_oid
*,
216 void (*free
)(void *, struct tmem_pool
*,
217 struct tmem_oid
*, uint32_t, bool);
218 #ifdef CONFIG_RAMSTER
219 void (*new_obj
)(struct tmem_obj
*);
220 void (*free_obj
)(struct tmem_pool
*, struct tmem_obj
*, bool);
221 void *(*repatriate_preload
)(void *, struct tmem_pool
*,
222 struct tmem_oid
*, uint32_t, bool *);
223 int (*repatriate
)(void *, void *, struct tmem_pool
*,
224 struct tmem_oid
*, uint32_t, bool, void *);
225 bool (*is_remote
)(void *);
226 int (*replace_in_obj
)(void *, struct tmem_obj
*);
229 extern void tmem_register_pamops(struct tmem_pamops
*m
);
231 /* memory allocation methods provided by the host implementation */
232 struct tmem_hostops
{
233 struct tmem_obj
*(*obj_alloc
)(struct tmem_pool
*);
234 void (*obj_free
)(struct tmem_obj
*, struct tmem_pool
*);
235 struct tmem_objnode
*(*objnode_alloc
)(struct tmem_pool
*);
236 void (*objnode_free
)(struct tmem_objnode
*, struct tmem_pool
*);
238 extern void tmem_register_hostops(struct tmem_hostops
*m
);
240 /* core tmem accessor functions */
241 extern int tmem_put(struct tmem_pool
*, struct tmem_oid
*, uint32_t index
,
243 extern int tmem_get(struct tmem_pool
*, struct tmem_oid
*, uint32_t index
,
244 char *, size_t *, bool, int);
245 extern int tmem_flush_page(struct tmem_pool
*, struct tmem_oid
*,
247 extern int tmem_flush_object(struct tmem_pool
*, struct tmem_oid
*);
248 extern int tmem_destroy_pool(struct tmem_pool
*);
249 extern void tmem_new_pool(struct tmem_pool
*, uint32_t);
250 #ifdef CONFIG_RAMSTER
251 extern int tmem_replace(struct tmem_pool
*, struct tmem_oid
*, uint32_t index
,
253 extern void *tmem_localify_get_pampd(struct tmem_pool
*, struct tmem_oid
*,
254 uint32_t index
, struct tmem_obj
**,
256 extern void tmem_localify_finish(struct tmem_obj
*, uint32_t index
,
257 void *, void *, bool);