fault.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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/gmap.h>
  35. #include <asm/irq.h>
  36. #include <asm/mmu_context.h>
  37. #include <asm/facility.h>
  38. #include "../kernel/entry.h"
  39. #define __FAIL_ADDR_MASK -4096L
  40. #define __SUBCODE_MASK 0x0600
  41. #define __PF_RES_FIELD 0x8000000000000000ULL
  42. #define VM_FAULT_BADCONTEXT 0x010000
  43. #define VM_FAULT_BADMAP 0x020000
  44. #define VM_FAULT_BADACCESS 0x040000
  45. #define VM_FAULT_SIGNAL 0x080000
  46. #define VM_FAULT_PFAULT 0x100000
  47. static unsigned long store_indication __read_mostly;
  48. static int __init fault_init(void)
  49. {
  50. if (test_facility(75))
  51. store_indication = 0xc00;
  52. return 0;
  53. }
  54. early_initcall(fault_init);
  55. static inline int notify_page_fault(struct pt_regs *regs)
  56. {
  57. int ret = 0;
  58. /* kprobe_running() needs smp_processor_id() */
  59. if (kprobes_built_in() && !user_mode(regs)) {
  60. preempt_disable();
  61. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  62. ret = 1;
  63. preempt_enable();
  64. }
  65. return ret;
  66. }
  67. /*
  68. * Unlock any spinlocks which will prevent us from getting the
  69. * message out.
  70. */
  71. void bust_spinlocks(int yes)
  72. {
  73. if (yes) {
  74. oops_in_progress = 1;
  75. } else {
  76. int loglevel_save = console_loglevel;
  77. console_unblank();
  78. oops_in_progress = 0;
  79. /*
  80. * OK, the message is on the console. Now we call printk()
  81. * without oops_in_progress set so that printk will give klogd
  82. * a poke. Hold onto your hats...
  83. */
  84. console_loglevel = 15;
  85. printk(" ");
  86. console_loglevel = loglevel_save;
  87. }
  88. }
  89. /*
  90. * Returns the address space associated with the fault.
  91. * Returns 0 for kernel space and 1 for user space.
  92. */
  93. static inline int user_space_fault(struct pt_regs *regs)
  94. {
  95. unsigned long trans_exc_code;
  96. /*
  97. * The lowest two bits of the translation exception
  98. * identification indicate which paging table was used.
  99. */
  100. trans_exc_code = regs->int_parm_long & 3;
  101. if (trans_exc_code == 3) /* home space -> kernel */
  102. return 0;
  103. if (user_mode(regs))
  104. return 1;
  105. if (trans_exc_code == 2) /* secondary space -> set_fs */
  106. return current->thread.mm_segment.ar4;
  107. if (current->flags & PF_VCPU)
  108. return 1;
  109. return 0;
  110. }
  111. static int bad_address(void *p)
  112. {
  113. unsigned long dummy;
  114. return probe_kernel_address((unsigned long *)p, dummy);
  115. }
  116. static void dump_pagetable(unsigned long asce, unsigned long address)
  117. {
  118. unsigned long *table = __va(asce & PAGE_MASK);
  119. pr_alert("AS:%016lx ", asce);
  120. switch (asce & _ASCE_TYPE_MASK) {
  121. case _ASCE_TYPE_REGION1:
  122. table = table + ((address >> 53) & 0x7ff);
  123. if (bad_address(table))
  124. goto bad;
  125. pr_cont("R1:%016lx ", *table);
  126. if (*table & _REGION_ENTRY_INVALID)
  127. goto out;
  128. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  129. /* fallthrough */
  130. case _ASCE_TYPE_REGION2:
  131. table = table + ((address >> 42) & 0x7ff);
  132. if (bad_address(table))
  133. goto bad;
  134. pr_cont("R2:%016lx ", *table);
  135. if (*table & _REGION_ENTRY_INVALID)
  136. goto out;
  137. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  138. /* fallthrough */
  139. case _ASCE_TYPE_REGION3:
  140. table = table + ((address >> 31) & 0x7ff);
  141. if (bad_address(table))
  142. goto bad;
  143. pr_cont("R3:%016lx ", *table);
  144. if (*table & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE))
  145. goto out;
  146. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  147. /* fallthrough */
  148. case _ASCE_TYPE_SEGMENT:
  149. table = table + ((address >> 20) & 0x7ff);
  150. if (bad_address(table))
  151. goto bad;
  152. pr_cont("S:%016lx ", *table);
  153. if (*table & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE))
  154. goto out;
  155. table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
  156. }
  157. table = table + ((address >> 12) & 0xff);
  158. if (bad_address(table))
  159. goto bad;
  160. pr_cont("P:%016lx ", *table);
  161. out:
  162. pr_cont("\n");
  163. return;
  164. bad:
  165. pr_cont("BAD\n");
  166. }
  167. static void dump_fault_info(struct pt_regs *regs)
  168. {
  169. unsigned long asce;
  170. pr_alert("Failing address: %016lx TEID: %016lx\n",
  171. regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long);
  172. pr_alert("Fault in ");
  173. switch (regs->int_parm_long & 3) {
  174. case 3:
  175. pr_cont("home space ");
  176. break;
  177. case 2:
  178. pr_cont("secondary space ");
  179. break;
  180. case 1:
  181. pr_cont("access register ");
  182. break;
  183. case 0:
  184. pr_cont("primary space ");
  185. break;
  186. }
  187. pr_cont("mode while using ");
  188. if (!user_space_fault(regs)) {
  189. asce = S390_lowcore.kernel_asce;
  190. pr_cont("kernel ");
  191. }
  192. #ifdef CONFIG_PGSTE
  193. else if ((current->flags & PF_VCPU) && S390_lowcore.gmap) {
  194. struct gmap *gmap = (struct gmap *)S390_lowcore.gmap;
  195. asce = gmap->asce;
  196. pr_cont("gmap ");
  197. }
  198. #endif
  199. else {
  200. asce = S390_lowcore.user_asce;
  201. pr_cont("user ");
  202. }
  203. pr_cont("ASCE.\n");
  204. dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK);
  205. }
  206. int show_unhandled_signals = 1;
  207. void report_user_fault(struct pt_regs *regs, long signr, int is_mm_fault)
  208. {
  209. if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
  210. return;
  211. if (!unhandled_signal(current, signr))
  212. return;
  213. if (!printk_ratelimit())
  214. return;
  215. printk(KERN_ALERT "User process fault: interruption code %04x ilc:%d ",
  216. regs->int_code & 0xffff, regs->int_code >> 17);
  217. print_vma_addr(KERN_CONT "in ", regs->psw.addr);
  218. printk(KERN_CONT "\n");
  219. if (is_mm_fault)
  220. dump_fault_info(regs);
  221. show_regs(regs);
  222. }
  223. /*
  224. * Send SIGSEGV to task. This is an external routine
  225. * to keep the stack usage of do_page_fault small.
  226. */
  227. static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
  228. {
  229. struct siginfo si;
  230. report_user_fault(regs, SIGSEGV, 1);
  231. si.si_signo = SIGSEGV;
  232. si.si_errno = 0;
  233. si.si_code = si_code;
  234. si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
  235. force_sig_info(SIGSEGV, &si, current);
  236. }
  237. static noinline void do_no_context(struct pt_regs *regs)
  238. {
  239. const struct exception_table_entry *fixup;
  240. /* Are we prepared to handle this kernel fault? */
  241. fixup = search_exception_tables(regs->psw.addr);
  242. if (fixup) {
  243. regs->psw.addr = extable_fixup(fixup);
  244. return;
  245. }
  246. /*
  247. * Oops. The kernel tried to access some bad page. We'll have to
  248. * terminate things with extreme prejudice.
  249. */
  250. if (!user_space_fault(regs))
  251. printk(KERN_ALERT "Unable to handle kernel pointer dereference"
  252. " in virtual kernel address space\n");
  253. else
  254. printk(KERN_ALERT "Unable to handle kernel paging request"
  255. " in virtual user address space\n");
  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(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. goto out;
  423. }
  424. if (unlikely(fault & VM_FAULT_ERROR))
  425. goto out_up;
  426. /*
  427. * Major/minor page fault accounting is only done on the
  428. * initial attempt. If we go through a retry, it is extremely
  429. * likely that the page will be found in page cache at that point.
  430. */
  431. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  432. if (fault & VM_FAULT_MAJOR) {
  433. tsk->maj_flt++;
  434. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  435. regs, address);
  436. } else {
  437. tsk->min_flt++;
  438. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  439. regs, address);
  440. }
  441. if (fault & VM_FAULT_RETRY) {
  442. #ifdef CONFIG_PGSTE
  443. if (gmap && (flags & FAULT_FLAG_RETRY_NOWAIT)) {
  444. /* FAULT_FLAG_RETRY_NOWAIT has been set,
  445. * mmap_sem has not been released */
  446. current->thread.gmap_pfault = 1;
  447. fault = VM_FAULT_PFAULT;
  448. goto out_up;
  449. }
  450. #endif
  451. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  452. * of starvation. */
  453. flags &= ~(FAULT_FLAG_ALLOW_RETRY |
  454. FAULT_FLAG_RETRY_NOWAIT);
  455. flags |= FAULT_FLAG_TRIED;
  456. down_read(&mm->mmap_sem);
  457. goto retry;
  458. }
  459. }
  460. #ifdef CONFIG_PGSTE
  461. if (gmap) {
  462. address = __gmap_link(gmap, current->thread.gmap_addr,
  463. address);
  464. if (address == -EFAULT) {
  465. fault = VM_FAULT_BADMAP;
  466. goto out_up;
  467. }
  468. if (address == -ENOMEM) {
  469. fault = VM_FAULT_OOM;
  470. goto out_up;
  471. }
  472. }
  473. #endif
  474. fault = 0;
  475. out_up:
  476. up_read(&mm->mmap_sem);
  477. out:
  478. return fault;
  479. }
  480. void do_protection_exception(struct pt_regs *regs)
  481. {
  482. unsigned long trans_exc_code;
  483. int fault;
  484. trans_exc_code = regs->int_parm_long;
  485. /*
  486. * Protection exceptions are suppressing, decrement psw address.
  487. * The exception to this rule are aborted transactions, for these
  488. * the PSW already points to the correct location.
  489. */
  490. if (!(regs->int_code & 0x200))
  491. regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
  492. /*
  493. * Check for low-address protection. This needs to be treated
  494. * as a special case because the translation exception code
  495. * field is not guaranteed to contain valid data in this case.
  496. */
  497. if (unlikely(!(trans_exc_code & 4))) {
  498. do_low_address(regs);
  499. return;
  500. }
  501. fault = do_exception(regs, VM_WRITE);
  502. if (unlikely(fault))
  503. do_fault_error(regs, fault);
  504. }
  505. NOKPROBE_SYMBOL(do_protection_exception);
  506. void do_dat_exception(struct pt_regs *regs)
  507. {
  508. int access, fault;
  509. access = VM_READ | VM_EXEC | VM_WRITE;
  510. fault = do_exception(regs, access);
  511. if (unlikely(fault))
  512. do_fault_error(regs, fault);
  513. }
  514. NOKPROBE_SYMBOL(do_dat_exception);
  515. #ifdef CONFIG_PFAULT
  516. /*
  517. * 'pfault' pseudo page faults routines.
  518. */
  519. static int pfault_disable;
  520. static int __init nopfault(char *str)
  521. {
  522. pfault_disable = 1;
  523. return 1;
  524. }
  525. __setup("nopfault", nopfault);
  526. struct pfault_refbk {
  527. u16 refdiagc;
  528. u16 reffcode;
  529. u16 refdwlen;
  530. u16 refversn;
  531. u64 refgaddr;
  532. u64 refselmk;
  533. u64 refcmpmk;
  534. u64 reserved;
  535. } __attribute__ ((packed, aligned(8)));
  536. int pfault_init(void)
  537. {
  538. struct pfault_refbk refbk = {
  539. .refdiagc = 0x258,
  540. .reffcode = 0,
  541. .refdwlen = 5,
  542. .refversn = 2,
  543. .refgaddr = __LC_LPP,
  544. .refselmk = 1ULL << 48,
  545. .refcmpmk = 1ULL << 48,
  546. .reserved = __PF_RES_FIELD };
  547. int rc;
  548. if (pfault_disable)
  549. return -1;
  550. diag_stat_inc(DIAG_STAT_X258);
  551. asm volatile(
  552. " diag %1,%0,0x258\n"
  553. "0: j 2f\n"
  554. "1: la %0,8\n"
  555. "2:\n"
  556. EX_TABLE(0b,1b)
  557. : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
  558. return rc;
  559. }
  560. void pfault_fini(void)
  561. {
  562. struct pfault_refbk refbk = {
  563. .refdiagc = 0x258,
  564. .reffcode = 1,
  565. .refdwlen = 5,
  566. .refversn = 2,
  567. };
  568. if (pfault_disable)
  569. return;
  570. diag_stat_inc(DIAG_STAT_X258);
  571. asm volatile(
  572. " diag %0,0,0x258\n"
  573. "0:\n"
  574. EX_TABLE(0b,0b)
  575. : : "a" (&refbk), "m" (refbk) : "cc");
  576. }
  577. static DEFINE_SPINLOCK(pfault_lock);
  578. static LIST_HEAD(pfault_list);
  579. #define PF_COMPLETE 0x0080
  580. /*
  581. * The mechanism of our pfault code: if Linux is running as guest, runs a user
  582. * space process and the user space process accesses a page that the host has
  583. * paged out we get a pfault interrupt.
  584. *
  585. * This allows us, within the guest, to schedule a different process. Without
  586. * this mechanism the host would have to suspend the whole virtual cpu until
  587. * the page has been paged in.
  588. *
  589. * So when we get such an interrupt then we set the state of the current task
  590. * to uninterruptible and also set the need_resched flag. Both happens within
  591. * interrupt context(!). If we later on want to return to user space we
  592. * recognize the need_resched flag and then call schedule(). It's not very
  593. * obvious how this works...
  594. *
  595. * Of course we have a lot of additional fun with the completion interrupt (->
  596. * host signals that a page of a process has been paged in and the process can
  597. * continue to run). This interrupt can arrive on any cpu and, since we have
  598. * virtual cpus, actually appear before the interrupt that signals that a page
  599. * is missing.
  600. */
  601. static void pfault_interrupt(struct ext_code ext_code,
  602. unsigned int param32, unsigned long param64)
  603. {
  604. struct task_struct *tsk;
  605. __u16 subcode;
  606. pid_t pid;
  607. /*
  608. * Get the external interruption subcode & pfault initial/completion
  609. * signal bit. VM stores this in the 'cpu address' field associated
  610. * with the external interrupt.
  611. */
  612. subcode = ext_code.subcode;
  613. if ((subcode & 0xff00) != __SUBCODE_MASK)
  614. return;
  615. inc_irq_stat(IRQEXT_PFL);
  616. /* Get the token (= pid of the affected task). */
  617. pid = param64 & LPP_PFAULT_PID_MASK;
  618. rcu_read_lock();
  619. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  620. if (tsk)
  621. get_task_struct(tsk);
  622. rcu_read_unlock();
  623. if (!tsk)
  624. return;
  625. spin_lock(&pfault_lock);
  626. if (subcode & PF_COMPLETE) {
  627. /* signal bit is set -> a page has been swapped in by VM */
  628. if (tsk->thread.pfault_wait == 1) {
  629. /* Initial interrupt was faster than the completion
  630. * interrupt. pfault_wait is valid. Set pfault_wait
  631. * back to zero and wake up the process. This can
  632. * safely be done because the task is still sleeping
  633. * and can't produce new pfaults. */
  634. tsk->thread.pfault_wait = 0;
  635. list_del(&tsk->thread.list);
  636. wake_up_process(tsk);
  637. put_task_struct(tsk);
  638. } else {
  639. /* Completion interrupt was faster than initial
  640. * interrupt. Set pfault_wait to -1 so the initial
  641. * interrupt doesn't put the task to sleep.
  642. * If the task is not running, ignore the completion
  643. * interrupt since it must be a leftover of a PFAULT
  644. * CANCEL operation which didn't remove all pending
  645. * completion interrupts. */
  646. if (tsk->state == TASK_RUNNING)
  647. tsk->thread.pfault_wait = -1;
  648. }
  649. } else {
  650. /* signal bit not set -> a real page is missing. */
  651. if (WARN_ON_ONCE(tsk != current))
  652. goto out;
  653. if (tsk->thread.pfault_wait == 1) {
  654. /* Already on the list with a reference: put to sleep */
  655. goto block;
  656. } else if (tsk->thread.pfault_wait == -1) {
  657. /* Completion interrupt was faster than the initial
  658. * interrupt (pfault_wait == -1). Set pfault_wait
  659. * back to zero and exit. */
  660. tsk->thread.pfault_wait = 0;
  661. } else {
  662. /* Initial interrupt arrived before completion
  663. * interrupt. Let the task sleep.
  664. * An extra task reference is needed since a different
  665. * cpu may set the task state to TASK_RUNNING again
  666. * before the scheduler is reached. */
  667. get_task_struct(tsk);
  668. tsk->thread.pfault_wait = 1;
  669. list_add(&tsk->thread.list, &pfault_list);
  670. block:
  671. /* Since this must be a userspace fault, there
  672. * is no kernel task state to trample. Rely on the
  673. * return to userspace schedule() to block. */
  674. __set_current_state(TASK_UNINTERRUPTIBLE);
  675. set_tsk_need_resched(tsk);
  676. }
  677. }
  678. out:
  679. spin_unlock(&pfault_lock);
  680. put_task_struct(tsk);
  681. }
  682. static int pfault_cpu_notify(struct notifier_block *self, unsigned long action,
  683. void *hcpu)
  684. {
  685. struct thread_struct *thread, *next;
  686. struct task_struct *tsk;
  687. switch (action & ~CPU_TASKS_FROZEN) {
  688. case CPU_DEAD:
  689. spin_lock_irq(&pfault_lock);
  690. list_for_each_entry_safe(thread, next, &pfault_list, list) {
  691. thread->pfault_wait = 0;
  692. list_del(&thread->list);
  693. tsk = container_of(thread, struct task_struct, thread);
  694. wake_up_process(tsk);
  695. put_task_struct(tsk);
  696. }
  697. spin_unlock_irq(&pfault_lock);
  698. break;
  699. default:
  700. break;
  701. }
  702. return NOTIFY_OK;
  703. }
  704. static int __init pfault_irq_init(void)
  705. {
  706. int rc;
  707. rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  708. if (rc)
  709. goto out_extint;
  710. rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
  711. if (rc)
  712. goto out_pfault;
  713. irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
  714. hotcpu_notifier(pfault_cpu_notify, 0);
  715. return 0;
  716. out_pfault:
  717. unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  718. out_extint:
  719. pfault_disable = 1;
  720. return rc;
  721. }
  722. early_initcall(pfault_irq_init);
  723. #endif /* CONFIG_PFAULT */