Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/os/shm.c
+++ new/usr/src/uts/common/os/shm.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1986, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 27 /* All Rights Reserved */
28 28
29 29 /*
30 30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 31 * The Regents of the University of California
32 32 * All Rights Reserved
33 33 *
34 34 * University Acknowledgment- Portions of this document are derived from
35 35 * software developed by the University of California, Berkeley, and its
36 36 * contributors.
37 37 */
38 38
39 39 /*
40 40 * Inter-Process Communication Shared Memory Facility.
41 41 *
42 42 * See os/ipc.c for a description of common IPC functionality.
43 43 *
44 44 * Resource controls
45 45 * -----------------
46 46 *
47 47 * Control: zone.max-shm-ids (rc_zone_shmmni)
48 48 * Description: Maximum number of shared memory ids allowed a zone.
49 49 *
50 50 * When shmget() is used to allocate a shared memory segment, one id
51 51 * is allocated. If the id allocation doesn't succeed, shmget()
52 52 * fails and errno is set to ENOSPC. Upon successful shmctl(,
53 53 * IPC_RMID) the id is deallocated.
54 54 *
55 55 * Control: project.max-shm-ids (rc_project_shmmni)
56 56 * Description: Maximum number of shared memory ids allowed a project.
57 57 *
58 58 * When shmget() is used to allocate a shared memory segment, one id
59 59 * is allocated. If the id allocation doesn't succeed, shmget()
60 60 * fails and errno is set to ENOSPC. Upon successful shmctl(,
61 61 * IPC_RMID) the id is deallocated.
62 62 *
63 63 * Control: zone.max-shm-memory (rc_zone_shmmax)
64 64 * Description: Total amount of shared memory allowed a zone.
65 65 *
66 66 * When shmget() is used to allocate a shared memory segment, the
67 67 * segment's size is allocated against this limit. If the space
68 68 * allocation doesn't succeed, shmget() fails and errno is set to
69 69 * EINVAL. The size will be deallocated once the last process has
70 70 * detached the segment and the segment has been successfully
71 71 * shmctl(, IPC_RMID)ed.
72 72 *
73 73 * Control: project.max-shm-memory (rc_project_shmmax)
74 74 * Description: Total amount of shared memory allowed a project.
75 75 *
76 76 * When shmget() is used to allocate a shared memory segment, the
77 77 * segment's size is allocated against this limit. If the space
78 78 * allocation doesn't succeed, shmget() fails and errno is set to
79 79 * EINVAL. The size will be deallocated once the last process has
80 80 * detached the segment and the segment has been successfully
81 81 * shmctl(, IPC_RMID)ed.
82 82 */
83 83
84 84 #include <sys/types.h>
85 85 #include <sys/param.h>
86 86 #include <sys/cred.h>
87 87 #include <sys/errno.h>
88 88 #include <sys/time.h>
89 89 #include <sys/kmem.h>
90 90 #include <sys/user.h>
91 91 #include <sys/proc.h>
92 92 #include <sys/systm.h>
93 93 #include <sys/prsystm.h>
94 94 #include <sys/sysmacros.h>
95 95 #include <sys/tuneable.h>
96 96 #include <sys/vm.h>
97 97 #include <sys/mman.h>
98 98 #include <sys/swap.h>
99 99 #include <sys/cmn_err.h>
100 100 #include <sys/debug.h>
101 101 #include <sys/lwpchan_impl.h>
102 102 #include <sys/avl.h>
103 103 #include <sys/modctl.h>
104 104 #include <sys/syscall.h>
105 105 #include <sys/task.h>
106 106 #include <sys/project.h>
107 107 #include <sys/policy.h>
108 108 #include <sys/zone.h>
109 109 #include <sys/rctl.h>
110 110
111 111 #include <sys/ipc.h>
112 112 #include <sys/ipc_impl.h>
113 113 #include <sys/shm.h>
114 114 #include <sys/shm_impl.h>
115 115
116 116 #include <vm/hat.h>
117 117 #include <vm/seg.h>
118 118 #include <vm/as.h>
119 119 #include <vm/seg_vn.h>
120 120 #include <vm/anon.h>
121 121 #include <vm/page.h>
122 122 #include <vm/vpage.h>
123 123 #include <vm/seg_spt.h>
124 124
125 125 #include <c2/audit.h>
126 126
127 127 static int shmem_lock(kshmid_t *sp, struct anon_map *amp);
128 128 static void shmem_unlock(kshmid_t *sp, struct anon_map *amp);
129 129 static void sa_add(struct proc *pp, caddr_t addr, size_t len, ulong_t flags,
130 130 kshmid_t *id);
131 131 static void shm_rm_amp(kshmid_t *sp);
132 132 static void shm_dtor(kipc_perm_t *);
133 133 static void shm_rmid(kipc_perm_t *);
134 134 static void shm_remove_zone(zoneid_t, void *);
135 135
136 136 /*
137 137 * Semantics for share_page_table and ism_off:
138 138 *
139 139 * These are hooks in /etc/system - only for internal testing purpose.
140 140 *
141 141 * Setting share_page_table automatically turns on the SHM_SHARE_MMU (ISM) flag
142 142 * in a call to shmat(2). In other words, with share_page_table set, you always
143 143 * get ISM, even if say, DISM is specified. It should really be called "ism_on".
144 144 *
145 145 * Setting ism_off turns off the SHM_SHARE_MMU flag from the flags passed to
146 146 * shmat(2).
147 147 *
148 148 * If both share_page_table and ism_off are set, share_page_table prevails.
149 149 *
150 150 * Although these tunables should probably be removed, they do have some
151 151 * external exposure; as long as they exist, they should at least work sensibly.
152 152 */
153 153
154 154 int share_page_table;
155 155 int ism_off;
156 156
157 157 /*
158 158 * The following tunables are obsolete. Though for compatibility we
159 159 * still read and interpret shminfo_shmmax and shminfo_shmmni (see
160 160 * os/project.c), the preferred mechanism for administrating the IPC
161 161 * Shared Memory facility is through the resource controls described at
162 162 * the top of this file.
163 163 */
164 164 size_t shminfo_shmmax = 0x800000; /* (obsolete) */
165 165 int shminfo_shmmni = 100; /* (obsolete) */
166 166 size_t shminfo_shmmin = 1; /* (obsolete) */
167 167 int shminfo_shmseg = 6; /* (obsolete) */
168 168
169 169 extern rctl_hndl_t rc_zone_shmmax;
170 170 extern rctl_hndl_t rc_zone_shmmni;
171 171 extern rctl_hndl_t rc_project_shmmax;
172 172 extern rctl_hndl_t rc_project_shmmni;
173 173 static ipc_service_t *shm_svc;
174 174 static zone_key_t shm_zone_key;
175 175
176 176 /*
177 177 * Module linkage information for the kernel.
178 178 */
179 179 static uintptr_t shmsys(int, uintptr_t, uintptr_t, uintptr_t);
180 180
181 181 static struct sysent ipcshm_sysent = {
182 182 4,
183 183 #ifdef _SYSCALL32_IMPL
184 184 SE_ARGC | SE_NOUNLOAD | SE_64RVAL,
185 185 #else /* _SYSCALL32_IMPL */
186 186 SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
187 187 #endif /* _SYSCALL32_IMPL */
188 188 (int (*)())shmsys
189 189 };
190 190
191 191 #ifdef _SYSCALL32_IMPL
192 192 static struct sysent ipcshm_sysent32 = {
193 193 4,
194 194 SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
195 195 (int (*)())shmsys
196 196 };
197 197 #endif /* _SYSCALL32_IMPL */
198 198
199 199 static struct modlsys modlsys = {
200 200 &mod_syscallops, "System V shared memory", &ipcshm_sysent
↓ open down ↓ |
200 lines elided |
↑ open up ↑ |
201 201 };
202 202
203 203 #ifdef _SYSCALL32_IMPL
204 204 static struct modlsys modlsys32 = {
205 205 &mod_syscallops32, "32-bit System V shared memory", &ipcshm_sysent32
206 206 };
207 207 #endif /* _SYSCALL32_IMPL */
208 208
209 209 static struct modlinkage modlinkage = {
210 210 MODREV_1,
211 - &modlsys,
211 + { &modlsys,
212 212 #ifdef _SYSCALL32_IMPL
213 - &modlsys32,
213 + &modlsys32,
214 214 #endif
215 - NULL
215 + NULL
216 + }
216 217 };
217 218
218 219
219 220 int
220 221 _init(void)
221 222 {
222 223 int result;
223 224
224 225 shm_svc = ipcs_create("shmids", rc_project_shmmni, rc_zone_shmmni,
225 226 sizeof (kshmid_t), shm_dtor, shm_rmid, AT_IPC_SHM,
226 227 offsetof(ipc_rqty_t, ipcq_shmmni));
227 228 zone_key_create(&shm_zone_key, NULL, shm_remove_zone, NULL);
228 229
229 230 if ((result = mod_install(&modlinkage)) == 0)
230 231 return (0);
231 232
232 233 (void) zone_key_delete(shm_zone_key);
233 234 ipcs_destroy(shm_svc);
234 235
235 236 return (result);
236 237 }
237 238
238 239 int
239 240 _fini(void)
240 241 {
241 242 return (EBUSY);
242 243 }
243 244
244 245 int
245 246 _info(struct modinfo *modinfop)
246 247 {
247 248 return (mod_info(&modlinkage, modinfop));
248 249 }
249 250
250 251 /*
251 252 * Shmat (attach shared segment) system call.
252 253 */
253 254 static int
254 255 shmat(int shmid, caddr_t uaddr, int uflags, uintptr_t *rvp)
255 256 {
256 257 kshmid_t *sp; /* shared memory header ptr */
257 258 size_t size;
258 259 int error = 0;
259 260 proc_t *pp = curproc;
260 261 struct as *as = pp->p_as;
261 262 struct segvn_crargs crargs; /* segvn create arguments */
262 263 kmutex_t *lock;
263 264 struct seg *segspt = NULL;
264 265 caddr_t addr = uaddr;
265 266 int flags = (uflags & SHMAT_VALID_FLAGS_MASK);
266 267 int useISM;
267 268 uchar_t prot = PROT_ALL;
268 269 int result;
269 270
270 271 if ((lock = ipc_lookup(shm_svc, shmid, (kipc_perm_t **)&sp)) == NULL)
271 272 return (EINVAL);
272 273 if (error = ipcperm_access(&sp->shm_perm, SHM_R, CRED()))
273 274 goto errret;
274 275 if ((flags & SHM_RDONLY) == 0 &&
275 276 (error = ipcperm_access(&sp->shm_perm, SHM_W, CRED())))
276 277 goto errret;
277 278 if (spt_invalid(flags)) {
278 279 error = EINVAL;
279 280 goto errret;
280 281 }
281 282 if (ism_off)
282 283 flags = flags & ~SHM_SHARE_MMU;
283 284 if (share_page_table) {
284 285 flags = flags & ~SHM_PAGEABLE;
285 286 flags = flags | SHM_SHARE_MMU;
286 287 }
287 288 useISM = (spt_locked(flags) || spt_pageable(flags));
288 289 if (useISM && (error = ipcperm_access(&sp->shm_perm, SHM_W, CRED())))
289 290 goto errret;
290 291 if (useISM && isspt(sp)) {
291 292 uint_t newsptflags = flags | spt_flags(sp->shm_sptseg);
292 293 /*
293 294 * If trying to change an existing {D}ISM segment from ISM
294 295 * to DISM or vice versa, return error. Note that this
295 296 * validation of flags needs to be done after the effect of
296 297 * tunables such as ism_off and share_page_table, for
297 298 * semantics that are consistent with the tunables' settings.
298 299 */
299 300 if (spt_invalid(newsptflags)) {
300 301 error = EINVAL;
301 302 goto errret;
302 303 }
303 304 }
304 305 ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER);
305 306 size = sp->shm_amp->size;
306 307 ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
307 308
308 309 /* somewhere to record spt info for final detach */
309 310 if (sp->shm_sptinfo == NULL)
310 311 sp->shm_sptinfo = kmem_zalloc(sizeof (sptinfo_t), KM_SLEEP);
311 312
312 313 as_rangelock(as);
313 314
314 315 if (useISM) {
315 316 /*
316 317 * Handle ISM
317 318 */
318 319 uint_t share_szc;
319 320 size_t share_size;
320 321 struct shm_data ssd;
321 322 uintptr_t align_hint;
322 323
323 324 /*
324 325 * Pick a share pagesize to use, if (!isspt(sp)).
325 326 * Otherwise use the already chosen page size.
326 327 *
327 328 * For the initial shmat (!isspt(sp)), where sptcreate is
328 329 * called, map_pgsz is called to recommend a [D]ISM pagesize,
329 330 * important for systems which offer more than one potential
330 331 * [D]ISM pagesize.
331 332 * If the shmat is just to attach to an already created
332 333 * [D]ISM segment, then use the previously selected page size.
333 334 */
334 335 if (!isspt(sp)) {
335 336 share_size = map_pgsz(MAPPGSZ_ISM, pp, addr, size, 0);
336 337 if (share_size == 0) {
337 338 as_rangeunlock(as);
338 339 error = EINVAL;
339 340 goto errret;
340 341 }
341 342 share_szc = page_szc(share_size);
342 343 } else {
343 344 share_szc = sp->shm_sptseg->s_szc;
344 345 share_size = page_get_pagesize(share_szc);
345 346 }
346 347 size = P2ROUNDUP(size, share_size);
347 348
348 349 align_hint = share_size;
349 350 #if defined(__i386) || defined(__amd64)
350 351 /*
351 352 * For x86, we want to share as much of the page table tree
352 353 * as possible. We use a large align_hint at first, but
353 354 * if that fails, then the code below retries with align_hint
354 355 * set to share_size.
355 356 *
356 357 * The explicit extern here is due to the difficulties
357 358 * of getting to platform dependent includes. When/if the
358 359 * platform dependent bits of this function are cleaned up,
359 360 * another way of doing this should found.
360 361 */
361 362 {
362 363 extern uint_t ptes_per_table;
363 364
364 365 while (size >= ptes_per_table * (uint64_t)align_hint)
365 366 align_hint *= ptes_per_table;
366 367 }
367 368 #endif /* __i386 || __amd64 */
368 369
369 370 #if defined(__sparcv9)
370 371 if (addr == 0 &&
371 372 pp->p_model == DATAMODEL_LP64 && AS_TYPE_64BIT(as)) {
372 373 /*
373 374 * If no address has been passed in, and this is a
374 375 * 64-bit process, we'll try to find an address
375 376 * in the predict-ISM zone.
376 377 */
377 378 caddr_t predbase = (caddr_t)PREDISM_1T_BASE;
378 379 size_t len = PREDISM_BOUND - PREDISM_1T_BASE;
379 380
380 381 as_purge(as);
381 382 if (as_gap(as, size + share_size, &predbase, &len,
382 383 AH_LO, (caddr_t)NULL) != -1) {
383 384 /*
384 385 * We found an address which looks like a
385 386 * candidate. We want to round it up, and
386 387 * then check that it's a valid user range.
387 388 * This assures that we won't fail below.
388 389 */
389 390 addr = (caddr_t)P2ROUNDUP((uintptr_t)predbase,
390 391 share_size);
391 392
392 393 if (valid_usr_range(addr, size, prot,
393 394 as, as->a_userlimit) != RANGE_OKAY) {
394 395 addr = 0;
395 396 }
396 397 }
397 398 }
398 399 #endif /* __sparcv9 */
399 400
400 401 if (addr == 0) {
401 402 for (;;) {
402 403 addr = (caddr_t)align_hint;
403 404 map_addr(&addr, size, 0ll, 1, MAP_ALIGN);
404 405 if (addr != NULL || align_hint == share_size)
405 406 break;
406 407 align_hint = share_size;
407 408 }
408 409 if (addr == NULL) {
409 410 as_rangeunlock(as);
410 411 error = ENOMEM;
411 412 goto errret;
412 413 }
413 414 ASSERT(((uintptr_t)addr & (align_hint - 1)) == 0);
414 415 } else {
415 416 /* Use the user-supplied attach address */
416 417 caddr_t base;
417 418 size_t len;
418 419
419 420 /*
420 421 * Check that the address range
421 422 * 1) is properly aligned
422 423 * 2) is correct in unix terms
423 424 * 3) is within an unmapped address segment
424 425 */
425 426 base = addr;
426 427 len = size; /* use spt aligned size */
427 428 /* XXX - in SunOS, is sp->shm_segsz */
428 429 if ((uintptr_t)base & (share_size - 1)) {
429 430 error = EINVAL;
430 431 as_rangeunlock(as);
431 432 goto errret;
432 433 }
433 434 result = valid_usr_range(base, len, prot, as,
434 435 as->a_userlimit);
435 436 if (result == RANGE_BADPROT) {
436 437 /*
437 438 * We try to accomodate processors which
438 439 * may not support execute permissions on
439 440 * all ISM segments by trying the check
440 441 * again but without PROT_EXEC.
441 442 */
442 443 prot &= ~PROT_EXEC;
443 444 result = valid_usr_range(base, len, prot, as,
444 445 as->a_userlimit);
445 446 }
446 447 as_purge(as);
447 448 if (result != RANGE_OKAY ||
448 449 as_gap(as, len, &base, &len, AH_LO,
449 450 (caddr_t)NULL) != 0) {
450 451 error = EINVAL;
451 452 as_rangeunlock(as);
452 453 goto errret;
453 454 }
454 455 }
455 456
456 457 if (!isspt(sp)) {
457 458 error = sptcreate(size, &segspt, sp->shm_amp, prot,
458 459 flags, share_szc);
459 460 if (error) {
460 461 as_rangeunlock(as);
461 462 goto errret;
462 463 }
463 464 sp->shm_sptinfo->sptas = segspt->s_as;
464 465 sp->shm_sptseg = segspt;
465 466 sp->shm_sptprot = prot;
466 467 } else if ((prot & sp->shm_sptprot) != sp->shm_sptprot) {
467 468 /*
468 469 * Ensure we're attaching to an ISM segment with
469 470 * fewer or equal permissions than what we're
470 471 * allowed. Fail if the segment has more
471 472 * permissions than what we're allowed.
472 473 */
473 474 error = EACCES;
474 475 as_rangeunlock(as);
475 476 goto errret;
476 477 }
477 478
478 479 ssd.shm_sptseg = sp->shm_sptseg;
479 480 ssd.shm_sptas = sp->shm_sptinfo->sptas;
480 481 ssd.shm_amp = sp->shm_amp;
481 482 error = as_map(as, addr, size, segspt_shmattach, &ssd);
482 483 if (error == 0)
483 484 sp->shm_ismattch++; /* keep count of ISM attaches */
484 485 } else {
485 486
486 487 /*
487 488 * Normal case.
488 489 */
489 490 if (flags & SHM_RDONLY)
490 491 prot &= ~PROT_WRITE;
491 492
492 493 if (addr == 0) {
493 494 /* Let the system pick the attach address */
494 495 map_addr(&addr, size, 0ll, 1, 0);
495 496 if (addr == NULL) {
496 497 as_rangeunlock(as);
497 498 error = ENOMEM;
498 499 goto errret;
499 500 }
500 501 } else {
501 502 /* Use the user-supplied attach address */
502 503 caddr_t base;
503 504 size_t len;
504 505
505 506 if (flags & SHM_RND)
506 507 addr = (caddr_t)((uintptr_t)addr &
507 508 ~(SHMLBA - 1));
508 509 /*
509 510 * Check that the address range
510 511 * 1) is properly aligned
511 512 * 2) is correct in unix terms
512 513 * 3) is within an unmapped address segment
513 514 */
514 515 base = addr;
515 516 len = size; /* use aligned size */
516 517 /* XXX - in SunOS, is sp->shm_segsz */
517 518 if ((uintptr_t)base & PAGEOFFSET) {
518 519 error = EINVAL;
519 520 as_rangeunlock(as);
520 521 goto errret;
521 522 }
522 523 result = valid_usr_range(base, len, prot, as,
523 524 as->a_userlimit);
524 525 if (result == RANGE_BADPROT) {
525 526 prot &= ~PROT_EXEC;
526 527 result = valid_usr_range(base, len, prot, as,
527 528 as->a_userlimit);
528 529 }
529 530 as_purge(as);
530 531 if (result != RANGE_OKAY ||
531 532 as_gap(as, len, &base, &len,
532 533 AH_LO, (caddr_t)NULL) != 0) {
533 534 error = EINVAL;
534 535 as_rangeunlock(as);
535 536 goto errret;
536 537 }
537 538 }
538 539
539 540 /* Initialize the create arguments and map the segment */
540 541 crargs = *(struct segvn_crargs *)zfod_argsp;
541 542 crargs.offset = 0;
542 543 crargs.type = MAP_SHARED;
543 544 crargs.amp = sp->shm_amp;
544 545 crargs.prot = prot;
545 546 crargs.maxprot = crargs.prot;
546 547 crargs.flags = 0;
547 548
548 549 error = as_map(as, addr, size, segvn_create, &crargs);
549 550 }
550 551
551 552 as_rangeunlock(as);
552 553 if (error)
553 554 goto errret;
554 555
555 556 /* record shmem range for the detach */
556 557 sa_add(pp, addr, (size_t)size, useISM ? SHMSA_ISM : 0, sp);
557 558 *rvp = (uintptr_t)addr;
558 559
559 560 sp->shm_atime = gethrestime_sec();
560 561 sp->shm_lpid = pp->p_pid;
561 562 ipc_hold(shm_svc, (kipc_perm_t *)sp);
562 563
563 564 /*
564 565 * Tell machine specific code that lwp has mapped shared memory
565 566 */
566 567 LWP_MMODEL_SHARED_AS(addr, size);
567 568
568 569 errret:
569 570 mutex_exit(lock);
570 571 return (error);
571 572 }
572 573
573 574 static void
574 575 shm_dtor(kipc_perm_t *perm)
575 576 {
576 577 kshmid_t *sp = (kshmid_t *)perm;
577 578 uint_t cnt;
578 579 size_t rsize;
579 580
580 581 ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER);
581 582 anonmap_purge(sp->shm_amp);
582 583 ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
583 584
584 585 if (sp->shm_sptinfo) {
585 586 if (isspt(sp)) {
586 587 sptdestroy(sp->shm_sptinfo->sptas, sp->shm_amp);
587 588 sp->shm_lkcnt = 0;
588 589 }
589 590 kmem_free(sp->shm_sptinfo, sizeof (sptinfo_t));
590 591 }
591 592
592 593 if (sp->shm_lkcnt > 0) {
593 594 shmem_unlock(sp, sp->shm_amp);
594 595 sp->shm_lkcnt = 0;
595 596 }
596 597
597 598 ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER);
598 599 cnt = --sp->shm_amp->refcnt;
599 600 ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
600 601 ASSERT(cnt == 0);
601 602 shm_rm_amp(sp);
602 603
603 604 if (sp->shm_perm.ipc_id != IPC_ID_INVAL) {
604 605 rsize = ptob(btopr(sp->shm_segsz));
605 606 ipcs_lock(shm_svc);
606 607 sp->shm_perm.ipc_proj->kpj_data.kpd_shmmax -= rsize;
607 608 sp->shm_perm.ipc_zone_ref.zref_zone->zone_shmmax -= rsize;
608 609 ipcs_unlock(shm_svc);
609 610 }
610 611 }
611 612
612 613 /* ARGSUSED */
613 614 static void
614 615 shm_rmid(kipc_perm_t *perm)
615 616 {
616 617 /* nothing to do */
617 618 }
618 619
619 620 /*
620 621 * Shmctl system call.
621 622 */
622 623 /* ARGSUSED */
623 624 static int
624 625 shmctl(int shmid, int cmd, void *arg)
625 626 {
626 627 kshmid_t *sp; /* shared memory header ptr */
627 628 STRUCT_DECL(shmid_ds, ds); /* for SVR4 IPC_SET */
628 629 int error = 0;
629 630 struct cred *cr = CRED();
630 631 kmutex_t *lock;
631 632 model_t mdl = get_udatamodel();
632 633 struct shmid_ds64 ds64;
633 634 shmatt_t nattch;
634 635
635 636 STRUCT_INIT(ds, mdl);
636 637
637 638 /*
638 639 * Perform pre- or non-lookup actions (e.g. copyins, RMID).
639 640 */
640 641 switch (cmd) {
641 642 case IPC_SET:
642 643 if (copyin(arg, STRUCT_BUF(ds), STRUCT_SIZE(ds)))
643 644 return (EFAULT);
644 645 break;
645 646
646 647 case IPC_SET64:
647 648 if (copyin(arg, &ds64, sizeof (struct shmid_ds64)))
648 649 return (EFAULT);
649 650 break;
650 651
651 652 case IPC_RMID:
652 653 return (ipc_rmid(shm_svc, shmid, cr));
653 654 }
654 655
655 656 if ((lock = ipc_lookup(shm_svc, shmid, (kipc_perm_t **)&sp)) == NULL)
656 657 return (EINVAL);
657 658
658 659 switch (cmd) {
659 660 /* Set ownership and permissions. */
660 661 case IPC_SET:
661 662 if (error = ipcperm_set(shm_svc, cr, &sp->shm_perm,
662 663 &STRUCT_BUF(ds)->shm_perm, mdl))
663 664 break;
664 665 sp->shm_ctime = gethrestime_sec();
665 666 break;
666 667
667 668 case IPC_STAT:
668 669 if (error = ipcperm_access(&sp->shm_perm, SHM_R, cr))
669 670 break;
670 671
671 672 nattch = sp->shm_perm.ipc_ref - 1;
672 673
673 674 ipcperm_stat(&STRUCT_BUF(ds)->shm_perm, &sp->shm_perm, mdl);
674 675 STRUCT_FSET(ds, shm_segsz, sp->shm_segsz);
675 676 STRUCT_FSETP(ds, shm_amp, NULL); /* kernel addr */
676 677 STRUCT_FSET(ds, shm_lkcnt, sp->shm_lkcnt);
677 678 STRUCT_FSET(ds, shm_lpid, sp->shm_lpid);
678 679 STRUCT_FSET(ds, shm_cpid, sp->shm_cpid);
679 680 STRUCT_FSET(ds, shm_nattch, nattch);
680 681 STRUCT_FSET(ds, shm_cnattch, sp->shm_ismattch);
681 682 STRUCT_FSET(ds, shm_atime, sp->shm_atime);
682 683 STRUCT_FSET(ds, shm_dtime, sp->shm_dtime);
683 684 STRUCT_FSET(ds, shm_ctime, sp->shm_ctime);
684 685
685 686 mutex_exit(lock);
686 687 if (copyout(STRUCT_BUF(ds), arg, STRUCT_SIZE(ds)))
687 688 return (EFAULT);
688 689
689 690 return (0);
690 691
691 692 case IPC_SET64:
692 693 if (error = ipcperm_set64(shm_svc, cr,
693 694 &sp->shm_perm, &ds64.shmx_perm))
694 695 break;
695 696 sp->shm_ctime = gethrestime_sec();
696 697 break;
697 698
698 699 case IPC_STAT64:
699 700 nattch = sp->shm_perm.ipc_ref - 1;
700 701
701 702 ipcperm_stat64(&ds64.shmx_perm, &sp->shm_perm);
702 703 ds64.shmx_segsz = sp->shm_segsz;
703 704 ds64.shmx_lkcnt = sp->shm_lkcnt;
704 705 ds64.shmx_lpid = sp->shm_lpid;
705 706 ds64.shmx_cpid = sp->shm_cpid;
706 707 ds64.shmx_nattch = nattch;
707 708 ds64.shmx_cnattch = sp->shm_ismattch;
708 709 ds64.shmx_atime = sp->shm_atime;
709 710 ds64.shmx_dtime = sp->shm_dtime;
710 711 ds64.shmx_ctime = sp->shm_ctime;
711 712
712 713 mutex_exit(lock);
713 714 if (copyout(&ds64, arg, sizeof (struct shmid_ds64)))
714 715 return (EFAULT);
715 716
716 717 return (0);
717 718
718 719 /* Lock segment in memory */
719 720 case SHM_LOCK:
720 721 if ((error = secpolicy_lock_memory(cr)) != 0)
721 722 break;
722 723
723 724 /* protect against overflow */
724 725 if (sp->shm_lkcnt >= USHRT_MAX) {
725 726 error = ENOMEM;
726 727 break;
727 728 }
728 729 if (!isspt(sp) && (sp->shm_lkcnt++ == 0)) {
729 730 if (error = shmem_lock(sp, sp->shm_amp)) {
730 731 ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock,
731 732 RW_WRITER);
732 733 cmn_err(CE_NOTE, "shmctl - couldn't lock %ld"
733 734 " pages into memory", sp->shm_amp->size);
734 735 ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
735 736 error = ENOMEM;
736 737 sp->shm_lkcnt--;
737 738 }
738 739 }
739 740 break;
740 741
741 742 /* Unlock segment */
742 743 case SHM_UNLOCK:
743 744 if ((error = secpolicy_lock_memory(cr)) != 0)
744 745 break;
745 746
746 747 if (sp->shm_lkcnt && (--sp->shm_lkcnt == 0)) {
747 748 shmem_unlock(sp, sp->shm_amp);
748 749 }
749 750 break;
750 751
751 752 default:
752 753 error = EINVAL;
753 754 break;
754 755 }
755 756 mutex_exit(lock);
756 757 return (error);
757 758 }
758 759
759 760 static void
760 761 shm_detach(proc_t *pp, segacct_t *sap)
761 762 {
762 763 kshmid_t *sp = sap->sa_id;
763 764 size_t len = sap->sa_len;
764 765 caddr_t addr = sap->sa_addr;
765 766
766 767 /*
767 768 * Discard lwpchan mappings.
768 769 */
769 770 if (pp->p_lcp != NULL)
770 771 lwpchan_delete_mapping(pp, addr, addr + len);
771 772 (void) as_unmap(pp->p_as, addr, len);
772 773
773 774 /*
774 775 * Perform some detach-time accounting.
775 776 */
776 777 (void) ipc_lock(shm_svc, sp->shm_perm.ipc_id);
777 778 if (sap->sa_flags & SHMSA_ISM)
778 779 sp->shm_ismattch--;
779 780 sp->shm_dtime = gethrestime_sec();
780 781 sp->shm_lpid = pp->p_pid;
781 782 ipc_rele(shm_svc, (kipc_perm_t *)sp); /* Drops lock */
782 783
783 784 kmem_free(sap, sizeof (segacct_t));
784 785 }
785 786
786 787 static int
787 788 shmdt(caddr_t addr)
788 789 {
789 790 proc_t *pp = curproc;
790 791 segacct_t *sap, template;
791 792
792 793 mutex_enter(&pp->p_lock);
793 794 prbarrier(pp); /* block /proc. See shmgetid(). */
794 795
795 796 template.sa_addr = addr;
796 797 template.sa_len = 0;
797 798 if ((pp->p_segacct == NULL) ||
798 799 ((sap = avl_find(pp->p_segacct, &template, NULL)) == NULL)) {
799 800 mutex_exit(&pp->p_lock);
800 801 return (EINVAL);
801 802 }
802 803 if (sap->sa_addr != addr) {
803 804 mutex_exit(&pp->p_lock);
804 805 return (EINVAL);
805 806 }
806 807 avl_remove(pp->p_segacct, sap);
807 808 mutex_exit(&pp->p_lock);
808 809
809 810 shm_detach(pp, sap);
810 811
811 812 return (0);
812 813 }
813 814
814 815 /*
815 816 * Remove all shared memory segments associated with a given zone.
816 817 * Called by zone_shutdown when the zone is halted.
817 818 */
818 819 /*ARGSUSED1*/
819 820 static void
820 821 shm_remove_zone(zoneid_t zoneid, void *arg)
821 822 {
822 823 ipc_remove_zone(shm_svc, zoneid);
823 824 }
824 825
825 826 /*
826 827 * Shmget (create new shmem) system call.
827 828 */
828 829 static int
829 830 shmget(key_t key, size_t size, int shmflg, uintptr_t *rvp)
830 831 {
831 832 proc_t *pp = curproc;
832 833 kshmid_t *sp;
833 834 kmutex_t *lock;
834 835 int error;
835 836
836 837 top:
837 838 if (error = ipc_get(shm_svc, key, shmflg, (kipc_perm_t **)&sp, &lock))
838 839 return (error);
839 840
840 841 if (!IPC_FREE(&sp->shm_perm)) {
841 842 /*
842 843 * A segment with the requested key exists.
843 844 */
844 845 if (size > sp->shm_segsz) {
845 846 mutex_exit(lock);
846 847 return (EINVAL);
847 848 }
848 849 } else {
849 850 /*
850 851 * A new segment should be created.
851 852 */
852 853 size_t npages = btopr(size);
853 854 size_t rsize = ptob(npages);
854 855
855 856 /*
856 857 * Check rsize and the per-project and per-zone limit on
857 858 * shared memory. Checking rsize handles both the size == 0
858 859 * case and the size < ULONG_MAX & PAGEMASK case (i.e.
859 860 * rounding up wraps a size_t).
860 861 */
861 862 if (rsize == 0 ||
862 863 (rctl_test(rc_project_shmmax,
863 864 pp->p_task->tk_proj->kpj_rctls, pp, rsize,
864 865 RCA_SAFE) & RCT_DENY) ||
865 866 (rctl_test(rc_zone_shmmax,
866 867 pp->p_zone->zone_rctls, pp, rsize,
867 868 RCA_SAFE) & RCT_DENY)) {
868 869
869 870 mutex_exit(&pp->p_lock);
870 871 mutex_exit(lock);
871 872 ipc_cleanup(shm_svc, (kipc_perm_t *)sp);
872 873 return (EINVAL);
873 874 }
874 875 mutex_exit(&pp->p_lock);
875 876 mutex_exit(lock);
876 877
877 878 if (anon_resv(rsize) == 0) {
878 879 ipc_cleanup(shm_svc, (kipc_perm_t *)sp);
879 880 return (ENOMEM);
880 881 }
881 882
882 883 /*
883 884 * If any new failure points are introduced between the
884 885 * the above anon_resv() and the below ipc_commit_begin(),
885 886 * these failure points will need to unreserve the anon
886 887 * reserved using anon_unresv().
887 888 *
888 889 * Once ipc_commit_begin() is called, the anon reserved
889 890 * above will be automatically unreserved by future calls to
890 891 * ipcs_cleanup() -> shm_dtor() -> shm_rm_amp(). If
891 892 * ipc_commit_begin() fails, it internally calls shm_dtor(),
892 893 * unreserving the above anon, and freeing the below amp.
893 894 */
894 895
895 896 sp->shm_amp = anonmap_alloc(rsize, rsize, ANON_SLEEP);
896 897 sp->shm_amp->a_sp = sp;
897 898 /*
898 899 * Store the original user's requested size, in bytes,
899 900 * rather than the page-aligned size. The former is
900 901 * used for IPC_STAT and shmget() lookups. The latter
901 902 * is saved in the anon_map structure and is used for
902 903 * calls to the vm layer.
903 904 */
904 905 sp->shm_segsz = size;
905 906 sp->shm_atime = sp->shm_dtime = 0;
906 907 sp->shm_ctime = gethrestime_sec();
907 908 sp->shm_lpid = (pid_t)0;
908 909 sp->shm_cpid = curproc->p_pid;
909 910 sp->shm_ismattch = 0;
910 911 sp->shm_sptinfo = NULL;
911 912 /*
912 913 * Check limits one last time, push id into global
913 914 * visibility, and update resource usage counts.
914 915 */
915 916 if (error = ipc_commit_begin(shm_svc, key, shmflg,
916 917 (kipc_perm_t *)sp)) {
917 918 if (error == EAGAIN)
918 919 goto top;
919 920 return (error);
920 921 }
921 922
922 923 if ((rctl_test(rc_project_shmmax,
923 924 sp->shm_perm.ipc_proj->kpj_rctls, pp, rsize,
924 925 RCA_SAFE) & RCT_DENY) ||
925 926 (rctl_test(rc_zone_shmmax,
926 927 sp->shm_perm.ipc_zone_ref.zref_zone->zone_rctls, pp, rsize,
927 928 RCA_SAFE) & RCT_DENY)) {
928 929 ipc_cleanup(shm_svc, (kipc_perm_t *)sp);
929 930 return (EINVAL);
930 931 }
931 932 sp->shm_perm.ipc_proj->kpj_data.kpd_shmmax += rsize;
932 933 sp->shm_perm.ipc_zone_ref.zref_zone->zone_shmmax += rsize;
933 934
934 935 lock = ipc_commit_end(shm_svc, &sp->shm_perm);
935 936 }
936 937
937 938 if (AU_AUDITING())
938 939 audit_ipcget(AT_IPC_SHM, (void *)sp);
939 940
940 941 *rvp = (uintptr_t)(sp->shm_perm.ipc_id);
941 942
942 943 mutex_exit(lock);
943 944 return (0);
944 945 }
945 946
946 947 /*
947 948 * shmids system call.
948 949 */
949 950 static int
950 951 shmids(int *buf, uint_t nids, uint_t *pnids)
951 952 {
952 953 return (ipc_ids(shm_svc, buf, nids, pnids));
953 954 }
954 955
955 956 /*
956 957 * System entry point for shmat, shmctl, shmdt, and shmget system calls.
957 958 */
958 959 static uintptr_t
959 960 shmsys(int opcode, uintptr_t a0, uintptr_t a1, uintptr_t a2)
960 961 {
961 962 int error;
962 963 uintptr_t r_val = 0;
963 964
964 965 switch (opcode) {
965 966 case SHMAT:
966 967 error = shmat((int)a0, (caddr_t)a1, (int)a2, &r_val);
967 968 break;
968 969 case SHMCTL:
969 970 error = shmctl((int)a0, (int)a1, (void *)a2);
970 971 break;
971 972 case SHMDT:
972 973 error = shmdt((caddr_t)a0);
973 974 break;
974 975 case SHMGET:
975 976 error = shmget((key_t)a0, (size_t)a1, (int)a2, &r_val);
976 977 break;
977 978 case SHMIDS:
978 979 error = shmids((int *)a0, (uint_t)a1, (uint_t *)a2);
979 980 break;
980 981 default:
981 982 error = EINVAL;
982 983 break;
983 984 }
984 985
985 986 if (error)
986 987 return ((uintptr_t)set_errno(error));
987 988
988 989 return (r_val);
989 990 }
990 991
991 992 /*
992 993 * segacct_t comparator
993 994 * This works as expected, with one minor change: the first of two real
994 995 * segments with equal addresses is considered to be 'greater than' the
995 996 * second. We only return equal when searching using a template, in
996 997 * which case we explicitly set the template segment's length to 0
997 998 * (which is invalid for a real segment).
998 999 */
999 1000 static int
1000 1001 shm_sacompar(const void *x, const void *y)
1001 1002 {
1002 1003 segacct_t *sa1 = (segacct_t *)x;
1003 1004 segacct_t *sa2 = (segacct_t *)y;
1004 1005
1005 1006 if (sa1->sa_addr < sa2->sa_addr) {
1006 1007 return (-1);
1007 1008 } else if (sa2->sa_len != 0) {
1008 1009 if (sa1->sa_addr >= sa2->sa_addr + sa2->sa_len) {
1009 1010 return (1);
1010 1011 } else if (sa1->sa_len != 0) {
1011 1012 return (1);
1012 1013 } else {
1013 1014 return (0);
1014 1015 }
1015 1016 } else if (sa1->sa_addr > sa2->sa_addr) {
1016 1017 return (1);
1017 1018 } else {
1018 1019 return (0);
1019 1020 }
1020 1021 }
1021 1022
1022 1023 /*
1023 1024 * add this record to the segacct list.
1024 1025 */
1025 1026 static void
1026 1027 sa_add(struct proc *pp, caddr_t addr, size_t len, ulong_t flags, kshmid_t *id)
1027 1028 {
1028 1029 segacct_t *nsap;
1029 1030 avl_tree_t *tree = NULL;
1030 1031 avl_index_t where;
1031 1032
1032 1033 nsap = kmem_alloc(sizeof (segacct_t), KM_SLEEP);
1033 1034 nsap->sa_addr = addr;
1034 1035 nsap->sa_len = len;
1035 1036 nsap->sa_flags = flags;
1036 1037 nsap->sa_id = id;
1037 1038
1038 1039 if (pp->p_segacct == NULL)
1039 1040 tree = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
1040 1041
1041 1042 mutex_enter(&pp->p_lock);
1042 1043 prbarrier(pp); /* block /proc. See shmgetid(). */
1043 1044
1044 1045 if (pp->p_segacct == NULL) {
1045 1046 avl_create(tree, shm_sacompar, sizeof (segacct_t),
1046 1047 offsetof(segacct_t, sa_tree));
1047 1048 pp->p_segacct = tree;
1048 1049 } else if (tree) {
1049 1050 kmem_free(tree, sizeof (avl_tree_t));
1050 1051 }
1051 1052
1052 1053 /*
1053 1054 * We can ignore the result of avl_find, as the comparator will
1054 1055 * never return equal for segments with non-zero length. This
1055 1056 * is a necessary hack to get around the fact that we do, in
1056 1057 * fact, have duplicate keys.
1057 1058 */
1058 1059 (void) avl_find(pp->p_segacct, nsap, &where);
1059 1060 avl_insert(pp->p_segacct, nsap, where);
1060 1061
1061 1062 mutex_exit(&pp->p_lock);
1062 1063 }
1063 1064
1064 1065 /*
1065 1066 * Duplicate parent's segacct records in child.
1066 1067 */
1067 1068 void
1068 1069 shmfork(struct proc *ppp, struct proc *cpp)
1069 1070 {
1070 1071 segacct_t *sap;
1071 1072 kshmid_t *sp;
1072 1073 kmutex_t *mp;
1073 1074
1074 1075 ASSERT(ppp->p_segacct != NULL);
1075 1076
1076 1077 /*
1077 1078 * We are the only lwp running in the parent so nobody can
1078 1079 * mess with our p_segacct list. Thus it is safe to traverse
1079 1080 * the list without holding p_lock. This is essential because
1080 1081 * we can't hold p_lock during a KM_SLEEP allocation.
1081 1082 */
1082 1083 for (sap = (segacct_t *)avl_first(ppp->p_segacct); sap != NULL;
1083 1084 sap = (segacct_t *)AVL_NEXT(ppp->p_segacct, sap)) {
1084 1085 sa_add(cpp, sap->sa_addr, sap->sa_len, sap->sa_flags,
1085 1086 sap->sa_id);
1086 1087 sp = sap->sa_id;
1087 1088 mp = ipc_lock(shm_svc, sp->shm_perm.ipc_id);
1088 1089 if (sap->sa_flags & SHMSA_ISM)
1089 1090 sp->shm_ismattch++;
1090 1091 ipc_hold(shm_svc, (kipc_perm_t *)sp);
1091 1092 mutex_exit(mp);
1092 1093 }
1093 1094 }
1094 1095
1095 1096 /*
1096 1097 * Detach shared memory segments from exiting process.
1097 1098 */
1098 1099 void
1099 1100 shmexit(struct proc *pp)
1100 1101 {
1101 1102 segacct_t *sap;
1102 1103 avl_tree_t *tree;
1103 1104 void *cookie = NULL;
1104 1105
1105 1106 ASSERT(pp->p_segacct != NULL);
1106 1107
1107 1108 mutex_enter(&pp->p_lock);
1108 1109 prbarrier(pp);
1109 1110 tree = pp->p_segacct;
1110 1111 pp->p_segacct = NULL;
1111 1112 mutex_exit(&pp->p_lock);
1112 1113
1113 1114 while ((sap = avl_destroy_nodes(tree, &cookie)) != NULL)
1114 1115 (void) shm_detach(pp, sap);
1115 1116
1116 1117 avl_destroy(tree);
1117 1118 kmem_free(tree, sizeof (avl_tree_t));
1118 1119 }
1119 1120
1120 1121 /*
1121 1122 * At this time pages should be in memory, so just lock them.
1122 1123 */
1123 1124 static void
1124 1125 lock_again(size_t npages, kshmid_t *sp, struct anon_map *amp)
1125 1126 {
1126 1127 struct anon *ap;
1127 1128 struct page *pp;
1128 1129 struct vnode *vp;
1129 1130 u_offset_t off;
1130 1131 ulong_t anon_idx;
1131 1132 anon_sync_obj_t cookie;
1132 1133
1133 1134 mutex_enter(&sp->shm_mlock);
1134 1135 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
1135 1136 for (anon_idx = 0; npages != 0; anon_idx++, npages--) {
1136 1137
1137 1138 anon_array_enter(amp, anon_idx, &cookie);
1138 1139 ap = anon_get_ptr(amp->ahp, anon_idx);
1139 1140 ASSERT(ap != NULL);
1140 1141 swap_xlate(ap, &vp, &off);
1141 1142 anon_array_exit(&cookie);
1142 1143
1143 1144 pp = page_lookup(vp, off, SE_SHARED);
1144 1145 if (pp == NULL) {
1145 1146 panic("lock_again: page not in the system");
1146 1147 /*NOTREACHED*/
1147 1148 }
1148 1149 /* page should already be locked by caller */
1149 1150 ASSERT(pp->p_lckcnt > 0);
1150 1151 (void) page_pp_lock(pp, 0, 0);
1151 1152 page_unlock(pp);
1152 1153 }
1153 1154 ANON_LOCK_EXIT(&->a_rwlock);
1154 1155 mutex_exit(&sp->shm_mlock);
1155 1156 }
1156 1157
1157 1158 /*
1158 1159 * Attach the shared memory segment to the process
1159 1160 * address space and lock the pages.
1160 1161 */
1161 1162 static int
1162 1163 shmem_lock(kshmid_t *sp, struct anon_map *amp)
1163 1164 {
1164 1165 size_t npages = btopr(amp->size);
1165 1166 struct as *as;
1166 1167 struct segvn_crargs crargs;
1167 1168 uint_t error;
1168 1169
1169 1170 /*
1170 1171 * A later ISM/DISM attach may increase the size of the amp, so
1171 1172 * cache the number of pages locked for the future shmem_unlock()
1172 1173 */
1173 1174 sp->shm_lkpages = npages;
1174 1175
1175 1176 as = as_alloc();
1176 1177 /* Initialize the create arguments and map the segment */
1177 1178 crargs = *(struct segvn_crargs *)zfod_argsp; /* structure copy */
1178 1179 crargs.offset = (u_offset_t)0;
1179 1180 crargs.type = MAP_SHARED;
1180 1181 crargs.amp = amp;
1181 1182 crargs.prot = PROT_ALL;
1182 1183 crargs.maxprot = crargs.prot;
1183 1184 crargs.flags = 0;
1184 1185 error = as_map(as, 0x0, amp->size, segvn_create, &crargs);
1185 1186 if (!error) {
1186 1187 if ((error = as_ctl(as, 0x0, amp->size, MC_LOCK, 0, 0,
1187 1188 NULL, 0)) == 0) {
1188 1189 lock_again(npages, sp, amp);
1189 1190 }
1190 1191 (void) as_unmap(as, 0x0, amp->size);
1191 1192 }
1192 1193 as_free(as);
1193 1194 return (error);
1194 1195 }
1195 1196
1196 1197
1197 1198 /*
1198 1199 * Unlock shared memory
1199 1200 */
1200 1201 static void
1201 1202 shmem_unlock(kshmid_t *sp, struct anon_map *amp)
1202 1203 {
1203 1204 struct anon *ap;
1204 1205 pgcnt_t npages = sp->shm_lkpages;
1205 1206 struct vnode *vp;
1206 1207 struct page *pp;
1207 1208 u_offset_t off;
1208 1209 ulong_t anon_idx;
1209 1210 size_t unlocked_bytes = 0;
1210 1211 kproject_t *proj;
1211 1212 anon_sync_obj_t cookie;
1212 1213
1213 1214 proj = sp->shm_perm.ipc_proj;
1214 1215 mutex_enter(&sp->shm_mlock);
1215 1216 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
1216 1217 for (anon_idx = 0; anon_idx < npages; anon_idx++) {
1217 1218
1218 1219 anon_array_enter(amp, anon_idx, &cookie);
1219 1220 if ((ap = anon_get_ptr(amp->ahp, anon_idx)) == NULL) {
1220 1221 panic("shmem_unlock: null app");
1221 1222 /*NOTREACHED*/
1222 1223 }
1223 1224 swap_xlate(ap, &vp, &off);
1224 1225 anon_array_exit(&cookie);
1225 1226 pp = page_lookup(vp, off, SE_SHARED);
1226 1227 if (pp == NULL) {
1227 1228 panic("shmem_unlock: page not in the system");
1228 1229 /*NOTREACHED*/
1229 1230 }
1230 1231 /*
1231 1232 * Page should at least have once lock from previous
1232 1233 * shmem_lock
1233 1234 */
1234 1235 ASSERT(pp->p_lckcnt > 0);
1235 1236 page_pp_unlock(pp, 0, 0);
1236 1237 if (pp->p_lckcnt == 0)
1237 1238 unlocked_bytes += PAGESIZE;
1238 1239
1239 1240 page_unlock(pp);
1240 1241 }
1241 1242
1242 1243 if (unlocked_bytes > 0) {
1243 1244 rctl_decr_locked_mem(NULL, proj, unlocked_bytes, 0);
1244 1245 }
1245 1246
1246 1247 ANON_LOCK_EXIT(&->a_rwlock);
1247 1248 mutex_exit(&sp->shm_mlock);
1248 1249 }
1249 1250
1250 1251 /*
1251 1252 * We call this routine when we have removed all references to this
1252 1253 * amp. This means all shmdt()s and the IPC_RMID have been done.
1253 1254 */
1254 1255 static void
1255 1256 shm_rm_amp(kshmid_t *sp)
1256 1257 {
1257 1258 struct anon_map *amp = sp->shm_amp;
1258 1259 zone_t *zone;
1259 1260
1260 1261 zone = sp->shm_perm.ipc_zone_ref.zref_zone;
1261 1262 ASSERT(zone != NULL);
1262 1263 /*
1263 1264 * Free up the anon_map.
1264 1265 */
1265 1266 lgrp_shm_policy_fini(amp, NULL);
1266 1267 ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
1267 1268 if (amp->a_szc != 0) {
1268 1269 anon_shmap_free_pages(amp, 0, amp->size);
1269 1270 } else {
1270 1271 anon_free(amp->ahp, 0, amp->size);
1271 1272 }
1272 1273 ANON_LOCK_EXIT(&->a_rwlock);
1273 1274 anon_unresv_zone(amp->swresv, zone);
1274 1275 anonmap_free(amp);
1275 1276 }
1276 1277
1277 1278 /*
1278 1279 * Return the shared memory id for the process's virtual address.
1279 1280 * Return SHMID_NONE if addr is not within a SysV shared memory segment.
1280 1281 * Return SHMID_FREE if addr's SysV shared memory segment's id has been freed.
1281 1282 *
1282 1283 * shmgetid() is called from code in /proc with the process locked but
1283 1284 * with pp->p_lock not held. The address space lock is held, so we
1284 1285 * cannot grab pp->p_lock here due to lock-ordering constraints.
1285 1286 * Because of all this, modifications to the p_segacct list must only
1286 1287 * be made after calling prbarrier() to ensure the process is not locked.
1287 1288 * See shmdt() and sa_add(), above. shmgetid() may also be called on a
1288 1289 * thread's own process without the process locked.
1289 1290 */
1290 1291 int
1291 1292 shmgetid(proc_t *pp, caddr_t addr)
1292 1293 {
1293 1294 segacct_t *sap, template;
1294 1295
1295 1296 ASSERT(MUTEX_NOT_HELD(&pp->p_lock));
1296 1297 ASSERT((pp->p_proc_flag & P_PR_LOCK) || pp == curproc);
1297 1298
1298 1299 if (pp->p_segacct == NULL)
1299 1300 return (SHMID_NONE);
1300 1301
1301 1302 template.sa_addr = addr;
1302 1303 template.sa_len = 0;
1303 1304 if ((sap = avl_find(pp->p_segacct, &template, NULL)) == NULL)
1304 1305 return (SHMID_NONE);
1305 1306
1306 1307 if (IPC_FREE(&sap->sa_id->shm_perm))
1307 1308 return (SHMID_FREE);
1308 1309
1309 1310 return (sap->sa_id->shm_perm.ipc_id);
1310 1311 }
↓ open down ↓ |
1085 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX