2 * Copyright (C) 2017 Red Hat. All rights reserved.
4 * This file is released under the GPL.
7 #include "dm-cache-background-tracker.h"
9 /*----------------------------------------------------------------*/
11 #define DM_MSG_PREFIX "dm-background-tracker"
14 struct list_head list
;
16 struct policy_work work
;
19 struct background_tracker
{
21 atomic_t pending_promotes
;
22 atomic_t pending_writebacks
;
23 atomic_t pending_demotes
;
25 struct list_head issued
;
26 struct list_head queued
;
27 struct rb_root pending
;
29 struct kmem_cache
*work_cache
;
32 struct background_tracker
*btracker_create(unsigned max_work
)
34 struct background_tracker
*b
= kmalloc(sizeof(*b
), GFP_KERNEL
);
37 DMERR("couldn't create background_tracker");
41 b
->max_work
= max_work
;
42 atomic_set(&b
->pending_promotes
, 0);
43 atomic_set(&b
->pending_writebacks
, 0);
44 atomic_set(&b
->pending_demotes
, 0);
46 INIT_LIST_HEAD(&b
->issued
);
47 INIT_LIST_HEAD(&b
->queued
);
50 b
->work_cache
= KMEM_CACHE(bt_work
, 0);
52 DMERR("couldn't create mempool for background work items");
59 EXPORT_SYMBOL_GPL(btracker_create
);
61 void btracker_destroy(struct background_tracker
*b
)
63 kmem_cache_destroy(b
->work_cache
);
66 EXPORT_SYMBOL_GPL(btracker_destroy
);
68 static int cmp_oblock(dm_oblock_t lhs
, dm_oblock_t rhs
)
70 if (from_oblock(lhs
) < from_oblock(rhs
))
73 if (from_oblock(rhs
) < from_oblock(lhs
))
79 static bool __insert_pending(struct background_tracker
*b
,
84 struct rb_node
**new = &b
->pending
.rb_node
, *parent
= NULL
;
87 w
= container_of(*new, struct bt_work
, node
);
90 cmp
= cmp_oblock(w
->work
.oblock
, nw
->work
.oblock
);
92 new = &((*new)->rb_left
);
95 new = &((*new)->rb_right
);
102 rb_link_node(&nw
->node
, parent
, new);
103 rb_insert_color(&nw
->node
, &b
->pending
);
108 static struct bt_work
*__find_pending(struct background_tracker
*b
,
113 struct rb_node
**new = &b
->pending
.rb_node
;
116 w
= container_of(*new, struct bt_work
, node
);
118 cmp
= cmp_oblock(w
->work
.oblock
, oblock
);
120 new = &((*new)->rb_left
);
123 new = &((*new)->rb_right
);
129 return *new ? w
: NULL
;
133 static void update_stats(struct background_tracker
*b
, struct policy_work
*w
, int delta
)
137 atomic_add(delta
, &b
->pending_promotes
);
141 atomic_add(delta
, &b
->pending_demotes
);
144 case POLICY_WRITEBACK
:
145 atomic_add(delta
, &b
->pending_writebacks
);
150 unsigned btracker_nr_writebacks_queued(struct background_tracker
*b
)
152 return atomic_read(&b
->pending_writebacks
);
154 EXPORT_SYMBOL_GPL(btracker_nr_writebacks_queued
);
156 unsigned btracker_nr_demotions_queued(struct background_tracker
*b
)
158 return atomic_read(&b
->pending_demotes
);
160 EXPORT_SYMBOL_GPL(btracker_nr_demotions_queued
);
162 static bool max_work_reached(struct background_tracker
*b
)
168 int btracker_queue(struct background_tracker
*b
,
169 struct policy_work
*work
,
170 struct policy_work
**pwork
)
177 if (max_work_reached(b
))
180 w
= kmem_cache_alloc(b
->work_cache
, GFP_NOWAIT
);
184 memcpy(&w
->work
, work
, sizeof(*work
));
186 if (!__insert_pending(b
, w
)) {
188 * There was a race, we'll just ignore this second
189 * bit of work for the same oblock.
191 kmem_cache_free(b
->work_cache
, w
);
197 list_add(&w
->list
, &b
->issued
);
199 list_add(&w
->list
, &b
->queued
);
200 update_stats(b
, &w
->work
, 1);
204 EXPORT_SYMBOL_GPL(btracker_queue
);
207 * Returns -ENODATA if there's no work.
209 int btracker_issue(struct background_tracker
*b
, struct policy_work
**work
)
213 if (list_empty(&b
->queued
))
216 w
= list_first_entry(&b
->queued
, struct bt_work
, list
);
217 list_move(&w
->list
, &b
->issued
);
222 EXPORT_SYMBOL_GPL(btracker_issue
);
224 void btracker_complete(struct background_tracker
*b
,
225 struct policy_work
*op
)
227 struct bt_work
*w
= container_of(op
, struct bt_work
, work
);
229 update_stats(b
, &w
->work
, -1);
230 rb_erase(&w
->node
, &b
->pending
);
232 kmem_cache_free(b
->work_cache
, w
);
234 EXPORT_SYMBOL_GPL(btracker_complete
);
236 bool btracker_promotion_already_present(struct background_tracker
*b
,
239 return __find_pending(b
, oblock
) != NULL
;
241 EXPORT_SYMBOL_GPL(btracker_promotion_already_present
);
243 /*----------------------------------------------------------------*/