ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 1)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 2) #include <linux/export.h>
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 3) #include <linux/generic-radix-tree.h>
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 4) #include <linux/gfp.h>
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 5) #include <linux/kmemleak.h>
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 6)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 7) #define GENRADIX_ARY (PAGE_SIZE / sizeof(struct genradix_node *))
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 8) #define GENRADIX_ARY_SHIFT ilog2(GENRADIX_ARY)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 9)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 10) struct genradix_node {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 11) union {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 12) /* Interior node: */
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 13) struct genradix_node *children[GENRADIX_ARY];
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 14)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 15) /* Leaf: */
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 16) u8 data[PAGE_SIZE];
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 17) };
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 18) };
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 19)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 20) static inline int genradix_depth_shift(unsigned depth)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 21) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 22) return PAGE_SHIFT + GENRADIX_ARY_SHIFT * depth;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 23) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 24)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 25) /*
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 26) * Returns size (of data, in bytes) that a tree of a given depth holds:
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 27) */
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 28) static inline size_t genradix_depth_size(unsigned depth)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 29) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 30) return 1UL << genradix_depth_shift(depth);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 31) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 32)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 33) /* depth that's needed for a genradix that can address up to ULONG_MAX: */
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 34) #define GENRADIX_MAX_DEPTH \
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 35) DIV_ROUND_UP(BITS_PER_LONG - PAGE_SHIFT, GENRADIX_ARY_SHIFT)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 36)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 37) #define GENRADIX_DEPTH_MASK \
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 38) ((unsigned long) (roundup_pow_of_two(GENRADIX_MAX_DEPTH + 1) - 1))
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 39)
e3f4faa42095c (Valdis Kletnieks 2019-09-25 16:46:23 -0700 40) static inline unsigned genradix_root_to_depth(struct genradix_root *r)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 41) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 42) return (unsigned long) r & GENRADIX_DEPTH_MASK;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 43) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 44)
e3f4faa42095c (Valdis Kletnieks 2019-09-25 16:46:23 -0700 45) static inline struct genradix_node *genradix_root_to_node(struct genradix_root *r)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 46) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 47) return (void *) ((unsigned long) r & ~GENRADIX_DEPTH_MASK);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 48) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 49)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 50) /*
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 51) * Returns pointer to the specified byte @offset within @radix, or NULL if not
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 52) * allocated
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 53) */
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 54) void *__genradix_ptr(struct __genradix *radix, size_t offset)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 55) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 56) struct genradix_root *r = READ_ONCE(radix->root);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 57) struct genradix_node *n = genradix_root_to_node(r);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 58) unsigned level = genradix_root_to_depth(r);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 59)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 60) if (ilog2(offset) >= genradix_depth_shift(level))
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 61) return NULL;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 62)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 63) while (1) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 64) if (!n)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 65) return NULL;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 66) if (!level)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 67) break;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 68)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 69) level--;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 70)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 71) n = n->children[offset >> genradix_depth_shift(level)];
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 72) offset &= genradix_depth_size(level) - 1;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 73) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 74)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 75) return &n->data[offset];
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 76) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 77) EXPORT_SYMBOL(__genradix_ptr);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 78)
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 79) static inline struct genradix_node *genradix_alloc_node(gfp_t gfp_mask)
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 80) {
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 81) struct genradix_node *node;
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 82)
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 83) node = (struct genradix_node *)__get_free_page(gfp_mask|__GFP_ZERO);
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 84)
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 85) /*
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 86) * We're using pages (not slab allocations) directly for kernel data
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 87) * structures, so we need to explicitly inform kmemleak of them in order
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 88) * to avoid false positive memory leak reports.
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 89) */
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 90) kmemleak_alloc(node, PAGE_SIZE, 1, gfp_mask);
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 91) return node;
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 92) }
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 93)
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 94) static inline void genradix_free_node(struct genradix_node *node)
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 95) {
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 96) kmemleak_free(node);
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 97) free_page((unsigned long)node);
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 98) }
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 99)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 100) /*
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 101) * Returns pointer to the specified byte @offset within @radix, allocating it if
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 102) * necessary - newly allocated slots are always zeroed out:
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 103) */
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 104) void *__genradix_ptr_alloc(struct __genradix *radix, size_t offset,
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 105) gfp_t gfp_mask)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 106) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 107) struct genradix_root *v = READ_ONCE(radix->root);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 108) struct genradix_node *n, *new_node = NULL;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 109) unsigned level;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 110)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 111) /* Increase tree depth if necessary: */
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 112) while (1) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 113) struct genradix_root *r = v, *new_root;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 114)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 115) n = genradix_root_to_node(r);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 116) level = genradix_root_to_depth(r);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 117)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 118) if (n && ilog2(offset) < genradix_depth_shift(level))
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 119) break;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 120)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 121) if (!new_node) {
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 122) new_node = genradix_alloc_node(gfp_mask);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 123) if (!new_node)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 124) return NULL;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 125) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 126)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 127) new_node->children[0] = n;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 128) new_root = ((struct genradix_root *)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 129) ((unsigned long) new_node | (n ? level + 1 : 0)));
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 130)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 131) if ((v = cmpxchg_release(&radix->root, r, new_root)) == r) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 132) v = new_root;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 133) new_node = NULL;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 134) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 135) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 136)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 137) while (level--) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 138) struct genradix_node **p =
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 139) &n->children[offset >> genradix_depth_shift(level)];
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 140) offset &= genradix_depth_size(level) - 1;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 141)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 142) n = READ_ONCE(*p);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 143) if (!n) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 144) if (!new_node) {
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 145) new_node = genradix_alloc_node(gfp_mask);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 146) if (!new_node)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 147) return NULL;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 148) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 149)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 150) if (!(n = cmpxchg_release(p, NULL, new_node)))
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 151) swap(n, new_node);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 152) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 153) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 154)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 155) if (new_node)
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 156) genradix_free_node(new_node);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 157)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 158) return &n->data[offset];
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 159) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 160) EXPORT_SYMBOL(__genradix_ptr_alloc);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 161)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 162) void *__genradix_iter_peek(struct genradix_iter *iter,
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 163) struct __genradix *radix,
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 164) size_t objs_per_page)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 165) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 166) struct genradix_root *r;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 167) struct genradix_node *n;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 168) unsigned level, i;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 169) restart:
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 170) r = READ_ONCE(radix->root);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 171) if (!r)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 172) return NULL;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 173)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 174) n = genradix_root_to_node(r);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 175) level = genradix_root_to_depth(r);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 176)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 177) if (ilog2(iter->offset) >= genradix_depth_shift(level))
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 178) return NULL;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 179)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 180) while (level) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 181) level--;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 182)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 183) i = (iter->offset >> genradix_depth_shift(level)) &
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 184) (GENRADIX_ARY - 1);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 185)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 186) while (!n->children[i]) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 187) i++;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 188) iter->offset = round_down(iter->offset +
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 189) genradix_depth_size(level),
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 190) genradix_depth_size(level));
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 191) iter->pos = (iter->offset >> PAGE_SHIFT) *
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 192) objs_per_page;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 193) if (i == GENRADIX_ARY)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 194) goto restart;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 195) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 196)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 197) n = n->children[i];
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 198) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 199)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 200) return &n->data[iter->offset & (PAGE_SIZE - 1)];
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 201) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 202) EXPORT_SYMBOL(__genradix_iter_peek);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 203)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 204) static void genradix_free_recurse(struct genradix_node *n, unsigned level)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 205) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 206) if (level) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 207) unsigned i;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 208)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 209) for (i = 0; i < GENRADIX_ARY; i++)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 210) if (n->children[i])
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 211) genradix_free_recurse(n->children[i], level - 1);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 212) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 213)
3c52b0af059e1 (Eric Biggers 2019-10-14 14:11:54 -0700 214) genradix_free_node(n);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 215) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 216)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 217) int __genradix_prealloc(struct __genradix *radix, size_t size,
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 218) gfp_t gfp_mask)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 219) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 220) size_t offset;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 221)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 222) for (offset = 0; offset < size; offset += PAGE_SIZE)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 223) if (!__genradix_ptr_alloc(radix, offset, gfp_mask))
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 224) return -ENOMEM;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 225)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 226) return 0;
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 227) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 228) EXPORT_SYMBOL(__genradix_prealloc);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 229)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 230) void __genradix_free(struct __genradix *radix)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 231) {
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 232) struct genradix_root *r = xchg(&radix->root, NULL);
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 233)
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 234) genradix_free_recurse(genradix_root_to_node(r),
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 235) genradix_root_to_depth(r));
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 236) }
ba20ba2e3743b (Kent Overstreet 2019-03-11 23:31:14 -0700 237) EXPORT_SYMBOL(__genradix_free);