Wed Jul 24 03:16:32 2013 UTC ()
Work around rb_tree_iterate API botch in linux_idr.c.


(riastradh)
diff -r1.1.2.6 -r1.1.2.7 src/sys/external/bsd/drm2/linux/linux_idr.c

cvs diff -r1.1.2.6 -r1.1.2.7 src/sys/external/bsd/drm2/linux/linux_idr.c (switch to unified diff)

--- src/sys/external/bsd/drm2/linux/linux_idr.c 2013/07/24 03:16:15 1.1.2.6
+++ src/sys/external/bsd/drm2/linux/linux_idr.c 2013/07/24 03:16:32 1.1.2.7
@@ -1,239 +1,237 @@ @@ -1,239 +1,237 @@
1/* $NetBSD: linux_idr.c,v 1.1.2.6 2013/07/24 03:16:15 riastradh Exp $ */ 1/* $NetBSD: linux_idr.c,v 1.1.2.7 2013/07/24 03:16:32 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell. 8 * by Taylor R. Campbell.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: linux_idr.c,v 1.1.2.6 2013/07/24 03:16:15 riastradh Exp $"); 33__KERNEL_RCSID(0, "$NetBSD: linux_idr.c,v 1.1.2.7 2013/07/24 03:16:32 riastradh Exp $");
34 34
35#include <sys/param.h> 35#include <sys/param.h>
36#include <sys/atomic.h> 36#include <sys/atomic.h>
37#include <sys/kmem.h> 37#include <sys/kmem.h>
38#include <sys/rbtree.h> 38#include <sys/rbtree.h>
39 39
40#include <linux/err.h> 40#include <linux/err.h>
41#include <linux/idr.h> 41#include <linux/idr.h>
42 42
43struct idr_node { 43struct idr_node {
44 rb_node_t in_rb_node; 44 rb_node_t in_rb_node;
45 int in_index; 45 int in_index;
46 void *in_data; 46 void *in_data;
47}; 47};
48 48
49static signed int idr_tree_compare_nodes(void *, const void *, const void *); 49static signed int idr_tree_compare_nodes(void *, const void *, const void *);
50static signed int idr_tree_compare_key(void *, const void *, const void *); 50static signed int idr_tree_compare_key(void *, const void *, const void *);
51 51
52static const rb_tree_ops_t idr_rb_ops = { 52static const rb_tree_ops_t idr_rb_ops = {
53 .rbto_compare_nodes = &idr_tree_compare_nodes, 53 .rbto_compare_nodes = &idr_tree_compare_nodes,
54 .rbto_compare_key = &idr_tree_compare_key, 54 .rbto_compare_key = &idr_tree_compare_key,
55 .rbto_node_offset = offsetof(struct idr_node, in_rb_node), 55 .rbto_node_offset = offsetof(struct idr_node, in_rb_node),
56 .rbto_context = NULL, 56 .rbto_context = NULL,
57}; 57};
58 58
59static signed int 59static signed int
60idr_tree_compare_nodes(void *ctx __unused, const void *na, const void *nb) 60idr_tree_compare_nodes(void *ctx __unused, const void *na, const void *nb)
61{ 61{
62 const int a = ((const struct idr_node *)na)->in_index; 62 const int a = ((const struct idr_node *)na)->in_index;
63 const int b = ((const struct idr_node *)nb)->in_index; 63 const int b = ((const struct idr_node *)nb)->in_index;
64 64
65 if (a < b) 65 if (a < b)
66 return -1; 66 return -1;
67 else if (b < a) 67 else if (b < a)
68 return +1; 68 return +1;
69 else 69 else
70 return 0; 70 return 0;
71} 71}
72 72
73static signed int 73static signed int
74idr_tree_compare_key(void *ctx __unused, const void *n, const void *key) 74idr_tree_compare_key(void *ctx __unused, const void *n, const void *key)
75{ 75{
76 const int a = ((const struct idr_node *)n)->in_index; 76 const int a = ((const struct idr_node *)n)->in_index;
77 const int b = *(const int *)key; 77 const int b = *(const int *)key;
78 78
79 if (a < b) 79 if (a < b)
80 return -1; 80 return -1;
81 else if (b < a) 81 else if (b < a)
82 return +1; 82 return +1;
83 else 83 else
84 return 0; 84 return 0;
85} 85}
86 86
87void 87void
88idr_init(struct idr *idr) 88idr_init(struct idr *idr)
89{ 89{
90 90
91 rw_init(&idr->idr_lock); 91 rw_init(&idr->idr_lock);
92 rb_tree_init(&idr->idr_tree, &idr_rb_ops); 92 rb_tree_init(&idr->idr_tree, &idr_rb_ops);
93 idr->idr_temp = NULL; 93 idr->idr_temp = NULL;
94} 94}
95 95
96void 96void
97idr_destroy(struct idr *idr) 97idr_destroy(struct idr *idr)
98{ 98{
99 99
100 if (idr->idr_temp != NULL) { 100 if (idr->idr_temp != NULL) {
101 /* XXX Probably shouldn't happen. */ 101 /* XXX Probably shouldn't happen. */
102 kmem_free(idr->idr_temp, sizeof(*idr->idr_temp)); 102 kmem_free(idr->idr_temp, sizeof(*idr->idr_temp));
103 idr->idr_temp = NULL; 103 idr->idr_temp = NULL;
104 } 104 }
105#if 0 /* XXX No rb_tree_destroy? */ 105#if 0 /* XXX No rb_tree_destroy? */
106 rb_tree_destroy(&idr->idr_tree); 106 rb_tree_destroy(&idr->idr_tree);
107#endif 107#endif
108 rw_destroy(&idr->idr_lock); 108 rw_destroy(&idr->idr_lock);
109} 109}
110 110
111void * 111void *
112idr_find(struct idr *idr, int id) 112idr_find(struct idr *idr, int id)
113{ 113{
114 const struct idr_node *node; 114 const struct idr_node *node;
115 void *data; 115 void *data;
116 116
117 rw_enter(&idr->idr_lock, RW_READER); 117 rw_enter(&idr->idr_lock, RW_READER);
118 node = rb_tree_find_node(&idr->idr_tree, &id); 118 node = rb_tree_find_node(&idr->idr_tree, &id);
119 data = (node == NULL? NULL : node->in_data); 119 data = (node == NULL? NULL : node->in_data);
120 rw_exit(&idr->idr_lock); 120 rw_exit(&idr->idr_lock);
121 121
122 return data; 122 return data;
123} 123}
124 124
125void * 125void *
126idr_replace(struct idr *idr, void *replacement, int id) 126idr_replace(struct idr *idr, void *replacement, int id)
127{ 127{
128 struct idr_node *node; 128 struct idr_node *node;
129 void *result; 129 void *result;
130 130
131 rw_enter(&idr->idr_lock, RW_WRITER); 131 rw_enter(&idr->idr_lock, RW_WRITER);
132 node = rb_tree_find_node(&idr->idr_tree, &id); 132 node = rb_tree_find_node(&idr->idr_tree, &id);
133 if (node == NULL) { 133 if (node == NULL) {
134 result = ERR_PTR(-ENOENT); 134 result = ERR_PTR(-ENOENT);
135 } else { 135 } else {
136 result = node->in_data; 136 result = node->in_data;
137 node->in_data = replacement; 137 node->in_data = replacement;
138 } 138 }
139 rw_exit(&idr->idr_lock); 139 rw_exit(&idr->idr_lock);
140 140
141 return result; 141 return result;
142} 142}
143 143
144void 144void
145idr_remove(struct idr *idr, int id) 145idr_remove(struct idr *idr, int id)
146{ 146{
147 struct idr_node *node; 147 struct idr_node *node;
148 148
149 rw_enter(&idr->idr_lock, RW_WRITER); 149 rw_enter(&idr->idr_lock, RW_WRITER);
150 node = rb_tree_find_node(&idr->idr_tree, &id); 150 node = rb_tree_find_node(&idr->idr_tree, &id);
151 KASSERT(node != NULL); 151 KASSERT(node != NULL);
152 rb_tree_remove_node(&idr->idr_tree, node); 152 rb_tree_remove_node(&idr->idr_tree, node);
153 rw_exit(&idr->idr_lock); 153 rw_exit(&idr->idr_lock);
154 kmem_free(node, sizeof(*node)); 154 kmem_free(node, sizeof(*node));
155} 155}
156 156
157void 157void
158idr_remove_all(struct idr *idr) 158idr_remove_all(struct idr *idr)
159{ 159{
160 struct idr_node *node; 160 struct idr_node *node;
161 161
162 rw_enter(&idr->idr_lock, RW_WRITER); 162 rw_enter(&idr->idr_lock, RW_WRITER);
163 while ((node = rb_tree_iterate(&idr->idr_tree, NULL, RB_DIR_RIGHT)) != 163 while ((node = RB_TREE_MIN(&idr->idr_tree)) != NULL) {
164 NULL) { 
165 rb_tree_remove_node(&idr->idr_tree, node); 164 rb_tree_remove_node(&idr->idr_tree, node);
166 rw_exit(&idr->idr_lock); 165 rw_exit(&idr->idr_lock);
167 kmem_free(node, sizeof(*node)); 166 kmem_free(node, sizeof(*node));
168 rw_enter(&idr->idr_lock, RW_WRITER); 167 rw_enter(&idr->idr_lock, RW_WRITER);
169 } 168 }
170 rw_exit(&idr->idr_lock); 169 rw_exit(&idr->idr_lock);
171} 170}
172 171
173int 172int
174idr_pre_get(struct idr *idr, int flags __unused /* XXX */) 173idr_pre_get(struct idr *idr, int flags __unused /* XXX */)
175{ 174{
176 struct idr_node *temp = kmem_alloc(sizeof(*temp), KM_SLEEP); 175 struct idr_node *temp = kmem_alloc(sizeof(*temp), KM_SLEEP);
177 176
178 rw_enter(&idr->idr_lock, RW_WRITER); 177 rw_enter(&idr->idr_lock, RW_WRITER);
179 if (idr->idr_temp == NULL) { 178 if (idr->idr_temp == NULL) {
180 idr->idr_temp = temp; 179 idr->idr_temp = temp;
181 temp = NULL; 180 temp = NULL;
182 } 181 }
183 rw_exit(&idr->idr_lock); 182 rw_exit(&idr->idr_lock);
184 183
185 if (temp != NULL) 184 if (temp != NULL)
186 kmem_free(temp, sizeof(*temp)); 185 kmem_free(temp, sizeof(*temp));
187 186
188 return 1; 187 return 1;
189} 188}
190 189
191int 190int
192idr_get_new_above(struct idr *idr, void *data, int min_id, int *id) 191idr_get_new_above(struct idr *idr, void *data, int min_id, int *id)
193{ 192{
194 struct idr_node *node, *search, *collision __unused; 193 struct idr_node *node, *search, *collision __unused;
195 int want_id = min_id; 194 int want_id = min_id;
196 195
197 rw_enter(&idr->idr_lock, RW_WRITER); 196 rw_enter(&idr->idr_lock, RW_WRITER);
198 197
199 node = idr->idr_temp; 198 node = idr->idr_temp;
200 if (node == NULL) { 199 if (node == NULL) {
201 rw_exit(&idr->idr_lock); 200 rw_exit(&idr->idr_lock);
202 return -EAGAIN; 201 return -EAGAIN;
203 } 202 }
204 idr->idr_temp = NULL; 203 idr->idr_temp = NULL;
205 204
206 search = rb_tree_find_node_geq(&idr->idr_tree, &min_id); 205 search = rb_tree_find_node_geq(&idr->idr_tree, &min_id);
207 while ((search != NULL) && (search->in_index == want_id)) { 206 while ((search != NULL) && (search->in_index == want_id)) {
208 search = rb_tree_iterate(&idr->idr_tree, search, RB_DIR_RIGHT); 207 search = rb_tree_iterate(&idr->idr_tree, search, RB_DIR_RIGHT);
209 want_id++; 208 want_id++;
210 } 209 }
211 210
212 node->in_index = want_id; 211 node->in_index = want_id;
213 node->in_data = data; 212 node->in_data = data;
214 213
215 collision = rb_tree_insert_node(&idr->idr_tree, node); 214 collision = rb_tree_insert_node(&idr->idr_tree, node);
216 KASSERT(collision == node); 215 KASSERT(collision == node);
217 216
218 rw_exit(&idr->idr_lock); 217 rw_exit(&idr->idr_lock);
219 218
220 return 0; 219 return 0;
221} 220}
222 221
223int 222int
224idr_for_each(struct idr *idr, int (*proc)(int, void *, void *), void *arg) 223idr_for_each(struct idr *idr, int (*proc)(int, void *, void *), void *arg)
225{ 224{
226 struct idr_node *node = NULL; 225 struct idr_node *node;
227 int error = 0; 226 int error = 0;
228 227
229 rw_enter(&idr->idr_lock, RW_READER); 228 rw_enter(&idr->idr_lock, RW_READER);
230 while ((node = rb_tree_iterate(&idr->idr_tree, node, RB_DIR_RIGHT)) 229 RB_TREE_FOREACH(node, &idr->idr_tree) {
231 != NULL) { 
232 error = (*proc)(node->in_index, node->in_data, arg); 230 error = (*proc)(node->in_index, node->in_data, arg);
233 if (error) 231 if (error)
234 break; 232 break;
235 } 233 }
236 rw_exit(&idr->idr_lock); 234 rw_exit(&idr->idr_lock);
237 235
238 return error; 236 return error;
239} 237}