fault.c 19 KB

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