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>
11 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
14 * Enumerate bits using enum autoincrement. Define the @name as the n-th bit.
16 #define ENUM_BIT(name) \
18 name = (1U << __ ## name ## _BIT), \
19 __ ## name ## _SEQ = __ ## name ## _BIT
21 static inline void cond_wake_up(struct wait_queue_head *wq)
24 * This implies a full smp_mb barrier, see comments for
25 * waitqueue_active why.
27 if (wq_has_sleeper(wq))
31 static inline void cond_wake_up_nomb(struct wait_queue_head *wq)
34 * Special case for conditional wakeup where the barrier required for
35 * waitqueue_active is implied by some of the preceding code. Eg. one
36 * of such atomic operations (atomic_dec_and_return, ...), or a
37 * unlock/lock sequence, etc.
39 if (waitqueue_active(wq))
43 static inline u64 div_factor(u64 num, int factor)
48 return div_u64(num, 10);
51 static inline u64 div_factor_fine(u64 num, int factor)
56 return div_u64(num, 100);
59 /* Copy of is_power_of_two that is 64bit safe */
60 static inline bool is_power_of_two_u64(u64 n)
62 return n != 0 && (n & (n - 1)) == 0;
65 static inline bool has_single_bit_set(u64 n)
67 return is_power_of_two_u64(n);
71 * Simple bytenr based rb_tree relate structures
73 * Any structure wants to use bytenr as single search index should have their
74 * structure start with these members.
76 struct rb_simple_node {
77 struct rb_node rb_node;
81 static inline struct rb_node *rb_simple_search(struct rb_root *root, u64 bytenr)
83 struct rb_node *node = root->rb_node;
84 struct rb_simple_node *entry;
87 entry = rb_entry(node, struct rb_simple_node, rb_node);
89 if (bytenr < entry->bytenr)
91 else if (bytenr > entry->bytenr)
92 node = node->rb_right;
100 * Search @root from an entry that starts or comes after @bytenr.
102 * @root: the root to search.
103 * @bytenr: bytenr to search from.
105 * Return the rb_node that start at or after @bytenr. If there is no entry at
106 * or after @bytner return NULL.
108 static inline struct rb_node *rb_simple_search_first(struct rb_root *root,
111 struct rb_node *node = root->rb_node, *ret = NULL;
112 struct rb_simple_node *entry, *ret_entry = NULL;
115 entry = rb_entry(node, struct rb_simple_node, rb_node);
117 if (bytenr < entry->bytenr) {
118 if (!ret || entry->bytenr < ret_entry->bytenr) {
123 node = node->rb_left;
124 } else if (bytenr > entry->bytenr) {
125 node = node->rb_right;
134 static inline struct rb_node *rb_simple_insert(struct rb_root *root, u64 bytenr,
135 struct rb_node *node)
137 struct rb_node **p = &root->rb_node;
138 struct rb_node *parent = NULL;
139 struct rb_simple_node *entry;
143 entry = rb_entry(parent, struct rb_simple_node, rb_node);
145 if (bytenr < entry->bytenr)
147 else if (bytenr > entry->bytenr)
153 rb_link_node(node, parent, p);
154 rb_insert_color(node, root);