fault.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 & PSW_ADDR_INSN);
  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. unsigned long address;
  237. /* Are we prepared to handle this kernel fault? */
  238. fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
  239. if (fixup) {
  240. regs->psw.addr = extable_fixup(fixup) | PSW_ADDR_AMODE;
  241. return;
  242. }
  243. /*
  244. * Oops. The kernel tried to access some bad page. We'll have to
  245. * terminate things with extreme prejudice.
  246. */
  247. address = regs->int_parm_long & __FAIL_ADDR_MASK;
  248. if (!user_space_fault(regs))
  249. printk(KERN_ALERT "Unable to handle kernel pointer dereference"
  250. " in virtual kernel address space\n");
  251. else
  252. printk(KERN_ALERT "Unable to handle kernel paging request"
  253. " in virtual user address space\n");
  254. printk(KERN_ALERT "failing address: %016lx TEID: %016lx\n",
  255. regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long);
  256. dump_fault_info(regs);
  257. die(regs, "Oops");
  258. do_exit(SIGKILL);
  259. }
  260. static noinline void do_low_address(struct pt_regs *regs)
  261. {
  262. /* Low-address protection hit in kernel mode means
  263. NULL pointer write access in kernel mode. */
  264. if (regs->psw.mask & PSW_MASK_PSTATE) {
  265. /* Low-address protection hit in user mode 'cannot happen'. */
  266. die (regs, "Low-address protection");
  267. do_exit(SIGKILL);
  268. }
  269. do_no_context(regs);
  270. }
  271. static noinline void do_sigbus(struct pt_regs *regs)
  272. {
  273. struct task_struct *tsk = current;
  274. struct siginfo si;
  275. /*
  276. * Send a sigbus, regardless of whether we were in kernel
  277. * or user mode.
  278. */
  279. si.si_signo = SIGBUS;
  280. si.si_errno = 0;
  281. si.si_code = BUS_ADRERR;
  282. si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
  283. force_sig_info(SIGBUS, &si, tsk);
  284. }
  285. static noinline void do_fault_error(struct pt_regs *regs, int fault)
  286. {
  287. int si_code;
  288. switch (fault) {
  289. case VM_FAULT_BADACCESS:
  290. case VM_FAULT_BADMAP:
  291. /* Bad memory access. Check if it is kernel or user space. */
  292. if (user_mode(regs)) {
  293. /* User mode accesses just cause a SIGSEGV */
  294. si_code = (fault == VM_FAULT_BADMAP) ?
  295. SEGV_MAPERR : SEGV_ACCERR;
  296. do_sigsegv(regs, si_code);
  297. return;
  298. }
  299. case VM_FAULT_BADCONTEXT:
  300. case VM_FAULT_PFAULT:
  301. do_no_context(regs);
  302. break;
  303. case VM_FAULT_SIGNAL:
  304. if (!user_mode(regs))
  305. do_no_context(regs);
  306. break;
  307. default: /* fault & VM_FAULT_ERROR */
  308. if (fault & VM_FAULT_OOM) {
  309. if (!user_mode(regs))
  310. do_no_context(regs);
  311. else
  312. pagefault_out_of_memory();
  313. } else if (fault & VM_FAULT_SIGSEGV) {
  314. /* Kernel mode? Handle exceptions or die */
  315. if (!user_mode(regs))
  316. do_no_context(regs);
  317. else
  318. do_sigsegv(regs, SEGV_MAPERR);
  319. } else if (fault & VM_FAULT_SIGBUS) {
  320. /* Kernel mode? Handle exceptions or die */
  321. if (!user_mode(regs))
  322. do_no_context(regs);
  323. else
  324. do_sigbus(regs);
  325. } else
  326. BUG();
  327. break;
  328. }
  329. }
  330. /*
  331. * This routine handles page faults. It determines the address,
  332. * and the problem, and then passes it off to one of the appropriate
  333. * routines.
  334. *
  335. * interruption code (int_code):
  336. * 04 Protection -> Write-Protection (suprression)
  337. * 10 Segment translation -> Not present (nullification)
  338. * 11 Page translation -> Not present (nullification)
  339. * 3b Region third trans. -> Not present (nullification)
  340. */
  341. static inline int do_exception(struct pt_regs *regs, int access)
  342. {
  343. #ifdef CONFIG_PGSTE
  344. struct gmap *gmap;
  345. #endif
  346. struct task_struct *tsk;
  347. struct mm_struct *mm;
  348. struct vm_area_struct *vma;
  349. unsigned long trans_exc_code;
  350. unsigned long address;
  351. unsigned int flags;
  352. int fault;
  353. tsk = current;
  354. /*
  355. * The instruction that caused the program check has
  356. * been nullified. Don't signal single step via SIGTRAP.
  357. */
  358. clear_pt_regs_flag(regs, PIF_PER_TRAP);
  359. if (notify_page_fault(regs))
  360. return 0;
  361. mm = tsk->mm;
  362. trans_exc_code = regs->int_parm_long;
  363. /*
  364. * Verify that the fault happened in user space, that
  365. * we are not in an interrupt and that there is a
  366. * user context.
  367. */
  368. fault = VM_FAULT_BADCONTEXT;
  369. if (unlikely(!user_space_fault(regs) || faulthandler_disabled() || !mm))
  370. goto out;
  371. address = trans_exc_code & __FAIL_ADDR_MASK;
  372. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  373. flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  374. if (user_mode(regs))
  375. flags |= FAULT_FLAG_USER;
  376. if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
  377. flags |= FAULT_FLAG_WRITE;
  378. down_read(&mm->mmap_sem);
  379. #ifdef CONFIG_PGSTE
  380. gmap = (current->flags & PF_VCPU) ?
  381. (struct gmap *) S390_lowcore.gmap : NULL;
  382. if (gmap) {
  383. current->thread.gmap_addr = address;
  384. address = __gmap_translate(gmap, address);
  385. if (address == -EFAULT) {
  386. fault = VM_FAULT_BADMAP;
  387. goto out_up;
  388. }
  389. if (gmap->pfault_enabled)
  390. flags |= FAULT_FLAG_RETRY_NOWAIT;
  391. }
  392. #endif
  393. retry:
  394. fault = VM_FAULT_BADMAP;
  395. vma = find_vma(mm, address);
  396. if (!vma)
  397. goto out_up;
  398. if (unlikely(vma->vm_start > address)) {
  399. if (!(vma->vm_flags & VM_GROWSDOWN))
  400. goto out_up;
  401. if (expand_stack(vma, address))
  402. goto out_up;
  403. }
  404. /*
  405. * Ok, we have a good vm_area for this memory access, so
  406. * we can handle it..
  407. */
  408. fault = VM_FAULT_BADACCESS;
  409. if (unlikely(!(vma->vm_flags & access)))
  410. goto out_up;
  411. if (is_vm_hugetlb_page(vma))
  412. address &= HPAGE_MASK;
  413. /*
  414. * If for any reason at all we couldn't handle the fault,
  415. * make sure we exit gracefully rather than endlessly redo
  416. * the fault.
  417. */
  418. fault = handle_mm_fault(mm, vma, address, flags);
  419. /* No reason to continue if interrupted by SIGKILL. */
  420. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
  421. fault = VM_FAULT_SIGNAL;
  422. if (flags & FAULT_FLAG_RETRY_NOWAIT)
  423. goto out_up;
  424. goto out;
  425. }
  426. if (unlikely(fault & VM_FAULT_ERROR))
  427. goto out_up;
  428. /*
  429. * Major/minor page fault accounting is only done on the
  430. * initial attempt. If we go through a retry, it is extremely
  431. * likely that the page will be found in page cache at that point.
  432. */
  433. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  434. if (fault & VM_FAULT_MAJOR) {
  435. tsk->maj_flt++;
  436. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  437. regs, address);
  438. } else {
  439. tsk->min_flt++;
  440. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  441. regs, address);
  442. }
  443. if (fault & VM_FAULT_RETRY) {
  444. #ifdef CONFIG_PGSTE
  445. if (gmap && (flags & FAULT_FLAG_RETRY_NOWAIT)) {
  446. /* FAULT_FLAG_RETRY_NOWAIT has been set,
  447. * mmap_sem has not been released */
  448. current->thread.gmap_pfault = 1;
  449. fault = VM_FAULT_PFAULT;
  450. goto out_up;
  451. }
  452. #endif
  453. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  454. * of starvation. */
  455. flags &= ~(FAULT_FLAG_ALLOW_RETRY |
  456. FAULT_FLAG_RETRY_NOWAIT);
  457. flags |= FAULT_FLAG_TRIED;
  458. down_read(&mm->mmap_sem);
  459. goto retry;
  460. }
  461. }
  462. #ifdef CONFIG_PGSTE
  463. if (gmap) {
  464. address = __gmap_link(gmap, current->thread.gmap_addr,
  465. address);
  466. if (address == -EFAULT) {
  467. fault = VM_FAULT_BADMAP;
  468. goto out_up;
  469. }
  470. if (address == -ENOMEM) {
  471. fault = VM_FAULT_OOM;
  472. goto out_up;
  473. }
  474. }
  475. #endif
  476. fault = 0;
  477. out_up:
  478. up_read(&mm->mmap_sem);
  479. out:
  480. return fault;
  481. }
  482. void do_protection_exception(struct pt_regs *regs)
  483. {
  484. unsigned long trans_exc_code;
  485. int fault;
  486. trans_exc_code = regs->int_parm_long;
  487. /*
  488. * Protection exceptions are suppressing, decrement psw address.
  489. * The exception to this rule are aborted transactions, for these
  490. * the PSW already points to the correct location.
  491. */
  492. if (!(regs->int_code & 0x200))
  493. regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
  494. /*
  495. * Check for low-address protection. This needs to be treated
  496. * as a special case because the translation exception code
  497. * field is not guaranteed to contain valid data in this case.
  498. */
  499. if (unlikely(!(trans_exc_code & 4))) {
  500. do_low_address(regs);
  501. return;
  502. }
  503. fault = do_exception(regs, VM_WRITE);
  504. if (unlikely(fault))
  505. do_fault_error(regs, fault);
  506. }
  507. NOKPROBE_SYMBOL(do_protection_exception);
  508. void do_dat_exception(struct pt_regs *regs)
  509. {
  510. int access, fault;
  511. access = VM_READ | VM_EXEC | VM_WRITE;
  512. fault = do_exception(regs, access);
  513. if (unlikely(fault))
  514. do_fault_error(regs, fault);
  515. }
  516. NOKPROBE_SYMBOL(do_dat_exception);
  517. #ifdef CONFIG_PFAULT
  518. /*
  519. * 'pfault' pseudo page faults routines.
  520. */
  521. static int pfault_disable;
  522. static int __init nopfault(char *str)
  523. {
  524. pfault_disable = 1;
  525. return 1;
  526. }
  527. __setup("nopfault", nopfault);
  528. struct pfault_refbk {
  529. u16 refdiagc;
  530. u16 reffcode;
  531. u16 refdwlen;
  532. u16 refversn;
  533. u64 refgaddr;
  534. u64 refselmk;
  535. u64 refcmpmk;
  536. u64 reserved;
  537. } __attribute__ ((packed, aligned(8)));
  538. int pfault_init(void)
  539. {
  540. struct pfault_refbk refbk = {
  541. .refdiagc = 0x258,
  542. .reffcode = 0,
  543. .refdwlen = 5,
  544. .refversn = 2,
  545. .refgaddr = __LC_LPP,
  546. .refselmk = 1ULL << 48,
  547. .refcmpmk = 1ULL << 48,
  548. .reserved = __PF_RES_FIELD };
  549. int rc;
  550. if (pfault_disable)
  551. return -1;
  552. diag_stat_inc(DIAG_STAT_X258);
  553. asm volatile(
  554. " diag %1,%0,0x258\n"
  555. "0: j 2f\n"
  556. "1: la %0,8\n"
  557. "2:\n"
  558. EX_TABLE(0b,1b)
  559. : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
  560. return rc;
  561. }
  562. void pfault_fini(void)
  563. {
  564. struct pfault_refbk refbk = {
  565. .refdiagc = 0x258,
  566. .reffcode = 1,
  567. .refdwlen = 5,
  568. .refversn = 2,
  569. };
  570. if (pfault_disable)
  571. return;
  572. diag_stat_inc(DIAG_STAT_X258);
  573. asm volatile(
  574. " diag %0,0,0x258\n"
  575. "0:\n"
  576. EX_TABLE(0b,0b)
  577. : : "a" (&refbk), "m" (refbk) : "cc");
  578. }
  579. static DEFINE_SPINLOCK(pfault_lock);
  580. static LIST_HEAD(pfault_list);
  581. static void pfault_interrupt(struct ext_code ext_code,
  582. unsigned int param32, unsigned long param64)
  583. {
  584. struct task_struct *tsk;
  585. __u16 subcode;
  586. pid_t pid;
  587. /*
  588. * Get the external interruption subcode & pfault
  589. * initial/completion signal bit. VM stores this
  590. * in the 'cpu address' field associated with the
  591. * external interrupt.
  592. */
  593. subcode = ext_code.subcode;
  594. if ((subcode & 0xff00) != __SUBCODE_MASK)
  595. return;
  596. inc_irq_stat(IRQEXT_PFL);
  597. /* Get the token (= pid of the affected task). */
  598. pid = param64 & LPP_PFAULT_PID_MASK;
  599. rcu_read_lock();
  600. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  601. if (tsk)
  602. get_task_struct(tsk);
  603. rcu_read_unlock();
  604. if (!tsk)
  605. return;
  606. spin_lock(&pfault_lock);
  607. if (subcode & 0x0080) {
  608. /* signal bit is set -> a page has been swapped in by VM */
  609. if (tsk->thread.pfault_wait == 1) {
  610. /* Initial interrupt was faster than the completion
  611. * interrupt. pfault_wait is valid. Set pfault_wait
  612. * back to zero and wake up the process. This can
  613. * safely be done because the task is still sleeping
  614. * and can't produce new pfaults. */
  615. tsk->thread.pfault_wait = 0;
  616. list_del(&tsk->thread.list);
  617. wake_up_process(tsk);
  618. put_task_struct(tsk);
  619. } else {
  620. /* Completion interrupt was faster than initial
  621. * interrupt. Set pfault_wait to -1 so the initial
  622. * interrupt doesn't put the task to sleep.
  623. * If the task is not running, ignore the completion
  624. * interrupt since it must be a leftover of a PFAULT
  625. * CANCEL operation which didn't remove all pending
  626. * completion interrupts. */
  627. if (tsk->state == TASK_RUNNING)
  628. tsk->thread.pfault_wait = -1;
  629. }
  630. } else {
  631. /* signal bit not set -> a real page is missing. */
  632. if (WARN_ON_ONCE(tsk != current))
  633. goto out;
  634. if (tsk->thread.pfault_wait == 1) {
  635. /* Already on the list with a reference: put to sleep */
  636. __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  637. set_tsk_need_resched(tsk);
  638. } else if (tsk->thread.pfault_wait == -1) {
  639. /* Completion interrupt was faster than the initial
  640. * interrupt (pfault_wait == -1). Set pfault_wait
  641. * back to zero and exit. */
  642. tsk->thread.pfault_wait = 0;
  643. } else {
  644. /* Initial interrupt arrived before completion
  645. * interrupt. Let the task sleep.
  646. * An extra task reference is needed since a different
  647. * cpu may set the task state to TASK_RUNNING again
  648. * before the scheduler is reached. */
  649. get_task_struct(tsk);
  650. tsk->thread.pfault_wait = 1;
  651. list_add(&tsk->thread.list, &pfault_list);
  652. __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  653. set_tsk_need_resched(tsk);
  654. }
  655. }
  656. out:
  657. spin_unlock(&pfault_lock);
  658. put_task_struct(tsk);
  659. }
  660. static int pfault_cpu_notify(struct notifier_block *self, unsigned long action,
  661. void *hcpu)
  662. {
  663. struct thread_struct *thread, *next;
  664. struct task_struct *tsk;
  665. switch (action & ~CPU_TASKS_FROZEN) {
  666. case CPU_DEAD:
  667. spin_lock_irq(&pfault_lock);
  668. list_for_each_entry_safe(thread, next, &pfault_list, list) {
  669. thread->pfault_wait = 0;
  670. list_del(&thread->list);
  671. tsk = container_of(thread, struct task_struct, thread);
  672. wake_up_process(tsk);
  673. put_task_struct(tsk);
  674. }
  675. spin_unlock_irq(&pfault_lock);
  676. break;
  677. default:
  678. break;
  679. }
  680. return NOTIFY_OK;
  681. }
  682. static int __init pfault_irq_init(void)
  683. {
  684. int rc;
  685. rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  686. if (rc)
  687. goto out_extint;
  688. rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
  689. if (rc)
  690. goto out_pfault;
  691. irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
  692. hotcpu_notifier(pfault_cpu_notify, 0);
  693. return 0;
  694. out_pfault:
  695. unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  696. out_extint:
  697. pfault_disable = 1;
  698. return rc;
  699. }
  700. early_initcall(pfault_irq_init);
  701. #endif /* CONFIG_PFAULT */