Wed Jul 24 03:16:16 2013 UTC ()
Tweak idr_pre_get.

. No need to test kmem_alloc(n, KM_SLEEP) for NULL.
. Avoid kmem_free with lock held, out of paranoia.


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

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

--- src/sys/external/bsd/drm2/linux/linux_idr.c 2013/07/24 02:51:35 1.1.2.5
+++ src/sys/external/bsd/drm2/linux/linux_idr.c 2013/07/24 03:16:15 1.1.2.6
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: linux_idr.c,v 1.1.2.5 2013/07/24 02:51:35 riastradh Exp $ */ 1/* $NetBSD: linux_idr.c,v 1.1.2.6 2013/07/24 03:16:15 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.
@@ -20,27 +20,27 @@ @@ -20,27 +20,27 @@
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.5 2013/07/24 02:51:35 riastradh Exp $"); 33__KERNEL_RCSID(0, "$NetBSD: linux_idr.c,v 1.1.2.6 2013/07/24 03:16:15 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;
@@ -163,38 +163,38 @@ idr_remove_all(struct idr *idr) @@ -163,38 +163,38 @@ idr_remove_all(struct idr *idr)
163 while ((node = rb_tree_iterate(&idr->idr_tree, NULL, RB_DIR_RIGHT)) != 163 while ((node = rb_tree_iterate(&idr->idr_tree, NULL, RB_DIR_RIGHT)) !=
164 NULL) { 164 NULL) {
165 rb_tree_remove_node(&idr->idr_tree, node); 165 rb_tree_remove_node(&idr->idr_tree, node);
166 rw_exit(&idr->idr_lock); 166 rw_exit(&idr->idr_lock);
167 kmem_free(node, sizeof(*node)); 167 kmem_free(node, sizeof(*node));
168 rw_enter(&idr->idr_lock, RW_WRITER); 168 rw_enter(&idr->idr_lock, RW_WRITER);
169 } 169 }
170 rw_exit(&idr->idr_lock); 170 rw_exit(&idr->idr_lock);
171} 171}
172 172
173int 173int
174idr_pre_get(struct idr *idr, int flags __unused /* XXX */) 174idr_pre_get(struct idr *idr, int flags __unused /* XXX */)
175{ 175{
176 struct idr_node *const temp = kmem_alloc(sizeof(*temp), KM_SLEEP); 176 struct idr_node *temp = kmem_alloc(sizeof(*temp), KM_SLEEP);
177 
178 if (temp == NULL) 
179 return 0; 
180 177
181 rw_enter(&idr->idr_lock, RW_WRITER); 178 rw_enter(&idr->idr_lock, RW_WRITER);
182 if (idr->idr_temp == NULL) 179 if (idr->idr_temp == NULL) {
183 idr->idr_temp = temp; 180 idr->idr_temp = temp;
184 else 181 temp = NULL;
185 kmem_free(temp, sizeof(*temp)); 182 }
186 rw_exit(&idr->idr_lock); 183 rw_exit(&idr->idr_lock);
187 184
 185 if (temp != NULL)
 186 kmem_free(temp, sizeof(*temp));
 187
188 return 1; 188 return 1;
189} 189}
190 190
191int 191int
192idr_get_new_above(struct idr *idr, void *data, int min_id, int *id) 192idr_get_new_above(struct idr *idr, void *data, int min_id, int *id)
193{ 193{
194 struct idr_node *node, *search, *collision __unused; 194 struct idr_node *node, *search, *collision __unused;
195 int want_id = min_id; 195 int want_id = min_id;
196 196
197 rw_enter(&idr->idr_lock, RW_WRITER); 197 rw_enter(&idr->idr_lock, RW_WRITER);
198 198
199 node = idr->idr_temp; 199 node = idr->idr_temp;
200 if (node == NULL) { 200 if (node == NULL) {