1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/sched.h>
7 #include <linux/wait.h>
8 #include <linux/math64.h>
9 #include <linux/rbtree.h>
12 * Enumerate bits using enum autoincrement. Define the @name as the n-th bit.
14 #define ENUM_BIT(name) \
16 name = (1U << __ ## name ## _BIT), \
17 __ ## name ## _SEQ = __ ## name ## _BIT
19 static inline void cond_wake_up(struct wait_queue_head *wq)
22 * This implies a full smp_mb barrier, see comments for
23 * waitqueue_active why.
25 if (wq_has_sleeper(wq))
29 static inline void cond_wake_up_nomb(struct wait_queue_head *wq)
32 * Special case for conditional wakeup where the barrier required for
33 * waitqueue_active is implied by some of the preceding code. Eg. one
34 * of such atomic operations (atomic_dec_and_return, ...), or a
35 * unlock/lock sequence, etc.
37 if (waitqueue_active(wq))
41 static inline u64 mult_perc(u64 num, u32 percent)
43 return div_u64(num * percent, 100);
45 /* Copy of is_power_of_two that is 64bit safe */
46 static inline bool is_power_of_two_u64(u64 n)
48 return n != 0 && (n & (n - 1)) == 0;
51 static inline bool has_single_bit_set(u64 n)
53 return is_power_of_two_u64(n);
57 * Simple bytenr based rb_tree relate structures
59 * Any structure wants to use bytenr as single search index should have their
60 * structure start with these members.
62 struct rb_simple_node {
63 struct rb_node rb_node;
67 static inline struct rb_node *rb_simple_search(struct rb_root *root, u64 bytenr)
69 struct rb_node *node = root->rb_node;
70 struct rb_simple_node *entry;
73 entry = rb_entry(node, struct rb_simple_node, rb_node);
75 if (bytenr < entry->bytenr)
77 else if (bytenr > entry->bytenr)
78 node = node->rb_right;
86 * Search @root from an entry that starts or comes after @bytenr.
88 * @root: the root to search.
89 * @bytenr: bytenr to search from.
91 * Return the rb_node that start at or after @bytenr. If there is no entry at
92 * or after @bytner return NULL.
94 static inline struct rb_node *rb_simple_search_first(struct rb_root *root,
97 struct rb_node *node = root->rb_node, *ret = NULL;
98 struct rb_simple_node *entry, *ret_entry = NULL;
101 entry = rb_entry(node, struct rb_simple_node, rb_node);
103 if (bytenr < entry->bytenr) {
104 if (!ret || entry->bytenr < ret_entry->bytenr) {
109 node = node->rb_left;
110 } else if (bytenr > entry->bytenr) {
111 node = node->rb_right;
120 static inline struct rb_node *rb_simple_insert(struct rb_root *root, u64 bytenr,
121 struct rb_node *node)
123 struct rb_node **p = &root->rb_node;
124 struct rb_node *parent = NULL;
125 struct rb_simple_node *entry;
129 entry = rb_entry(parent, struct rb_simple_node, rb_node);
131 if (bytenr < entry->bytenr)
133 else if (bytenr > entry->bytenr)
139 rb_link_node(node, parent, p);
140 rb_insert_color(node, root);
144 static inline bool bitmap_test_range_all_set(const unsigned long *addr,
148 unsigned long found_zero;
150 found_zero = find_next_zero_bit(addr, start + nbits, start);
151 return (found_zero == start + nbits);
154 static inline bool bitmap_test_range_all_zero(const unsigned long *addr,
158 unsigned long found_set;
160 found_set = find_next_bit(addr, start + nbits, start);
161 return (found_set == start + nbits);