Sat Jul 30 06:19:02 2011 UTC ()
Correct sizes to pass uvm_km_free(9) in error paths.


(uebayasi)
diff -r1.61 -r1.62 src/sys/kern/sysv_msg.c
diff -r1.87 -r1.88 src/sys/kern/sysv_sem.c
diff -r1.120 -r1.121 src/sys/kern/sysv_shm.c

cvs diff -r1.61 -r1.62 src/sys/kern/sysv_msg.c (expand / switch to unified diff)

--- src/sys/kern/sysv_msg.c 2009/01/28 00:59:03 1.61
+++ src/sys/kern/sysv_msg.c 2011/07/30 06:19:02 1.62
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: sysv_msg.c,v 1.61 2009/01/28 00:59:03 njoly Exp $ */ 1/* $NetBSD: sysv_msg.c,v 1.62 2011/07/30 06:19:02 uebayasi Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1999, 2006, 2007 The NetBSD Foundation, Inc. 4 * Copyright (c) 1999, 2006, 2007 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 Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran. 9 * NASA Ames Research Center, and by Andrew Doran.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -40,27 +40,27 @@ @@ -40,27 +40,27 @@
40 * This system call was implemented by Daniel Boulet under contract from RTMX. 40 * This system call was implemented by Daniel Boulet under contract from RTMX.
41 * 41 *
42 * Redistribution and use in source forms, with and without modification, 42 * Redistribution and use in source forms, with and without modification,
43 * are permitted provided that this entire comment appears intact. 43 * are permitted provided that this entire comment appears intact.
44 * 44 *
45 * Redistribution in binary form may occur without any restrictions. 45 * Redistribution in binary form may occur without any restrictions.
46 * Obviously, it would be nice if you gave credit where credit is due 46 * Obviously, it would be nice if you gave credit where credit is due
47 * but requiring it would be too onerous. 47 * but requiring it would be too onerous.
48 * 48 *
49 * This software is provided ``AS IS'' without any warranties of any kind. 49 * This software is provided ``AS IS'' without any warranties of any kind.
50 */ 50 */
51 51
52#include <sys/cdefs.h> 52#include <sys/cdefs.h>
53__KERNEL_RCSID(0, "$NetBSD: sysv_msg.c,v 1.61 2009/01/28 00:59:03 njoly Exp $"); 53__KERNEL_RCSID(0, "$NetBSD: sysv_msg.c,v 1.62 2011/07/30 06:19:02 uebayasi Exp $");
54 54
55#define SYSVMSG 55#define SYSVMSG
56 56
57#include <sys/param.h> 57#include <sys/param.h>
58#include <sys/kernel.h> 58#include <sys/kernel.h>
59#include <sys/msg.h> 59#include <sys/msg.h>
60#include <sys/sysctl.h> 60#include <sys/sysctl.h>
61#include <sys/mount.h> /* XXX for <sys/syscallargs.h> */ 61#include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
62#include <sys/syscallargs.h> 62#include <sys/syscallargs.h>
63#include <sys/kauth.h> 63#include <sys/kauth.h>
64 64
65#define MSG_DEBUG 65#define MSG_DEBUG
66#undef MSG_DEBUG_OK 66#undef MSG_DEBUG_OK
@@ -106,28 +106,28 @@ msginit(void) @@ -106,28 +106,28 @@ msginit(void)
106 panic("msginfo.msgssz = %d, not a small power of 2", 106 panic("msginfo.msgssz = %d, not a small power of 2",
107 msginfo.msgssz); 107 msginfo.msgssz);
108 } 108 }
109 109
110 if (msginfo.msgseg > 32767) { 110 if (msginfo.msgseg > 32767) {
111 panic("msginfo.msgseg = %d > 32767", msginfo.msgseg); 111 panic("msginfo.msgseg = %d > 32767", msginfo.msgseg);
112 } 112 }
113 113
114 /* Allocate the wired memory for our structures */ 114 /* Allocate the wired memory for our structures */
115 sz = ALIGN(msginfo.msgmax) + 115 sz = ALIGN(msginfo.msgmax) +
116 ALIGN(msginfo.msgseg * sizeof(struct msgmap)) + 116 ALIGN(msginfo.msgseg * sizeof(struct msgmap)) +
117 ALIGN(msginfo.msgtql * sizeof(struct __msg)) + 117 ALIGN(msginfo.msgtql * sizeof(struct __msg)) +
118 ALIGN(msginfo.msgmni * sizeof(kmsq_t)); 118 ALIGN(msginfo.msgmni * sizeof(kmsq_t));
119 v = uvm_km_alloc(kernel_map, round_page(sz), 0, 119 sz = round_page(sz);
120 UVM_KMF_WIRED|UVM_KMF_ZERO); 120 v = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
121 if (v == 0) 121 if (v == 0)
122 panic("sysv_msg: cannot allocate memory"); 122 panic("sysv_msg: cannot allocate memory");
123 msgpool = (void *)v; 123 msgpool = (void *)v;
124 msgmaps = (void *)((uintptr_t)msgpool + ALIGN(msginfo.msgmax)); 124 msgmaps = (void *)((uintptr_t)msgpool + ALIGN(msginfo.msgmax));
125 msghdrs = (void *)((uintptr_t)msgmaps + 125 msghdrs = (void *)((uintptr_t)msgmaps +
126 ALIGN(msginfo.msgseg * sizeof(struct msgmap))); 126 ALIGN(msginfo.msgseg * sizeof(struct msgmap)));
127 msqs = (void *)((uintptr_t)msghdrs + 127 msqs = (void *)((uintptr_t)msghdrs +
128 ALIGN(msginfo.msgtql * sizeof(struct __msg))); 128 ALIGN(msginfo.msgtql * sizeof(struct __msg)));
129 129
130 for (i = 0; i < (msginfo.msgseg - 1); i++) 130 for (i = 0; i < (msginfo.msgseg - 1); i++)
131 msgmaps[i].next = i + 1; 131 msgmaps[i].next = i + 1;
132 msgmaps[msginfo.msgseg - 1].next = -1; 132 msgmaps[msginfo.msgseg - 1].next = -1;
133 133
@@ -166,28 +166,28 @@ msgrealloc(int newmsgmni, int newmsgseg) @@ -166,28 +166,28 @@ msgrealloc(int newmsgmni, int newmsgseg)
166 vaddr_t v; 166 vaddr_t v;
167 int i, sz, msqid, newmsgmax, new_nfree_msgmaps; 167 int i, sz, msqid, newmsgmax, new_nfree_msgmaps;
168 short new_free_msgmaps; 168 short new_free_msgmaps;
169 169
170 if (newmsgmni < 1 || newmsgseg < 1) 170 if (newmsgmni < 1 || newmsgseg < 1)
171 return EINVAL; 171 return EINVAL;
172 172
173 /* Allocate the wired memory for our structures */ 173 /* Allocate the wired memory for our structures */
174 newmsgmax = msginfo.msgssz * newmsgseg; 174 newmsgmax = msginfo.msgssz * newmsgseg;
175 sz = ALIGN(newmsgmax) + 175 sz = ALIGN(newmsgmax) +
176 ALIGN(newmsgseg * sizeof(struct msgmap)) + 176 ALIGN(newmsgseg * sizeof(struct msgmap)) +
177 ALIGN(msginfo.msgtql * sizeof(struct __msg)) + 177 ALIGN(msginfo.msgtql * sizeof(struct __msg)) +
178 ALIGN(newmsgmni * sizeof(kmsq_t)); 178 ALIGN(newmsgmni * sizeof(kmsq_t));
179 v = uvm_km_alloc(kernel_map, round_page(sz), 0, 179 sz = round_page(sz);
180 UVM_KMF_WIRED|UVM_KMF_ZERO); 180 v = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
181 if (v == 0) 181 if (v == 0)
182 return ENOMEM; 182 return ENOMEM;
183 183
184 mutex_enter(&msgmutex); 184 mutex_enter(&msgmutex);
185 if (msg_realloc_state) { 185 if (msg_realloc_state) {
186 mutex_exit(&msgmutex); 186 mutex_exit(&msgmutex);
187 uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED); 187 uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
188 return EBUSY; 188 return EBUSY;
189 } 189 }
190 msg_realloc_state = true; 190 msg_realloc_state = true;
191 if (msg_waiters) { 191 if (msg_waiters) {
192 /* 192 /*
193 * Mark reallocation state, wake-up all waiters, 193 * Mark reallocation state, wake-up all waiters,
@@ -329,26 +329,27 @@ msgrealloc(int newmsgmni, int newmsgseg) @@ -329,26 +329,27 @@ msgrealloc(int newmsgmni, int newmsgseg)
329 329
330 /* Next message header */ 330 /* Next message header */
331 msghdr = msghdr->msg_next; 331 msghdr = msghdr->msg_next;
332 } 332 }
333 nmptr->_msg_last = nmsghdr; 333 nmptr->_msg_last = nmsghdr;
334 } 334 }
335 KASSERT((msginfo.msgseg - nfree_msgmaps) == 335 KASSERT((msginfo.msgseg - nfree_msgmaps) ==
336 (newmsgseg - new_nfree_msgmaps)); 336 (newmsgseg - new_nfree_msgmaps));
337 337
338 sz = ALIGN(msginfo.msgmax) + 338 sz = ALIGN(msginfo.msgmax) +
339 ALIGN(msginfo.msgseg * sizeof(struct msgmap)) + 339 ALIGN(msginfo.msgseg * sizeof(struct msgmap)) +
340 ALIGN(msginfo.msgtql * sizeof(struct __msg)) + 340 ALIGN(msginfo.msgtql * sizeof(struct __msg)) +
341 ALIGN(msginfo.msgmni * sizeof(kmsq_t)); 341 ALIGN(msginfo.msgmni * sizeof(kmsq_t));
 342 sz = round_page(sz);
342 343
343 for (i = 0; i < msginfo.msgmni; i++) 344 for (i = 0; i < msginfo.msgmni; i++)
344 cv_destroy(&msqs[i].msq_cv); 345 cv_destroy(&msqs[i].msq_cv);
345 346
346 /* Set the pointers and update the new values */ 347 /* Set the pointers and update the new values */
347 msgpool = new_msgpool; 348 msgpool = new_msgpool;
348 msgmaps = new_msgmaps; 349 msgmaps = new_msgmaps;
349 msghdrs = new_msghdrs; 350 msghdrs = new_msghdrs;
350 msqs = new_msqs; 351 msqs = new_msqs;
351 352
352 free_msghdrs = new_free_msghdrs; 353 free_msghdrs = new_free_msghdrs;
353 free_msgmaps = new_free_msgmaps; 354 free_msgmaps = new_free_msgmaps;
354 nfree_msgmaps = new_nfree_msgmaps; 355 nfree_msgmaps = new_nfree_msgmaps;

cvs diff -r1.87 -r1.88 src/sys/kern/sysv_sem.c (expand / switch to unified diff)

--- src/sys/kern/sysv_sem.c 2011/05/13 22:16:44 1.87
+++ src/sys/kern/sysv_sem.c 2011/07/30 06:19:02 1.88
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: sysv_sem.c,v 1.87 2011/05/13 22:16:44 rmind Exp $ */ 1/* $NetBSD: sysv_sem.c,v 1.88 2011/07/30 06:19:02 uebayasi Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc. 4 * Copyright (c) 1999, 2007 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 Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran. 9 * NASA Ames Research Center, and by Andrew Doran.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -29,27 +29,27 @@ @@ -29,27 +29,27 @@
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33/* 33/*
34 * Implementation of SVID semaphores 34 * Implementation of SVID semaphores
35 * 35 *
36 * Author: Daniel Boulet 36 * Author: Daniel Boulet
37 * 37 *
38 * This software is provided ``AS IS'' without any warranties of any kind. 38 * This software is provided ``AS IS'' without any warranties of any kind.
39 */ 39 */
40 40
41#include <sys/cdefs.h> 41#include <sys/cdefs.h>
42__KERNEL_RCSID(0, "$NetBSD: sysv_sem.c,v 1.87 2011/05/13 22:16:44 rmind Exp $"); 42__KERNEL_RCSID(0, "$NetBSD: sysv_sem.c,v 1.88 2011/07/30 06:19:02 uebayasi Exp $");
43 43
44#define SYSVSEM 44#define SYSVSEM
45 45
46#include <sys/param.h> 46#include <sys/param.h>
47#include <sys/kernel.h> 47#include <sys/kernel.h>
48#include <sys/sem.h> 48#include <sys/sem.h>
49#include <sys/sysctl.h> 49#include <sys/sysctl.h>
50#include <sys/kmem.h> 50#include <sys/kmem.h>
51#include <sys/mount.h> /* XXX for <sys/syscallargs.h> */ 51#include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
52#include <sys/syscallargs.h> 52#include <sys/syscallargs.h>
53#include <sys/kauth.h> 53#include <sys/kauth.h>
54 54
55/*  55/*
@@ -96,28 +96,28 @@ seminit(void) @@ -96,28 +96,28 @@ seminit(void)
96 vaddr_t v; 96 vaddr_t v;
97 97
98 mutex_init(&semlock, MUTEX_DEFAULT, IPL_NONE); 98 mutex_init(&semlock, MUTEX_DEFAULT, IPL_NONE);
99 cv_init(&sem_realloc_cv, "semrealc"); 99 cv_init(&sem_realloc_cv, "semrealc");
100 sem_realloc_state = false; 100 sem_realloc_state = false;
101 semtot = 0; 101 semtot = 0;
102 sem_waiters = 0; 102 sem_waiters = 0;
103 103
104 /* Allocate the wired memory for our structures */ 104 /* Allocate the wired memory for our structures */
105 sz = ALIGN(seminfo.semmni * sizeof(struct semid_ds)) + 105 sz = ALIGN(seminfo.semmni * sizeof(struct semid_ds)) +
106 ALIGN(seminfo.semmns * sizeof(struct __sem)) + 106 ALIGN(seminfo.semmns * sizeof(struct __sem)) +
107 ALIGN(seminfo.semmni * sizeof(kcondvar_t)) + 107 ALIGN(seminfo.semmni * sizeof(kcondvar_t)) +
108 ALIGN(seminfo.semmnu * seminfo.semusz); 108 ALIGN(seminfo.semmnu * seminfo.semusz);
109 v = uvm_km_alloc(kernel_map, round_page(sz), 0, 109 sz = round_page(sz);
110 UVM_KMF_WIRED|UVM_KMF_ZERO); 110 v = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
111 if (v == 0) 111 if (v == 0)
112 panic("sysv_sem: cannot allocate memory"); 112 panic("sysv_sem: cannot allocate memory");
113 sema = (void *)v; 113 sema = (void *)v;
114 sem = (void *)((uintptr_t)sema + 114 sem = (void *)((uintptr_t)sema +
115 ALIGN(seminfo.semmni * sizeof(struct semid_ds))); 115 ALIGN(seminfo.semmni * sizeof(struct semid_ds)));
116 semcv = (void *)((uintptr_t)sem + 116 semcv = (void *)((uintptr_t)sem +
117 ALIGN(seminfo.semmns * sizeof(struct __sem))); 117 ALIGN(seminfo.semmns * sizeof(struct __sem)));
118 semu = (void *)((uintptr_t)semcv + 118 semu = (void *)((uintptr_t)semcv +
119 ALIGN(seminfo.semmni * sizeof(kcondvar_t))); 119 ALIGN(seminfo.semmni * sizeof(kcondvar_t)));
120 120
121 for (i = 0; i < seminfo.semmni; i++) { 121 for (i = 0; i < seminfo.semmni; i++) {
122 sema[i]._sem_base = 0; 122 sema[i]._sem_base = 0;
123 sema[i].sem_perm.mode = 0; 123 sema[i].sem_perm.mode = 0;
@@ -140,28 +140,28 @@ semrealloc(int newsemmni, int newsemmns, @@ -140,28 +140,28 @@ semrealloc(int newsemmni, int newsemmns,
140 int *new_semu; 140 int *new_semu;
141 kcondvar_t *new_semcv; 141 kcondvar_t *new_semcv;
142 vaddr_t v; 142 vaddr_t v;
143 int i, j, lsemid, nmnus, sz; 143 int i, j, lsemid, nmnus, sz;
144 144
145 if (newsemmni < 1 || newsemmns < 1 || newsemmnu < 1) 145 if (newsemmni < 1 || newsemmns < 1 || newsemmnu < 1)
146 return EINVAL; 146 return EINVAL;
147 147
148 /* Allocate the wired memory for our structures */ 148 /* Allocate the wired memory for our structures */
149 sz = ALIGN(newsemmni * sizeof(struct semid_ds)) + 149 sz = ALIGN(newsemmni * sizeof(struct semid_ds)) +
150 ALIGN(newsemmns * sizeof(struct __sem)) + 150 ALIGN(newsemmns * sizeof(struct __sem)) +
151 ALIGN(newsemmni * sizeof(kcondvar_t)) + 151 ALIGN(newsemmni * sizeof(kcondvar_t)) +
152 ALIGN(newsemmnu * seminfo.semusz); 152 ALIGN(newsemmnu * seminfo.semusz);
153 v = uvm_km_alloc(kernel_map, round_page(sz), 0, 153 sz = round_page(sz);
154 UVM_KMF_WIRED|UVM_KMF_ZERO); 154 v = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
155 if (v == 0) 155 if (v == 0)
156 return ENOMEM; 156 return ENOMEM;
157 157
158 mutex_enter(&semlock); 158 mutex_enter(&semlock);
159 if (sem_realloc_state) { 159 if (sem_realloc_state) {
160 mutex_exit(&semlock); 160 mutex_exit(&semlock);
161 uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED); 161 uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
162 return EBUSY; 162 return EBUSY;
163 } 163 }
164 sem_realloc_state = true; 164 sem_realloc_state = true;
165 if (sem_waiters) { 165 if (sem_waiters) {
166 /* 166 /*
167 * Mark reallocation state, wake-up all waiters, 167 * Mark reallocation state, wake-up all waiters,
@@ -241,26 +241,27 @@ semrealloc(int newsemmni, int newsemmns, @@ -241,26 +241,27 @@ semrealloc(int newsemmni, int newsemmns,
241 new_semu_list = nsuptr; 241 new_semu_list = nsuptr;
242 j++; 242 j++;
243 } 243 }
244 244
245 for (i = 0; i < seminfo.semmni; i++) { 245 for (i = 0; i < seminfo.semmni; i++) {
246 KASSERT(cv_has_waiters(&semcv[i]) == false); 246 KASSERT(cv_has_waiters(&semcv[i]) == false);
247 cv_destroy(&semcv[i]); 247 cv_destroy(&semcv[i]);
248 } 248 }
249 249
250 sz = ALIGN(seminfo.semmni * sizeof(struct semid_ds)) + 250 sz = ALIGN(seminfo.semmni * sizeof(struct semid_ds)) +
251 ALIGN(seminfo.semmns * sizeof(struct __sem)) + 251 ALIGN(seminfo.semmns * sizeof(struct __sem)) +
252 ALIGN(seminfo.semmni * sizeof(kcondvar_t)) + 252 ALIGN(seminfo.semmni * sizeof(kcondvar_t)) +
253 ALIGN(seminfo.semmnu * seminfo.semusz); 253 ALIGN(seminfo.semmnu * seminfo.semusz);
 254 sz = round_page(sz);
254 255
255 /* Set the pointers and update the new values */ 256 /* Set the pointers and update the new values */
256 sema = new_sema; 257 sema = new_sema;
257 sem = new_sem; 258 sem = new_sem;
258 semcv = new_semcv; 259 semcv = new_semcv;
259 semu = new_semu; 260 semu = new_semu;
260 semu_list = new_semu_list; 261 semu_list = new_semu_list;
261 262
262 seminfo.semmni = newsemmni; 263 seminfo.semmni = newsemmni;
263 seminfo.semmns = newsemmns; 264 seminfo.semmns = newsemmns;
264 seminfo.semmnu = newsemmnu; 265 seminfo.semmnu = newsemmnu;
265 266
266 /* Reallocation completed - notify all waiters, if any */ 267 /* Reallocation completed - notify all waiters, if any */

cvs diff -r1.120 -r1.121 src/sys/kern/sysv_shm.c (expand / switch to unified diff)

--- src/sys/kern/sysv_shm.c 2011/06/12 03:35:56 1.120
+++ src/sys/kern/sysv_shm.c 2011/07/30 06:19:02 1.121
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: sysv_shm.c,v 1.120 2011/06/12 03:35:56 rmind Exp $ */ 1/* $NetBSD: sysv_shm.c,v 1.121 2011/07/30 06:19:02 uebayasi Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc. 4 * Copyright (c) 1999, 2007 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 Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Mindaugas Rasiukevicius. 9 * NASA Ames Research Center, and by Mindaugas Rasiukevicius.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -51,27 +51,27 @@ @@ -51,27 +51,27 @@
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 54 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 */ 61 */
62 62
63#include <sys/cdefs.h> 63#include <sys/cdefs.h>
64__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.120 2011/06/12 03:35:56 rmind Exp $"); 64__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.121 2011/07/30 06:19:02 uebayasi Exp $");
65 65
66#define SYSVSHM 66#define SYSVSHM
67 67
68#include <sys/param.h> 68#include <sys/param.h>
69#include <sys/kernel.h> 69#include <sys/kernel.h>
70#include <sys/kmem.h> 70#include <sys/kmem.h>
71#include <sys/shm.h> 71#include <sys/shm.h>
72#include <sys/mutex.h> 72#include <sys/mutex.h>
73#include <sys/mman.h> 73#include <sys/mman.h>
74#include <sys/stat.h> 74#include <sys/stat.h>
75#include <sys/sysctl.h> 75#include <sys/sysctl.h>
76#include <sys/mount.h> /* XXX for <sys/syscallargs.h> */ 76#include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
77#include <sys/syscallargs.h> 77#include <sys/syscallargs.h>
@@ -867,28 +867,28 @@ shmrealloc(int newshmni) @@ -867,28 +867,28 @@ shmrealloc(int newshmni)
867{ 867{
868 vaddr_t v; 868 vaddr_t v;
869 struct shmid_ds *oldshmsegs, *newshmsegs; 869 struct shmid_ds *oldshmsegs, *newshmsegs;
870 kcondvar_t *newshm_cv, *oldshm_cv; 870 kcondvar_t *newshm_cv, *oldshm_cv;
871 size_t sz; 871 size_t sz;
872 int i, lsegid, oldshmni; 872 int i, lsegid, oldshmni;
873 873
874 if (newshmni < 1) 874 if (newshmni < 1)
875 return EINVAL; 875 return EINVAL;
876 876
877 /* Allocate new memory area */ 877 /* Allocate new memory area */
878 sz = ALIGN(newshmni * sizeof(struct shmid_ds)) + 878 sz = ALIGN(newshmni * sizeof(struct shmid_ds)) +
879 ALIGN(newshmni * sizeof(kcondvar_t)); 879 ALIGN(newshmni * sizeof(kcondvar_t));
880 v = uvm_km_alloc(kernel_map, round_page(sz), 0, 880 sz = round_page(sz);
881 UVM_KMF_WIRED|UVM_KMF_ZERO); 881 v = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
882 if (v == 0) 882 if (v == 0)
883 return ENOMEM; 883 return ENOMEM;
884 884
885 mutex_enter(&shm_lock); 885 mutex_enter(&shm_lock);
886 while (shm_realloc_state || shm_realloc_disable) 886 while (shm_realloc_state || shm_realloc_disable)
887 cv_wait(&shm_realloc_cv, &shm_lock); 887 cv_wait(&shm_realloc_cv, &shm_lock);
888 888
889 /* 889 /*
890 * Get the number of last segment. Fail we are trying to 890 * Get the number of last segment. Fail we are trying to
891 * reallocate less memory than we use. 891 * reallocate less memory than we use.
892 */ 892 */
893 lsegid = 0; 893 lsegid = 0;
894 for (i = 0; i < shminfo.shmmni; i++) 894 for (i = 0; i < shminfo.shmmni; i++)
@@ -926,46 +926,47 @@ shmrealloc(int newshmni) @@ -926,46 +926,47 @@ shmrealloc(int newshmni)
926 /* Reallocation completed - notify all waiters, if any */ 926 /* Reallocation completed - notify all waiters, if any */
927 shm_realloc_state = false; 927 shm_realloc_state = false;
928 cv_broadcast(&shm_realloc_cv); 928 cv_broadcast(&shm_realloc_cv);
929 mutex_exit(&shm_lock); 929 mutex_exit(&shm_lock);
930 930
931 /* Release now unused resources. */ 931 /* Release now unused resources. */
932 oldshm_cv = (void *)((uintptr_t)oldshmsegs + 932 oldshm_cv = (void *)((uintptr_t)oldshmsegs +
933 ALIGN(oldshmni * sizeof(struct shmid_ds))); 933 ALIGN(oldshmni * sizeof(struct shmid_ds)));
934 for (i = 0; i < oldshmni; i++) 934 for (i = 0; i < oldshmni; i++)
935 cv_destroy(&oldshm_cv[i]); 935 cv_destroy(&oldshm_cv[i]);
936 936
937 sz = ALIGN(oldshmni * sizeof(struct shmid_ds)) + 937 sz = ALIGN(oldshmni * sizeof(struct shmid_ds)) +
938 ALIGN(oldshmni * sizeof(kcondvar_t)); 938 ALIGN(oldshmni * sizeof(kcondvar_t));
 939 sz = round_page(sz);
939 uvm_km_free(kernel_map, (vaddr_t)oldshmsegs, sz, UVM_KMF_WIRED); 940 uvm_km_free(kernel_map, (vaddr_t)oldshmsegs, sz, UVM_KMF_WIRED);
940 941
941 return 0; 942 return 0;
942} 943}
943 944
944void 945void
945shminit(void) 946shminit(void)
946{ 947{
947 vaddr_t v; 948 vaddr_t v;
948 size_t sz; 949 size_t sz;
949 int i; 950 int i;
950 951
951 mutex_init(&shm_lock, MUTEX_DEFAULT, IPL_NONE); 952 mutex_init(&shm_lock, MUTEX_DEFAULT, IPL_NONE);
952 cv_init(&shm_realloc_cv, "shmrealc"); 953 cv_init(&shm_realloc_cv, "shmrealc");
953 954
954 /* Allocate the wired memory for our structures */ 955 /* Allocate the wired memory for our structures */
955 sz = ALIGN(shminfo.shmmni * sizeof(struct shmid_ds)) + 956 sz = ALIGN(shminfo.shmmni * sizeof(struct shmid_ds)) +
956 ALIGN(shminfo.shmmni * sizeof(kcondvar_t)); 957 ALIGN(shminfo.shmmni * sizeof(kcondvar_t));
957 v = uvm_km_alloc(kernel_map, round_page(sz), 0, 958 sz = round_page(sz);
958 UVM_KMF_WIRED|UVM_KMF_ZERO); 959 v = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
959 if (v == 0) 960 if (v == 0)
960 panic("sysv_shm: cannot allocate memory"); 961 panic("sysv_shm: cannot allocate memory");
961 shmsegs = (void *)v; 962 shmsegs = (void *)v;
962 shm_cv = (void *)((uintptr_t)shmsegs + 963 shm_cv = (void *)((uintptr_t)shmsegs +
963 ALIGN(shminfo.shmmni * sizeof(struct shmid_ds))); 964 ALIGN(shminfo.shmmni * sizeof(struct shmid_ds)));
964 965
965 if (shminfo.shmmax == 0) 966 if (shminfo.shmmax == 0)
966 shminfo.shmmax = max(physmem / 4, 1024) * PAGE_SIZE; 967 shminfo.shmmax = max(physmem / 4, 1024) * PAGE_SIZE;
967 else 968 else
968 shminfo.shmmax *= PAGE_SIZE; 969 shminfo.shmmax *= PAGE_SIZE;
969 shminfo.shmall = shminfo.shmmax / PAGE_SIZE; 970 shminfo.shmall = shminfo.shmmax / PAGE_SIZE;
970 971
971 for (i = 0; i < shminfo.shmmni; i++) { 972 for (i = 0; i < shminfo.shmmni; i++) {