From: Liam R. Howlett Date: Mon, 10 Dec 2018 17:15:27 +0000 (-0500) Subject: maple_tree: Don't assume 16 byte alignment X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=55d029aa5f13b002fe0e64a1e27c00b3be5c978c;p=users%2Fjedix%2Flinux-maple.git maple_tree: Don't assume 16 byte alignment Assume the allocations are 8 byte aligned, so only use the last two bits to indicate the requested allocation. Test and return the allocation count. Also, fix a small issue in decrementing the requested count before we actually have succeeded in allocating. Signed-off-by: Liam R. Howlett --- diff --git a/lib/maple_tree.c b/lib/maple_tree.c index 8a9c1c20fc8b..57c0b554f6d1 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -63,28 +63,30 @@ static inline void *ma_mk_node(const struct maple_node *node) return (void *)((unsigned long)node | 2); } -static inline void ma_set_alloc_cnt(struct maple_state *ms, int count) +static inline struct maple_node *ma_get_alloc(const struct maple_state *ms) { - ms->alloc = (struct maple_node *)((unsigned long)ms->alloc & ~0x03); - ms->alloc = (struct maple_node *)((unsigned long)ms->alloc | count); + return (struct maple_node *)((unsigned long)ms->alloc & ~0x0F); } static inline int ma_get_alloc_cnt(const struct maple_state *ms) { - return (int)((unsigned long)ms->alloc & 3); + struct maple_node *node = ma_get_alloc(ms); + + if (!node) + return 0; + if (!node->slot[0]) + return 1; + if (!node->slot[1]) + return 2; + return 3; } static inline void ma_set_alloc_req(struct maple_state *ms, int count) { - ms->alloc = (struct maple_node *)((unsigned long)ms->alloc & ~0x0C); - ms->alloc = (struct maple_node *)((unsigned long)ms->alloc | - (count << 2)); + ms->alloc = (struct maple_node *)((unsigned long)ms->alloc & ~0x03); + ms->alloc = (struct maple_node *)((unsigned long)ms->alloc | count); } static inline int ma_get_alloc_req(const struct maple_state *ms) { - return (int)(((unsigned long)ms->alloc & 0x0C) >> 2); -} -static inline struct maple_node *ma_get_alloc(const struct maple_state *ms) -{ - return (struct maple_node *)((unsigned long)ms->alloc & ~0x0F); + return (int)(((unsigned long)ms->alloc & 0x03)); } static inline struct maple_node *ma_next_alloc(struct maple_state *ms) { @@ -102,7 +104,6 @@ static inline struct maple_node *ma_next_alloc(struct maple_state *ms) } else { smn = (struct maple_node*)mn->slot[cnt - 1]; mn->slot[cnt - 1] = NULL; - ma_set_alloc_cnt(ms, cnt); mn = smn; } @@ -150,18 +151,17 @@ static void maple_new_node(struct maple_state *ms, gfp_t gfp) slot = allocated - 1; while (req > 0) { - req--; smn = kzalloc(sizeof(*mn), gfp); if (!smn) goto slot_failed; smn->parent = NULL; mn->slot[slot++] = smn; + req--; allocated++; } slot_failed: ms->alloc = mn; - ma_set_alloc_cnt(ms, allocated); ma_set_alloc_req(ms, req); list_failed: