fault.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * S390 version
  3. * Copyright IBM Corp. 1999
  4. * Author(s): Hartmut Penner (hp@de.ibm.com)
  5. * Ulrich Weigand (uweigand@de.ibm.com)
  6. *
  7. * Derived from "arch/i386/mm/fault.c"
  8. * Copyright (C) 1995 Linus Torvalds
  9. */
  10. #include <linux/kernel_stat.h>
  11. #include <linux/perf_event.h>
  12. #include <linux/signal.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/mman.h>
  20. #include <linux/mm.h>
  21. #include <linux/compat.h>
  22. #include <linux/smp.h>
  23. #include <linux/kdebug.h>
  24. #include <linux/init.h>
  25. #include <linux/console.h>
  26. #include <linux/module.h>
  27. #include <linux/hardirq.h>
  28. #include <linux/kprobes.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/hugetlb.h>
  31. #include <asm/asm-offsets.h>
  32. #include <asm/diag.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/irq.h>
  35. #include <asm/mmu_context.h>
  36. #include <asm/facility.h>
  37. #include "../kernel/entry.h"
  38. #define __FAIL_ADDR_MASK -4096L
  39. #define __SUBCODE_MASK 0x0600
  40. #define __PF_RES_FIELD 0x8000000000000000ULL
  41. #define VM_FAULT_BADCONTEXT 0x010000
  42. #define VM_FAULT_BADMAP 0x020000
  43. #define VM_FAULT_BADACCESS 0x040000
  44. #define VM_FAULT_SIGNAL 0x080000
  45. #define VM_FAULT_PFAULT 0x100000
  46. static unsigned long store_indication __read_mostly;
  47. static int __init fault_init(void)
  48. {
  49. if (test_facility(75))
  50. store_indication = 0xc00;
  51. return 0;
  52. }
  53. early_initcall(fault_init);
  54. static inline int notify_page_fault(struct pt_regs *regs)
  55. {
  56. int ret = 0;
  57. /* kprobe_running() needs smp_processor_id() */
  58. if (kprobes_built_in() && !user_mode(regs)) {
  59. preempt_disable();
  60. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  61. ret = 1;
  62. preempt_enable();
  63. }
  64. return ret;
  65. }
  66. /*
  67. * Unlock any spinlocks which will prevent us from getting the
  68. * message out.
  69. */
  70. void bust_spinlocks(int yes)
  71. {
  72. if (yes) {
  73. oops_in_progress = 1;
  74. } else {
  75. int loglevel_save = console_loglevel;
  76. console_unblank();
  77. oops_in_progress = 0;
  78. /*
  79. * OK, the message is on the console. Now we call printk()
  80. * without oops_in_progress set so that printk will give klogd
  81. * a poke. Hold onto your hats...
  82. */
  83. console_loglevel = 15;
  84. printk(" ");
  85. console_loglevel = loglevel_save;
  86. }
  87. }
  88. /*
  89. * Returns the address space associated with the fault.
  90. * Returns 0 for kernel space and 1 for user space.
  91. */
  92. static inline int user_space_fault(struct pt_regs *regs)
  93. {
  94. unsigned long trans_exc_code;
  95. /*
  96. * The lowest two bits of the translation exception
  97. * identification indicate which paging table was used.
  98. */
  99. trans_exc_code = regs->int_parm_long & 3;
  100. if (trans_exc_code == 3) /* home space -> kernel */
  101. return 0;
  102. if (user_mode(regs))
  103. return 1;
  104. if (trans_exc_code == 2) /* secondary space -> set_fs */
  105. return current->thread.mm_segment.ar4;
  106. if (current->flags & PF_VCPU)
  107. return 1;
  108. return 0;
  109. }
  110. static int bad_address(void *p)
  111. {
  112. unsigned long dummy;
  113. return probe_kernel_address((unsigned long *)p, dummy);
  114. }
  115. static void dump_pagetable(unsigned long asce, unsigned long address)
  116. {
  117. unsigned long *table = __va(asce & PAGE_MASK);
  118. pr_alert("AS:%016lx ", asce);
  119. switch (asce & _ASCE_TYPE_MASK) {
  120. case _ASCE_TYPE_REGION1:
  121. table = table + ((address >> 53) & 0x7ff);
  122. if (bad_address(table))
  123. goto bad;
  124. pr_cont("R1:%016lx ", *table);
  125. if (*table & _REGION_ENTRY_INVALID)
  126. goto out;
  127. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  128. /* fallthrough */
  129. case _ASCE_TYPE_REGION2:
  130. table = table + ((address >> 42) & 0x7ff);
  131. if (bad_address(table))
  132. goto bad;
  133. pr_cont("R2:%016lx ", *table);
  134. if (*table & _REGION_ENTRY_INVALID)
  135. goto out;
  136. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  137. /* fallthrough */
  138. case _ASCE_TYPE_REGION3:
  139. table = table + ((address >> 31) & 0x7ff);
  140. if (bad_address(table))
  141. goto bad;
  142. pr_cont("R3:%016lx ", *table);
  143. if (*table & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE))
  144. goto out;
  145. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  146. /* fallthrough */
  147. case _ASCE_TYPE_SEGMENT:
  148. table = table + ((address >> 20) & 0x7ff);
  149. if (bad_address(table))
  150. goto bad;
  151. pr_cont("S:%016lx ", *table);
  152. if (*table & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE))
  153. goto out;
  154. table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
  155. }
  156. table = table + ((address >> 12) & 0xff);
  157. if (bad_address(table))
  158. goto bad;
  159. pr_cont("P:%016lx ", *table);
  160. out:
  161. pr_cont("\n");
  162. return;
  163. bad:
  164. pr_cont("BAD\n");
  165. }
  166. static void dump_fault_info(struct pt_regs *regs)
  167. {
  168. unsigned long asce;
  169. pr_alert("Fault in ");
  170. switch (regs->int_parm_long & 3) {
  171. case 3:
  172. pr_cont("home space ");
  173. break;
  174. case 2:
  175. pr_cont("secondary space ");
  176. break;
  177. case 1:
  178. pr_cont("access register ");
  179. break;
  180. case 0:
  181. pr_cont("primary space ");
  182. break;
  183. }
  184. pr_cont("mode while using ");
  185. if (!user_space_fault(regs)) {
  186. asce = S390_lowcore.kernel_asce;
  187. pr_cont("kernel ");
  188. }
  189. #ifdef CONFIG_PGSTE
  190. else if ((current->flags & PF_VCPU) && S390_lowcore.gmap) {
  191. struct gmap *gmap = (struct gmap *)S390_lowcore.gmap;
  192. asce = gmap->asce;
  193. pr_cont("gmap ");
  194. }
  195. #endif
  196. else {
  197. asce = S390_lowcore.user_asce;
  198. pr_cont("user ");
  199. }
  200. pr_cont("ASCE.\n");
  201. dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK);
  202. }
  203. static inline void report_user_fault(struct pt_regs *regs, long signr)
  204. {
  205. if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
  206. return;
  207. if (!unhandled_signal(current, signr))
  208. return;
  209. if (!printk_ratelimit())
  210. return;
  211. printk(KERN_ALERT "User process fault: interruption code %04x ilc:%d ",
  212. regs->int_code & 0xffff, regs->int_code >> 17);
  213. print_vma_addr(KERN_CONT "in ", regs->psw.addr);
  214. printk(KERN_CONT "\n");
  215. printk(KERN_ALERT "failing address: %016lx TEID: %016lx\n",
  216. regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long);
  217. dump_fault_info(regs);
  218. show_regs(regs);
  219. }
  220. /*
  221. * Send SIGSEGV to task. This is an external routine
  222. * to keep the stack usage of do_page_fault small.
  223. */
  224. static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
  225. {
  226. struct siginfo si;
  227. report_user_fault(regs, SIGSEGV);
  228. si.si_signo = SIGSEGV;
  229. si.si_code = si_code;
  230. si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
  231. force_sig_info(SIGSEGV, &si, current);
  232. }
  233. static noinline void do_no_context(struct pt_regs *regs)
  234. {
  235. const struct exception_table_entry *fixup;
  236. /* Are we prepared to handle this kernel fault? */
  237. fixup = search_exception_tables(regs->psw.addr);
  238. if (fixup) {
  239. regs->psw.addr = extable_fixup(fixup);
  240. return;
  241. }
  242. /*
  243. * Oops. The kernel tried to access some bad page. We'll have to
  244. * terminate things with extreme prejudice.
  245. */
  246. if (!user_space_fault(regs))
  247. printk(KERN_ALERT "Unable to handle kernel pointer dereference"
  248. " in virtual kernel address space\n");
  249. else
  250. printk(KERN_ALERT "Unable to handle kernel paging request"
  251. " in virtual user address space\n");
  252. printk(KERN_ALERT "failing address: %016lx TEID: %016lx\n",
  253. regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long);
  254. dump_fault_info(regs);
  255. die(regs, "Oops");
  256. do_exit(SIGKILL);
  257. }
  258. static noinline void do_low_address(struct pt_regs *regs)
  259. {
  260. /* Low-address protection hit in kernel mode means
  261. NULL pointer write access in kernel mode. */
  262. if (regs->psw.mask & PSW_MASK_PSTATE) {
  263. /* Low-address protection hit in user mode 'cannot happen'. */
  264. die (regs, "Low-address protection");
  265. do_exit(SIGKILL);
  266. }
  267. do_no_context(regs);
  268. }
  269. static noinline void do_sigbus(struct pt_regs *regs)
  270. {
  271. struct task_struct *tsk = current;
  272. struct siginfo si;
  273. /*
  274. * Send a sigbus, regardless of whether we were in kernel
  275. * or user mode.
  276. */
  277. si.si_signo = SIGBUS;
  278. si.si_errno = 0;
  279. si.si_code = BUS_ADRERR;
  280. si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
  281. force_sig_info(SIGBUS, &si, tsk);
  282. }
  283. static noinline void do_fault_error(struct pt_regs *regs, int fault)
  284. {
  285. int si_code;
  286. switch (fault) {
  287. case VM_FAULT_BADACCESS:
  288. case VM_FAULT_BADMAP:
  289. /* Bad memory access. Check if it is kernel or user space. */
  290. if (user_mode(regs)) {
  291. /* User mode accesses just cause a SIGSEGV */
  292. si_code = (fault == VM_FAULT_BADMAP) ?
  293. SEGV_MAPERR : SEGV_ACCERR;
  294. do_sigsegv(regs, si_code);
  295. return;
  296. }
  297. case VM_FAULT_BADCONTEXT:
  298. case VM_FAULT_PFAULT:
  299. do_no_context(regs);
  300. break;
  301. case VM_FAULT_SIGNAL:
  302. if (!user_mode(regs))
  303. do_no_context(regs);
  304. break;
  305. default: /* fault & VM_FAULT_ERROR */
  306. if (fault & VM_FAULT_OOM) {
  307. if (!user_mode(regs))
  308. do_no_context(regs);
  309. else
  310. pagefault_out_of_memory();
  311. } else if (fault & VM_FAULT_SIGSEGV) {
  312. /* Kernel mode? Handle exceptions or die */
  313. if (!user_mode(regs))
  314. do_no_context(regs);
  315. else
  316. do_sigsegv(regs, SEGV_MAPERR);
  317. } else if (fault & VM_FAULT_SIGBUS) {
  318. /* Kernel mode? Handle exceptions or die */
  319. if (!user_mode(regs))
  320. do_no_context(regs);
  321. else
  322. do_sigbus(regs);
  323. } else
  324. BUG();
  325. break;
  326. }
  327. }
  328. /*
  329. * This routine handles page faults. It determines the address,
  330. * and the problem, and then passes it off to one of the appropriate
  331. * routines.
  332. *
  333. * interruption code (int_code):
  334. * 04 Protection -> Write-Protection (suprression)
  335. * 10 Segment translation -> Not present (nullification)
  336. * 11 Page translation -> Not present (nullification)
  337. * 3b Region third trans. -> Not present (nullification)
  338. */
  339. static inline int do_exception(struct pt_regs *regs, int access)
  340. {
  341. #ifdef CONFIG_PGSTE
  342. struct gmap *gmap;
  343. #endif
  344. struct task_struct *tsk;
  345. struct mm_struct *mm;
  346. struct vm_area_struct *vma;
  347. unsigned long trans_exc_code;
  348. unsigned long address;
  349. unsigned int flags;
  350. int fault;
  351. tsk = current;
  352. /*
  353. * The instruction that caused the program check has
  354. * been nullified. Don't signal single step via SIGTRAP.
  355. */
  356. clear_pt_regs_flag(regs, PIF_PER_TRAP);
  357. if (notify_page_fault(regs))
  358. return 0;
  359. mm = tsk->mm;
  360. trans_exc_code = regs->int_parm_long;
  361. /*
  362. * Verify that the fault happened in user space, that
  363. * we are not in an interrupt and that there is a
  364. * user context.
  365. */
  366. fault = VM_FAULT_BADCONTEXT;
  367. if (unlikely(!user_space_fault(regs) || faulthandler_disabled() || !mm))
  368. goto out;
  369. address = trans_exc_code & __FAIL_ADDR_MASK;
  370. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  371. flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  372. if (user_mode(regs))
  373. flags |= FAULT_FLAG_USER;
  374. if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
  375. flags |= FAULT_FLAG_WRITE;
  376. down_read(&mm->mmap_sem);
  377. #ifdef CONFIG_PGSTE
  378. gmap = (current->flags & PF_VCPU) ?
  379. (struct gmap *) S390_lowcore.gmap : NULL;
  380. if (gmap) {
  381. current->thread.gmap_addr = address;
  382. address = __gmap_translate(gmap, address);
  383. if (address == -EFAULT) {
  384. fault = VM_FAULT_BADMAP;
  385. goto out_up;
  386. }
  387. if (gmap->pfault_enabled)
  388. flags |= FAULT_FLAG_RETRY_NOWAIT;
  389. }
  390. #endif
  391. retry:
  392. fault = VM_FAULT_BADMAP;
  393. vma = find_vma(mm, address);
  394. if (!vma)
  395. goto out_up;
  396. if (unlikely(vma->vm_start > address)) {
  397. if (!(vma->vm_flags & VM_GROWSDOWN))
  398. goto out_up;
  399. if (expand_stack(vma, address))
  400. goto out_up;
  401. }
  402. /*
  403. * Ok, we have a good vm_area for this memory access, so
  404. * we can handle it..
  405. */
  406. fault = VM_FAULT_BADACCESS;
  407. if (unlikely(!(vma->vm_flags & access)))
  408. goto out_up;
  409. if (is_vm_hugetlb_page(vma))
  410. address &= HPAGE_MASK;
  411. /*
  412. * If for any reason at all we couldn't handle the fault,
  413. * make sure we exit gracefully rather than endlessly redo
  414. * the fault.
  415. */
  416. fault = handle_mm_fault(mm, vma, address, flags);
  417. /* No reason to continue if interrupted by SIGKILL. */
  418. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
  419. fault = VM_FAULT_SIGNAL;
  420. goto out;
  421. }
  422. if (unlikely(fault & VM_FAULT_ERROR))
  423. goto out_up;
  424. /*
  425. * Major/minor page fault accounting is only done on the
  426. * initial attempt. If we go through a retry, it is extremely
  427. * likely that the page will be found in page cache at that point.
  428. */
  429. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  430. if (fault & VM_FAULT_MAJOR) {
  431. tsk->maj_flt++;
  432. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  433. regs, address);
  434. } else {
  435. tsk->min_flt++;
  436. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  437. regs, address);
  438. }
  439. if (fault & VM_FAULT_RETRY) {
  440. #ifdef CONFIG_PGSTE
  441. if (gmap && (flags & FAULT_FLAG_RETRY_NOWAIT)) {
  442. /* FAULT_FLAG_RETRY_NOWAIT has been set,
  443. * mmap_sem has not been released */
  444. current->thread.gmap_pfault = 1;
  445. fault = VM_FAULT_PFAULT;
  446. goto out_up;
  447. }
  448. #endif
  449. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  450. * of starvation. */
  451. flags &= ~(FAULT_FLAG_ALLOW_RETRY |
  452. FAULT_FLAG_RETRY_NOWAIT);
  453. flags |= FAULT_FLAG_TRIED;
  454. down_read(&mm->mmap_sem);
  455. goto retry;
  456. }
  457. }
  458. #ifdef CONFIG_PGSTE
  459. if (gmap) {
  460. address = __gmap_link(gmap, current->thread.gmap_addr,
  461. address);
  462. if (address == -EFAULT) {
  463. fault = VM_FAULT_BADMAP;
  464. goto out_up;
  465. }
  466. if (address == -ENOMEM) {
  467. fault = VM_FAULT_OOM;
  468. goto out_up;
  469. }
  470. }
  471. #endif
  472. fault = 0;
  473. out_up:
  474. up_read(&mm->mmap_sem);
  475. out:
  476. return fault;
  477. }
  478. void do_protection_exception(struct pt_regs *regs)
  479. {
  480. unsigned long trans_exc_code;
  481. int fault;
  482. trans_exc_code = regs->int_parm_long;
  483. /*
  484. * Protection exceptions are suppressing, decrement psw address.
  485. * The exception to this rule are aborted transactions, for these
  486. * the PSW already points to the correct location.
  487. */
  488. if (!(regs->int_code & 0x200))
  489. regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
  490. /*
  491. * Check for low-address protection. This needs to be treated
  492. * as a special case because the translation exception code
  493. * field is not guaranteed to contain valid data in this case.
  494. */
  495. if (unlikely(!(trans_exc_code & 4))) {
  496. do_low_address(regs);
  497. return;
  498. }
  499. fault = do_exception(regs, VM_WRITE);
  500. if (unlikely(fault))
  501. do_fault_error(regs, fault);
  502. }
  503. NOKPROBE_SYMBOL(do_protection_exception);
  504. void do_dat_exception(struct pt_regs *regs)
  505. {
  506. int access, fault;
  507. access = VM_READ | VM_EXEC | VM_WRITE;
  508. fault = do_exception(regs, access);
  509. if (unlikely(fault))
  510. do_fault_error(regs, fault);
  511. }
  512. NOKPROBE_SYMBOL(do_dat_exception);
  513. #ifdef CONFIG_PFAULT
  514. /*
  515. * 'pfault' pseudo page faults routines.
  516. */
  517. static int pfault_disable;
  518. static int __init nopfault(char *str)
  519. {
  520. pfault_disable = 1;
  521. return 1;
  522. }
  523. __setup("nopfault", nopfault);
  524. struct pfault_refbk {
  525. u16 refdiagc;
  526. u16 reffcode;
  527. u16 refdwlen;
  528. u16 refversn;
  529. u64 refgaddr;
  530. u64 refselmk;
  531. u64 refcmpmk;
  532. u64 reserved;
  533. } __attribute__ ((packed, aligned(8)));
  534. int pfault_init(void)
  535. {
  536. struct pfault_refbk refbk = {
  537. .refdiagc = 0x258,
  538. .reffcode = 0,
  539. .refdwlen = 5,
  540. .refversn = 2,
  541. .refgaddr = __LC_LPP,
  542. .refselmk = 1ULL << 48,
  543. .refcmpmk = 1ULL << 48,
  544. .reserved = __PF_RES_FIELD };
  545. int rc;
  546. if (pfault_disable)
  547. return -1;
  548. diag_stat_inc(DIAG_STAT_X258);
  549. asm volatile(
  550. " diag %1,%0,0x258\n"
  551. "0: j 2f\n"
  552. "1: la %0,8\n"
  553. "2:\n"
  554. EX_TABLE(0b,1b)
  555. : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
  556. return rc;
  557. }
  558. void pfault_fini(void)
  559. {
  560. struct pfault_refbk refbk = {
  561. .refdiagc = 0x258,
  562. .reffcode = 1,
  563. .refdwlen = 5,
  564. .refversn = 2,
  565. };
  566. if (pfault_disable)
  567. return;
  568. diag_stat_inc(DIAG_STAT_X258);
  569. asm volatile(
  570. " diag %0,0,0x258\n"
  571. "0:\n"
  572. EX_TABLE(0b,0b)
  573. : : "a" (&refbk), "m" (refbk) : "cc");
  574. }
  575. static DEFINE_SPINLOCK(pfault_lock);
  576. static LIST_HEAD(pfault_list);
  577. static void pfault_interrupt(struct ext_code ext_code,
  578. unsigned int param32, unsigned long param64)
  579. {
  580. struct task_struct *tsk;
  581. __u16 subcode;
  582. pid_t pid;
  583. /*
  584. * Get the external interruption subcode & pfault
  585. * initial/completion signal bit. VM stores this
  586. * in the 'cpu address' field associated with the
  587. * external interrupt.
  588. */
  589. subcode = ext_code.subcode;
  590. if ((subcode & 0xff00) != __SUBCODE_MASK)
  591. return;
  592. inc_irq_stat(IRQEXT_PFL);
  593. /* Get the token (= pid of the affected task). */
  594. pid = param64 & LPP_PFAULT_PID_MASK;
  595. rcu_read_lock();
  596. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  597. if (tsk)
  598. get_task_struct(tsk);
  599. rcu_read_unlock();
  600. if (!tsk)
  601. return;
  602. spin_lock(&pfault_lock);
  603. if (subcode & 0x0080) {
  604. /* signal bit is set -> a page has been swapped in by VM */
  605. if (tsk->thread.pfault_wait == 1) {
  606. /* Initial interrupt was faster than the completion
  607. * interrupt. pfault_wait is valid. Set pfault_wait
  608. * back to zero and wake up the process. This can
  609. * safely be done because the task is still sleeping
  610. * and can't produce new pfaults. */
  611. tsk->thread.pfault_wait = 0;
  612. list_del(&tsk->thread.list);
  613. wake_up_process(tsk);
  614. put_task_struct(tsk);
  615. } else {
  616. /* Completion interrupt was faster than initial
  617. * interrupt. Set pfault_wait to -1 so the initial
  618. * interrupt doesn't put the task to sleep.
  619. * If the task is not running, ignore the completion
  620. * interrupt since it must be a leftover of a PFAULT
  621. * CANCEL operation which didn't remove all pending
  622. * completion interrupts. */
  623. if (tsk->state == TASK_RUNNING)
  624. tsk->thread.pfault_wait = -1;
  625. }
  626. } else {
  627. /* signal bit not set -> a real page is missing. */
  628. if (WARN_ON_ONCE(tsk != current))
  629. goto out;
  630. if (tsk->thread.pfault_wait == 1) {
  631. /* Already on the list with a reference: put to sleep */
  632. __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  633. set_tsk_need_resched(tsk);
  634. } else if (tsk->thread.pfault_wait == -1) {
  635. /* Completion interrupt was faster than the initial
  636. * interrupt (pfault_wait == -1). Set pfault_wait
  637. * back to zero and exit. */
  638. tsk->thread.pfault_wait = 0;
  639. } else {
  640. /* Initial interrupt arrived before completion
  641. * interrupt. Let the task sleep.
  642. * An extra task reference is needed since a different
  643. * cpu may set the task state to TASK_RUNNING again
  644. * before the scheduler is reached. */
  645. get_task_struct(tsk);
  646. tsk->thread.pfault_wait = 1;
  647. list_add(&tsk->thread.list, &pfault_list);
  648. __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  649. set_tsk_need_resched(tsk);
  650. }
  651. }
  652. out:
  653. spin_unlock(&pfault_lock);
  654. put_task_struct(tsk);
  655. }
  656. static int pfault_cpu_notify(struct notifier_block *self, unsigned long action,
  657. void *hcpu)
  658. {
  659. struct thread_struct *thread, *next;
  660. struct task_struct *tsk;
  661. switch (action & ~CPU_TASKS_FROZEN) {
  662. case CPU_DEAD:
  663. spin_lock_irq(&pfault_lock);
  664. list_for_each_entry_safe(thread, next, &pfault_list, list) {
  665. thread->pfault_wait = 0;
  666. list_del(&thread->list);
  667. tsk = container_of(thread, struct task_struct, thread);
  668. wake_up_process(tsk);
  669. put_task_struct(tsk);
  670. }
  671. spin_unlock_irq(&pfault_lock);
  672. break;
  673. default:
  674. break;
  675. }
  676. return NOTIFY_OK;
  677. }
  678. static int __init pfault_irq_init(void)
  679. {
  680. int rc;
  681. rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  682. if (rc)
  683. goto out_extint;
  684. rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
  685. if (rc)
  686. goto out_pfault;
  687. irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
  688. hotcpu_notifier(pfault_cpu_notify, 0);
  689. return 0;
  690. out_pfault:
  691. unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  692. out_extint:
  693. pfault_disable = 1;
  694. return rc;
  695. }
  696. early_initcall(pfault_irq_init);
  697. #endif /* CONFIG_PFAULT */