Lines Matching refs:ctx

43 int mapping_add(struct mapping_ctx *ctx, void *data, u32 *id)
49 mutex_lock(&ctx->lock);
51 hash_key = jhash(data, ctx->data_size, 0);
52 hash_for_each_possible(ctx->ht, mi, node, hash_key) {
53 if (!memcmp(data, mi->data, ctx->data_size))
57 mi = kzalloc(sizeof(*mi) + ctx->data_size, GFP_KERNEL);
61 memcpy(mi->data, data, ctx->data_size);
62 hash_add(ctx->ht, &mi->node, hash_key);
64 err = xa_alloc(&ctx->xarray, &mi->id, mi, XA_LIMIT(1, ctx->max_id),
72 mutex_unlock(&ctx->lock);
80 mutex_unlock(&ctx->lock);
85 static void mapping_remove_and_free(struct mapping_ctx *ctx,
88 xa_erase(&ctx->xarray, mi->id);
92 static void mapping_free_item(struct mapping_ctx *ctx,
95 if (!ctx->delayed_removal) {
96 mapping_remove_and_free(ctx, mi);
102 spin_lock(&ctx->pending_list_lock);
103 list_add_tail(&mi->list, &ctx->pending_list);
104 spin_unlock(&ctx->pending_list_lock);
106 schedule_delayed_work(&ctx->dwork, MAPPING_GRACE_PERIOD);
109 int mapping_remove(struct mapping_ctx *ctx, u32 id)
115 mutex_lock(&ctx->lock);
116 mi = xa_load(&ctx->xarray, index);
125 mapping_free_item(ctx, mi);
127 mutex_unlock(&ctx->lock);
132 int mapping_find(struct mapping_ctx *ctx, u32 id, void *data)
139 mi = xa_load(&ctx->xarray, index);
143 memcpy(data, mi->data, ctx->data_size);
152 mapping_remove_and_free_list(struct mapping_ctx *ctx, struct list_head *list)
157 mapping_remove_and_free(ctx, mi);
165 struct mapping_ctx *ctx;
167 ctx = container_of(work, struct mapping_ctx, dwork.work);
169 spin_lock(&ctx->pending_list_lock);
170 list_for_each_entry_safe(mi, next, &ctx->pending_list, list) {
177 spin_unlock(&ctx->pending_list_lock);
179 mapping_remove_and_free_list(ctx, &pending_items);
182 schedule_delayed_work(&ctx->dwork, abs(min_timeout - now));
185 static void mapping_flush_work(struct mapping_ctx *ctx)
187 if (!ctx->delayed_removal)
190 cancel_delayed_work_sync(&ctx->dwork);
191 mapping_remove_and_free_list(ctx, &ctx->pending_list);
197 struct mapping_ctx *ctx;
199 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
200 if (!ctx)
203 ctx->max_id = max_id ? max_id : UINT_MAX;
204 ctx->data_size = data_size;
207 INIT_DELAYED_WORK(&ctx->dwork, mapping_work_handler);
208 INIT_LIST_HEAD(&ctx->pending_list);
209 spin_lock_init(&ctx->pending_list_lock);
210 ctx->delayed_removal = true;
213 mutex_init(&ctx->lock);
214 xa_init_flags(&ctx->xarray, XA_FLAGS_ALLOC1);
216 refcount_set(&ctx->refcount, 1);
217 INIT_LIST_HEAD(&ctx->list);
219 return ctx;
225 struct mapping_ctx *ctx;
228 list_for_each_entry(ctx, &shared_ctx_list, list) {
229 if (ctx->id == id && ctx->type == type) {
230 if (refcount_inc_not_zero(&ctx->refcount))
236 ctx = mapping_create(data_size, max_id, delayed_removal);
237 if (IS_ERR(ctx))
240 ctx->id = id;
241 ctx->type = type;
242 list_add(&ctx->list, &shared_ctx_list);
246 return ctx;
249 void mapping_destroy(struct mapping_ctx *ctx)
251 if (!refcount_dec_and_test(&ctx->refcount))
255 list_del(&ctx->list);
258 mapping_flush_work(ctx);
259 xa_destroy(&ctx->xarray);
260 mutex_destroy(&ctx->lock);
262 kfree(ctx);