This source file includes following definitions.
- shm_init
- findkey
- newseg
- sys_shmget
- killseg
- sys_shmctl
- insert_attach
- remove_attach
- shm_map
- sys_shmat
- shm_open
- shm_close
- sys_shmdt
- shm_swap_in
- shm_swap
1
2
3
4
5
6
7
8 #include <linux/errno.h>
9 #include <linux/sched.h>
10 #include <linux/mm.h>
11 #include <linux/ipc.h>
12 #include <linux/shm.h>
13 #include <linux/stat.h>
14 #include <linux/malloc.h>
15
16 #include <asm/segment.h>
17 #include <asm/pgtable.h>
18
19 extern int ipcperms (struct ipc_perm *ipcp, short shmflg);
20 extern unsigned long get_swap_page (void);
21 static int findkey (key_t key);
22 static int newseg (key_t key, int shmflg, int size);
23 static int shm_map (struct vm_area_struct *shmd);
24 static void killseg (int id);
25 static void shm_open (struct vm_area_struct *shmd);
26 static void shm_close (struct vm_area_struct *shmd);
27 static pte_t shm_swap_in(struct vm_area_struct *, unsigned long, unsigned long);
28
29 static int shm_tot = 0;
30 static int shm_rss = 0;
31 static int shm_swp = 0;
32 static int max_shmid = 0;
33 static struct wait_queue *shm_lock = NULL;
34 static struct shmid_ds *shm_segs[SHMMNI];
35
36 static unsigned short shm_seq = 0;
37
38
39 static ulong swap_attempts = 0;
40 static ulong swap_successes = 0;
41 static ulong used_segs = 0;
42
43 void shm_init (void)
44 {
45 int id;
46
47 for (id = 0; id < SHMMNI; id++)
48 shm_segs[id] = (struct shmid_ds *) IPC_UNUSED;
49 shm_tot = shm_rss = shm_seq = max_shmid = used_segs = 0;
50 shm_lock = NULL;
51 return;
52 }
53
54 static int findkey (key_t key)
55 {
56 int id;
57 struct shmid_ds *shp;
58
59 for (id = 0; id <= max_shmid; id++) {
60 while ((shp = shm_segs[id]) == IPC_NOID)
61 sleep_on (&shm_lock);
62 if (shp == IPC_UNUSED)
63 continue;
64 if (key == shp->shm_perm.key)
65 return id;
66 }
67 return -1;
68 }
69
70
71
72
73 static int newseg (key_t key, int shmflg, int size)
74 {
75 struct shmid_ds *shp;
76 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
77 int id, i;
78
79 if (size < SHMMIN)
80 return -EINVAL;
81 if (shm_tot + numpages >= SHMALL)
82 return -ENOSPC;
83 for (id = 0; id < SHMMNI; id++)
84 if (shm_segs[id] == IPC_UNUSED) {
85 shm_segs[id] = (struct shmid_ds *) IPC_NOID;
86 goto found;
87 }
88 return -ENOSPC;
89
90 found:
91 shp = (struct shmid_ds *) kmalloc (sizeof (*shp), GFP_KERNEL);
92 if (!shp) {
93 shm_segs[id] = (struct shmid_ds *) IPC_UNUSED;
94 if (shm_lock)
95 wake_up (&shm_lock);
96 return -ENOMEM;
97 }
98
99 shp->shm_pages = (ulong *) kmalloc (numpages*sizeof(ulong),GFP_KERNEL);
100 if (!shp->shm_pages) {
101 shm_segs[id] = (struct shmid_ds *) IPC_UNUSED;
102 if (shm_lock)
103 wake_up (&shm_lock);
104 kfree(shp);
105 return -ENOMEM;
106 }
107
108 for (i = 0; i < numpages; shp->shm_pages[i++] = 0);
109 shm_tot += numpages;
110 shp->shm_perm.key = key;
111 shp->shm_perm.mode = (shmflg & S_IRWXUGO);
112 shp->shm_perm.cuid = shp->shm_perm.uid = current->euid;
113 shp->shm_perm.cgid = shp->shm_perm.gid = current->egid;
114 shp->shm_perm.seq = shm_seq;
115 shp->shm_segsz = size;
116 shp->shm_cpid = current->pid;
117 shp->attaches = NULL;
118 shp->shm_lpid = shp->shm_nattch = 0;
119 shp->shm_atime = shp->shm_dtime = 0;
120 shp->shm_ctime = CURRENT_TIME;
121 shp->shm_npages = numpages;
122
123 if (id > max_shmid)
124 max_shmid = id;
125 shm_segs[id] = shp;
126 used_segs++;
127 if (shm_lock)
128 wake_up (&shm_lock);
129 return (unsigned int) shp->shm_perm.seq * SHMMNI + id;
130 }
131
132 asmlinkage int sys_shmget (key_t key, int size, int shmflg)
133 {
134 struct shmid_ds *shp;
135 int id = 0;
136
137 if (size < 0 || size > SHMMAX)
138 return -EINVAL;
139 if (key == IPC_PRIVATE)
140 return newseg(key, shmflg, size);
141 if ((id = findkey (key)) == -1) {
142 if (!(shmflg & IPC_CREAT))
143 return -ENOENT;
144 return newseg(key, shmflg, size);
145 }
146 if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL))
147 return -EEXIST;
148 shp = shm_segs[id];
149 if (shp->shm_perm.mode & SHM_DEST)
150 return -EIDRM;
151 if (size > shp->shm_segsz)
152 return -EINVAL;
153 if (ipcperms (&shp->shm_perm, shmflg))
154 return -EACCES;
155 return (unsigned int) shp->shm_perm.seq * SHMMNI + id;
156 }
157
158
159
160
161
162 static void killseg (int id)
163 {
164 struct shmid_ds *shp;
165 int i, numpages;
166
167 shp = shm_segs[id];
168 if (shp == IPC_NOID || shp == IPC_UNUSED) {
169 printk ("shm nono: killseg called on unused seg id=%d\n", id);
170 return;
171 }
172 shp->shm_perm.seq++;
173 shm_seq = (shm_seq+1) % ((unsigned)(1<<31)/SHMMNI);
174 shm_segs[id] = (struct shmid_ds *) IPC_UNUSED;
175 used_segs--;
176 if (id == max_shmid)
177 while (max_shmid && (shm_segs[--max_shmid] == IPC_UNUSED));
178 if (!shp->shm_pages) {
179 printk ("shm nono: killseg shp->pages=NULL. id=%d\n", id);
180 return;
181 }
182 numpages = shp->shm_npages;
183 for (i = 0; i < numpages ; i++) {
184 pte_t pte;
185 pte_val(pte) = shp->shm_pages[i];
186 if (pte_none(pte))
187 continue;
188 if (pte_present(pte)) {
189 free_page (pte_page(pte));
190 shm_rss--;
191 } else {
192 swap_free(pte_val(pte));
193 shm_swp--;
194 }
195 }
196 kfree(shp->shm_pages);
197 shm_tot -= numpages;
198 kfree(shp);
199 return;
200 }
201
202 asmlinkage int sys_shmctl (int shmid, int cmd, struct shmid_ds *buf)
203 {
204 struct shmid_ds tbuf;
205 struct shmid_ds *shp;
206 struct ipc_perm *ipcp;
207 int id, err;
208
209 if (cmd < 0 || shmid < 0)
210 return -EINVAL;
211 if (cmd == IPC_SET) {
212 if (!buf)
213 return -EFAULT;
214 err = verify_area (VERIFY_READ, buf, sizeof (*buf));
215 if (err)
216 return err;
217 memcpy_fromfs (&tbuf, buf, sizeof (*buf));
218 }
219
220 switch (cmd) {
221 case IPC_INFO:
222 {
223 struct shminfo shminfo;
224 if (!buf)
225 return -EFAULT;
226 shminfo.shmmni = SHMMNI;
227 shminfo.shmmax = SHMMAX;
228 shminfo.shmmin = SHMMIN;
229 shminfo.shmall = SHMALL;
230 shminfo.shmseg = SHMSEG;
231 err = verify_area (VERIFY_WRITE, buf, sizeof (struct shminfo));
232 if (err)
233 return err;
234 memcpy_tofs (buf, &shminfo, sizeof(struct shminfo));
235 return max_shmid;
236 }
237 case SHM_INFO:
238 {
239 struct shm_info shm_info;
240 if (!buf)
241 return -EFAULT;
242 err = verify_area (VERIFY_WRITE, buf, sizeof (shm_info));
243 if (err)
244 return err;
245 shm_info.used_ids = used_segs;
246 shm_info.shm_rss = shm_rss;
247 shm_info.shm_tot = shm_tot;
248 shm_info.shm_swp = shm_swp;
249 shm_info.swap_attempts = swap_attempts;
250 shm_info.swap_successes = swap_successes;
251 memcpy_tofs (buf, &shm_info, sizeof(shm_info));
252 return max_shmid;
253 }
254 case SHM_STAT:
255 if (!buf)
256 return -EFAULT;
257 err = verify_area (VERIFY_WRITE, buf, sizeof (*buf));
258 if (err)
259 return err;
260 if (shmid > max_shmid)
261 return -EINVAL;
262 shp = shm_segs[shmid];
263 if (shp == IPC_UNUSED || shp == IPC_NOID)
264 return -EINVAL;
265 if (ipcperms (&shp->shm_perm, S_IRUGO))
266 return -EACCES;
267 id = (unsigned int) shp->shm_perm.seq * SHMMNI + shmid;
268 tbuf.shm_perm = shp->shm_perm;
269 tbuf.shm_segsz = shp->shm_segsz;
270 tbuf.shm_atime = shp->shm_atime;
271 tbuf.shm_dtime = shp->shm_dtime;
272 tbuf.shm_ctime = shp->shm_ctime;
273 tbuf.shm_cpid = shp->shm_cpid;
274 tbuf.shm_lpid = shp->shm_lpid;
275 tbuf.shm_nattch = shp->shm_nattch;
276 memcpy_tofs (buf, &tbuf, sizeof(*buf));
277 return id;
278 }
279
280 shp = shm_segs[id = (unsigned int) shmid % SHMMNI];
281 if (shp == IPC_UNUSED || shp == IPC_NOID)
282 return -EINVAL;
283 if (shp->shm_perm.seq != (unsigned int) shmid / SHMMNI)
284 return -EIDRM;
285 ipcp = &shp->shm_perm;
286
287 switch (cmd) {
288 case SHM_UNLOCK:
289 if (!suser())
290 return -EPERM;
291 if (!(ipcp->mode & SHM_LOCKED))
292 return -EINVAL;
293 ipcp->mode &= ~SHM_LOCKED;
294 break;
295 case SHM_LOCK:
296
297
298
299 if (!suser())
300 return -EPERM;
301 if (ipcp->mode & SHM_LOCKED)
302 return -EINVAL;
303 ipcp->mode |= SHM_LOCKED;
304 break;
305 case IPC_STAT:
306 if (ipcperms (ipcp, S_IRUGO))
307 return -EACCES;
308 if (!buf)
309 return -EFAULT;
310 err = verify_area (VERIFY_WRITE, buf, sizeof (*buf));
311 if (err)
312 return err;
313 tbuf.shm_perm = shp->shm_perm;
314 tbuf.shm_segsz = shp->shm_segsz;
315 tbuf.shm_atime = shp->shm_atime;
316 tbuf.shm_dtime = shp->shm_dtime;
317 tbuf.shm_ctime = shp->shm_ctime;
318 tbuf.shm_cpid = shp->shm_cpid;
319 tbuf.shm_lpid = shp->shm_lpid;
320 tbuf.shm_nattch = shp->shm_nattch;
321 memcpy_tofs (buf, &tbuf, sizeof(*buf));
322 break;
323 case IPC_SET:
324 if (suser() || current->euid == shp->shm_perm.uid ||
325 current->euid == shp->shm_perm.cuid) {
326 ipcp->uid = tbuf.shm_perm.uid;
327 ipcp->gid = tbuf.shm_perm.gid;
328 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
329 | (tbuf.shm_perm.mode & S_IRWXUGO);
330 shp->shm_ctime = CURRENT_TIME;
331 break;
332 }
333 return -EPERM;
334 case IPC_RMID:
335 if (suser() || current->euid == shp->shm_perm.uid ||
336 current->euid == shp->shm_perm.cuid) {
337 shp->shm_perm.mode |= SHM_DEST;
338 if (shp->shm_nattch <= 0)
339 killseg (id);
340 break;
341 }
342 return -EPERM;
343 default:
344 return -EINVAL;
345 }
346 return 0;
347 }
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362 static struct vm_operations_struct shm_vm_ops = {
363 shm_open,
364 shm_close,
365 NULL,
366 NULL,
367 NULL,
368 NULL,
369 NULL,
370 NULL,
371 NULL,
372 shm_swap_in
373 };
374
375
376 static inline void insert_attach (struct shmid_ds * shp, struct vm_area_struct * shmd)
377 {
378 struct vm_area_struct * attaches;
379
380 if ((attaches = shp->attaches)) {
381 shmd->vm_next_share = attaches;
382 shmd->vm_prev_share = attaches->vm_prev_share;
383 shmd->vm_prev_share->vm_next_share = shmd;
384 attaches->vm_prev_share = shmd;
385 } else
386 shp->attaches = shmd->vm_next_share = shmd->vm_prev_share = shmd;
387 }
388
389
390 static inline void remove_attach (struct shmid_ds * shp, struct vm_area_struct * shmd)
391 {
392 if (shmd->vm_next_share == shmd) {
393 if (shp->attaches != shmd) {
394 printk("shm_close: shm segment (id=%ld) attach list inconsistent\n",
395 SWP_OFFSET(shmd->vm_pte) & SHM_ID_MASK);
396 printk("shm_close: %08lx-%08lx %c%c%c%c %08lx %08lx\n",
397 shmd->vm_start, shmd->vm_end,
398 shmd->vm_flags & VM_READ ? 'r' : '-',
399 shmd->vm_flags & VM_WRITE ? 'w' : '-',
400 shmd->vm_flags & VM_EXEC ? 'x' : '-',
401 shmd->vm_flags & VM_MAYSHARE ? 's' : 'p',
402 shmd->vm_offset, shmd->vm_pte);
403 }
404 shp->attaches = NULL;
405 } else {
406 if (shp->attaches == shmd)
407 shp->attaches = shmd->vm_next_share;
408 shmd->vm_prev_share->vm_next_share = shmd->vm_next_share;
409 shmd->vm_next_share->vm_prev_share = shmd->vm_prev_share;
410 }
411 }
412
413
414
415
416
417 static int shm_map (struct vm_area_struct *shmd)
418 {
419 pgd_t *page_dir;
420 pmd_t *page_middle;
421 pte_t *page_table;
422 unsigned long tmp, shm_sgn;
423
424
425 do_munmap(shmd->vm_start, shmd->vm_end - shmd->vm_start);
426
427
428 current->mm->total_vm += (shmd->vm_end - shmd->vm_start) >> PAGE_SHIFT;
429 insert_vm_struct(current, shmd);
430 merge_segments(current, shmd->vm_start, shmd->vm_end);
431
432
433 shm_sgn = shmd->vm_pte +
434 SWP_ENTRY(0, (shmd->vm_offset >> PAGE_SHIFT) << SHM_IDX_SHIFT);
435 for (tmp = shmd->vm_start;
436 tmp < shmd->vm_end;
437 tmp += PAGE_SIZE, shm_sgn += SWP_ENTRY(0, 1 << SHM_IDX_SHIFT))
438 {
439 page_dir = pgd_offset(shmd->vm_mm,tmp);
440 page_middle = pmd_alloc(page_dir,tmp);
441 if (!page_middle)
442 return -ENOMEM;
443 page_table = pte_alloc(page_middle,tmp);
444 if (!page_table)
445 return -ENOMEM;
446 set_pte(page_table, __pte(shm_sgn));
447 }
448 invalidate_range(shmd->vm_mm, shmd->vm_start, shmd->vm_end);
449 return 0;
450 }
451
452
453
454
455 asmlinkage int sys_shmat (int shmid, char *shmaddr, int shmflg, ulong *raddr)
456 {
457 struct shmid_ds *shp;
458 struct vm_area_struct *shmd;
459 int err;
460 unsigned int id;
461 unsigned long addr;
462
463 if (shmid < 0) {
464
465 return -EINVAL;
466 }
467
468 shp = shm_segs[id = (unsigned int) shmid % SHMMNI];
469 if (shp == IPC_UNUSED || shp == IPC_NOID) {
470
471 return -EINVAL;
472 }
473
474 if (!(addr = (ulong) shmaddr)) {
475 if (shmflg & SHM_REMAP)
476 return -EINVAL;
477 if (!(addr = get_unmapped_area(0, shp->shm_segsz)))
478 return -ENOMEM;
479 } else if (addr & (SHMLBA-1)) {
480 if (shmflg & SHM_RND)
481 addr &= ~(SHMLBA-1);
482 else
483 return -EINVAL;
484 }
485
486
487
488
489 if (addr < current->mm->start_stack &&
490 addr > current->mm->start_stack - PAGE_SIZE*(shp->shm_npages + 4))
491 {
492
493 return -EINVAL;
494 }
495 if (!(shmflg & SHM_REMAP))
496 if ((shmd = find_vma_intersection(current, addr, addr + shp->shm_segsz))) {
497
498
499 return -EINVAL;
500 }
501
502 if (ipcperms(&shp->shm_perm, shmflg & SHM_RDONLY ? S_IRUGO : S_IRUGO|S_IWUGO))
503 return -EACCES;
504 if (shp->shm_perm.seq != (unsigned int) shmid / SHMMNI)
505 return -EIDRM;
506
507 shmd = (struct vm_area_struct *) kmalloc (sizeof(*shmd), GFP_KERNEL);
508 if (!shmd)
509 return -ENOMEM;
510 if ((shp != shm_segs[id]) || (shp->shm_perm.seq != (unsigned int) shmid / SHMMNI)) {
511 kfree(shmd);
512 return -EIDRM;
513 }
514
515 shmd->vm_pte = SWP_ENTRY(SHM_SWP_TYPE, id);
516 shmd->vm_start = addr;
517 shmd->vm_end = addr + shp->shm_npages * PAGE_SIZE;
518 shmd->vm_mm = current->mm;
519 shmd->vm_page_prot = (shmflg & SHM_RDONLY) ? PAGE_READONLY : PAGE_SHARED;
520 shmd->vm_flags = VM_SHM | VM_MAYSHARE | VM_SHARED
521 | VM_MAYREAD | VM_MAYEXEC | VM_READ | VM_EXEC
522 | ((shmflg & SHM_RDONLY) ? 0 : VM_MAYWRITE | VM_WRITE);
523 shmd->vm_next_share = shmd->vm_prev_share = NULL;
524 shmd->vm_inode = NULL;
525 shmd->vm_offset = 0;
526 shmd->vm_ops = &shm_vm_ops;
527
528 shp->shm_nattch++;
529 if ((err = shm_map (shmd))) {
530 if (--shp->shm_nattch <= 0 && shp->shm_perm.mode & SHM_DEST)
531 killseg(id);
532 kfree(shmd);
533 return err;
534 }
535
536 insert_attach(shp,shmd);
537
538 shp->shm_lpid = current->pid;
539 shp->shm_atime = CURRENT_TIME;
540
541 *raddr = addr;
542 return 0;
543 }
544
545
546 static void shm_open (struct vm_area_struct *shmd)
547 {
548 unsigned int id;
549 struct shmid_ds *shp;
550
551 id = SWP_OFFSET(shmd->vm_pte) & SHM_ID_MASK;
552 shp = shm_segs[id];
553 if (shp == IPC_UNUSED) {
554 printk("shm_open: unused id=%d PANIC\n", id);
555 return;
556 }
557 insert_attach(shp,shmd);
558 shp->shm_nattch++;
559 shp->shm_atime = CURRENT_TIME;
560 shp->shm_lpid = current->pid;
561 }
562
563
564
565
566
567
568
569 static void shm_close (struct vm_area_struct *shmd)
570 {
571 struct shmid_ds *shp;
572 int id;
573
574
575 id = SWP_OFFSET(shmd->vm_pte) & SHM_ID_MASK;
576 shp = shm_segs[id];
577 remove_attach(shp,shmd);
578 shp->shm_lpid = current->pid;
579 shp->shm_dtime = CURRENT_TIME;
580 if (--shp->shm_nattch <= 0 && shp->shm_perm.mode & SHM_DEST)
581 killseg (id);
582 }
583
584
585
586
587
588 asmlinkage int sys_shmdt (char *shmaddr)
589 {
590 struct vm_area_struct *shmd, *shmdnext;
591
592 for (shmd = current->mm->mmap; shmd; shmd = shmdnext) {
593 shmdnext = shmd->vm_next;
594 if (shmd->vm_ops == &shm_vm_ops
595 && shmd->vm_start - shmd->vm_offset == (ulong) shmaddr)
596 do_munmap(shmd->vm_start, shmd->vm_end - shmd->vm_start);
597 }
598 return 0;
599 }
600
601
602
603
604 static pte_t shm_swap_in(struct vm_area_struct * shmd, unsigned long offset, unsigned long code)
605 {
606 pte_t pte;
607 struct shmid_ds *shp;
608 unsigned int id, idx;
609
610 id = SWP_OFFSET(code) & SHM_ID_MASK;
611 if (id != (SWP_OFFSET(shmd->vm_pte) & SHM_ID_MASK)) {
612 printk ("shm_swap_in: code id = %d and shmd id = %ld differ\n",
613 id, SWP_OFFSET(shmd->vm_pte) & SHM_ID_MASK);
614 return BAD_PAGE;
615 }
616 if (id > max_shmid) {
617 printk ("shm_swap_in: id=%d too big. proc mem corrupted\n", id);
618 return BAD_PAGE;
619 }
620 shp = shm_segs[id];
621 if (shp == IPC_UNUSED || shp == IPC_NOID) {
622 printk ("shm_swap_in: id=%d invalid. Race.\n", id);
623 return BAD_PAGE;
624 }
625 idx = (SWP_OFFSET(code) >> SHM_IDX_SHIFT) & SHM_IDX_MASK;
626 if (idx != (offset >> PAGE_SHIFT)) {
627 printk ("shm_swap_in: code idx = %u and shmd idx = %lu differ\n",
628 idx, offset >> PAGE_SHIFT);
629 return BAD_PAGE;
630 }
631 if (idx >= shp->shm_npages) {
632 printk ("shm_swap_in : too large page index. id=%d\n", id);
633 return BAD_PAGE;
634 }
635
636 pte_val(pte) = shp->shm_pages[idx];
637 if (!pte_present(pte)) {
638 unsigned long page = get_free_page(GFP_KERNEL);
639 if (!page) {
640 oom(current);
641 return BAD_PAGE;
642 }
643 pte_val(pte) = shp->shm_pages[idx];
644 if (pte_present(pte)) {
645 free_page (page);
646 goto done;
647 }
648 if (!pte_none(pte)) {
649 read_swap_page(pte_val(pte), (char *) page);
650 pte_val(pte) = shp->shm_pages[idx];
651 if (pte_present(pte)) {
652 free_page (page);
653 goto done;
654 }
655 swap_free(pte_val(pte));
656 shm_swp--;
657 }
658 shm_rss++;
659 pte = pte_mkdirty(mk_pte(page, PAGE_SHARED));
660 shp->shm_pages[idx] = pte_val(pte);
661 } else
662 --current->maj_flt;
663
664 done:
665 current->min_flt++;
666 mem_map[MAP_NR(pte_page(pte))].count++;
667 return pte_modify(pte, shmd->vm_page_prot);
668 }
669
670
671
672
673 static unsigned long swap_id = 0;
674 static unsigned long swap_idx = 0;
675
676 int shm_swap (int prio, unsigned long limit)
677 {
678 pte_t page;
679 struct shmid_ds *shp;
680 struct vm_area_struct *shmd;
681 unsigned long swap_nr;
682 unsigned long id, idx;
683 int loop = 0;
684 int counter;
685
686 counter = shm_rss >> prio;
687 if (!counter || !(swap_nr = get_swap_page()))
688 return 0;
689
690 check_id:
691 shp = shm_segs[swap_id];
692 if (shp == IPC_UNUSED || shp == IPC_NOID || shp->shm_perm.mode & SHM_LOCKED ) {
693 next_id:
694 swap_idx = 0;
695 if (++swap_id > max_shmid) {
696 if (loop)
697 goto failed;
698 loop = 1;
699 swap_id = 0;
700 }
701 goto check_id;
702 }
703 id = swap_id;
704
705 check_table:
706 idx = swap_idx++;
707 if (idx >= shp->shm_npages)
708 goto next_id;
709
710 pte_val(page) = shp->shm_pages[idx];
711 if (!pte_present(page))
712 goto check_table;
713 if (pte_page(page) >= limit)
714 goto check_table;
715 swap_attempts++;
716
717 if (--counter < 0) {
718 failed:
719 swap_free (swap_nr);
720 return 0;
721 }
722 if (shp->attaches)
723 for (shmd = shp->attaches; ; ) {
724 do {
725 pgd_t *page_dir;
726 pmd_t *page_middle;
727 pte_t *page_table, pte;
728 unsigned long tmp;
729
730 if ((SWP_OFFSET(shmd->vm_pte) & SHM_ID_MASK) != id) {
731 printk ("shm_swap: id=%ld does not match shmd->vm_pte.id=%ld\n",
732 id, SWP_OFFSET(shmd->vm_pte) & SHM_ID_MASK);
733 continue;
734 }
735 tmp = shmd->vm_start + (idx << PAGE_SHIFT) - shmd->vm_offset;
736 if (!(tmp >= shmd->vm_start && tmp < shmd->vm_end))
737 continue;
738 page_dir = pgd_offset(shmd->vm_mm,tmp);
739 if (pgd_none(*page_dir) || pgd_bad(*page_dir)) {
740 printk("shm_swap: bad pgtbl! id=%ld start=%lx idx=%ld\n",
741 id, shmd->vm_start, idx);
742 pgd_clear(page_dir);
743 continue;
744 }
745 page_middle = pmd_offset(page_dir,tmp);
746 if (pmd_none(*page_middle) || pmd_bad(*page_middle)) {
747 printk("shm_swap: bad pgmid! id=%ld start=%lx idx=%ld\n",
748 id, shmd->vm_start, idx);
749 pmd_clear(page_middle);
750 continue;
751 }
752 page_table = pte_offset(page_middle,tmp);
753 pte = *page_table;
754 if (!pte_present(pte))
755 continue;
756 if (pte_young(pte)) {
757 set_pte(page_table, pte_mkold(pte));
758 continue;
759 }
760 if (pte_page(pte) != pte_page(page))
761 printk("shm_swap_out: page and pte mismatch\n");
762 set_pte(page_table,
763 __pte(shmd->vm_pte + SWP_ENTRY(0, idx << SHM_IDX_SHIFT)));
764 mem_map[MAP_NR(pte_page(pte))].count--;
765 if (shmd->vm_mm->rss > 0)
766 shmd->vm_mm->rss--;
767 invalidate_range(shmd->vm_mm, shmd->vm_start, shmd->vm_end);
768
769 } while (0);
770 if ((shmd = shmd->vm_next_share) == shp->attaches)
771 break;
772 }
773
774 if (mem_map[MAP_NR(pte_page(page))].count != 1)
775 goto check_table;
776 shp->shm_pages[idx] = swap_nr;
777 write_swap_page (swap_nr, (char *) pte_page(page));
778 free_page(pte_page(page));
779 swap_successes++;
780 shm_swp++;
781 shm_rss--;
782 return 1;
783 }