Print this page
10095 unchecked return value in segvn_pagelock()
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/vm/seg_vn.c
+++ new/usr/src/uts/common/vm/seg_vn.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 * Copyright (c) 1986, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright 2018 Joyent, Inc.
24 24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 25 */
26 26
27 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 28 /* All Rights Reserved */
29 29
30 30 /*
31 31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 32 * The Regents of the University of California
33 33 * All Rights Reserved
34 34 *
35 35 * University Acknowledgment- Portions of this document are derived from
36 36 * software developed by the University of California, Berkeley, and its
37 37 * contributors.
38 38 */
39 39
40 40 /*
41 41 * VM - shared or copy-on-write from a vnode/anonymous memory.
42 42 */
43 43
44 44 #include <sys/types.h>
45 45 #include <sys/param.h>
46 46 #include <sys/t_lock.h>
47 47 #include <sys/errno.h>
48 48 #include <sys/systm.h>
49 49 #include <sys/mman.h>
50 50 #include <sys/debug.h>
51 51 #include <sys/cred.h>
52 52 #include <sys/vmsystm.h>
53 53 #include <sys/tuneable.h>
54 54 #include <sys/bitmap.h>
55 55 #include <sys/swap.h>
56 56 #include <sys/kmem.h>
57 57 #include <sys/sysmacros.h>
58 58 #include <sys/vtrace.h>
59 59 #include <sys/cmn_err.h>
60 60 #include <sys/callb.h>
61 61 #include <sys/vm.h>
62 62 #include <sys/dumphdr.h>
63 63 #include <sys/lgrp.h>
64 64
65 65 #include <vm/hat.h>
66 66 #include <vm/as.h>
67 67 #include <vm/seg.h>
68 68 #include <vm/seg_vn.h>
69 69 #include <vm/pvn.h>
70 70 #include <vm/anon.h>
71 71 #include <vm/page.h>
72 72 #include <vm/vpage.h>
73 73 #include <sys/proc.h>
74 74 #include <sys/task.h>
75 75 #include <sys/project.h>
76 76 #include <sys/zone.h>
77 77 #include <sys/shm_impl.h>
78 78
79 79 /*
80 80 * segvn_fault needs a temporary page list array. To avoid calling kmem all
81 81 * the time, it creates a small (PVN_GETPAGE_NUM entry) array and uses it if
82 82 * it can. In the rare case when this page list is not large enough, it
83 83 * goes and gets a large enough array from kmem.
84 84 *
85 85 * This small page list array covers either 8 pages or 64kB worth of pages -
86 86 * whichever is smaller.
87 87 */
88 88 #define PVN_MAX_GETPAGE_SZ 0x10000
89 89 #define PVN_MAX_GETPAGE_NUM 0x8
90 90
91 91 #if PVN_MAX_GETPAGE_SZ > PVN_MAX_GETPAGE_NUM * PAGESIZE
92 92 #define PVN_GETPAGE_SZ ptob(PVN_MAX_GETPAGE_NUM)
93 93 #define PVN_GETPAGE_NUM PVN_MAX_GETPAGE_NUM
94 94 #else
95 95 #define PVN_GETPAGE_SZ PVN_MAX_GETPAGE_SZ
96 96 #define PVN_GETPAGE_NUM btop(PVN_MAX_GETPAGE_SZ)
97 97 #endif
98 98
99 99 /*
100 100 * Private seg op routines.
101 101 */
102 102 static int segvn_dup(struct seg *seg, struct seg *newseg);
103 103 static int segvn_unmap(struct seg *seg, caddr_t addr, size_t len);
104 104 static void segvn_free(struct seg *seg);
105 105 static faultcode_t segvn_fault(struct hat *hat, struct seg *seg,
106 106 caddr_t addr, size_t len, enum fault_type type,
107 107 enum seg_rw rw);
108 108 static faultcode_t segvn_faulta(struct seg *seg, caddr_t addr);
109 109 static int segvn_setprot(struct seg *seg, caddr_t addr,
110 110 size_t len, uint_t prot);
111 111 static int segvn_checkprot(struct seg *seg, caddr_t addr,
112 112 size_t len, uint_t prot);
113 113 static int segvn_kluster(struct seg *seg, caddr_t addr, ssize_t delta);
114 114 static size_t segvn_swapout(struct seg *seg);
115 115 static int segvn_sync(struct seg *seg, caddr_t addr, size_t len,
116 116 int attr, uint_t flags);
117 117 static size_t segvn_incore(struct seg *seg, caddr_t addr, size_t len,
118 118 char *vec);
119 119 static int segvn_lockop(struct seg *seg, caddr_t addr, size_t len,
120 120 int attr, int op, ulong_t *lockmap, size_t pos);
121 121 static int segvn_getprot(struct seg *seg, caddr_t addr, size_t len,
122 122 uint_t *protv);
123 123 static u_offset_t segvn_getoffset(struct seg *seg, caddr_t addr);
124 124 static int segvn_gettype(struct seg *seg, caddr_t addr);
125 125 static int segvn_getvp(struct seg *seg, caddr_t addr,
126 126 struct vnode **vpp);
127 127 static int segvn_advise(struct seg *seg, caddr_t addr, size_t len,
128 128 uint_t behav);
129 129 static void segvn_dump(struct seg *seg);
130 130 static int segvn_pagelock(struct seg *seg, caddr_t addr, size_t len,
131 131 struct page ***ppp, enum lock_type type, enum seg_rw rw);
132 132 static int segvn_setpagesize(struct seg *seg, caddr_t addr, size_t len,
133 133 uint_t szc);
134 134 static int segvn_getmemid(struct seg *seg, caddr_t addr,
135 135 memid_t *memidp);
136 136 static lgrp_mem_policy_info_t *segvn_getpolicy(struct seg *, caddr_t);
137 137 static int segvn_capable(struct seg *seg, segcapability_t capable);
138 138 static int segvn_inherit(struct seg *, caddr_t, size_t, uint_t);
139 139
140 140 struct seg_ops segvn_ops = {
141 141 segvn_dup,
142 142 segvn_unmap,
143 143 segvn_free,
144 144 segvn_fault,
145 145 segvn_faulta,
146 146 segvn_setprot,
147 147 segvn_checkprot,
148 148 segvn_kluster,
149 149 segvn_swapout,
150 150 segvn_sync,
151 151 segvn_incore,
152 152 segvn_lockop,
153 153 segvn_getprot,
154 154 segvn_getoffset,
155 155 segvn_gettype,
156 156 segvn_getvp,
157 157 segvn_advise,
158 158 segvn_dump,
159 159 segvn_pagelock,
160 160 segvn_setpagesize,
161 161 segvn_getmemid,
162 162 segvn_getpolicy,
163 163 segvn_capable,
164 164 segvn_inherit
165 165 };
166 166
167 167 /*
168 168 * Common zfod structures, provided as a shorthand for others to use.
169 169 */
170 170 static segvn_crargs_t zfod_segvn_crargs =
171 171 SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
172 172 static segvn_crargs_t kzfod_segvn_crargs =
173 173 SEGVN_ZFOD_ARGS(PROT_ZFOD & ~PROT_USER,
174 174 PROT_ALL & ~PROT_USER);
175 175 static segvn_crargs_t stack_noexec_crargs =
176 176 SEGVN_ZFOD_ARGS(PROT_ZFOD & ~PROT_EXEC, PROT_ALL);
177 177
178 178 caddr_t zfod_argsp = (caddr_t)&zfod_segvn_crargs; /* user zfod argsp */
179 179 caddr_t kzfod_argsp = (caddr_t)&kzfod_segvn_crargs; /* kernel zfod argsp */
180 180 caddr_t stack_exec_argsp = (caddr_t)&zfod_segvn_crargs; /* executable stack */
181 181 caddr_t stack_noexec_argsp = (caddr_t)&stack_noexec_crargs; /* noexec stack */
182 182
183 183 #define vpgtob(n) ((n) * sizeof (struct vpage)) /* For brevity */
184 184
185 185 size_t segvn_comb_thrshld = UINT_MAX; /* patchable -- see 1196681 */
186 186
187 187 size_t segvn_pglock_comb_thrshld = (1UL << 16); /* 64K */
188 188 size_t segvn_pglock_comb_balign = (1UL << 16); /* 64K */
189 189 uint_t segvn_pglock_comb_bshift;
190 190 size_t segvn_pglock_comb_palign;
191 191
192 192 static int segvn_concat(struct seg *, struct seg *, int);
193 193 static int segvn_extend_prev(struct seg *, struct seg *,
194 194 struct segvn_crargs *, size_t);
195 195 static int segvn_extend_next(struct seg *, struct seg *,
196 196 struct segvn_crargs *, size_t);
197 197 static void segvn_softunlock(struct seg *, caddr_t, size_t, enum seg_rw);
198 198 static void segvn_pagelist_rele(page_t **);
199 199 static void segvn_setvnode_mpss(vnode_t *);
200 200 static void segvn_relocate_pages(page_t **, page_t *);
201 201 static int segvn_full_szcpages(page_t **, uint_t, int *, uint_t *);
202 202 static int segvn_fill_vp_pages(struct segvn_data *, vnode_t *, u_offset_t,
203 203 uint_t, page_t **, page_t **, uint_t *, int *);
204 204 static faultcode_t segvn_fault_vnodepages(struct hat *, struct seg *, caddr_t,
205 205 caddr_t, enum fault_type, enum seg_rw, caddr_t, caddr_t, int);
206 206 static faultcode_t segvn_fault_anonpages(struct hat *, struct seg *, caddr_t,
207 207 caddr_t, enum fault_type, enum seg_rw, caddr_t, caddr_t, int);
208 208 static faultcode_t segvn_faultpage(struct hat *, struct seg *, caddr_t,
209 209 u_offset_t, struct vpage *, page_t **, uint_t,
210 210 enum fault_type, enum seg_rw, int);
211 211 static void segvn_vpage(struct seg *);
212 212 static size_t segvn_count_swap_by_vpages(struct seg *);
213 213
214 214 static void segvn_purge(struct seg *seg);
215 215 static int segvn_reclaim(void *, caddr_t, size_t, struct page **,
216 216 enum seg_rw, int);
217 217 static int shamp_reclaim(void *, caddr_t, size_t, struct page **,
218 218 enum seg_rw, int);
219 219
220 220 static int sameprot(struct seg *, caddr_t, size_t);
221 221
222 222 static int segvn_demote_range(struct seg *, caddr_t, size_t, int, uint_t);
223 223 static int segvn_clrszc(struct seg *);
224 224 static struct seg *segvn_split_seg(struct seg *, caddr_t);
225 225 static int segvn_claim_pages(struct seg *, struct vpage *, u_offset_t,
226 226 ulong_t, uint_t);
227 227
228 228 static void segvn_hat_rgn_unload_callback(caddr_t, caddr_t, caddr_t,
229 229 size_t, void *, u_offset_t);
230 230
231 231 static struct kmem_cache *segvn_cache;
232 232 static struct kmem_cache **segvn_szc_cache;
233 233
234 234 #ifdef VM_STATS
235 235 static struct segvnvmstats_str {
236 236 ulong_t fill_vp_pages[31];
237 237 ulong_t fltvnpages[49];
238 238 ulong_t fullszcpages[10];
239 239 ulong_t relocatepages[3];
240 240 ulong_t fltanpages[17];
241 241 ulong_t pagelock[2];
242 242 ulong_t demoterange[3];
243 243 } segvnvmstats;
244 244 #endif /* VM_STATS */
245 245
246 246 #define SDR_RANGE 1 /* demote entire range */
247 247 #define SDR_END 2 /* demote non aligned ends only */
248 248
249 249 #define CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr) { \
250 250 if ((len) != 0) { \
251 251 lpgaddr = (caddr_t)P2ALIGN((uintptr_t)(addr), pgsz); \
252 252 ASSERT(lpgaddr >= (seg)->s_base); \
253 253 lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)((addr) + \
254 254 (len)), pgsz); \
255 255 ASSERT(lpgeaddr > lpgaddr); \
256 256 ASSERT(lpgeaddr <= (seg)->s_base + (seg)->s_size); \
257 257 } else { \
258 258 lpgeaddr = lpgaddr = (addr); \
259 259 } \
260 260 }
261 261
262 262 /*ARGSUSED*/
263 263 static int
264 264 segvn_cache_constructor(void *buf, void *cdrarg, int kmflags)
265 265 {
266 266 struct segvn_data *svd = buf;
267 267
268 268 rw_init(&svd->lock, NULL, RW_DEFAULT, NULL);
269 269 mutex_init(&svd->segfree_syncmtx, NULL, MUTEX_DEFAULT, NULL);
270 270 svd->svn_trnext = svd->svn_trprev = NULL;
271 271 return (0);
272 272 }
273 273
274 274 /*ARGSUSED1*/
275 275 static void
276 276 segvn_cache_destructor(void *buf, void *cdrarg)
277 277 {
278 278 struct segvn_data *svd = buf;
279 279
280 280 rw_destroy(&svd->lock);
281 281 mutex_destroy(&svd->segfree_syncmtx);
282 282 }
283 283
284 284 /*ARGSUSED*/
285 285 static int
286 286 svntr_cache_constructor(void *buf, void *cdrarg, int kmflags)
287 287 {
288 288 bzero(buf, sizeof (svntr_t));
289 289 return (0);
290 290 }
291 291
292 292 /*
293 293 * Patching this variable to non-zero allows the system to run with
294 294 * stacks marked as "not executable". It's a bit of a kludge, but is
295 295 * provided as a tweakable for platforms that export those ABIs
296 296 * (e.g. sparc V8) that have executable stacks enabled by default.
297 297 * There are also some restrictions for platforms that don't actually
298 298 * implement 'noexec' protections.
299 299 *
300 300 * Once enabled, the system is (therefore) unable to provide a fully
301 301 * ABI-compliant execution environment, though practically speaking,
302 302 * most everything works. The exceptions are generally some interpreters
303 303 * and debuggers that create executable code on the stack and jump
304 304 * into it (without explicitly mprotecting the address range to include
305 305 * PROT_EXEC).
306 306 *
307 307 * One important class of applications that are disabled are those
308 308 * that have been transformed into malicious agents using one of the
309 309 * numerous "buffer overflow" attacks. See 4007890.
310 310 */
311 311 int noexec_user_stack = 0;
312 312 int noexec_user_stack_log = 1;
313 313
314 314 int segvn_lpg_disable = 0;
315 315 uint_t segvn_maxpgszc = 0;
316 316
317 317 ulong_t segvn_vmpss_clrszc_cnt;
318 318 ulong_t segvn_vmpss_clrszc_err;
319 319 ulong_t segvn_fltvnpages_clrszc_cnt;
320 320 ulong_t segvn_fltvnpages_clrszc_err;
321 321 ulong_t segvn_setpgsz_align_err;
322 322 ulong_t segvn_setpgsz_anon_align_err;
323 323 ulong_t segvn_setpgsz_getattr_err;
324 324 ulong_t segvn_setpgsz_eof_err;
325 325 ulong_t segvn_faultvnmpss_align_err1;
326 326 ulong_t segvn_faultvnmpss_align_err2;
327 327 ulong_t segvn_faultvnmpss_align_err3;
328 328 ulong_t segvn_faultvnmpss_align_err4;
329 329 ulong_t segvn_faultvnmpss_align_err5;
330 330 ulong_t segvn_vmpss_pageio_deadlk_err;
331 331
332 332 int segvn_use_regions = 1;
333 333
334 334 /*
335 335 * Segvn supports text replication optimization for NUMA platforms. Text
336 336 * replica's are represented by anon maps (amp). There's one amp per text file
337 337 * region per lgroup. A process chooses the amp for each of its text mappings
338 338 * based on the lgroup assignment of its main thread (t_tid = 1). All
339 339 * processes that want a replica on a particular lgroup for the same text file
340 340 * mapping share the same amp. amp's are looked up in svntr_hashtab hash table
341 341 * with vp,off,size,szc used as a key. Text replication segments are read only
342 342 * MAP_PRIVATE|MAP_TEXT segments that map vnode. Replication is achieved by
343 343 * forcing COW faults from vnode to amp and mapping amp pages instead of vnode
344 344 * pages. Replication amp is assigned to a segment when it gets its first
345 345 * pagefault. To handle main thread lgroup rehoming segvn_trasync_thread
346 346 * rechecks periodically if the process still maps an amp local to the main
347 347 * thread. If not async thread forces process to remap to an amp in the new
348 348 * home lgroup of the main thread. Current text replication implementation
349 349 * only provides the benefit to workloads that do most of their work in the
350 350 * main thread of a process or all the threads of a process run in the same
351 351 * lgroup. To extend text replication benefit to different types of
352 352 * multithreaded workloads further work would be needed in the hat layer to
353 353 * allow the same virtual address in the same hat to simultaneously map
354 354 * different physical addresses (i.e. page table replication would be needed
355 355 * for x86).
356 356 *
357 357 * amp pages are used instead of vnode pages as long as segment has a very
358 358 * simple life cycle. It's created via segvn_create(), handles S_EXEC
359 359 * (S_READ) pagefaults and is fully unmapped. If anything more complicated
360 360 * happens such as protection is changed, real COW fault happens, pagesize is
361 361 * changed, MC_LOCK is requested or segment is partially unmapped we turn off
362 362 * text replication by converting the segment back to vnode only segment
363 363 * (unmap segment's address range and set svd->amp to NULL).
364 364 *
365 365 * The original file can be changed after amp is inserted into
366 366 * svntr_hashtab. Processes that are launched after the file is already
367 367 * changed can't use the replica's created prior to the file change. To
368 368 * implement this functionality hash entries are timestamped. Replica's can
369 369 * only be used if current file modification time is the same as the timestamp
370 370 * saved when hash entry was created. However just timestamps alone are not
371 371 * sufficient to detect file modification via mmap(MAP_SHARED) mappings. We
372 372 * deal with file changes via MAP_SHARED mappings differently. When writable
373 373 * MAP_SHARED mappings are created to vnodes marked as executable we mark all
374 374 * existing replica's for this vnode as not usable for future text
375 375 * mappings. And we don't create new replica's for files that currently have
376 376 * potentially writable MAP_SHARED mappings (i.e. vn_is_mapped(V_WRITE) is
377 377 * true).
378 378 */
379 379
380 380 #define SEGVN_TEXTREPL_MAXBYTES_FACTOR (20)
381 381 size_t segvn_textrepl_max_bytes_factor = SEGVN_TEXTREPL_MAXBYTES_FACTOR;
382 382
383 383 static ulong_t svntr_hashtab_sz = 512;
384 384 static svntr_bucket_t *svntr_hashtab = NULL;
385 385 static struct kmem_cache *svntr_cache;
386 386 static svntr_stats_t *segvn_textrepl_stats;
387 387 static ksema_t segvn_trasync_sem;
388 388
389 389 int segvn_disable_textrepl = 1;
390 390 size_t textrepl_size_thresh = (size_t)-1;
391 391 size_t segvn_textrepl_bytes = 0;
392 392 size_t segvn_textrepl_max_bytes = 0;
393 393 clock_t segvn_update_textrepl_interval = 0;
394 394 int segvn_update_tr_time = 10;
395 395 int segvn_disable_textrepl_update = 0;
396 396
397 397 static void segvn_textrepl(struct seg *);
398 398 static void segvn_textunrepl(struct seg *, int);
399 399 static void segvn_inval_trcache(vnode_t *);
400 400 static void segvn_trasync_thread(void);
401 401 static void segvn_trupdate_wakeup(void *);
402 402 static void segvn_trupdate(void);
403 403 static void segvn_trupdate_seg(struct seg *, segvn_data_t *, svntr_t *,
404 404 ulong_t);
405 405
406 406 /*
407 407 * Initialize segvn data structures
408 408 */
409 409 void
410 410 segvn_init(void)
411 411 {
412 412 uint_t maxszc;
413 413 uint_t szc;
414 414 size_t pgsz;
415 415
416 416 segvn_cache = kmem_cache_create("segvn_cache",
417 417 sizeof (struct segvn_data), 0,
418 418 segvn_cache_constructor, segvn_cache_destructor, NULL,
419 419 NULL, NULL, 0);
420 420
421 421 if (segvn_lpg_disable == 0) {
422 422 szc = maxszc = page_num_pagesizes() - 1;
423 423 if (szc == 0) {
424 424 segvn_lpg_disable = 1;
425 425 }
426 426 if (page_get_pagesize(0) != PAGESIZE) {
427 427 panic("segvn_init: bad szc 0");
428 428 /*NOTREACHED*/
429 429 }
430 430 while (szc != 0) {
431 431 pgsz = page_get_pagesize(szc);
432 432 if (pgsz <= PAGESIZE || !IS_P2ALIGNED(pgsz, pgsz)) {
433 433 panic("segvn_init: bad szc %d", szc);
434 434 /*NOTREACHED*/
435 435 }
436 436 szc--;
437 437 }
438 438 if (segvn_maxpgszc == 0 || segvn_maxpgszc > maxszc)
439 439 segvn_maxpgszc = maxszc;
440 440 }
441 441
442 442 if (segvn_maxpgszc) {
443 443 segvn_szc_cache = (struct kmem_cache **)kmem_alloc(
444 444 (segvn_maxpgszc + 1) * sizeof (struct kmem_cache *),
445 445 KM_SLEEP);
446 446 }
447 447
448 448 for (szc = 1; szc <= segvn_maxpgszc; szc++) {
449 449 char str[32];
450 450
451 451 (void) sprintf(str, "segvn_szc_cache%d", szc);
452 452 segvn_szc_cache[szc] = kmem_cache_create(str,
453 453 page_get_pagecnt(szc) * sizeof (page_t *), 0,
454 454 NULL, NULL, NULL, NULL, NULL, KMC_NODEBUG);
455 455 }
456 456
457 457
458 458 if (segvn_use_regions && !hat_supported(HAT_SHARED_REGIONS, NULL))
459 459 segvn_use_regions = 0;
460 460
461 461 /*
462 462 * For now shared regions and text replication segvn support
463 463 * are mutually exclusive. This is acceptable because
464 464 * currently significant benefit from text replication was
465 465 * only observed on AMD64 NUMA platforms (due to relatively
466 466 * small L2$ size) and currently we don't support shared
467 467 * regions on x86.
468 468 */
469 469 if (segvn_use_regions && !segvn_disable_textrepl) {
470 470 segvn_disable_textrepl = 1;
471 471 }
472 472
473 473 #if defined(_LP64)
474 474 if (lgrp_optimizations() && textrepl_size_thresh != (size_t)-1 &&
475 475 !segvn_disable_textrepl) {
476 476 ulong_t i;
477 477 size_t hsz = svntr_hashtab_sz * sizeof (svntr_bucket_t);
478 478
479 479 svntr_cache = kmem_cache_create("svntr_cache",
480 480 sizeof (svntr_t), 0, svntr_cache_constructor, NULL,
481 481 NULL, NULL, NULL, 0);
482 482 svntr_hashtab = kmem_zalloc(hsz, KM_SLEEP);
483 483 for (i = 0; i < svntr_hashtab_sz; i++) {
484 484 mutex_init(&svntr_hashtab[i].tr_lock, NULL,
485 485 MUTEX_DEFAULT, NULL);
486 486 }
487 487 segvn_textrepl_max_bytes = ptob(physmem) /
488 488 segvn_textrepl_max_bytes_factor;
489 489 segvn_textrepl_stats = kmem_zalloc(NCPU *
490 490 sizeof (svntr_stats_t), KM_SLEEP);
491 491 sema_init(&segvn_trasync_sem, 0, NULL, SEMA_DEFAULT, NULL);
492 492 (void) thread_create(NULL, 0, segvn_trasync_thread,
493 493 NULL, 0, &p0, TS_RUN, minclsyspri);
494 494 }
495 495 #endif
496 496
497 497 if (!ISP2(segvn_pglock_comb_balign) ||
498 498 segvn_pglock_comb_balign < PAGESIZE) {
499 499 segvn_pglock_comb_balign = 1UL << 16; /* 64K */
500 500 }
501 501 segvn_pglock_comb_bshift = highbit(segvn_pglock_comb_balign) - 1;
502 502 segvn_pglock_comb_palign = btop(segvn_pglock_comb_balign);
503 503 }
504 504
505 505 #define SEGVN_PAGEIO ((void *)0x1)
506 506 #define SEGVN_NOPAGEIO ((void *)0x2)
507 507
508 508 static void
509 509 segvn_setvnode_mpss(vnode_t *vp)
510 510 {
511 511 int err;
512 512
513 513 ASSERT(vp->v_mpssdata == NULL ||
514 514 vp->v_mpssdata == SEGVN_PAGEIO ||
515 515 vp->v_mpssdata == SEGVN_NOPAGEIO);
516 516
517 517 if (vp->v_mpssdata == NULL) {
518 518 if (vn_vmpss_usepageio(vp)) {
519 519 err = VOP_PAGEIO(vp, (page_t *)NULL,
520 520 (u_offset_t)0, 0, 0, CRED(), NULL);
521 521 } else {
522 522 err = ENOSYS;
523 523 }
524 524 /*
525 525 * set v_mpssdata just once per vnode life
526 526 * so that it never changes.
527 527 */
528 528 mutex_enter(&vp->v_lock);
529 529 if (vp->v_mpssdata == NULL) {
530 530 if (err == EINVAL) {
531 531 vp->v_mpssdata = SEGVN_PAGEIO;
532 532 } else {
533 533 vp->v_mpssdata = SEGVN_NOPAGEIO;
534 534 }
535 535 }
536 536 mutex_exit(&vp->v_lock);
537 537 }
538 538 }
539 539
540 540 int
541 541 segvn_create(struct seg **segpp, void *argsp)
542 542 {
543 543 struct seg *seg = *segpp;
544 544 extern lgrp_mem_policy_t lgrp_mem_default_policy;
545 545 struct segvn_crargs *a = (struct segvn_crargs *)argsp;
546 546 struct segvn_data *svd;
547 547 size_t swresv = 0;
548 548 struct cred *cred;
549 549 struct anon_map *amp;
550 550 int error = 0;
551 551 size_t pgsz;
552 552 lgrp_mem_policy_t mpolicy = lgrp_mem_default_policy;
553 553 int use_rgn = 0;
554 554 int trok = 0;
555 555
556 556 ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
557 557
558 558 if (a->type != MAP_PRIVATE && a->type != MAP_SHARED) {
559 559 panic("segvn_create type");
560 560 /*NOTREACHED*/
561 561 }
562 562
563 563 /*
564 564 * Check arguments. If a shared anon structure is given then
565 565 * it is illegal to also specify a vp.
566 566 */
567 567 if (a->amp != NULL && a->vp != NULL) {
568 568 panic("segvn_create anon_map");
569 569 /*NOTREACHED*/
570 570 }
571 571
572 572 if (a->type == MAP_PRIVATE && (a->flags & MAP_TEXT) &&
573 573 a->vp != NULL && a->prot == (PROT_USER | PROT_READ | PROT_EXEC) &&
574 574 segvn_use_regions) {
575 575 use_rgn = 1;
576 576 }
577 577
578 578 /* MAP_NORESERVE on a MAP_SHARED segment is meaningless. */
579 579 if (a->type == MAP_SHARED)
580 580 a->flags &= ~MAP_NORESERVE;
581 581
582 582 if (a->szc != 0) {
583 583 if (segvn_lpg_disable != 0 || (a->szc == AS_MAP_NO_LPOOB) ||
584 584 (a->amp != NULL && a->type == MAP_PRIVATE) ||
585 585 (a->flags & MAP_NORESERVE) || seg->s_as == &kas) {
586 586 a->szc = 0;
587 587 } else {
588 588 if (a->szc > segvn_maxpgszc)
589 589 a->szc = segvn_maxpgszc;
590 590 pgsz = page_get_pagesize(a->szc);
591 591 if (!IS_P2ALIGNED(seg->s_base, pgsz) ||
592 592 !IS_P2ALIGNED(seg->s_size, pgsz)) {
593 593 a->szc = 0;
594 594 } else if (a->vp != NULL) {
595 595 if (IS_SWAPFSVP(a->vp) || VN_ISKAS(a->vp)) {
596 596 /*
597 597 * paranoid check.
598 598 * hat_page_demote() is not supported
599 599 * on swapfs pages.
600 600 */
601 601 a->szc = 0;
602 602 } else if (map_addr_vacalign_check(seg->s_base,
603 603 a->offset & PAGEMASK)) {
604 604 a->szc = 0;
605 605 }
606 606 } else if (a->amp != NULL) {
607 607 pgcnt_t anum = btopr(a->offset);
608 608 pgcnt_t pgcnt = page_get_pagecnt(a->szc);
609 609 if (!IS_P2ALIGNED(anum, pgcnt)) {
610 610 a->szc = 0;
611 611 }
612 612 }
613 613 }
614 614 }
615 615
616 616 /*
617 617 * If segment may need private pages, reserve them now.
618 618 */
619 619 if (!(a->flags & MAP_NORESERVE) && ((a->vp == NULL && a->amp == NULL) ||
620 620 (a->type == MAP_PRIVATE && (a->prot & PROT_WRITE)))) {
621 621 if (anon_resv_zone(seg->s_size,
622 622 seg->s_as->a_proc->p_zone) == 0)
623 623 return (EAGAIN);
624 624 swresv = seg->s_size;
625 625 TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
626 626 seg, swresv, 1);
627 627 }
628 628
629 629 /*
630 630 * Reserve any mapping structures that may be required.
631 631 *
632 632 * Don't do it for segments that may use regions. It's currently a
633 633 * noop in the hat implementations anyway.
634 634 */
635 635 if (!use_rgn) {
636 636 hat_map(seg->s_as->a_hat, seg->s_base, seg->s_size, HAT_MAP);
637 637 }
638 638
639 639 if (a->cred) {
640 640 cred = a->cred;
641 641 crhold(cred);
642 642 } else {
643 643 crhold(cred = CRED());
644 644 }
645 645
646 646 /* Inform the vnode of the new mapping */
647 647 if (a->vp != NULL) {
648 648 error = VOP_ADDMAP(a->vp, a->offset & PAGEMASK,
649 649 seg->s_as, seg->s_base, seg->s_size, a->prot,
650 650 a->maxprot, a->type, cred, NULL);
651 651 if (error) {
652 652 if (swresv != 0) {
653 653 anon_unresv_zone(swresv,
654 654 seg->s_as->a_proc->p_zone);
655 655 TRACE_3(TR_FAC_VM, TR_ANON_PROC,
656 656 "anon proc:%p %lu %u", seg, swresv, 0);
657 657 }
658 658 crfree(cred);
659 659 if (!use_rgn) {
660 660 hat_unload(seg->s_as->a_hat, seg->s_base,
661 661 seg->s_size, HAT_UNLOAD_UNMAP);
662 662 }
663 663 return (error);
664 664 }
665 665 /*
666 666 * svntr_hashtab will be NULL if we support shared regions.
667 667 */
668 668 trok = ((a->flags & MAP_TEXT) &&
669 669 (seg->s_size > textrepl_size_thresh ||
670 670 (a->flags & _MAP_TEXTREPL)) &&
671 671 lgrp_optimizations() && svntr_hashtab != NULL &&
672 672 a->type == MAP_PRIVATE && swresv == 0 &&
673 673 !(a->flags & MAP_NORESERVE) &&
674 674 seg->s_as != &kas && a->vp->v_type == VREG);
675 675
676 676 ASSERT(!trok || !use_rgn);
677 677 }
678 678
679 679 /*
680 680 * MAP_NORESERVE mappings don't count towards the VSZ of a process
681 681 * until we fault the pages in.
682 682 */
683 683 if ((a->vp == NULL || a->vp->v_type != VREG) &&
684 684 a->flags & MAP_NORESERVE) {
685 685 seg->s_as->a_resvsize -= seg->s_size;
686 686 }
687 687
688 688 /*
689 689 * If more than one segment in the address space, and they're adjacent
690 690 * virtually, try to concatenate them. Don't concatenate if an
691 691 * explicit anon_map structure was supplied (e.g., SystemV shared
692 692 * memory) or if we'll use text replication for this segment.
693 693 */
694 694 if (a->amp == NULL && !use_rgn && !trok) {
695 695 struct seg *pseg, *nseg;
696 696 struct segvn_data *psvd, *nsvd;
697 697 lgrp_mem_policy_t ppolicy, npolicy;
698 698 uint_t lgrp_mem_policy_flags = 0;
699 699
700 700 /*
701 701 * Memory policy flags (lgrp_mem_policy_flags) is valid when
702 702 * extending stack/heap segments.
703 703 */
704 704 if ((a->vp == NULL) && (a->type == MAP_PRIVATE) &&
705 705 !(a->flags & MAP_NORESERVE) && (seg->s_as != &kas)) {
706 706 lgrp_mem_policy_flags = a->lgrp_mem_policy_flags;
707 707 } else {
708 708 /*
709 709 * Get policy when not extending it from another segment
710 710 */
711 711 mpolicy = lgrp_mem_policy_default(seg->s_size, a->type);
712 712 }
713 713
714 714 /*
715 715 * First, try to concatenate the previous and new segments
716 716 */
717 717 pseg = AS_SEGPREV(seg->s_as, seg);
718 718 if (pseg != NULL &&
719 719 pseg->s_base + pseg->s_size == seg->s_base &&
720 720 pseg->s_ops == &segvn_ops) {
721 721 /*
722 722 * Get memory allocation policy from previous segment.
723 723 * When extension is specified (e.g. for heap) apply
724 724 * this policy to the new segment regardless of the
725 725 * outcome of segment concatenation. Extension occurs
726 726 * for non-default policy otherwise default policy is
727 727 * used and is based on extended segment size.
728 728 */
729 729 psvd = (struct segvn_data *)pseg->s_data;
730 730 ppolicy = psvd->policy_info.mem_policy;
731 731 if (lgrp_mem_policy_flags ==
732 732 LGRP_MP_FLAG_EXTEND_UP) {
733 733 if (ppolicy != lgrp_mem_default_policy) {
734 734 mpolicy = ppolicy;
735 735 } else {
736 736 mpolicy = lgrp_mem_policy_default(
737 737 pseg->s_size + seg->s_size,
738 738 a->type);
739 739 }
740 740 }
741 741
742 742 if (mpolicy == ppolicy &&
743 743 (pseg->s_size + seg->s_size <=
744 744 segvn_comb_thrshld || psvd->amp == NULL) &&
745 745 segvn_extend_prev(pseg, seg, a, swresv) == 0) {
746 746 /*
747 747 * success! now try to concatenate
748 748 * with following seg
749 749 */
750 750 crfree(cred);
751 751 nseg = AS_SEGNEXT(pseg->s_as, pseg);
752 752 if (nseg != NULL &&
753 753 nseg != pseg &&
754 754 nseg->s_ops == &segvn_ops &&
755 755 pseg->s_base + pseg->s_size ==
756 756 nseg->s_base)
757 757 (void) segvn_concat(pseg, nseg, 0);
758 758 ASSERT(pseg->s_szc == 0 ||
759 759 (a->szc == pseg->s_szc &&
760 760 IS_P2ALIGNED(pseg->s_base, pgsz) &&
761 761 IS_P2ALIGNED(pseg->s_size, pgsz)));
762 762 /*
763 763 * Communicate out the newly concatenated
764 764 * segment as part of the result.
765 765 */
766 766 *segpp = pseg;
767 767 return (0);
768 768 }
769 769 }
770 770
771 771 /*
772 772 * Failed, so try to concatenate with following seg
773 773 */
774 774 nseg = AS_SEGNEXT(seg->s_as, seg);
775 775 if (nseg != NULL &&
776 776 seg->s_base + seg->s_size == nseg->s_base &&
777 777 nseg->s_ops == &segvn_ops) {
778 778 /*
779 779 * Get memory allocation policy from next segment.
780 780 * When extension is specified (e.g. for stack) apply
781 781 * this policy to the new segment regardless of the
782 782 * outcome of segment concatenation. Extension occurs
783 783 * for non-default policy otherwise default policy is
784 784 * used and is based on extended segment size.
785 785 */
786 786 nsvd = (struct segvn_data *)nseg->s_data;
787 787 npolicy = nsvd->policy_info.mem_policy;
788 788 if (lgrp_mem_policy_flags ==
789 789 LGRP_MP_FLAG_EXTEND_DOWN) {
790 790 if (npolicy != lgrp_mem_default_policy) {
791 791 mpolicy = npolicy;
792 792 } else {
793 793 mpolicy = lgrp_mem_policy_default(
794 794 nseg->s_size + seg->s_size,
795 795 a->type);
796 796 }
797 797 }
798 798
799 799 if (mpolicy == npolicy &&
800 800 segvn_extend_next(seg, nseg, a, swresv) == 0) {
801 801 crfree(cred);
802 802 ASSERT(nseg->s_szc == 0 ||
803 803 (a->szc == nseg->s_szc &&
804 804 IS_P2ALIGNED(nseg->s_base, pgsz) &&
805 805 IS_P2ALIGNED(nseg->s_size, pgsz)));
806 806 /*
807 807 * Communicate out the newly concatenated
808 808 * segment as part of the result.
809 809 */
810 810 *segpp = nseg;
811 811 return (0);
812 812 }
813 813 }
814 814 }
815 815
816 816 if (a->vp != NULL) {
817 817 VN_HOLD(a->vp);
818 818 if (a->type == MAP_SHARED)
819 819 lgrp_shm_policy_init(NULL, a->vp);
820 820 }
821 821 svd = kmem_cache_alloc(segvn_cache, KM_SLEEP);
822 822
823 823 seg->s_ops = &segvn_ops;
824 824 seg->s_data = (void *)svd;
825 825 seg->s_szc = a->szc;
826 826
827 827 svd->seg = seg;
828 828 svd->vp = a->vp;
829 829 /*
830 830 * Anonymous mappings have no backing file so the offset is meaningless.
831 831 */
832 832 svd->offset = a->vp ? (a->offset & PAGEMASK) : 0;
833 833 svd->prot = a->prot;
834 834 svd->maxprot = a->maxprot;
835 835 svd->pageprot = 0;
836 836 svd->type = a->type;
837 837 svd->vpage = NULL;
838 838 svd->cred = cred;
839 839 svd->advice = MADV_NORMAL;
840 840 svd->pageadvice = 0;
841 841 svd->flags = (ushort_t)a->flags;
842 842 svd->softlockcnt = 0;
843 843 svd->softlockcnt_sbase = 0;
844 844 svd->softlockcnt_send = 0;
845 845 svd->svn_inz = 0;
846 846 svd->rcookie = HAT_INVALID_REGION_COOKIE;
847 847 svd->pageswap = 0;
848 848
849 849 if (a->szc != 0 && a->vp != NULL) {
850 850 segvn_setvnode_mpss(a->vp);
851 851 }
852 852 if (svd->type == MAP_SHARED && svd->vp != NULL &&
853 853 (svd->vp->v_flag & VVMEXEC) && (svd->prot & PROT_WRITE)) {
854 854 ASSERT(vn_is_mapped(svd->vp, V_WRITE));
855 855 segvn_inval_trcache(svd->vp);
856 856 }
857 857
858 858 amp = a->amp;
859 859 if ((svd->amp = amp) == NULL) {
860 860 svd->anon_index = 0;
861 861 if (svd->type == MAP_SHARED) {
862 862 svd->swresv = 0;
863 863 /*
864 864 * Shared mappings to a vp need no other setup.
865 865 * If we have a shared mapping to an anon_map object
866 866 * which hasn't been allocated yet, allocate the
867 867 * struct now so that it will be properly shared
868 868 * by remembering the swap reservation there.
869 869 */
870 870 if (a->vp == NULL) {
871 871 svd->amp = anonmap_alloc(seg->s_size, swresv,
872 872 ANON_SLEEP);
873 873 svd->amp->a_szc = seg->s_szc;
874 874 }
875 875 } else {
876 876 /*
877 877 * Private mapping (with or without a vp).
878 878 * Allocate anon_map when needed.
879 879 */
880 880 svd->swresv = swresv;
881 881 }
882 882 } else {
883 883 pgcnt_t anon_num;
884 884
885 885 /*
886 886 * Mapping to an existing anon_map structure without a vp.
887 887 * For now we will insure that the segment size isn't larger
888 888 * than the size - offset gives us. Later on we may wish to
889 889 * have the anon array dynamically allocated itself so that
890 890 * we don't always have to allocate all the anon pointer slots.
891 891 * This of course involves adding extra code to check that we
892 892 * aren't trying to use an anon pointer slot beyond the end
893 893 * of the currently allocated anon array.
894 894 */
895 895 if ((amp->size - a->offset) < seg->s_size) {
896 896 panic("segvn_create anon_map size");
897 897 /*NOTREACHED*/
898 898 }
899 899
900 900 anon_num = btopr(a->offset);
901 901
902 902 if (a->type == MAP_SHARED) {
903 903 /*
904 904 * SHARED mapping to a given anon_map.
905 905 */
906 906 ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
907 907 amp->refcnt++;
908 908 if (a->szc > amp->a_szc) {
909 909 amp->a_szc = a->szc;
910 910 }
911 911 ANON_LOCK_EXIT(&->a_rwlock);
912 912 svd->anon_index = anon_num;
913 913 svd->swresv = 0;
914 914 } else {
915 915 /*
916 916 * PRIVATE mapping to a given anon_map.
917 917 * Make sure that all the needed anon
918 918 * structures are created (so that we will
919 919 * share the underlying pages if nothing
920 920 * is written by this mapping) and then
921 921 * duplicate the anon array as is done
922 922 * when a privately mapped segment is dup'ed.
923 923 */
924 924 struct anon *ap;
925 925 caddr_t addr;
926 926 caddr_t eaddr;
927 927 ulong_t anon_idx;
928 928 int hat_flag = HAT_LOAD;
929 929
930 930 if (svd->flags & MAP_TEXT) {
931 931 hat_flag |= HAT_LOAD_TEXT;
932 932 }
933 933
934 934 svd->amp = anonmap_alloc(seg->s_size, 0, ANON_SLEEP);
935 935 svd->amp->a_szc = seg->s_szc;
936 936 svd->anon_index = 0;
937 937 svd->swresv = swresv;
938 938
939 939 /*
940 940 * Prevent 2 threads from allocating anon
941 941 * slots simultaneously.
942 942 */
943 943 ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
944 944 eaddr = seg->s_base + seg->s_size;
945 945
946 946 for (anon_idx = anon_num, addr = seg->s_base;
947 947 addr < eaddr; addr += PAGESIZE, anon_idx++) {
948 948 page_t *pp;
949 949
950 950 if ((ap = anon_get_ptr(amp->ahp,
951 951 anon_idx)) != NULL)
952 952 continue;
953 953
954 954 /*
955 955 * Allocate the anon struct now.
956 956 * Might as well load up translation
957 957 * to the page while we're at it...
958 958 */
959 959 pp = anon_zero(seg, addr, &ap, cred);
960 960 if (ap == NULL || pp == NULL) {
961 961 panic("segvn_create anon_zero");
962 962 /*NOTREACHED*/
963 963 }
964 964
965 965 /*
966 966 * Re-acquire the anon_map lock and
967 967 * initialize the anon array entry.
968 968 */
969 969 ASSERT(anon_get_ptr(amp->ahp,
970 970 anon_idx) == NULL);
971 971 (void) anon_set_ptr(amp->ahp, anon_idx, ap,
972 972 ANON_SLEEP);
973 973
974 974 ASSERT(seg->s_szc == 0);
975 975 ASSERT(!IS_VMODSORT(pp->p_vnode));
976 976
977 977 ASSERT(use_rgn == 0);
978 978 hat_memload(seg->s_as->a_hat, addr, pp,
979 979 svd->prot & ~PROT_WRITE, hat_flag);
980 980
981 981 page_unlock(pp);
982 982 }
983 983 ASSERT(seg->s_szc == 0);
984 984 anon_dup(amp->ahp, anon_num, svd->amp->ahp,
985 985 0, seg->s_size);
986 986 ANON_LOCK_EXIT(&->a_rwlock);
987 987 }
988 988 }
989 989
990 990 /*
991 991 * Set default memory allocation policy for segment
992 992 *
993 993 * Always set policy for private memory at least for initialization
994 994 * even if this is a shared memory segment
995 995 */
996 996 (void) lgrp_privm_policy_set(mpolicy, &svd->policy_info, seg->s_size);
997 997
998 998 if (svd->type == MAP_SHARED)
999 999 (void) lgrp_shm_policy_set(mpolicy, svd->amp, svd->anon_index,
1000 1000 svd->vp, svd->offset, seg->s_size);
1001 1001
1002 1002 if (use_rgn) {
1003 1003 ASSERT(!trok);
1004 1004 ASSERT(svd->amp == NULL);
1005 1005 svd->rcookie = hat_join_region(seg->s_as->a_hat, seg->s_base,
1006 1006 seg->s_size, (void *)svd->vp, svd->offset, svd->prot,
1007 1007 (uchar_t)seg->s_szc, segvn_hat_rgn_unload_callback,
1008 1008 HAT_REGION_TEXT);
1009 1009 }
1010 1010
1011 1011 ASSERT(!trok || !(svd->prot & PROT_WRITE));
1012 1012 svd->tr_state = trok ? SEGVN_TR_INIT : SEGVN_TR_OFF;
1013 1013
1014 1014 return (0);
1015 1015 }
1016 1016
1017 1017 /*
1018 1018 * Concatenate two existing segments, if possible.
1019 1019 * Return 0 on success, -1 if two segments are not compatible
1020 1020 * or -2 on memory allocation failure.
1021 1021 * If amp_cat == 1 then try and concat segments with anon maps
1022 1022 */
1023 1023 static int
1024 1024 segvn_concat(struct seg *seg1, struct seg *seg2, int amp_cat)
1025 1025 {
1026 1026 struct segvn_data *svd1 = seg1->s_data;
1027 1027 struct segvn_data *svd2 = seg2->s_data;
1028 1028 struct anon_map *amp1 = svd1->amp;
1029 1029 struct anon_map *amp2 = svd2->amp;
1030 1030 struct vpage *vpage1 = svd1->vpage;
1031 1031 struct vpage *vpage2 = svd2->vpage, *nvpage = NULL;
1032 1032 size_t size, nvpsize;
1033 1033 pgcnt_t npages1, npages2;
1034 1034
1035 1035 ASSERT(seg1->s_as && seg2->s_as && seg1->s_as == seg2->s_as);
1036 1036 ASSERT(AS_WRITE_HELD(seg1->s_as));
1037 1037 ASSERT(seg1->s_ops == seg2->s_ops);
1038 1038
1039 1039 if (HAT_IS_REGION_COOKIE_VALID(svd1->rcookie) ||
1040 1040 HAT_IS_REGION_COOKIE_VALID(svd2->rcookie)) {
1041 1041 return (-1);
1042 1042 }
1043 1043
1044 1044 /* both segments exist, try to merge them */
1045 1045 #define incompat(x) (svd1->x != svd2->x)
1046 1046 if (incompat(vp) || incompat(maxprot) ||
1047 1047 (!svd1->pageadvice && !svd2->pageadvice && incompat(advice)) ||
1048 1048 (!svd1->pageprot && !svd2->pageprot && incompat(prot)) ||
1049 1049 incompat(type) || incompat(cred) || incompat(flags) ||
1050 1050 seg1->s_szc != seg2->s_szc || incompat(policy_info.mem_policy) ||
1051 1051 (svd2->softlockcnt > 0) || svd1->softlockcnt_send > 0)
1052 1052 return (-1);
1053 1053 #undef incompat
1054 1054
1055 1055 /*
1056 1056 * vp == NULL implies zfod, offset doesn't matter
1057 1057 */
1058 1058 if (svd1->vp != NULL &&
1059 1059 svd1->offset + seg1->s_size != svd2->offset) {
1060 1060 return (-1);
1061 1061 }
1062 1062
1063 1063 /*
1064 1064 * Don't concatenate if either segment uses text replication.
1065 1065 */
1066 1066 if (svd1->tr_state != SEGVN_TR_OFF || svd2->tr_state != SEGVN_TR_OFF) {
1067 1067 return (-1);
1068 1068 }
1069 1069
1070 1070 /*
1071 1071 * Fail early if we're not supposed to concatenate
1072 1072 * segments with non NULL amp.
1073 1073 */
1074 1074 if (amp_cat == 0 && (amp1 != NULL || amp2 != NULL)) {
1075 1075 return (-1);
1076 1076 }
1077 1077
1078 1078 if (svd1->vp == NULL && svd1->type == MAP_SHARED) {
1079 1079 if (amp1 != amp2) {
1080 1080 return (-1);
1081 1081 }
1082 1082 if (amp1 != NULL && svd1->anon_index + btop(seg1->s_size) !=
1083 1083 svd2->anon_index) {
1084 1084 return (-1);
1085 1085 }
1086 1086 ASSERT(amp1 == NULL || amp1->refcnt >= 2);
1087 1087 }
1088 1088
1089 1089 /*
1090 1090 * If either seg has vpages, create a new merged vpage array.
1091 1091 */
1092 1092 if (vpage1 != NULL || vpage2 != NULL) {
1093 1093 struct vpage *vp, *evp;
1094 1094
1095 1095 npages1 = seg_pages(seg1);
1096 1096 npages2 = seg_pages(seg2);
1097 1097 nvpsize = vpgtob(npages1 + npages2);
1098 1098
1099 1099 if ((nvpage = kmem_zalloc(nvpsize, KM_NOSLEEP)) == NULL) {
1100 1100 return (-2);
1101 1101 }
1102 1102
1103 1103 if (vpage1 != NULL) {
1104 1104 bcopy(vpage1, nvpage, vpgtob(npages1));
1105 1105 } else {
1106 1106 evp = nvpage + npages1;
1107 1107 for (vp = nvpage; vp < evp; vp++) {
1108 1108 VPP_SETPROT(vp, svd1->prot);
1109 1109 VPP_SETADVICE(vp, svd1->advice);
1110 1110 }
1111 1111 }
1112 1112
1113 1113 if (vpage2 != NULL) {
1114 1114 bcopy(vpage2, nvpage + npages1, vpgtob(npages2));
1115 1115 } else {
1116 1116 evp = nvpage + npages1 + npages2;
1117 1117 for (vp = nvpage + npages1; vp < evp; vp++) {
1118 1118 VPP_SETPROT(vp, svd2->prot);
1119 1119 VPP_SETADVICE(vp, svd2->advice);
1120 1120 }
1121 1121 }
1122 1122
1123 1123 if (svd2->pageswap && (!svd1->pageswap && svd1->swresv)) {
1124 1124 ASSERT(svd1->swresv == seg1->s_size);
1125 1125 ASSERT(!(svd1->flags & MAP_NORESERVE));
1126 1126 ASSERT(!(svd2->flags & MAP_NORESERVE));
1127 1127 evp = nvpage + npages1;
1128 1128 for (vp = nvpage; vp < evp; vp++) {
1129 1129 VPP_SETSWAPRES(vp);
1130 1130 }
1131 1131 }
1132 1132
1133 1133 if (svd1->pageswap && (!svd2->pageswap && svd2->swresv)) {
1134 1134 ASSERT(svd2->swresv == seg2->s_size);
1135 1135 ASSERT(!(svd1->flags & MAP_NORESERVE));
1136 1136 ASSERT(!(svd2->flags & MAP_NORESERVE));
1137 1137 vp = nvpage + npages1;
1138 1138 evp = vp + npages2;
1139 1139 for (; vp < evp; vp++) {
1140 1140 VPP_SETSWAPRES(vp);
1141 1141 }
1142 1142 }
1143 1143 }
1144 1144 ASSERT((vpage1 != NULL || vpage2 != NULL) ||
1145 1145 (svd1->pageswap == 0 && svd2->pageswap == 0));
1146 1146
1147 1147 /*
1148 1148 * If either segment has private pages, create a new merged anon
1149 1149 * array. If mergeing shared anon segments just decrement anon map's
1150 1150 * refcnt.
1151 1151 */
1152 1152 if (amp1 != NULL && svd1->type == MAP_SHARED) {
1153 1153 ASSERT(amp1 == amp2 && svd1->vp == NULL);
1154 1154 ANON_LOCK_ENTER(&1->a_rwlock, RW_WRITER);
1155 1155 ASSERT(amp1->refcnt >= 2);
1156 1156 amp1->refcnt--;
1157 1157 ANON_LOCK_EXIT(&1->a_rwlock);
1158 1158 svd2->amp = NULL;
1159 1159 } else if (amp1 != NULL || amp2 != NULL) {
1160 1160 struct anon_hdr *nahp;
1161 1161 struct anon_map *namp = NULL;
1162 1162 size_t asize;
1163 1163
1164 1164 ASSERT(svd1->type == MAP_PRIVATE);
1165 1165
1166 1166 asize = seg1->s_size + seg2->s_size;
1167 1167 if ((nahp = anon_create(btop(asize), ANON_NOSLEEP)) == NULL) {
1168 1168 if (nvpage != NULL) {
1169 1169 kmem_free(nvpage, nvpsize);
1170 1170 }
1171 1171 return (-2);
1172 1172 }
1173 1173 if (amp1 != NULL) {
1174 1174 /*
1175 1175 * XXX anon rwlock is not really needed because
1176 1176 * this is a private segment and we are writers.
1177 1177 */
1178 1178 ANON_LOCK_ENTER(&1->a_rwlock, RW_WRITER);
1179 1179 ASSERT(amp1->refcnt == 1);
1180 1180 if (anon_copy_ptr(amp1->ahp, svd1->anon_index,
1181 1181 nahp, 0, btop(seg1->s_size), ANON_NOSLEEP)) {
1182 1182 anon_release(nahp, btop(asize));
1183 1183 ANON_LOCK_EXIT(&1->a_rwlock);
1184 1184 if (nvpage != NULL) {
1185 1185 kmem_free(nvpage, nvpsize);
1186 1186 }
1187 1187 return (-2);
1188 1188 }
1189 1189 }
1190 1190 if (amp2 != NULL) {
1191 1191 ANON_LOCK_ENTER(&2->a_rwlock, RW_WRITER);
1192 1192 ASSERT(amp2->refcnt == 1);
1193 1193 if (anon_copy_ptr(amp2->ahp, svd2->anon_index,
1194 1194 nahp, btop(seg1->s_size), btop(seg2->s_size),
1195 1195 ANON_NOSLEEP)) {
1196 1196 anon_release(nahp, btop(asize));
1197 1197 ANON_LOCK_EXIT(&2->a_rwlock);
1198 1198 if (amp1 != NULL) {
1199 1199 ANON_LOCK_EXIT(&1->a_rwlock);
1200 1200 }
1201 1201 if (nvpage != NULL) {
1202 1202 kmem_free(nvpage, nvpsize);
1203 1203 }
1204 1204 return (-2);
1205 1205 }
1206 1206 }
1207 1207 if (amp1 != NULL) {
1208 1208 namp = amp1;
1209 1209 anon_release(amp1->ahp, btop(amp1->size));
1210 1210 }
1211 1211 if (amp2 != NULL) {
1212 1212 if (namp == NULL) {
1213 1213 ASSERT(amp1 == NULL);
1214 1214 namp = amp2;
1215 1215 anon_release(amp2->ahp, btop(amp2->size));
1216 1216 } else {
1217 1217 amp2->refcnt--;
1218 1218 ANON_LOCK_EXIT(&2->a_rwlock);
1219 1219 anonmap_free(amp2);
1220 1220 }
1221 1221 svd2->amp = NULL; /* needed for seg_free */
1222 1222 }
1223 1223 namp->ahp = nahp;
1224 1224 namp->size = asize;
1225 1225 svd1->amp = namp;
1226 1226 svd1->anon_index = 0;
1227 1227 ANON_LOCK_EXIT(&namp->a_rwlock);
1228 1228 }
1229 1229 /*
1230 1230 * Now free the old vpage structures.
1231 1231 */
1232 1232 if (nvpage != NULL) {
1233 1233 if (vpage1 != NULL) {
1234 1234 kmem_free(vpage1, vpgtob(npages1));
1235 1235 }
1236 1236 if (vpage2 != NULL) {
1237 1237 svd2->vpage = NULL;
1238 1238 kmem_free(vpage2, vpgtob(npages2));
1239 1239 }
1240 1240 if (svd2->pageprot) {
1241 1241 svd1->pageprot = 1;
1242 1242 }
1243 1243 if (svd2->pageadvice) {
1244 1244 svd1->pageadvice = 1;
1245 1245 }
1246 1246 if (svd2->pageswap) {
1247 1247 svd1->pageswap = 1;
1248 1248 }
1249 1249 svd1->vpage = nvpage;
1250 1250 }
1251 1251
1252 1252 /* all looks ok, merge segments */
1253 1253 svd1->swresv += svd2->swresv;
1254 1254 svd2->swresv = 0; /* so seg_free doesn't release swap space */
1255 1255 size = seg2->s_size;
1256 1256 seg_free(seg2);
1257 1257 seg1->s_size += size;
1258 1258 return (0);
1259 1259 }
1260 1260
1261 1261 /*
1262 1262 * Extend the previous segment (seg1) to include the
1263 1263 * new segment (seg2 + a), if possible.
1264 1264 * Return 0 on success.
1265 1265 */
1266 1266 static int
1267 1267 segvn_extend_prev(struct seg *seg1, struct seg *seg2, struct segvn_crargs *a,
1268 1268 size_t swresv)
1269 1269 {
1270 1270 struct segvn_data *svd1 = (struct segvn_data *)seg1->s_data;
1271 1271 size_t size;
1272 1272 struct anon_map *amp1;
1273 1273 struct vpage *new_vpage;
1274 1274
1275 1275 /*
1276 1276 * We don't need any segment level locks for "segvn" data
1277 1277 * since the address space is "write" locked.
1278 1278 */
1279 1279 ASSERT(seg1->s_as && AS_WRITE_HELD(seg1->s_as));
1280 1280
1281 1281 if (HAT_IS_REGION_COOKIE_VALID(svd1->rcookie)) {
1282 1282 return (-1);
1283 1283 }
1284 1284
1285 1285 /* second segment is new, try to extend first */
1286 1286 /* XXX - should also check cred */
1287 1287 if (svd1->vp != a->vp || svd1->maxprot != a->maxprot ||
1288 1288 (!svd1->pageprot && (svd1->prot != a->prot)) ||
1289 1289 svd1->type != a->type || svd1->flags != a->flags ||
1290 1290 seg1->s_szc != a->szc || svd1->softlockcnt_send > 0)
1291 1291 return (-1);
1292 1292
1293 1293 /* vp == NULL implies zfod, offset doesn't matter */
1294 1294 if (svd1->vp != NULL &&
1295 1295 svd1->offset + seg1->s_size != (a->offset & PAGEMASK))
1296 1296 return (-1);
1297 1297
1298 1298 if (svd1->tr_state != SEGVN_TR_OFF) {
1299 1299 return (-1);
1300 1300 }
1301 1301
1302 1302 amp1 = svd1->amp;
1303 1303 if (amp1) {
1304 1304 pgcnt_t newpgs;
1305 1305
1306 1306 /*
1307 1307 * Segment has private pages, can data structures
1308 1308 * be expanded?
1309 1309 *
1310 1310 * Acquire the anon_map lock to prevent it from changing,
1311 1311 * if it is shared. This ensures that the anon_map
1312 1312 * will not change while a thread which has a read/write
1313 1313 * lock on an address space references it.
1314 1314 * XXX - Don't need the anon_map lock at all if "refcnt"
1315 1315 * is 1.
1316 1316 *
1317 1317 * Can't grow a MAP_SHARED segment with an anonmap because
1318 1318 * there may be existing anon slots where we want to extend
1319 1319 * the segment and we wouldn't know what to do with them
1320 1320 * (e.g., for tmpfs right thing is to just leave them there,
1321 1321 * for /dev/zero they should be cleared out).
1322 1322 */
1323 1323 if (svd1->type == MAP_SHARED)
1324 1324 return (-1);
1325 1325
1326 1326 ANON_LOCK_ENTER(&1->a_rwlock, RW_WRITER);
1327 1327 if (amp1->refcnt > 1) {
1328 1328 ANON_LOCK_EXIT(&1->a_rwlock);
1329 1329 return (-1);
1330 1330 }
1331 1331 newpgs = anon_grow(amp1->ahp, &svd1->anon_index,
1332 1332 btop(seg1->s_size), btop(seg2->s_size), ANON_NOSLEEP);
1333 1333
1334 1334 if (newpgs == 0) {
1335 1335 ANON_LOCK_EXIT(&1->a_rwlock);
1336 1336 return (-1);
1337 1337 }
1338 1338 amp1->size = ptob(newpgs);
1339 1339 ANON_LOCK_EXIT(&1->a_rwlock);
1340 1340 }
1341 1341 if (svd1->vpage != NULL) {
1342 1342 struct vpage *vp, *evp;
1343 1343 new_vpage =
1344 1344 kmem_zalloc(vpgtob(seg_pages(seg1) + seg_pages(seg2)),
1345 1345 KM_NOSLEEP);
1346 1346 if (new_vpage == NULL)
1347 1347 return (-1);
1348 1348 bcopy(svd1->vpage, new_vpage, vpgtob(seg_pages(seg1)));
1349 1349 kmem_free(svd1->vpage, vpgtob(seg_pages(seg1)));
1350 1350 svd1->vpage = new_vpage;
1351 1351
1352 1352 vp = new_vpage + seg_pages(seg1);
1353 1353 evp = vp + seg_pages(seg2);
1354 1354 for (; vp < evp; vp++)
1355 1355 VPP_SETPROT(vp, a->prot);
1356 1356 if (svd1->pageswap && swresv) {
1357 1357 ASSERT(!(svd1->flags & MAP_NORESERVE));
1358 1358 ASSERT(swresv == seg2->s_size);
1359 1359 vp = new_vpage + seg_pages(seg1);
1360 1360 for (; vp < evp; vp++) {
1361 1361 VPP_SETSWAPRES(vp);
1362 1362 }
1363 1363 }
1364 1364 }
1365 1365 ASSERT(svd1->vpage != NULL || svd1->pageswap == 0);
1366 1366 size = seg2->s_size;
1367 1367 seg_free(seg2);
1368 1368 seg1->s_size += size;
1369 1369 svd1->swresv += swresv;
1370 1370 if (svd1->pageprot && (a->prot & PROT_WRITE) &&
1371 1371 svd1->type == MAP_SHARED && svd1->vp != NULL &&
1372 1372 (svd1->vp->v_flag & VVMEXEC)) {
1373 1373 ASSERT(vn_is_mapped(svd1->vp, V_WRITE));
1374 1374 segvn_inval_trcache(svd1->vp);
1375 1375 }
1376 1376 return (0);
1377 1377 }
1378 1378
1379 1379 /*
1380 1380 * Extend the next segment (seg2) to include the
1381 1381 * new segment (seg1 + a), if possible.
1382 1382 * Return 0 on success.
1383 1383 */
1384 1384 static int
1385 1385 segvn_extend_next(struct seg *seg1, struct seg *seg2, struct segvn_crargs *a,
1386 1386 size_t swresv)
1387 1387 {
1388 1388 struct segvn_data *svd2 = (struct segvn_data *)seg2->s_data;
1389 1389 size_t size;
1390 1390 struct anon_map *amp2;
1391 1391 struct vpage *new_vpage;
1392 1392
1393 1393 /*
1394 1394 * We don't need any segment level locks for "segvn" data
1395 1395 * since the address space is "write" locked.
1396 1396 */
1397 1397 ASSERT(seg2->s_as && AS_WRITE_HELD(seg2->s_as));
1398 1398
1399 1399 if (HAT_IS_REGION_COOKIE_VALID(svd2->rcookie)) {
1400 1400 return (-1);
1401 1401 }
1402 1402
1403 1403 /* first segment is new, try to extend second */
1404 1404 /* XXX - should also check cred */
1405 1405 if (svd2->vp != a->vp || svd2->maxprot != a->maxprot ||
1406 1406 (!svd2->pageprot && (svd2->prot != a->prot)) ||
1407 1407 svd2->type != a->type || svd2->flags != a->flags ||
1408 1408 seg2->s_szc != a->szc || svd2->softlockcnt_sbase > 0)
1409 1409 return (-1);
1410 1410 /* vp == NULL implies zfod, offset doesn't matter */
1411 1411 if (svd2->vp != NULL &&
1412 1412 (a->offset & PAGEMASK) + seg1->s_size != svd2->offset)
1413 1413 return (-1);
1414 1414
1415 1415 if (svd2->tr_state != SEGVN_TR_OFF) {
1416 1416 return (-1);
1417 1417 }
1418 1418
1419 1419 amp2 = svd2->amp;
1420 1420 if (amp2) {
1421 1421 pgcnt_t newpgs;
1422 1422
1423 1423 /*
1424 1424 * Segment has private pages, can data structures
1425 1425 * be expanded?
1426 1426 *
1427 1427 * Acquire the anon_map lock to prevent it from changing,
1428 1428 * if it is shared. This ensures that the anon_map
1429 1429 * will not change while a thread which has a read/write
1430 1430 * lock on an address space references it.
1431 1431 *
1432 1432 * XXX - Don't need the anon_map lock at all if "refcnt"
1433 1433 * is 1.
1434 1434 */
1435 1435 if (svd2->type == MAP_SHARED)
1436 1436 return (-1);
1437 1437
1438 1438 ANON_LOCK_ENTER(&2->a_rwlock, RW_WRITER);
1439 1439 if (amp2->refcnt > 1) {
1440 1440 ANON_LOCK_EXIT(&2->a_rwlock);
1441 1441 return (-1);
1442 1442 }
1443 1443 newpgs = anon_grow(amp2->ahp, &svd2->anon_index,
1444 1444 btop(seg2->s_size), btop(seg1->s_size),
1445 1445 ANON_NOSLEEP | ANON_GROWDOWN);
1446 1446
1447 1447 if (newpgs == 0) {
1448 1448 ANON_LOCK_EXIT(&2->a_rwlock);
1449 1449 return (-1);
1450 1450 }
1451 1451 amp2->size = ptob(newpgs);
1452 1452 ANON_LOCK_EXIT(&2->a_rwlock);
1453 1453 }
1454 1454 if (svd2->vpage != NULL) {
1455 1455 struct vpage *vp, *evp;
1456 1456 new_vpage =
1457 1457 kmem_zalloc(vpgtob(seg_pages(seg1) + seg_pages(seg2)),
1458 1458 KM_NOSLEEP);
1459 1459 if (new_vpage == NULL) {
1460 1460 /* Not merging segments so adjust anon_index back */
1461 1461 if (amp2)
1462 1462 svd2->anon_index += seg_pages(seg1);
1463 1463 return (-1);
1464 1464 }
1465 1465 bcopy(svd2->vpage, new_vpage + seg_pages(seg1),
1466 1466 vpgtob(seg_pages(seg2)));
1467 1467 kmem_free(svd2->vpage, vpgtob(seg_pages(seg2)));
1468 1468 svd2->vpage = new_vpage;
1469 1469
1470 1470 vp = new_vpage;
1471 1471 evp = vp + seg_pages(seg1);
1472 1472 for (; vp < evp; vp++)
1473 1473 VPP_SETPROT(vp, a->prot);
1474 1474 if (svd2->pageswap && swresv) {
1475 1475 ASSERT(!(svd2->flags & MAP_NORESERVE));
1476 1476 ASSERT(swresv == seg1->s_size);
1477 1477 vp = new_vpage;
1478 1478 for (; vp < evp; vp++) {
1479 1479 VPP_SETSWAPRES(vp);
1480 1480 }
1481 1481 }
1482 1482 }
1483 1483 ASSERT(svd2->vpage != NULL || svd2->pageswap == 0);
1484 1484 size = seg1->s_size;
1485 1485 seg_free(seg1);
1486 1486 seg2->s_size += size;
1487 1487 seg2->s_base -= size;
1488 1488 svd2->offset -= size;
1489 1489 svd2->swresv += swresv;
1490 1490 if (svd2->pageprot && (a->prot & PROT_WRITE) &&
1491 1491 svd2->type == MAP_SHARED && svd2->vp != NULL &&
1492 1492 (svd2->vp->v_flag & VVMEXEC)) {
1493 1493 ASSERT(vn_is_mapped(svd2->vp, V_WRITE));
1494 1494 segvn_inval_trcache(svd2->vp);
1495 1495 }
1496 1496 return (0);
1497 1497 }
1498 1498
1499 1499 /*
1500 1500 * Duplicate all the pages in the segment. This may break COW sharing for a
1501 1501 * given page. If the page is marked with inherit zero set, then instead of
1502 1502 * duplicating the page, we zero the page.
1503 1503 */
1504 1504 static int
1505 1505 segvn_dup_pages(struct seg *seg, struct seg *newseg)
1506 1506 {
1507 1507 int error;
1508 1508 uint_t prot;
1509 1509 page_t *pp;
1510 1510 struct anon *ap, *newap;
1511 1511 size_t i;
1512 1512 caddr_t addr;
1513 1513
1514 1514 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
1515 1515 struct segvn_data *newsvd = (struct segvn_data *)newseg->s_data;
1516 1516 ulong_t old_idx = svd->anon_index;
1517 1517 ulong_t new_idx = 0;
1518 1518
1519 1519 i = btopr(seg->s_size);
1520 1520 addr = seg->s_base;
1521 1521
1522 1522 /*
1523 1523 * XXX break cow sharing using PAGESIZE
1524 1524 * pages. They will be relocated into larger
1525 1525 * pages at fault time.
1526 1526 */
1527 1527 while (i-- > 0) {
1528 1528 if ((ap = anon_get_ptr(svd->amp->ahp, old_idx)) != NULL) {
1529 1529 struct vpage *vpp;
1530 1530
1531 1531 vpp = &svd->vpage[seg_page(seg, addr)];
1532 1532
1533 1533 /*
1534 1534 * prot need not be computed below 'cause anon_private
1535 1535 * is going to ignore it anyway as child doesn't inherit
1536 1536 * pagelock from parent.
1537 1537 */
1538 1538 prot = svd->pageprot ? VPP_PROT(vpp) : svd->prot;
1539 1539
1540 1540 /*
1541 1541 * Check whether we should zero this or dup it.
1542 1542 */
1543 1543 if (svd->svn_inz == SEGVN_INZ_ALL ||
1544 1544 (svd->svn_inz == SEGVN_INZ_VPP &&
1545 1545 VPP_ISINHZERO(vpp))) {
1546 1546 pp = anon_zero(newseg, addr, &newap,
1547 1547 newsvd->cred);
1548 1548 } else {
1549 1549 page_t *anon_pl[1+1];
1550 1550 uint_t vpprot;
1551 1551 error = anon_getpage(&ap, &vpprot, anon_pl,
1552 1552 PAGESIZE, seg, addr, S_READ, svd->cred);
1553 1553 if (error != 0)
1554 1554 return (error);
1555 1555
1556 1556 pp = anon_private(&newap, newseg, addr, prot,
1557 1557 anon_pl[0], 0, newsvd->cred);
1558 1558 }
1559 1559 if (pp == NULL) {
1560 1560 return (ENOMEM);
1561 1561 }
1562 1562 (void) anon_set_ptr(newsvd->amp->ahp, new_idx, newap,
1563 1563 ANON_SLEEP);
1564 1564 page_unlock(pp);
1565 1565 }
1566 1566 addr += PAGESIZE;
1567 1567 old_idx++;
1568 1568 new_idx++;
1569 1569 }
1570 1570
1571 1571 return (0);
1572 1572 }
1573 1573
1574 1574 static int
1575 1575 segvn_dup(struct seg *seg, struct seg *newseg)
1576 1576 {
1577 1577 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
1578 1578 struct segvn_data *newsvd;
1579 1579 pgcnt_t npages = seg_pages(seg);
1580 1580 int error = 0;
1581 1581 size_t len;
1582 1582 struct anon_map *amp;
1583 1583
1584 1584 ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
1585 1585 ASSERT(newseg->s_as->a_proc->p_parent == curproc);
1586 1586
1587 1587 /*
1588 1588 * If segment has anon reserved, reserve more for the new seg.
1589 1589 * For a MAP_NORESERVE segment swresv will be a count of all the
1590 1590 * allocated anon slots; thus we reserve for the child as many slots
1591 1591 * as the parent has allocated. This semantic prevents the child or
1592 1592 * parent from dieing during a copy-on-write fault caused by trying
1593 1593 * to write a shared pre-existing anon page.
1594 1594 */
1595 1595 if ((len = svd->swresv) != 0) {
1596 1596 if (anon_resv(svd->swresv) == 0)
1597 1597 return (ENOMEM);
1598 1598
1599 1599 TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
1600 1600 seg, len, 0);
1601 1601 }
1602 1602
1603 1603 newsvd = kmem_cache_alloc(segvn_cache, KM_SLEEP);
1604 1604
1605 1605 newseg->s_ops = &segvn_ops;
1606 1606 newseg->s_data = (void *)newsvd;
1607 1607 newseg->s_szc = seg->s_szc;
1608 1608
1609 1609 newsvd->seg = newseg;
1610 1610 if ((newsvd->vp = svd->vp) != NULL) {
1611 1611 VN_HOLD(svd->vp);
1612 1612 if (svd->type == MAP_SHARED)
1613 1613 lgrp_shm_policy_init(NULL, svd->vp);
1614 1614 }
1615 1615 newsvd->offset = svd->offset;
1616 1616 newsvd->prot = svd->prot;
1617 1617 newsvd->maxprot = svd->maxprot;
1618 1618 newsvd->pageprot = svd->pageprot;
1619 1619 newsvd->type = svd->type;
1620 1620 newsvd->cred = svd->cred;
1621 1621 crhold(newsvd->cred);
1622 1622 newsvd->advice = svd->advice;
1623 1623 newsvd->pageadvice = svd->pageadvice;
1624 1624 newsvd->svn_inz = svd->svn_inz;
1625 1625 newsvd->swresv = svd->swresv;
1626 1626 newsvd->pageswap = svd->pageswap;
1627 1627 newsvd->flags = svd->flags;
1628 1628 newsvd->softlockcnt = 0;
1629 1629 newsvd->softlockcnt_sbase = 0;
1630 1630 newsvd->softlockcnt_send = 0;
1631 1631 newsvd->policy_info = svd->policy_info;
1632 1632 newsvd->rcookie = HAT_INVALID_REGION_COOKIE;
1633 1633
1634 1634 if ((amp = svd->amp) == NULL || svd->tr_state == SEGVN_TR_ON) {
1635 1635 /*
1636 1636 * Not attaching to a shared anon object.
1637 1637 */
1638 1638 ASSERT(!HAT_IS_REGION_COOKIE_VALID(svd->rcookie) ||
1639 1639 svd->tr_state == SEGVN_TR_OFF);
1640 1640 if (svd->tr_state == SEGVN_TR_ON) {
1641 1641 ASSERT(newsvd->vp != NULL && amp != NULL);
1642 1642 newsvd->tr_state = SEGVN_TR_INIT;
1643 1643 } else {
1644 1644 newsvd->tr_state = svd->tr_state;
1645 1645 }
1646 1646 newsvd->amp = NULL;
1647 1647 newsvd->anon_index = 0;
1648 1648 } else {
1649 1649 /* regions for now are only used on pure vnode segments */
1650 1650 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
1651 1651 ASSERT(svd->tr_state == SEGVN_TR_OFF);
1652 1652 newsvd->tr_state = SEGVN_TR_OFF;
1653 1653 if (svd->type == MAP_SHARED) {
1654 1654 ASSERT(svd->svn_inz == SEGVN_INZ_NONE);
1655 1655 newsvd->amp = amp;
1656 1656 ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
1657 1657 amp->refcnt++;
1658 1658 ANON_LOCK_EXIT(&->a_rwlock);
1659 1659 newsvd->anon_index = svd->anon_index;
1660 1660 } else {
1661 1661 int reclaim = 1;
1662 1662
1663 1663 /*
1664 1664 * Allocate and initialize new anon_map structure.
1665 1665 */
1666 1666 newsvd->amp = anonmap_alloc(newseg->s_size, 0,
1667 1667 ANON_SLEEP);
1668 1668 newsvd->amp->a_szc = newseg->s_szc;
1669 1669 newsvd->anon_index = 0;
1670 1670 ASSERT(svd->svn_inz == SEGVN_INZ_NONE ||
1671 1671 svd->svn_inz == SEGVN_INZ_ALL ||
1672 1672 svd->svn_inz == SEGVN_INZ_VPP);
1673 1673
1674 1674 /*
1675 1675 * We don't have to acquire the anon_map lock
1676 1676 * for the new segment (since it belongs to an
1677 1677 * address space that is still not associated
1678 1678 * with any process), or the segment in the old
1679 1679 * address space (since all threads in it
1680 1680 * are stopped while duplicating the address space).
1681 1681 */
1682 1682
1683 1683 /*
1684 1684 * The goal of the following code is to make sure that
1685 1685 * softlocked pages do not end up as copy on write
1686 1686 * pages. This would cause problems where one
1687 1687 * thread writes to a page that is COW and a different
1688 1688 * thread in the same process has softlocked it. The
1689 1689 * softlock lock would move away from this process
1690 1690 * because the write would cause this process to get
1691 1691 * a copy (without the softlock).
1692 1692 *
1693 1693 * The strategy here is to just break the
1694 1694 * sharing on pages that could possibly be
1695 1695 * softlocked.
1696 1696 *
1697 1697 * In addition, if any pages have been marked that they
1698 1698 * should be inherited as zero, then we immediately go
1699 1699 * ahead and break COW and zero them. In the case of a
1700 1700 * softlocked page that should be inherited zero, we
1701 1701 * break COW and just get a zero page.
1702 1702 */
1703 1703 retry:
1704 1704 if (svd->softlockcnt ||
1705 1705 svd->svn_inz != SEGVN_INZ_NONE) {
1706 1706 /*
1707 1707 * The softlock count might be non zero
1708 1708 * because some pages are still stuck in the
1709 1709 * cache for lazy reclaim. Flush the cache
1710 1710 * now. This should drop the count to zero.
1711 1711 * [or there is really I/O going on to these
1712 1712 * pages]. Note, we have the writers lock so
1713 1713 * nothing gets inserted during the flush.
1714 1714 */
1715 1715 if (svd->softlockcnt && reclaim == 1) {
1716 1716 segvn_purge(seg);
1717 1717 reclaim = 0;
1718 1718 goto retry;
1719 1719 }
1720 1720
1721 1721 error = segvn_dup_pages(seg, newseg);
1722 1722 if (error != 0) {
1723 1723 newsvd->vpage = NULL;
1724 1724 goto out;
1725 1725 }
1726 1726 } else { /* common case */
1727 1727 if (seg->s_szc != 0) {
1728 1728 /*
1729 1729 * If at least one of anon slots of a
1730 1730 * large page exists then make sure
1731 1731 * all anon slots of a large page
1732 1732 * exist to avoid partial cow sharing
1733 1733 * of a large page in the future.
1734 1734 */
1735 1735 anon_dup_fill_holes(amp->ahp,
1736 1736 svd->anon_index, newsvd->amp->ahp,
1737 1737 0, seg->s_size, seg->s_szc,
1738 1738 svd->vp != NULL);
1739 1739 } else {
1740 1740 anon_dup(amp->ahp, svd->anon_index,
1741 1741 newsvd->amp->ahp, 0, seg->s_size);
1742 1742 }
1743 1743
1744 1744 hat_clrattr(seg->s_as->a_hat, seg->s_base,
1745 1745 seg->s_size, PROT_WRITE);
1746 1746 }
1747 1747 }
1748 1748 }
1749 1749 /*
1750 1750 * If necessary, create a vpage structure for the new segment.
1751 1751 * Do not copy any page lock indications.
1752 1752 */
1753 1753 if (svd->vpage != NULL) {
1754 1754 uint_t i;
1755 1755 struct vpage *ovp = svd->vpage;
1756 1756 struct vpage *nvp;
1757 1757
1758 1758 nvp = newsvd->vpage =
1759 1759 kmem_alloc(vpgtob(npages), KM_SLEEP);
1760 1760 for (i = 0; i < npages; i++) {
1761 1761 *nvp = *ovp++;
1762 1762 VPP_CLRPPLOCK(nvp++);
1763 1763 }
1764 1764 } else
1765 1765 newsvd->vpage = NULL;
1766 1766
1767 1767 /* Inform the vnode of the new mapping */
1768 1768 if (newsvd->vp != NULL) {
1769 1769 error = VOP_ADDMAP(newsvd->vp, (offset_t)newsvd->offset,
1770 1770 newseg->s_as, newseg->s_base, newseg->s_size, newsvd->prot,
1771 1771 newsvd->maxprot, newsvd->type, newsvd->cred, NULL);
1772 1772 }
1773 1773 out:
1774 1774 if (error == 0 && HAT_IS_REGION_COOKIE_VALID(svd->rcookie)) {
1775 1775 ASSERT(newsvd->amp == NULL);
1776 1776 ASSERT(newsvd->tr_state == SEGVN_TR_OFF);
1777 1777 newsvd->rcookie = svd->rcookie;
1778 1778 hat_dup_region(newseg->s_as->a_hat, newsvd->rcookie);
1779 1779 }
1780 1780 return (error);
1781 1781 }
1782 1782
1783 1783
1784 1784 /*
1785 1785 * callback function to invoke free_vp_pages() for only those pages actually
1786 1786 * processed by the HAT when a shared region is destroyed.
1787 1787 */
1788 1788 extern int free_pages;
1789 1789
1790 1790 static void
1791 1791 segvn_hat_rgn_unload_callback(caddr_t saddr, caddr_t eaddr, caddr_t r_saddr,
1792 1792 size_t r_size, void *r_obj, u_offset_t r_objoff)
1793 1793 {
1794 1794 u_offset_t off;
1795 1795 size_t len;
1796 1796 vnode_t *vp = (vnode_t *)r_obj;
1797 1797
1798 1798 ASSERT(eaddr > saddr);
1799 1799 ASSERT(saddr >= r_saddr);
1800 1800 ASSERT(saddr < r_saddr + r_size);
1801 1801 ASSERT(eaddr > r_saddr);
1802 1802 ASSERT(eaddr <= r_saddr + r_size);
1803 1803 ASSERT(vp != NULL);
1804 1804
1805 1805 if (!free_pages) {
1806 1806 return;
1807 1807 }
1808 1808
1809 1809 len = eaddr - saddr;
1810 1810 off = (saddr - r_saddr) + r_objoff;
1811 1811 free_vp_pages(vp, off, len);
1812 1812 }
1813 1813
1814 1814 /*
1815 1815 * callback function used by segvn_unmap to invoke free_vp_pages() for only
1816 1816 * those pages actually processed by the HAT
1817 1817 */
1818 1818 static void
1819 1819 segvn_hat_unload_callback(hat_callback_t *cb)
1820 1820 {
1821 1821 struct seg *seg = cb->hcb_data;
1822 1822 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
1823 1823 size_t len;
1824 1824 u_offset_t off;
1825 1825
1826 1826 ASSERT(svd->vp != NULL);
1827 1827 ASSERT(cb->hcb_end_addr > cb->hcb_start_addr);
1828 1828 ASSERT(cb->hcb_start_addr >= seg->s_base);
1829 1829
1830 1830 len = cb->hcb_end_addr - cb->hcb_start_addr;
1831 1831 off = cb->hcb_start_addr - seg->s_base;
1832 1832 free_vp_pages(svd->vp, svd->offset + off, len);
1833 1833 }
1834 1834
1835 1835 /*
1836 1836 * This function determines the number of bytes of swap reserved by
1837 1837 * a segment for which per-page accounting is present. It is used to
1838 1838 * calculate the correct value of a segvn_data's swresv.
1839 1839 */
1840 1840 static size_t
1841 1841 segvn_count_swap_by_vpages(struct seg *seg)
1842 1842 {
1843 1843 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
1844 1844 struct vpage *vp, *evp;
1845 1845 size_t nswappages = 0;
1846 1846
1847 1847 ASSERT(svd->pageswap);
1848 1848 ASSERT(svd->vpage != NULL);
1849 1849
1850 1850 evp = &svd->vpage[seg_page(seg, seg->s_base + seg->s_size)];
1851 1851
1852 1852 for (vp = svd->vpage; vp < evp; vp++) {
1853 1853 if (VPP_ISSWAPRES(vp))
1854 1854 nswappages++;
1855 1855 }
1856 1856
1857 1857 return (nswappages << PAGESHIFT);
1858 1858 }
1859 1859
1860 1860 static int
1861 1861 segvn_unmap(struct seg *seg, caddr_t addr, size_t len)
1862 1862 {
1863 1863 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
1864 1864 struct segvn_data *nsvd;
1865 1865 struct seg *nseg;
1866 1866 struct anon_map *amp;
1867 1867 pgcnt_t opages; /* old segment size in pages */
1868 1868 pgcnt_t npages; /* new segment size in pages */
1869 1869 pgcnt_t dpages; /* pages being deleted (unmapped) */
1870 1870 hat_callback_t callback; /* used for free_vp_pages() */
1871 1871 hat_callback_t *cbp = NULL;
1872 1872 caddr_t nbase;
1873 1873 size_t nsize;
1874 1874 size_t oswresv;
1875 1875 int reclaim = 1;
1876 1876
1877 1877 /*
1878 1878 * We don't need any segment level locks for "segvn" data
1879 1879 * since the address space is "write" locked.
1880 1880 */
1881 1881 ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
1882 1882
1883 1883 /*
1884 1884 * Fail the unmap if pages are SOFTLOCKed through this mapping.
1885 1885 * softlockcnt is protected from change by the as write lock.
1886 1886 */
1887 1887 retry:
1888 1888 if (svd->softlockcnt > 0) {
1889 1889 ASSERT(svd->tr_state == SEGVN_TR_OFF);
1890 1890
1891 1891 /*
1892 1892 * If this is shared segment non 0 softlockcnt
1893 1893 * means locked pages are still in use.
1894 1894 */
1895 1895 if (svd->type == MAP_SHARED) {
1896 1896 return (EAGAIN);
1897 1897 }
1898 1898
1899 1899 /*
1900 1900 * since we do have the writers lock nobody can fill
1901 1901 * the cache during the purge. The flush either succeeds
1902 1902 * or we still have pending I/Os.
1903 1903 */
1904 1904 if (reclaim == 1) {
1905 1905 segvn_purge(seg);
1906 1906 reclaim = 0;
1907 1907 goto retry;
1908 1908 }
1909 1909 return (EAGAIN);
1910 1910 }
1911 1911
1912 1912 /*
1913 1913 * Check for bad sizes
1914 1914 */
1915 1915 if (addr < seg->s_base || addr + len > seg->s_base + seg->s_size ||
1916 1916 (len & PAGEOFFSET) || ((uintptr_t)addr & PAGEOFFSET)) {
1917 1917 panic("segvn_unmap");
1918 1918 /*NOTREACHED*/
1919 1919 }
1920 1920
1921 1921 if (seg->s_szc != 0) {
1922 1922 size_t pgsz = page_get_pagesize(seg->s_szc);
1923 1923 int err;
1924 1924 if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(len, pgsz)) {
1925 1925 ASSERT(seg->s_base != addr || seg->s_size != len);
1926 1926 if (HAT_IS_REGION_COOKIE_VALID(svd->rcookie)) {
1927 1927 ASSERT(svd->amp == NULL);
1928 1928 ASSERT(svd->tr_state == SEGVN_TR_OFF);
1929 1929 hat_leave_region(seg->s_as->a_hat,
1930 1930 svd->rcookie, HAT_REGION_TEXT);
1931 1931 svd->rcookie = HAT_INVALID_REGION_COOKIE;
1932 1932 /*
1933 1933 * could pass a flag to segvn_demote_range()
1934 1934 * below to tell it not to do any unloads but
1935 1935 * this case is rare enough to not bother for
1936 1936 * now.
1937 1937 */
1938 1938 } else if (svd->tr_state == SEGVN_TR_INIT) {
1939 1939 svd->tr_state = SEGVN_TR_OFF;
1940 1940 } else if (svd->tr_state == SEGVN_TR_ON) {
1941 1941 ASSERT(svd->amp != NULL);
1942 1942 segvn_textunrepl(seg, 1);
1943 1943 ASSERT(svd->amp == NULL);
1944 1944 ASSERT(svd->tr_state == SEGVN_TR_OFF);
1945 1945 }
1946 1946 VM_STAT_ADD(segvnvmstats.demoterange[0]);
1947 1947 err = segvn_demote_range(seg, addr, len, SDR_END, 0);
1948 1948 if (err == 0) {
1949 1949 return (IE_RETRY);
1950 1950 }
1951 1951 return (err);
1952 1952 }
1953 1953 }
1954 1954
1955 1955 /* Inform the vnode of the unmapping. */
1956 1956 if (svd->vp) {
1957 1957 int error;
1958 1958
1959 1959 error = VOP_DELMAP(svd->vp,
1960 1960 (offset_t)svd->offset + (uintptr_t)(addr - seg->s_base),
1961 1961 seg->s_as, addr, len, svd->prot, svd->maxprot,
1962 1962 svd->type, svd->cred, NULL);
1963 1963
1964 1964 if (error == EAGAIN)
1965 1965 return (error);
1966 1966 }
1967 1967
1968 1968 /*
1969 1969 * Remove any page locks set through this mapping.
1970 1970 * If text replication is not off no page locks could have been
1971 1971 * established via this mapping.
1972 1972 */
1973 1973 if (svd->tr_state == SEGVN_TR_OFF) {
1974 1974 (void) segvn_lockop(seg, addr, len, 0, MC_UNLOCK, NULL, 0);
1975 1975 }
1976 1976
1977 1977 if (HAT_IS_REGION_COOKIE_VALID(svd->rcookie)) {
1978 1978 ASSERT(svd->amp == NULL);
1979 1979 ASSERT(svd->tr_state == SEGVN_TR_OFF);
1980 1980 ASSERT(svd->type == MAP_PRIVATE);
1981 1981 hat_leave_region(seg->s_as->a_hat, svd->rcookie,
1982 1982 HAT_REGION_TEXT);
1983 1983 svd->rcookie = HAT_INVALID_REGION_COOKIE;
1984 1984 } else if (svd->tr_state == SEGVN_TR_ON) {
1985 1985 ASSERT(svd->amp != NULL);
1986 1986 ASSERT(svd->pageprot == 0 && !(svd->prot & PROT_WRITE));
1987 1987 segvn_textunrepl(seg, 1);
1988 1988 ASSERT(svd->amp == NULL && svd->tr_state == SEGVN_TR_OFF);
1989 1989 } else {
1990 1990 if (svd->tr_state != SEGVN_TR_OFF) {
1991 1991 ASSERT(svd->tr_state == SEGVN_TR_INIT);
1992 1992 svd->tr_state = SEGVN_TR_OFF;
1993 1993 }
1994 1994 /*
1995 1995 * Unload any hardware translations in the range to be taken
1996 1996 * out. Use a callback to invoke free_vp_pages() effectively.
1997 1997 */
1998 1998 if (svd->vp != NULL && free_pages != 0) {
1999 1999 callback.hcb_data = seg;
2000 2000 callback.hcb_function = segvn_hat_unload_callback;
2001 2001 cbp = &callback;
2002 2002 }
2003 2003 hat_unload_callback(seg->s_as->a_hat, addr, len,
2004 2004 HAT_UNLOAD_UNMAP, cbp);
2005 2005
2006 2006 if (svd->type == MAP_SHARED && svd->vp != NULL &&
2007 2007 (svd->vp->v_flag & VVMEXEC) &&
2008 2008 ((svd->prot & PROT_WRITE) || svd->pageprot)) {
2009 2009 segvn_inval_trcache(svd->vp);
2010 2010 }
2011 2011 }
2012 2012
2013 2013 /*
2014 2014 * Check for entire segment
2015 2015 */
2016 2016 if (addr == seg->s_base && len == seg->s_size) {
2017 2017 seg_free(seg);
2018 2018 return (0);
2019 2019 }
2020 2020
2021 2021 opages = seg_pages(seg);
2022 2022 dpages = btop(len);
2023 2023 npages = opages - dpages;
2024 2024 amp = svd->amp;
2025 2025 ASSERT(amp == NULL || amp->a_szc >= seg->s_szc);
2026 2026
2027 2027 /*
2028 2028 * Check for beginning of segment
2029 2029 */
2030 2030 if (addr == seg->s_base) {
2031 2031 if (svd->vpage != NULL) {
2032 2032 size_t nbytes;
2033 2033 struct vpage *ovpage;
2034 2034
2035 2035 ovpage = svd->vpage; /* keep pointer to vpage */
2036 2036
2037 2037 nbytes = vpgtob(npages);
2038 2038 svd->vpage = kmem_alloc(nbytes, KM_SLEEP);
2039 2039 bcopy(&ovpage[dpages], svd->vpage, nbytes);
2040 2040
2041 2041 /* free up old vpage */
2042 2042 kmem_free(ovpage, vpgtob(opages));
2043 2043 }
2044 2044 if (amp != NULL) {
2045 2045 ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
2046 2046 if (amp->refcnt == 1 || svd->type == MAP_PRIVATE) {
2047 2047 /*
2048 2048 * Shared anon map is no longer in use. Before
2049 2049 * freeing its pages purge all entries from
2050 2050 * pcache that belong to this amp.
2051 2051 */
2052 2052 if (svd->type == MAP_SHARED) {
2053 2053 ASSERT(amp->refcnt == 1);
2054 2054 ASSERT(svd->softlockcnt == 0);
2055 2055 anonmap_purge(amp);
2056 2056 }
2057 2057 /*
2058 2058 * Free up now unused parts of anon_map array.
2059 2059 */
2060 2060 if (amp->a_szc == seg->s_szc) {
2061 2061 if (seg->s_szc != 0) {
2062 2062 anon_free_pages(amp->ahp,
2063 2063 svd->anon_index, len,
2064 2064 seg->s_szc);
2065 2065 } else {
2066 2066 anon_free(amp->ahp,
2067 2067 svd->anon_index,
2068 2068 len);
2069 2069 }
2070 2070 } else {
2071 2071 ASSERT(svd->type == MAP_SHARED);
2072 2072 ASSERT(amp->a_szc > seg->s_szc);
2073 2073 anon_shmap_free_pages(amp,
2074 2074 svd->anon_index, len);
2075 2075 }
2076 2076
2077 2077 /*
2078 2078 * Unreserve swap space for the
2079 2079 * unmapped chunk of this segment in
2080 2080 * case it's MAP_SHARED
2081 2081 */
2082 2082 if (svd->type == MAP_SHARED) {
2083 2083 anon_unresv_zone(len,
2084 2084 seg->s_as->a_proc->p_zone);
2085 2085 amp->swresv -= len;
2086 2086 }
2087 2087 }
2088 2088 ANON_LOCK_EXIT(&->a_rwlock);
2089 2089 svd->anon_index += dpages;
2090 2090 }
2091 2091 if (svd->vp != NULL)
2092 2092 svd->offset += len;
2093 2093
2094 2094 seg->s_base += len;
2095 2095 seg->s_size -= len;
2096 2096
2097 2097 if (svd->swresv) {
2098 2098 if (svd->flags & MAP_NORESERVE) {
2099 2099 ASSERT(amp);
2100 2100 oswresv = svd->swresv;
2101 2101
2102 2102 svd->swresv = ptob(anon_pages(amp->ahp,
2103 2103 svd->anon_index, npages));
2104 2104 anon_unresv_zone(oswresv - svd->swresv,
2105 2105 seg->s_as->a_proc->p_zone);
2106 2106 if (SEG_IS_PARTIAL_RESV(seg))
2107 2107 seg->s_as->a_resvsize -= oswresv -
2108 2108 svd->swresv;
2109 2109 } else {
2110 2110 size_t unlen;
2111 2111
2112 2112 if (svd->pageswap) {
2113 2113 oswresv = svd->swresv;
2114 2114 svd->swresv =
2115 2115 segvn_count_swap_by_vpages(seg);
2116 2116 ASSERT(oswresv >= svd->swresv);
2117 2117 unlen = oswresv - svd->swresv;
2118 2118 } else {
2119 2119 svd->swresv -= len;
2120 2120 ASSERT(svd->swresv == seg->s_size);
2121 2121 unlen = len;
2122 2122 }
2123 2123 anon_unresv_zone(unlen,
2124 2124 seg->s_as->a_proc->p_zone);
2125 2125 }
2126 2126 TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
2127 2127 seg, len, 0);
2128 2128 }
2129 2129
2130 2130 return (0);
2131 2131 }
2132 2132
2133 2133 /*
2134 2134 * Check for end of segment
2135 2135 */
2136 2136 if (addr + len == seg->s_base + seg->s_size) {
2137 2137 if (svd->vpage != NULL) {
2138 2138 size_t nbytes;
2139 2139 struct vpage *ovpage;
2140 2140
2141 2141 ovpage = svd->vpage; /* keep pointer to vpage */
2142 2142
2143 2143 nbytes = vpgtob(npages);
2144 2144 svd->vpage = kmem_alloc(nbytes, KM_SLEEP);
2145 2145 bcopy(ovpage, svd->vpage, nbytes);
2146 2146
2147 2147 /* free up old vpage */
2148 2148 kmem_free(ovpage, vpgtob(opages));
2149 2149
2150 2150 }
2151 2151 if (amp != NULL) {
2152 2152 ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
2153 2153 if (amp->refcnt == 1 || svd->type == MAP_PRIVATE) {
2154 2154 /*
2155 2155 * Free up now unused parts of anon_map array.
2156 2156 */
2157 2157 ulong_t an_idx = svd->anon_index + npages;
2158 2158
2159 2159 /*
2160 2160 * Shared anon map is no longer in use. Before
2161 2161 * freeing its pages purge all entries from
2162 2162 * pcache that belong to this amp.
2163 2163 */
2164 2164 if (svd->type == MAP_SHARED) {
2165 2165 ASSERT(amp->refcnt == 1);
2166 2166 ASSERT(svd->softlockcnt == 0);
2167 2167 anonmap_purge(amp);
2168 2168 }
2169 2169
2170 2170 if (amp->a_szc == seg->s_szc) {
2171 2171 if (seg->s_szc != 0) {
2172 2172 anon_free_pages(amp->ahp,
2173 2173 an_idx, len,
2174 2174 seg->s_szc);
2175 2175 } else {
2176 2176 anon_free(amp->ahp, an_idx,
2177 2177 len);
2178 2178 }
2179 2179 } else {
2180 2180 ASSERT(svd->type == MAP_SHARED);
2181 2181 ASSERT(amp->a_szc > seg->s_szc);
2182 2182 anon_shmap_free_pages(amp,
2183 2183 an_idx, len);
2184 2184 }
2185 2185
2186 2186 /*
2187 2187 * Unreserve swap space for the
2188 2188 * unmapped chunk of this segment in
2189 2189 * case it's MAP_SHARED
2190 2190 */
2191 2191 if (svd->type == MAP_SHARED) {
2192 2192 anon_unresv_zone(len,
2193 2193 seg->s_as->a_proc->p_zone);
2194 2194 amp->swresv -= len;
2195 2195 }
2196 2196 }
2197 2197 ANON_LOCK_EXIT(&->a_rwlock);
2198 2198 }
2199 2199
2200 2200 seg->s_size -= len;
2201 2201
2202 2202 if (svd->swresv) {
2203 2203 if (svd->flags & MAP_NORESERVE) {
2204 2204 ASSERT(amp);
2205 2205 oswresv = svd->swresv;
2206 2206 svd->swresv = ptob(anon_pages(amp->ahp,
2207 2207 svd->anon_index, npages));
2208 2208 anon_unresv_zone(oswresv - svd->swresv,
2209 2209 seg->s_as->a_proc->p_zone);
2210 2210 if (SEG_IS_PARTIAL_RESV(seg))
2211 2211 seg->s_as->a_resvsize -= oswresv -
2212 2212 svd->swresv;
2213 2213 } else {
2214 2214 size_t unlen;
2215 2215
2216 2216 if (svd->pageswap) {
2217 2217 oswresv = svd->swresv;
2218 2218 svd->swresv =
2219 2219 segvn_count_swap_by_vpages(seg);
2220 2220 ASSERT(oswresv >= svd->swresv);
2221 2221 unlen = oswresv - svd->swresv;
2222 2222 } else {
2223 2223 svd->swresv -= len;
2224 2224 ASSERT(svd->swresv == seg->s_size);
2225 2225 unlen = len;
2226 2226 }
2227 2227 anon_unresv_zone(unlen,
2228 2228 seg->s_as->a_proc->p_zone);
2229 2229 }
2230 2230 TRACE_3(TR_FAC_VM, TR_ANON_PROC,
2231 2231 "anon proc:%p %lu %u", seg, len, 0);
2232 2232 }
2233 2233
2234 2234 return (0);
2235 2235 }
2236 2236
2237 2237 /*
2238 2238 * The section to go is in the middle of the segment,
2239 2239 * have to make it into two segments. nseg is made for
2240 2240 * the high end while seg is cut down at the low end.
2241 2241 */
2242 2242 nbase = addr + len; /* new seg base */
2243 2243 nsize = (seg->s_base + seg->s_size) - nbase; /* new seg size */
2244 2244 seg->s_size = addr - seg->s_base; /* shrink old seg */
2245 2245 nseg = seg_alloc(seg->s_as, nbase, nsize);
2246 2246 if (nseg == NULL) {
2247 2247 panic("segvn_unmap seg_alloc");
2248 2248 /*NOTREACHED*/
2249 2249 }
2250 2250 nseg->s_ops = seg->s_ops;
2251 2251 nsvd = kmem_cache_alloc(segvn_cache, KM_SLEEP);
2252 2252 nseg->s_data = (void *)nsvd;
2253 2253 nseg->s_szc = seg->s_szc;
2254 2254 *nsvd = *svd;
2255 2255 nsvd->seg = nseg;
2256 2256 nsvd->offset = svd->offset + (uintptr_t)(nseg->s_base - seg->s_base);
2257 2257 nsvd->swresv = 0;
2258 2258 nsvd->softlockcnt = 0;
2259 2259 nsvd->softlockcnt_sbase = 0;
2260 2260 nsvd->softlockcnt_send = 0;
2261 2261 nsvd->svn_inz = svd->svn_inz;
2262 2262 ASSERT(nsvd->rcookie == HAT_INVALID_REGION_COOKIE);
2263 2263
2264 2264 if (svd->vp != NULL) {
2265 2265 VN_HOLD(nsvd->vp);
2266 2266 if (nsvd->type == MAP_SHARED)
2267 2267 lgrp_shm_policy_init(NULL, nsvd->vp);
2268 2268 }
2269 2269 crhold(svd->cred);
2270 2270
2271 2271 if (svd->vpage == NULL) {
2272 2272 nsvd->vpage = NULL;
2273 2273 } else {
2274 2274 /* need to split vpage into two arrays */
2275 2275 size_t nbytes;
2276 2276 struct vpage *ovpage;
2277 2277
2278 2278 ovpage = svd->vpage; /* keep pointer to vpage */
2279 2279
2280 2280 npages = seg_pages(seg); /* seg has shrunk */
2281 2281 nbytes = vpgtob(npages);
2282 2282 svd->vpage = kmem_alloc(nbytes, KM_SLEEP);
2283 2283
2284 2284 bcopy(ovpage, svd->vpage, nbytes);
2285 2285
2286 2286 npages = seg_pages(nseg);
2287 2287 nbytes = vpgtob(npages);
2288 2288 nsvd->vpage = kmem_alloc(nbytes, KM_SLEEP);
2289 2289
2290 2290 bcopy(&ovpage[opages - npages], nsvd->vpage, nbytes);
2291 2291
2292 2292 /* free up old vpage */
2293 2293 kmem_free(ovpage, vpgtob(opages));
2294 2294 }
2295 2295
2296 2296 if (amp == NULL) {
2297 2297 nsvd->amp = NULL;
2298 2298 nsvd->anon_index = 0;
2299 2299 } else {
2300 2300 /*
2301 2301 * Need to create a new anon map for the new segment.
2302 2302 * We'll also allocate a new smaller array for the old
2303 2303 * smaller segment to save space.
2304 2304 */
2305 2305 opages = btop((uintptr_t)(addr - seg->s_base));
2306 2306 ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
2307 2307 if (amp->refcnt == 1 || svd->type == MAP_PRIVATE) {
2308 2308 /*
2309 2309 * Free up now unused parts of anon_map array.
2310 2310 */
2311 2311 ulong_t an_idx = svd->anon_index + opages;
2312 2312
2313 2313 /*
2314 2314 * Shared anon map is no longer in use. Before
2315 2315 * freeing its pages purge all entries from
2316 2316 * pcache that belong to this amp.
2317 2317 */
2318 2318 if (svd->type == MAP_SHARED) {
2319 2319 ASSERT(amp->refcnt == 1);
2320 2320 ASSERT(svd->softlockcnt == 0);
2321 2321 anonmap_purge(amp);
2322 2322 }
2323 2323
2324 2324 if (amp->a_szc == seg->s_szc) {
2325 2325 if (seg->s_szc != 0) {
2326 2326 anon_free_pages(amp->ahp, an_idx, len,
2327 2327 seg->s_szc);
2328 2328 } else {
2329 2329 anon_free(amp->ahp, an_idx,
2330 2330 len);
2331 2331 }
2332 2332 } else {
2333 2333 ASSERT(svd->type == MAP_SHARED);
2334 2334 ASSERT(amp->a_szc > seg->s_szc);
2335 2335 anon_shmap_free_pages(amp, an_idx, len);
2336 2336 }
2337 2337
2338 2338 /*
2339 2339 * Unreserve swap space for the
2340 2340 * unmapped chunk of this segment in
2341 2341 * case it's MAP_SHARED
2342 2342 */
2343 2343 if (svd->type == MAP_SHARED) {
2344 2344 anon_unresv_zone(len,
2345 2345 seg->s_as->a_proc->p_zone);
2346 2346 amp->swresv -= len;
2347 2347 }
2348 2348 }
2349 2349 nsvd->anon_index = svd->anon_index +
2350 2350 btop((uintptr_t)(nseg->s_base - seg->s_base));
2351 2351 if (svd->type == MAP_SHARED) {
2352 2352 amp->refcnt++;
2353 2353 nsvd->amp = amp;
2354 2354 } else {
2355 2355 struct anon_map *namp;
2356 2356 struct anon_hdr *nahp;
2357 2357
2358 2358 ASSERT(svd->type == MAP_PRIVATE);
2359 2359 nahp = anon_create(btop(seg->s_size), ANON_SLEEP);
2360 2360 namp = anonmap_alloc(nseg->s_size, 0, ANON_SLEEP);
2361 2361 namp->a_szc = seg->s_szc;
2362 2362 (void) anon_copy_ptr(amp->ahp, svd->anon_index, nahp,
2363 2363 0, btop(seg->s_size), ANON_SLEEP);
2364 2364 (void) anon_copy_ptr(amp->ahp, nsvd->anon_index,
2365 2365 namp->ahp, 0, btop(nseg->s_size), ANON_SLEEP);
2366 2366 anon_release(amp->ahp, btop(amp->size));
2367 2367 svd->anon_index = 0;
2368 2368 nsvd->anon_index = 0;
2369 2369 amp->ahp = nahp;
2370 2370 amp->size = seg->s_size;
2371 2371 nsvd->amp = namp;
2372 2372 }
2373 2373 ANON_LOCK_EXIT(&->a_rwlock);
2374 2374 }
2375 2375 if (svd->swresv) {
2376 2376 if (svd->flags & MAP_NORESERVE) {
2377 2377 ASSERT(amp);
2378 2378 oswresv = svd->swresv;
2379 2379 svd->swresv = ptob(anon_pages(amp->ahp,
2380 2380 svd->anon_index, btop(seg->s_size)));
2381 2381 nsvd->swresv = ptob(anon_pages(nsvd->amp->ahp,
2382 2382 nsvd->anon_index, btop(nseg->s_size)));
2383 2383 ASSERT(oswresv >= (svd->swresv + nsvd->swresv));
2384 2384 anon_unresv_zone(oswresv - (svd->swresv + nsvd->swresv),
2385 2385 seg->s_as->a_proc->p_zone);
2386 2386 if (SEG_IS_PARTIAL_RESV(seg))
2387 2387 seg->s_as->a_resvsize -= oswresv -
2388 2388 (svd->swresv + nsvd->swresv);
2389 2389 } else {
2390 2390 size_t unlen;
2391 2391
2392 2392 if (svd->pageswap) {
2393 2393 oswresv = svd->swresv;
2394 2394 svd->swresv = segvn_count_swap_by_vpages(seg);
2395 2395 nsvd->swresv = segvn_count_swap_by_vpages(nseg);
2396 2396 ASSERT(oswresv >= (svd->swresv + nsvd->swresv));
2397 2397 unlen = oswresv - (svd->swresv + nsvd->swresv);
2398 2398 } else {
2399 2399 if (seg->s_size + nseg->s_size + len !=
2400 2400 svd->swresv) {
2401 2401 panic("segvn_unmap: cannot split "
2402 2402 "swap reservation");
2403 2403 /*NOTREACHED*/
2404 2404 }
2405 2405 svd->swresv = seg->s_size;
2406 2406 nsvd->swresv = nseg->s_size;
2407 2407 unlen = len;
2408 2408 }
2409 2409 anon_unresv_zone(unlen,
2410 2410 seg->s_as->a_proc->p_zone);
2411 2411 }
2412 2412 TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
2413 2413 seg, len, 0);
2414 2414 }
2415 2415
2416 2416 return (0); /* I'm glad that's all over with! */
2417 2417 }
2418 2418
2419 2419 static void
2420 2420 segvn_free(struct seg *seg)
2421 2421 {
2422 2422 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
2423 2423 pgcnt_t npages = seg_pages(seg);
2424 2424 struct anon_map *amp;
2425 2425 size_t len;
2426 2426
2427 2427 /*
2428 2428 * We don't need any segment level locks for "segvn" data
2429 2429 * since the address space is "write" locked.
2430 2430 */
2431 2431 ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
2432 2432 ASSERT(svd->tr_state == SEGVN_TR_OFF);
2433 2433
2434 2434 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
2435 2435
2436 2436 /*
2437 2437 * Be sure to unlock pages. XXX Why do things get free'ed instead
2438 2438 * of unmapped? XXX
2439 2439 */
2440 2440 (void) segvn_lockop(seg, seg->s_base, seg->s_size,
2441 2441 0, MC_UNLOCK, NULL, 0);
2442 2442
2443 2443 /*
2444 2444 * Deallocate the vpage and anon pointers if necessary and possible.
2445 2445 */
2446 2446 if (svd->vpage != NULL) {
2447 2447 kmem_free(svd->vpage, vpgtob(npages));
2448 2448 svd->vpage = NULL;
2449 2449 }
2450 2450 if ((amp = svd->amp) != NULL) {
2451 2451 /*
2452 2452 * If there are no more references to this anon_map
2453 2453 * structure, then deallocate the structure after freeing
2454 2454 * up all the anon slot pointers that we can.
2455 2455 */
2456 2456 ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
2457 2457 ASSERT(amp->a_szc >= seg->s_szc);
2458 2458 if (--amp->refcnt == 0) {
2459 2459 if (svd->type == MAP_PRIVATE) {
2460 2460 /*
2461 2461 * Private - we only need to anon_free
2462 2462 * the part that this segment refers to.
2463 2463 */
2464 2464 if (seg->s_szc != 0) {
2465 2465 anon_free_pages(amp->ahp,
2466 2466 svd->anon_index, seg->s_size,
2467 2467 seg->s_szc);
2468 2468 } else {
2469 2469 anon_free(amp->ahp, svd->anon_index,
2470 2470 seg->s_size);
2471 2471 }
2472 2472 } else {
2473 2473
2474 2474 /*
2475 2475 * Shared anon map is no longer in use. Before
2476 2476 * freeing its pages purge all entries from
2477 2477 * pcache that belong to this amp.
2478 2478 */
2479 2479 ASSERT(svd->softlockcnt == 0);
2480 2480 anonmap_purge(amp);
2481 2481
2482 2482 /*
2483 2483 * Shared - anon_free the entire
2484 2484 * anon_map's worth of stuff and
2485 2485 * release any swap reservation.
2486 2486 */
2487 2487 if (amp->a_szc != 0) {
2488 2488 anon_shmap_free_pages(amp, 0,
2489 2489 amp->size);
2490 2490 } else {
2491 2491 anon_free(amp->ahp, 0, amp->size);
2492 2492 }
2493 2493 if ((len = amp->swresv) != 0) {
2494 2494 anon_unresv_zone(len,
2495 2495 seg->s_as->a_proc->p_zone);
2496 2496 TRACE_3(TR_FAC_VM, TR_ANON_PROC,
2497 2497 "anon proc:%p %lu %u", seg, len, 0);
2498 2498 }
2499 2499 }
2500 2500 svd->amp = NULL;
2501 2501 ANON_LOCK_EXIT(&->a_rwlock);
2502 2502 anonmap_free(amp);
2503 2503 } else if (svd->type == MAP_PRIVATE) {
2504 2504 /*
2505 2505 * We had a private mapping which still has
2506 2506 * a held anon_map so just free up all the
2507 2507 * anon slot pointers that we were using.
2508 2508 */
2509 2509 if (seg->s_szc != 0) {
2510 2510 anon_free_pages(amp->ahp, svd->anon_index,
2511 2511 seg->s_size, seg->s_szc);
2512 2512 } else {
2513 2513 anon_free(amp->ahp, svd->anon_index,
2514 2514 seg->s_size);
2515 2515 }
2516 2516 ANON_LOCK_EXIT(&->a_rwlock);
2517 2517 } else {
2518 2518 ANON_LOCK_EXIT(&->a_rwlock);
2519 2519 }
2520 2520 }
2521 2521
2522 2522 /*
2523 2523 * Release swap reservation.
2524 2524 */
2525 2525 if ((len = svd->swresv) != 0) {
2526 2526 anon_unresv_zone(svd->swresv,
2527 2527 seg->s_as->a_proc->p_zone);
2528 2528 TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
2529 2529 seg, len, 0);
2530 2530 if (SEG_IS_PARTIAL_RESV(seg))
2531 2531 seg->s_as->a_resvsize -= svd->swresv;
2532 2532 svd->swresv = 0;
2533 2533 }
2534 2534 /*
2535 2535 * Release claim on vnode, credentials, and finally free the
2536 2536 * private data.
2537 2537 */
2538 2538 if (svd->vp != NULL) {
2539 2539 if (svd->type == MAP_SHARED)
2540 2540 lgrp_shm_policy_fini(NULL, svd->vp);
2541 2541 VN_RELE(svd->vp);
2542 2542 svd->vp = NULL;
2543 2543 }
2544 2544 crfree(svd->cred);
2545 2545 svd->pageprot = 0;
2546 2546 svd->pageadvice = 0;
2547 2547 svd->pageswap = 0;
2548 2548 svd->cred = NULL;
2549 2549
2550 2550 /*
2551 2551 * Take segfree_syncmtx lock to let segvn_reclaim() finish if it's
2552 2552 * still working with this segment without holding as lock (in case
2553 2553 * it's called by pcache async thread).
2554 2554 */
2555 2555 ASSERT(svd->softlockcnt == 0);
2556 2556 mutex_enter(&svd->segfree_syncmtx);
2557 2557 mutex_exit(&svd->segfree_syncmtx);
2558 2558
2559 2559 seg->s_data = NULL;
2560 2560 kmem_cache_free(segvn_cache, svd);
2561 2561 }
2562 2562
2563 2563 /*
2564 2564 * Do a F_SOFTUNLOCK call over the range requested. The range must have
2565 2565 * already been F_SOFTLOCK'ed.
2566 2566 * Caller must always match addr and len of a softunlock with a previous
2567 2567 * softlock with exactly the same addr and len.
2568 2568 */
2569 2569 static void
2570 2570 segvn_softunlock(struct seg *seg, caddr_t addr, size_t len, enum seg_rw rw)
2571 2571 {
2572 2572 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
2573 2573 page_t *pp;
2574 2574 caddr_t adr;
2575 2575 struct vnode *vp;
2576 2576 u_offset_t offset;
2577 2577 ulong_t anon_index;
2578 2578 struct anon_map *amp;
2579 2579 struct anon *ap = NULL;
2580 2580
2581 2581 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
2582 2582 ASSERT(SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
2583 2583
2584 2584 if ((amp = svd->amp) != NULL)
2585 2585 anon_index = svd->anon_index + seg_page(seg, addr);
2586 2586
2587 2587 if (HAT_IS_REGION_COOKIE_VALID(svd->rcookie)) {
2588 2588 ASSERT(svd->tr_state == SEGVN_TR_OFF);
2589 2589 hat_unlock_region(seg->s_as->a_hat, addr, len, svd->rcookie);
2590 2590 } else {
2591 2591 hat_unlock(seg->s_as->a_hat, addr, len);
2592 2592 }
2593 2593 for (adr = addr; adr < addr + len; adr += PAGESIZE) {
2594 2594 if (amp != NULL) {
2595 2595 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
2596 2596 if ((ap = anon_get_ptr(amp->ahp, anon_index++))
2597 2597 != NULL) {
2598 2598 swap_xlate(ap, &vp, &offset);
2599 2599 } else {
2600 2600 vp = svd->vp;
2601 2601 offset = svd->offset +
2602 2602 (uintptr_t)(adr - seg->s_base);
2603 2603 }
2604 2604 ANON_LOCK_EXIT(&->a_rwlock);
2605 2605 } else {
2606 2606 vp = svd->vp;
2607 2607 offset = svd->offset +
2608 2608 (uintptr_t)(adr - seg->s_base);
2609 2609 }
2610 2610
2611 2611 /*
2612 2612 * Use page_find() instead of page_lookup() to
2613 2613 * find the page since we know that it is locked.
2614 2614 */
2615 2615 pp = page_find(vp, offset);
2616 2616 if (pp == NULL) {
2617 2617 panic(
2618 2618 "segvn_softunlock: addr %p, ap %p, vp %p, off %llx",
2619 2619 (void *)adr, (void *)ap, (void *)vp, offset);
2620 2620 /*NOTREACHED*/
2621 2621 }
2622 2622
2623 2623 if (rw == S_WRITE) {
2624 2624 hat_setrefmod(pp);
2625 2625 if (seg->s_as->a_vbits)
2626 2626 hat_setstat(seg->s_as, adr, PAGESIZE,
2627 2627 P_REF | P_MOD);
2628 2628 } else if (rw != S_OTHER) {
2629 2629 hat_setref(pp);
2630 2630 if (seg->s_as->a_vbits)
2631 2631 hat_setstat(seg->s_as, adr, PAGESIZE, P_REF);
2632 2632 }
2633 2633 TRACE_3(TR_FAC_VM, TR_SEGVN_FAULT,
2634 2634 "segvn_fault:pp %p vp %p offset %llx", pp, vp, offset);
2635 2635 page_unlock(pp);
2636 2636 }
2637 2637 ASSERT(svd->softlockcnt >= btop(len));
2638 2638 if (!atomic_add_long_nv((ulong_t *)&svd->softlockcnt, -btop(len))) {
2639 2639 /*
2640 2640 * All SOFTLOCKS are gone. Wakeup any waiting
2641 2641 * unmappers so they can try again to unmap.
2642 2642 * Check for waiters first without the mutex
2643 2643 * held so we don't always grab the mutex on
2644 2644 * softunlocks.
2645 2645 */
2646 2646 if (AS_ISUNMAPWAIT(seg->s_as)) {
2647 2647 mutex_enter(&seg->s_as->a_contents);
2648 2648 if (AS_ISUNMAPWAIT(seg->s_as)) {
2649 2649 AS_CLRUNMAPWAIT(seg->s_as);
2650 2650 cv_broadcast(&seg->s_as->a_cv);
2651 2651 }
2652 2652 mutex_exit(&seg->s_as->a_contents);
2653 2653 }
2654 2654 }
2655 2655 }
2656 2656
2657 2657 #define PAGE_HANDLED ((page_t *)-1)
2658 2658
2659 2659 /*
2660 2660 * Release all the pages in the NULL terminated ppp list
2661 2661 * which haven't already been converted to PAGE_HANDLED.
2662 2662 */
2663 2663 static void
2664 2664 segvn_pagelist_rele(page_t **ppp)
2665 2665 {
2666 2666 for (; *ppp != NULL; ppp++) {
2667 2667 if (*ppp != PAGE_HANDLED)
2668 2668 page_unlock(*ppp);
2669 2669 }
2670 2670 }
2671 2671
2672 2672 static int stealcow = 1;
2673 2673
2674 2674 /*
2675 2675 * Workaround for viking chip bug. See bug id 1220902.
2676 2676 * To fix this down in pagefault() would require importing so
2677 2677 * much as and segvn code as to be unmaintainable.
2678 2678 */
2679 2679 int enable_mbit_wa = 0;
2680 2680
2681 2681 /*
2682 2682 * Handles all the dirty work of getting the right
2683 2683 * anonymous pages and loading up the translations.
2684 2684 * This routine is called only from segvn_fault()
2685 2685 * when looping over the range of addresses requested.
2686 2686 *
2687 2687 * The basic algorithm here is:
2688 2688 * If this is an anon_zero case
2689 2689 * Call anon_zero to allocate page
2690 2690 * Load up translation
2691 2691 * Return
2692 2692 * endif
2693 2693 * If this is an anon page
2694 2694 * Use anon_getpage to get the page
2695 2695 * else
2696 2696 * Find page in pl[] list passed in
2697 2697 * endif
2698 2698 * If not a cow
2699 2699 * Load up the translation to the page
2700 2700 * return
2701 2701 * endif
2702 2702 * Call anon_private to handle cow
2703 2703 * Load up (writable) translation to new page
2704 2704 */
2705 2705 static faultcode_t
2706 2706 segvn_faultpage(
2707 2707 struct hat *hat, /* the hat to use for mapping */
2708 2708 struct seg *seg, /* seg_vn of interest */
2709 2709 caddr_t addr, /* address in as */
2710 2710 u_offset_t off, /* offset in vp */
2711 2711 struct vpage *vpage, /* pointer to vpage for vp, off */
2712 2712 page_t *pl[], /* object source page pointer */
2713 2713 uint_t vpprot, /* access allowed to object pages */
2714 2714 enum fault_type type, /* type of fault */
2715 2715 enum seg_rw rw, /* type of access at fault */
2716 2716 int brkcow) /* we may need to break cow */
2717 2717 {
2718 2718 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
2719 2719 page_t *pp, **ppp;
2720 2720 uint_t pageflags = 0;
2721 2721 page_t *anon_pl[1 + 1];
2722 2722 page_t *opp = NULL; /* original page */
2723 2723 uint_t prot;
2724 2724 int err;
2725 2725 int cow;
2726 2726 int claim;
2727 2727 int steal = 0;
2728 2728 ulong_t anon_index;
2729 2729 struct anon *ap, *oldap;
2730 2730 struct anon_map *amp;
2731 2731 int hat_flag = (type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD;
2732 2732 int anon_lock = 0;
2733 2733 anon_sync_obj_t cookie;
2734 2734
2735 2735 if (svd->flags & MAP_TEXT) {
2736 2736 hat_flag |= HAT_LOAD_TEXT;
2737 2737 }
2738 2738
2739 2739 ASSERT(SEGVN_READ_HELD(seg->s_as, &svd->lock));
2740 2740 ASSERT(seg->s_szc == 0);
2741 2741 ASSERT(svd->tr_state != SEGVN_TR_INIT);
2742 2742
2743 2743 /*
2744 2744 * Initialize protection value for this page.
2745 2745 * If we have per page protection values check it now.
2746 2746 */
2747 2747 if (svd->pageprot) {
2748 2748 uint_t protchk;
2749 2749
2750 2750 switch (rw) {
2751 2751 case S_READ:
2752 2752 protchk = PROT_READ;
2753 2753 break;
2754 2754 case S_WRITE:
2755 2755 protchk = PROT_WRITE;
2756 2756 break;
2757 2757 case S_EXEC:
2758 2758 protchk = PROT_EXEC;
2759 2759 break;
2760 2760 case S_OTHER:
2761 2761 default:
2762 2762 protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
2763 2763 break;
2764 2764 }
2765 2765
2766 2766 prot = VPP_PROT(vpage);
2767 2767 if ((prot & protchk) == 0)
2768 2768 return (FC_PROT); /* illegal access type */
2769 2769 } else {
2770 2770 prot = svd->prot;
2771 2771 }
2772 2772
2773 2773 if (type == F_SOFTLOCK) {
2774 2774 atomic_inc_ulong((ulong_t *)&svd->softlockcnt);
2775 2775 }
2776 2776
2777 2777 /*
2778 2778 * Always acquire the anon array lock to prevent 2 threads from
2779 2779 * allocating separate anon slots for the same "addr".
2780 2780 */
2781 2781
2782 2782 if ((amp = svd->amp) != NULL) {
2783 2783 ASSERT(RW_READ_HELD(&->a_rwlock));
2784 2784 anon_index = svd->anon_index + seg_page(seg, addr);
2785 2785 anon_array_enter(amp, anon_index, &cookie);
2786 2786 anon_lock = 1;
2787 2787 }
2788 2788
2789 2789 if (svd->vp == NULL && amp != NULL) {
2790 2790 if ((ap = anon_get_ptr(amp->ahp, anon_index)) == NULL) {
2791 2791 /*
2792 2792 * Allocate a (normally) writable anonymous page of
2793 2793 * zeroes. If no advance reservations, reserve now.
2794 2794 */
2795 2795 if (svd->flags & MAP_NORESERVE) {
2796 2796 if (anon_resv_zone(ptob(1),
2797 2797 seg->s_as->a_proc->p_zone)) {
2798 2798 atomic_add_long(&svd->swresv, ptob(1));
2799 2799 atomic_add_long(&seg->s_as->a_resvsize,
2800 2800 ptob(1));
2801 2801 } else {
2802 2802 err = ENOMEM;
2803 2803 goto out;
2804 2804 }
2805 2805 }
2806 2806 if ((pp = anon_zero(seg, addr, &ap,
2807 2807 svd->cred)) == NULL) {
2808 2808 err = ENOMEM;
2809 2809 goto out; /* out of swap space */
2810 2810 }
2811 2811 /*
2812 2812 * Re-acquire the anon_map lock and
2813 2813 * initialize the anon array entry.
2814 2814 */
2815 2815 (void) anon_set_ptr(amp->ahp, anon_index, ap,
2816 2816 ANON_SLEEP);
2817 2817
2818 2818 ASSERT(pp->p_szc == 0);
2819 2819
2820 2820 /*
2821 2821 * Handle pages that have been marked for migration
2822 2822 */
2823 2823 if (lgrp_optimizations())
2824 2824 page_migrate(seg, addr, &pp, 1);
2825 2825
2826 2826 if (enable_mbit_wa) {
2827 2827 if (rw == S_WRITE)
2828 2828 hat_setmod(pp);
2829 2829 else if (!hat_ismod(pp))
2830 2830 prot &= ~PROT_WRITE;
2831 2831 }
2832 2832 /*
2833 2833 * If AS_PAGLCK is set in a_flags (via memcntl(2)
2834 2834 * with MC_LOCKAS, MCL_FUTURE) and this is a
2835 2835 * MAP_NORESERVE segment, we may need to
2836 2836 * permanently lock the page as it is being faulted
2837 2837 * for the first time. The following text applies
2838 2838 * only to MAP_NORESERVE segments:
2839 2839 *
2840 2840 * As per memcntl(2), if this segment was created
2841 2841 * after MCL_FUTURE was applied (a "future"
2842 2842 * segment), its pages must be locked. If this
2843 2843 * segment existed at MCL_FUTURE application (a
2844 2844 * "past" segment), the interface is unclear.
2845 2845 *
2846 2846 * We decide to lock only if vpage is present:
2847 2847 *
2848 2848 * - "future" segments will have a vpage array (see
2849 2849 * as_map), and so will be locked as required
2850 2850 *
2851 2851 * - "past" segments may not have a vpage array,
2852 2852 * depending on whether events (such as
2853 2853 * mprotect) have occurred. Locking if vpage
2854 2854 * exists will preserve legacy behavior. Not
2855 2855 * locking if vpage is absent, will not break
2856 2856 * the interface or legacy behavior. Note that
2857 2857 * allocating vpage here if it's absent requires
2858 2858 * upgrading the segvn reader lock, the cost of
2859 2859 * which does not seem worthwhile.
2860 2860 *
2861 2861 * Usually testing and setting VPP_ISPPLOCK and
2862 2862 * VPP_SETPPLOCK requires holding the segvn lock as
2863 2863 * writer, but in this case all readers are
2864 2864 * serializing on the anon array lock.
2865 2865 */
2866 2866 if (AS_ISPGLCK(seg->s_as) && vpage != NULL &&
2867 2867 (svd->flags & MAP_NORESERVE) &&
2868 2868 !VPP_ISPPLOCK(vpage)) {
2869 2869 proc_t *p = seg->s_as->a_proc;
2870 2870 ASSERT(svd->type == MAP_PRIVATE);
2871 2871 mutex_enter(&p->p_lock);
2872 2872 if (rctl_incr_locked_mem(p, NULL, PAGESIZE,
2873 2873 1) == 0) {
2874 2874 claim = VPP_PROT(vpage) & PROT_WRITE;
2875 2875 if (page_pp_lock(pp, claim, 0)) {
2876 2876 VPP_SETPPLOCK(vpage);
2877 2877 } else {
2878 2878 rctl_decr_locked_mem(p, NULL,
2879 2879 PAGESIZE, 1);
2880 2880 }
2881 2881 }
2882 2882 mutex_exit(&p->p_lock);
2883 2883 }
2884 2884
2885 2885 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
2886 2886 hat_memload(hat, addr, pp, prot, hat_flag);
2887 2887
2888 2888 if (!(hat_flag & HAT_LOAD_LOCK))
2889 2889 page_unlock(pp);
2890 2890
2891 2891 anon_array_exit(&cookie);
2892 2892 return (0);
2893 2893 }
2894 2894 }
2895 2895
2896 2896 /*
2897 2897 * Obtain the page structure via anon_getpage() if it is
2898 2898 * a private copy of an object (the result of a previous
2899 2899 * copy-on-write).
2900 2900 */
2901 2901 if (amp != NULL) {
2902 2902 if ((ap = anon_get_ptr(amp->ahp, anon_index)) != NULL) {
2903 2903 err = anon_getpage(&ap, &vpprot, anon_pl, PAGESIZE,
2904 2904 seg, addr, rw, svd->cred);
2905 2905 if (err)
2906 2906 goto out;
2907 2907
2908 2908 if (svd->type == MAP_SHARED) {
2909 2909 /*
2910 2910 * If this is a shared mapping to an
2911 2911 * anon_map, then ignore the write
2912 2912 * permissions returned by anon_getpage().
2913 2913 * They apply to the private mappings
2914 2914 * of this anon_map.
2915 2915 */
2916 2916 vpprot |= PROT_WRITE;
2917 2917 }
2918 2918 opp = anon_pl[0];
2919 2919 }
2920 2920 }
2921 2921
2922 2922 /*
2923 2923 * Search the pl[] list passed in if it is from the
2924 2924 * original object (i.e., not a private copy).
2925 2925 */
2926 2926 if (opp == NULL) {
2927 2927 /*
2928 2928 * Find original page. We must be bringing it in
2929 2929 * from the list in pl[].
2930 2930 */
2931 2931 for (ppp = pl; (opp = *ppp) != NULL; ppp++) {
2932 2932 if (opp == PAGE_HANDLED)
2933 2933 continue;
2934 2934 ASSERT(opp->p_vnode == svd->vp); /* XXX */
2935 2935 if (opp->p_offset == off)
2936 2936 break;
2937 2937 }
2938 2938 if (opp == NULL) {
2939 2939 panic("segvn_faultpage not found");
2940 2940 /*NOTREACHED*/
2941 2941 }
2942 2942 *ppp = PAGE_HANDLED;
2943 2943
2944 2944 }
2945 2945
2946 2946 ASSERT(PAGE_LOCKED(opp));
2947 2947
2948 2948 TRACE_3(TR_FAC_VM, TR_SEGVN_FAULT,
2949 2949 "segvn_fault:pp %p vp %p offset %llx", opp, NULL, 0);
2950 2950
2951 2951 /*
2952 2952 * The fault is treated as a copy-on-write fault if a
2953 2953 * write occurs on a private segment and the object
2954 2954 * page (i.e., mapping) is write protected. We assume
2955 2955 * that fatal protection checks have already been made.
2956 2956 */
2957 2957
2958 2958 if (brkcow) {
2959 2959 ASSERT(svd->tr_state == SEGVN_TR_OFF);
2960 2960 cow = !(vpprot & PROT_WRITE);
2961 2961 } else if (svd->tr_state == SEGVN_TR_ON) {
2962 2962 /*
2963 2963 * If we are doing text replication COW on first touch.
2964 2964 */
2965 2965 ASSERT(amp != NULL);
2966 2966 ASSERT(svd->vp != NULL);
2967 2967 ASSERT(rw != S_WRITE);
2968 2968 cow = (ap == NULL);
2969 2969 } else {
2970 2970 cow = 0;
2971 2971 }
2972 2972
2973 2973 /*
2974 2974 * If not a copy-on-write case load the translation
2975 2975 * and return.
2976 2976 */
2977 2977 if (cow == 0) {
2978 2978
2979 2979 /*
2980 2980 * Handle pages that have been marked for migration
2981 2981 */
2982 2982 if (lgrp_optimizations())
2983 2983 page_migrate(seg, addr, &opp, 1);
2984 2984
2985 2985 if (IS_VMODSORT(opp->p_vnode) || enable_mbit_wa) {
2986 2986 if (rw == S_WRITE)
2987 2987 hat_setmod(opp);
2988 2988 else if (rw != S_OTHER && !hat_ismod(opp))
2989 2989 prot &= ~PROT_WRITE;
2990 2990 }
2991 2991
2992 2992 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE ||
2993 2993 (!svd->pageprot && svd->prot == (prot & vpprot)));
2994 2994 ASSERT(amp == NULL ||
2995 2995 svd->rcookie == HAT_INVALID_REGION_COOKIE);
2996 2996 hat_memload_region(hat, addr, opp, prot & vpprot, hat_flag,
2997 2997 svd->rcookie);
2998 2998
2999 2999 if (!(hat_flag & HAT_LOAD_LOCK))
3000 3000 page_unlock(opp);
3001 3001
3002 3002 if (anon_lock) {
3003 3003 anon_array_exit(&cookie);
3004 3004 }
3005 3005 return (0);
3006 3006 }
3007 3007
3008 3008 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
3009 3009
3010 3010 hat_setref(opp);
3011 3011
3012 3012 ASSERT(amp != NULL && anon_lock);
3013 3013
3014 3014 /*
3015 3015 * Steal the page only if it isn't a private page
3016 3016 * since stealing a private page is not worth the effort.
3017 3017 */
3018 3018 if ((ap = anon_get_ptr(amp->ahp, anon_index)) == NULL)
3019 3019 steal = 1;
3020 3020
3021 3021 /*
3022 3022 * Steal the original page if the following conditions are true:
3023 3023 *
3024 3024 * We are low on memory, the page is not private, page is not large,
3025 3025 * not shared, not modified, not `locked' or if we have it `locked'
3026 3026 * (i.e., p_cowcnt == 1 and p_lckcnt == 0, which also implies
3027 3027 * that the page is not shared) and if it doesn't have any
3028 3028 * translations. page_struct_lock isn't needed to look at p_cowcnt
3029 3029 * and p_lckcnt because we first get exclusive lock on page.
3030 3030 */
3031 3031 (void) hat_pagesync(opp, HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD);
3032 3032
3033 3033 if (stealcow && freemem < minfree && steal && opp->p_szc == 0 &&
3034 3034 page_tryupgrade(opp) && !hat_ismod(opp) &&
3035 3035 ((opp->p_lckcnt == 0 && opp->p_cowcnt == 0) ||
3036 3036 (opp->p_lckcnt == 0 && opp->p_cowcnt == 1 &&
3037 3037 vpage != NULL && VPP_ISPPLOCK(vpage)))) {
3038 3038 /*
3039 3039 * Check if this page has other translations
3040 3040 * after unloading our translation.
3041 3041 */
3042 3042 if (hat_page_is_mapped(opp)) {
3043 3043 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
3044 3044 hat_unload(seg->s_as->a_hat, addr, PAGESIZE,
3045 3045 HAT_UNLOAD);
3046 3046 }
3047 3047
3048 3048 /*
3049 3049 * hat_unload() might sync back someone else's recent
3050 3050 * modification, so check again.
3051 3051 */
3052 3052 if (!hat_ismod(opp) && !hat_page_is_mapped(opp))
3053 3053 pageflags |= STEAL_PAGE;
3054 3054 }
3055 3055
3056 3056 /*
3057 3057 * If we have a vpage pointer, see if it indicates that we have
3058 3058 * ``locked'' the page we map -- if so, tell anon_private to
3059 3059 * transfer the locking resource to the new page.
3060 3060 *
3061 3061 * See Statement at the beginning of segvn_lockop regarding
3062 3062 * the way lockcnts/cowcnts are handled during COW.
3063 3063 *
3064 3064 */
3065 3065 if (vpage != NULL && VPP_ISPPLOCK(vpage))
3066 3066 pageflags |= LOCK_PAGE;
3067 3067
3068 3068 /*
3069 3069 * Allocate a private page and perform the copy.
3070 3070 * For MAP_NORESERVE reserve swap space now, unless this
3071 3071 * is a cow fault on an existing anon page in which case
3072 3072 * MAP_NORESERVE will have made advance reservations.
3073 3073 */
3074 3074 if ((svd->flags & MAP_NORESERVE) && (ap == NULL)) {
3075 3075 if (anon_resv_zone(ptob(1), seg->s_as->a_proc->p_zone)) {
3076 3076 atomic_add_long(&svd->swresv, ptob(1));
3077 3077 atomic_add_long(&seg->s_as->a_resvsize, ptob(1));
3078 3078 } else {
3079 3079 page_unlock(opp);
3080 3080 err = ENOMEM;
3081 3081 goto out;
3082 3082 }
3083 3083 }
3084 3084 oldap = ap;
3085 3085 pp = anon_private(&ap, seg, addr, prot, opp, pageflags, svd->cred);
3086 3086 if (pp == NULL) {
3087 3087 err = ENOMEM; /* out of swap space */
3088 3088 goto out;
3089 3089 }
3090 3090
3091 3091 /*
3092 3092 * If we copied away from an anonymous page, then
3093 3093 * we are one step closer to freeing up an anon slot.
3094 3094 *
3095 3095 * NOTE: The original anon slot must be released while
3096 3096 * holding the "anon_map" lock. This is necessary to prevent
3097 3097 * other threads from obtaining a pointer to the anon slot
3098 3098 * which may be freed if its "refcnt" is 1.
3099 3099 */
3100 3100 if (oldap != NULL)
3101 3101 anon_decref(oldap);
3102 3102
3103 3103 (void) anon_set_ptr(amp->ahp, anon_index, ap, ANON_SLEEP);
3104 3104
3105 3105 /*
3106 3106 * Handle pages that have been marked for migration
3107 3107 */
3108 3108 if (lgrp_optimizations())
3109 3109 page_migrate(seg, addr, &pp, 1);
3110 3110
3111 3111 ASSERT(pp->p_szc == 0);
3112 3112
3113 3113 ASSERT(!IS_VMODSORT(pp->p_vnode));
3114 3114 if (enable_mbit_wa) {
3115 3115 if (rw == S_WRITE)
3116 3116 hat_setmod(pp);
3117 3117 else if (!hat_ismod(pp))
3118 3118 prot &= ~PROT_WRITE;
3119 3119 }
3120 3120
3121 3121 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
3122 3122 hat_memload(hat, addr, pp, prot, hat_flag);
3123 3123
3124 3124 if (!(hat_flag & HAT_LOAD_LOCK))
3125 3125 page_unlock(pp);
3126 3126
3127 3127 ASSERT(anon_lock);
3128 3128 anon_array_exit(&cookie);
3129 3129 return (0);
3130 3130 out:
3131 3131 if (anon_lock)
3132 3132 anon_array_exit(&cookie);
3133 3133
3134 3134 if (type == F_SOFTLOCK) {
3135 3135 atomic_dec_ulong((ulong_t *)&svd->softlockcnt);
3136 3136 }
3137 3137 return (FC_MAKE_ERR(err));
3138 3138 }
3139 3139
3140 3140 /*
3141 3141 * relocate a bunch of smaller targ pages into one large repl page. all targ
3142 3142 * pages must be complete pages smaller than replacement pages.
3143 3143 * it's assumed that no page's szc can change since they are all PAGESIZE or
3144 3144 * complete large pages locked SHARED.
3145 3145 */
3146 3146 static void
3147 3147 segvn_relocate_pages(page_t **targ, page_t *replacement)
3148 3148 {
3149 3149 page_t *pp;
3150 3150 pgcnt_t repl_npgs, curnpgs;
3151 3151 pgcnt_t i;
3152 3152 uint_t repl_szc = replacement->p_szc;
3153 3153 page_t *first_repl = replacement;
3154 3154 page_t *repl;
3155 3155 spgcnt_t npgs;
3156 3156
3157 3157 VM_STAT_ADD(segvnvmstats.relocatepages[0]);
3158 3158
3159 3159 ASSERT(repl_szc != 0);
3160 3160 npgs = repl_npgs = page_get_pagecnt(repl_szc);
3161 3161
3162 3162 i = 0;
3163 3163 while (repl_npgs) {
3164 3164 spgcnt_t nreloc;
3165 3165 int err;
3166 3166 ASSERT(replacement != NULL);
3167 3167 pp = targ[i];
3168 3168 ASSERT(pp->p_szc < repl_szc);
3169 3169 ASSERT(PAGE_EXCL(pp));
3170 3170 ASSERT(!PP_ISFREE(pp));
3171 3171 curnpgs = page_get_pagecnt(pp->p_szc);
3172 3172 if (curnpgs == 1) {
3173 3173 VM_STAT_ADD(segvnvmstats.relocatepages[1]);
3174 3174 repl = replacement;
3175 3175 page_sub(&replacement, repl);
3176 3176 ASSERT(PAGE_EXCL(repl));
3177 3177 ASSERT(!PP_ISFREE(repl));
3178 3178 ASSERT(repl->p_szc == repl_szc);
3179 3179 } else {
3180 3180 page_t *repl_savepp;
3181 3181 int j;
3182 3182 VM_STAT_ADD(segvnvmstats.relocatepages[2]);
3183 3183 repl_savepp = replacement;
3184 3184 for (j = 0; j < curnpgs; j++) {
3185 3185 repl = replacement;
3186 3186 page_sub(&replacement, repl);
3187 3187 ASSERT(PAGE_EXCL(repl));
3188 3188 ASSERT(!PP_ISFREE(repl));
3189 3189 ASSERT(repl->p_szc == repl_szc);
3190 3190 ASSERT(page_pptonum(targ[i + j]) ==
3191 3191 page_pptonum(targ[i]) + j);
3192 3192 }
3193 3193 repl = repl_savepp;
3194 3194 ASSERT(IS_P2ALIGNED(page_pptonum(repl), curnpgs));
3195 3195 }
3196 3196 err = page_relocate(&pp, &repl, 0, 1, &nreloc, NULL);
3197 3197 if (err || nreloc != curnpgs) {
3198 3198 panic("segvn_relocate_pages: "
3199 3199 "page_relocate failed err=%d curnpgs=%ld "
3200 3200 "nreloc=%ld", err, curnpgs, nreloc);
3201 3201 }
3202 3202 ASSERT(curnpgs <= repl_npgs);
3203 3203 repl_npgs -= curnpgs;
3204 3204 i += curnpgs;
3205 3205 }
3206 3206 ASSERT(replacement == NULL);
3207 3207
3208 3208 repl = first_repl;
3209 3209 repl_npgs = npgs;
3210 3210 for (i = 0; i < repl_npgs; i++) {
3211 3211 ASSERT(PAGE_EXCL(repl));
3212 3212 ASSERT(!PP_ISFREE(repl));
3213 3213 targ[i] = repl;
3214 3214 page_downgrade(targ[i]);
3215 3215 repl++;
3216 3216 }
3217 3217 }
3218 3218
3219 3219 /*
3220 3220 * Check if all pages in ppa array are complete smaller than szc pages and
3221 3221 * their roots will still be aligned relative to their current size if the
3222 3222 * entire ppa array is relocated into one szc page. If these conditions are
3223 3223 * not met return 0.
3224 3224 *
3225 3225 * If all pages are properly aligned attempt to upgrade their locks
3226 3226 * to exclusive mode. If it fails set *upgrdfail to 1 and return 0.
3227 3227 * upgrdfail was set to 0 by caller.
3228 3228 *
3229 3229 * Return 1 if all pages are aligned and locked exclusively.
3230 3230 *
3231 3231 * If all pages in ppa array happen to be physically contiguous to make one
3232 3232 * szc page and all exclusive locks are successfully obtained promote the page
3233 3233 * size to szc and set *pszc to szc. Return 1 with pages locked shared.
3234 3234 */
3235 3235 static int
3236 3236 segvn_full_szcpages(page_t **ppa, uint_t szc, int *upgrdfail, uint_t *pszc)
3237 3237 {
3238 3238 page_t *pp;
3239 3239 pfn_t pfn;
3240 3240 pgcnt_t totnpgs = page_get_pagecnt(szc);
3241 3241 pfn_t first_pfn;
3242 3242 int contig = 1;
3243 3243 pgcnt_t i;
3244 3244 pgcnt_t j;
3245 3245 uint_t curszc;
3246 3246 pgcnt_t curnpgs;
3247 3247 int root = 0;
3248 3248
3249 3249 ASSERT(szc > 0);
3250 3250
3251 3251 VM_STAT_ADD(segvnvmstats.fullszcpages[0]);
3252 3252
3253 3253 for (i = 0; i < totnpgs; i++) {
3254 3254 pp = ppa[i];
3255 3255 ASSERT(PAGE_SHARED(pp));
3256 3256 ASSERT(!PP_ISFREE(pp));
3257 3257 pfn = page_pptonum(pp);
3258 3258 if (i == 0) {
3259 3259 if (!IS_P2ALIGNED(pfn, totnpgs)) {
3260 3260 contig = 0;
3261 3261 } else {
3262 3262 first_pfn = pfn;
3263 3263 }
3264 3264 } else if (contig && pfn != first_pfn + i) {
3265 3265 contig = 0;
3266 3266 }
3267 3267 if (pp->p_szc == 0) {
3268 3268 if (root) {
3269 3269 VM_STAT_ADD(segvnvmstats.fullszcpages[1]);
3270 3270 return (0);
3271 3271 }
3272 3272 } else if (!root) {
3273 3273 if ((curszc = pp->p_szc) >= szc) {
3274 3274 VM_STAT_ADD(segvnvmstats.fullszcpages[2]);
3275 3275 return (0);
3276 3276 }
3277 3277 if (curszc == 0) {
3278 3278 /*
3279 3279 * p_szc changed means we don't have all pages
3280 3280 * locked. return failure.
3281 3281 */
3282 3282 VM_STAT_ADD(segvnvmstats.fullszcpages[3]);
3283 3283 return (0);
3284 3284 }
3285 3285 curnpgs = page_get_pagecnt(curszc);
3286 3286 if (!IS_P2ALIGNED(pfn, curnpgs) ||
3287 3287 !IS_P2ALIGNED(i, curnpgs)) {
3288 3288 VM_STAT_ADD(segvnvmstats.fullszcpages[4]);
3289 3289 return (0);
3290 3290 }
3291 3291 root = 1;
3292 3292 } else {
3293 3293 ASSERT(i > 0);
3294 3294 VM_STAT_ADD(segvnvmstats.fullszcpages[5]);
3295 3295 if (pp->p_szc != curszc) {
3296 3296 VM_STAT_ADD(segvnvmstats.fullszcpages[6]);
3297 3297 return (0);
3298 3298 }
3299 3299 if (pfn - 1 != page_pptonum(ppa[i - 1])) {
3300 3300 panic("segvn_full_szcpages: "
3301 3301 "large page not physically contiguous");
3302 3302 }
3303 3303 if (P2PHASE(pfn, curnpgs) == curnpgs - 1) {
3304 3304 root = 0;
3305 3305 }
3306 3306 }
3307 3307 }
3308 3308
3309 3309 for (i = 0; i < totnpgs; i++) {
3310 3310 ASSERT(ppa[i]->p_szc < szc);
3311 3311 if (!page_tryupgrade(ppa[i])) {
3312 3312 for (j = 0; j < i; j++) {
3313 3313 page_downgrade(ppa[j]);
3314 3314 }
3315 3315 *pszc = ppa[i]->p_szc;
3316 3316 *upgrdfail = 1;
3317 3317 VM_STAT_ADD(segvnvmstats.fullszcpages[7]);
3318 3318 return (0);
3319 3319 }
3320 3320 }
3321 3321
3322 3322 /*
3323 3323 * When a page is put a free cachelist its szc is set to 0. if file
3324 3324 * system reclaimed pages from cachelist targ pages will be physically
3325 3325 * contiguous with 0 p_szc. in this case just upgrade szc of targ
3326 3326 * pages without any relocations.
3327 3327 * To avoid any hat issues with previous small mappings
3328 3328 * hat_pageunload() the target pages first.
3329 3329 */
3330 3330 if (contig) {
3331 3331 VM_STAT_ADD(segvnvmstats.fullszcpages[8]);
3332 3332 for (i = 0; i < totnpgs; i++) {
3333 3333 (void) hat_pageunload(ppa[i], HAT_FORCE_PGUNLOAD);
3334 3334 }
3335 3335 for (i = 0; i < totnpgs; i++) {
3336 3336 ppa[i]->p_szc = szc;
3337 3337 }
3338 3338 for (i = 0; i < totnpgs; i++) {
3339 3339 ASSERT(PAGE_EXCL(ppa[i]));
3340 3340 page_downgrade(ppa[i]);
3341 3341 }
3342 3342 if (pszc != NULL) {
3343 3343 *pszc = szc;
3344 3344 }
3345 3345 }
3346 3346 VM_STAT_ADD(segvnvmstats.fullszcpages[9]);
3347 3347 return (1);
3348 3348 }
3349 3349
3350 3350 /*
3351 3351 * Create physically contiguous pages for [vp, off] - [vp, off +
3352 3352 * page_size(szc)) range and for private segment return them in ppa array.
3353 3353 * Pages are created either via IO or relocations.
3354 3354 *
3355 3355 * Return 1 on success and 0 on failure.
3356 3356 *
3357 3357 * If physically contiguous pages already exist for this range return 1 without
3358 3358 * filling ppa array. Caller initializes ppa[0] as NULL to detect that ppa
3359 3359 * array wasn't filled. In this case caller fills ppa array via VOP_GETPAGE().
3360 3360 */
3361 3361
3362 3362 static int
3363 3363 segvn_fill_vp_pages(struct segvn_data *svd, vnode_t *vp, u_offset_t off,
3364 3364 uint_t szc, page_t **ppa, page_t **ppplist, uint_t *ret_pszc,
3365 3365 int *downsize)
3366 3366 {
3367 3367 page_t *pplist = *ppplist;
3368 3368 size_t pgsz = page_get_pagesize(szc);
3369 3369 pgcnt_t pages = btop(pgsz);
3370 3370 ulong_t start_off = off;
3371 3371 u_offset_t eoff = off + pgsz;
3372 3372 spgcnt_t nreloc;
3373 3373 u_offset_t io_off = off;
3374 3374 size_t io_len;
3375 3375 page_t *io_pplist = NULL;
3376 3376 page_t *done_pplist = NULL;
3377 3377 pgcnt_t pgidx = 0;
3378 3378 page_t *pp;
3379 3379 page_t *newpp;
3380 3380 page_t *targpp;
3381 3381 int io_err = 0;
3382 3382 int i;
3383 3383 pfn_t pfn;
3384 3384 ulong_t ppages;
3385 3385 page_t *targ_pplist = NULL;
3386 3386 page_t *repl_pplist = NULL;
3387 3387 page_t *tmp_pplist;
3388 3388 int nios = 0;
3389 3389 uint_t pszc;
3390 3390 struct vattr va;
3391 3391
3392 3392 VM_STAT_ADD(segvnvmstats.fill_vp_pages[0]);
3393 3393
3394 3394 ASSERT(szc != 0);
3395 3395 ASSERT(pplist->p_szc == szc);
3396 3396
3397 3397 /*
3398 3398 * downsize will be set to 1 only if we fail to lock pages. this will
3399 3399 * allow subsequent faults to try to relocate the page again. If we
3400 3400 * fail due to misalignment don't downsize and let the caller map the
3401 3401 * whole region with small mappings to avoid more faults into the area
3402 3402 * where we can't get large pages anyway.
3403 3403 */
3404 3404 *downsize = 0;
3405 3405
3406 3406 while (off < eoff) {
3407 3407 newpp = pplist;
3408 3408 ASSERT(newpp != NULL);
3409 3409 ASSERT(PAGE_EXCL(newpp));
3410 3410 ASSERT(!PP_ISFREE(newpp));
3411 3411 /*
3412 3412 * we pass NULL for nrelocp to page_lookup_create()
3413 3413 * so that it doesn't relocate. We relocate here
3414 3414 * later only after we make sure we can lock all
3415 3415 * pages in the range we handle and they are all
3416 3416 * aligned.
3417 3417 */
3418 3418 pp = page_lookup_create(vp, off, SE_SHARED, newpp, NULL, 0);
3419 3419 ASSERT(pp != NULL);
3420 3420 ASSERT(!PP_ISFREE(pp));
3421 3421 ASSERT(pp->p_vnode == vp);
3422 3422 ASSERT(pp->p_offset == off);
3423 3423 if (pp == newpp) {
3424 3424 VM_STAT_ADD(segvnvmstats.fill_vp_pages[1]);
3425 3425 page_sub(&pplist, pp);
3426 3426 ASSERT(PAGE_EXCL(pp));
3427 3427 ASSERT(page_iolock_assert(pp));
3428 3428 page_list_concat(&io_pplist, &pp);
3429 3429 off += PAGESIZE;
3430 3430 continue;
3431 3431 }
3432 3432 VM_STAT_ADD(segvnvmstats.fill_vp_pages[2]);
3433 3433 pfn = page_pptonum(pp);
3434 3434 pszc = pp->p_szc;
3435 3435 if (pszc >= szc && targ_pplist == NULL && io_pplist == NULL &&
3436 3436 IS_P2ALIGNED(pfn, pages)) {
3437 3437 ASSERT(repl_pplist == NULL);
3438 3438 ASSERT(done_pplist == NULL);
3439 3439 ASSERT(pplist == *ppplist);
3440 3440 page_unlock(pp);
3441 3441 page_free_replacement_page(pplist);
3442 3442 page_create_putback(pages);
3443 3443 *ppplist = NULL;
3444 3444 VM_STAT_ADD(segvnvmstats.fill_vp_pages[3]);
3445 3445 return (1);
3446 3446 }
3447 3447 if (pszc >= szc) {
3448 3448 page_unlock(pp);
3449 3449 segvn_faultvnmpss_align_err1++;
3450 3450 goto out;
3451 3451 }
3452 3452 ppages = page_get_pagecnt(pszc);
3453 3453 if (!IS_P2ALIGNED(pfn, ppages)) {
3454 3454 ASSERT(pszc > 0);
3455 3455 /*
3456 3456 * sizing down to pszc won't help.
3457 3457 */
3458 3458 page_unlock(pp);
3459 3459 segvn_faultvnmpss_align_err2++;
3460 3460 goto out;
3461 3461 }
3462 3462 pfn = page_pptonum(newpp);
3463 3463 if (!IS_P2ALIGNED(pfn, ppages)) {
3464 3464 ASSERT(pszc > 0);
3465 3465 /*
3466 3466 * sizing down to pszc won't help.
3467 3467 */
3468 3468 page_unlock(pp);
3469 3469 segvn_faultvnmpss_align_err3++;
3470 3470 goto out;
3471 3471 }
3472 3472 if (!PAGE_EXCL(pp)) {
3473 3473 VM_STAT_ADD(segvnvmstats.fill_vp_pages[4]);
3474 3474 page_unlock(pp);
3475 3475 *downsize = 1;
3476 3476 *ret_pszc = pp->p_szc;
3477 3477 goto out;
3478 3478 }
3479 3479 targpp = pp;
3480 3480 if (io_pplist != NULL) {
3481 3481 VM_STAT_ADD(segvnvmstats.fill_vp_pages[5]);
3482 3482 io_len = off - io_off;
3483 3483 /*
3484 3484 * Some file systems like NFS don't check EOF
3485 3485 * conditions in VOP_PAGEIO(). Check it here
3486 3486 * now that pages are locked SE_EXCL. Any file
3487 3487 * truncation will wait until the pages are
3488 3488 * unlocked so no need to worry that file will
3489 3489 * be truncated after we check its size here.
3490 3490 * XXX fix NFS to remove this check.
3491 3491 */
3492 3492 va.va_mask = AT_SIZE;
3493 3493 if (VOP_GETATTR(vp, &va, ATTR_HINT, svd->cred, NULL)) {
3494 3494 VM_STAT_ADD(segvnvmstats.fill_vp_pages[6]);
3495 3495 page_unlock(targpp);
3496 3496 goto out;
3497 3497 }
3498 3498 if (btopr(va.va_size) < btopr(io_off + io_len)) {
3499 3499 VM_STAT_ADD(segvnvmstats.fill_vp_pages[7]);
3500 3500 *downsize = 1;
3501 3501 *ret_pszc = 0;
3502 3502 page_unlock(targpp);
3503 3503 goto out;
3504 3504 }
3505 3505 io_err = VOP_PAGEIO(vp, io_pplist, io_off, io_len,
3506 3506 B_READ, svd->cred, NULL);
3507 3507 if (io_err) {
3508 3508 VM_STAT_ADD(segvnvmstats.fill_vp_pages[8]);
3509 3509 page_unlock(targpp);
3510 3510 if (io_err == EDEADLK) {
3511 3511 segvn_vmpss_pageio_deadlk_err++;
3512 3512 }
3513 3513 goto out;
3514 3514 }
3515 3515 nios++;
3516 3516 VM_STAT_ADD(segvnvmstats.fill_vp_pages[9]);
3517 3517 while (io_pplist != NULL) {
3518 3518 pp = io_pplist;
3519 3519 page_sub(&io_pplist, pp);
3520 3520 ASSERT(page_iolock_assert(pp));
3521 3521 page_io_unlock(pp);
3522 3522 pgidx = (pp->p_offset - start_off) >>
3523 3523 PAGESHIFT;
3524 3524 ASSERT(pgidx < pages);
3525 3525 ppa[pgidx] = pp;
3526 3526 page_list_concat(&done_pplist, &pp);
3527 3527 }
3528 3528 }
3529 3529 pp = targpp;
3530 3530 ASSERT(PAGE_EXCL(pp));
3531 3531 ASSERT(pp->p_szc <= pszc);
3532 3532 if (pszc != 0 && !group_page_trylock(pp, SE_EXCL)) {
3533 3533 VM_STAT_ADD(segvnvmstats.fill_vp_pages[10]);
3534 3534 page_unlock(pp);
3535 3535 *downsize = 1;
3536 3536 *ret_pszc = pp->p_szc;
3537 3537 goto out;
3538 3538 }
3539 3539 VM_STAT_ADD(segvnvmstats.fill_vp_pages[11]);
3540 3540 /*
3541 3541 * page szc chould have changed before the entire group was
3542 3542 * locked. reread page szc.
3543 3543 */
3544 3544 pszc = pp->p_szc;
3545 3545 ppages = page_get_pagecnt(pszc);
3546 3546
3547 3547 /* link just the roots */
3548 3548 page_list_concat(&targ_pplist, &pp);
3549 3549 page_sub(&pplist, newpp);
3550 3550 page_list_concat(&repl_pplist, &newpp);
3551 3551 off += PAGESIZE;
3552 3552 while (--ppages != 0) {
3553 3553 newpp = pplist;
3554 3554 page_sub(&pplist, newpp);
3555 3555 off += PAGESIZE;
3556 3556 }
3557 3557 io_off = off;
3558 3558 }
3559 3559 if (io_pplist != NULL) {
3560 3560 VM_STAT_ADD(segvnvmstats.fill_vp_pages[12]);
3561 3561 io_len = eoff - io_off;
3562 3562 va.va_mask = AT_SIZE;
3563 3563 if (VOP_GETATTR(vp, &va, ATTR_HINT, svd->cred, NULL) != 0) {
3564 3564 VM_STAT_ADD(segvnvmstats.fill_vp_pages[13]);
3565 3565 goto out;
3566 3566 }
3567 3567 if (btopr(va.va_size) < btopr(io_off + io_len)) {
3568 3568 VM_STAT_ADD(segvnvmstats.fill_vp_pages[14]);
3569 3569 *downsize = 1;
3570 3570 *ret_pszc = 0;
3571 3571 goto out;
3572 3572 }
3573 3573 io_err = VOP_PAGEIO(vp, io_pplist, io_off, io_len,
3574 3574 B_READ, svd->cred, NULL);
3575 3575 if (io_err) {
3576 3576 VM_STAT_ADD(segvnvmstats.fill_vp_pages[15]);
3577 3577 if (io_err == EDEADLK) {
3578 3578 segvn_vmpss_pageio_deadlk_err++;
3579 3579 }
3580 3580 goto out;
3581 3581 }
3582 3582 nios++;
3583 3583 while (io_pplist != NULL) {
3584 3584 pp = io_pplist;
3585 3585 page_sub(&io_pplist, pp);
3586 3586 ASSERT(page_iolock_assert(pp));
3587 3587 page_io_unlock(pp);
3588 3588 pgidx = (pp->p_offset - start_off) >> PAGESHIFT;
3589 3589 ASSERT(pgidx < pages);
3590 3590 ppa[pgidx] = pp;
3591 3591 }
3592 3592 }
3593 3593 /*
3594 3594 * we're now bound to succeed or panic.
3595 3595 * remove pages from done_pplist. it's not needed anymore.
3596 3596 */
3597 3597 while (done_pplist != NULL) {
3598 3598 pp = done_pplist;
3599 3599 page_sub(&done_pplist, pp);
3600 3600 }
3601 3601 VM_STAT_ADD(segvnvmstats.fill_vp_pages[16]);
3602 3602 ASSERT(pplist == NULL);
3603 3603 *ppplist = NULL;
3604 3604 while (targ_pplist != NULL) {
3605 3605 int ret;
3606 3606 VM_STAT_ADD(segvnvmstats.fill_vp_pages[17]);
3607 3607 ASSERT(repl_pplist);
3608 3608 pp = targ_pplist;
3609 3609 page_sub(&targ_pplist, pp);
3610 3610 pgidx = (pp->p_offset - start_off) >> PAGESHIFT;
3611 3611 newpp = repl_pplist;
3612 3612 page_sub(&repl_pplist, newpp);
3613 3613 #ifdef DEBUG
3614 3614 pfn = page_pptonum(pp);
3615 3615 pszc = pp->p_szc;
3616 3616 ppages = page_get_pagecnt(pszc);
3617 3617 ASSERT(IS_P2ALIGNED(pfn, ppages));
3618 3618 pfn = page_pptonum(newpp);
3619 3619 ASSERT(IS_P2ALIGNED(pfn, ppages));
3620 3620 ASSERT(P2PHASE(pfn, pages) == pgidx);
3621 3621 #endif
3622 3622 nreloc = 0;
3623 3623 ret = page_relocate(&pp, &newpp, 0, 1, &nreloc, NULL);
3624 3624 if (ret != 0 || nreloc == 0) {
3625 3625 panic("segvn_fill_vp_pages: "
3626 3626 "page_relocate failed");
3627 3627 }
3628 3628 pp = newpp;
3629 3629 while (nreloc-- != 0) {
3630 3630 ASSERT(PAGE_EXCL(pp));
3631 3631 ASSERT(pp->p_vnode == vp);
3632 3632 ASSERT(pgidx ==
3633 3633 ((pp->p_offset - start_off) >> PAGESHIFT));
3634 3634 ppa[pgidx++] = pp;
3635 3635 pp++;
3636 3636 }
3637 3637 }
3638 3638
3639 3639 if (svd->type == MAP_PRIVATE) {
3640 3640 VM_STAT_ADD(segvnvmstats.fill_vp_pages[18]);
3641 3641 for (i = 0; i < pages; i++) {
3642 3642 ASSERT(ppa[i] != NULL);
3643 3643 ASSERT(PAGE_EXCL(ppa[i]));
3644 3644 ASSERT(ppa[i]->p_vnode == vp);
3645 3645 ASSERT(ppa[i]->p_offset ==
3646 3646 start_off + (i << PAGESHIFT));
3647 3647 page_downgrade(ppa[i]);
3648 3648 }
3649 3649 ppa[pages] = NULL;
3650 3650 } else {
3651 3651 VM_STAT_ADD(segvnvmstats.fill_vp_pages[19]);
3652 3652 /*
3653 3653 * the caller will still call VOP_GETPAGE() for shared segments
3654 3654 * to check FS write permissions. For private segments we map
3655 3655 * file read only anyway. so no VOP_GETPAGE is needed.
3656 3656 */
3657 3657 for (i = 0; i < pages; i++) {
3658 3658 ASSERT(ppa[i] != NULL);
3659 3659 ASSERT(PAGE_EXCL(ppa[i]));
3660 3660 ASSERT(ppa[i]->p_vnode == vp);
3661 3661 ASSERT(ppa[i]->p_offset ==
3662 3662 start_off + (i << PAGESHIFT));
3663 3663 page_unlock(ppa[i]);
3664 3664 }
3665 3665 ppa[0] = NULL;
3666 3666 }
3667 3667
3668 3668 return (1);
3669 3669 out:
3670 3670 /*
3671 3671 * Do the cleanup. Unlock target pages we didn't relocate. They are
3672 3672 * linked on targ_pplist by root pages. reassemble unused replacement
3673 3673 * and io pages back to pplist.
3674 3674 */
3675 3675 if (io_pplist != NULL) {
3676 3676 VM_STAT_ADD(segvnvmstats.fill_vp_pages[20]);
3677 3677 pp = io_pplist;
3678 3678 do {
3679 3679 ASSERT(pp->p_vnode == vp);
3680 3680 ASSERT(pp->p_offset == io_off);
3681 3681 ASSERT(page_iolock_assert(pp));
3682 3682 page_io_unlock(pp);
3683 3683 page_hashout(pp, NULL);
3684 3684 io_off += PAGESIZE;
3685 3685 } while ((pp = pp->p_next) != io_pplist);
3686 3686 page_list_concat(&io_pplist, &pplist);
3687 3687 pplist = io_pplist;
3688 3688 }
3689 3689 tmp_pplist = NULL;
3690 3690 while (targ_pplist != NULL) {
3691 3691 VM_STAT_ADD(segvnvmstats.fill_vp_pages[21]);
3692 3692 pp = targ_pplist;
3693 3693 ASSERT(PAGE_EXCL(pp));
3694 3694 page_sub(&targ_pplist, pp);
3695 3695
3696 3696 pszc = pp->p_szc;
3697 3697 ppages = page_get_pagecnt(pszc);
3698 3698 ASSERT(IS_P2ALIGNED(page_pptonum(pp), ppages));
3699 3699
3700 3700 if (pszc != 0) {
3701 3701 group_page_unlock(pp);
3702 3702 }
3703 3703 page_unlock(pp);
3704 3704
3705 3705 pp = repl_pplist;
3706 3706 ASSERT(pp != NULL);
3707 3707 ASSERT(PAGE_EXCL(pp));
3708 3708 ASSERT(pp->p_szc == szc);
3709 3709 page_sub(&repl_pplist, pp);
3710 3710
3711 3711 ASSERT(IS_P2ALIGNED(page_pptonum(pp), ppages));
3712 3712
3713 3713 /* relink replacement page */
3714 3714 page_list_concat(&tmp_pplist, &pp);
3715 3715 while (--ppages != 0) {
3716 3716 VM_STAT_ADD(segvnvmstats.fill_vp_pages[22]);
3717 3717 pp++;
3718 3718 ASSERT(PAGE_EXCL(pp));
3719 3719 ASSERT(pp->p_szc == szc);
3720 3720 page_list_concat(&tmp_pplist, &pp);
3721 3721 }
3722 3722 }
3723 3723 if (tmp_pplist != NULL) {
3724 3724 VM_STAT_ADD(segvnvmstats.fill_vp_pages[23]);
3725 3725 page_list_concat(&tmp_pplist, &pplist);
3726 3726 pplist = tmp_pplist;
3727 3727 }
3728 3728 /*
3729 3729 * at this point all pages are either on done_pplist or
3730 3730 * pplist. They can't be all on done_pplist otherwise
3731 3731 * we'd've been done.
3732 3732 */
3733 3733 ASSERT(pplist != NULL);
3734 3734 if (nios != 0) {
3735 3735 VM_STAT_ADD(segvnvmstats.fill_vp_pages[24]);
3736 3736 pp = pplist;
3737 3737 do {
3738 3738 VM_STAT_ADD(segvnvmstats.fill_vp_pages[25]);
3739 3739 ASSERT(pp->p_szc == szc);
3740 3740 ASSERT(PAGE_EXCL(pp));
3741 3741 ASSERT(pp->p_vnode != vp);
3742 3742 pp->p_szc = 0;
3743 3743 } while ((pp = pp->p_next) != pplist);
3744 3744
3745 3745 pp = done_pplist;
3746 3746 do {
3747 3747 VM_STAT_ADD(segvnvmstats.fill_vp_pages[26]);
3748 3748 ASSERT(pp->p_szc == szc);
3749 3749 ASSERT(PAGE_EXCL(pp));
3750 3750 ASSERT(pp->p_vnode == vp);
3751 3751 pp->p_szc = 0;
3752 3752 } while ((pp = pp->p_next) != done_pplist);
3753 3753
3754 3754 while (pplist != NULL) {
3755 3755 VM_STAT_ADD(segvnvmstats.fill_vp_pages[27]);
3756 3756 pp = pplist;
3757 3757 page_sub(&pplist, pp);
3758 3758 page_free(pp, 0);
3759 3759 }
3760 3760
3761 3761 while (done_pplist != NULL) {
3762 3762 VM_STAT_ADD(segvnvmstats.fill_vp_pages[28]);
3763 3763 pp = done_pplist;
3764 3764 page_sub(&done_pplist, pp);
3765 3765 page_unlock(pp);
3766 3766 }
3767 3767 *ppplist = NULL;
3768 3768 return (0);
3769 3769 }
3770 3770 ASSERT(pplist == *ppplist);
3771 3771 if (io_err) {
3772 3772 VM_STAT_ADD(segvnvmstats.fill_vp_pages[29]);
3773 3773 /*
3774 3774 * don't downsize on io error.
3775 3775 * see if vop_getpage succeeds.
3776 3776 * pplist may still be used in this case
3777 3777 * for relocations.
3778 3778 */
3779 3779 return (0);
3780 3780 }
3781 3781 VM_STAT_ADD(segvnvmstats.fill_vp_pages[30]);
3782 3782 page_free_replacement_page(pplist);
3783 3783 page_create_putback(pages);
3784 3784 *ppplist = NULL;
3785 3785 return (0);
3786 3786 }
3787 3787
3788 3788 int segvn_anypgsz = 0;
3789 3789
3790 3790 #define SEGVN_RESTORE_SOFTLOCK_VP(type, pages) \
3791 3791 if ((type) == F_SOFTLOCK) { \
3792 3792 atomic_add_long((ulong_t *)&(svd)->softlockcnt, \
3793 3793 -(pages)); \
3794 3794 }
3795 3795
3796 3796 #define SEGVN_UPDATE_MODBITS(ppa, pages, rw, prot, vpprot) \
3797 3797 if (IS_VMODSORT((ppa)[0]->p_vnode)) { \
3798 3798 if ((rw) == S_WRITE) { \
3799 3799 for (i = 0; i < (pages); i++) { \
3800 3800 ASSERT((ppa)[i]->p_vnode == \
3801 3801 (ppa)[0]->p_vnode); \
3802 3802 hat_setmod((ppa)[i]); \
3803 3803 } \
3804 3804 } else if ((rw) != S_OTHER && \
3805 3805 ((prot) & (vpprot) & PROT_WRITE)) { \
3806 3806 for (i = 0; i < (pages); i++) { \
3807 3807 ASSERT((ppa)[i]->p_vnode == \
3808 3808 (ppa)[0]->p_vnode); \
3809 3809 if (!hat_ismod((ppa)[i])) { \
3810 3810 prot &= ~PROT_WRITE; \
3811 3811 break; \
3812 3812 } \
3813 3813 } \
3814 3814 } \
3815 3815 }
3816 3816
3817 3817 #ifdef VM_STATS
3818 3818
3819 3819 #define SEGVN_VMSTAT_FLTVNPAGES(idx) \
3820 3820 VM_STAT_ADD(segvnvmstats.fltvnpages[(idx)]);
3821 3821
3822 3822 #else /* VM_STATS */
3823 3823
3824 3824 #define SEGVN_VMSTAT_FLTVNPAGES(idx)
3825 3825
3826 3826 #endif
3827 3827
3828 3828 static faultcode_t
3829 3829 segvn_fault_vnodepages(struct hat *hat, struct seg *seg, caddr_t lpgaddr,
3830 3830 caddr_t lpgeaddr, enum fault_type type, enum seg_rw rw, caddr_t addr,
3831 3831 caddr_t eaddr, int brkcow)
3832 3832 {
3833 3833 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
3834 3834 struct anon_map *amp = svd->amp;
3835 3835 uchar_t segtype = svd->type;
3836 3836 uint_t szc = seg->s_szc;
3837 3837 size_t pgsz = page_get_pagesize(szc);
3838 3838 size_t maxpgsz = pgsz;
3839 3839 pgcnt_t pages = btop(pgsz);
3840 3840 pgcnt_t maxpages = pages;
3841 3841 size_t ppasize = (pages + 1) * sizeof (page_t *);
3842 3842 caddr_t a = lpgaddr;
3843 3843 caddr_t maxlpgeaddr = lpgeaddr;
3844 3844 u_offset_t off = svd->offset + (uintptr_t)(a - seg->s_base);
3845 3845 ulong_t aindx = svd->anon_index + seg_page(seg, a);
3846 3846 struct vpage *vpage = (svd->vpage != NULL) ?
3847 3847 &svd->vpage[seg_page(seg, a)] : NULL;
3848 3848 vnode_t *vp = svd->vp;
3849 3849 page_t **ppa;
3850 3850 uint_t pszc;
3851 3851 size_t ppgsz;
3852 3852 pgcnt_t ppages;
3853 3853 faultcode_t err = 0;
3854 3854 int ierr;
3855 3855 int vop_size_err = 0;
3856 3856 uint_t protchk, prot, vpprot;
3857 3857 ulong_t i;
3858 3858 int hat_flag = (type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD;
3859 3859 anon_sync_obj_t an_cookie;
3860 3860 enum seg_rw arw;
3861 3861 int alloc_failed = 0;
3862 3862 int adjszc_chk;
3863 3863 struct vattr va;
3864 3864 page_t *pplist;
3865 3865 pfn_t pfn;
3866 3866 int physcontig;
3867 3867 int upgrdfail;
3868 3868 int segvn_anypgsz_vnode = 0; /* for now map vnode with 2 page sizes */
3869 3869 int tron = (svd->tr_state == SEGVN_TR_ON);
3870 3870
3871 3871 ASSERT(szc != 0);
3872 3872 ASSERT(vp != NULL);
3873 3873 ASSERT(brkcow == 0 || amp != NULL);
3874 3874 ASSERT(tron == 0 || amp != NULL);
3875 3875 ASSERT(enable_mbit_wa == 0); /* no mbit simulations with large pages */
3876 3876 ASSERT(!(svd->flags & MAP_NORESERVE));
3877 3877 ASSERT(type != F_SOFTUNLOCK);
3878 3878 ASSERT(IS_P2ALIGNED(a, maxpgsz));
3879 3879 ASSERT(amp == NULL || IS_P2ALIGNED(aindx, maxpages));
3880 3880 ASSERT(SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
3881 3881 ASSERT(seg->s_szc < NBBY * sizeof (int));
3882 3882 ASSERT(type != F_SOFTLOCK || lpgeaddr - a == maxpgsz);
3883 3883 ASSERT(svd->tr_state != SEGVN_TR_INIT);
3884 3884
3885 3885 VM_STAT_COND_ADD(type == F_SOFTLOCK, segvnvmstats.fltvnpages[0]);
3886 3886 VM_STAT_COND_ADD(type != F_SOFTLOCK, segvnvmstats.fltvnpages[1]);
3887 3887
3888 3888 if (svd->flags & MAP_TEXT) {
3889 3889 hat_flag |= HAT_LOAD_TEXT;
3890 3890 }
3891 3891
3892 3892 if (svd->pageprot) {
3893 3893 switch (rw) {
3894 3894 case S_READ:
3895 3895 protchk = PROT_READ;
3896 3896 break;
3897 3897 case S_WRITE:
3898 3898 protchk = PROT_WRITE;
3899 3899 break;
3900 3900 case S_EXEC:
3901 3901 protchk = PROT_EXEC;
3902 3902 break;
3903 3903 case S_OTHER:
3904 3904 default:
3905 3905 protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
3906 3906 break;
3907 3907 }
3908 3908 } else {
3909 3909 prot = svd->prot;
3910 3910 /* caller has already done segment level protection check. */
3911 3911 }
3912 3912
3913 3913 if (rw == S_WRITE && segtype == MAP_PRIVATE) {
3914 3914 SEGVN_VMSTAT_FLTVNPAGES(2);
3915 3915 arw = S_READ;
3916 3916 } else {
3917 3917 arw = rw;
3918 3918 }
3919 3919
3920 3920 ppa = kmem_alloc(ppasize, KM_SLEEP);
3921 3921
3922 3922 VM_STAT_COND_ADD(amp != NULL, segvnvmstats.fltvnpages[3]);
3923 3923
3924 3924 for (;;) {
3925 3925 adjszc_chk = 0;
3926 3926 for (; a < lpgeaddr; a += pgsz, off += pgsz, aindx += pages) {
3927 3927 if (adjszc_chk) {
3928 3928 while (szc < seg->s_szc) {
3929 3929 uintptr_t e;
3930 3930 uint_t tszc;
3931 3931 tszc = segvn_anypgsz_vnode ? szc + 1 :
3932 3932 seg->s_szc;
3933 3933 ppgsz = page_get_pagesize(tszc);
3934 3934 if (!IS_P2ALIGNED(a, ppgsz) ||
3935 3935 ((alloc_failed >> tszc) & 0x1)) {
3936 3936 break;
3937 3937 }
3938 3938 SEGVN_VMSTAT_FLTVNPAGES(4);
3939 3939 szc = tszc;
3940 3940 pgsz = ppgsz;
3941 3941 pages = btop(pgsz);
3942 3942 e = P2ROUNDUP((uintptr_t)eaddr, pgsz);
3943 3943 lpgeaddr = (caddr_t)e;
3944 3944 }
3945 3945 }
3946 3946
3947 3947 again:
3948 3948 if (IS_P2ALIGNED(a, maxpgsz) && amp != NULL) {
3949 3949 ASSERT(IS_P2ALIGNED(aindx, maxpages));
3950 3950 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
3951 3951 anon_array_enter(amp, aindx, &an_cookie);
3952 3952 if (anon_get_ptr(amp->ahp, aindx) != NULL) {
3953 3953 SEGVN_VMSTAT_FLTVNPAGES(5);
3954 3954 ASSERT(anon_pages(amp->ahp, aindx,
3955 3955 maxpages) == maxpages);
3956 3956 anon_array_exit(&an_cookie);
3957 3957 ANON_LOCK_EXIT(&->a_rwlock);
3958 3958 err = segvn_fault_anonpages(hat, seg,
3959 3959 a, a + maxpgsz, type, rw,
3960 3960 MAX(a, addr),
3961 3961 MIN(a + maxpgsz, eaddr), brkcow);
3962 3962 if (err != 0) {
3963 3963 SEGVN_VMSTAT_FLTVNPAGES(6);
3964 3964 goto out;
3965 3965 }
3966 3966 if (szc < seg->s_szc) {
3967 3967 szc = seg->s_szc;
3968 3968 pgsz = maxpgsz;
3969 3969 pages = maxpages;
3970 3970 lpgeaddr = maxlpgeaddr;
3971 3971 }
3972 3972 goto next;
3973 3973 } else {
3974 3974 ASSERT(anon_pages(amp->ahp, aindx,
3975 3975 maxpages) == 0);
3976 3976 SEGVN_VMSTAT_FLTVNPAGES(7);
3977 3977 anon_array_exit(&an_cookie);
3978 3978 ANON_LOCK_EXIT(&->a_rwlock);
3979 3979 }
3980 3980 }
3981 3981 ASSERT(!brkcow || IS_P2ALIGNED(a, maxpgsz));
3982 3982 ASSERT(!tron || IS_P2ALIGNED(a, maxpgsz));
3983 3983
3984 3984 if (svd->pageprot != 0 && IS_P2ALIGNED(a, maxpgsz)) {
3985 3985 ASSERT(vpage != NULL);
3986 3986 prot = VPP_PROT(vpage);
3987 3987 ASSERT(sameprot(seg, a, maxpgsz));
3988 3988 if ((prot & protchk) == 0) {
3989 3989 SEGVN_VMSTAT_FLTVNPAGES(8);
3990 3990 err = FC_PROT;
3991 3991 goto out;
3992 3992 }
3993 3993 }
3994 3994 if (type == F_SOFTLOCK) {
3995 3995 atomic_add_long((ulong_t *)&svd->softlockcnt,
3996 3996 pages);
3997 3997 }
3998 3998
3999 3999 pplist = NULL;
4000 4000 physcontig = 0;
4001 4001 ppa[0] = NULL;
4002 4002 if (!brkcow && !tron && szc &&
4003 4003 !page_exists_physcontig(vp, off, szc,
4004 4004 segtype == MAP_PRIVATE ? ppa : NULL)) {
4005 4005 SEGVN_VMSTAT_FLTVNPAGES(9);
4006 4006 if (page_alloc_pages(vp, seg, a, &pplist, NULL,
4007 4007 szc, 0, 0) && type != F_SOFTLOCK) {
4008 4008 SEGVN_VMSTAT_FLTVNPAGES(10);
4009 4009 pszc = 0;
4010 4010 ierr = -1;
4011 4011 alloc_failed |= (1 << szc);
4012 4012 break;
4013 4013 }
4014 4014 if (pplist != NULL &&
4015 4015 vp->v_mpssdata == SEGVN_PAGEIO) {
4016 4016 int downsize;
4017 4017 SEGVN_VMSTAT_FLTVNPAGES(11);
4018 4018 physcontig = segvn_fill_vp_pages(svd,
4019 4019 vp, off, szc, ppa, &pplist,
4020 4020 &pszc, &downsize);
4021 4021 ASSERT(!physcontig || pplist == NULL);
4022 4022 if (!physcontig && downsize &&
4023 4023 type != F_SOFTLOCK) {
4024 4024 ASSERT(pplist == NULL);
4025 4025 SEGVN_VMSTAT_FLTVNPAGES(12);
4026 4026 ierr = -1;
4027 4027 break;
4028 4028 }
4029 4029 ASSERT(!physcontig ||
4030 4030 segtype == MAP_PRIVATE ||
4031 4031 ppa[0] == NULL);
4032 4032 if (physcontig && ppa[0] == NULL) {
4033 4033 physcontig = 0;
4034 4034 }
4035 4035 }
4036 4036 } else if (!brkcow && !tron && szc && ppa[0] != NULL) {
4037 4037 SEGVN_VMSTAT_FLTVNPAGES(13);
4038 4038 ASSERT(segtype == MAP_PRIVATE);
4039 4039 physcontig = 1;
4040 4040 }
4041 4041
4042 4042 if (!physcontig) {
4043 4043 SEGVN_VMSTAT_FLTVNPAGES(14);
4044 4044 ppa[0] = NULL;
4045 4045 ierr = VOP_GETPAGE(vp, (offset_t)off, pgsz,
4046 4046 &vpprot, ppa, pgsz, seg, a, arw,
4047 4047 svd->cred, NULL);
4048 4048 #ifdef DEBUG
4049 4049 if (ierr == 0) {
4050 4050 for (i = 0; i < pages; i++) {
4051 4051 ASSERT(PAGE_LOCKED(ppa[i]));
4052 4052 ASSERT(!PP_ISFREE(ppa[i]));
4053 4053 ASSERT(ppa[i]->p_vnode == vp);
4054 4054 ASSERT(ppa[i]->p_offset ==
4055 4055 off + (i << PAGESHIFT));
4056 4056 }
4057 4057 }
4058 4058 #endif /* DEBUG */
4059 4059 if (segtype == MAP_PRIVATE) {
4060 4060 SEGVN_VMSTAT_FLTVNPAGES(15);
4061 4061 vpprot &= ~PROT_WRITE;
4062 4062 }
4063 4063 } else {
4064 4064 ASSERT(segtype == MAP_PRIVATE);
4065 4065 SEGVN_VMSTAT_FLTVNPAGES(16);
4066 4066 vpprot = PROT_ALL & ~PROT_WRITE;
4067 4067 ierr = 0;
4068 4068 }
4069 4069
4070 4070 if (ierr != 0) {
4071 4071 SEGVN_VMSTAT_FLTVNPAGES(17);
4072 4072 if (pplist != NULL) {
4073 4073 SEGVN_VMSTAT_FLTVNPAGES(18);
4074 4074 page_free_replacement_page(pplist);
4075 4075 page_create_putback(pages);
4076 4076 }
4077 4077 SEGVN_RESTORE_SOFTLOCK_VP(type, pages);
4078 4078 if (a + pgsz <= eaddr) {
4079 4079 SEGVN_VMSTAT_FLTVNPAGES(19);
4080 4080 err = FC_MAKE_ERR(ierr);
4081 4081 goto out;
4082 4082 }
4083 4083 va.va_mask = AT_SIZE;
4084 4084 if (VOP_GETATTR(vp, &va, 0, svd->cred, NULL)) {
4085 4085 SEGVN_VMSTAT_FLTVNPAGES(20);
4086 4086 err = FC_MAKE_ERR(EIO);
4087 4087 goto out;
4088 4088 }
4089 4089 if (btopr(va.va_size) >= btopr(off + pgsz)) {
4090 4090 SEGVN_VMSTAT_FLTVNPAGES(21);
4091 4091 err = FC_MAKE_ERR(ierr);
4092 4092 goto out;
4093 4093 }
4094 4094 if (btopr(va.va_size) <
4095 4095 btopr(off + (eaddr - a))) {
4096 4096 SEGVN_VMSTAT_FLTVNPAGES(22);
4097 4097 err = FC_MAKE_ERR(ierr);
4098 4098 goto out;
4099 4099 }
4100 4100 if (brkcow || tron || type == F_SOFTLOCK) {
4101 4101 /* can't reduce map area */
4102 4102 SEGVN_VMSTAT_FLTVNPAGES(23);
4103 4103 vop_size_err = 1;
4104 4104 goto out;
4105 4105 }
4106 4106 SEGVN_VMSTAT_FLTVNPAGES(24);
4107 4107 ASSERT(szc != 0);
4108 4108 pszc = 0;
4109 4109 ierr = -1;
4110 4110 break;
4111 4111 }
4112 4112
4113 4113 if (amp != NULL) {
4114 4114 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
4115 4115 anon_array_enter(amp, aindx, &an_cookie);
4116 4116 }
4117 4117 if (amp != NULL &&
4118 4118 anon_get_ptr(amp->ahp, aindx) != NULL) {
4119 4119 ulong_t taindx = P2ALIGN(aindx, maxpages);
4120 4120
4121 4121 SEGVN_VMSTAT_FLTVNPAGES(25);
4122 4122 ASSERT(anon_pages(amp->ahp, taindx,
4123 4123 maxpages) == maxpages);
4124 4124 for (i = 0; i < pages; i++) {
4125 4125 page_unlock(ppa[i]);
4126 4126 }
4127 4127 anon_array_exit(&an_cookie);
4128 4128 ANON_LOCK_EXIT(&->a_rwlock);
4129 4129 if (pplist != NULL) {
4130 4130 page_free_replacement_page(pplist);
4131 4131 page_create_putback(pages);
4132 4132 }
4133 4133 SEGVN_RESTORE_SOFTLOCK_VP(type, pages);
4134 4134 if (szc < seg->s_szc) {
4135 4135 SEGVN_VMSTAT_FLTVNPAGES(26);
4136 4136 /*
4137 4137 * For private segments SOFTLOCK
4138 4138 * either always breaks cow (any rw
4139 4139 * type except S_READ_NOCOW) or
4140 4140 * address space is locked as writer
4141 4141 * (S_READ_NOCOW case) and anon slots
4142 4142 * can't show up on second check.
4143 4143 * Therefore if we are here for
4144 4144 * SOFTLOCK case it must be a cow
4145 4145 * break but cow break never reduces
4146 4146 * szc. text replication (tron) in
4147 4147 * this case works as cow break.
4148 4148 * Thus the assert below.
4149 4149 */
4150 4150 ASSERT(!brkcow && !tron &&
4151 4151 type != F_SOFTLOCK);
4152 4152 pszc = seg->s_szc;
4153 4153 ierr = -2;
4154 4154 break;
4155 4155 }
4156 4156 ASSERT(IS_P2ALIGNED(a, maxpgsz));
4157 4157 goto again;
4158 4158 }
4159 4159 #ifdef DEBUG
4160 4160 if (amp != NULL) {
4161 4161 ulong_t taindx = P2ALIGN(aindx, maxpages);
4162 4162 ASSERT(!anon_pages(amp->ahp, taindx, maxpages));
4163 4163 }
4164 4164 #endif /* DEBUG */
4165 4165
4166 4166 if (brkcow || tron) {
4167 4167 ASSERT(amp != NULL);
4168 4168 ASSERT(pplist == NULL);
4169 4169 ASSERT(szc == seg->s_szc);
4170 4170 ASSERT(IS_P2ALIGNED(a, maxpgsz));
4171 4171 ASSERT(IS_P2ALIGNED(aindx, maxpages));
4172 4172 SEGVN_VMSTAT_FLTVNPAGES(27);
4173 4173 ierr = anon_map_privatepages(amp, aindx, szc,
4174 4174 seg, a, prot, ppa, vpage, segvn_anypgsz,
4175 4175 tron ? PG_LOCAL : 0, svd->cred);
4176 4176 if (ierr != 0) {
4177 4177 SEGVN_VMSTAT_FLTVNPAGES(28);
4178 4178 anon_array_exit(&an_cookie);
4179 4179 ANON_LOCK_EXIT(&->a_rwlock);
4180 4180 SEGVN_RESTORE_SOFTLOCK_VP(type, pages);
4181 4181 err = FC_MAKE_ERR(ierr);
4182 4182 goto out;
4183 4183 }
4184 4184
4185 4185 ASSERT(!IS_VMODSORT(ppa[0]->p_vnode));
4186 4186 /*
4187 4187 * p_szc can't be changed for locked
4188 4188 * swapfs pages.
4189 4189 */
4190 4190 ASSERT(svd->rcookie ==
4191 4191 HAT_INVALID_REGION_COOKIE);
4192 4192 hat_memload_array(hat, a, pgsz, ppa, prot,
4193 4193 hat_flag);
4194 4194
4195 4195 if (!(hat_flag & HAT_LOAD_LOCK)) {
4196 4196 SEGVN_VMSTAT_FLTVNPAGES(29);
4197 4197 for (i = 0; i < pages; i++) {
4198 4198 page_unlock(ppa[i]);
4199 4199 }
4200 4200 }
4201 4201 anon_array_exit(&an_cookie);
4202 4202 ANON_LOCK_EXIT(&->a_rwlock);
4203 4203 goto next;
4204 4204 }
4205 4205
4206 4206 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE ||
4207 4207 (!svd->pageprot && svd->prot == (prot & vpprot)));
4208 4208
4209 4209 pfn = page_pptonum(ppa[0]);
4210 4210 /*
4211 4211 * hat_page_demote() needs an SE_EXCL lock on one of
4212 4212 * constituent page_t's and it decreases root's p_szc
4213 4213 * last. This means if root's p_szc is equal szc and
4214 4214 * all its constituent pages are locked
4215 4215 * hat_page_demote() that could have changed p_szc to
4216 4216 * szc is already done and no new have page_demote()
4217 4217 * can start for this large page.
4218 4218 */
4219 4219
4220 4220 /*
4221 4221 * we need to make sure same mapping size is used for
4222 4222 * the same address range if there's a possibility the
4223 4223 * adddress is already mapped because hat layer panics
4224 4224 * when translation is loaded for the range already
4225 4225 * mapped with a different page size. We achieve it
4226 4226 * by always using largest page size possible subject
4227 4227 * to the constraints of page size, segment page size
4228 4228 * and page alignment. Since mappings are invalidated
4229 4229 * when those constraints change and make it
4230 4230 * impossible to use previously used mapping size no
4231 4231 * mapping size conflicts should happen.
4232 4232 */
4233 4233
4234 4234 chkszc:
4235 4235 if ((pszc = ppa[0]->p_szc) == szc &&
4236 4236 IS_P2ALIGNED(pfn, pages)) {
4237 4237
4238 4238 SEGVN_VMSTAT_FLTVNPAGES(30);
4239 4239 #ifdef DEBUG
4240 4240 for (i = 0; i < pages; i++) {
4241 4241 ASSERT(PAGE_LOCKED(ppa[i]));
4242 4242 ASSERT(!PP_ISFREE(ppa[i]));
4243 4243 ASSERT(page_pptonum(ppa[i]) ==
4244 4244 pfn + i);
4245 4245 ASSERT(ppa[i]->p_szc == szc);
4246 4246 ASSERT(ppa[i]->p_vnode == vp);
4247 4247 ASSERT(ppa[i]->p_offset ==
4248 4248 off + (i << PAGESHIFT));
4249 4249 }
4250 4250 #endif /* DEBUG */
4251 4251 /*
4252 4252 * All pages are of szc we need and they are
4253 4253 * all locked so they can't change szc. load
4254 4254 * translations.
4255 4255 *
4256 4256 * if page got promoted since last check
4257 4257 * we don't need pplist.
4258 4258 */
4259 4259 if (pplist != NULL) {
4260 4260 page_free_replacement_page(pplist);
4261 4261 page_create_putback(pages);
4262 4262 }
4263 4263 if (PP_ISMIGRATE(ppa[0])) {
4264 4264 page_migrate(seg, a, ppa, pages);
4265 4265 }
4266 4266 SEGVN_UPDATE_MODBITS(ppa, pages, rw,
4267 4267 prot, vpprot);
4268 4268 hat_memload_array_region(hat, a, pgsz,
4269 4269 ppa, prot & vpprot, hat_flag,
4270 4270 svd->rcookie);
4271 4271
4272 4272 if (!(hat_flag & HAT_LOAD_LOCK)) {
4273 4273 for (i = 0; i < pages; i++) {
4274 4274 page_unlock(ppa[i]);
4275 4275 }
4276 4276 }
4277 4277 if (amp != NULL) {
4278 4278 anon_array_exit(&an_cookie);
4279 4279 ANON_LOCK_EXIT(&->a_rwlock);
4280 4280 }
4281 4281 goto next;
4282 4282 }
4283 4283
4284 4284 /*
4285 4285 * See if upsize is possible.
4286 4286 */
4287 4287 if (pszc > szc && szc < seg->s_szc &&
4288 4288 (segvn_anypgsz_vnode || pszc >= seg->s_szc)) {
4289 4289 pgcnt_t aphase;
4290 4290 uint_t pszc1 = MIN(pszc, seg->s_szc);
4291 4291 ppgsz = page_get_pagesize(pszc1);
4292 4292 ppages = btop(ppgsz);
4293 4293 aphase = btop(P2PHASE((uintptr_t)a, ppgsz));
4294 4294
4295 4295 ASSERT(type != F_SOFTLOCK);
4296 4296
4297 4297 SEGVN_VMSTAT_FLTVNPAGES(31);
4298 4298 if (aphase != P2PHASE(pfn, ppages)) {
4299 4299 segvn_faultvnmpss_align_err4++;
4300 4300 } else {
4301 4301 SEGVN_VMSTAT_FLTVNPAGES(32);
4302 4302 if (pplist != NULL) {
4303 4303 page_t *pl = pplist;
4304 4304 page_free_replacement_page(pl);
4305 4305 page_create_putback(pages);
4306 4306 }
4307 4307 for (i = 0; i < pages; i++) {
4308 4308 page_unlock(ppa[i]);
4309 4309 }
4310 4310 if (amp != NULL) {
4311 4311 anon_array_exit(&an_cookie);
4312 4312 ANON_LOCK_EXIT(&->a_rwlock);
4313 4313 }
4314 4314 pszc = pszc1;
4315 4315 ierr = -2;
4316 4316 break;
4317 4317 }
4318 4318 }
4319 4319
4320 4320 /*
4321 4321 * check if we should use smallest mapping size.
4322 4322 */
4323 4323 upgrdfail = 0;
4324 4324 if (szc == 0 ||
4325 4325 (pszc >= szc &&
4326 4326 !IS_P2ALIGNED(pfn, pages)) ||
4327 4327 (pszc < szc &&
4328 4328 !segvn_full_szcpages(ppa, szc, &upgrdfail,
4329 4329 &pszc))) {
4330 4330
4331 4331 if (upgrdfail && type != F_SOFTLOCK) {
4332 4332 /*
4333 4333 * segvn_full_szcpages failed to lock
4334 4334 * all pages EXCL. Size down.
4335 4335 */
4336 4336 ASSERT(pszc < szc);
4337 4337
4338 4338 SEGVN_VMSTAT_FLTVNPAGES(33);
4339 4339
4340 4340 if (pplist != NULL) {
4341 4341 page_t *pl = pplist;
4342 4342 page_free_replacement_page(pl);
4343 4343 page_create_putback(pages);
4344 4344 }
4345 4345
4346 4346 for (i = 0; i < pages; i++) {
4347 4347 page_unlock(ppa[i]);
4348 4348 }
4349 4349 if (amp != NULL) {
4350 4350 anon_array_exit(&an_cookie);
4351 4351 ANON_LOCK_EXIT(&->a_rwlock);
4352 4352 }
4353 4353 ierr = -1;
4354 4354 break;
4355 4355 }
4356 4356 if (szc != 0 && !upgrdfail) {
4357 4357 segvn_faultvnmpss_align_err5++;
4358 4358 }
4359 4359 SEGVN_VMSTAT_FLTVNPAGES(34);
4360 4360 if (pplist != NULL) {
4361 4361 page_free_replacement_page(pplist);
4362 4362 page_create_putback(pages);
4363 4363 }
4364 4364 SEGVN_UPDATE_MODBITS(ppa, pages, rw,
4365 4365 prot, vpprot);
4366 4366 if (upgrdfail && segvn_anypgsz_vnode) {
4367 4367 /* SOFTLOCK case */
4368 4368 hat_memload_array_region(hat, a, pgsz,
4369 4369 ppa, prot & vpprot, hat_flag,
4370 4370 svd->rcookie);
4371 4371 } else {
4372 4372 for (i = 0; i < pages; i++) {
4373 4373 hat_memload_region(hat,
4374 4374 a + (i << PAGESHIFT),
4375 4375 ppa[i], prot & vpprot,
4376 4376 hat_flag, svd->rcookie);
4377 4377 }
4378 4378 }
4379 4379 if (!(hat_flag & HAT_LOAD_LOCK)) {
4380 4380 for (i = 0; i < pages; i++) {
4381 4381 page_unlock(ppa[i]);
4382 4382 }
4383 4383 }
4384 4384 if (amp != NULL) {
4385 4385 anon_array_exit(&an_cookie);
4386 4386 ANON_LOCK_EXIT(&->a_rwlock);
4387 4387 }
4388 4388 goto next;
4389 4389 }
4390 4390
4391 4391 if (pszc == szc) {
4392 4392 /*
4393 4393 * segvn_full_szcpages() upgraded pages szc.
4394 4394 */
4395 4395 ASSERT(pszc == ppa[0]->p_szc);
4396 4396 ASSERT(IS_P2ALIGNED(pfn, pages));
4397 4397 goto chkszc;
4398 4398 }
4399 4399
4400 4400 if (pszc > szc) {
4401 4401 kmutex_t *szcmtx;
4402 4402 SEGVN_VMSTAT_FLTVNPAGES(35);
4403 4403 /*
4404 4404 * p_szc of ppa[0] can change since we haven't
4405 4405 * locked all constituent pages. Call
4406 4406 * page_lock_szc() to prevent szc changes.
4407 4407 * This should be a rare case that happens when
4408 4408 * multiple segments use a different page size
4409 4409 * to map the same file offsets.
4410 4410 */
4411 4411 szcmtx = page_szc_lock(ppa[0]);
4412 4412 pszc = ppa[0]->p_szc;
4413 4413 ASSERT(szcmtx != NULL || pszc == 0);
4414 4414 ASSERT(ppa[0]->p_szc <= pszc);
4415 4415 if (pszc <= szc) {
4416 4416 SEGVN_VMSTAT_FLTVNPAGES(36);
4417 4417 if (szcmtx != NULL) {
4418 4418 mutex_exit(szcmtx);
4419 4419 }
4420 4420 goto chkszc;
4421 4421 }
4422 4422 if (pplist != NULL) {
4423 4423 /*
4424 4424 * page got promoted since last check.
4425 4425 * we don't need preaalocated large
4426 4426 * page.
4427 4427 */
4428 4428 SEGVN_VMSTAT_FLTVNPAGES(37);
4429 4429 page_free_replacement_page(pplist);
4430 4430 page_create_putback(pages);
4431 4431 }
4432 4432 SEGVN_UPDATE_MODBITS(ppa, pages, rw,
4433 4433 prot, vpprot);
4434 4434 hat_memload_array_region(hat, a, pgsz, ppa,
4435 4435 prot & vpprot, hat_flag, svd->rcookie);
4436 4436 mutex_exit(szcmtx);
4437 4437 if (!(hat_flag & HAT_LOAD_LOCK)) {
4438 4438 for (i = 0; i < pages; i++) {
4439 4439 page_unlock(ppa[i]);
4440 4440 }
4441 4441 }
4442 4442 if (amp != NULL) {
4443 4443 anon_array_exit(&an_cookie);
4444 4444 ANON_LOCK_EXIT(&->a_rwlock);
4445 4445 }
4446 4446 goto next;
4447 4447 }
4448 4448
4449 4449 /*
4450 4450 * if page got demoted since last check
4451 4451 * we could have not allocated larger page.
4452 4452 * allocate now.
4453 4453 */
4454 4454 if (pplist == NULL &&
4455 4455 page_alloc_pages(vp, seg, a, &pplist, NULL,
4456 4456 szc, 0, 0) && type != F_SOFTLOCK) {
4457 4457 SEGVN_VMSTAT_FLTVNPAGES(38);
4458 4458 for (i = 0; i < pages; i++) {
4459 4459 page_unlock(ppa[i]);
4460 4460 }
4461 4461 if (amp != NULL) {
4462 4462 anon_array_exit(&an_cookie);
4463 4463 ANON_LOCK_EXIT(&->a_rwlock);
4464 4464 }
4465 4465 ierr = -1;
4466 4466 alloc_failed |= (1 << szc);
4467 4467 break;
4468 4468 }
4469 4469
4470 4470 SEGVN_VMSTAT_FLTVNPAGES(39);
4471 4471
4472 4472 if (pplist != NULL) {
4473 4473 segvn_relocate_pages(ppa, pplist);
4474 4474 #ifdef DEBUG
4475 4475 } else {
4476 4476 ASSERT(type == F_SOFTLOCK);
4477 4477 SEGVN_VMSTAT_FLTVNPAGES(40);
4478 4478 #endif /* DEBUG */
4479 4479 }
4480 4480
4481 4481 SEGVN_UPDATE_MODBITS(ppa, pages, rw, prot, vpprot);
4482 4482
4483 4483 if (pplist == NULL && segvn_anypgsz_vnode == 0) {
4484 4484 ASSERT(type == F_SOFTLOCK);
4485 4485 for (i = 0; i < pages; i++) {
4486 4486 ASSERT(ppa[i]->p_szc < szc);
4487 4487 hat_memload_region(hat,
4488 4488 a + (i << PAGESHIFT),
4489 4489 ppa[i], prot & vpprot, hat_flag,
4490 4490 svd->rcookie);
4491 4491 }
4492 4492 } else {
4493 4493 ASSERT(pplist != NULL || type == F_SOFTLOCK);
4494 4494 hat_memload_array_region(hat, a, pgsz, ppa,
4495 4495 prot & vpprot, hat_flag, svd->rcookie);
4496 4496 }
4497 4497 if (!(hat_flag & HAT_LOAD_LOCK)) {
4498 4498 for (i = 0; i < pages; i++) {
4499 4499 ASSERT(PAGE_SHARED(ppa[i]));
4500 4500 page_unlock(ppa[i]);
4501 4501 }
4502 4502 }
4503 4503 if (amp != NULL) {
4504 4504 anon_array_exit(&an_cookie);
4505 4505 ANON_LOCK_EXIT(&->a_rwlock);
4506 4506 }
4507 4507
4508 4508 next:
4509 4509 if (vpage != NULL) {
4510 4510 vpage += pages;
4511 4511 }
4512 4512 adjszc_chk = 1;
4513 4513 }
4514 4514 if (a == lpgeaddr)
4515 4515 break;
4516 4516 ASSERT(a < lpgeaddr);
4517 4517
4518 4518 ASSERT(!brkcow && !tron && type != F_SOFTLOCK);
4519 4519
4520 4520 /*
4521 4521 * ierr == -1 means we failed to map with a large page.
4522 4522 * (either due to allocation/relocation failures or
4523 4523 * misalignment with other mappings to this file.
4524 4524 *
4525 4525 * ierr == -2 means some other thread allocated a large page
4526 4526 * after we gave up tp map with a large page. retry with
4527 4527 * larger mapping.
4528 4528 */
4529 4529 ASSERT(ierr == -1 || ierr == -2);
4530 4530 ASSERT(ierr == -2 || szc != 0);
4531 4531 ASSERT(ierr == -1 || szc < seg->s_szc);
4532 4532 if (ierr == -2) {
4533 4533 SEGVN_VMSTAT_FLTVNPAGES(41);
4534 4534 ASSERT(pszc > szc && pszc <= seg->s_szc);
4535 4535 szc = pszc;
4536 4536 } else if (segvn_anypgsz_vnode) {
4537 4537 SEGVN_VMSTAT_FLTVNPAGES(42);
4538 4538 szc--;
4539 4539 } else {
4540 4540 SEGVN_VMSTAT_FLTVNPAGES(43);
4541 4541 ASSERT(pszc < szc);
4542 4542 /*
4543 4543 * other process created pszc large page.
4544 4544 * but we still have to drop to 0 szc.
4545 4545 */
4546 4546 szc = 0;
4547 4547 }
4548 4548
4549 4549 pgsz = page_get_pagesize(szc);
4550 4550 pages = btop(pgsz);
4551 4551 if (ierr == -2) {
4552 4552 /*
4553 4553 * Size up case. Note lpgaddr may only be needed for
4554 4554 * softlock case so we don't adjust it here.
4555 4555 */
4556 4556 a = (caddr_t)P2ALIGN((uintptr_t)a, pgsz);
4557 4557 ASSERT(a >= lpgaddr);
4558 4558 lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr, pgsz);
4559 4559 off = svd->offset + (uintptr_t)(a - seg->s_base);
4560 4560 aindx = svd->anon_index + seg_page(seg, a);
4561 4561 vpage = (svd->vpage != NULL) ?
4562 4562 &svd->vpage[seg_page(seg, a)] : NULL;
4563 4563 } else {
4564 4564 /*
4565 4565 * Size down case. Note lpgaddr may only be needed for
4566 4566 * softlock case so we don't adjust it here.
4567 4567 */
4568 4568 ASSERT(IS_P2ALIGNED(a, pgsz));
4569 4569 ASSERT(IS_P2ALIGNED(lpgeaddr, pgsz));
4570 4570 lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr, pgsz);
4571 4571 ASSERT(a < lpgeaddr);
4572 4572 if (a < addr) {
4573 4573 SEGVN_VMSTAT_FLTVNPAGES(44);
4574 4574 /*
4575 4575 * The beginning of the large page region can
4576 4576 * be pulled to the right to make a smaller
4577 4577 * region. We haven't yet faulted a single
4578 4578 * page.
4579 4579 */
4580 4580 a = (caddr_t)P2ALIGN((uintptr_t)addr, pgsz);
4581 4581 ASSERT(a >= lpgaddr);
4582 4582 off = svd->offset +
4583 4583 (uintptr_t)(a - seg->s_base);
4584 4584 aindx = svd->anon_index + seg_page(seg, a);
4585 4585 vpage = (svd->vpage != NULL) ?
4586 4586 &svd->vpage[seg_page(seg, a)] : NULL;
4587 4587 }
4588 4588 }
4589 4589 }
4590 4590 out:
4591 4591 kmem_free(ppa, ppasize);
4592 4592 if (!err && !vop_size_err) {
4593 4593 SEGVN_VMSTAT_FLTVNPAGES(45);
4594 4594 return (0);
4595 4595 }
4596 4596 if (type == F_SOFTLOCK && a > lpgaddr) {
4597 4597 SEGVN_VMSTAT_FLTVNPAGES(46);
4598 4598 segvn_softunlock(seg, lpgaddr, a - lpgaddr, S_OTHER);
4599 4599 }
4600 4600 if (!vop_size_err) {
4601 4601 SEGVN_VMSTAT_FLTVNPAGES(47);
4602 4602 return (err);
4603 4603 }
4604 4604 ASSERT(brkcow || tron || type == F_SOFTLOCK);
4605 4605 /*
4606 4606 * Large page end is mapped beyond the end of file and it's a cow
4607 4607 * fault (can be a text replication induced cow) or softlock so we can't
4608 4608 * reduce the map area. For now just demote the segment. This should
4609 4609 * really only happen if the end of the file changed after the mapping
4610 4610 * was established since when large page segments are created we make
4611 4611 * sure they don't extend beyond the end of the file.
4612 4612 */
4613 4613 SEGVN_VMSTAT_FLTVNPAGES(48);
4614 4614
4615 4615 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
4616 4616 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
4617 4617 err = 0;
4618 4618 if (seg->s_szc != 0) {
4619 4619 segvn_fltvnpages_clrszc_cnt++;
4620 4620 ASSERT(svd->softlockcnt == 0);
4621 4621 err = segvn_clrszc(seg);
4622 4622 if (err != 0) {
4623 4623 segvn_fltvnpages_clrszc_err++;
4624 4624 }
4625 4625 }
4626 4626 ASSERT(err || seg->s_szc == 0);
4627 4627 SEGVN_LOCK_DOWNGRADE(seg->s_as, &svd->lock);
4628 4628 /* segvn_fault will do its job as if szc had been zero to begin with */
4629 4629 return (err == 0 ? IE_RETRY : FC_MAKE_ERR(err));
4630 4630 }
4631 4631
4632 4632 /*
4633 4633 * This routine will attempt to fault in one large page.
4634 4634 * it will use smaller pages if that fails.
4635 4635 * It should only be called for pure anonymous segments.
4636 4636 */
4637 4637 static faultcode_t
4638 4638 segvn_fault_anonpages(struct hat *hat, struct seg *seg, caddr_t lpgaddr,
4639 4639 caddr_t lpgeaddr, enum fault_type type, enum seg_rw rw, caddr_t addr,
4640 4640 caddr_t eaddr, int brkcow)
4641 4641 {
4642 4642 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
4643 4643 struct anon_map *amp = svd->amp;
4644 4644 uchar_t segtype = svd->type;
4645 4645 uint_t szc = seg->s_szc;
4646 4646 size_t pgsz = page_get_pagesize(szc);
4647 4647 size_t maxpgsz = pgsz;
4648 4648 pgcnt_t pages = btop(pgsz);
4649 4649 uint_t ppaszc = szc;
4650 4650 caddr_t a = lpgaddr;
4651 4651 ulong_t aindx = svd->anon_index + seg_page(seg, a);
4652 4652 struct vpage *vpage = (svd->vpage != NULL) ?
4653 4653 &svd->vpage[seg_page(seg, a)] : NULL;
4654 4654 page_t **ppa;
4655 4655 uint_t ppa_szc;
4656 4656 faultcode_t err;
4657 4657 int ierr;
4658 4658 uint_t protchk, prot, vpprot;
4659 4659 ulong_t i;
4660 4660 int hat_flag = (type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD;
4661 4661 anon_sync_obj_t cookie;
4662 4662 int adjszc_chk;
4663 4663 int pgflags = (svd->tr_state == SEGVN_TR_ON) ? PG_LOCAL : 0;
4664 4664
4665 4665 ASSERT(szc != 0);
4666 4666 ASSERT(amp != NULL);
4667 4667 ASSERT(enable_mbit_wa == 0); /* no mbit simulations with large pages */
4668 4668 ASSERT(!(svd->flags & MAP_NORESERVE));
4669 4669 ASSERT(type != F_SOFTUNLOCK);
4670 4670 ASSERT(IS_P2ALIGNED(a, maxpgsz));
4671 4671 ASSERT(!brkcow || svd->tr_state == SEGVN_TR_OFF);
4672 4672 ASSERT(svd->tr_state != SEGVN_TR_INIT);
4673 4673
4674 4674 ASSERT(SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
4675 4675
4676 4676 VM_STAT_COND_ADD(type == F_SOFTLOCK, segvnvmstats.fltanpages[0]);
4677 4677 VM_STAT_COND_ADD(type != F_SOFTLOCK, segvnvmstats.fltanpages[1]);
4678 4678
4679 4679 if (svd->flags & MAP_TEXT) {
4680 4680 hat_flag |= HAT_LOAD_TEXT;
4681 4681 }
4682 4682
4683 4683 if (svd->pageprot) {
4684 4684 switch (rw) {
4685 4685 case S_READ:
4686 4686 protchk = PROT_READ;
4687 4687 break;
4688 4688 case S_WRITE:
4689 4689 protchk = PROT_WRITE;
4690 4690 break;
4691 4691 case S_EXEC:
4692 4692 protchk = PROT_EXEC;
4693 4693 break;
4694 4694 case S_OTHER:
4695 4695 default:
4696 4696 protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
4697 4697 break;
4698 4698 }
4699 4699 VM_STAT_ADD(segvnvmstats.fltanpages[2]);
4700 4700 } else {
4701 4701 prot = svd->prot;
4702 4702 /* caller has already done segment level protection check. */
4703 4703 }
4704 4704
4705 4705 ppa = kmem_cache_alloc(segvn_szc_cache[ppaszc], KM_SLEEP);
4706 4706 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
4707 4707 for (;;) {
4708 4708 adjszc_chk = 0;
4709 4709 for (; a < lpgeaddr; a += pgsz, aindx += pages) {
4710 4710 if (svd->pageprot != 0 && IS_P2ALIGNED(a, maxpgsz)) {
4711 4711 VM_STAT_ADD(segvnvmstats.fltanpages[3]);
4712 4712 ASSERT(vpage != NULL);
4713 4713 prot = VPP_PROT(vpage);
4714 4714 ASSERT(sameprot(seg, a, maxpgsz));
4715 4715 if ((prot & protchk) == 0) {
4716 4716 err = FC_PROT;
4717 4717 goto error;
4718 4718 }
4719 4719 }
4720 4720 if (adjszc_chk && IS_P2ALIGNED(a, maxpgsz) &&
4721 4721 pgsz < maxpgsz) {
4722 4722 ASSERT(a > lpgaddr);
4723 4723 szc = seg->s_szc;
4724 4724 pgsz = maxpgsz;
4725 4725 pages = btop(pgsz);
4726 4726 ASSERT(IS_P2ALIGNED(aindx, pages));
4727 4727 lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr,
4728 4728 pgsz);
4729 4729 }
4730 4730 if (type == F_SOFTLOCK) {
4731 4731 atomic_add_long((ulong_t *)&svd->softlockcnt,
4732 4732 pages);
4733 4733 }
4734 4734 anon_array_enter(amp, aindx, &cookie);
4735 4735 ppa_szc = (uint_t)-1;
4736 4736 ierr = anon_map_getpages(amp, aindx, szc, seg, a,
4737 4737 prot, &vpprot, ppa, &ppa_szc, vpage, rw, brkcow,
4738 4738 segvn_anypgsz, pgflags, svd->cred);
4739 4739 if (ierr != 0) {
4740 4740 anon_array_exit(&cookie);
4741 4741 VM_STAT_ADD(segvnvmstats.fltanpages[4]);
4742 4742 if (type == F_SOFTLOCK) {
4743 4743 atomic_add_long(
4744 4744 (ulong_t *)&svd->softlockcnt,
4745 4745 -pages);
4746 4746 }
4747 4747 if (ierr > 0) {
4748 4748 VM_STAT_ADD(segvnvmstats.fltanpages[6]);
4749 4749 err = FC_MAKE_ERR(ierr);
4750 4750 goto error;
4751 4751 }
4752 4752 break;
4753 4753 }
4754 4754
4755 4755 ASSERT(!IS_VMODSORT(ppa[0]->p_vnode));
4756 4756
4757 4757 ASSERT(segtype == MAP_SHARED ||
4758 4758 ppa[0]->p_szc <= szc);
4759 4759 ASSERT(segtype == MAP_PRIVATE ||
4760 4760 ppa[0]->p_szc >= szc);
4761 4761
4762 4762 /*
4763 4763 * Handle pages that have been marked for migration
4764 4764 */
4765 4765 if (lgrp_optimizations())
4766 4766 page_migrate(seg, a, ppa, pages);
4767 4767
4768 4768 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
4769 4769
4770 4770 if (segtype == MAP_SHARED) {
4771 4771 vpprot |= PROT_WRITE;
4772 4772 }
4773 4773
4774 4774 hat_memload_array(hat, a, pgsz, ppa,
4775 4775 prot & vpprot, hat_flag);
4776 4776
4777 4777 if (hat_flag & HAT_LOAD_LOCK) {
4778 4778 VM_STAT_ADD(segvnvmstats.fltanpages[7]);
4779 4779 } else {
4780 4780 VM_STAT_ADD(segvnvmstats.fltanpages[8]);
4781 4781 for (i = 0; i < pages; i++)
4782 4782 page_unlock(ppa[i]);
4783 4783 }
4784 4784 if (vpage != NULL)
4785 4785 vpage += pages;
4786 4786
4787 4787 anon_array_exit(&cookie);
4788 4788 adjszc_chk = 1;
4789 4789 }
4790 4790 if (a == lpgeaddr)
4791 4791 break;
4792 4792 ASSERT(a < lpgeaddr);
4793 4793 /*
4794 4794 * ierr == -1 means we failed to allocate a large page.
4795 4795 * so do a size down operation.
4796 4796 *
4797 4797 * ierr == -2 means some other process that privately shares
4798 4798 * pages with this process has allocated a larger page and we
4799 4799 * need to retry with larger pages. So do a size up
4800 4800 * operation. This relies on the fact that large pages are
4801 4801 * never partially shared i.e. if we share any constituent
4802 4802 * page of a large page with another process we must share the
4803 4803 * entire large page. Note this cannot happen for SOFTLOCK
4804 4804 * case, unless current address (a) is at the beginning of the
4805 4805 * next page size boundary because the other process couldn't
4806 4806 * have relocated locked pages.
4807 4807 */
4808 4808 ASSERT(ierr == -1 || ierr == -2);
4809 4809
4810 4810 if (segvn_anypgsz) {
4811 4811 ASSERT(ierr == -2 || szc != 0);
4812 4812 ASSERT(ierr == -1 || szc < seg->s_szc);
4813 4813 szc = (ierr == -1) ? szc - 1 : szc + 1;
4814 4814 } else {
4815 4815 /*
4816 4816 * For non COW faults and segvn_anypgsz == 0
4817 4817 * we need to be careful not to loop forever
4818 4818 * if existing page is found with szc other
4819 4819 * than 0 or seg->s_szc. This could be due
4820 4820 * to page relocations on behalf of DR or
4821 4821 * more likely large page creation. For this
4822 4822 * case simply re-size to existing page's szc
4823 4823 * if returned by anon_map_getpages().
4824 4824 */
4825 4825 if (ppa_szc == (uint_t)-1) {
4826 4826 szc = (ierr == -1) ? 0 : seg->s_szc;
4827 4827 } else {
4828 4828 ASSERT(ppa_szc <= seg->s_szc);
4829 4829 ASSERT(ierr == -2 || ppa_szc < szc);
4830 4830 ASSERT(ierr == -1 || ppa_szc > szc);
4831 4831 szc = ppa_szc;
4832 4832 }
4833 4833 }
4834 4834
4835 4835 pgsz = page_get_pagesize(szc);
4836 4836 pages = btop(pgsz);
4837 4837 ASSERT(type != F_SOFTLOCK || ierr == -1 ||
4838 4838 (IS_P2ALIGNED(a, pgsz) && IS_P2ALIGNED(lpgeaddr, pgsz)));
4839 4839 if (type == F_SOFTLOCK) {
4840 4840 /*
4841 4841 * For softlocks we cannot reduce the fault area
4842 4842 * (calculated based on the largest page size for this
4843 4843 * segment) for size down and a is already next
4844 4844 * page size aligned as assertted above for size
4845 4845 * ups. Therefore just continue in case of softlock.
4846 4846 */
4847 4847 VM_STAT_ADD(segvnvmstats.fltanpages[9]);
4848 4848 continue; /* keep lint happy */
4849 4849 } else if (ierr == -2) {
4850 4850
4851 4851 /*
4852 4852 * Size up case. Note lpgaddr may only be needed for
4853 4853 * softlock case so we don't adjust it here.
4854 4854 */
4855 4855 VM_STAT_ADD(segvnvmstats.fltanpages[10]);
4856 4856 a = (caddr_t)P2ALIGN((uintptr_t)a, pgsz);
4857 4857 ASSERT(a >= lpgaddr);
4858 4858 lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr, pgsz);
4859 4859 aindx = svd->anon_index + seg_page(seg, a);
4860 4860 vpage = (svd->vpage != NULL) ?
4861 4861 &svd->vpage[seg_page(seg, a)] : NULL;
4862 4862 } else {
4863 4863 /*
4864 4864 * Size down case. Note lpgaddr may only be needed for
4865 4865 * softlock case so we don't adjust it here.
4866 4866 */
4867 4867 VM_STAT_ADD(segvnvmstats.fltanpages[11]);
4868 4868 ASSERT(IS_P2ALIGNED(a, pgsz));
4869 4869 ASSERT(IS_P2ALIGNED(lpgeaddr, pgsz));
4870 4870 lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr, pgsz);
4871 4871 ASSERT(a < lpgeaddr);
4872 4872 if (a < addr) {
4873 4873 /*
4874 4874 * The beginning of the large page region can
4875 4875 * be pulled to the right to make a smaller
4876 4876 * region. We haven't yet faulted a single
4877 4877 * page.
4878 4878 */
4879 4879 VM_STAT_ADD(segvnvmstats.fltanpages[12]);
4880 4880 a = (caddr_t)P2ALIGN((uintptr_t)addr, pgsz);
4881 4881 ASSERT(a >= lpgaddr);
4882 4882 aindx = svd->anon_index + seg_page(seg, a);
4883 4883 vpage = (svd->vpage != NULL) ?
4884 4884 &svd->vpage[seg_page(seg, a)] : NULL;
4885 4885 }
4886 4886 }
4887 4887 }
4888 4888 VM_STAT_ADD(segvnvmstats.fltanpages[13]);
4889 4889 ANON_LOCK_EXIT(&->a_rwlock);
4890 4890 kmem_cache_free(segvn_szc_cache[ppaszc], ppa);
4891 4891 return (0);
4892 4892 error:
4893 4893 VM_STAT_ADD(segvnvmstats.fltanpages[14]);
4894 4894 ANON_LOCK_EXIT(&->a_rwlock);
4895 4895 kmem_cache_free(segvn_szc_cache[ppaszc], ppa);
4896 4896 if (type == F_SOFTLOCK && a > lpgaddr) {
4897 4897 VM_STAT_ADD(segvnvmstats.fltanpages[15]);
4898 4898 segvn_softunlock(seg, lpgaddr, a - lpgaddr, S_OTHER);
4899 4899 }
4900 4900 return (err);
4901 4901 }
4902 4902
4903 4903 int fltadvice = 1; /* set to free behind pages for sequential access */
4904 4904
4905 4905 /*
4906 4906 * This routine is called via a machine specific fault handling routine.
4907 4907 * It is also called by software routines wishing to lock or unlock
4908 4908 * a range of addresses.
4909 4909 *
4910 4910 * Here is the basic algorithm:
4911 4911 * If unlocking
4912 4912 * Call segvn_softunlock
4913 4913 * Return
4914 4914 * endif
4915 4915 * Checking and set up work
4916 4916 * If we will need some non-anonymous pages
4917 4917 * Call VOP_GETPAGE over the range of non-anonymous pages
4918 4918 * endif
4919 4919 * Loop over all addresses requested
4920 4920 * Call segvn_faultpage passing in page list
4921 4921 * to load up translations and handle anonymous pages
4922 4922 * endloop
4923 4923 * Load up translation to any additional pages in page list not
4924 4924 * already handled that fit into this segment
4925 4925 */
4926 4926 static faultcode_t
4927 4927 segvn_fault(struct hat *hat, struct seg *seg, caddr_t addr, size_t len,
4928 4928 enum fault_type type, enum seg_rw rw)
4929 4929 {
4930 4930 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
4931 4931 page_t **plp, **ppp, *pp;
4932 4932 u_offset_t off;
4933 4933 caddr_t a;
4934 4934 struct vpage *vpage;
4935 4935 uint_t vpprot, prot;
4936 4936 int err;
4937 4937 page_t *pl[PVN_GETPAGE_NUM + 1];
4938 4938 size_t plsz, pl_alloc_sz;
4939 4939 size_t page;
4940 4940 ulong_t anon_index;
4941 4941 struct anon_map *amp;
4942 4942 int dogetpage = 0;
4943 4943 caddr_t lpgaddr, lpgeaddr;
4944 4944 size_t pgsz;
4945 4945 anon_sync_obj_t cookie;
4946 4946 int brkcow = BREAK_COW_SHARE(rw, type, svd->type);
4947 4947
4948 4948 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
4949 4949 ASSERT(svd->amp == NULL || svd->rcookie == HAT_INVALID_REGION_COOKIE);
4950 4950
4951 4951 /*
4952 4952 * First handle the easy stuff
4953 4953 */
4954 4954 if (type == F_SOFTUNLOCK) {
4955 4955 if (rw == S_READ_NOCOW) {
4956 4956 rw = S_READ;
4957 4957 ASSERT(AS_WRITE_HELD(seg->s_as));
4958 4958 }
4959 4959 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
4960 4960 pgsz = (seg->s_szc == 0) ? PAGESIZE :
4961 4961 page_get_pagesize(seg->s_szc);
4962 4962 VM_STAT_COND_ADD(pgsz > PAGESIZE, segvnvmstats.fltanpages[16]);
4963 4963 CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr);
4964 4964 segvn_softunlock(seg, lpgaddr, lpgeaddr - lpgaddr, rw);
4965 4965 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
4966 4966 return (0);
4967 4967 }
4968 4968
4969 4969 ASSERT(svd->tr_state == SEGVN_TR_OFF ||
4970 4970 !HAT_IS_REGION_COOKIE_VALID(svd->rcookie));
4971 4971 if (brkcow == 0) {
4972 4972 if (svd->tr_state == SEGVN_TR_INIT) {
4973 4973 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
4974 4974 if (svd->tr_state == SEGVN_TR_INIT) {
4975 4975 ASSERT(svd->vp != NULL && svd->amp == NULL);
4976 4976 ASSERT(svd->flags & MAP_TEXT);
4977 4977 ASSERT(svd->type == MAP_PRIVATE);
4978 4978 segvn_textrepl(seg);
4979 4979 ASSERT(svd->tr_state != SEGVN_TR_INIT);
4980 4980 ASSERT(svd->tr_state != SEGVN_TR_ON ||
4981 4981 svd->amp != NULL);
4982 4982 }
4983 4983 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
4984 4984 }
4985 4985 } else if (svd->tr_state != SEGVN_TR_OFF) {
4986 4986 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
4987 4987
4988 4988 if (rw == S_WRITE && svd->tr_state != SEGVN_TR_OFF) {
4989 4989 ASSERT(!svd->pageprot && !(svd->prot & PROT_WRITE));
4990 4990 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
4991 4991 return (FC_PROT);
4992 4992 }
4993 4993
4994 4994 if (svd->tr_state == SEGVN_TR_ON) {
4995 4995 ASSERT(svd->vp != NULL && svd->amp != NULL);
4996 4996 segvn_textunrepl(seg, 0);
4997 4997 ASSERT(svd->amp == NULL &&
4998 4998 svd->tr_state == SEGVN_TR_OFF);
4999 4999 } else if (svd->tr_state != SEGVN_TR_OFF) {
5000 5000 svd->tr_state = SEGVN_TR_OFF;
5001 5001 }
5002 5002 ASSERT(svd->amp == NULL && svd->tr_state == SEGVN_TR_OFF);
5003 5003 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5004 5004 }
5005 5005
5006 5006 top:
5007 5007 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
5008 5008
5009 5009 /*
5010 5010 * If we have the same protections for the entire segment,
5011 5011 * insure that the access being attempted is legitimate.
5012 5012 */
5013 5013
5014 5014 if (svd->pageprot == 0) {
5015 5015 uint_t protchk;
5016 5016
5017 5017 switch (rw) {
5018 5018 case S_READ:
5019 5019 case S_READ_NOCOW:
5020 5020 protchk = PROT_READ;
5021 5021 break;
5022 5022 case S_WRITE:
5023 5023 protchk = PROT_WRITE;
5024 5024 break;
5025 5025 case S_EXEC:
5026 5026 protchk = PROT_EXEC;
5027 5027 break;
5028 5028 case S_OTHER:
5029 5029 default:
5030 5030 protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
5031 5031 break;
5032 5032 }
5033 5033
5034 5034 if ((svd->prot & protchk) == 0) {
5035 5035 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5036 5036 return (FC_PROT); /* illegal access type */
5037 5037 }
5038 5038 }
5039 5039
5040 5040 if (brkcow && HAT_IS_REGION_COOKIE_VALID(svd->rcookie)) {
5041 5041 /* this must be SOFTLOCK S_READ fault */
5042 5042 ASSERT(svd->amp == NULL);
5043 5043 ASSERT(svd->tr_state == SEGVN_TR_OFF);
5044 5044 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5045 5045 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
5046 5046 if (HAT_IS_REGION_COOKIE_VALID(svd->rcookie)) {
5047 5047 /*
5048 5048 * this must be the first ever non S_READ_NOCOW
5049 5049 * softlock for this segment.
5050 5050 */
5051 5051 ASSERT(svd->softlockcnt == 0);
5052 5052 hat_leave_region(seg->s_as->a_hat, svd->rcookie,
5053 5053 HAT_REGION_TEXT);
5054 5054 svd->rcookie = HAT_INVALID_REGION_COOKIE;
5055 5055 }
5056 5056 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5057 5057 goto top;
5058 5058 }
5059 5059
5060 5060 /*
5061 5061 * We can't allow the long term use of softlocks for vmpss segments,
5062 5062 * because in some file truncation cases we should be able to demote
5063 5063 * the segment, which requires that there are no softlocks. The
5064 5064 * only case where it's ok to allow a SOFTLOCK fault against a vmpss
5065 5065 * segment is S_READ_NOCOW, where the caller holds the address space
5066 5066 * locked as writer and calls softunlock before dropping the as lock.
5067 5067 * S_READ_NOCOW is used by /proc to read memory from another user.
5068 5068 *
5069 5069 * Another deadlock between SOFTLOCK and file truncation can happen
5070 5070 * because segvn_fault_vnodepages() calls the FS one pagesize at
5071 5071 * a time. A second VOP_GETPAGE() call by segvn_fault_vnodepages()
5072 5072 * can cause a deadlock because the first set of page_t's remain
5073 5073 * locked SE_SHARED. To avoid this, we demote segments on a first
5074 5074 * SOFTLOCK if they have a length greater than the segment's
5075 5075 * page size.
5076 5076 *
5077 5077 * So for now, we only avoid demoting a segment on a SOFTLOCK when
5078 5078 * the access type is S_READ_NOCOW and the fault length is less than
5079 5079 * or equal to the segment's page size. While this is quite restrictive,
5080 5080 * it should be the most common case of SOFTLOCK against a vmpss
5081 5081 * segment.
5082 5082 *
5083 5083 * For S_READ_NOCOW, it's safe not to do a copy on write because the
5084 5084 * caller makes sure no COW will be caused by another thread for a
5085 5085 * softlocked page.
5086 5086 */
5087 5087 if (type == F_SOFTLOCK && svd->vp != NULL && seg->s_szc != 0) {
5088 5088 int demote = 0;
5089 5089
5090 5090 if (rw != S_READ_NOCOW) {
5091 5091 demote = 1;
5092 5092 }
5093 5093 if (!demote && len > PAGESIZE) {
5094 5094 pgsz = page_get_pagesize(seg->s_szc);
5095 5095 CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr,
5096 5096 lpgeaddr);
5097 5097 if (lpgeaddr - lpgaddr > pgsz) {
5098 5098 demote = 1;
5099 5099 }
5100 5100 }
5101 5101
5102 5102 ASSERT(demote || AS_WRITE_HELD(seg->s_as));
5103 5103
5104 5104 if (demote) {
5105 5105 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5106 5106 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
5107 5107 if (seg->s_szc != 0) {
5108 5108 segvn_vmpss_clrszc_cnt++;
5109 5109 ASSERT(svd->softlockcnt == 0);
5110 5110 err = segvn_clrszc(seg);
5111 5111 if (err) {
5112 5112 segvn_vmpss_clrszc_err++;
5113 5113 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5114 5114 return (FC_MAKE_ERR(err));
5115 5115 }
5116 5116 }
5117 5117 ASSERT(seg->s_szc == 0);
5118 5118 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5119 5119 goto top;
5120 5120 }
5121 5121 }
5122 5122
5123 5123 /*
5124 5124 * Check to see if we need to allocate an anon_map structure.
5125 5125 */
5126 5126 if (svd->amp == NULL && (svd->vp == NULL || brkcow)) {
5127 5127 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
5128 5128 /*
5129 5129 * Drop the "read" lock on the segment and acquire
5130 5130 * the "write" version since we have to allocate the
5131 5131 * anon_map.
5132 5132 */
5133 5133 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5134 5134 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
5135 5135
5136 5136 if (svd->amp == NULL) {
5137 5137 svd->amp = anonmap_alloc(seg->s_size, 0, ANON_SLEEP);
5138 5138 svd->amp->a_szc = seg->s_szc;
5139 5139 }
5140 5140 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5141 5141
5142 5142 /*
5143 5143 * Start all over again since segment protections
5144 5144 * may have changed after we dropped the "read" lock.
5145 5145 */
5146 5146 goto top;
5147 5147 }
5148 5148
5149 5149 /*
5150 5150 * S_READ_NOCOW vs S_READ distinction was
5151 5151 * only needed for the code above. After
5152 5152 * that we treat it as S_READ.
5153 5153 */
5154 5154 if (rw == S_READ_NOCOW) {
5155 5155 ASSERT(type == F_SOFTLOCK);
5156 5156 ASSERT(AS_WRITE_HELD(seg->s_as));
5157 5157 rw = S_READ;
5158 5158 }
5159 5159
5160 5160 amp = svd->amp;
5161 5161
5162 5162 /*
5163 5163 * MADV_SEQUENTIAL work is ignored for large page segments.
5164 5164 */
5165 5165 if (seg->s_szc != 0) {
5166 5166 pgsz = page_get_pagesize(seg->s_szc);
5167 5167 ASSERT(SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
5168 5168 CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr);
5169 5169 if (svd->vp == NULL) {
5170 5170 err = segvn_fault_anonpages(hat, seg, lpgaddr,
5171 5171 lpgeaddr, type, rw, addr, addr + len, brkcow);
5172 5172 } else {
5173 5173 err = segvn_fault_vnodepages(hat, seg, lpgaddr,
5174 5174 lpgeaddr, type, rw, addr, addr + len, brkcow);
5175 5175 if (err == IE_RETRY) {
5176 5176 ASSERT(seg->s_szc == 0);
5177 5177 ASSERT(SEGVN_READ_HELD(seg->s_as, &svd->lock));
5178 5178 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5179 5179 goto top;
5180 5180 }
5181 5181 }
5182 5182 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5183 5183 return (err);
5184 5184 }
5185 5185
5186 5186 page = seg_page(seg, addr);
5187 5187 if (amp != NULL) {
5188 5188 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
5189 5189 anon_index = svd->anon_index + page;
5190 5190
5191 5191 if (type == F_PROT && rw == S_READ &&
5192 5192 svd->tr_state == SEGVN_TR_OFF &&
5193 5193 svd->type == MAP_PRIVATE && svd->pageprot == 0) {
5194 5194 size_t index = anon_index;
5195 5195 struct anon *ap;
5196 5196
5197 5197 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
5198 5198 /*
5199 5199 * The fast path could apply to S_WRITE also, except
5200 5200 * that the protection fault could be caused by lazy
5201 5201 * tlb flush when ro->rw. In this case, the pte is
5202 5202 * RW already. But RO in the other cpu's tlb causes
5203 5203 * the fault. Since hat_chgprot won't do anything if
5204 5204 * pte doesn't change, we may end up faulting
5205 5205 * indefinitely until the RO tlb entry gets replaced.
5206 5206 */
5207 5207 for (a = addr; a < addr + len; a += PAGESIZE, index++) {
5208 5208 anon_array_enter(amp, index, &cookie);
5209 5209 ap = anon_get_ptr(amp->ahp, index);
5210 5210 anon_array_exit(&cookie);
5211 5211 if ((ap == NULL) || (ap->an_refcnt != 1)) {
5212 5212 ANON_LOCK_EXIT(&->a_rwlock);
5213 5213 goto slow;
5214 5214 }
5215 5215 }
5216 5216 hat_chgprot(seg->s_as->a_hat, addr, len, svd->prot);
5217 5217 ANON_LOCK_EXIT(&->a_rwlock);
5218 5218 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5219 5219 return (0);
5220 5220 }
5221 5221 }
5222 5222 slow:
5223 5223
5224 5224 if (svd->vpage == NULL)
5225 5225 vpage = NULL;
5226 5226 else
5227 5227 vpage = &svd->vpage[page];
5228 5228
5229 5229 off = svd->offset + (uintptr_t)(addr - seg->s_base);
5230 5230
5231 5231 /*
5232 5232 * If MADV_SEQUENTIAL has been set for the particular page we
5233 5233 * are faulting on, free behind all pages in the segment and put
5234 5234 * them on the free list.
5235 5235 */
5236 5236
5237 5237 if ((page != 0) && fltadvice && svd->tr_state != SEGVN_TR_ON) {
5238 5238 struct vpage *vpp;
5239 5239 ulong_t fanon_index;
5240 5240 size_t fpage;
5241 5241 u_offset_t pgoff, fpgoff;
5242 5242 struct vnode *fvp;
5243 5243 struct anon *fap = NULL;
5244 5244
5245 5245 if (svd->advice == MADV_SEQUENTIAL ||
5246 5246 (svd->pageadvice &&
5247 5247 VPP_ADVICE(vpage) == MADV_SEQUENTIAL)) {
5248 5248 pgoff = off - PAGESIZE;
5249 5249 fpage = page - 1;
5250 5250 if (vpage != NULL)
5251 5251 vpp = &svd->vpage[fpage];
5252 5252 if (amp != NULL)
5253 5253 fanon_index = svd->anon_index + fpage;
5254 5254
5255 5255 while (pgoff > svd->offset) {
5256 5256 if (svd->advice != MADV_SEQUENTIAL &&
5257 5257 (!svd->pageadvice || (vpage &&
5258 5258 VPP_ADVICE(vpp) != MADV_SEQUENTIAL)))
5259 5259 break;
5260 5260
5261 5261 /*
5262 5262 * If this is an anon page, we must find the
5263 5263 * correct <vp, offset> for it
5264 5264 */
5265 5265 fap = NULL;
5266 5266 if (amp != NULL) {
5267 5267 ANON_LOCK_ENTER(&->a_rwlock,
5268 5268 RW_READER);
5269 5269 anon_array_enter(amp, fanon_index,
5270 5270 &cookie);
5271 5271 fap = anon_get_ptr(amp->ahp,
5272 5272 fanon_index);
5273 5273 if (fap != NULL) {
5274 5274 swap_xlate(fap, &fvp, &fpgoff);
5275 5275 } else {
5276 5276 fpgoff = pgoff;
5277 5277 fvp = svd->vp;
5278 5278 }
5279 5279 anon_array_exit(&cookie);
5280 5280 ANON_LOCK_EXIT(&->a_rwlock);
5281 5281 } else {
5282 5282 fpgoff = pgoff;
5283 5283 fvp = svd->vp;
5284 5284 }
5285 5285 if (fvp == NULL)
5286 5286 break; /* XXX */
5287 5287 /*
5288 5288 * Skip pages that are free or have an
5289 5289 * "exclusive" lock.
5290 5290 */
5291 5291 pp = page_lookup_nowait(fvp, fpgoff, SE_SHARED);
5292 5292 if (pp == NULL)
5293 5293 break;
5294 5294 /*
5295 5295 * We don't need the page_struct_lock to test
5296 5296 * as this is only advisory; even if we
5297 5297 * acquire it someone might race in and lock
5298 5298 * the page after we unlock and before the
5299 5299 * PUTPAGE, then VOP_PUTPAGE will do nothing.
5300 5300 */
5301 5301 if (pp->p_lckcnt == 0 && pp->p_cowcnt == 0) {
5302 5302 /*
5303 5303 * Hold the vnode before releasing
5304 5304 * the page lock to prevent it from
5305 5305 * being freed and re-used by some
5306 5306 * other thread.
5307 5307 */
5308 5308 VN_HOLD(fvp);
5309 5309 page_unlock(pp);
5310 5310 /*
5311 5311 * We should build a page list
5312 5312 * to kluster putpages XXX
5313 5313 */
5314 5314 (void) VOP_PUTPAGE(fvp,
5315 5315 (offset_t)fpgoff, PAGESIZE,
5316 5316 (B_DONTNEED|B_FREE|B_ASYNC),
5317 5317 svd->cred, NULL);
5318 5318 VN_RELE(fvp);
5319 5319 } else {
5320 5320 /*
5321 5321 * XXX - Should the loop terminate if
5322 5322 * the page is `locked'?
5323 5323 */
5324 5324 page_unlock(pp);
5325 5325 }
5326 5326 --vpp;
5327 5327 --fanon_index;
5328 5328 pgoff -= PAGESIZE;
5329 5329 }
5330 5330 }
5331 5331 }
5332 5332
5333 5333 plp = pl;
5334 5334 *plp = NULL;
5335 5335 pl_alloc_sz = 0;
5336 5336
5337 5337 /*
5338 5338 * See if we need to call VOP_GETPAGE for
5339 5339 * *any* of the range being faulted on.
5340 5340 * We can skip all of this work if there
5341 5341 * was no original vnode.
5342 5342 */
5343 5343 if (svd->vp != NULL) {
5344 5344 u_offset_t vp_off;
5345 5345 size_t vp_len;
5346 5346 struct anon *ap;
5347 5347 vnode_t *vp;
5348 5348
5349 5349 vp_off = off;
5350 5350 vp_len = len;
5351 5351
5352 5352 if (amp == NULL)
5353 5353 dogetpage = 1;
5354 5354 else {
5355 5355 /*
5356 5356 * Only acquire reader lock to prevent amp->ahp
5357 5357 * from being changed. It's ok to miss pages,
5358 5358 * hence we don't do anon_array_enter
5359 5359 */
5360 5360 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
5361 5361 ap = anon_get_ptr(amp->ahp, anon_index);
5362 5362
5363 5363 if (len <= PAGESIZE)
5364 5364 /* inline non_anon() */
5365 5365 dogetpage = (ap == NULL);
5366 5366 else
5367 5367 dogetpage = non_anon(amp->ahp, anon_index,
5368 5368 &vp_off, &vp_len);
5369 5369 ANON_LOCK_EXIT(&->a_rwlock);
5370 5370 }
5371 5371
5372 5372 if (dogetpage) {
5373 5373 enum seg_rw arw;
5374 5374 struct as *as = seg->s_as;
5375 5375
5376 5376 if (len > ptob((sizeof (pl) / sizeof (pl[0])) - 1)) {
5377 5377 /*
5378 5378 * Page list won't fit in local array,
5379 5379 * allocate one of the needed size.
5380 5380 */
5381 5381 pl_alloc_sz =
5382 5382 (btop(len) + 1) * sizeof (page_t *);
5383 5383 plp = kmem_alloc(pl_alloc_sz, KM_SLEEP);
5384 5384 plp[0] = NULL;
5385 5385 plsz = len;
5386 5386 } else if (rw == S_WRITE && svd->type == MAP_PRIVATE ||
5387 5387 svd->tr_state == SEGVN_TR_ON || rw == S_OTHER ||
5388 5388 (((size_t)(addr + PAGESIZE) <
5389 5389 (size_t)(seg->s_base + seg->s_size)) &&
5390 5390 hat_probe(as->a_hat, addr + PAGESIZE))) {
5391 5391 /*
5392 5392 * Ask VOP_GETPAGE to return the exact number
5393 5393 * of pages if
5394 5394 * (a) this is a COW fault, or
5395 5395 * (b) this is a software fault, or
5396 5396 * (c) next page is already mapped.
5397 5397 */
5398 5398 plsz = len;
5399 5399 } else {
5400 5400 /*
5401 5401 * Ask VOP_GETPAGE to return adjacent pages
5402 5402 * within the segment.
5403 5403 */
5404 5404 plsz = MIN((size_t)PVN_GETPAGE_SZ, (size_t)
5405 5405 ((seg->s_base + seg->s_size) - addr));
5406 5406 ASSERT((addr + plsz) <=
5407 5407 (seg->s_base + seg->s_size));
5408 5408 }
5409 5409
5410 5410 /*
5411 5411 * Need to get some non-anonymous pages.
5412 5412 * We need to make only one call to GETPAGE to do
5413 5413 * this to prevent certain deadlocking conditions
5414 5414 * when we are doing locking. In this case
5415 5415 * non_anon() should have picked up the smallest
5416 5416 * range which includes all the non-anonymous
5417 5417 * pages in the requested range. We have to
5418 5418 * be careful regarding which rw flag to pass in
5419 5419 * because on a private mapping, the underlying
5420 5420 * object is never allowed to be written.
5421 5421 */
5422 5422 if (rw == S_WRITE && svd->type == MAP_PRIVATE) {
5423 5423 arw = S_READ;
5424 5424 } else {
5425 5425 arw = rw;
5426 5426 }
5427 5427 vp = svd->vp;
5428 5428 TRACE_3(TR_FAC_VM, TR_SEGVN_GETPAGE,
5429 5429 "segvn_getpage:seg %p addr %p vp %p",
5430 5430 seg, addr, vp);
5431 5431 err = VOP_GETPAGE(vp, (offset_t)vp_off, vp_len,
5432 5432 &vpprot, plp, plsz, seg, addr + (vp_off - off), arw,
5433 5433 svd->cred, NULL);
5434 5434 if (err) {
5435 5435 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5436 5436 segvn_pagelist_rele(plp);
5437 5437 if (pl_alloc_sz)
5438 5438 kmem_free(plp, pl_alloc_sz);
5439 5439 return (FC_MAKE_ERR(err));
5440 5440 }
5441 5441 if (svd->type == MAP_PRIVATE)
5442 5442 vpprot &= ~PROT_WRITE;
5443 5443 }
5444 5444 }
5445 5445
5446 5446 /*
5447 5447 * N.B. at this time the plp array has all the needed non-anon
5448 5448 * pages in addition to (possibly) having some adjacent pages.
5449 5449 */
5450 5450
5451 5451 /*
5452 5452 * Always acquire the anon_array_lock to prevent
5453 5453 * 2 threads from allocating separate anon slots for
5454 5454 * the same "addr".
5455 5455 *
5456 5456 * If this is a copy-on-write fault and we don't already
5457 5457 * have the anon_array_lock, acquire it to prevent the
5458 5458 * fault routine from handling multiple copy-on-write faults
5459 5459 * on the same "addr" in the same address space.
5460 5460 *
5461 5461 * Only one thread should deal with the fault since after
5462 5462 * it is handled, the other threads can acquire a translation
5463 5463 * to the newly created private page. This prevents two or
5464 5464 * more threads from creating different private pages for the
5465 5465 * same fault.
5466 5466 *
5467 5467 * We grab "serialization" lock here if this is a MAP_PRIVATE segment
5468 5468 * to prevent deadlock between this thread and another thread
5469 5469 * which has soft-locked this page and wants to acquire serial_lock.
5470 5470 * ( bug 4026339 )
5471 5471 *
5472 5472 * The fix for bug 4026339 becomes unnecessary when using the
5473 5473 * locking scheme with per amp rwlock and a global set of hash
5474 5474 * lock, anon_array_lock. If we steal a vnode page when low
5475 5475 * on memory and upgrad the page lock through page_rename,
5476 5476 * then the page is PAGE_HANDLED, nothing needs to be done
5477 5477 * for this page after returning from segvn_faultpage.
5478 5478 *
5479 5479 * But really, the page lock should be downgraded after
5480 5480 * the stolen page is page_rename'd.
5481 5481 */
5482 5482
5483 5483 if (amp != NULL)
5484 5484 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
5485 5485
5486 5486 /*
5487 5487 * Ok, now loop over the address range and handle faults
5488 5488 */
5489 5489 for (a = addr; a < addr + len; a += PAGESIZE, off += PAGESIZE) {
5490 5490 err = segvn_faultpage(hat, seg, a, off, vpage, plp, vpprot,
5491 5491 type, rw, brkcow);
5492 5492 if (err) {
5493 5493 if (amp != NULL)
5494 5494 ANON_LOCK_EXIT(&->a_rwlock);
5495 5495 if (type == F_SOFTLOCK && a > addr) {
5496 5496 segvn_softunlock(seg, addr, (a - addr),
5497 5497 S_OTHER);
5498 5498 }
5499 5499 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5500 5500 segvn_pagelist_rele(plp);
5501 5501 if (pl_alloc_sz)
5502 5502 kmem_free(plp, pl_alloc_sz);
5503 5503 return (err);
5504 5504 }
5505 5505 if (vpage) {
5506 5506 vpage++;
5507 5507 } else if (svd->vpage) {
5508 5508 page = seg_page(seg, addr);
5509 5509 vpage = &svd->vpage[++page];
5510 5510 }
5511 5511 }
5512 5512
5513 5513 /* Didn't get pages from the underlying fs so we're done */
5514 5514 if (!dogetpage)
5515 5515 goto done;
5516 5516
5517 5517 /*
5518 5518 * Now handle any other pages in the list returned.
5519 5519 * If the page can be used, load up the translations now.
5520 5520 * Note that the for loop will only be entered if "plp"
5521 5521 * is pointing to a non-NULL page pointer which means that
5522 5522 * VOP_GETPAGE() was called and vpprot has been initialized.
5523 5523 */
5524 5524 if (svd->pageprot == 0)
5525 5525 prot = svd->prot & vpprot;
5526 5526
5527 5527
5528 5528 /*
5529 5529 * Large Files: diff should be unsigned value because we started
5530 5530 * supporting > 2GB segment sizes from 2.5.1 and when a
5531 5531 * large file of size > 2GB gets mapped to address space
5532 5532 * the diff value can be > 2GB.
5533 5533 */
5534 5534
5535 5535 for (ppp = plp; (pp = *ppp) != NULL; ppp++) {
5536 5536 size_t diff;
5537 5537 struct anon *ap;
5538 5538 int anon_index;
5539 5539 anon_sync_obj_t cookie;
5540 5540 int hat_flag = HAT_LOAD_ADV;
5541 5541
5542 5542 if (svd->flags & MAP_TEXT) {
5543 5543 hat_flag |= HAT_LOAD_TEXT;
5544 5544 }
5545 5545
5546 5546 if (pp == PAGE_HANDLED)
5547 5547 continue;
5548 5548
5549 5549 if (svd->tr_state != SEGVN_TR_ON &&
5550 5550 pp->p_offset >= svd->offset &&
5551 5551 pp->p_offset < svd->offset + seg->s_size) {
5552 5552
5553 5553 diff = pp->p_offset - svd->offset;
5554 5554
5555 5555 /*
5556 5556 * Large Files: Following is the assertion
5557 5557 * validating the above cast.
5558 5558 */
5559 5559 ASSERT(svd->vp == pp->p_vnode);
5560 5560
5561 5561 page = btop(diff);
5562 5562 if (svd->pageprot)
5563 5563 prot = VPP_PROT(&svd->vpage[page]) & vpprot;
5564 5564
5565 5565 /*
5566 5566 * Prevent other threads in the address space from
5567 5567 * creating private pages (i.e., allocating anon slots)
5568 5568 * while we are in the process of loading translations
5569 5569 * to additional pages returned by the underlying
5570 5570 * object.
5571 5571 */
5572 5572 if (amp != NULL) {
5573 5573 anon_index = svd->anon_index + page;
5574 5574 anon_array_enter(amp, anon_index, &cookie);
5575 5575 ap = anon_get_ptr(amp->ahp, anon_index);
5576 5576 }
5577 5577 if ((amp == NULL) || (ap == NULL)) {
5578 5578 if (IS_VMODSORT(pp->p_vnode) ||
5579 5579 enable_mbit_wa) {
5580 5580 if (rw == S_WRITE)
5581 5581 hat_setmod(pp);
5582 5582 else if (rw != S_OTHER &&
5583 5583 !hat_ismod(pp))
5584 5584 prot &= ~PROT_WRITE;
5585 5585 }
5586 5586 /*
5587 5587 * Skip mapping read ahead pages marked
5588 5588 * for migration, so they will get migrated
5589 5589 * properly on fault
5590 5590 */
5591 5591 ASSERT(amp == NULL ||
5592 5592 svd->rcookie == HAT_INVALID_REGION_COOKIE);
5593 5593 if ((prot & PROT_READ) && !PP_ISMIGRATE(pp)) {
5594 5594 hat_memload_region(hat,
5595 5595 seg->s_base + diff,
5596 5596 pp, prot, hat_flag,
5597 5597 svd->rcookie);
5598 5598 }
5599 5599 }
5600 5600 if (amp != NULL)
5601 5601 anon_array_exit(&cookie);
5602 5602 }
5603 5603 page_unlock(pp);
5604 5604 }
5605 5605 done:
5606 5606 if (amp != NULL)
5607 5607 ANON_LOCK_EXIT(&->a_rwlock);
5608 5608 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5609 5609 if (pl_alloc_sz)
5610 5610 kmem_free(plp, pl_alloc_sz);
5611 5611 return (0);
5612 5612 }
5613 5613
5614 5614 /*
5615 5615 * This routine is used to start I/O on pages asynchronously. XXX it will
5616 5616 * only create PAGESIZE pages. At fault time they will be relocated into
5617 5617 * larger pages.
5618 5618 */
5619 5619 static faultcode_t
5620 5620 segvn_faulta(struct seg *seg, caddr_t addr)
5621 5621 {
5622 5622 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
5623 5623 int err;
5624 5624 struct anon_map *amp;
5625 5625 vnode_t *vp;
5626 5626
5627 5627 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
5628 5628
5629 5629 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
5630 5630 if ((amp = svd->amp) != NULL) {
5631 5631 struct anon *ap;
5632 5632
5633 5633 /*
5634 5634 * Reader lock to prevent amp->ahp from being changed.
5635 5635 * This is advisory, it's ok to miss a page, so
5636 5636 * we don't do anon_array_enter lock.
5637 5637 */
5638 5638 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
5639 5639 if ((ap = anon_get_ptr(amp->ahp,
5640 5640 svd->anon_index + seg_page(seg, addr))) != NULL) {
5641 5641
5642 5642 err = anon_getpage(&ap, NULL, NULL,
5643 5643 0, seg, addr, S_READ, svd->cred);
5644 5644
5645 5645 ANON_LOCK_EXIT(&->a_rwlock);
5646 5646 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5647 5647 if (err)
5648 5648 return (FC_MAKE_ERR(err));
5649 5649 return (0);
5650 5650 }
5651 5651 ANON_LOCK_EXIT(&->a_rwlock);
5652 5652 }
5653 5653
5654 5654 if (svd->vp == NULL) {
5655 5655 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5656 5656 return (0); /* zfod page - do nothing now */
5657 5657 }
5658 5658
5659 5659 vp = svd->vp;
5660 5660 TRACE_3(TR_FAC_VM, TR_SEGVN_GETPAGE,
5661 5661 "segvn_getpage:seg %p addr %p vp %p", seg, addr, vp);
5662 5662 err = VOP_GETPAGE(vp,
5663 5663 (offset_t)(svd->offset + (uintptr_t)(addr - seg->s_base)),
5664 5664 PAGESIZE, NULL, NULL, 0, seg, addr,
5665 5665 S_OTHER, svd->cred, NULL);
5666 5666
5667 5667 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5668 5668 if (err)
5669 5669 return (FC_MAKE_ERR(err));
5670 5670 return (0);
5671 5671 }
5672 5672
5673 5673 static int
5674 5674 segvn_setprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
5675 5675 {
5676 5676 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
5677 5677 struct vpage *cvp, *svp, *evp;
5678 5678 struct vnode *vp;
5679 5679 size_t pgsz;
5680 5680 pgcnt_t pgcnt;
5681 5681 anon_sync_obj_t cookie;
5682 5682 int unload_done = 0;
5683 5683
5684 5684 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
5685 5685
5686 5686 if ((svd->maxprot & prot) != prot)
5687 5687 return (EACCES); /* violated maxprot */
5688 5688
5689 5689 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
5690 5690
5691 5691 /* return if prot is the same */
5692 5692 if (!svd->pageprot && svd->prot == prot) {
5693 5693 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5694 5694 return (0);
5695 5695 }
5696 5696
5697 5697 /*
5698 5698 * Since we change protections we first have to flush the cache.
5699 5699 * This makes sure all the pagelock calls have to recheck
5700 5700 * protections.
5701 5701 */
5702 5702 if (svd->softlockcnt > 0) {
5703 5703 ASSERT(svd->tr_state == SEGVN_TR_OFF);
5704 5704
5705 5705 /*
5706 5706 * If this is shared segment non 0 softlockcnt
5707 5707 * means locked pages are still in use.
5708 5708 */
5709 5709 if (svd->type == MAP_SHARED) {
5710 5710 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5711 5711 return (EAGAIN);
5712 5712 }
5713 5713
5714 5714 /*
5715 5715 * Since we do have the segvn writers lock nobody can fill
5716 5716 * the cache with entries belonging to this seg during
5717 5717 * the purge. The flush either succeeds or we still have
5718 5718 * pending I/Os.
5719 5719 */
5720 5720 segvn_purge(seg);
5721 5721 if (svd->softlockcnt > 0) {
5722 5722 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5723 5723 return (EAGAIN);
5724 5724 }
5725 5725 }
5726 5726
5727 5727 if (HAT_IS_REGION_COOKIE_VALID(svd->rcookie)) {
5728 5728 ASSERT(svd->amp == NULL);
5729 5729 ASSERT(svd->tr_state == SEGVN_TR_OFF);
5730 5730 hat_leave_region(seg->s_as->a_hat, svd->rcookie,
5731 5731 HAT_REGION_TEXT);
5732 5732 svd->rcookie = HAT_INVALID_REGION_COOKIE;
5733 5733 unload_done = 1;
5734 5734 } else if (svd->tr_state == SEGVN_TR_INIT) {
5735 5735 svd->tr_state = SEGVN_TR_OFF;
5736 5736 } else if (svd->tr_state == SEGVN_TR_ON) {
5737 5737 ASSERT(svd->amp != NULL);
5738 5738 segvn_textunrepl(seg, 0);
5739 5739 ASSERT(svd->amp == NULL && svd->tr_state == SEGVN_TR_OFF);
5740 5740 unload_done = 1;
5741 5741 }
5742 5742
5743 5743 if ((prot & PROT_WRITE) && svd->type == MAP_SHARED &&
5744 5744 svd->vp != NULL && (svd->vp->v_flag & VVMEXEC)) {
5745 5745 ASSERT(vn_is_mapped(svd->vp, V_WRITE));
5746 5746 segvn_inval_trcache(svd->vp);
5747 5747 }
5748 5748 if (seg->s_szc != 0) {
5749 5749 int err;
5750 5750 pgsz = page_get_pagesize(seg->s_szc);
5751 5751 pgcnt = pgsz >> PAGESHIFT;
5752 5752 ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
5753 5753 if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(len, pgsz)) {
5754 5754 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5755 5755 ASSERT(seg->s_base != addr || seg->s_size != len);
5756 5756 /*
5757 5757 * If we are holding the as lock as a reader then
5758 5758 * we need to return IE_RETRY and let the as
5759 5759 * layer drop and re-acquire the lock as a writer.
5760 5760 */
5761 5761 if (AS_READ_HELD(seg->s_as))
5762 5762 return (IE_RETRY);
5763 5763 VM_STAT_ADD(segvnvmstats.demoterange[1]);
5764 5764 if (svd->type == MAP_PRIVATE || svd->vp != NULL) {
5765 5765 err = segvn_demote_range(seg, addr, len,
5766 5766 SDR_END, 0);
5767 5767 } else {
5768 5768 uint_t szcvec = map_pgszcvec(seg->s_base,
5769 5769 pgsz, (uintptr_t)seg->s_base,
5770 5770 (svd->flags & MAP_TEXT), MAPPGSZC_SHM, 0);
5771 5771 err = segvn_demote_range(seg, addr, len,
5772 5772 SDR_END, szcvec);
5773 5773 }
5774 5774 if (err == 0)
5775 5775 return (IE_RETRY);
5776 5776 if (err == ENOMEM)
5777 5777 return (IE_NOMEM);
5778 5778 return (err);
5779 5779 }
5780 5780 }
5781 5781
5782 5782
5783 5783 /*
5784 5784 * If it's a private mapping and we're making it writable then we
5785 5785 * may have to reserve the additional swap space now. If we are
5786 5786 * making writable only a part of the segment then we use its vpage
5787 5787 * array to keep a record of the pages for which we have reserved
5788 5788 * swap. In this case we set the pageswap field in the segment's
5789 5789 * segvn structure to record this.
5790 5790 *
5791 5791 * If it's a private mapping to a file (i.e., vp != NULL) and we're
5792 5792 * removing write permission on the entire segment and we haven't
5793 5793 * modified any pages, we can release the swap space.
5794 5794 */
5795 5795 if (svd->type == MAP_PRIVATE) {
5796 5796 if (prot & PROT_WRITE) {
5797 5797 if (!(svd->flags & MAP_NORESERVE) &&
5798 5798 !(svd->swresv && svd->pageswap == 0)) {
5799 5799 size_t sz = 0;
5800 5800
5801 5801 /*
5802 5802 * Start by determining how much swap
5803 5803 * space is required.
5804 5804 */
5805 5805 if (addr == seg->s_base &&
5806 5806 len == seg->s_size &&
5807 5807 svd->pageswap == 0) {
5808 5808 /* The whole segment */
5809 5809 sz = seg->s_size;
5810 5810 } else {
5811 5811 /*
5812 5812 * Make sure that the vpage array
5813 5813 * exists, and make a note of the
5814 5814 * range of elements corresponding
5815 5815 * to len.
5816 5816 */
5817 5817 segvn_vpage(seg);
5818 5818 if (svd->vpage == NULL) {
5819 5819 SEGVN_LOCK_EXIT(seg->s_as,
5820 5820 &svd->lock);
5821 5821 return (ENOMEM);
5822 5822 }
5823 5823 svp = &svd->vpage[seg_page(seg, addr)];
5824 5824 evp = &svd->vpage[seg_page(seg,
5825 5825 addr + len)];
5826 5826
5827 5827 if (svd->pageswap == 0) {
5828 5828 /*
5829 5829 * This is the first time we've
5830 5830 * asked for a part of this
5831 5831 * segment, so we need to
5832 5832 * reserve everything we've
5833 5833 * been asked for.
5834 5834 */
5835 5835 sz = len;
5836 5836 } else {
5837 5837 /*
5838 5838 * We have to count the number
5839 5839 * of pages required.
5840 5840 */
5841 5841 for (cvp = svp; cvp < evp;
5842 5842 cvp++) {
5843 5843 if (!VPP_ISSWAPRES(cvp))
5844 5844 sz++;
5845 5845 }
5846 5846 sz <<= PAGESHIFT;
5847 5847 }
5848 5848 }
5849 5849
5850 5850 /* Try to reserve the necessary swap. */
5851 5851 if (anon_resv_zone(sz,
5852 5852 seg->s_as->a_proc->p_zone) == 0) {
5853 5853 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5854 5854 return (IE_NOMEM);
5855 5855 }
5856 5856
5857 5857 /*
5858 5858 * Make a note of how much swap space
5859 5859 * we've reserved.
5860 5860 */
5861 5861 if (svd->pageswap == 0 && sz == seg->s_size) {
5862 5862 svd->swresv = sz;
5863 5863 } else {
5864 5864 ASSERT(svd->vpage != NULL);
5865 5865 svd->swresv += sz;
5866 5866 svd->pageswap = 1;
5867 5867 for (cvp = svp; cvp < evp; cvp++) {
5868 5868 if (!VPP_ISSWAPRES(cvp))
5869 5869 VPP_SETSWAPRES(cvp);
5870 5870 }
5871 5871 }
5872 5872 }
5873 5873 } else {
5874 5874 /*
5875 5875 * Swap space is released only if this segment
5876 5876 * does not map anonymous memory, since read faults
5877 5877 * on such segments still need an anon slot to read
5878 5878 * in the data.
5879 5879 */
5880 5880 if (svd->swresv != 0 && svd->vp != NULL &&
5881 5881 svd->amp == NULL && addr == seg->s_base &&
5882 5882 len == seg->s_size && svd->pageprot == 0) {
5883 5883 ASSERT(svd->pageswap == 0);
5884 5884 anon_unresv_zone(svd->swresv,
5885 5885 seg->s_as->a_proc->p_zone);
5886 5886 svd->swresv = 0;
5887 5887 TRACE_3(TR_FAC_VM, TR_ANON_PROC,
5888 5888 "anon proc:%p %lu %u", seg, 0, 0);
5889 5889 }
5890 5890 }
5891 5891 }
5892 5892
5893 5893 if (addr == seg->s_base && len == seg->s_size && svd->vpage == NULL) {
5894 5894 if (svd->prot == prot) {
5895 5895 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5896 5896 return (0); /* all done */
5897 5897 }
5898 5898 svd->prot = (uchar_t)prot;
5899 5899 } else if (svd->type == MAP_PRIVATE) {
5900 5900 struct anon *ap = NULL;
5901 5901 page_t *pp;
5902 5902 u_offset_t offset, off;
5903 5903 struct anon_map *amp;
5904 5904 ulong_t anon_idx = 0;
5905 5905
5906 5906 /*
5907 5907 * A vpage structure exists or else the change does not
5908 5908 * involve the entire segment. Establish a vpage structure
5909 5909 * if none is there. Then, for each page in the range,
5910 5910 * adjust its individual permissions. Note that write-
5911 5911 * enabling a MAP_PRIVATE page can affect the claims for
5912 5912 * locked down memory. Overcommitting memory terminates
5913 5913 * the operation.
5914 5914 */
5915 5915 segvn_vpage(seg);
5916 5916 if (svd->vpage == NULL) {
5917 5917 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
5918 5918 return (ENOMEM);
5919 5919 }
5920 5920 svd->pageprot = 1;
5921 5921 if ((amp = svd->amp) != NULL) {
5922 5922 anon_idx = svd->anon_index + seg_page(seg, addr);
5923 5923 ASSERT(seg->s_szc == 0 ||
5924 5924 IS_P2ALIGNED(anon_idx, pgcnt));
5925 5925 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
5926 5926 }
5927 5927
5928 5928 offset = svd->offset + (uintptr_t)(addr - seg->s_base);
5929 5929 evp = &svd->vpage[seg_page(seg, addr + len)];
5930 5930
5931 5931 /*
5932 5932 * See Statement at the beginning of segvn_lockop regarding
5933 5933 * the way cowcnts and lckcnts are handled.
5934 5934 */
5935 5935 for (svp = &svd->vpage[seg_page(seg, addr)]; svp < evp; svp++) {
5936 5936
5937 5937 if (seg->s_szc != 0) {
5938 5938 if (amp != NULL) {
5939 5939 anon_array_enter(amp, anon_idx,
5940 5940 &cookie);
5941 5941 }
5942 5942 if (IS_P2ALIGNED(anon_idx, pgcnt) &&
5943 5943 !segvn_claim_pages(seg, svp, offset,
5944 5944 anon_idx, prot)) {
5945 5945 if (amp != NULL) {
5946 5946 anon_array_exit(&cookie);
5947 5947 }
5948 5948 break;
5949 5949 }
5950 5950 if (amp != NULL) {
5951 5951 anon_array_exit(&cookie);
5952 5952 }
5953 5953 anon_idx++;
5954 5954 } else {
5955 5955 if (amp != NULL) {
5956 5956 anon_array_enter(amp, anon_idx,
5957 5957 &cookie);
5958 5958 ap = anon_get_ptr(amp->ahp, anon_idx++);
5959 5959 }
5960 5960
5961 5961 if (VPP_ISPPLOCK(svp) &&
5962 5962 VPP_PROT(svp) != prot) {
5963 5963
5964 5964 if (amp == NULL || ap == NULL) {
5965 5965 vp = svd->vp;
5966 5966 off = offset;
5967 5967 } else
5968 5968 swap_xlate(ap, &vp, &off);
5969 5969 if (amp != NULL)
5970 5970 anon_array_exit(&cookie);
5971 5971
5972 5972 if ((pp = page_lookup(vp, off,
5973 5973 SE_SHARED)) == NULL) {
5974 5974 panic("segvn_setprot: no page");
5975 5975 /*NOTREACHED*/
5976 5976 }
5977 5977 ASSERT(seg->s_szc == 0);
5978 5978 if ((VPP_PROT(svp) ^ prot) &
5979 5979 PROT_WRITE) {
5980 5980 if (prot & PROT_WRITE) {
5981 5981 if (!page_addclaim(
5982 5982 pp)) {
5983 5983 page_unlock(pp);
5984 5984 break;
5985 5985 }
5986 5986 } else {
5987 5987 if (!page_subclaim(
5988 5988 pp)) {
5989 5989 page_unlock(pp);
5990 5990 break;
5991 5991 }
5992 5992 }
5993 5993 }
5994 5994 page_unlock(pp);
5995 5995 } else if (amp != NULL)
5996 5996 anon_array_exit(&cookie);
5997 5997 }
5998 5998 VPP_SETPROT(svp, prot);
5999 5999 offset += PAGESIZE;
6000 6000 }
6001 6001 if (amp != NULL)
6002 6002 ANON_LOCK_EXIT(&->a_rwlock);
6003 6003
6004 6004 /*
6005 6005 * Did we terminate prematurely? If so, simply unload
6006 6006 * the translations to the things we've updated so far.
6007 6007 */
6008 6008 if (svp != evp) {
6009 6009 if (unload_done) {
6010 6010 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
6011 6011 return (IE_NOMEM);
6012 6012 }
6013 6013 len = (svp - &svd->vpage[seg_page(seg, addr)]) *
6014 6014 PAGESIZE;
6015 6015 ASSERT(seg->s_szc == 0 || IS_P2ALIGNED(len, pgsz));
6016 6016 if (len != 0)
6017 6017 hat_unload(seg->s_as->a_hat, addr,
6018 6018 len, HAT_UNLOAD);
6019 6019 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
6020 6020 return (IE_NOMEM);
6021 6021 }
6022 6022 } else {
6023 6023 segvn_vpage(seg);
6024 6024 if (svd->vpage == NULL) {
6025 6025 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
6026 6026 return (ENOMEM);
6027 6027 }
6028 6028 svd->pageprot = 1;
6029 6029 evp = &svd->vpage[seg_page(seg, addr + len)];
6030 6030 for (svp = &svd->vpage[seg_page(seg, addr)]; svp < evp; svp++) {
6031 6031 VPP_SETPROT(svp, prot);
6032 6032 }
6033 6033 }
6034 6034
6035 6035 if (unload_done) {
6036 6036 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
6037 6037 return (0);
6038 6038 }
6039 6039
6040 6040 if (((prot & PROT_WRITE) != 0 &&
6041 6041 (svd->vp != NULL || svd->type == MAP_PRIVATE)) ||
6042 6042 (prot & ~PROT_USER) == PROT_NONE) {
6043 6043 /*
6044 6044 * Either private or shared data with write access (in
6045 6045 * which case we need to throw out all former translations
6046 6046 * so that we get the right translations set up on fault
6047 6047 * and we don't allow write access to any copy-on-write pages
6048 6048 * that might be around or to prevent write access to pages
6049 6049 * representing holes in a file), or we don't have permission
6050 6050 * to access the memory at all (in which case we have to
6051 6051 * unload any current translations that might exist).
6052 6052 */
6053 6053 hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD);
6054 6054 } else {
6055 6055 /*
6056 6056 * A shared mapping or a private mapping in which write
6057 6057 * protection is going to be denied - just change all the
6058 6058 * protections over the range of addresses in question.
6059 6059 * segvn does not support any other attributes other
6060 6060 * than prot so we can use hat_chgattr.
6061 6061 */
6062 6062 hat_chgattr(seg->s_as->a_hat, addr, len, prot);
6063 6063 }
6064 6064
6065 6065 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
6066 6066
6067 6067 return (0);
6068 6068 }
6069 6069
6070 6070 /*
6071 6071 * segvn_setpagesize is called via SEGOP_SETPAGESIZE from as_setpagesize,
6072 6072 * to determine if the seg is capable of mapping the requested szc.
6073 6073 */
6074 6074 static int
6075 6075 segvn_setpagesize(struct seg *seg, caddr_t addr, size_t len, uint_t szc)
6076 6076 {
6077 6077 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
6078 6078 struct segvn_data *nsvd;
6079 6079 struct anon_map *amp = svd->amp;
6080 6080 struct seg *nseg;
6081 6081 caddr_t eaddr = addr + len, a;
6082 6082 size_t pgsz = page_get_pagesize(szc);
6083 6083 pgcnt_t pgcnt = page_get_pagecnt(szc);
6084 6084 int err;
6085 6085 u_offset_t off = svd->offset + (uintptr_t)(addr - seg->s_base);
6086 6086
6087 6087 ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
6088 6088 ASSERT(addr >= seg->s_base && eaddr <= seg->s_base + seg->s_size);
6089 6089
6090 6090 if (seg->s_szc == szc || segvn_lpg_disable != 0) {
6091 6091 return (0);
6092 6092 }
6093 6093
6094 6094 /*
6095 6095 * addr should always be pgsz aligned but eaddr may be misaligned if
6096 6096 * it's at the end of the segment.
6097 6097 *
6098 6098 * XXX we should assert this condition since as_setpagesize() logic
6099 6099 * guarantees it.
6100 6100 */
6101 6101 if (!IS_P2ALIGNED(addr, pgsz) ||
6102 6102 (!IS_P2ALIGNED(eaddr, pgsz) &&
6103 6103 eaddr != seg->s_base + seg->s_size)) {
6104 6104
6105 6105 segvn_setpgsz_align_err++;
6106 6106 return (EINVAL);
6107 6107 }
6108 6108
6109 6109 if (amp != NULL && svd->type == MAP_SHARED) {
6110 6110 ulong_t an_idx = svd->anon_index + seg_page(seg, addr);
6111 6111 if (!IS_P2ALIGNED(an_idx, pgcnt)) {
6112 6112
6113 6113 segvn_setpgsz_anon_align_err++;
6114 6114 return (EINVAL);
6115 6115 }
6116 6116 }
6117 6117
6118 6118 if ((svd->flags & MAP_NORESERVE) || seg->s_as == &kas ||
6119 6119 szc > segvn_maxpgszc) {
6120 6120 return (EINVAL);
6121 6121 }
6122 6122
6123 6123 /* paranoid check */
6124 6124 if (svd->vp != NULL &&
6125 6125 (IS_SWAPFSVP(svd->vp) || VN_ISKAS(svd->vp))) {
6126 6126 return (EINVAL);
6127 6127 }
6128 6128
6129 6129 if (seg->s_szc == 0 && svd->vp != NULL &&
6130 6130 map_addr_vacalign_check(addr, off)) {
6131 6131 return (EINVAL);
6132 6132 }
6133 6133
6134 6134 /*
6135 6135 * Check that protections are the same within new page
6136 6136 * size boundaries.
6137 6137 */
6138 6138 if (svd->pageprot) {
6139 6139 for (a = addr; a < eaddr; a += pgsz) {
6140 6140 if ((a + pgsz) > eaddr) {
6141 6141 if (!sameprot(seg, a, eaddr - a)) {
6142 6142 return (EINVAL);
6143 6143 }
6144 6144 } else {
6145 6145 if (!sameprot(seg, a, pgsz)) {
6146 6146 return (EINVAL);
6147 6147 }
6148 6148 }
6149 6149 }
6150 6150 }
6151 6151
6152 6152 /*
6153 6153 * Since we are changing page size we first have to flush
6154 6154 * the cache. This makes sure all the pagelock calls have
6155 6155 * to recheck protections.
6156 6156 */
6157 6157 if (svd->softlockcnt > 0) {
6158 6158 ASSERT(svd->tr_state == SEGVN_TR_OFF);
6159 6159
6160 6160 /*
6161 6161 * If this is shared segment non 0 softlockcnt
6162 6162 * means locked pages are still in use.
6163 6163 */
6164 6164 if (svd->type == MAP_SHARED) {
6165 6165 return (EAGAIN);
6166 6166 }
6167 6167
6168 6168 /*
6169 6169 * Since we do have the segvn writers lock nobody can fill
6170 6170 * the cache with entries belonging to this seg during
6171 6171 * the purge. The flush either succeeds or we still have
6172 6172 * pending I/Os.
6173 6173 */
6174 6174 segvn_purge(seg);
6175 6175 if (svd->softlockcnt > 0) {
6176 6176 return (EAGAIN);
6177 6177 }
6178 6178 }
6179 6179
6180 6180 if (HAT_IS_REGION_COOKIE_VALID(svd->rcookie)) {
6181 6181 ASSERT(svd->amp == NULL);
6182 6182 ASSERT(svd->tr_state == SEGVN_TR_OFF);
6183 6183 hat_leave_region(seg->s_as->a_hat, svd->rcookie,
6184 6184 HAT_REGION_TEXT);
6185 6185 svd->rcookie = HAT_INVALID_REGION_COOKIE;
6186 6186 } else if (svd->tr_state == SEGVN_TR_INIT) {
6187 6187 svd->tr_state = SEGVN_TR_OFF;
6188 6188 } else if (svd->tr_state == SEGVN_TR_ON) {
6189 6189 ASSERT(svd->amp != NULL);
6190 6190 segvn_textunrepl(seg, 1);
6191 6191 ASSERT(svd->amp == NULL && svd->tr_state == SEGVN_TR_OFF);
6192 6192 amp = NULL;
6193 6193 }
6194 6194
6195 6195 /*
6196 6196 * Operation for sub range of existing segment.
6197 6197 */
6198 6198 if (addr != seg->s_base || eaddr != (seg->s_base + seg->s_size)) {
6199 6199 if (szc < seg->s_szc) {
6200 6200 VM_STAT_ADD(segvnvmstats.demoterange[2]);
6201 6201 err = segvn_demote_range(seg, addr, len, SDR_RANGE, 0);
6202 6202 if (err == 0) {
6203 6203 return (IE_RETRY);
6204 6204 }
6205 6205 if (err == ENOMEM) {
6206 6206 return (IE_NOMEM);
6207 6207 }
6208 6208 return (err);
6209 6209 }
6210 6210 if (addr != seg->s_base) {
6211 6211 nseg = segvn_split_seg(seg, addr);
6212 6212 if (eaddr != (nseg->s_base + nseg->s_size)) {
6213 6213 /* eaddr is szc aligned */
6214 6214 (void) segvn_split_seg(nseg, eaddr);
6215 6215 }
6216 6216 return (IE_RETRY);
6217 6217 }
6218 6218 if (eaddr != (seg->s_base + seg->s_size)) {
6219 6219 /* eaddr is szc aligned */
6220 6220 (void) segvn_split_seg(seg, eaddr);
6221 6221 }
6222 6222 return (IE_RETRY);
6223 6223 }
6224 6224
6225 6225 /*
6226 6226 * Break any low level sharing and reset seg->s_szc to 0.
6227 6227 */
6228 6228 if ((err = segvn_clrszc(seg)) != 0) {
6229 6229 if (err == ENOMEM) {
6230 6230 err = IE_NOMEM;
6231 6231 }
6232 6232 return (err);
6233 6233 }
6234 6234 ASSERT(seg->s_szc == 0);
6235 6235
6236 6236 /*
6237 6237 * If the end of the current segment is not pgsz aligned
6238 6238 * then attempt to concatenate with the next segment.
6239 6239 */
6240 6240 if (!IS_P2ALIGNED(eaddr, pgsz)) {
6241 6241 nseg = AS_SEGNEXT(seg->s_as, seg);
6242 6242 if (nseg == NULL || nseg == seg || eaddr != nseg->s_base) {
6243 6243 return (ENOMEM);
6244 6244 }
6245 6245 if (nseg->s_ops != &segvn_ops) {
6246 6246 return (EINVAL);
6247 6247 }
6248 6248 nsvd = (struct segvn_data *)nseg->s_data;
6249 6249 if (nsvd->softlockcnt > 0) {
6250 6250 /*
6251 6251 * If this is shared segment non 0 softlockcnt
6252 6252 * means locked pages are still in use.
6253 6253 */
6254 6254 if (nsvd->type == MAP_SHARED) {
6255 6255 return (EAGAIN);
6256 6256 }
6257 6257 segvn_purge(nseg);
6258 6258 if (nsvd->softlockcnt > 0) {
6259 6259 return (EAGAIN);
6260 6260 }
6261 6261 }
6262 6262 err = segvn_clrszc(nseg);
6263 6263 if (err == ENOMEM) {
6264 6264 err = IE_NOMEM;
6265 6265 }
6266 6266 if (err != 0) {
6267 6267 return (err);
6268 6268 }
6269 6269 ASSERT(nsvd->rcookie == HAT_INVALID_REGION_COOKIE);
6270 6270 err = segvn_concat(seg, nseg, 1);
6271 6271 if (err == -1) {
6272 6272 return (EINVAL);
6273 6273 }
6274 6274 if (err == -2) {
6275 6275 return (IE_NOMEM);
6276 6276 }
6277 6277 return (IE_RETRY);
6278 6278 }
6279 6279
6280 6280 /*
6281 6281 * May need to re-align anon array to
6282 6282 * new szc.
6283 6283 */
6284 6284 if (amp != NULL) {
6285 6285 if (!IS_P2ALIGNED(svd->anon_index, pgcnt)) {
6286 6286 struct anon_hdr *nahp;
6287 6287
6288 6288 ASSERT(svd->type == MAP_PRIVATE);
6289 6289
6290 6290 ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
6291 6291 ASSERT(amp->refcnt == 1);
6292 6292 nahp = anon_create(btop(amp->size), ANON_NOSLEEP);
6293 6293 if (nahp == NULL) {
6294 6294 ANON_LOCK_EXIT(&->a_rwlock);
6295 6295 return (IE_NOMEM);
6296 6296 }
6297 6297 if (anon_copy_ptr(amp->ahp, svd->anon_index,
6298 6298 nahp, 0, btop(seg->s_size), ANON_NOSLEEP)) {
6299 6299 anon_release(nahp, btop(amp->size));
6300 6300 ANON_LOCK_EXIT(&->a_rwlock);
6301 6301 return (IE_NOMEM);
6302 6302 }
6303 6303 anon_release(amp->ahp, btop(amp->size));
6304 6304 amp->ahp = nahp;
6305 6305 svd->anon_index = 0;
6306 6306 ANON_LOCK_EXIT(&->a_rwlock);
6307 6307 }
6308 6308 }
6309 6309 if (svd->vp != NULL && szc != 0) {
6310 6310 struct vattr va;
6311 6311 u_offset_t eoffpage = svd->offset;
6312 6312 va.va_mask = AT_SIZE;
6313 6313 eoffpage += seg->s_size;
6314 6314 eoffpage = btopr(eoffpage);
6315 6315 if (VOP_GETATTR(svd->vp, &va, 0, svd->cred, NULL) != 0) {
6316 6316 segvn_setpgsz_getattr_err++;
6317 6317 return (EINVAL);
6318 6318 }
6319 6319 if (btopr(va.va_size) < eoffpage) {
6320 6320 segvn_setpgsz_eof_err++;
6321 6321 return (EINVAL);
6322 6322 }
6323 6323 if (amp != NULL) {
6324 6324 /*
6325 6325 * anon_fill_cow_holes() may call VOP_GETPAGE().
6326 6326 * don't take anon map lock here to avoid holding it
6327 6327 * across VOP_GETPAGE() calls that may call back into
6328 6328 * segvn for klsutering checks. We don't really need
6329 6329 * anon map lock here since it's a private segment and
6330 6330 * we hold as level lock as writers.
6331 6331 */
6332 6332 if ((err = anon_fill_cow_holes(seg, seg->s_base,
6333 6333 amp->ahp, svd->anon_index, svd->vp, svd->offset,
6334 6334 seg->s_size, szc, svd->prot, svd->vpage,
6335 6335 svd->cred)) != 0) {
6336 6336 return (EINVAL);
6337 6337 }
6338 6338 }
6339 6339 segvn_setvnode_mpss(svd->vp);
6340 6340 }
6341 6341
6342 6342 if (amp != NULL) {
6343 6343 ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
6344 6344 if (svd->type == MAP_PRIVATE) {
6345 6345 amp->a_szc = szc;
6346 6346 } else if (szc > amp->a_szc) {
6347 6347 amp->a_szc = szc;
6348 6348 }
6349 6349 ANON_LOCK_EXIT(&->a_rwlock);
6350 6350 }
6351 6351
6352 6352 seg->s_szc = szc;
6353 6353
6354 6354 return (0);
6355 6355 }
6356 6356
6357 6357 static int
6358 6358 segvn_clrszc(struct seg *seg)
6359 6359 {
6360 6360 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
6361 6361 struct anon_map *amp = svd->amp;
6362 6362 size_t pgsz;
6363 6363 pgcnt_t pages;
6364 6364 int err = 0;
6365 6365 caddr_t a = seg->s_base;
6366 6366 caddr_t ea = a + seg->s_size;
6367 6367 ulong_t an_idx = svd->anon_index;
6368 6368 vnode_t *vp = svd->vp;
6369 6369 struct vpage *vpage = svd->vpage;
6370 6370 page_t *anon_pl[1 + 1], *pp;
6371 6371 struct anon *ap, *oldap;
6372 6372 uint_t prot = svd->prot, vpprot;
6373 6373 int pageflag = 0;
6374 6374
6375 6375 ASSERT(AS_WRITE_HELD(seg->s_as) ||
6376 6376 SEGVN_WRITE_HELD(seg->s_as, &svd->lock));
6377 6377 ASSERT(svd->softlockcnt == 0);
6378 6378
6379 6379 if (vp == NULL && amp == NULL) {
6380 6380 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
6381 6381 seg->s_szc = 0;
6382 6382 return (0);
6383 6383 }
6384 6384
6385 6385 if (HAT_IS_REGION_COOKIE_VALID(svd->rcookie)) {
6386 6386 ASSERT(svd->amp == NULL);
6387 6387 ASSERT(svd->tr_state == SEGVN_TR_OFF);
6388 6388 hat_leave_region(seg->s_as->a_hat, svd->rcookie,
6389 6389 HAT_REGION_TEXT);
6390 6390 svd->rcookie = HAT_INVALID_REGION_COOKIE;
6391 6391 } else if (svd->tr_state == SEGVN_TR_ON) {
6392 6392 ASSERT(svd->amp != NULL);
6393 6393 segvn_textunrepl(seg, 1);
6394 6394 ASSERT(svd->amp == NULL && svd->tr_state == SEGVN_TR_OFF);
6395 6395 amp = NULL;
6396 6396 } else {
6397 6397 if (svd->tr_state != SEGVN_TR_OFF) {
6398 6398 ASSERT(svd->tr_state == SEGVN_TR_INIT);
6399 6399 svd->tr_state = SEGVN_TR_OFF;
6400 6400 }
6401 6401
6402 6402 /*
6403 6403 * do HAT_UNLOAD_UNMAP since we are changing the pagesize.
6404 6404 * unload argument is 0 when we are freeing the segment
6405 6405 * and unload was already done.
6406 6406 */
6407 6407 hat_unload(seg->s_as->a_hat, seg->s_base, seg->s_size,
6408 6408 HAT_UNLOAD_UNMAP);
6409 6409 }
6410 6410
6411 6411 if (amp == NULL || svd->type == MAP_SHARED) {
6412 6412 seg->s_szc = 0;
6413 6413 return (0);
6414 6414 }
6415 6415
6416 6416 pgsz = page_get_pagesize(seg->s_szc);
6417 6417 pages = btop(pgsz);
6418 6418
6419 6419 /*
6420 6420 * XXX anon rwlock is not really needed because this is a
6421 6421 * private segment and we are writers.
6422 6422 */
6423 6423 ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
6424 6424
6425 6425 for (; a < ea; a += pgsz, an_idx += pages) {
6426 6426 if ((oldap = anon_get_ptr(amp->ahp, an_idx)) != NULL) {
6427 6427 ASSERT(vpage != NULL || svd->pageprot == 0);
6428 6428 if (vpage != NULL) {
6429 6429 ASSERT(sameprot(seg, a, pgsz));
6430 6430 prot = VPP_PROT(vpage);
6431 6431 pageflag = VPP_ISPPLOCK(vpage) ? LOCK_PAGE : 0;
6432 6432 }
6433 6433 if (seg->s_szc != 0) {
6434 6434 ASSERT(vp == NULL || anon_pages(amp->ahp,
6435 6435 an_idx, pages) == pages);
6436 6436 if ((err = anon_map_demotepages(amp, an_idx,
6437 6437 seg, a, prot, vpage, svd->cred)) != 0) {
6438 6438 goto out;
6439 6439 }
6440 6440 } else {
6441 6441 if (oldap->an_refcnt == 1) {
6442 6442 continue;
6443 6443 }
6444 6444 if ((err = anon_getpage(&oldap, &vpprot,
6445 6445 anon_pl, PAGESIZE, seg, a, S_READ,
6446 6446 svd->cred))) {
6447 6447 goto out;
6448 6448 }
6449 6449 if ((pp = anon_private(&ap, seg, a, prot,
6450 6450 anon_pl[0], pageflag, svd->cred)) == NULL) {
6451 6451 err = ENOMEM;
6452 6452 goto out;
6453 6453 }
6454 6454 anon_decref(oldap);
6455 6455 (void) anon_set_ptr(amp->ahp, an_idx, ap,
6456 6456 ANON_SLEEP);
6457 6457 page_unlock(pp);
6458 6458 }
6459 6459 }
6460 6460 vpage = (vpage == NULL) ? NULL : vpage + pages;
6461 6461 }
6462 6462
6463 6463 amp->a_szc = 0;
6464 6464 seg->s_szc = 0;
6465 6465 out:
6466 6466 ANON_LOCK_EXIT(&->a_rwlock);
6467 6467 return (err);
6468 6468 }
6469 6469
6470 6470 static int
6471 6471 segvn_claim_pages(
6472 6472 struct seg *seg,
6473 6473 struct vpage *svp,
6474 6474 u_offset_t off,
6475 6475 ulong_t anon_idx,
6476 6476 uint_t prot)
6477 6477 {
6478 6478 pgcnt_t pgcnt = page_get_pagecnt(seg->s_szc);
6479 6479 size_t ppasize = (pgcnt + 1) * sizeof (page_t *);
6480 6480 page_t **ppa;
6481 6481 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
6482 6482 struct anon_map *amp = svd->amp;
6483 6483 struct vpage *evp = svp + pgcnt;
6484 6484 caddr_t addr = ((uintptr_t)(svp - svd->vpage) << PAGESHIFT)
6485 6485 + seg->s_base;
6486 6486 struct anon *ap;
6487 6487 struct vnode *vp = svd->vp;
6488 6488 page_t *pp;
6489 6489 pgcnt_t pg_idx, i;
6490 6490 int err = 0;
6491 6491 anoff_t aoff;
6492 6492 int anon = (amp != NULL) ? 1 : 0;
6493 6493
6494 6494 ASSERT(svd->type == MAP_PRIVATE);
6495 6495 ASSERT(svd->vpage != NULL);
6496 6496 ASSERT(seg->s_szc != 0);
6497 6497 ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
6498 6498 ASSERT(amp == NULL || IS_P2ALIGNED(anon_idx, pgcnt));
6499 6499 ASSERT(sameprot(seg, addr, pgcnt << PAGESHIFT));
6500 6500
6501 6501 if (VPP_PROT(svp) == prot)
6502 6502 return (1);
6503 6503 if (!((VPP_PROT(svp) ^ prot) & PROT_WRITE))
6504 6504 return (1);
6505 6505
6506 6506 ppa = kmem_alloc(ppasize, KM_SLEEP);
6507 6507 if (anon && vp != NULL) {
6508 6508 if (anon_get_ptr(amp->ahp, anon_idx) == NULL) {
6509 6509 anon = 0;
6510 6510 ASSERT(!anon_pages(amp->ahp, anon_idx, pgcnt));
6511 6511 }
6512 6512 ASSERT(!anon ||
6513 6513 anon_pages(amp->ahp, anon_idx, pgcnt) == pgcnt);
6514 6514 }
6515 6515
6516 6516 for (*ppa = NULL, pg_idx = 0; svp < evp; svp++, anon_idx++) {
6517 6517 if (!VPP_ISPPLOCK(svp))
6518 6518 continue;
6519 6519 if (anon) {
6520 6520 ap = anon_get_ptr(amp->ahp, anon_idx);
6521 6521 if (ap == NULL) {
6522 6522 panic("segvn_claim_pages: no anon slot");
6523 6523 }
6524 6524 swap_xlate(ap, &vp, &aoff);
6525 6525 off = (u_offset_t)aoff;
6526 6526 }
6527 6527 ASSERT(vp != NULL);
6528 6528 if ((pp = page_lookup(vp,
6529 6529 (u_offset_t)off, SE_SHARED)) == NULL) {
6530 6530 panic("segvn_claim_pages: no page");
6531 6531 }
6532 6532 ppa[pg_idx++] = pp;
6533 6533 off += PAGESIZE;
6534 6534 }
6535 6535
6536 6536 if (ppa[0] == NULL) {
6537 6537 kmem_free(ppa, ppasize);
6538 6538 return (1);
6539 6539 }
6540 6540
6541 6541 ASSERT(pg_idx <= pgcnt);
6542 6542 ppa[pg_idx] = NULL;
6543 6543
6544 6544
6545 6545 /* Find each large page within ppa, and adjust its claim */
6546 6546
6547 6547 /* Does ppa cover a single large page? */
6548 6548 if (ppa[0]->p_szc == seg->s_szc) {
6549 6549 if (prot & PROT_WRITE)
6550 6550 err = page_addclaim_pages(ppa);
6551 6551 else
6552 6552 err = page_subclaim_pages(ppa);
6553 6553 } else {
6554 6554 for (i = 0; ppa[i]; i += pgcnt) {
6555 6555 ASSERT(IS_P2ALIGNED(page_pptonum(ppa[i]), pgcnt));
6556 6556 if (prot & PROT_WRITE)
6557 6557 err = page_addclaim_pages(&ppa[i]);
6558 6558 else
6559 6559 err = page_subclaim_pages(&ppa[i]);
6560 6560 if (err == 0)
6561 6561 break;
6562 6562 }
6563 6563 }
6564 6564
6565 6565 for (i = 0; i < pg_idx; i++) {
6566 6566 ASSERT(ppa[i] != NULL);
6567 6567 page_unlock(ppa[i]);
6568 6568 }
6569 6569
6570 6570 kmem_free(ppa, ppasize);
6571 6571 return (err);
6572 6572 }
6573 6573
6574 6574 /*
6575 6575 * Returns right (upper address) segment if split occurred.
6576 6576 * If the address is equal to the beginning or end of its segment it returns
6577 6577 * the current segment.
6578 6578 */
6579 6579 static struct seg *
6580 6580 segvn_split_seg(struct seg *seg, caddr_t addr)
6581 6581 {
6582 6582 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
6583 6583 struct seg *nseg;
6584 6584 size_t nsize;
6585 6585 struct segvn_data *nsvd;
6586 6586
6587 6587 ASSERT(AS_WRITE_HELD(seg->s_as));
6588 6588 ASSERT(svd->tr_state == SEGVN_TR_OFF);
6589 6589
6590 6590 ASSERT(addr >= seg->s_base);
6591 6591 ASSERT(addr <= seg->s_base + seg->s_size);
6592 6592 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
6593 6593
6594 6594 if (addr == seg->s_base || addr == seg->s_base + seg->s_size)
6595 6595 return (seg);
6596 6596
6597 6597 nsize = seg->s_base + seg->s_size - addr;
6598 6598 seg->s_size = addr - seg->s_base;
6599 6599 nseg = seg_alloc(seg->s_as, addr, nsize);
6600 6600 ASSERT(nseg != NULL);
6601 6601 nseg->s_ops = seg->s_ops;
6602 6602 nsvd = kmem_cache_alloc(segvn_cache, KM_SLEEP);
6603 6603 nseg->s_data = (void *)nsvd;
6604 6604 nseg->s_szc = seg->s_szc;
6605 6605 *nsvd = *svd;
6606 6606 ASSERT(nsvd->rcookie == HAT_INVALID_REGION_COOKIE);
6607 6607 nsvd->seg = nseg;
6608 6608 rw_init(&nsvd->lock, NULL, RW_DEFAULT, NULL);
6609 6609
6610 6610 if (nsvd->vp != NULL) {
6611 6611 VN_HOLD(nsvd->vp);
6612 6612 nsvd->offset = svd->offset +
6613 6613 (uintptr_t)(nseg->s_base - seg->s_base);
6614 6614 if (nsvd->type == MAP_SHARED)
6615 6615 lgrp_shm_policy_init(NULL, nsvd->vp);
6616 6616 } else {
6617 6617 /*
6618 6618 * The offset for an anonymous segment has no signifigance in
6619 6619 * terms of an offset into a file. If we were to use the above
6620 6620 * calculation instead, the structures read out of
6621 6621 * /proc/<pid>/xmap would be more difficult to decipher since
6622 6622 * it would be unclear whether two seemingly contiguous
6623 6623 * prxmap_t structures represented different segments or a
6624 6624 * single segment that had been split up into multiple prxmap_t
6625 6625 * structures (e.g. if some part of the segment had not yet
6626 6626 * been faulted in).
6627 6627 */
6628 6628 nsvd->offset = 0;
6629 6629 }
6630 6630
6631 6631 ASSERT(svd->softlockcnt == 0);
6632 6632 ASSERT(svd->softlockcnt_sbase == 0);
6633 6633 ASSERT(svd->softlockcnt_send == 0);
6634 6634 crhold(svd->cred);
6635 6635
6636 6636 if (svd->vpage != NULL) {
6637 6637 size_t bytes = vpgtob(seg_pages(seg));
6638 6638 size_t nbytes = vpgtob(seg_pages(nseg));
6639 6639 struct vpage *ovpage = svd->vpage;
6640 6640
6641 6641 svd->vpage = kmem_alloc(bytes, KM_SLEEP);
6642 6642 bcopy(ovpage, svd->vpage, bytes);
6643 6643 nsvd->vpage = kmem_alloc(nbytes, KM_SLEEP);
6644 6644 bcopy(ovpage + seg_pages(seg), nsvd->vpage, nbytes);
6645 6645 kmem_free(ovpage, bytes + nbytes);
6646 6646 }
6647 6647 if (svd->amp != NULL && svd->type == MAP_PRIVATE) {
6648 6648 struct anon_map *oamp = svd->amp, *namp;
6649 6649 struct anon_hdr *nahp;
6650 6650
6651 6651 ANON_LOCK_ENTER(&oamp->a_rwlock, RW_WRITER);
6652 6652 ASSERT(oamp->refcnt == 1);
6653 6653 nahp = anon_create(btop(seg->s_size), ANON_SLEEP);
6654 6654 (void) anon_copy_ptr(oamp->ahp, svd->anon_index,
6655 6655 nahp, 0, btop(seg->s_size), ANON_SLEEP);
6656 6656
6657 6657 namp = anonmap_alloc(nseg->s_size, 0, ANON_SLEEP);
6658 6658 namp->a_szc = nseg->s_szc;
6659 6659 (void) anon_copy_ptr(oamp->ahp,
6660 6660 svd->anon_index + btop(seg->s_size),
6661 6661 namp->ahp, 0, btop(nseg->s_size), ANON_SLEEP);
6662 6662 anon_release(oamp->ahp, btop(oamp->size));
6663 6663 oamp->ahp = nahp;
6664 6664 oamp->size = seg->s_size;
6665 6665 svd->anon_index = 0;
6666 6666 nsvd->amp = namp;
6667 6667 nsvd->anon_index = 0;
6668 6668 ANON_LOCK_EXIT(&oamp->a_rwlock);
6669 6669 } else if (svd->amp != NULL) {
6670 6670 pgcnt_t pgcnt = page_get_pagecnt(seg->s_szc);
6671 6671 ASSERT(svd->amp == nsvd->amp);
6672 6672 ASSERT(seg->s_szc <= svd->amp->a_szc);
6673 6673 nsvd->anon_index = svd->anon_index + seg_pages(seg);
6674 6674 ASSERT(IS_P2ALIGNED(nsvd->anon_index, pgcnt));
6675 6675 ANON_LOCK_ENTER(&svd->amp->a_rwlock, RW_WRITER);
6676 6676 svd->amp->refcnt++;
6677 6677 ANON_LOCK_EXIT(&svd->amp->a_rwlock);
6678 6678 }
6679 6679
6680 6680 /*
6681 6681 * Split the amount of swap reserved.
6682 6682 */
6683 6683 if (svd->swresv) {
6684 6684 /*
6685 6685 * For MAP_NORESERVE, only allocate swap reserve for pages
6686 6686 * being used. Other segments get enough to cover whole
6687 6687 * segment.
6688 6688 */
6689 6689 if (svd->flags & MAP_NORESERVE) {
6690 6690 size_t oswresv;
6691 6691
6692 6692 ASSERT(svd->amp);
6693 6693 oswresv = svd->swresv;
6694 6694 svd->swresv = ptob(anon_pages(svd->amp->ahp,
6695 6695 svd->anon_index, btop(seg->s_size)));
6696 6696 nsvd->swresv = ptob(anon_pages(nsvd->amp->ahp,
6697 6697 nsvd->anon_index, btop(nseg->s_size)));
6698 6698 ASSERT(oswresv >= (svd->swresv + nsvd->swresv));
6699 6699 } else {
6700 6700 if (svd->pageswap) {
6701 6701 svd->swresv = segvn_count_swap_by_vpages(seg);
6702 6702 ASSERT(nsvd->swresv >= svd->swresv);
6703 6703 nsvd->swresv -= svd->swresv;
6704 6704 } else {
6705 6705 ASSERT(svd->swresv == seg->s_size +
6706 6706 nseg->s_size);
6707 6707 svd->swresv = seg->s_size;
6708 6708 nsvd->swresv = nseg->s_size;
6709 6709 }
6710 6710 }
6711 6711 }
6712 6712
6713 6713 return (nseg);
6714 6714 }
6715 6715
6716 6716 /*
6717 6717 * called on memory operations (unmap, setprot, setpagesize) for a subset
6718 6718 * of a large page segment to either demote the memory range (SDR_RANGE)
6719 6719 * or the ends (SDR_END) by addr/len.
6720 6720 *
6721 6721 * returns 0 on success. returns errno, including ENOMEM, on failure.
6722 6722 */
6723 6723 static int
6724 6724 segvn_demote_range(
6725 6725 struct seg *seg,
6726 6726 caddr_t addr,
6727 6727 size_t len,
6728 6728 int flag,
6729 6729 uint_t szcvec)
6730 6730 {
6731 6731 caddr_t eaddr = addr + len;
6732 6732 caddr_t lpgaddr, lpgeaddr;
6733 6733 struct seg *nseg;
6734 6734 struct seg *badseg1 = NULL;
6735 6735 struct seg *badseg2 = NULL;
6736 6736 size_t pgsz;
6737 6737 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
6738 6738 int err;
6739 6739 uint_t szc = seg->s_szc;
6740 6740 uint_t tszcvec;
6741 6741
6742 6742 ASSERT(AS_WRITE_HELD(seg->s_as));
6743 6743 ASSERT(svd->tr_state == SEGVN_TR_OFF);
6744 6744 ASSERT(szc != 0);
6745 6745 pgsz = page_get_pagesize(szc);
6746 6746 ASSERT(seg->s_base != addr || seg->s_size != len);
6747 6747 ASSERT(addr >= seg->s_base && eaddr <= seg->s_base + seg->s_size);
6748 6748 ASSERT(svd->softlockcnt == 0);
6749 6749 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
6750 6750 ASSERT(szcvec == 0 || (flag == SDR_END && svd->type == MAP_SHARED));
6751 6751
6752 6752 CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr);
6753 6753 ASSERT(flag == SDR_RANGE || eaddr < lpgeaddr || addr > lpgaddr);
6754 6754 if (flag == SDR_RANGE) {
6755 6755 /* demote entire range */
6756 6756 badseg1 = nseg = segvn_split_seg(seg, lpgaddr);
6757 6757 (void) segvn_split_seg(nseg, lpgeaddr);
6758 6758 ASSERT(badseg1->s_base == lpgaddr);
6759 6759 ASSERT(badseg1->s_size == lpgeaddr - lpgaddr);
6760 6760 } else if (addr != lpgaddr) {
6761 6761 ASSERT(flag == SDR_END);
6762 6762 badseg1 = nseg = segvn_split_seg(seg, lpgaddr);
6763 6763 if (eaddr != lpgeaddr && eaddr > lpgaddr + pgsz &&
6764 6764 eaddr < lpgaddr + 2 * pgsz) {
6765 6765 (void) segvn_split_seg(nseg, lpgeaddr);
6766 6766 ASSERT(badseg1->s_base == lpgaddr);
6767 6767 ASSERT(badseg1->s_size == 2 * pgsz);
6768 6768 } else {
6769 6769 nseg = segvn_split_seg(nseg, lpgaddr + pgsz);
6770 6770 ASSERT(badseg1->s_base == lpgaddr);
6771 6771 ASSERT(badseg1->s_size == pgsz);
6772 6772 if (eaddr != lpgeaddr && eaddr > lpgaddr + pgsz) {
6773 6773 ASSERT(lpgeaddr - lpgaddr > 2 * pgsz);
6774 6774 nseg = segvn_split_seg(nseg, lpgeaddr - pgsz);
6775 6775 badseg2 = nseg;
6776 6776 (void) segvn_split_seg(nseg, lpgeaddr);
6777 6777 ASSERT(badseg2->s_base == lpgeaddr - pgsz);
6778 6778 ASSERT(badseg2->s_size == pgsz);
6779 6779 }
6780 6780 }
6781 6781 } else {
6782 6782 ASSERT(flag == SDR_END);
6783 6783 ASSERT(eaddr < lpgeaddr);
6784 6784 badseg1 = nseg = segvn_split_seg(seg, lpgeaddr - pgsz);
6785 6785 (void) segvn_split_seg(nseg, lpgeaddr);
6786 6786 ASSERT(badseg1->s_base == lpgeaddr - pgsz);
6787 6787 ASSERT(badseg1->s_size == pgsz);
6788 6788 }
6789 6789
6790 6790 ASSERT(badseg1 != NULL);
6791 6791 ASSERT(badseg1->s_szc == szc);
6792 6792 ASSERT(flag == SDR_RANGE || badseg1->s_size == pgsz ||
6793 6793 badseg1->s_size == 2 * pgsz);
6794 6794 ASSERT(sameprot(badseg1, badseg1->s_base, pgsz));
6795 6795 ASSERT(badseg1->s_size == pgsz ||
6796 6796 sameprot(badseg1, badseg1->s_base + pgsz, pgsz));
6797 6797 if (err = segvn_clrszc(badseg1)) {
6798 6798 return (err);
6799 6799 }
6800 6800 ASSERT(badseg1->s_szc == 0);
6801 6801
6802 6802 if (szc > 1 && (tszcvec = P2PHASE(szcvec, 1 << szc)) > 1) {
6803 6803 uint_t tszc = highbit(tszcvec) - 1;
6804 6804 caddr_t ta = MAX(addr, badseg1->s_base);
6805 6805 caddr_t te;
6806 6806 size_t tpgsz = page_get_pagesize(tszc);
6807 6807
6808 6808 ASSERT(svd->type == MAP_SHARED);
6809 6809 ASSERT(flag == SDR_END);
6810 6810 ASSERT(tszc < szc && tszc > 0);
6811 6811
6812 6812 if (eaddr > badseg1->s_base + badseg1->s_size) {
6813 6813 te = badseg1->s_base + badseg1->s_size;
6814 6814 } else {
6815 6815 te = eaddr;
6816 6816 }
6817 6817
6818 6818 ASSERT(ta <= te);
6819 6819 badseg1->s_szc = tszc;
6820 6820 if (!IS_P2ALIGNED(ta, tpgsz) || !IS_P2ALIGNED(te, tpgsz)) {
6821 6821 if (badseg2 != NULL) {
6822 6822 err = segvn_demote_range(badseg1, ta, te - ta,
6823 6823 SDR_END, tszcvec);
6824 6824 if (err != 0) {
6825 6825 return (err);
6826 6826 }
6827 6827 } else {
6828 6828 return (segvn_demote_range(badseg1, ta,
6829 6829 te - ta, SDR_END, tszcvec));
6830 6830 }
6831 6831 }
6832 6832 }
6833 6833
6834 6834 if (badseg2 == NULL)
6835 6835 return (0);
6836 6836 ASSERT(badseg2->s_szc == szc);
6837 6837 ASSERT(badseg2->s_size == pgsz);
6838 6838 ASSERT(sameprot(badseg2, badseg2->s_base, badseg2->s_size));
6839 6839 if (err = segvn_clrszc(badseg2)) {
6840 6840 return (err);
6841 6841 }
6842 6842 ASSERT(badseg2->s_szc == 0);
6843 6843
6844 6844 if (szc > 1 && (tszcvec = P2PHASE(szcvec, 1 << szc)) > 1) {
6845 6845 uint_t tszc = highbit(tszcvec) - 1;
6846 6846 size_t tpgsz = page_get_pagesize(tszc);
6847 6847
6848 6848 ASSERT(svd->type == MAP_SHARED);
6849 6849 ASSERT(flag == SDR_END);
6850 6850 ASSERT(tszc < szc && tszc > 0);
6851 6851 ASSERT(badseg2->s_base > addr);
6852 6852 ASSERT(eaddr > badseg2->s_base);
6853 6853 ASSERT(eaddr < badseg2->s_base + badseg2->s_size);
6854 6854
6855 6855 badseg2->s_szc = tszc;
6856 6856 if (!IS_P2ALIGNED(eaddr, tpgsz)) {
6857 6857 return (segvn_demote_range(badseg2, badseg2->s_base,
6858 6858 eaddr - badseg2->s_base, SDR_END, tszcvec));
6859 6859 }
6860 6860 }
6861 6861
6862 6862 return (0);
6863 6863 }
6864 6864
6865 6865 static int
6866 6866 segvn_checkprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
6867 6867 {
6868 6868 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
6869 6869 struct vpage *vp, *evp;
6870 6870
6871 6871 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
6872 6872
6873 6873 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
6874 6874 /*
6875 6875 * If segment protection can be used, simply check against them.
6876 6876 */
6877 6877 if (svd->pageprot == 0) {
6878 6878 int err;
6879 6879
6880 6880 err = ((svd->prot & prot) != prot) ? EACCES : 0;
6881 6881 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
6882 6882 return (err);
6883 6883 }
6884 6884
6885 6885 /*
6886 6886 * Have to check down to the vpage level.
6887 6887 */
6888 6888 evp = &svd->vpage[seg_page(seg, addr + len)];
6889 6889 for (vp = &svd->vpage[seg_page(seg, addr)]; vp < evp; vp++) {
6890 6890 if ((VPP_PROT(vp) & prot) != prot) {
6891 6891 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
6892 6892 return (EACCES);
6893 6893 }
6894 6894 }
6895 6895 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
6896 6896 return (0);
6897 6897 }
6898 6898
6899 6899 static int
6900 6900 segvn_getprot(struct seg *seg, caddr_t addr, size_t len, uint_t *protv)
6901 6901 {
6902 6902 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
6903 6903 size_t pgno = seg_page(seg, addr + len) - seg_page(seg, addr) + 1;
6904 6904
6905 6905 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
6906 6906
6907 6907 if (pgno != 0) {
6908 6908 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
6909 6909 if (svd->pageprot == 0) {
6910 6910 do {
6911 6911 protv[--pgno] = svd->prot;
6912 6912 } while (pgno != 0);
6913 6913 } else {
6914 6914 size_t pgoff = seg_page(seg, addr);
6915 6915
6916 6916 do {
6917 6917 pgno--;
6918 6918 protv[pgno] = VPP_PROT(&svd->vpage[pgno+pgoff]);
6919 6919 } while (pgno != 0);
6920 6920 }
6921 6921 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
6922 6922 }
6923 6923 return (0);
6924 6924 }
6925 6925
6926 6926 static u_offset_t
6927 6927 segvn_getoffset(struct seg *seg, caddr_t addr)
6928 6928 {
6929 6929 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
6930 6930
6931 6931 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
6932 6932
6933 6933 return (svd->offset + (uintptr_t)(addr - seg->s_base));
6934 6934 }
6935 6935
6936 6936 /*ARGSUSED*/
6937 6937 static int
6938 6938 segvn_gettype(struct seg *seg, caddr_t addr)
6939 6939 {
6940 6940 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
6941 6941
6942 6942 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
6943 6943
6944 6944 return (svd->type | (svd->flags & (MAP_NORESERVE | MAP_TEXT |
6945 6945 MAP_INITDATA)));
6946 6946 }
6947 6947
6948 6948 /*ARGSUSED*/
6949 6949 static int
6950 6950 segvn_getvp(struct seg *seg, caddr_t addr, struct vnode **vpp)
6951 6951 {
6952 6952 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
6953 6953
6954 6954 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
6955 6955
6956 6956 *vpp = svd->vp;
6957 6957 return (0);
6958 6958 }
6959 6959
6960 6960 /*
6961 6961 * Check to see if it makes sense to do kluster/read ahead to
6962 6962 * addr + delta relative to the mapping at addr. We assume here
6963 6963 * that delta is a signed PAGESIZE'd multiple (which can be negative).
6964 6964 *
6965 6965 * For segvn, we currently "approve" of the action if we are
6966 6966 * still in the segment and it maps from the same vp/off,
6967 6967 * or if the advice stored in segvn_data or vpages allows it.
6968 6968 * Currently, klustering is not allowed only if MADV_RANDOM is set.
6969 6969 */
6970 6970 static int
6971 6971 segvn_kluster(struct seg *seg, caddr_t addr, ssize_t delta)
6972 6972 {
6973 6973 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
6974 6974 struct anon *oap, *ap;
6975 6975 ssize_t pd;
6976 6976 size_t page;
6977 6977 struct vnode *vp1, *vp2;
6978 6978 u_offset_t off1, off2;
6979 6979 struct anon_map *amp;
6980 6980
6981 6981 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
6982 6982 ASSERT(AS_WRITE_HELD(seg->s_as) ||
6983 6983 SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
6984 6984
6985 6985 if (addr + delta < seg->s_base ||
6986 6986 addr + delta >= (seg->s_base + seg->s_size))
6987 6987 return (-1); /* exceeded segment bounds */
6988 6988
6989 6989 pd = delta / (ssize_t)PAGESIZE; /* divide to preserve sign bit */
6990 6990 page = seg_page(seg, addr);
6991 6991
6992 6992 /*
6993 6993 * Check to see if either of the pages addr or addr + delta
6994 6994 * have advice set that prevents klustering (if MADV_RANDOM advice
6995 6995 * is set for entire segment, or MADV_SEQUENTIAL is set and delta
6996 6996 * is negative).
6997 6997 */
6998 6998 if (svd->advice == MADV_RANDOM ||
6999 6999 svd->advice == MADV_SEQUENTIAL && delta < 0)
7000 7000 return (-1);
7001 7001 else if (svd->pageadvice && svd->vpage) {
7002 7002 struct vpage *bvpp, *evpp;
7003 7003
7004 7004 bvpp = &svd->vpage[page];
7005 7005 evpp = &svd->vpage[page + pd];
7006 7006 if (VPP_ADVICE(bvpp) == MADV_RANDOM ||
7007 7007 VPP_ADVICE(evpp) == MADV_SEQUENTIAL && delta < 0)
7008 7008 return (-1);
7009 7009 if (VPP_ADVICE(bvpp) != VPP_ADVICE(evpp) &&
7010 7010 VPP_ADVICE(evpp) == MADV_RANDOM)
7011 7011 return (-1);
7012 7012 }
7013 7013
7014 7014 if (svd->type == MAP_SHARED)
7015 7015 return (0); /* shared mapping - all ok */
7016 7016
7017 7017 if ((amp = svd->amp) == NULL)
7018 7018 return (0); /* off original vnode */
7019 7019
7020 7020 page += svd->anon_index;
7021 7021
7022 7022 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
7023 7023
7024 7024 oap = anon_get_ptr(amp->ahp, page);
7025 7025 ap = anon_get_ptr(amp->ahp, page + pd);
7026 7026
7027 7027 ANON_LOCK_EXIT(&->a_rwlock);
7028 7028
7029 7029 if ((oap == NULL && ap != NULL) || (oap != NULL && ap == NULL)) {
7030 7030 return (-1); /* one with and one without an anon */
7031 7031 }
7032 7032
7033 7033 if (oap == NULL) { /* implies that ap == NULL */
7034 7034 return (0); /* off original vnode */
7035 7035 }
7036 7036
7037 7037 /*
7038 7038 * Now we know we have two anon pointers - check to
7039 7039 * see if they happen to be properly allocated.
7040 7040 */
7041 7041
7042 7042 /*
7043 7043 * XXX We cheat here and don't lock the anon slots. We can't because
7044 7044 * we may have been called from the anon layer which might already
7045 7045 * have locked them. We are holding a refcnt on the slots so they
7046 7046 * can't disappear. The worst that will happen is we'll get the wrong
7047 7047 * names (vp, off) for the slots and make a poor klustering decision.
7048 7048 */
7049 7049 swap_xlate(ap, &vp1, &off1);
7050 7050 swap_xlate(oap, &vp2, &off2);
7051 7051
7052 7052
7053 7053 if (!VOP_CMP(vp1, vp2, NULL) || off1 - off2 != delta)
7054 7054 return (-1);
7055 7055 return (0);
7056 7056 }
7057 7057
7058 7058 /*
7059 7059 * Swap the pages of seg out to secondary storage, returning the
7060 7060 * number of bytes of storage freed.
7061 7061 *
7062 7062 * The basic idea is first to unload all translations and then to call
7063 7063 * VOP_PUTPAGE() for all newly-unmapped pages, to push them out to the
7064 7064 * swap device. Pages to which other segments have mappings will remain
7065 7065 * mapped and won't be swapped. Our caller (as_swapout) has already
7066 7066 * performed the unloading step.
7067 7067 *
7068 7068 * The value returned is intended to correlate well with the process's
7069 7069 * memory requirements. However, there are some caveats:
7070 7070 * 1) When given a shared segment as argument, this routine will
7071 7071 * only succeed in swapping out pages for the last sharer of the
7072 7072 * segment. (Previous callers will only have decremented mapping
7073 7073 * reference counts.)
7074 7074 * 2) We assume that the hat layer maintains a large enough translation
7075 7075 * cache to capture process reference patterns.
7076 7076 */
7077 7077 static size_t
7078 7078 segvn_swapout(struct seg *seg)
7079 7079 {
7080 7080 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
7081 7081 struct anon_map *amp;
7082 7082 pgcnt_t pgcnt = 0;
7083 7083 pgcnt_t npages;
7084 7084 pgcnt_t page;
7085 7085 ulong_t anon_index;
7086 7086
7087 7087 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
7088 7088
7089 7089 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
7090 7090 /*
7091 7091 * Find pages unmapped by our caller and force them
7092 7092 * out to the virtual swap device.
7093 7093 */
7094 7094 if ((amp = svd->amp) != NULL)
7095 7095 anon_index = svd->anon_index;
7096 7096 npages = seg->s_size >> PAGESHIFT;
7097 7097 for (page = 0; page < npages; page++) {
7098 7098 page_t *pp;
7099 7099 struct anon *ap;
7100 7100 struct vnode *vp;
7101 7101 u_offset_t off;
7102 7102 anon_sync_obj_t cookie;
7103 7103
7104 7104 /*
7105 7105 * Obtain <vp, off> pair for the page, then look it up.
7106 7106 *
7107 7107 * Note that this code is willing to consider regular
7108 7108 * pages as well as anon pages. Is this appropriate here?
7109 7109 */
7110 7110 ap = NULL;
7111 7111 if (amp != NULL) {
7112 7112 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
7113 7113 if (anon_array_try_enter(amp, anon_index + page,
7114 7114 &cookie)) {
7115 7115 ANON_LOCK_EXIT(&->a_rwlock);
7116 7116 continue;
7117 7117 }
7118 7118 ap = anon_get_ptr(amp->ahp, anon_index + page);
7119 7119 if (ap != NULL) {
7120 7120 swap_xlate(ap, &vp, &off);
7121 7121 } else {
7122 7122 vp = svd->vp;
7123 7123 off = svd->offset + ptob(page);
7124 7124 }
7125 7125 anon_array_exit(&cookie);
7126 7126 ANON_LOCK_EXIT(&->a_rwlock);
7127 7127 } else {
7128 7128 vp = svd->vp;
7129 7129 off = svd->offset + ptob(page);
7130 7130 }
7131 7131 if (vp == NULL) { /* untouched zfod page */
7132 7132 ASSERT(ap == NULL);
7133 7133 continue;
7134 7134 }
7135 7135
7136 7136 pp = page_lookup_nowait(vp, off, SE_SHARED);
7137 7137 if (pp == NULL)
7138 7138 continue;
7139 7139
7140 7140
7141 7141 /*
7142 7142 * Examine the page to see whether it can be tossed out,
7143 7143 * keeping track of how many we've found.
7144 7144 */
7145 7145 if (!page_tryupgrade(pp)) {
7146 7146 /*
7147 7147 * If the page has an i/o lock and no mappings,
7148 7148 * it's very likely that the page is being
7149 7149 * written out as a result of klustering.
7150 7150 * Assume this is so and take credit for it here.
7151 7151 */
7152 7152 if (!page_io_trylock(pp)) {
7153 7153 if (!hat_page_is_mapped(pp))
7154 7154 pgcnt++;
7155 7155 } else {
7156 7156 page_io_unlock(pp);
7157 7157 }
7158 7158 page_unlock(pp);
7159 7159 continue;
7160 7160 }
7161 7161 ASSERT(!page_iolock_assert(pp));
7162 7162
7163 7163
7164 7164 /*
7165 7165 * Skip if page is locked or has mappings.
7166 7166 * We don't need the page_struct_lock to look at lckcnt
7167 7167 * and cowcnt because the page is exclusive locked.
7168 7168 */
7169 7169 if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0 ||
7170 7170 hat_page_is_mapped(pp)) {
7171 7171 page_unlock(pp);
7172 7172 continue;
7173 7173 }
7174 7174
7175 7175 /*
7176 7176 * dispose skips large pages so try to demote first.
7177 7177 */
7178 7178 if (pp->p_szc != 0 && !page_try_demote_pages(pp)) {
7179 7179 page_unlock(pp);
7180 7180 /*
7181 7181 * XXX should skip the remaining page_t's of this
7182 7182 * large page.
7183 7183 */
7184 7184 continue;
7185 7185 }
7186 7186
7187 7187 ASSERT(pp->p_szc == 0);
7188 7188
7189 7189 /*
7190 7190 * No longer mapped -- we can toss it out. How
7191 7191 * we do so depends on whether or not it's dirty.
7192 7192 */
7193 7193 if (hat_ismod(pp) && pp->p_vnode) {
7194 7194 /*
7195 7195 * We must clean the page before it can be
7196 7196 * freed. Setting B_FREE will cause pvn_done
7197 7197 * to free the page when the i/o completes.
7198 7198 * XXX: This also causes it to be accounted
7199 7199 * as a pageout instead of a swap: need
7200 7200 * B_SWAPOUT bit to use instead of B_FREE.
7201 7201 *
7202 7202 * Hold the vnode before releasing the page lock
7203 7203 * to prevent it from being freed and re-used by
7204 7204 * some other thread.
7205 7205 */
7206 7206 VN_HOLD(vp);
7207 7207 page_unlock(pp);
7208 7208
7209 7209 /*
7210 7210 * Queue all i/o requests for the pageout thread
7211 7211 * to avoid saturating the pageout devices.
7212 7212 */
7213 7213 if (!queue_io_request(vp, off))
7214 7214 VN_RELE(vp);
7215 7215 } else {
7216 7216 /*
7217 7217 * The page was clean, free it.
7218 7218 *
7219 7219 * XXX: Can we ever encounter modified pages
7220 7220 * with no associated vnode here?
7221 7221 */
7222 7222 ASSERT(pp->p_vnode != NULL);
7223 7223 /*LINTED: constant in conditional context*/
7224 7224 VN_DISPOSE(pp, B_FREE, 0, kcred);
7225 7225 }
7226 7226
7227 7227 /*
7228 7228 * Credit now even if i/o is in progress.
7229 7229 */
7230 7230 pgcnt++;
7231 7231 }
7232 7232 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7233 7233
7234 7234 /*
7235 7235 * Wakeup pageout to initiate i/o on all queued requests.
7236 7236 */
7237 7237 cv_signal_pageout();
7238 7238 return (ptob(pgcnt));
7239 7239 }
7240 7240
7241 7241 /*
7242 7242 * Synchronize primary storage cache with real object in virtual memory.
7243 7243 *
7244 7244 * XXX - Anonymous pages should not be sync'ed out at all.
7245 7245 */
7246 7246 static int
7247 7247 segvn_sync(struct seg *seg, caddr_t addr, size_t len, int attr, uint_t flags)
7248 7248 {
7249 7249 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
7250 7250 struct vpage *vpp;
7251 7251 page_t *pp;
7252 7252 u_offset_t offset;
7253 7253 struct vnode *vp;
7254 7254 u_offset_t off;
7255 7255 caddr_t eaddr;
7256 7256 int bflags;
7257 7257 int err = 0;
7258 7258 int segtype;
7259 7259 int pageprot;
7260 7260 int prot;
7261 7261 ulong_t anon_index;
7262 7262 struct anon_map *amp;
7263 7263 struct anon *ap;
7264 7264 anon_sync_obj_t cookie;
7265 7265
7266 7266 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
7267 7267
7268 7268 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
7269 7269
7270 7270 if (svd->softlockcnt > 0) {
7271 7271 /*
7272 7272 * If this is shared segment non 0 softlockcnt
7273 7273 * means locked pages are still in use.
7274 7274 */
7275 7275 if (svd->type == MAP_SHARED) {
7276 7276 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7277 7277 return (EAGAIN);
7278 7278 }
7279 7279
7280 7280 /*
7281 7281 * flush all pages from seg cache
7282 7282 * otherwise we may deadlock in swap_putpage
7283 7283 * for B_INVAL page (4175402).
7284 7284 *
7285 7285 * Even if we grab segvn WRITER's lock
7286 7286 * here, there might be another thread which could've
7287 7287 * successfully performed lookup/insert just before
7288 7288 * we acquired the lock here. So, grabbing either
7289 7289 * lock here is of not much use. Until we devise
7290 7290 * a strategy at upper layers to solve the
7291 7291 * synchronization issues completely, we expect
7292 7292 * applications to handle this appropriately.
7293 7293 */
7294 7294 segvn_purge(seg);
7295 7295 if (svd->softlockcnt > 0) {
7296 7296 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7297 7297 return (EAGAIN);
7298 7298 }
7299 7299 } else if (svd->type == MAP_SHARED && svd->amp != NULL &&
7300 7300 svd->amp->a_softlockcnt > 0) {
7301 7301 /*
7302 7302 * Try to purge this amp's entries from pcache. It will
7303 7303 * succeed only if other segments that share the amp have no
7304 7304 * outstanding softlock's.
7305 7305 */
7306 7306 segvn_purge(seg);
7307 7307 if (svd->amp->a_softlockcnt > 0 || svd->softlockcnt > 0) {
7308 7308 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7309 7309 return (EAGAIN);
7310 7310 }
7311 7311 }
7312 7312
7313 7313 vpp = svd->vpage;
7314 7314 offset = svd->offset + (uintptr_t)(addr - seg->s_base);
7315 7315 bflags = ((flags & MS_ASYNC) ? B_ASYNC : 0) |
7316 7316 ((flags & MS_INVALIDATE) ? B_INVAL : 0);
7317 7317
7318 7318 if (attr) {
7319 7319 pageprot = attr & ~(SHARED|PRIVATE);
7320 7320 segtype = (attr & SHARED) ? MAP_SHARED : MAP_PRIVATE;
7321 7321
7322 7322 /*
7323 7323 * We are done if the segment types don't match
7324 7324 * or if we have segment level protections and
7325 7325 * they don't match.
7326 7326 */
7327 7327 if (svd->type != segtype) {
7328 7328 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7329 7329 return (0);
7330 7330 }
7331 7331 if (vpp == NULL) {
7332 7332 if (svd->prot != pageprot) {
7333 7333 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7334 7334 return (0);
7335 7335 }
7336 7336 prot = svd->prot;
7337 7337 } else
7338 7338 vpp = &svd->vpage[seg_page(seg, addr)];
7339 7339
7340 7340 } else if (svd->vp && svd->amp == NULL &&
7341 7341 (flags & MS_INVALIDATE) == 0) {
7342 7342
7343 7343 /*
7344 7344 * No attributes, no anonymous pages and MS_INVALIDATE flag
7345 7345 * is not on, just use one big request.
7346 7346 */
7347 7347 err = VOP_PUTPAGE(svd->vp, (offset_t)offset, len,
7348 7348 bflags, svd->cred, NULL);
7349 7349 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7350 7350 return (err);
7351 7351 }
7352 7352
7353 7353 if ((amp = svd->amp) != NULL)
7354 7354 anon_index = svd->anon_index + seg_page(seg, addr);
7355 7355
7356 7356 for (eaddr = addr + len; addr < eaddr; addr += PAGESIZE) {
7357 7357 ap = NULL;
7358 7358 if (amp != NULL) {
7359 7359 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
7360 7360 anon_array_enter(amp, anon_index, &cookie);
7361 7361 ap = anon_get_ptr(amp->ahp, anon_index++);
7362 7362 if (ap != NULL) {
7363 7363 swap_xlate(ap, &vp, &off);
7364 7364 } else {
7365 7365 vp = svd->vp;
7366 7366 off = offset;
7367 7367 }
7368 7368 anon_array_exit(&cookie);
7369 7369 ANON_LOCK_EXIT(&->a_rwlock);
7370 7370 } else {
7371 7371 vp = svd->vp;
7372 7372 off = offset;
7373 7373 }
7374 7374 offset += PAGESIZE;
7375 7375
7376 7376 if (vp == NULL) /* untouched zfod page */
7377 7377 continue;
7378 7378
7379 7379 if (attr) {
7380 7380 if (vpp) {
7381 7381 prot = VPP_PROT(vpp);
7382 7382 vpp++;
7383 7383 }
7384 7384 if (prot != pageprot) {
7385 7385 continue;
7386 7386 }
7387 7387 }
7388 7388
7389 7389 /*
7390 7390 * See if any of these pages are locked -- if so, then we
7391 7391 * will have to truncate an invalidate request at the first
7392 7392 * locked one. We don't need the page_struct_lock to test
7393 7393 * as this is only advisory; even if we acquire it someone
7394 7394 * might race in and lock the page after we unlock and before
7395 7395 * we do the PUTPAGE, then PUTPAGE simply does nothing.
7396 7396 */
7397 7397 if (flags & MS_INVALIDATE) {
7398 7398 if ((pp = page_lookup(vp, off, SE_SHARED)) != NULL) {
7399 7399 if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
7400 7400 page_unlock(pp);
7401 7401 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7402 7402 return (EBUSY);
7403 7403 }
7404 7404 if (ap != NULL && pp->p_szc != 0 &&
7405 7405 page_tryupgrade(pp)) {
7406 7406 if (pp->p_lckcnt == 0 &&
7407 7407 pp->p_cowcnt == 0) {
7408 7408 /*
7409 7409 * swapfs VN_DISPOSE() won't
7410 7410 * invalidate large pages.
7411 7411 * Attempt to demote.
7412 7412 * XXX can't help it if it
7413 7413 * fails. But for swapfs
7414 7414 * pages it is no big deal.
7415 7415 */
7416 7416 (void) page_try_demote_pages(
7417 7417 pp);
7418 7418 }
7419 7419 }
7420 7420 page_unlock(pp);
7421 7421 }
7422 7422 } else if (svd->type == MAP_SHARED && amp != NULL) {
7423 7423 /*
7424 7424 * Avoid writing out to disk ISM's large pages
7425 7425 * because segspt_free_pages() relies on NULL an_pvp
7426 7426 * of anon slots of such pages.
7427 7427 */
7428 7428
7429 7429 ASSERT(svd->vp == NULL);
7430 7430 /*
7431 7431 * swapfs uses page_lookup_nowait if not freeing or
7432 7432 * invalidating and skips a page if
7433 7433 * page_lookup_nowait returns NULL.
7434 7434 */
7435 7435 pp = page_lookup_nowait(vp, off, SE_SHARED);
7436 7436 if (pp == NULL) {
7437 7437 continue;
7438 7438 }
7439 7439 if (pp->p_szc != 0) {
7440 7440 page_unlock(pp);
7441 7441 continue;
7442 7442 }
7443 7443
7444 7444 /*
7445 7445 * Note ISM pages are created large so (vp, off)'s
7446 7446 * page cannot suddenly become large after we unlock
7447 7447 * pp.
7448 7448 */
7449 7449 page_unlock(pp);
7450 7450 }
7451 7451 /*
7452 7452 * XXX - Should ultimately try to kluster
7453 7453 * calls to VOP_PUTPAGE() for performance.
7454 7454 */
7455 7455 VN_HOLD(vp);
7456 7456 err = VOP_PUTPAGE(vp, (offset_t)off, PAGESIZE,
7457 7457 (bflags | (IS_SWAPFSVP(vp) ? B_PAGE_NOWAIT : 0)),
7458 7458 svd->cred, NULL);
7459 7459
7460 7460 VN_RELE(vp);
7461 7461 if (err)
7462 7462 break;
7463 7463 }
7464 7464 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7465 7465 return (err);
7466 7466 }
7467 7467
7468 7468 /*
7469 7469 * Determine if we have data corresponding to pages in the
7470 7470 * primary storage virtual memory cache (i.e., "in core").
7471 7471 */
7472 7472 static size_t
7473 7473 segvn_incore(struct seg *seg, caddr_t addr, size_t len, char *vec)
7474 7474 {
7475 7475 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
7476 7476 struct vnode *vp, *avp;
7477 7477 u_offset_t offset, aoffset;
7478 7478 size_t p, ep;
7479 7479 int ret;
7480 7480 struct vpage *vpp;
7481 7481 page_t *pp;
7482 7482 uint_t start;
7483 7483 struct anon_map *amp; /* XXX - for locknest */
7484 7484 struct anon *ap;
7485 7485 uint_t attr;
7486 7486 anon_sync_obj_t cookie;
7487 7487
7488 7488 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
7489 7489
7490 7490 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
7491 7491 if (svd->amp == NULL && svd->vp == NULL) {
7492 7492 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7493 7493 bzero(vec, btopr(len));
7494 7494 return (len); /* no anonymous pages created yet */
7495 7495 }
7496 7496
7497 7497 p = seg_page(seg, addr);
7498 7498 ep = seg_page(seg, addr + len);
7499 7499 start = svd->vp ? SEG_PAGE_VNODEBACKED : 0;
7500 7500
7501 7501 amp = svd->amp;
7502 7502 for (; p < ep; p++, addr += PAGESIZE) {
7503 7503 vpp = (svd->vpage) ? &svd->vpage[p]: NULL;
7504 7504 ret = start;
7505 7505 ap = NULL;
7506 7506 avp = NULL;
7507 7507 /* Grab the vnode/offset for the anon slot */
7508 7508 if (amp != NULL) {
7509 7509 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
7510 7510 anon_array_enter(amp, svd->anon_index + p, &cookie);
7511 7511 ap = anon_get_ptr(amp->ahp, svd->anon_index + p);
7512 7512 if (ap != NULL) {
7513 7513 swap_xlate(ap, &avp, &aoffset);
7514 7514 }
7515 7515 anon_array_exit(&cookie);
7516 7516 ANON_LOCK_EXIT(&->a_rwlock);
7517 7517 }
7518 7518 if ((avp != NULL) && page_exists(avp, aoffset)) {
7519 7519 /* A page exists for the anon slot */
7520 7520 ret |= SEG_PAGE_INCORE;
7521 7521
7522 7522 /*
7523 7523 * If page is mapped and writable
7524 7524 */
7525 7525 attr = (uint_t)0;
7526 7526 if ((hat_getattr(seg->s_as->a_hat, addr,
7527 7527 &attr) != -1) && (attr & PROT_WRITE)) {
7528 7528 ret |= SEG_PAGE_ANON;
7529 7529 }
7530 7530 /*
7531 7531 * Don't get page_struct lock for lckcnt and cowcnt,
7532 7532 * since this is purely advisory.
7533 7533 */
7534 7534 if ((pp = page_lookup_nowait(avp, aoffset,
7535 7535 SE_SHARED)) != NULL) {
7536 7536 if (pp->p_lckcnt)
7537 7537 ret |= SEG_PAGE_SOFTLOCK;
7538 7538 if (pp->p_cowcnt)
7539 7539 ret |= SEG_PAGE_HASCOW;
7540 7540 page_unlock(pp);
7541 7541 }
7542 7542 }
7543 7543
7544 7544 /* Gather vnode statistics */
7545 7545 vp = svd->vp;
7546 7546 offset = svd->offset + (uintptr_t)(addr - seg->s_base);
7547 7547
7548 7548 if (vp != NULL) {
7549 7549 /*
7550 7550 * Try to obtain a "shared" lock on the page
7551 7551 * without blocking. If this fails, determine
7552 7552 * if the page is in memory.
7553 7553 */
7554 7554 pp = page_lookup_nowait(vp, offset, SE_SHARED);
7555 7555 if ((pp == NULL) && (page_exists(vp, offset))) {
7556 7556 /* Page is incore, and is named */
7557 7557 ret |= (SEG_PAGE_INCORE | SEG_PAGE_VNODE);
7558 7558 }
7559 7559 /*
7560 7560 * Don't get page_struct lock for lckcnt and cowcnt,
7561 7561 * since this is purely advisory.
7562 7562 */
7563 7563 if (pp != NULL) {
7564 7564 ret |= (SEG_PAGE_INCORE | SEG_PAGE_VNODE);
7565 7565 if (pp->p_lckcnt)
7566 7566 ret |= SEG_PAGE_SOFTLOCK;
7567 7567 if (pp->p_cowcnt)
7568 7568 ret |= SEG_PAGE_HASCOW;
7569 7569 page_unlock(pp);
7570 7570 }
7571 7571 }
7572 7572
7573 7573 /* Gather virtual page information */
7574 7574 if (vpp) {
7575 7575 if (VPP_ISPPLOCK(vpp))
7576 7576 ret |= SEG_PAGE_LOCKED;
7577 7577 vpp++;
7578 7578 }
7579 7579
7580 7580 *vec++ = (char)ret;
7581 7581 }
7582 7582 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7583 7583 return (len);
7584 7584 }
7585 7585
7586 7586 /*
7587 7587 * Statement for p_cowcnts/p_lckcnts.
7588 7588 *
7589 7589 * p_cowcnt is updated while mlock/munlocking MAP_PRIVATE and PROT_WRITE region
7590 7590 * irrespective of the following factors or anything else:
7591 7591 *
7592 7592 * (1) anon slots are populated or not
7593 7593 * (2) cow is broken or not
7594 7594 * (3) refcnt on ap is 1 or greater than 1
7595 7595 *
7596 7596 * If it's not MAP_PRIVATE and PROT_WRITE, p_lckcnt is updated during mlock
7597 7597 * and munlock.
7598 7598 *
7599 7599 *
7600 7600 * Handling p_cowcnts/p_lckcnts during copy-on-write fault:
7601 7601 *
7602 7602 * if vpage has PROT_WRITE
7603 7603 * transfer cowcnt on the oldpage -> cowcnt on the newpage
7604 7604 * else
7605 7605 * transfer lckcnt on the oldpage -> lckcnt on the newpage
7606 7606 *
7607 7607 * During copy-on-write, decrement p_cowcnt on the oldpage and increment
7608 7608 * p_cowcnt on the newpage *if* the corresponding vpage has PROT_WRITE.
7609 7609 *
7610 7610 * We may also break COW if softlocking on read access in the physio case.
7611 7611 * In this case, vpage may not have PROT_WRITE. So, we need to decrement
7612 7612 * p_lckcnt on the oldpage and increment p_lckcnt on the newpage *if* the
7613 7613 * vpage doesn't have PROT_WRITE.
7614 7614 *
7615 7615 *
7616 7616 * Handling p_cowcnts/p_lckcnts during mprotect on mlocked region:
7617 7617 *
7618 7618 * If a MAP_PRIVATE region loses PROT_WRITE, we decrement p_cowcnt and
7619 7619 * increment p_lckcnt by calling page_subclaim() which takes care of
7620 7620 * availrmem accounting and p_lckcnt overflow.
7621 7621 *
7622 7622 * If a MAP_PRIVATE region gains PROT_WRITE, we decrement p_lckcnt and
7623 7623 * increment p_cowcnt by calling page_addclaim() which takes care of
7624 7624 * availrmem availability and p_cowcnt overflow.
7625 7625 */
7626 7626
7627 7627 /*
7628 7628 * Lock down (or unlock) pages mapped by this segment.
7629 7629 *
7630 7630 * XXX only creates PAGESIZE pages if anon slots are not initialized.
7631 7631 * At fault time they will be relocated into larger pages.
7632 7632 */
7633 7633 static int
7634 7634 segvn_lockop(struct seg *seg, caddr_t addr, size_t len,
7635 7635 int attr, int op, ulong_t *lockmap, size_t pos)
7636 7636 {
7637 7637 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
7638 7638 struct vpage *vpp;
7639 7639 struct vpage *evp;
7640 7640 page_t *pp;
7641 7641 u_offset_t offset;
7642 7642 u_offset_t off;
7643 7643 int segtype;
7644 7644 int pageprot;
7645 7645 int claim;
7646 7646 struct vnode *vp;
7647 7647 ulong_t anon_index;
7648 7648 struct anon_map *amp;
7649 7649 struct anon *ap;
7650 7650 struct vattr va;
7651 7651 anon_sync_obj_t cookie;
7652 7652 struct kshmid *sp = NULL;
7653 7653 struct proc *p = curproc;
7654 7654 kproject_t *proj = NULL;
7655 7655 int chargeproc = 1;
7656 7656 size_t locked_bytes = 0;
7657 7657 size_t unlocked_bytes = 0;
7658 7658 int err = 0;
7659 7659
7660 7660 /*
7661 7661 * Hold write lock on address space because may split or concatenate
7662 7662 * segments
7663 7663 */
7664 7664 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
7665 7665
7666 7666 /*
7667 7667 * If this is a shm, use shm's project and zone, else use
7668 7668 * project and zone of calling process
7669 7669 */
7670 7670
7671 7671 /* Determine if this segment backs a sysV shm */
7672 7672 if (svd->amp != NULL && svd->amp->a_sp != NULL) {
7673 7673 ASSERT(svd->type == MAP_SHARED);
7674 7674 ASSERT(svd->tr_state == SEGVN_TR_OFF);
7675 7675 sp = svd->amp->a_sp;
7676 7676 proj = sp->shm_perm.ipc_proj;
7677 7677 chargeproc = 0;
7678 7678 }
7679 7679
7680 7680 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
7681 7681 if (attr) {
7682 7682 pageprot = attr & ~(SHARED|PRIVATE);
7683 7683 segtype = attr & SHARED ? MAP_SHARED : MAP_PRIVATE;
7684 7684
7685 7685 /*
7686 7686 * We are done if the segment types don't match
7687 7687 * or if we have segment level protections and
7688 7688 * they don't match.
7689 7689 */
7690 7690 if (svd->type != segtype) {
7691 7691 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7692 7692 return (0);
7693 7693 }
7694 7694 if (svd->pageprot == 0 && svd->prot != pageprot) {
7695 7695 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7696 7696 return (0);
7697 7697 }
7698 7698 }
7699 7699
7700 7700 if (op == MC_LOCK) {
7701 7701 if (svd->tr_state == SEGVN_TR_INIT) {
7702 7702 svd->tr_state = SEGVN_TR_OFF;
7703 7703 } else if (svd->tr_state == SEGVN_TR_ON) {
7704 7704 ASSERT(svd->amp != NULL);
7705 7705 segvn_textunrepl(seg, 0);
7706 7706 ASSERT(svd->amp == NULL &&
7707 7707 svd->tr_state == SEGVN_TR_OFF);
7708 7708 }
7709 7709 }
7710 7710
7711 7711 /*
7712 7712 * If we're locking, then we must create a vpage structure if
7713 7713 * none exists. If we're unlocking, then check to see if there
7714 7714 * is a vpage -- if not, then we could not have locked anything.
7715 7715 */
7716 7716
7717 7717 if ((vpp = svd->vpage) == NULL) {
7718 7718 if (op == MC_LOCK) {
7719 7719 segvn_vpage(seg);
7720 7720 if (svd->vpage == NULL) {
7721 7721 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7722 7722 return (ENOMEM);
7723 7723 }
7724 7724 } else {
7725 7725 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7726 7726 return (0);
7727 7727 }
7728 7728 }
7729 7729
7730 7730 /*
7731 7731 * The anonymous data vector (i.e., previously
7732 7732 * unreferenced mapping to swap space) can be allocated
7733 7733 * by lazily testing for its existence.
7734 7734 */
7735 7735 if (op == MC_LOCK && svd->amp == NULL && svd->vp == NULL) {
7736 7736 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
7737 7737 svd->amp = anonmap_alloc(seg->s_size, 0, ANON_SLEEP);
7738 7738 svd->amp->a_szc = seg->s_szc;
7739 7739 }
7740 7740
7741 7741 if ((amp = svd->amp) != NULL) {
7742 7742 anon_index = svd->anon_index + seg_page(seg, addr);
7743 7743 }
7744 7744
7745 7745 offset = svd->offset + (uintptr_t)(addr - seg->s_base);
7746 7746 evp = &svd->vpage[seg_page(seg, addr + len)];
7747 7747
7748 7748 if (sp != NULL)
7749 7749 mutex_enter(&sp->shm_mlock);
7750 7750
7751 7751 /* determine number of unlocked bytes in range for lock operation */
7752 7752 if (op == MC_LOCK) {
7753 7753
7754 7754 if (sp == NULL) {
7755 7755 for (vpp = &svd->vpage[seg_page(seg, addr)]; vpp < evp;
7756 7756 vpp++) {
7757 7757 if (!VPP_ISPPLOCK(vpp))
7758 7758 unlocked_bytes += PAGESIZE;
7759 7759 }
7760 7760 } else {
7761 7761 ulong_t i_idx, i_edx;
7762 7762 anon_sync_obj_t i_cookie;
7763 7763 struct anon *i_ap;
7764 7764 struct vnode *i_vp;
7765 7765 u_offset_t i_off;
7766 7766
7767 7767 /* Only count sysV pages once for locked memory */
7768 7768 i_edx = svd->anon_index + seg_page(seg, addr + len);
7769 7769 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
7770 7770 for (i_idx = anon_index; i_idx < i_edx; i_idx++) {
7771 7771 anon_array_enter(amp, i_idx, &i_cookie);
7772 7772 i_ap = anon_get_ptr(amp->ahp, i_idx);
7773 7773 if (i_ap == NULL) {
7774 7774 unlocked_bytes += PAGESIZE;
7775 7775 anon_array_exit(&i_cookie);
7776 7776 continue;
7777 7777 }
7778 7778 swap_xlate(i_ap, &i_vp, &i_off);
7779 7779 anon_array_exit(&i_cookie);
7780 7780 pp = page_lookup(i_vp, i_off, SE_SHARED);
7781 7781 if (pp == NULL) {
7782 7782 unlocked_bytes += PAGESIZE;
7783 7783 continue;
7784 7784 } else if (pp->p_lckcnt == 0)
7785 7785 unlocked_bytes += PAGESIZE;
7786 7786 page_unlock(pp);
7787 7787 }
7788 7788 ANON_LOCK_EXIT(&->a_rwlock);
7789 7789 }
7790 7790
7791 7791 mutex_enter(&p->p_lock);
7792 7792 err = rctl_incr_locked_mem(p, proj, unlocked_bytes,
7793 7793 chargeproc);
7794 7794 mutex_exit(&p->p_lock);
7795 7795
7796 7796 if (err) {
7797 7797 if (sp != NULL)
7798 7798 mutex_exit(&sp->shm_mlock);
7799 7799 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
7800 7800 return (err);
7801 7801 }
7802 7802 }
7803 7803 /*
7804 7804 * Loop over all pages in the range. Process if we're locking and
7805 7805 * page has not already been locked in this mapping; or if we're
7806 7806 * unlocking and the page has been locked.
7807 7807 */
7808 7808 for (vpp = &svd->vpage[seg_page(seg, addr)]; vpp < evp;
7809 7809 vpp++, pos++, addr += PAGESIZE, offset += PAGESIZE, anon_index++) {
7810 7810 if ((attr == 0 || VPP_PROT(vpp) == pageprot) &&
7811 7811 ((op == MC_LOCK && !VPP_ISPPLOCK(vpp)) ||
7812 7812 (op == MC_UNLOCK && VPP_ISPPLOCK(vpp)))) {
7813 7813
7814 7814 if (amp != NULL)
7815 7815 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
7816 7816 /*
7817 7817 * If this isn't a MAP_NORESERVE segment and
7818 7818 * we're locking, allocate anon slots if they
7819 7819 * don't exist. The page is brought in later on.
7820 7820 */
7821 7821 if (op == MC_LOCK && svd->vp == NULL &&
7822 7822 ((svd->flags & MAP_NORESERVE) == 0) &&
7823 7823 amp != NULL &&
7824 7824 ((ap = anon_get_ptr(amp->ahp, anon_index))
7825 7825 == NULL)) {
7826 7826 anon_array_enter(amp, anon_index, &cookie);
7827 7827
7828 7828 if ((ap = anon_get_ptr(amp->ahp,
7829 7829 anon_index)) == NULL) {
7830 7830 pp = anon_zero(seg, addr, &ap,
7831 7831 svd->cred);
7832 7832 if (pp == NULL) {
7833 7833 anon_array_exit(&cookie);
7834 7834 ANON_LOCK_EXIT(&->a_rwlock);
7835 7835 err = ENOMEM;
7836 7836 goto out;
7837 7837 }
7838 7838 ASSERT(anon_get_ptr(amp->ahp,
7839 7839 anon_index) == NULL);
7840 7840 (void) anon_set_ptr(amp->ahp,
7841 7841 anon_index, ap, ANON_SLEEP);
7842 7842 page_unlock(pp);
7843 7843 }
7844 7844 anon_array_exit(&cookie);
7845 7845 }
7846 7846
7847 7847 /*
7848 7848 * Get name for page, accounting for
7849 7849 * existence of private copy.
7850 7850 */
7851 7851 ap = NULL;
7852 7852 if (amp != NULL) {
7853 7853 anon_array_enter(amp, anon_index, &cookie);
7854 7854 ap = anon_get_ptr(amp->ahp, anon_index);
7855 7855 if (ap != NULL) {
7856 7856 swap_xlate(ap, &vp, &off);
7857 7857 } else {
7858 7858 if (svd->vp == NULL &&
7859 7859 (svd->flags & MAP_NORESERVE)) {
7860 7860 anon_array_exit(&cookie);
7861 7861 ANON_LOCK_EXIT(&->a_rwlock);
7862 7862 continue;
7863 7863 }
7864 7864 vp = svd->vp;
7865 7865 off = offset;
7866 7866 }
7867 7867 if (op != MC_LOCK || ap == NULL) {
7868 7868 anon_array_exit(&cookie);
7869 7869 ANON_LOCK_EXIT(&->a_rwlock);
7870 7870 }
7871 7871 } else {
7872 7872 vp = svd->vp;
7873 7873 off = offset;
7874 7874 }
7875 7875
7876 7876 /*
7877 7877 * Get page frame. It's ok if the page is
7878 7878 * not available when we're unlocking, as this
7879 7879 * may simply mean that a page we locked got
7880 7880 * truncated out of existence after we locked it.
7881 7881 *
7882 7882 * Invoke VOP_GETPAGE() to obtain the page struct
7883 7883 * since we may need to read it from disk if its
7884 7884 * been paged out.
7885 7885 */
7886 7886 if (op != MC_LOCK)
7887 7887 pp = page_lookup(vp, off, SE_SHARED);
7888 7888 else {
7889 7889 page_t *pl[1 + 1];
7890 7890 int error;
7891 7891
7892 7892 ASSERT(vp != NULL);
7893 7893
7894 7894 error = VOP_GETPAGE(vp, (offset_t)off, PAGESIZE,
7895 7895 (uint_t *)NULL, pl, PAGESIZE, seg, addr,
7896 7896 S_OTHER, svd->cred, NULL);
7897 7897
7898 7898 if (error && ap != NULL) {
7899 7899 anon_array_exit(&cookie);
7900 7900 ANON_LOCK_EXIT(&->a_rwlock);
7901 7901 }
7902 7902
7903 7903 /*
7904 7904 * If the error is EDEADLK then we must bounce
7905 7905 * up and drop all vm subsystem locks and then
7906 7906 * retry the operation later
7907 7907 * This behavior is a temporary measure because
7908 7908 * ufs/sds logging is badly designed and will
7909 7909 * deadlock if we don't allow this bounce to
7910 7910 * happen. The real solution is to re-design
7911 7911 * the logging code to work properly. See bug
7912 7912 * 4125102 for details of the problem.
7913 7913 */
7914 7914 if (error == EDEADLK) {
7915 7915 err = error;
7916 7916 goto out;
7917 7917 }
7918 7918 /*
7919 7919 * Quit if we fail to fault in the page. Treat
7920 7920 * the failure as an error, unless the addr
7921 7921 * is mapped beyond the end of a file.
7922 7922 */
7923 7923 if (error && svd->vp) {
7924 7924 va.va_mask = AT_SIZE;
7925 7925 if (VOP_GETATTR(svd->vp, &va, 0,
7926 7926 svd->cred, NULL) != 0) {
7927 7927 err = EIO;
7928 7928 goto out;
7929 7929 }
7930 7930 if (btopr(va.va_size) >=
7931 7931 btopr(off + 1)) {
7932 7932 err = EIO;
7933 7933 goto out;
7934 7934 }
7935 7935 goto out;
7936 7936
7937 7937 } else if (error) {
7938 7938 err = EIO;
7939 7939 goto out;
7940 7940 }
7941 7941 pp = pl[0];
7942 7942 ASSERT(pp != NULL);
7943 7943 }
7944 7944
7945 7945 /*
7946 7946 * See Statement at the beginning of this routine.
7947 7947 *
7948 7948 * claim is always set if MAP_PRIVATE and PROT_WRITE
7949 7949 * irrespective of following factors:
7950 7950 *
7951 7951 * (1) anon slots are populated or not
7952 7952 * (2) cow is broken or not
7953 7953 * (3) refcnt on ap is 1 or greater than 1
7954 7954 *
7955 7955 * See 4140683 for details
7956 7956 */
7957 7957 claim = ((VPP_PROT(vpp) & PROT_WRITE) &&
7958 7958 (svd->type == MAP_PRIVATE));
7959 7959
7960 7960 /*
7961 7961 * Perform page-level operation appropriate to
7962 7962 * operation. If locking, undo the SOFTLOCK
7963 7963 * performed to bring the page into memory
7964 7964 * after setting the lock. If unlocking,
7965 7965 * and no page was found, account for the claim
7966 7966 * separately.
7967 7967 */
7968 7968 if (op == MC_LOCK) {
7969 7969 int ret = 1; /* Assume success */
7970 7970
7971 7971 ASSERT(!VPP_ISPPLOCK(vpp));
7972 7972
7973 7973 ret = page_pp_lock(pp, claim, 0);
7974 7974 if (ap != NULL) {
7975 7975 if (ap->an_pvp != NULL) {
7976 7976 anon_swap_free(ap, pp);
7977 7977 }
7978 7978 anon_array_exit(&cookie);
7979 7979 ANON_LOCK_EXIT(&->a_rwlock);
7980 7980 }
7981 7981 if (ret == 0) {
7982 7982 /* locking page failed */
7983 7983 page_unlock(pp);
7984 7984 err = EAGAIN;
7985 7985 goto out;
7986 7986 }
7987 7987 VPP_SETPPLOCK(vpp);
7988 7988 if (sp != NULL) {
7989 7989 if (pp->p_lckcnt == 1)
7990 7990 locked_bytes += PAGESIZE;
7991 7991 } else
7992 7992 locked_bytes += PAGESIZE;
7993 7993
7994 7994 if (lockmap != (ulong_t *)NULL)
7995 7995 BT_SET(lockmap, pos);
7996 7996
7997 7997 page_unlock(pp);
7998 7998 } else {
7999 7999 ASSERT(VPP_ISPPLOCK(vpp));
8000 8000 if (pp != NULL) {
8001 8001 /* sysV pages should be locked */
8002 8002 ASSERT(sp == NULL || pp->p_lckcnt > 0);
8003 8003 page_pp_unlock(pp, claim, 0);
8004 8004 if (sp != NULL) {
8005 8005 if (pp->p_lckcnt == 0)
8006 8006 unlocked_bytes
8007 8007 += PAGESIZE;
8008 8008 } else
8009 8009 unlocked_bytes += PAGESIZE;
8010 8010 page_unlock(pp);
8011 8011 } else {
8012 8012 ASSERT(sp == NULL);
8013 8013 unlocked_bytes += PAGESIZE;
8014 8014 }
8015 8015 VPP_CLRPPLOCK(vpp);
8016 8016 }
8017 8017 }
8018 8018 }
8019 8019 out:
8020 8020 if (op == MC_LOCK) {
8021 8021 /* Credit back bytes that did not get locked */
8022 8022 if ((unlocked_bytes - locked_bytes) > 0) {
8023 8023 if (proj == NULL)
8024 8024 mutex_enter(&p->p_lock);
8025 8025 rctl_decr_locked_mem(p, proj,
8026 8026 (unlocked_bytes - locked_bytes), chargeproc);
8027 8027 if (proj == NULL)
8028 8028 mutex_exit(&p->p_lock);
8029 8029 }
8030 8030
8031 8031 } else {
8032 8032 /* Account bytes that were unlocked */
8033 8033 if (unlocked_bytes > 0) {
8034 8034 if (proj == NULL)
8035 8035 mutex_enter(&p->p_lock);
8036 8036 rctl_decr_locked_mem(p, proj, unlocked_bytes,
8037 8037 chargeproc);
8038 8038 if (proj == NULL)
8039 8039 mutex_exit(&p->p_lock);
8040 8040 }
8041 8041 }
8042 8042 if (sp != NULL)
8043 8043 mutex_exit(&sp->shm_mlock);
8044 8044 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8045 8045
8046 8046 return (err);
8047 8047 }
8048 8048
8049 8049 /*
8050 8050 * Set advice from user for specified pages
8051 8051 * There are 10 types of advice:
8052 8052 * MADV_NORMAL - Normal (default) behavior (whatever that is)
8053 8053 * MADV_RANDOM - Random page references
8054 8054 * do not allow readahead or 'klustering'
8055 8055 * MADV_SEQUENTIAL - Sequential page references
8056 8056 * Pages previous to the one currently being
8057 8057 * accessed (determined by fault) are 'not needed'
8058 8058 * and are freed immediately
8059 8059 * MADV_WILLNEED - Pages are likely to be used (fault ahead in mctl)
8060 8060 * MADV_DONTNEED - Pages are not needed (synced out in mctl)
8061 8061 * MADV_FREE - Contents can be discarded
8062 8062 * MADV_ACCESS_DEFAULT- Default access
8063 8063 * MADV_ACCESS_LWP - Next LWP will access heavily
8064 8064 * MADV_ACCESS_MANY- Many LWPs or processes will access heavily
8065 8065 * MADV_PURGE - Contents will be immediately discarded
8066 8066 */
8067 8067 static int
8068 8068 segvn_advise(struct seg *seg, caddr_t addr, size_t len, uint_t behav)
8069 8069 {
8070 8070 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
8071 8071 size_t page;
8072 8072 int err = 0;
8073 8073 int already_set;
8074 8074 struct anon_map *amp;
8075 8075 ulong_t anon_index;
8076 8076 struct seg *next;
8077 8077 lgrp_mem_policy_t policy;
8078 8078 struct seg *prev;
8079 8079 struct vnode *vp;
8080 8080
8081 8081 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
8082 8082
8083 8083 /*
8084 8084 * In case of MADV_FREE/MADV_PURGE, we won't be modifying any segment
8085 8085 * private data structures; so, we only need to grab READER's lock
8086 8086 */
8087 8087 if (behav != MADV_FREE && behav != MADV_PURGE) {
8088 8088 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
8089 8089 if (svd->tr_state != SEGVN_TR_OFF) {
8090 8090 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8091 8091 return (0);
8092 8092 }
8093 8093 } else {
8094 8094 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
8095 8095 }
8096 8096
8097 8097 /*
8098 8098 * Large pages are assumed to be only turned on when accesses to the
8099 8099 * segment's address range have spatial and temporal locality. That
8100 8100 * justifies ignoring MADV_SEQUENTIAL for large page segments.
8101 8101 * Also, ignore advice affecting lgroup memory allocation
8102 8102 * if don't need to do lgroup optimizations on this system
8103 8103 */
8104 8104
8105 8105 if ((behav == MADV_SEQUENTIAL &&
8106 8106 (seg->s_szc != 0 || HAT_IS_REGION_COOKIE_VALID(svd->rcookie))) ||
8107 8107 (!lgrp_optimizations() && (behav == MADV_ACCESS_DEFAULT ||
8108 8108 behav == MADV_ACCESS_LWP || behav == MADV_ACCESS_MANY))) {
8109 8109 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8110 8110 return (0);
8111 8111 }
8112 8112
8113 8113 if (behav == MADV_SEQUENTIAL || behav == MADV_ACCESS_DEFAULT ||
8114 8114 behav == MADV_ACCESS_LWP || behav == MADV_ACCESS_MANY) {
8115 8115 /*
8116 8116 * Since we are going to unload hat mappings
8117 8117 * we first have to flush the cache. Otherwise
8118 8118 * this might lead to system panic if another
8119 8119 * thread is doing physio on the range whose
8120 8120 * mappings are unloaded by madvise(3C).
8121 8121 */
8122 8122 if (svd->softlockcnt > 0) {
8123 8123 /*
8124 8124 * If this is shared segment non 0 softlockcnt
8125 8125 * means locked pages are still in use.
8126 8126 */
8127 8127 if (svd->type == MAP_SHARED) {
8128 8128 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8129 8129 return (EAGAIN);
8130 8130 }
8131 8131 /*
8132 8132 * Since we do have the segvn writers lock
8133 8133 * nobody can fill the cache with entries
8134 8134 * belonging to this seg during the purge.
8135 8135 * The flush either succeeds or we still
8136 8136 * have pending I/Os. In the later case,
8137 8137 * madvise(3C) fails.
8138 8138 */
8139 8139 segvn_purge(seg);
8140 8140 if (svd->softlockcnt > 0) {
8141 8141 /*
8142 8142 * Since madvise(3C) is advisory and
8143 8143 * it's not part of UNIX98, madvise(3C)
8144 8144 * failure here doesn't cause any hardship.
8145 8145 * Note that we don't block in "as" layer.
8146 8146 */
8147 8147 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8148 8148 return (EAGAIN);
8149 8149 }
8150 8150 } else if (svd->type == MAP_SHARED && svd->amp != NULL &&
8151 8151 svd->amp->a_softlockcnt > 0) {
8152 8152 /*
8153 8153 * Try to purge this amp's entries from pcache. It
8154 8154 * will succeed only if other segments that share the
8155 8155 * amp have no outstanding softlock's.
8156 8156 */
8157 8157 segvn_purge(seg);
8158 8158 }
8159 8159 }
8160 8160
8161 8161 amp = svd->amp;
8162 8162 vp = svd->vp;
8163 8163 if (behav == MADV_FREE || behav == MADV_PURGE) {
8164 8164 pgcnt_t purged;
8165 8165
8166 8166 if (behav == MADV_FREE && (vp != NULL || amp == NULL)) {
8167 8167 /*
8168 8168 * MADV_FREE is not supported for segments with an
8169 8169 * underlying object; if anonmap is NULL, anon slots
8170 8170 * are not yet populated and there is nothing for us
8171 8171 * to do. As MADV_FREE is advisory, we don't return an
8172 8172 * error in either case.
8173 8173 */
8174 8174 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8175 8175 return (0);
8176 8176 }
8177 8177
8178 8178 if (amp == NULL) {
8179 8179 /*
8180 8180 * If we're here with a NULL anonmap, it's because we
8181 8181 * are doing a MADV_PURGE. We have nothing to do, but
8182 8182 * because MADV_PURGE isn't merely advisory, we return
8183 8183 * an error in this case.
8184 8184 */
8185 8185 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8186 8186 return (EBUSY);
8187 8187 }
8188 8188
8189 8189 segvn_purge(seg);
8190 8190
8191 8191 page = seg_page(seg, addr);
8192 8192 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
8193 8193 err = anon_disclaim(amp,
8194 8194 svd->anon_index + page, len, behav, &purged);
8195 8195
8196 8196 if (purged != 0 && (svd->flags & MAP_NORESERVE)) {
8197 8197 /*
8198 8198 * If we purged pages on a MAP_NORESERVE mapping, we
8199 8199 * need to be sure to now unreserve our reserved swap.
8200 8200 * (We use the atomic operations to manipulate our
8201 8201 * segment and address space counters because we only
8202 8202 * have the corresponding locks held as reader, not
8203 8203 * writer.)
8204 8204 */
8205 8205 ssize_t bytes = ptob(purged);
8206 8206
8207 8207 anon_unresv_zone(bytes, seg->s_as->a_proc->p_zone);
8208 8208 atomic_add_long(&svd->swresv, -bytes);
8209 8209 atomic_add_long(&seg->s_as->a_resvsize, -bytes);
8210 8210 }
8211 8211
8212 8212 ANON_LOCK_EXIT(&->a_rwlock);
8213 8213 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8214 8214
8215 8215 /*
8216 8216 * MADV_PURGE and MADV_FREE differ in their return semantics:
8217 8217 * because MADV_PURGE is designed to be bug-for-bug compatible
8218 8218 * with its clumsy Linux forebear, it will fail where MADV_FREE
8219 8219 * does not.
8220 8220 */
8221 8221 return (behav == MADV_PURGE ? err : 0);
8222 8222 }
8223 8223
8224 8224 /*
8225 8225 * If advice is to be applied to entire segment,
8226 8226 * use advice field in seg_data structure
8227 8227 * otherwise use appropriate vpage entry.
8228 8228 */
8229 8229 if ((addr == seg->s_base) && (len == seg->s_size)) {
8230 8230 switch (behav) {
8231 8231 case MADV_ACCESS_LWP:
8232 8232 case MADV_ACCESS_MANY:
8233 8233 case MADV_ACCESS_DEFAULT:
8234 8234 /*
8235 8235 * Set memory allocation policy for this segment
8236 8236 */
8237 8237 policy = lgrp_madv_to_policy(behav, len, svd->type);
8238 8238 if (svd->type == MAP_SHARED)
8239 8239 already_set = lgrp_shm_policy_set(policy, amp,
8240 8240 svd->anon_index, vp, svd->offset, len);
8241 8241 else {
8242 8242 /*
8243 8243 * For private memory, need writers lock on
8244 8244 * address space because the segment may be
8245 8245 * split or concatenated when changing policy
8246 8246 */
8247 8247 if (AS_READ_HELD(seg->s_as)) {
8248 8248 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8249 8249 return (IE_RETRY);
8250 8250 }
8251 8251
8252 8252 already_set = lgrp_privm_policy_set(policy,
8253 8253 &svd->policy_info, len);
8254 8254 }
8255 8255
8256 8256 /*
8257 8257 * If policy set already and it shouldn't be reapplied,
8258 8258 * don't do anything.
8259 8259 */
8260 8260 if (already_set &&
8261 8261 !LGRP_MEM_POLICY_REAPPLICABLE(policy))
8262 8262 break;
8263 8263
8264 8264 /*
8265 8265 * Mark any existing pages in given range for
8266 8266 * migration
8267 8267 */
8268 8268 page_mark_migrate(seg, addr, len, amp, svd->anon_index,
8269 8269 vp, svd->offset, 1);
8270 8270
8271 8271 /*
8272 8272 * If same policy set already or this is a shared
8273 8273 * memory segment, don't need to try to concatenate
8274 8274 * segment with adjacent ones.
8275 8275 */
8276 8276 if (already_set || svd->type == MAP_SHARED)
8277 8277 break;
8278 8278
8279 8279 /*
8280 8280 * Try to concatenate this segment with previous
8281 8281 * one and next one, since we changed policy for
8282 8282 * this one and it may be compatible with adjacent
8283 8283 * ones now.
8284 8284 */
8285 8285 prev = AS_SEGPREV(seg->s_as, seg);
8286 8286 next = AS_SEGNEXT(seg->s_as, seg);
8287 8287
8288 8288 if (next && next->s_ops == &segvn_ops &&
8289 8289 addr + len == next->s_base)
8290 8290 (void) segvn_concat(seg, next, 1);
8291 8291
8292 8292 if (prev && prev->s_ops == &segvn_ops &&
8293 8293 addr == prev->s_base + prev->s_size) {
8294 8294 /*
8295 8295 * Drop lock for private data of current
8296 8296 * segment before concatenating (deleting) it
8297 8297 * and return IE_REATTACH to tell as_ctl() that
8298 8298 * current segment has changed
8299 8299 */
8300 8300 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8301 8301 if (!segvn_concat(prev, seg, 1))
8302 8302 err = IE_REATTACH;
8303 8303
8304 8304 return (err);
8305 8305 }
8306 8306 break;
8307 8307
8308 8308 case MADV_SEQUENTIAL:
8309 8309 /*
8310 8310 * unloading mapping guarantees
8311 8311 * detection in segvn_fault
8312 8312 */
8313 8313 ASSERT(seg->s_szc == 0);
8314 8314 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
8315 8315 hat_unload(seg->s_as->a_hat, addr, len,
8316 8316 HAT_UNLOAD);
8317 8317 /* FALLTHROUGH */
8318 8318 case MADV_NORMAL:
8319 8319 case MADV_RANDOM:
8320 8320 svd->advice = (uchar_t)behav;
8321 8321 svd->pageadvice = 0;
8322 8322 break;
8323 8323 case MADV_WILLNEED: /* handled in memcntl */
8324 8324 case MADV_DONTNEED: /* handled in memcntl */
8325 8325 case MADV_FREE: /* handled above */
8326 8326 case MADV_PURGE: /* handled above */
8327 8327 break;
8328 8328 default:
8329 8329 err = EINVAL;
8330 8330 }
8331 8331 } else {
8332 8332 caddr_t eaddr;
8333 8333 struct seg *new_seg;
8334 8334 struct segvn_data *new_svd;
8335 8335 u_offset_t off;
8336 8336 caddr_t oldeaddr;
8337 8337
8338 8338 page = seg_page(seg, addr);
8339 8339
8340 8340 segvn_vpage(seg);
8341 8341 if (svd->vpage == NULL) {
8342 8342 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8343 8343 return (ENOMEM);
8344 8344 }
8345 8345
8346 8346 switch (behav) {
8347 8347 struct vpage *bvpp, *evpp;
8348 8348
8349 8349 case MADV_ACCESS_LWP:
8350 8350 case MADV_ACCESS_MANY:
8351 8351 case MADV_ACCESS_DEFAULT:
8352 8352 /*
8353 8353 * Set memory allocation policy for portion of this
8354 8354 * segment
8355 8355 */
8356 8356
8357 8357 /*
8358 8358 * Align address and length of advice to page
8359 8359 * boundaries for large pages
8360 8360 */
8361 8361 if (seg->s_szc != 0) {
8362 8362 size_t pgsz;
8363 8363
8364 8364 pgsz = page_get_pagesize(seg->s_szc);
8365 8365 addr = (caddr_t)P2ALIGN((uintptr_t)addr, pgsz);
8366 8366 len = P2ROUNDUP(len, pgsz);
8367 8367 }
8368 8368
8369 8369 /*
8370 8370 * Check to see whether policy is set already
8371 8371 */
8372 8372 policy = lgrp_madv_to_policy(behav, len, svd->type);
8373 8373
8374 8374 anon_index = svd->anon_index + page;
8375 8375 off = svd->offset + (uintptr_t)(addr - seg->s_base);
8376 8376
8377 8377 if (svd->type == MAP_SHARED)
8378 8378 already_set = lgrp_shm_policy_set(policy, amp,
8379 8379 anon_index, vp, off, len);
8380 8380 else
8381 8381 already_set =
8382 8382 (policy == svd->policy_info.mem_policy);
8383 8383
8384 8384 /*
8385 8385 * If policy set already and it shouldn't be reapplied,
8386 8386 * don't do anything.
8387 8387 */
8388 8388 if (already_set &&
8389 8389 !LGRP_MEM_POLICY_REAPPLICABLE(policy))
8390 8390 break;
8391 8391
8392 8392 /*
8393 8393 * For private memory, need writers lock on
8394 8394 * address space because the segment may be
8395 8395 * split or concatenated when changing policy
8396 8396 */
8397 8397 if (svd->type == MAP_PRIVATE &&
8398 8398 AS_READ_HELD(seg->s_as)) {
8399 8399 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8400 8400 return (IE_RETRY);
8401 8401 }
8402 8402
8403 8403 /*
8404 8404 * Mark any existing pages in given range for
8405 8405 * migration
8406 8406 */
8407 8407 page_mark_migrate(seg, addr, len, amp, svd->anon_index,
8408 8408 vp, svd->offset, 1);
8409 8409
8410 8410 /*
8411 8411 * Don't need to try to split or concatenate
8412 8412 * segments, since policy is same or this is a shared
8413 8413 * memory segment
8414 8414 */
8415 8415 if (already_set || svd->type == MAP_SHARED)
8416 8416 break;
8417 8417
8418 8418 if (HAT_IS_REGION_COOKIE_VALID(svd->rcookie)) {
8419 8419 ASSERT(svd->amp == NULL);
8420 8420 ASSERT(svd->tr_state == SEGVN_TR_OFF);
8421 8421 ASSERT(svd->softlockcnt == 0);
8422 8422 hat_leave_region(seg->s_as->a_hat, svd->rcookie,
8423 8423 HAT_REGION_TEXT);
8424 8424 svd->rcookie = HAT_INVALID_REGION_COOKIE;
8425 8425 }
8426 8426
8427 8427 /*
8428 8428 * Split off new segment if advice only applies to a
8429 8429 * portion of existing segment starting in middle
8430 8430 */
8431 8431 new_seg = NULL;
8432 8432 eaddr = addr + len;
8433 8433 oldeaddr = seg->s_base + seg->s_size;
8434 8434 if (addr > seg->s_base) {
8435 8435 /*
8436 8436 * Must flush I/O page cache
8437 8437 * before splitting segment
8438 8438 */
8439 8439 if (svd->softlockcnt > 0)
8440 8440 segvn_purge(seg);
8441 8441
8442 8442 /*
8443 8443 * Split segment and return IE_REATTACH to tell
8444 8444 * as_ctl() that current segment changed
8445 8445 */
8446 8446 new_seg = segvn_split_seg(seg, addr);
8447 8447 new_svd = (struct segvn_data *)new_seg->s_data;
8448 8448 err = IE_REATTACH;
8449 8449
8450 8450 /*
8451 8451 * If new segment ends where old one
8452 8452 * did, try to concatenate the new
8453 8453 * segment with next one.
8454 8454 */
8455 8455 if (eaddr == oldeaddr) {
8456 8456 /*
8457 8457 * Set policy for new segment
8458 8458 */
8459 8459 (void) lgrp_privm_policy_set(policy,
8460 8460 &new_svd->policy_info,
8461 8461 new_seg->s_size);
8462 8462
8463 8463 next = AS_SEGNEXT(new_seg->s_as,
8464 8464 new_seg);
8465 8465
8466 8466 if (next &&
8467 8467 next->s_ops == &segvn_ops &&
8468 8468 eaddr == next->s_base)
8469 8469 (void) segvn_concat(new_seg,
8470 8470 next, 1);
8471 8471 }
8472 8472 }
8473 8473
8474 8474 /*
8475 8475 * Split off end of existing segment if advice only
8476 8476 * applies to a portion of segment ending before
8477 8477 * end of the existing segment
8478 8478 */
8479 8479 if (eaddr < oldeaddr) {
8480 8480 /*
8481 8481 * Must flush I/O page cache
8482 8482 * before splitting segment
8483 8483 */
8484 8484 if (svd->softlockcnt > 0)
8485 8485 segvn_purge(seg);
8486 8486
8487 8487 /*
8488 8488 * If beginning of old segment was already
8489 8489 * split off, use new segment to split end off
8490 8490 * from.
8491 8491 */
8492 8492 if (new_seg != NULL && new_seg != seg) {
8493 8493 /*
8494 8494 * Split segment
8495 8495 */
8496 8496 (void) segvn_split_seg(new_seg, eaddr);
8497 8497
8498 8498 /*
8499 8499 * Set policy for new segment
8500 8500 */
8501 8501 (void) lgrp_privm_policy_set(policy,
8502 8502 &new_svd->policy_info,
8503 8503 new_seg->s_size);
8504 8504 } else {
8505 8505 /*
8506 8506 * Split segment and return IE_REATTACH
8507 8507 * to tell as_ctl() that current
8508 8508 * segment changed
8509 8509 */
8510 8510 (void) segvn_split_seg(seg, eaddr);
8511 8511 err = IE_REATTACH;
8512 8512
8513 8513 (void) lgrp_privm_policy_set(policy,
8514 8514 &svd->policy_info, seg->s_size);
8515 8515
8516 8516 /*
8517 8517 * If new segment starts where old one
8518 8518 * did, try to concatenate it with
8519 8519 * previous segment.
8520 8520 */
8521 8521 if (addr == seg->s_base) {
8522 8522 prev = AS_SEGPREV(seg->s_as,
8523 8523 seg);
8524 8524
8525 8525 /*
8526 8526 * Drop lock for private data
8527 8527 * of current segment before
8528 8528 * concatenating (deleting) it
8529 8529 */
8530 8530 if (prev &&
8531 8531 prev->s_ops ==
8532 8532 &segvn_ops &&
8533 8533 addr == prev->s_base +
8534 8534 prev->s_size) {
8535 8535 SEGVN_LOCK_EXIT(
8536 8536 seg->s_as,
8537 8537 &svd->lock);
8538 8538 (void) segvn_concat(
8539 8539 prev, seg, 1);
8540 8540 return (err);
8541 8541 }
8542 8542 }
8543 8543 }
8544 8544 }
8545 8545 break;
8546 8546 case MADV_SEQUENTIAL:
8547 8547 ASSERT(seg->s_szc == 0);
8548 8548 ASSERT(svd->rcookie == HAT_INVALID_REGION_COOKIE);
8549 8549 hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD);
8550 8550 /* FALLTHROUGH */
8551 8551 case MADV_NORMAL:
8552 8552 case MADV_RANDOM:
8553 8553 bvpp = &svd->vpage[page];
8554 8554 evpp = &svd->vpage[page + (len >> PAGESHIFT)];
8555 8555 for (; bvpp < evpp; bvpp++)
8556 8556 VPP_SETADVICE(bvpp, behav);
8557 8557 svd->advice = MADV_NORMAL;
8558 8558 break;
8559 8559 case MADV_WILLNEED: /* handled in memcntl */
8560 8560 case MADV_DONTNEED: /* handled in memcntl */
8561 8561 case MADV_FREE: /* handled above */
8562 8562 case MADV_PURGE: /* handled above */
8563 8563 break;
8564 8564 default:
8565 8565 err = EINVAL;
8566 8566 }
8567 8567 }
8568 8568 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8569 8569 return (err);
8570 8570 }
8571 8571
8572 8572 /*
8573 8573 * There is one kind of inheritance that can be specified for pages:
8574 8574 *
8575 8575 * SEGP_INH_ZERO - Pages should be zeroed in the child
8576 8576 */
8577 8577 static int
8578 8578 segvn_inherit(struct seg *seg, caddr_t addr, size_t len, uint_t behav)
8579 8579 {
8580 8580 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
8581 8581 struct vpage *bvpp, *evpp;
8582 8582 size_t page;
8583 8583 int ret = 0;
8584 8584
8585 8585 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
8586 8586
8587 8587 /* Can't support something we don't know about */
8588 8588 if (behav != SEGP_INH_ZERO)
8589 8589 return (ENOTSUP);
8590 8590
8591 8591 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
8592 8592
8593 8593 /*
8594 8594 * This must be a straightforward anonymous segment that is mapped
8595 8595 * privately and is not backed by a vnode.
8596 8596 */
8597 8597 if (svd->tr_state != SEGVN_TR_OFF ||
8598 8598 svd->type != MAP_PRIVATE ||
8599 8599 svd->vp != NULL) {
8600 8600 ret = EINVAL;
8601 8601 goto out;
8602 8602 }
8603 8603
8604 8604 /*
8605 8605 * If the entire segment has been marked as inherit zero, then no reason
8606 8606 * to do anything else.
8607 8607 */
8608 8608 if (svd->svn_inz == SEGVN_INZ_ALL) {
8609 8609 ret = 0;
8610 8610 goto out;
8611 8611 }
8612 8612
8613 8613 /*
8614 8614 * If this applies to the entire segment, simply mark it and we're done.
8615 8615 */
8616 8616 if ((addr == seg->s_base) && (len == seg->s_size)) {
8617 8617 svd->svn_inz = SEGVN_INZ_ALL;
8618 8618 ret = 0;
8619 8619 goto out;
8620 8620 }
8621 8621
8622 8622 /*
8623 8623 * We've been asked to mark a subset of this segment as inherit zero,
8624 8624 * therefore we need to mainpulate its vpages.
8625 8625 */
8626 8626 if (svd->vpage == NULL) {
8627 8627 segvn_vpage(seg);
8628 8628 if (svd->vpage == NULL) {
8629 8629 ret = ENOMEM;
8630 8630 goto out;
8631 8631 }
8632 8632 }
8633 8633
8634 8634 svd->svn_inz = SEGVN_INZ_VPP;
8635 8635 page = seg_page(seg, addr);
8636 8636 bvpp = &svd->vpage[page];
8637 8637 evpp = &svd->vpage[page + (len >> PAGESHIFT)];
8638 8638 for (; bvpp < evpp; bvpp++)
8639 8639 VPP_SETINHZERO(bvpp);
8640 8640 ret = 0;
8641 8641
8642 8642 out:
8643 8643 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
8644 8644 return (ret);
8645 8645 }
8646 8646
8647 8647 /*
8648 8648 * Create a vpage structure for this seg.
8649 8649 */
8650 8650 static void
8651 8651 segvn_vpage(struct seg *seg)
8652 8652 {
8653 8653 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
8654 8654 struct vpage *vp, *evp;
8655 8655 static pgcnt_t page_limit = 0;
8656 8656
8657 8657 ASSERT(SEGVN_WRITE_HELD(seg->s_as, &svd->lock));
8658 8658
8659 8659 /*
8660 8660 * If no vpage structure exists, allocate one. Copy the protections
8661 8661 * and the advice from the segment itself to the individual pages.
8662 8662 */
8663 8663 if (svd->vpage == NULL) {
8664 8664 /*
8665 8665 * Start by calculating the number of pages we must allocate to
8666 8666 * track the per-page vpage structs needs for this entire
8667 8667 * segment. If we know now that it will require more than our
8668 8668 * heuristic for the maximum amount of kmem we can consume then
8669 8669 * fail. We do this here, instead of trying to detect this deep
8670 8670 * in page_resv and propagating the error up, since the entire
8671 8671 * memory allocation stack is not amenable to passing this
8672 8672 * back. Instead, it wants to keep trying.
8673 8673 *
8674 8674 * As a heuristic we set a page limit of 5/8s of total_pages
8675 8675 * for this allocation. We use shifts so that no floating
8676 8676 * point conversion takes place and only need to do the
8677 8677 * calculation once.
8678 8678 */
8679 8679 ulong_t mem_needed = seg_pages(seg) * sizeof (struct vpage);
8680 8680 pgcnt_t npages = mem_needed >> PAGESHIFT;
8681 8681
8682 8682 if (page_limit == 0)
8683 8683 page_limit = (total_pages >> 1) + (total_pages >> 3);
8684 8684
8685 8685 if (npages > page_limit)
8686 8686 return;
8687 8687
8688 8688 svd->pageadvice = 1;
8689 8689 svd->vpage = kmem_zalloc(mem_needed, KM_SLEEP);
8690 8690 evp = &svd->vpage[seg_page(seg, seg->s_base + seg->s_size)];
8691 8691 for (vp = svd->vpage; vp < evp; vp++) {
8692 8692 VPP_SETPROT(vp, svd->prot);
8693 8693 VPP_SETADVICE(vp, svd->advice);
8694 8694 }
8695 8695 }
8696 8696 }
8697 8697
8698 8698 /*
8699 8699 * Dump the pages belonging to this segvn segment.
8700 8700 */
8701 8701 static void
8702 8702 segvn_dump(struct seg *seg)
8703 8703 {
8704 8704 struct segvn_data *svd;
8705 8705 page_t *pp;
8706 8706 struct anon_map *amp;
8707 8707 ulong_t anon_index;
8708 8708 struct vnode *vp;
8709 8709 u_offset_t off, offset;
8710 8710 pfn_t pfn;
8711 8711 pgcnt_t page, npages;
8712 8712 caddr_t addr;
8713 8713
8714 8714 npages = seg_pages(seg);
8715 8715 svd = (struct segvn_data *)seg->s_data;
8716 8716 vp = svd->vp;
8717 8717 off = offset = svd->offset;
8718 8718 addr = seg->s_base;
8719 8719
8720 8720 if ((amp = svd->amp) != NULL) {
8721 8721 anon_index = svd->anon_index;
8722 8722 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
8723 8723 }
8724 8724
8725 8725 for (page = 0; page < npages; page++, offset += PAGESIZE) {
8726 8726 struct anon *ap;
8727 8727 int we_own_it = 0;
8728 8728
8729 8729 if (amp && (ap = anon_get_ptr(svd->amp->ahp, anon_index++))) {
8730 8730 swap_xlate_nopanic(ap, &vp, &off);
8731 8731 } else {
8732 8732 vp = svd->vp;
8733 8733 off = offset;
8734 8734 }
8735 8735
8736 8736 /*
8737 8737 * If pp == NULL, the page either does not exist
8738 8738 * or is exclusively locked. So determine if it
8739 8739 * exists before searching for it.
8740 8740 */
8741 8741
8742 8742 if ((pp = page_lookup_nowait(vp, off, SE_SHARED)))
8743 8743 we_own_it = 1;
8744 8744 else
8745 8745 pp = page_exists(vp, off);
8746 8746
8747 8747 if (pp) {
8748 8748 pfn = page_pptonum(pp);
8749 8749 dump_addpage(seg->s_as, addr, pfn);
8750 8750 if (we_own_it)
8751 8751 page_unlock(pp);
8752 8752 }
8753 8753 addr += PAGESIZE;
8754 8754 dump_timeleft = dump_timeout;
8755 8755 }
8756 8756
8757 8757 if (amp != NULL)
8758 8758 ANON_LOCK_EXIT(&->a_rwlock);
8759 8759 }
8760 8760
8761 8761 #ifdef DEBUG
8762 8762 static uint32_t segvn_pglock_mtbf = 0;
8763 8763 #endif
8764 8764
8765 8765 #define PCACHE_SHWLIST ((page_t *)-2)
8766 8766 #define NOPCACHE_SHWLIST ((page_t *)-1)
8767 8767
8768 8768 /*
8769 8769 * Lock/Unlock anon pages over a given range. Return shadow list. This routine
8770 8770 * uses global segment pcache to cache shadow lists (i.e. pp arrays) of pages
8771 8771 * to avoid the overhead of per page locking, unlocking for subsequent IOs to
8772 8772 * the same parts of the segment. Currently shadow list creation is only
8773 8773 * supported for pure anon segments. MAP_PRIVATE segment pcache entries are
8774 8774 * tagged with segment pointer, starting virtual address and length. This
8775 8775 * approach for MAP_SHARED segments may add many pcache entries for the same
8776 8776 * set of pages and lead to long hash chains that decrease pcache lookup
8777 8777 * performance. To avoid this issue for shared segments shared anon map and
8778 8778 * starting anon index are used for pcache entry tagging. This allows all
8779 8779 * segments to share pcache entries for the same anon range and reduces pcache
8780 8780 * chain's length as well as memory overhead from duplicate shadow lists and
8781 8781 * pcache entries.
8782 8782 *
8783 8783 * softlockcnt field in segvn_data structure counts the number of F_SOFTLOCK'd
8784 8784 * pages via segvn_fault() and pagelock'd pages via this routine. But pagelock
8785 8785 * part of softlockcnt accounting is done differently for private and shared
8786 8786 * segments. In private segment case softlock is only incremented when a new
8787 8787 * shadow list is created but not when an existing one is found via
8788 8788 * seg_plookup(). pcache entries have reference count incremented/decremented
8789 8789 * by each seg_plookup()/seg_pinactive() operation. Only entries that have 0
8790 8790 * reference count can be purged (and purging is needed before segment can be
8791 8791 * freed). When a private segment pcache entry is purged segvn_reclaim() will
8792 8792 * decrement softlockcnt. Since in private segment case each of its pcache
8793 8793 * entries only belongs to this segment we can expect that when
8794 8794 * segvn_pagelock(L_PAGEUNLOCK) was called for all outstanding IOs in this
8795 8795 * segment purge will succeed and softlockcnt will drop to 0. In shared
8796 8796 * segment case reference count in pcache entry counts active locks from many
8797 8797 * different segments so we can't expect segment purging to succeed even when
8798 8798 * segvn_pagelock(L_PAGEUNLOCK) was called for all outstanding IOs in this
8799 8799 * segment. To be able to determine when there're no pending pagelocks in
8800 8800 * shared segment case we don't rely on purging to make softlockcnt drop to 0
8801 8801 * but instead softlockcnt is incremented and decremented for every
8802 8802 * segvn_pagelock(L_PAGELOCK/L_PAGEUNLOCK) call regardless if a new shadow
8803 8803 * list was created or an existing one was found. When softlockcnt drops to 0
8804 8804 * this segment no longer has any claims for pcached shadow lists and the
8805 8805 * segment can be freed even if there're still active pcache entries
8806 8806 * shared by this segment anon map. Shared segment pcache entries belong to
8807 8807 * anon map and are typically removed when anon map is freed after all
8808 8808 * processes destroy the segments that use this anon map.
8809 8809 */
8810 8810 static int
8811 8811 segvn_pagelock(struct seg *seg, caddr_t addr, size_t len, struct page ***ppp,
8812 8812 enum lock_type type, enum seg_rw rw)
8813 8813 {
8814 8814 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
8815 8815 size_t np;
8816 8816 pgcnt_t adjustpages;
8817 8817 pgcnt_t npages;
8818 8818 ulong_t anon_index;
8819 8819 uint_t protchk = (rw == S_READ) ? PROT_READ : PROT_WRITE;
8820 8820 uint_t error;
8821 8821 struct anon_map *amp;
8822 8822 pgcnt_t anpgcnt;
8823 8823 struct page **pplist, **pl, *pp;
8824 8824 caddr_t a;
8825 8825 size_t page;
8826 8826 caddr_t lpgaddr, lpgeaddr;
8827 8827 anon_sync_obj_t cookie;
8828 8828 int anlock;
8829 8829 struct anon_map *pamp;
8830 8830 caddr_t paddr;
8831 8831 seg_preclaim_cbfunc_t preclaim_callback;
8832 8832 size_t pgsz;
8833 8833 int use_pcache;
8834 8834 size_t wlen;
8835 8835 uint_t pflags = 0;
8836 8836 int sftlck_sbase = 0;
8837 8837 int sftlck_send = 0;
8838 8838
8839 8839 #ifdef DEBUG
8840 8840 if (type == L_PAGELOCK && segvn_pglock_mtbf) {
8841 8841 hrtime_t ts = gethrtime();
8842 8842 if ((ts % segvn_pglock_mtbf) == 0) {
8843 8843 return (ENOTSUP);
8844 8844 }
8845 8845 if ((ts % segvn_pglock_mtbf) == 1) {
8846 8846 return (EFAULT);
8847 8847 }
8848 8848 }
8849 8849 #endif
8850 8850
8851 8851 TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_START,
8852 8852 "segvn_pagelock: start seg %p addr %p", seg, addr);
8853 8853
8854 8854 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
8855 8855 ASSERT(type == L_PAGELOCK || type == L_PAGEUNLOCK);
8856 8856
8857 8857 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
8858 8858
8859 8859 /*
8860 8860 * for now we only support pagelock to anon memory. We would have to
8861 8861 * check protections for vnode objects and call into the vnode driver.
8862 8862 * That's too much for a fast path. Let the fault entry point handle
8863 8863 * it.
8864 8864 */
8865 8865 if (svd->vp != NULL) {
8866 8866 if (type == L_PAGELOCK) {
8867 8867 error = ENOTSUP;
8868 8868 goto out;
8869 8869 }
8870 8870 panic("segvn_pagelock(L_PAGEUNLOCK): vp != NULL");
8871 8871 }
8872 8872 if ((amp = svd->amp) == NULL) {
8873 8873 if (type == L_PAGELOCK) {
8874 8874 error = EFAULT;
8875 8875 goto out;
8876 8876 }
8877 8877 panic("segvn_pagelock(L_PAGEUNLOCK): amp == NULL");
8878 8878 }
8879 8879 if (rw != S_READ && rw != S_WRITE) {
8880 8880 if (type == L_PAGELOCK) {
8881 8881 error = ENOTSUP;
8882 8882 goto out;
8883 8883 }
8884 8884 panic("segvn_pagelock(L_PAGEUNLOCK): bad rw");
8885 8885 }
8886 8886
8887 8887 if (seg->s_szc != 0) {
8888 8888 /*
8889 8889 * We are adjusting the pagelock region to the large page size
8890 8890 * boundary because the unlocked part of a large page cannot
8891 8891 * be freed anyway unless all constituent pages of a large
8892 8892 * page are locked. Bigger regions reduce pcache chain length
8893 8893 * and improve lookup performance. The tradeoff is that the
8894 8894 * very first segvn_pagelock() call for a given page is more
8895 8895 * expensive if only 1 page_t is needed for IO. This is only
8896 8896 * an issue if pcache entry doesn't get reused by several
8897 8897 * subsequent calls. We optimize here for the case when pcache
8898 8898 * is heavily used by repeated IOs to the same address range.
8899 8899 *
8900 8900 * Note segment's page size cannot change while we are holding
8901 8901 * as lock. And then it cannot change while softlockcnt is
8902 8902 * not 0. This will allow us to correctly recalculate large
8903 8903 * page size region for the matching pageunlock/reclaim call
8904 8904 * since as_pageunlock() caller must always match
8905 8905 * as_pagelock() call's addr and len.
8906 8906 *
8907 8907 * For pageunlock *ppp points to the pointer of page_t that
8908 8908 * corresponds to the real unadjusted start address. Similar
8909 8909 * for pagelock *ppp must point to the pointer of page_t that
8910 8910 * corresponds to the real unadjusted start address.
8911 8911 */
8912 8912 pgsz = page_get_pagesize(seg->s_szc);
8913 8913 CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr);
8914 8914 adjustpages = btop((uintptr_t)(addr - lpgaddr));
8915 8915 } else if (len < segvn_pglock_comb_thrshld) {
8916 8916 lpgaddr = addr;
8917 8917 lpgeaddr = addr + len;
8918 8918 adjustpages = 0;
8919 8919 pgsz = PAGESIZE;
8920 8920 } else {
8921 8921 /*
8922 8922 * Align the address range of large enough requests to allow
8923 8923 * combining of different shadow lists into 1 to reduce memory
8924 8924 * overhead from potentially overlapping large shadow lists
8925 8925 * (worst case is we have a 1MB IO into buffers with start
8926 8926 * addresses separated by 4K). Alignment is only possible if
8927 8927 * padded chunks have sufficient access permissions. Note
8928 8928 * permissions won't change between L_PAGELOCK and
8929 8929 * L_PAGEUNLOCK calls since non 0 softlockcnt will force
8930 8930 * segvn_setprot() to wait until softlockcnt drops to 0. This
8931 8931 * allows us to determine in L_PAGEUNLOCK the same range we
8932 8932 * computed in L_PAGELOCK.
8933 8933 *
8934 8934 * If alignment is limited by segment ends set
8935 8935 * sftlck_sbase/sftlck_send flags. In L_PAGELOCK case when
8936 8936 * these flags are set bump softlockcnt_sbase/softlockcnt_send
8937 8937 * per segment counters. In L_PAGEUNLOCK case decrease
8938 8938 * softlockcnt_sbase/softlockcnt_send counters if
8939 8939 * sftlck_sbase/sftlck_send flags are set. When
8940 8940 * softlockcnt_sbase/softlockcnt_send are non 0
8941 8941 * segvn_concat()/segvn_extend_prev()/segvn_extend_next()
8942 8942 * won't merge the segments. This restriction combined with
8943 8943 * restriction on segment unmapping and splitting for segments
8944 8944 * that have non 0 softlockcnt allows L_PAGEUNLOCK to
8945 8945 * correctly determine the same range that was previously
8946 8946 * locked by matching L_PAGELOCK.
8947 8947 */
8948 8948 pflags = SEGP_PSHIFT | (segvn_pglock_comb_bshift << 16);
8949 8949 pgsz = PAGESIZE;
8950 8950 if (svd->type == MAP_PRIVATE) {
8951 8951 lpgaddr = (caddr_t)P2ALIGN((uintptr_t)addr,
8952 8952 segvn_pglock_comb_balign);
8953 8953 if (lpgaddr < seg->s_base) {
8954 8954 lpgaddr = seg->s_base;
8955 8955 sftlck_sbase = 1;
8956 8956 }
8957 8957 } else {
8958 8958 ulong_t aix = svd->anon_index + seg_page(seg, addr);
8959 8959 ulong_t aaix = P2ALIGN(aix, segvn_pglock_comb_palign);
8960 8960 if (aaix < svd->anon_index) {
8961 8961 lpgaddr = seg->s_base;
8962 8962 sftlck_sbase = 1;
8963 8963 } else {
8964 8964 lpgaddr = addr - ptob(aix - aaix);
8965 8965 ASSERT(lpgaddr >= seg->s_base);
8966 8966 }
8967 8967 }
8968 8968 if (svd->pageprot && lpgaddr != addr) {
8969 8969 struct vpage *vp = &svd->vpage[seg_page(seg, lpgaddr)];
8970 8970 struct vpage *evp = &svd->vpage[seg_page(seg, addr)];
8971 8971 while (vp < evp) {
8972 8972 if ((VPP_PROT(vp) & protchk) == 0) {
8973 8973 break;
8974 8974 }
8975 8975 vp++;
8976 8976 }
8977 8977 if (vp < evp) {
8978 8978 lpgaddr = addr;
8979 8979 pflags = 0;
8980 8980 }
8981 8981 }
8982 8982 lpgeaddr = addr + len;
8983 8983 if (pflags) {
8984 8984 if (svd->type == MAP_PRIVATE) {
8985 8985 lpgeaddr = (caddr_t)P2ROUNDUP(
8986 8986 (uintptr_t)lpgeaddr,
8987 8987 segvn_pglock_comb_balign);
8988 8988 } else {
8989 8989 ulong_t aix = svd->anon_index +
8990 8990 seg_page(seg, lpgeaddr);
8991 8991 ulong_t aaix = P2ROUNDUP(aix,
8992 8992 segvn_pglock_comb_palign);
8993 8993 if (aaix < aix) {
8994 8994 lpgeaddr = 0;
8995 8995 } else {
8996 8996 lpgeaddr += ptob(aaix - aix);
8997 8997 }
8998 8998 }
8999 8999 if (lpgeaddr == 0 ||
9000 9000 lpgeaddr > seg->s_base + seg->s_size) {
9001 9001 lpgeaddr = seg->s_base + seg->s_size;
9002 9002 sftlck_send = 1;
9003 9003 }
9004 9004 }
9005 9005 if (svd->pageprot && lpgeaddr != addr + len) {
9006 9006 struct vpage *vp;
9007 9007 struct vpage *evp;
9008 9008
9009 9009 vp = &svd->vpage[seg_page(seg, addr + len)];
9010 9010 evp = &svd->vpage[seg_page(seg, lpgeaddr)];
9011 9011
9012 9012 while (vp < evp) {
9013 9013 if ((VPP_PROT(vp) & protchk) == 0) {
9014 9014 break;
9015 9015 }
9016 9016 vp++;
9017 9017 }
9018 9018 if (vp < evp) {
9019 9019 lpgeaddr = addr + len;
9020 9020 }
9021 9021 }
9022 9022 adjustpages = btop((uintptr_t)(addr - lpgaddr));
9023 9023 }
9024 9024
9025 9025 /*
9026 9026 * For MAP_SHARED segments we create pcache entries tagged by amp and
9027 9027 * anon index so that we can share pcache entries with other segments
9028 9028 * that map this amp. For private segments pcache entries are tagged
9029 9029 * with segment and virtual address.
9030 9030 */
9031 9031 if (svd->type == MAP_SHARED) {
9032 9032 pamp = amp;
9033 9033 paddr = (caddr_t)((lpgaddr - seg->s_base) +
9034 9034 ptob(svd->anon_index));
9035 9035 preclaim_callback = shamp_reclaim;
9036 9036 } else {
9037 9037 pamp = NULL;
9038 9038 paddr = lpgaddr;
9039 9039 preclaim_callback = segvn_reclaim;
9040 9040 }
9041 9041
9042 9042 if (type == L_PAGEUNLOCK) {
9043 9043 VM_STAT_ADD(segvnvmstats.pagelock[0]);
9044 9044
9045 9045 /*
9046 9046 * update hat ref bits for /proc. We need to make sure
9047 9047 * that threads tracing the ref and mod bits of the
9048 9048 * address space get the right data.
9049 9049 * Note: page ref and mod bits are updated at reclaim time
9050 9050 */
9051 9051 if (seg->s_as->a_vbits) {
9052 9052 for (a = addr; a < addr + len; a += PAGESIZE) {
9053 9053 if (rw == S_WRITE) {
9054 9054 hat_setstat(seg->s_as, a,
9055 9055 PAGESIZE, P_REF | P_MOD);
9056 9056 } else {
9057 9057 hat_setstat(seg->s_as, a,
9058 9058 PAGESIZE, P_REF);
9059 9059 }
9060 9060 }
9061 9061 }
9062 9062
9063 9063 /*
9064 9064 * Check the shadow list entry after the last page used in
9065 9065 * this IO request. If it's NOPCACHE_SHWLIST the shadow list
9066 9066 * was not inserted into pcache and is not large page
9067 9067 * adjusted. In this case call reclaim callback directly and
9068 9068 * don't adjust the shadow list start and size for large
9069 9069 * pages.
9070 9070 */
9071 9071 npages = btop(len);
9072 9072 if ((*ppp)[npages] == NOPCACHE_SHWLIST) {
↓ open down ↓ |
9072 lines elided |
↑ open up ↑ |
9073 9073 void *ptag;
9074 9074 if (pamp != NULL) {
9075 9075 ASSERT(svd->type == MAP_SHARED);
9076 9076 ptag = (void *)pamp;
9077 9077 paddr = (caddr_t)((addr - seg->s_base) +
9078 9078 ptob(svd->anon_index));
9079 9079 } else {
9080 9080 ptag = (void *)seg;
9081 9081 paddr = addr;
9082 9082 }
9083 - (*preclaim_callback)(ptag, paddr, len, *ppp, rw, 0);
9083 + (void) preclaim_callback(ptag, paddr, len, *ppp, rw, 0);
9084 9084 } else {
9085 9085 ASSERT((*ppp)[npages] == PCACHE_SHWLIST ||
9086 9086 IS_SWAPFSVP((*ppp)[npages]->p_vnode));
9087 9087 len = lpgeaddr - lpgaddr;
9088 9088 npages = btop(len);
9089 9089 seg_pinactive(seg, pamp, paddr, len,
9090 9090 *ppp - adjustpages, rw, pflags, preclaim_callback);
9091 9091 }
9092 9092
9093 9093 if (pamp != NULL) {
9094 9094 ASSERT(svd->type == MAP_SHARED);
9095 9095 ASSERT(svd->softlockcnt >= npages);
9096 9096 atomic_add_long((ulong_t *)&svd->softlockcnt, -npages);
9097 9097 }
9098 9098
9099 9099 if (sftlck_sbase) {
9100 9100 ASSERT(svd->softlockcnt_sbase > 0);
9101 9101 atomic_dec_ulong((ulong_t *)&svd->softlockcnt_sbase);
9102 9102 }
9103 9103 if (sftlck_send) {
9104 9104 ASSERT(svd->softlockcnt_send > 0);
9105 9105 atomic_dec_ulong((ulong_t *)&svd->softlockcnt_send);
9106 9106 }
9107 9107
9108 9108 /*
9109 9109 * If someone is blocked while unmapping, we purge
9110 9110 * segment page cache and thus reclaim pplist synchronously
9111 9111 * without waiting for seg_pasync_thread. This speeds up
9112 9112 * unmapping in cases where munmap(2) is called, while
9113 9113 * raw async i/o is still in progress or where a thread
9114 9114 * exits on data fault in a multithreaded application.
9115 9115 */
9116 9116 if (AS_ISUNMAPWAIT(seg->s_as)) {
9117 9117 if (svd->softlockcnt == 0) {
9118 9118 mutex_enter(&seg->s_as->a_contents);
9119 9119 if (AS_ISUNMAPWAIT(seg->s_as)) {
9120 9120 AS_CLRUNMAPWAIT(seg->s_as);
9121 9121 cv_broadcast(&seg->s_as->a_cv);
9122 9122 }
9123 9123 mutex_exit(&seg->s_as->a_contents);
9124 9124 } else if (pamp == NULL) {
9125 9125 /*
9126 9126 * softlockcnt is not 0 and this is a
9127 9127 * MAP_PRIVATE segment. Try to purge its
9128 9128 * pcache entries to reduce softlockcnt.
9129 9129 * If it drops to 0 segvn_reclaim()
9130 9130 * will wake up a thread waiting on
9131 9131 * unmapwait flag.
9132 9132 *
9133 9133 * We don't purge MAP_SHARED segments with non
9134 9134 * 0 softlockcnt since IO is still in progress
9135 9135 * for such segments.
9136 9136 */
9137 9137 ASSERT(svd->type == MAP_PRIVATE);
9138 9138 segvn_purge(seg);
9139 9139 }
9140 9140 }
9141 9141 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
9142 9142 TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_UNLOCK_END,
9143 9143 "segvn_pagelock: unlock seg %p addr %p", seg, addr);
9144 9144 return (0);
9145 9145 }
9146 9146
9147 9147 /* The L_PAGELOCK case ... */
9148 9148
9149 9149 VM_STAT_ADD(segvnvmstats.pagelock[1]);
9150 9150
9151 9151 /*
9152 9152 * For MAP_SHARED segments we have to check protections before
9153 9153 * seg_plookup() since pcache entries may be shared by many segments
9154 9154 * with potentially different page protections.
9155 9155 */
9156 9156 if (pamp != NULL) {
9157 9157 ASSERT(svd->type == MAP_SHARED);
9158 9158 if (svd->pageprot == 0) {
9159 9159 if ((svd->prot & protchk) == 0) {
9160 9160 error = EACCES;
9161 9161 goto out;
9162 9162 }
9163 9163 } else {
9164 9164 /*
9165 9165 * check page protections
9166 9166 */
9167 9167 caddr_t ea;
9168 9168
9169 9169 if (seg->s_szc) {
9170 9170 a = lpgaddr;
9171 9171 ea = lpgeaddr;
9172 9172 } else {
9173 9173 a = addr;
9174 9174 ea = addr + len;
9175 9175 }
9176 9176 for (; a < ea; a += pgsz) {
9177 9177 struct vpage *vp;
9178 9178
9179 9179 ASSERT(seg->s_szc == 0 ||
9180 9180 sameprot(seg, a, pgsz));
9181 9181 vp = &svd->vpage[seg_page(seg, a)];
9182 9182 if ((VPP_PROT(vp) & protchk) == 0) {
9183 9183 error = EACCES;
9184 9184 goto out;
9185 9185 }
9186 9186 }
9187 9187 }
9188 9188 }
9189 9189
9190 9190 /*
9191 9191 * try to find pages in segment page cache
9192 9192 */
9193 9193 pplist = seg_plookup(seg, pamp, paddr, lpgeaddr - lpgaddr, rw, pflags);
9194 9194 if (pplist != NULL) {
9195 9195 if (pamp != NULL) {
9196 9196 npages = btop((uintptr_t)(lpgeaddr - lpgaddr));
9197 9197 ASSERT(svd->type == MAP_SHARED);
9198 9198 atomic_add_long((ulong_t *)&svd->softlockcnt,
9199 9199 npages);
9200 9200 }
9201 9201 if (sftlck_sbase) {
9202 9202 atomic_inc_ulong((ulong_t *)&svd->softlockcnt_sbase);
9203 9203 }
9204 9204 if (sftlck_send) {
9205 9205 atomic_inc_ulong((ulong_t *)&svd->softlockcnt_send);
9206 9206 }
9207 9207 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
9208 9208 *ppp = pplist + adjustpages;
9209 9209 TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_HIT_END,
9210 9210 "segvn_pagelock: cache hit seg %p addr %p", seg, addr);
9211 9211 return (0);
9212 9212 }
9213 9213
9214 9214 /*
9215 9215 * For MAP_SHARED segments we already verified above that segment
9216 9216 * protections allow this pagelock operation.
9217 9217 */
9218 9218 if (pamp == NULL) {
9219 9219 ASSERT(svd->type == MAP_PRIVATE);
9220 9220 if (svd->pageprot == 0) {
9221 9221 if ((svd->prot & protchk) == 0) {
9222 9222 error = EACCES;
9223 9223 goto out;
9224 9224 }
9225 9225 if (svd->prot & PROT_WRITE) {
9226 9226 wlen = lpgeaddr - lpgaddr;
9227 9227 } else {
9228 9228 wlen = 0;
9229 9229 ASSERT(rw == S_READ);
9230 9230 }
9231 9231 } else {
9232 9232 int wcont = 1;
9233 9233 /*
9234 9234 * check page protections
9235 9235 */
9236 9236 for (a = lpgaddr, wlen = 0; a < lpgeaddr; a += pgsz) {
9237 9237 struct vpage *vp;
9238 9238
9239 9239 ASSERT(seg->s_szc == 0 ||
9240 9240 sameprot(seg, a, pgsz));
9241 9241 vp = &svd->vpage[seg_page(seg, a)];
9242 9242 if ((VPP_PROT(vp) & protchk) == 0) {
9243 9243 error = EACCES;
9244 9244 goto out;
9245 9245 }
9246 9246 if (wcont && (VPP_PROT(vp) & PROT_WRITE)) {
9247 9247 wlen += pgsz;
9248 9248 } else {
9249 9249 wcont = 0;
9250 9250 ASSERT(rw == S_READ);
9251 9251 }
9252 9252 }
9253 9253 }
9254 9254 ASSERT(rw == S_READ || wlen == lpgeaddr - lpgaddr);
9255 9255 ASSERT(rw == S_WRITE || wlen <= lpgeaddr - lpgaddr);
9256 9256 }
9257 9257
9258 9258 /*
9259 9259 * Only build large page adjusted shadow list if we expect to insert
9260 9260 * it into pcache. For large enough pages it's a big overhead to
9261 9261 * create a shadow list of the entire large page. But this overhead
9262 9262 * should be amortized over repeated pcache hits on subsequent reuse
9263 9263 * of this shadow list (IO into any range within this shadow list will
9264 9264 * find it in pcache since we large page align the request for pcache
9265 9265 * lookups). pcache performance is improved with bigger shadow lists
9266 9266 * as it reduces the time to pcache the entire big segment and reduces
9267 9267 * pcache chain length.
9268 9268 */
9269 9269 if (seg_pinsert_check(seg, pamp, paddr,
9270 9270 lpgeaddr - lpgaddr, pflags) == SEGP_SUCCESS) {
9271 9271 addr = lpgaddr;
9272 9272 len = lpgeaddr - lpgaddr;
9273 9273 use_pcache = 1;
9274 9274 } else {
9275 9275 use_pcache = 0;
9276 9276 /*
9277 9277 * Since this entry will not be inserted into the pcache, we
9278 9278 * will not do any adjustments to the starting address or
9279 9279 * size of the memory to be locked.
9280 9280 */
9281 9281 adjustpages = 0;
9282 9282 }
9283 9283 npages = btop(len);
9284 9284
9285 9285 pplist = kmem_alloc(sizeof (page_t *) * (npages + 1), KM_SLEEP);
9286 9286 pl = pplist;
9287 9287 *ppp = pplist + adjustpages;
9288 9288 /*
9289 9289 * If use_pcache is 0 this shadow list is not large page adjusted.
9290 9290 * Record this info in the last entry of shadow array so that
9291 9291 * L_PAGEUNLOCK can determine if it should large page adjust the
9292 9292 * address range to find the real range that was locked.
9293 9293 */
9294 9294 pl[npages] = use_pcache ? PCACHE_SHWLIST : NOPCACHE_SHWLIST;
9295 9295
9296 9296 page = seg_page(seg, addr);
9297 9297 anon_index = svd->anon_index + page;
9298 9298
9299 9299 anlock = 0;
9300 9300 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
9301 9301 ASSERT(amp->a_szc >= seg->s_szc);
9302 9302 anpgcnt = page_get_pagecnt(amp->a_szc);
9303 9303 for (a = addr; a < addr + len; a += PAGESIZE, anon_index++) {
9304 9304 struct anon *ap;
9305 9305 struct vnode *vp;
9306 9306 u_offset_t off;
9307 9307
9308 9308 /*
9309 9309 * Lock and unlock anon array only once per large page.
9310 9310 * anon_array_enter() locks the root anon slot according to
9311 9311 * a_szc which can't change while anon map is locked. We lock
9312 9312 * anon the first time through this loop and each time we
9313 9313 * reach anon index that corresponds to a root of a large
9314 9314 * page.
9315 9315 */
9316 9316 if (a == addr || P2PHASE(anon_index, anpgcnt) == 0) {
9317 9317 ASSERT(anlock == 0);
9318 9318 anon_array_enter(amp, anon_index, &cookie);
9319 9319 anlock = 1;
9320 9320 }
9321 9321 ap = anon_get_ptr(amp->ahp, anon_index);
9322 9322
9323 9323 /*
9324 9324 * We must never use seg_pcache for COW pages
9325 9325 * because we might end up with original page still
9326 9326 * lying in seg_pcache even after private page is
9327 9327 * created. This leads to data corruption as
9328 9328 * aio_write refers to the page still in cache
9329 9329 * while all other accesses refer to the private
9330 9330 * page.
9331 9331 */
9332 9332 if (ap == NULL || ap->an_refcnt != 1) {
9333 9333 struct vpage *vpage;
9334 9334
9335 9335 if (seg->s_szc) {
9336 9336 error = EFAULT;
9337 9337 break;
9338 9338 }
9339 9339 if (svd->vpage != NULL) {
9340 9340 vpage = &svd->vpage[seg_page(seg, a)];
9341 9341 } else {
9342 9342 vpage = NULL;
9343 9343 }
9344 9344 ASSERT(anlock);
9345 9345 anon_array_exit(&cookie);
9346 9346 anlock = 0;
9347 9347 pp = NULL;
9348 9348 error = segvn_faultpage(seg->s_as->a_hat, seg, a, 0,
9349 9349 vpage, &pp, 0, F_INVAL, rw, 1);
9350 9350 if (error) {
9351 9351 error = fc_decode(error);
9352 9352 break;
9353 9353 }
9354 9354 anon_array_enter(amp, anon_index, &cookie);
9355 9355 anlock = 1;
9356 9356 ap = anon_get_ptr(amp->ahp, anon_index);
9357 9357 if (ap == NULL || ap->an_refcnt != 1) {
9358 9358 error = EFAULT;
9359 9359 break;
9360 9360 }
9361 9361 }
9362 9362 swap_xlate(ap, &vp, &off);
9363 9363 pp = page_lookup_nowait(vp, off, SE_SHARED);
9364 9364 if (pp == NULL) {
9365 9365 error = EFAULT;
9366 9366 break;
9367 9367 }
9368 9368 if (ap->an_pvp != NULL) {
9369 9369 anon_swap_free(ap, pp);
9370 9370 }
9371 9371 /*
9372 9372 * Unlock anon if this is the last slot in a large page.
9373 9373 */
9374 9374 if (P2PHASE(anon_index, anpgcnt) == anpgcnt - 1) {
9375 9375 ASSERT(anlock);
9376 9376 anon_array_exit(&cookie);
9377 9377 anlock = 0;
9378 9378 }
9379 9379 *pplist++ = pp;
9380 9380 }
9381 9381 if (anlock) { /* Ensure the lock is dropped */
9382 9382 anon_array_exit(&cookie);
9383 9383 }
9384 9384 ANON_LOCK_EXIT(&->a_rwlock);
9385 9385
9386 9386 if (a >= addr + len) {
9387 9387 atomic_add_long((ulong_t *)&svd->softlockcnt, npages);
9388 9388 if (pamp != NULL) {
9389 9389 ASSERT(svd->type == MAP_SHARED);
9390 9390 atomic_add_long((ulong_t *)&pamp->a_softlockcnt,
9391 9391 npages);
9392 9392 wlen = len;
9393 9393 }
9394 9394 if (sftlck_sbase) {
9395 9395 atomic_inc_ulong((ulong_t *)&svd->softlockcnt_sbase);
9396 9396 }
9397 9397 if (sftlck_send) {
9398 9398 atomic_inc_ulong((ulong_t *)&svd->softlockcnt_send);
9399 9399 }
9400 9400 if (use_pcache) {
9401 9401 (void) seg_pinsert(seg, pamp, paddr, len, wlen, pl,
9402 9402 rw, pflags, preclaim_callback);
9403 9403 }
9404 9404 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
9405 9405 TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_FILL_END,
9406 9406 "segvn_pagelock: cache fill seg %p addr %p", seg, addr);
9407 9407 return (0);
9408 9408 }
9409 9409
9410 9410 pplist = pl;
9411 9411 np = ((uintptr_t)(a - addr)) >> PAGESHIFT;
9412 9412 while (np > (uint_t)0) {
9413 9413 ASSERT(PAGE_LOCKED(*pplist));
9414 9414 page_unlock(*pplist);
9415 9415 np--;
9416 9416 pplist++;
9417 9417 }
9418 9418 kmem_free(pl, sizeof (page_t *) * (npages + 1));
9419 9419 out:
9420 9420 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
9421 9421 *ppp = NULL;
9422 9422 TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_MISS_END,
9423 9423 "segvn_pagelock: cache miss seg %p addr %p", seg, addr);
9424 9424 return (error);
9425 9425 }
9426 9426
9427 9427 /*
9428 9428 * purge any cached pages in the I/O page cache
9429 9429 */
9430 9430 static void
9431 9431 segvn_purge(struct seg *seg)
9432 9432 {
9433 9433 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
9434 9434
9435 9435 /*
9436 9436 * pcache is only used by pure anon segments.
9437 9437 */
9438 9438 if (svd->amp == NULL || svd->vp != NULL) {
9439 9439 return;
9440 9440 }
9441 9441
9442 9442 /*
9443 9443 * For MAP_SHARED segments non 0 segment's softlockcnt means
9444 9444 * active IO is still in progress via this segment. So we only
9445 9445 * purge MAP_SHARED segments when their softlockcnt is 0.
9446 9446 */
9447 9447 if (svd->type == MAP_PRIVATE) {
9448 9448 if (svd->softlockcnt) {
9449 9449 seg_ppurge(seg, NULL, 0);
9450 9450 }
9451 9451 } else if (svd->softlockcnt == 0 && svd->amp->a_softlockcnt != 0) {
9452 9452 seg_ppurge(seg, svd->amp, 0);
9453 9453 }
9454 9454 }
9455 9455
9456 9456 /*
9457 9457 * If async argument is not 0 we are called from pcache async thread and don't
9458 9458 * hold AS lock.
9459 9459 */
9460 9460
9461 9461 /*ARGSUSED*/
9462 9462 static int
9463 9463 segvn_reclaim(void *ptag, caddr_t addr, size_t len, struct page **pplist,
9464 9464 enum seg_rw rw, int async)
9465 9465 {
9466 9466 struct seg *seg = (struct seg *)ptag;
9467 9467 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
9468 9468 pgcnt_t np, npages;
9469 9469 struct page **pl;
9470 9470
9471 9471 npages = np = btop(len);
9472 9472 ASSERT(npages);
9473 9473
9474 9474 ASSERT(svd->vp == NULL && svd->amp != NULL);
9475 9475 ASSERT(svd->softlockcnt >= npages);
9476 9476 ASSERT(async || AS_LOCK_HELD(seg->s_as));
9477 9477
9478 9478 pl = pplist;
9479 9479
9480 9480 ASSERT(pl[np] == NOPCACHE_SHWLIST || pl[np] == PCACHE_SHWLIST);
9481 9481 ASSERT(!async || pl[np] == PCACHE_SHWLIST);
9482 9482
9483 9483 while (np > (uint_t)0) {
9484 9484 if (rw == S_WRITE) {
9485 9485 hat_setrefmod(*pplist);
9486 9486 } else {
9487 9487 hat_setref(*pplist);
9488 9488 }
9489 9489 page_unlock(*pplist);
9490 9490 np--;
9491 9491 pplist++;
9492 9492 }
9493 9493
9494 9494 kmem_free(pl, sizeof (page_t *) * (npages + 1));
9495 9495
9496 9496 /*
9497 9497 * If we are pcache async thread we don't hold AS lock. This means if
9498 9498 * softlockcnt drops to 0 after the decrement below address space may
9499 9499 * get freed. We can't allow it since after softlock derement to 0 we
9500 9500 * still need to access as structure for possible wakeup of unmap
9501 9501 * waiters. To prevent the disappearance of as we take this segment
9502 9502 * segfree_syncmtx. segvn_free() also takes this mutex as a barrier to
9503 9503 * make sure this routine completes before segment is freed.
9504 9504 *
9505 9505 * The second complication we have to deal with in async case is a
9506 9506 * possibility of missed wake up of unmap wait thread. When we don't
9507 9507 * hold as lock here we may take a_contents lock before unmap wait
9508 9508 * thread that was first to see softlockcnt was still not 0. As a
9509 9509 * result we'll fail to wake up an unmap wait thread. To avoid this
9510 9510 * race we set nounmapwait flag in as structure if we drop softlockcnt
9511 9511 * to 0 when we were called by pcache async thread. unmapwait thread
9512 9512 * will not block if this flag is set.
9513 9513 */
9514 9514 if (async) {
9515 9515 mutex_enter(&svd->segfree_syncmtx);
9516 9516 }
9517 9517
9518 9518 if (!atomic_add_long_nv((ulong_t *)&svd->softlockcnt, -npages)) {
9519 9519 if (async || AS_ISUNMAPWAIT(seg->s_as)) {
9520 9520 mutex_enter(&seg->s_as->a_contents);
9521 9521 if (async) {
9522 9522 AS_SETNOUNMAPWAIT(seg->s_as);
9523 9523 }
9524 9524 if (AS_ISUNMAPWAIT(seg->s_as)) {
9525 9525 AS_CLRUNMAPWAIT(seg->s_as);
9526 9526 cv_broadcast(&seg->s_as->a_cv);
9527 9527 }
9528 9528 mutex_exit(&seg->s_as->a_contents);
9529 9529 }
9530 9530 }
9531 9531
9532 9532 if (async) {
9533 9533 mutex_exit(&svd->segfree_syncmtx);
9534 9534 }
9535 9535 return (0);
9536 9536 }
9537 9537
9538 9538 /*ARGSUSED*/
9539 9539 static int
9540 9540 shamp_reclaim(void *ptag, caddr_t addr, size_t len, struct page **pplist,
9541 9541 enum seg_rw rw, int async)
9542 9542 {
9543 9543 amp_t *amp = (amp_t *)ptag;
9544 9544 pgcnt_t np, npages;
9545 9545 struct page **pl;
9546 9546
9547 9547 npages = np = btop(len);
9548 9548 ASSERT(npages);
9549 9549 ASSERT(amp->a_softlockcnt >= npages);
9550 9550
9551 9551 pl = pplist;
9552 9552
9553 9553 ASSERT(pl[np] == NOPCACHE_SHWLIST || pl[np] == PCACHE_SHWLIST);
9554 9554 ASSERT(!async || pl[np] == PCACHE_SHWLIST);
9555 9555
9556 9556 while (np > (uint_t)0) {
9557 9557 if (rw == S_WRITE) {
9558 9558 hat_setrefmod(*pplist);
9559 9559 } else {
9560 9560 hat_setref(*pplist);
9561 9561 }
9562 9562 page_unlock(*pplist);
9563 9563 np--;
9564 9564 pplist++;
9565 9565 }
9566 9566
9567 9567 kmem_free(pl, sizeof (page_t *) * (npages + 1));
9568 9568
9569 9569 /*
9570 9570 * If somebody sleeps in anonmap_purge() wake them up if a_softlockcnt
9571 9571 * drops to 0. anon map can't be freed until a_softlockcnt drops to 0
9572 9572 * and anonmap_purge() acquires a_purgemtx.
9573 9573 */
9574 9574 mutex_enter(&->a_purgemtx);
9575 9575 if (!atomic_add_long_nv((ulong_t *)&->a_softlockcnt, -npages) &&
9576 9576 amp->a_purgewait) {
9577 9577 amp->a_purgewait = 0;
9578 9578 cv_broadcast(&->a_purgecv);
9579 9579 }
9580 9580 mutex_exit(&->a_purgemtx);
9581 9581 return (0);
9582 9582 }
9583 9583
9584 9584 /*
9585 9585 * get a memory ID for an addr in a given segment
9586 9586 *
9587 9587 * XXX only creates PAGESIZE pages if anon slots are not initialized.
9588 9588 * At fault time they will be relocated into larger pages.
9589 9589 */
9590 9590 static int
9591 9591 segvn_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp)
9592 9592 {
9593 9593 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
9594 9594 struct anon *ap = NULL;
9595 9595 ulong_t anon_index;
9596 9596 struct anon_map *amp;
9597 9597 anon_sync_obj_t cookie;
9598 9598
9599 9599 if (svd->type == MAP_PRIVATE) {
9600 9600 memidp->val[0] = (uintptr_t)seg->s_as;
9601 9601 memidp->val[1] = (uintptr_t)addr;
9602 9602 return (0);
9603 9603 }
9604 9604
9605 9605 if (svd->type == MAP_SHARED) {
9606 9606 if (svd->vp) {
9607 9607 memidp->val[0] = (uintptr_t)svd->vp;
9608 9608 memidp->val[1] = (u_longlong_t)svd->offset +
9609 9609 (uintptr_t)(addr - seg->s_base);
9610 9610 return (0);
9611 9611 } else {
9612 9612
9613 9613 SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
9614 9614 if ((amp = svd->amp) != NULL) {
9615 9615 anon_index = svd->anon_index +
9616 9616 seg_page(seg, addr);
9617 9617 }
9618 9618 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
9619 9619
9620 9620 ASSERT(amp != NULL);
9621 9621
9622 9622 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
9623 9623 anon_array_enter(amp, anon_index, &cookie);
9624 9624 ap = anon_get_ptr(amp->ahp, anon_index);
9625 9625 if (ap == NULL) {
9626 9626 page_t *pp;
9627 9627
9628 9628 pp = anon_zero(seg, addr, &ap, svd->cred);
9629 9629 if (pp == NULL) {
9630 9630 anon_array_exit(&cookie);
9631 9631 ANON_LOCK_EXIT(&->a_rwlock);
9632 9632 return (ENOMEM);
9633 9633 }
9634 9634 ASSERT(anon_get_ptr(amp->ahp, anon_index)
9635 9635 == NULL);
9636 9636 (void) anon_set_ptr(amp->ahp, anon_index,
9637 9637 ap, ANON_SLEEP);
9638 9638 page_unlock(pp);
9639 9639 }
9640 9640
9641 9641 anon_array_exit(&cookie);
9642 9642 ANON_LOCK_EXIT(&->a_rwlock);
9643 9643
9644 9644 memidp->val[0] = (uintptr_t)ap;
9645 9645 memidp->val[1] = (uintptr_t)addr & PAGEOFFSET;
9646 9646 return (0);
9647 9647 }
9648 9648 }
9649 9649 return (EINVAL);
9650 9650 }
9651 9651
9652 9652 static int
9653 9653 sameprot(struct seg *seg, caddr_t a, size_t len)
9654 9654 {
9655 9655 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
9656 9656 struct vpage *vpage;
9657 9657 spgcnt_t pages = btop(len);
9658 9658 uint_t prot;
9659 9659
9660 9660 if (svd->pageprot == 0)
9661 9661 return (1);
9662 9662
9663 9663 ASSERT(svd->vpage != NULL);
9664 9664
9665 9665 vpage = &svd->vpage[seg_page(seg, a)];
9666 9666 prot = VPP_PROT(vpage);
9667 9667 vpage++;
9668 9668 pages--;
9669 9669 while (pages-- > 0) {
9670 9670 if (prot != VPP_PROT(vpage))
9671 9671 return (0);
9672 9672 vpage++;
9673 9673 }
9674 9674 return (1);
9675 9675 }
9676 9676
9677 9677 /*
9678 9678 * Get memory allocation policy info for specified address in given segment
9679 9679 */
9680 9680 static lgrp_mem_policy_info_t *
9681 9681 segvn_getpolicy(struct seg *seg, caddr_t addr)
9682 9682 {
9683 9683 struct anon_map *amp;
9684 9684 ulong_t anon_index;
9685 9685 lgrp_mem_policy_info_t *policy_info;
9686 9686 struct segvn_data *svn_data;
9687 9687 u_offset_t vn_off;
9688 9688 vnode_t *vp;
9689 9689
9690 9690 ASSERT(seg != NULL);
9691 9691
9692 9692 svn_data = (struct segvn_data *)seg->s_data;
9693 9693 if (svn_data == NULL)
9694 9694 return (NULL);
9695 9695
9696 9696 /*
9697 9697 * Get policy info for private or shared memory
9698 9698 */
9699 9699 if (svn_data->type != MAP_SHARED) {
9700 9700 if (svn_data->tr_state != SEGVN_TR_ON) {
9701 9701 policy_info = &svn_data->policy_info;
9702 9702 } else {
9703 9703 policy_info = &svn_data->tr_policy_info;
9704 9704 ASSERT(policy_info->mem_policy ==
9705 9705 LGRP_MEM_POLICY_NEXT_SEG);
9706 9706 }
9707 9707 } else {
9708 9708 amp = svn_data->amp;
9709 9709 anon_index = svn_data->anon_index + seg_page(seg, addr);
9710 9710 vp = svn_data->vp;
9711 9711 vn_off = svn_data->offset + (uintptr_t)(addr - seg->s_base);
9712 9712 policy_info = lgrp_shm_policy_get(amp, anon_index, vp, vn_off);
9713 9713 }
9714 9714
9715 9715 return (policy_info);
9716 9716 }
9717 9717
9718 9718 /*ARGSUSED*/
9719 9719 static int
9720 9720 segvn_capable(struct seg *seg, segcapability_t capability)
9721 9721 {
9722 9722 return (0);
9723 9723 }
9724 9724
9725 9725 /*
9726 9726 * Bind text vnode segment to an amp. If we bind successfully mappings will be
9727 9727 * established to per vnode mapping per lgroup amp pages instead of to vnode
9728 9728 * pages. There's one amp per vnode text mapping per lgroup. Many processes
9729 9729 * may share the same text replication amp. If a suitable amp doesn't already
9730 9730 * exist in svntr hash table create a new one. We may fail to bind to amp if
9731 9731 * segment is not eligible for text replication. Code below first checks for
9732 9732 * these conditions. If binding is successful segment tr_state is set to on
9733 9733 * and svd->amp points to the amp to use. Otherwise tr_state is set to off and
9734 9734 * svd->amp remains as NULL.
9735 9735 */
9736 9736 static void
9737 9737 segvn_textrepl(struct seg *seg)
9738 9738 {
9739 9739 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
9740 9740 vnode_t *vp = svd->vp;
9741 9741 u_offset_t off = svd->offset;
9742 9742 size_t size = seg->s_size;
9743 9743 u_offset_t eoff = off + size;
9744 9744 uint_t szc = seg->s_szc;
9745 9745 ulong_t hash = SVNTR_HASH_FUNC(vp);
9746 9746 svntr_t *svntrp;
9747 9747 struct vattr va;
9748 9748 proc_t *p = seg->s_as->a_proc;
9749 9749 lgrp_id_t lgrp_id;
9750 9750 lgrp_id_t olid;
9751 9751 int first;
9752 9752 struct anon_map *amp;
9753 9753
9754 9754 ASSERT(AS_LOCK_HELD(seg->s_as));
9755 9755 ASSERT(SEGVN_WRITE_HELD(seg->s_as, &svd->lock));
9756 9756 ASSERT(p != NULL);
9757 9757 ASSERT(svd->tr_state == SEGVN_TR_INIT);
9758 9758 ASSERT(!HAT_IS_REGION_COOKIE_VALID(svd->rcookie));
9759 9759 ASSERT(svd->flags & MAP_TEXT);
9760 9760 ASSERT(svd->type == MAP_PRIVATE);
9761 9761 ASSERT(vp != NULL && svd->amp == NULL);
9762 9762 ASSERT(!svd->pageprot && !(svd->prot & PROT_WRITE));
9763 9763 ASSERT(!(svd->flags & MAP_NORESERVE) && svd->swresv == 0);
9764 9764 ASSERT(seg->s_as != &kas);
9765 9765 ASSERT(off < eoff);
9766 9766 ASSERT(svntr_hashtab != NULL);
9767 9767
9768 9768 /*
9769 9769 * If numa optimizations are no longer desired bail out.
9770 9770 */
9771 9771 if (!lgrp_optimizations()) {
9772 9772 svd->tr_state = SEGVN_TR_OFF;
9773 9773 return;
9774 9774 }
9775 9775
9776 9776 /*
9777 9777 * Avoid creating anon maps with size bigger than the file size.
9778 9778 * If VOP_GETATTR() call fails bail out.
9779 9779 */
9780 9780 va.va_mask = AT_SIZE | AT_MTIME | AT_CTIME;
9781 9781 if (VOP_GETATTR(vp, &va, 0, svd->cred, NULL) != 0) {
9782 9782 svd->tr_state = SEGVN_TR_OFF;
9783 9783 SEGVN_TR_ADDSTAT(gaerr);
9784 9784 return;
9785 9785 }
9786 9786 if (btopr(va.va_size) < btopr(eoff)) {
9787 9787 svd->tr_state = SEGVN_TR_OFF;
9788 9788 SEGVN_TR_ADDSTAT(overmap);
9789 9789 return;
9790 9790 }
9791 9791
9792 9792 /*
9793 9793 * VVMEXEC may not be set yet if exec() prefaults text segment. Set
9794 9794 * this flag now before vn_is_mapped(V_WRITE) so that MAP_SHARED
9795 9795 * mapping that checks if trcache for this vnode needs to be
9796 9796 * invalidated can't miss us.
9797 9797 */
9798 9798 if (!(vp->v_flag & VVMEXEC)) {
9799 9799 mutex_enter(&vp->v_lock);
9800 9800 vp->v_flag |= VVMEXEC;
9801 9801 mutex_exit(&vp->v_lock);
9802 9802 }
9803 9803 mutex_enter(&svntr_hashtab[hash].tr_lock);
9804 9804 /*
9805 9805 * Bail out if potentially MAP_SHARED writable mappings exist to this
9806 9806 * vnode. We don't want to use old file contents from existing
9807 9807 * replicas if this mapping was established after the original file
9808 9808 * was changed.
9809 9809 */
9810 9810 if (vn_is_mapped(vp, V_WRITE)) {
9811 9811 mutex_exit(&svntr_hashtab[hash].tr_lock);
9812 9812 svd->tr_state = SEGVN_TR_OFF;
9813 9813 SEGVN_TR_ADDSTAT(wrcnt);
9814 9814 return;
9815 9815 }
9816 9816 svntrp = svntr_hashtab[hash].tr_head;
9817 9817 for (; svntrp != NULL; svntrp = svntrp->tr_next) {
9818 9818 ASSERT(svntrp->tr_refcnt != 0);
9819 9819 if (svntrp->tr_vp != vp) {
9820 9820 continue;
9821 9821 }
9822 9822
9823 9823 /*
9824 9824 * Bail out if the file or its attributes were changed after
9825 9825 * this replication entry was created since we need to use the
9826 9826 * latest file contents. Note that mtime test alone is not
9827 9827 * sufficient because a user can explicitly change mtime via
9828 9828 * utimes(2) interfaces back to the old value after modifiying
9829 9829 * the file contents. To detect this case we also have to test
9830 9830 * ctime which among other things records the time of the last
9831 9831 * mtime change by utimes(2). ctime is not changed when the file
9832 9832 * is only read or executed so we expect that typically existing
9833 9833 * replication amp's can be used most of the time.
9834 9834 */
9835 9835 if (!svntrp->tr_valid ||
9836 9836 svntrp->tr_mtime.tv_sec != va.va_mtime.tv_sec ||
9837 9837 svntrp->tr_mtime.tv_nsec != va.va_mtime.tv_nsec ||
9838 9838 svntrp->tr_ctime.tv_sec != va.va_ctime.tv_sec ||
9839 9839 svntrp->tr_ctime.tv_nsec != va.va_ctime.tv_nsec) {
9840 9840 mutex_exit(&svntr_hashtab[hash].tr_lock);
9841 9841 svd->tr_state = SEGVN_TR_OFF;
9842 9842 SEGVN_TR_ADDSTAT(stale);
9843 9843 return;
9844 9844 }
9845 9845 /*
9846 9846 * if off, eoff and szc match current segment we found the
9847 9847 * existing entry we can use.
9848 9848 */
9849 9849 if (svntrp->tr_off == off && svntrp->tr_eoff == eoff &&
9850 9850 svntrp->tr_szc == szc) {
9851 9851 break;
9852 9852 }
9853 9853 /*
9854 9854 * Don't create different but overlapping in file offsets
9855 9855 * entries to avoid replication of the same file pages more
9856 9856 * than once per lgroup.
9857 9857 */
9858 9858 if ((off >= svntrp->tr_off && off < svntrp->tr_eoff) ||
9859 9859 (eoff > svntrp->tr_off && eoff <= svntrp->tr_eoff)) {
9860 9860 mutex_exit(&svntr_hashtab[hash].tr_lock);
9861 9861 svd->tr_state = SEGVN_TR_OFF;
9862 9862 SEGVN_TR_ADDSTAT(overlap);
9863 9863 return;
9864 9864 }
9865 9865 }
9866 9866 /*
9867 9867 * If we didn't find existing entry create a new one.
9868 9868 */
9869 9869 if (svntrp == NULL) {
9870 9870 svntrp = kmem_cache_alloc(svntr_cache, KM_NOSLEEP);
9871 9871 if (svntrp == NULL) {
9872 9872 mutex_exit(&svntr_hashtab[hash].tr_lock);
9873 9873 svd->tr_state = SEGVN_TR_OFF;
9874 9874 SEGVN_TR_ADDSTAT(nokmem);
9875 9875 return;
9876 9876 }
9877 9877 #ifdef DEBUG
9878 9878 {
9879 9879 lgrp_id_t i;
9880 9880 for (i = 0; i < NLGRPS_MAX; i++) {
9881 9881 ASSERT(svntrp->tr_amp[i] == NULL);
9882 9882 }
9883 9883 }
9884 9884 #endif /* DEBUG */
9885 9885 svntrp->tr_vp = vp;
9886 9886 svntrp->tr_off = off;
9887 9887 svntrp->tr_eoff = eoff;
9888 9888 svntrp->tr_szc = szc;
9889 9889 svntrp->tr_valid = 1;
9890 9890 svntrp->tr_mtime = va.va_mtime;
9891 9891 svntrp->tr_ctime = va.va_ctime;
9892 9892 svntrp->tr_refcnt = 0;
9893 9893 svntrp->tr_next = svntr_hashtab[hash].tr_head;
9894 9894 svntr_hashtab[hash].tr_head = svntrp;
9895 9895 }
9896 9896 first = 1;
9897 9897 again:
9898 9898 /*
9899 9899 * We want to pick a replica with pages on main thread's (t_tid = 1,
9900 9900 * aka T1) lgrp. Currently text replication is only optimized for
9901 9901 * workloads that either have all threads of a process on the same
9902 9902 * lgrp or execute their large text primarily on main thread.
9903 9903 */
9904 9904 lgrp_id = p->p_t1_lgrpid;
9905 9905 if (lgrp_id == LGRP_NONE) {
9906 9906 /*
9907 9907 * In case exec() prefaults text on non main thread use
9908 9908 * current thread lgrpid. It will become main thread anyway
9909 9909 * soon.
9910 9910 */
9911 9911 lgrp_id = lgrp_home_id(curthread);
9912 9912 }
9913 9913 /*
9914 9914 * Set p_tr_lgrpid to lgrpid if it hasn't been set yet. Otherwise
9915 9915 * just set it to NLGRPS_MAX if it's different from current process T1
9916 9916 * home lgrp. p_tr_lgrpid is used to detect if process uses text
9917 9917 * replication and T1 new home is different from lgrp used for text
9918 9918 * replication. When this happens asyncronous segvn thread rechecks if
9919 9919 * segments should change lgrps used for text replication. If we fail
9920 9920 * to set p_tr_lgrpid with atomic_cas_32 then set it to NLGRPS_MAX
9921 9921 * without cas if it's not already NLGRPS_MAX and not equal lgrp_id
9922 9922 * we want to use. We don't need to use cas in this case because
9923 9923 * another thread that races in between our non atomic check and set
9924 9924 * may only change p_tr_lgrpid to NLGRPS_MAX at this point.
9925 9925 */
9926 9926 ASSERT(lgrp_id != LGRP_NONE && lgrp_id < NLGRPS_MAX);
9927 9927 olid = p->p_tr_lgrpid;
9928 9928 if (lgrp_id != olid && olid != NLGRPS_MAX) {
9929 9929 lgrp_id_t nlid = (olid == LGRP_NONE) ? lgrp_id : NLGRPS_MAX;
9930 9930 if (atomic_cas_32((uint32_t *)&p->p_tr_lgrpid, olid, nlid) !=
9931 9931 olid) {
9932 9932 olid = p->p_tr_lgrpid;
9933 9933 ASSERT(olid != LGRP_NONE);
9934 9934 if (olid != lgrp_id && olid != NLGRPS_MAX) {
9935 9935 p->p_tr_lgrpid = NLGRPS_MAX;
9936 9936 }
9937 9937 }
9938 9938 ASSERT(p->p_tr_lgrpid != LGRP_NONE);
9939 9939 membar_producer();
9940 9940 /*
9941 9941 * lgrp_move_thread() won't schedule async recheck after
9942 9942 * p->p_t1_lgrpid update unless p->p_tr_lgrpid is not
9943 9943 * LGRP_NONE. Recheck p_t1_lgrpid once now that p->p_tr_lgrpid
9944 9944 * is not LGRP_NONE.
9945 9945 */
9946 9946 if (first && p->p_t1_lgrpid != LGRP_NONE &&
9947 9947 p->p_t1_lgrpid != lgrp_id) {
9948 9948 first = 0;
9949 9949 goto again;
9950 9950 }
9951 9951 }
9952 9952 /*
9953 9953 * If no amp was created yet for lgrp_id create a new one as long as
9954 9954 * we have enough memory to afford it.
9955 9955 */
9956 9956 if ((amp = svntrp->tr_amp[lgrp_id]) == NULL) {
9957 9957 size_t trmem = atomic_add_long_nv(&segvn_textrepl_bytes, size);
9958 9958 if (trmem > segvn_textrepl_max_bytes) {
9959 9959 SEGVN_TR_ADDSTAT(normem);
9960 9960 goto fail;
9961 9961 }
9962 9962 if (anon_try_resv_zone(size, NULL) == 0) {
9963 9963 SEGVN_TR_ADDSTAT(noanon);
9964 9964 goto fail;
9965 9965 }
9966 9966 amp = anonmap_alloc(size, size, ANON_NOSLEEP);
9967 9967 if (amp == NULL) {
9968 9968 anon_unresv_zone(size, NULL);
9969 9969 SEGVN_TR_ADDSTAT(nokmem);
9970 9970 goto fail;
9971 9971 }
9972 9972 ASSERT(amp->refcnt == 1);
9973 9973 amp->a_szc = szc;
9974 9974 svntrp->tr_amp[lgrp_id] = amp;
9975 9975 SEGVN_TR_ADDSTAT(newamp);
9976 9976 }
9977 9977 svntrp->tr_refcnt++;
9978 9978 ASSERT(svd->svn_trnext == NULL);
9979 9979 ASSERT(svd->svn_trprev == NULL);
9980 9980 svd->svn_trnext = svntrp->tr_svnhead;
9981 9981 svd->svn_trprev = NULL;
9982 9982 if (svntrp->tr_svnhead != NULL) {
9983 9983 svntrp->tr_svnhead->svn_trprev = svd;
9984 9984 }
9985 9985 svntrp->tr_svnhead = svd;
9986 9986 ASSERT(amp->a_szc == szc && amp->size == size && amp->swresv == size);
9987 9987 ASSERT(amp->refcnt >= 1);
9988 9988 svd->amp = amp;
9989 9989 svd->anon_index = 0;
9990 9990 svd->tr_policy_info.mem_policy = LGRP_MEM_POLICY_NEXT_SEG;
9991 9991 svd->tr_policy_info.mem_lgrpid = lgrp_id;
9992 9992 svd->tr_state = SEGVN_TR_ON;
9993 9993 mutex_exit(&svntr_hashtab[hash].tr_lock);
9994 9994 SEGVN_TR_ADDSTAT(repl);
9995 9995 return;
9996 9996 fail:
9997 9997 ASSERT(segvn_textrepl_bytes >= size);
9998 9998 atomic_add_long(&segvn_textrepl_bytes, -size);
9999 9999 ASSERT(svntrp != NULL);
10000 10000 ASSERT(svntrp->tr_amp[lgrp_id] == NULL);
10001 10001 if (svntrp->tr_refcnt == 0) {
10002 10002 ASSERT(svntrp == svntr_hashtab[hash].tr_head);
10003 10003 svntr_hashtab[hash].tr_head = svntrp->tr_next;
10004 10004 mutex_exit(&svntr_hashtab[hash].tr_lock);
10005 10005 kmem_cache_free(svntr_cache, svntrp);
10006 10006 } else {
10007 10007 mutex_exit(&svntr_hashtab[hash].tr_lock);
10008 10008 }
10009 10009 svd->tr_state = SEGVN_TR_OFF;
10010 10010 }
10011 10011
10012 10012 /*
10013 10013 * Convert seg back to regular vnode mapping seg by unbinding it from its text
10014 10014 * replication amp. This routine is most typically called when segment is
10015 10015 * unmapped but can also be called when segment no longer qualifies for text
10016 10016 * replication (e.g. due to protection changes). If unload_unmap is set use
10017 10017 * HAT_UNLOAD_UNMAP flag in hat_unload_callback(). If we are the last user of
10018 10018 * svntr free all its anon maps and remove it from the hash table.
10019 10019 */
10020 10020 static void
10021 10021 segvn_textunrepl(struct seg *seg, int unload_unmap)
10022 10022 {
10023 10023 struct segvn_data *svd = (struct segvn_data *)seg->s_data;
10024 10024 vnode_t *vp = svd->vp;
10025 10025 u_offset_t off = svd->offset;
10026 10026 size_t size = seg->s_size;
10027 10027 u_offset_t eoff = off + size;
10028 10028 uint_t szc = seg->s_szc;
10029 10029 ulong_t hash = SVNTR_HASH_FUNC(vp);
10030 10030 svntr_t *svntrp;
10031 10031 svntr_t **prv_svntrp;
10032 10032 lgrp_id_t lgrp_id = svd->tr_policy_info.mem_lgrpid;
10033 10033 lgrp_id_t i;
10034 10034
10035 10035 ASSERT(AS_LOCK_HELD(seg->s_as));
10036 10036 ASSERT(AS_WRITE_HELD(seg->s_as) ||
10037 10037 SEGVN_WRITE_HELD(seg->s_as, &svd->lock));
10038 10038 ASSERT(svd->tr_state == SEGVN_TR_ON);
10039 10039 ASSERT(!HAT_IS_REGION_COOKIE_VALID(svd->rcookie));
10040 10040 ASSERT(svd->amp != NULL);
10041 10041 ASSERT(svd->amp->refcnt >= 1);
10042 10042 ASSERT(svd->anon_index == 0);
10043 10043 ASSERT(lgrp_id != LGRP_NONE && lgrp_id < NLGRPS_MAX);
10044 10044 ASSERT(svntr_hashtab != NULL);
10045 10045
10046 10046 mutex_enter(&svntr_hashtab[hash].tr_lock);
10047 10047 prv_svntrp = &svntr_hashtab[hash].tr_head;
10048 10048 for (; (svntrp = *prv_svntrp) != NULL; prv_svntrp = &svntrp->tr_next) {
10049 10049 ASSERT(svntrp->tr_refcnt != 0);
10050 10050 if (svntrp->tr_vp == vp && svntrp->tr_off == off &&
10051 10051 svntrp->tr_eoff == eoff && svntrp->tr_szc == szc) {
10052 10052 break;
10053 10053 }
10054 10054 }
10055 10055 if (svntrp == NULL) {
10056 10056 panic("segvn_textunrepl: svntr record not found");
10057 10057 }
10058 10058 if (svntrp->tr_amp[lgrp_id] != svd->amp) {
10059 10059 panic("segvn_textunrepl: amp mismatch");
10060 10060 }
10061 10061 svd->tr_state = SEGVN_TR_OFF;
10062 10062 svd->amp = NULL;
10063 10063 if (svd->svn_trprev == NULL) {
10064 10064 ASSERT(svntrp->tr_svnhead == svd);
10065 10065 svntrp->tr_svnhead = svd->svn_trnext;
10066 10066 if (svntrp->tr_svnhead != NULL) {
10067 10067 svntrp->tr_svnhead->svn_trprev = NULL;
10068 10068 }
10069 10069 svd->svn_trnext = NULL;
10070 10070 } else {
10071 10071 svd->svn_trprev->svn_trnext = svd->svn_trnext;
10072 10072 if (svd->svn_trnext != NULL) {
10073 10073 svd->svn_trnext->svn_trprev = svd->svn_trprev;
10074 10074 svd->svn_trnext = NULL;
10075 10075 }
10076 10076 svd->svn_trprev = NULL;
10077 10077 }
10078 10078 if (--svntrp->tr_refcnt) {
10079 10079 mutex_exit(&svntr_hashtab[hash].tr_lock);
10080 10080 goto done;
10081 10081 }
10082 10082 *prv_svntrp = svntrp->tr_next;
10083 10083 mutex_exit(&svntr_hashtab[hash].tr_lock);
10084 10084 for (i = 0; i < NLGRPS_MAX; i++) {
10085 10085 struct anon_map *amp = svntrp->tr_amp[i];
10086 10086 if (amp == NULL) {
10087 10087 continue;
10088 10088 }
10089 10089 ASSERT(amp->refcnt == 1);
10090 10090 ASSERT(amp->swresv == size);
10091 10091 ASSERT(amp->size == size);
10092 10092 ASSERT(amp->a_szc == szc);
10093 10093 if (amp->a_szc != 0) {
10094 10094 anon_free_pages(amp->ahp, 0, size, szc);
10095 10095 } else {
10096 10096 anon_free(amp->ahp, 0, size);
10097 10097 }
10098 10098 svntrp->tr_amp[i] = NULL;
10099 10099 ASSERT(segvn_textrepl_bytes >= size);
10100 10100 atomic_add_long(&segvn_textrepl_bytes, -size);
10101 10101 anon_unresv_zone(amp->swresv, NULL);
10102 10102 amp->refcnt = 0;
10103 10103 anonmap_free(amp);
10104 10104 }
10105 10105 kmem_cache_free(svntr_cache, svntrp);
10106 10106 done:
10107 10107 hat_unload_callback(seg->s_as->a_hat, seg->s_base, size,
10108 10108 unload_unmap ? HAT_UNLOAD_UNMAP : 0, NULL);
10109 10109 }
10110 10110
10111 10111 /*
10112 10112 * This is called when a MAP_SHARED writable mapping is created to a vnode
10113 10113 * that is currently used for execution (VVMEXEC flag is set). In this case we
10114 10114 * need to prevent further use of existing replicas.
10115 10115 */
10116 10116 static void
10117 10117 segvn_inval_trcache(vnode_t *vp)
10118 10118 {
10119 10119 ulong_t hash = SVNTR_HASH_FUNC(vp);
10120 10120 svntr_t *svntrp;
10121 10121
10122 10122 ASSERT(vp->v_flag & VVMEXEC);
10123 10123
10124 10124 if (svntr_hashtab == NULL) {
10125 10125 return;
10126 10126 }
10127 10127
10128 10128 mutex_enter(&svntr_hashtab[hash].tr_lock);
10129 10129 svntrp = svntr_hashtab[hash].tr_head;
10130 10130 for (; svntrp != NULL; svntrp = svntrp->tr_next) {
10131 10131 ASSERT(svntrp->tr_refcnt != 0);
10132 10132 if (svntrp->tr_vp == vp && svntrp->tr_valid) {
10133 10133 svntrp->tr_valid = 0;
10134 10134 }
10135 10135 }
10136 10136 mutex_exit(&svntr_hashtab[hash].tr_lock);
10137 10137 }
10138 10138
10139 10139 static void
10140 10140 segvn_trasync_thread(void)
10141 10141 {
10142 10142 callb_cpr_t cpr_info;
10143 10143 kmutex_t cpr_lock; /* just for CPR stuff */
10144 10144
10145 10145 mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
10146 10146
10147 10147 CALLB_CPR_INIT(&cpr_info, &cpr_lock,
10148 10148 callb_generic_cpr, "segvn_async");
10149 10149
10150 10150 if (segvn_update_textrepl_interval == 0) {
10151 10151 segvn_update_textrepl_interval = segvn_update_tr_time * hz;
10152 10152 } else {
10153 10153 segvn_update_textrepl_interval *= hz;
10154 10154 }
10155 10155 (void) timeout(segvn_trupdate_wakeup, NULL,
10156 10156 segvn_update_textrepl_interval);
10157 10157
10158 10158 for (;;) {
10159 10159 mutex_enter(&cpr_lock);
10160 10160 CALLB_CPR_SAFE_BEGIN(&cpr_info);
10161 10161 mutex_exit(&cpr_lock);
10162 10162 sema_p(&segvn_trasync_sem);
10163 10163 mutex_enter(&cpr_lock);
10164 10164 CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock);
10165 10165 mutex_exit(&cpr_lock);
10166 10166 segvn_trupdate();
10167 10167 }
10168 10168 }
10169 10169
10170 10170 static uint64_t segvn_lgrp_trthr_migrs_snpsht = 0;
10171 10171
10172 10172 static void
10173 10173 segvn_trupdate_wakeup(void *dummy)
10174 10174 {
10175 10175 uint64_t cur_lgrp_trthr_migrs = lgrp_get_trthr_migrations();
10176 10176
10177 10177 if (cur_lgrp_trthr_migrs != segvn_lgrp_trthr_migrs_snpsht) {
10178 10178 segvn_lgrp_trthr_migrs_snpsht = cur_lgrp_trthr_migrs;
10179 10179 sema_v(&segvn_trasync_sem);
10180 10180 }
10181 10181
10182 10182 if (!segvn_disable_textrepl_update &&
10183 10183 segvn_update_textrepl_interval != 0) {
10184 10184 (void) timeout(segvn_trupdate_wakeup, dummy,
10185 10185 segvn_update_textrepl_interval);
10186 10186 }
10187 10187 }
10188 10188
10189 10189 static void
10190 10190 segvn_trupdate(void)
10191 10191 {
10192 10192 ulong_t hash;
10193 10193 svntr_t *svntrp;
10194 10194 segvn_data_t *svd;
10195 10195
10196 10196 ASSERT(svntr_hashtab != NULL);
10197 10197
10198 10198 for (hash = 0; hash < svntr_hashtab_sz; hash++) {
10199 10199 mutex_enter(&svntr_hashtab[hash].tr_lock);
10200 10200 svntrp = svntr_hashtab[hash].tr_head;
10201 10201 for (; svntrp != NULL; svntrp = svntrp->tr_next) {
10202 10202 ASSERT(svntrp->tr_refcnt != 0);
10203 10203 svd = svntrp->tr_svnhead;
10204 10204 for (; svd != NULL; svd = svd->svn_trnext) {
10205 10205 segvn_trupdate_seg(svd->seg, svd, svntrp,
10206 10206 hash);
10207 10207 }
10208 10208 }
10209 10209 mutex_exit(&svntr_hashtab[hash].tr_lock);
10210 10210 }
10211 10211 }
10212 10212
10213 10213 static void
10214 10214 segvn_trupdate_seg(struct seg *seg, segvn_data_t *svd, svntr_t *svntrp,
10215 10215 ulong_t hash)
10216 10216 {
10217 10217 proc_t *p;
10218 10218 lgrp_id_t lgrp_id;
10219 10219 struct as *as;
10220 10220 size_t size;
10221 10221 struct anon_map *amp;
10222 10222
10223 10223 ASSERT(svd->vp != NULL);
10224 10224 ASSERT(svd->vp == svntrp->tr_vp);
10225 10225 ASSERT(svd->offset == svntrp->tr_off);
10226 10226 ASSERT(svd->offset + seg->s_size == svntrp->tr_eoff);
10227 10227 ASSERT(seg != NULL);
10228 10228 ASSERT(svd->seg == seg);
10229 10229 ASSERT(seg->s_data == (void *)svd);
10230 10230 ASSERT(seg->s_szc == svntrp->tr_szc);
10231 10231 ASSERT(svd->tr_state == SEGVN_TR_ON);
10232 10232 ASSERT(!HAT_IS_REGION_COOKIE_VALID(svd->rcookie));
10233 10233 ASSERT(svd->amp != NULL);
10234 10234 ASSERT(svd->tr_policy_info.mem_policy == LGRP_MEM_POLICY_NEXT_SEG);
10235 10235 ASSERT(svd->tr_policy_info.mem_lgrpid != LGRP_NONE);
10236 10236 ASSERT(svd->tr_policy_info.mem_lgrpid < NLGRPS_MAX);
10237 10237 ASSERT(svntrp->tr_amp[svd->tr_policy_info.mem_lgrpid] == svd->amp);
10238 10238 ASSERT(svntrp->tr_refcnt != 0);
10239 10239 ASSERT(mutex_owned(&svntr_hashtab[hash].tr_lock));
10240 10240
10241 10241 as = seg->s_as;
10242 10242 ASSERT(as != NULL && as != &kas);
10243 10243 p = as->a_proc;
10244 10244 ASSERT(p != NULL);
10245 10245 ASSERT(p->p_tr_lgrpid != LGRP_NONE);
10246 10246 lgrp_id = p->p_t1_lgrpid;
10247 10247 if (lgrp_id == LGRP_NONE) {
10248 10248 return;
10249 10249 }
10250 10250 ASSERT(lgrp_id < NLGRPS_MAX);
10251 10251 if (svd->tr_policy_info.mem_lgrpid == lgrp_id) {
10252 10252 return;
10253 10253 }
10254 10254
10255 10255 /*
10256 10256 * Use tryenter locking since we are locking as/seg and svntr hash
10257 10257 * lock in reverse from syncrounous thread order.
10258 10258 */
10259 10259 if (!AS_LOCK_TRYENTER(as, RW_READER)) {
10260 10260 SEGVN_TR_ADDSTAT(nolock);
10261 10261 if (segvn_lgrp_trthr_migrs_snpsht) {
10262 10262 segvn_lgrp_trthr_migrs_snpsht = 0;
10263 10263 }
10264 10264 return;
10265 10265 }
10266 10266 if (!SEGVN_LOCK_TRYENTER(seg->s_as, &svd->lock, RW_WRITER)) {
10267 10267 AS_LOCK_EXIT(as);
10268 10268 SEGVN_TR_ADDSTAT(nolock);
10269 10269 if (segvn_lgrp_trthr_migrs_snpsht) {
10270 10270 segvn_lgrp_trthr_migrs_snpsht = 0;
10271 10271 }
10272 10272 return;
10273 10273 }
10274 10274 size = seg->s_size;
10275 10275 if (svntrp->tr_amp[lgrp_id] == NULL) {
10276 10276 size_t trmem = atomic_add_long_nv(&segvn_textrepl_bytes, size);
10277 10277 if (trmem > segvn_textrepl_max_bytes) {
10278 10278 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
10279 10279 AS_LOCK_EXIT(as);
10280 10280 atomic_add_long(&segvn_textrepl_bytes, -size);
10281 10281 SEGVN_TR_ADDSTAT(normem);
10282 10282 return;
10283 10283 }
10284 10284 if (anon_try_resv_zone(size, NULL) == 0) {
10285 10285 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
10286 10286 AS_LOCK_EXIT(as);
10287 10287 atomic_add_long(&segvn_textrepl_bytes, -size);
10288 10288 SEGVN_TR_ADDSTAT(noanon);
10289 10289 return;
10290 10290 }
10291 10291 amp = anonmap_alloc(size, size, KM_NOSLEEP);
10292 10292 if (amp == NULL) {
10293 10293 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
10294 10294 AS_LOCK_EXIT(as);
10295 10295 atomic_add_long(&segvn_textrepl_bytes, -size);
10296 10296 anon_unresv_zone(size, NULL);
10297 10297 SEGVN_TR_ADDSTAT(nokmem);
10298 10298 return;
10299 10299 }
10300 10300 ASSERT(amp->refcnt == 1);
10301 10301 amp->a_szc = seg->s_szc;
10302 10302 svntrp->tr_amp[lgrp_id] = amp;
10303 10303 }
10304 10304 /*
10305 10305 * We don't need to drop the bucket lock but here we give other
10306 10306 * threads a chance. svntr and svd can't be unlinked as long as
10307 10307 * segment lock is held as a writer and AS held as well. After we
10308 10308 * retake bucket lock we'll continue from where we left. We'll be able
10309 10309 * to reach the end of either list since new entries are always added
10310 10310 * to the beginning of the lists.
10311 10311 */
10312 10312 mutex_exit(&svntr_hashtab[hash].tr_lock);
10313 10313 hat_unload_callback(as->a_hat, seg->s_base, size, 0, NULL);
10314 10314 mutex_enter(&svntr_hashtab[hash].tr_lock);
10315 10315
10316 10316 ASSERT(svd->tr_state == SEGVN_TR_ON);
10317 10317 ASSERT(svd->amp != NULL);
10318 10318 ASSERT(svd->tr_policy_info.mem_policy == LGRP_MEM_POLICY_NEXT_SEG);
10319 10319 ASSERT(svd->tr_policy_info.mem_lgrpid != lgrp_id);
10320 10320 ASSERT(svd->amp != svntrp->tr_amp[lgrp_id]);
10321 10321
10322 10322 svd->tr_policy_info.mem_lgrpid = lgrp_id;
10323 10323 svd->amp = svntrp->tr_amp[lgrp_id];
10324 10324 p->p_tr_lgrpid = NLGRPS_MAX;
10325 10325 SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
10326 10326 AS_LOCK_EXIT(as);
10327 10327
10328 10328 ASSERT(svntrp->tr_refcnt != 0);
10329 10329 ASSERT(svd->vp == svntrp->tr_vp);
10330 10330 ASSERT(svd->tr_policy_info.mem_lgrpid == lgrp_id);
10331 10331 ASSERT(svd->amp != NULL && svd->amp == svntrp->tr_amp[lgrp_id]);
10332 10332 ASSERT(svd->seg == seg);
10333 10333 ASSERT(svd->tr_state == SEGVN_TR_ON);
10334 10334
10335 10335 SEGVN_TR_ADDSTAT(asyncrepl);
10336 10336 }
↓ open down ↓ |
1243 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX