fault.c 19 KB

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