1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * S390 version 4 * Copyright IBM Corp. 1999 5 * Author(s): Hartmut Penner (hp@de.ibm.com) 6 * Ulrich Weigand (uweigand@de.ibm.com) 7 * 8 * Derived from "arch/i386/mm/fault.c" 9 * Copyright (C) 1995 Linus Torvalds 10 */ 11 12 #include <linux/kernel_stat.h> 13 #include <linux/perf_event.h> 14 #include <linux/signal.h> 15 #include <linux/sched.h> 16 #include <linux/sched/debug.h> 17 #include <linux/kernel.h> 18 #include <linux/errno.h> 19 #include <linux/string.h> 20 #include <linux/types.h> 21 #include <linux/ptrace.h> 22 #include <linux/mman.h> 23 #include <linux/mm.h> 24 #include <linux/compat.h> 25 #include <linux/smp.h> 26 #include <linux/kdebug.h> 27 #include <linux/init.h> 28 #include <linux/console.h> 29 #include <linux/extable.h> 30 #include <linux/hardirq.h> 31 #include <linux/kprobes.h> 32 #include <linux/uaccess.h> 33 #include <linux/hugetlb.h> 34 #include <asm/asm-offsets.h> 35 #include <asm/diag.h> 36 #include <asm/pgtable.h> 37 #include <asm/gmap.h> 38 #include <asm/irq.h> 39 #include <asm/mmu_context.h> 40 #include <asm/facility.h> 41 #include "../kernel/entry.h" 42 43 #define __FAIL_ADDR_MASK -4096L 44 #define __SUBCODE_MASK 0x0600 45 #define __PF_RES_FIELD 0x8000000000000000ULL 46 47 #define VM_FAULT_BADCONTEXT 0x010000 48 #define VM_FAULT_BADMAP 0x020000 49 #define VM_FAULT_BADACCESS 0x040000 50 #define VM_FAULT_SIGNAL 0x080000 51 #define VM_FAULT_PFAULT 0x100000 52 53 enum fault_type { 54 KERNEL_FAULT, 55 USER_FAULT, 56 VDSO_FAULT, 57 GMAP_FAULT, 58 }; 59 60 static unsigned long store_indication __read_mostly; 61 62 static int __init fault_init(void) 63 { 64 if (test_facility(75)) 65 store_indication = 0xc00; 66 return 0; 67 } 68 early_initcall(fault_init); 69 70 static inline int notify_page_fault(struct pt_regs *regs) 71 { 72 int ret = 0; 73 74 /* kprobe_running() needs smp_processor_id() */ 75 if (kprobes_built_in() && !user_mode(regs)) { 76 preempt_disable(); 77 if (kprobe_running() && kprobe_fault_handler(regs, 14)) 78 ret = 1; 79 preempt_enable(); 80 } 81 return ret; 82 } 83 84 /* 85 * Find out which address space caused the exception. 86 * Access register mode is impossible, ignore space == 3. 87 */ 88 static enum fault_type get_fault_type(struct pt_regs *regs) 89 { 90 unsigned long trans_exc_code; 91 92 trans_exc_code = regs->int_parm_long & 3; 93 if (likely(trans_exc_code == 0)) { 94 /* primary space exception */ 95 if (IS_ENABLED(CONFIG_PGSTE) && 96 test_pt_regs_flag(regs, PIF_GUEST_FAULT)) 97 return GMAP_FAULT; 98 if (current->thread.mm_segment == USER_DS) 99 return USER_FAULT; 100 return KERNEL_FAULT; 101 } 102 if (trans_exc_code == 2) { 103 /* secondary space exception */ 104 if (current->thread.mm_segment & 1) { 105 if (current->thread.mm_segment == USER_DS_SACF) 106 return USER_FAULT; 107 return KERNEL_FAULT; 108 } 109 return VDSO_FAULT; 110 } 111 /* home space exception -> access via kernel ASCE */ 112 return KERNEL_FAULT; 113 } 114 115 static int bad_address(void *p) 116 { 117 unsigned long dummy; 118 119 return probe_kernel_address((unsigned long *)p, dummy); 120 } 121 122 static void dump_pagetable(unsigned long asce, unsigned long address) 123 { 124 unsigned long *table = __va(asce & _ASCE_ORIGIN); 125 126 pr_alert("AS:%016lx ", asce); 127 switch (asce & _ASCE_TYPE_MASK) { 128 case _ASCE_TYPE_REGION1: 129 table += (address & _REGION1_INDEX) >> _REGION1_SHIFT; 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 += (address & _REGION2_INDEX) >> _REGION2_SHIFT; 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 += (address & _REGION3_INDEX) >> _REGION3_SHIFT; 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 += (address & _SEGMENT_INDEX) >> _SEGMENT_SHIFT; 157 if (bad_address(table)) 158 goto bad; 159 pr_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 += (address & _PAGE_INDEX) >> _PAGE_SHIFT; 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 175 static void dump_fault_info(struct pt_regs *regs) 176 { 177 unsigned long asce; 178 179 pr_alert("Failing address: %016lx TEID: %016lx\n", 180 regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long); 181 pr_alert("Fault in "); 182 switch (regs->int_parm_long & 3) { 183 case 3: 184 pr_cont("home space "); 185 break; 186 case 2: 187 pr_cont("secondary space "); 188 break; 189 case 1: 190 pr_cont("access register "); 191 break; 192 case 0: 193 pr_cont("primary space "); 194 break; 195 } 196 pr_cont("mode while using "); 197 switch (get_fault_type(regs)) { 198 case USER_FAULT: 199 asce = S390_lowcore.user_asce; 200 pr_cont("user "); 201 break; 202 case VDSO_FAULT: 203 asce = S390_lowcore.vdso_asce; 204 pr_cont("vdso "); 205 break; 206 case GMAP_FAULT: 207 asce = ((struct gmap *) S390_lowcore.gmap)->asce; 208 pr_cont("gmap "); 209 break; 210 case KERNEL_FAULT: 211 asce = S390_lowcore.kernel_asce; 212 pr_cont("kernel "); 213 break; 214 default: 215 unreachable(); 216 } 217 pr_cont("ASCE.\n"); 218 dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK); 219 } 220 221 int show_unhandled_signals = 1; 222 223 void report_user_fault(struct pt_regs *regs, long signr, int is_mm_fault) 224 { 225 if ((task_pid_nr(current) > 1) && !show_unhandled_signals) 226 return; 227 if (!unhandled_signal(current, signr)) 228 return; 229 if (!printk_ratelimit()) 230 return; 231 printk(KERN_ALERT "User process fault: interruption code %04x ilc:%d ", 232 regs->int_code & 0xffff, regs->int_code >> 17); 233 print_vma_addr(KERN_CONT "in ", regs->psw.addr); 234 printk(KERN_CONT "\n"); 235 if (is_mm_fault) 236 dump_fault_info(regs); 237 show_regs(regs); 238 } 239 240 /* 241 * Send SIGSEGV to task. This is an external routine 242 * to keep the stack usage of do_page_fault small. 243 */ 244 static noinline void do_sigsegv(struct pt_regs *regs, int si_code) 245 { 246 report_user_fault(regs, SIGSEGV, 1); 247 force_sig_fault(SIGSEGV, si_code, 248 (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK), 249 current); 250 } 251 252 const struct exception_table_entry *s390_search_extables(unsigned long addr) 253 { 254 const struct exception_table_entry *fixup; 255 256 fixup = search_extable(__start_dma_ex_table, 257 __stop_dma_ex_table - __start_dma_ex_table, 258 addr); 259 if (!fixup) 260 fixup = search_exception_tables(addr); 261 return fixup; 262 } 263 264 static noinline void do_no_context(struct pt_regs *regs) 265 { 266 const struct exception_table_entry *fixup; 267 268 /* Are we prepared to handle this kernel fault? */ 269 fixup = s390_search_extables(regs->psw.addr); 270 if (fixup) { 271 regs->psw.addr = extable_fixup(fixup); 272 return; 273 } 274 275 /* 276 * Oops. The kernel tried to access some bad page. We'll have to 277 * terminate things with extreme prejudice. 278 */ 279 if (get_fault_type(regs) == KERNEL_FAULT) 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 dump_fault_info(regs); 286 die(regs, "Oops"); 287 do_exit(SIGKILL); 288 } 289 290 static noinline void do_low_address(struct pt_regs *regs) 291 { 292 /* Low-address protection hit in kernel mode means 293 NULL pointer write access in kernel mode. */ 294 if (regs->psw.mask & PSW_MASK_PSTATE) { 295 /* Low-address protection hit in user mode 'cannot happen'. */ 296 die (regs, "Low-address protection"); 297 do_exit(SIGKILL); 298 } 299 300 do_no_context(regs); 301 } 302 303 static noinline void do_sigbus(struct pt_regs *regs) 304 { 305 /* 306 * Send a sigbus, regardless of whether we were in kernel 307 * or user mode. 308 */ 309 force_sig_fault(SIGBUS, BUS_ADRERR, 310 (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK), 311 current); 312 } 313 314 static noinline int signal_return(struct pt_regs *regs) 315 { 316 u16 instruction; 317 int rc; 318 319 rc = __get_user(instruction, (u16 __user *) regs->psw.addr); 320 if (rc) 321 return rc; 322 if (instruction == 0x0a77) { 323 set_pt_regs_flag(regs, PIF_SYSCALL); 324 regs->int_code = 0x00040077; 325 return 0; 326 } else if (instruction == 0x0aad) { 327 set_pt_regs_flag(regs, PIF_SYSCALL); 328 regs->int_code = 0x000400ad; 329 return 0; 330 } 331 return -EACCES; 332 } 333 334 static noinline void do_fault_error(struct pt_regs *regs, int access, 335 vm_fault_t fault) 336 { 337 int si_code; 338 339 switch (fault) { 340 case VM_FAULT_BADACCESS: 341 if (access == VM_EXEC && signal_return(regs) == 0) 342 break; 343 case VM_FAULT_BADMAP: 344 /* Bad memory access. Check if it is kernel or user space. */ 345 if (user_mode(regs)) { 346 /* User mode accesses just cause a SIGSEGV */ 347 si_code = (fault == VM_FAULT_BADMAP) ? 348 SEGV_MAPERR : SEGV_ACCERR; 349 do_sigsegv(regs, si_code); 350 break; 351 } 352 case VM_FAULT_BADCONTEXT: 353 case VM_FAULT_PFAULT: 354 do_no_context(regs); 355 break; 356 case VM_FAULT_SIGNAL: 357 if (!user_mode(regs)) 358 do_no_context(regs); 359 break; 360 default: /* fault & VM_FAULT_ERROR */ 361 if (fault & VM_FAULT_OOM) { 362 if (!user_mode(regs)) 363 do_no_context(regs); 364 else 365 pagefault_out_of_memory(); 366 } else if (fault & VM_FAULT_SIGSEGV) { 367 /* Kernel mode? Handle exceptions or die */ 368 if (!user_mode(regs)) 369 do_no_context(regs); 370 else 371 do_sigsegv(regs, SEGV_MAPERR); 372 } else if (fault & VM_FAULT_SIGBUS) { 373 /* Kernel mode? Handle exceptions or die */ 374 if (!user_mode(regs)) 375 do_no_context(regs); 376 else 377 do_sigbus(regs); 378 } else 379 BUG(); 380 break; 381 } 382 } 383 384 /* 385 * This routine handles page faults. It determines the address, 386 * and the problem, and then passes it off to one of the appropriate 387 * routines. 388 * 389 * interruption code (int_code): 390 * 04 Protection -> Write-Protection (suprression) 391 * 10 Segment translation -> Not present (nullification) 392 * 11 Page translation -> Not present (nullification) 393 * 3b Region third trans. -> Not present (nullification) 394 */ 395 static inline vm_fault_t do_exception(struct pt_regs *regs, int access) 396 { 397 struct gmap *gmap; 398 struct task_struct *tsk; 399 struct mm_struct *mm; 400 struct vm_area_struct *vma; 401 enum fault_type type; 402 unsigned long trans_exc_code; 403 unsigned long address; 404 unsigned int flags; 405 vm_fault_t fault; 406 407 tsk = current; 408 /* 409 * The instruction that caused the program check has 410 * been nullified. Don't signal single step via SIGTRAP. 411 */ 412 clear_pt_regs_flag(regs, PIF_PER_TRAP); 413 414 if (notify_page_fault(regs)) 415 return 0; 416 417 mm = tsk->mm; 418 trans_exc_code = regs->int_parm_long; 419 420 /* 421 * Verify that the fault happened in user space, that 422 * we are not in an interrupt and that there is a 423 * user context. 424 */ 425 fault = VM_FAULT_BADCONTEXT; 426 type = get_fault_type(regs); 427 switch (type) { 428 case KERNEL_FAULT: 429 goto out; 430 case VDSO_FAULT: 431 fault = VM_FAULT_BADMAP; 432 goto out; 433 case USER_FAULT: 434 case GMAP_FAULT: 435 if (faulthandler_disabled() || !mm) 436 goto out; 437 break; 438 } 439 440 address = trans_exc_code & __FAIL_ADDR_MASK; 441 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 442 flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; 443 if (user_mode(regs)) 444 flags |= FAULT_FLAG_USER; 445 if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400) 446 flags |= FAULT_FLAG_WRITE; 447 down_read(&mm->mmap_sem); 448 449 gmap = NULL; 450 if (IS_ENABLED(CONFIG_PGSTE) && type == GMAP_FAULT) { 451 gmap = (struct gmap *) S390_lowcore.gmap; 452 current->thread.gmap_addr = address; 453 current->thread.gmap_write_flag = !!(flags & FAULT_FLAG_WRITE); 454 current->thread.gmap_int_code = regs->int_code & 0xffff; 455 address = __gmap_translate(gmap, address); 456 if (address == -EFAULT) { 457 fault = VM_FAULT_BADMAP; 458 goto out_up; 459 } 460 if (gmap->pfault_enabled) 461 flags |= FAULT_FLAG_RETRY_NOWAIT; 462 } 463 464 retry: 465 fault = VM_FAULT_BADMAP; 466 vma = find_vma(mm, address); 467 if (!vma) 468 goto out_up; 469 470 if (unlikely(vma->vm_start > address)) { 471 if (!(vma->vm_flags & VM_GROWSDOWN)) 472 goto out_up; 473 if (expand_stack(vma, address)) 474 goto out_up; 475 } 476 477 /* 478 * Ok, we have a good vm_area for this memory access, so 479 * we can handle it.. 480 */ 481 fault = VM_FAULT_BADACCESS; 482 if (unlikely(!(vma->vm_flags & access))) 483 goto out_up; 484 485 if (is_vm_hugetlb_page(vma)) 486 address &= HPAGE_MASK; 487 /* 488 * If for any reason at all we couldn't handle the fault, 489 * make sure we exit gracefully rather than endlessly redo 490 * the fault. 491 */ 492 fault = handle_mm_fault(vma, address, flags); 493 /* No reason to continue if interrupted by SIGKILL. */ 494 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) { 495 fault = VM_FAULT_SIGNAL; 496 if (flags & FAULT_FLAG_RETRY_NOWAIT) 497 goto out_up; 498 goto out; 499 } 500 if (unlikely(fault & VM_FAULT_ERROR)) 501 goto out_up; 502 503 /* 504 * Major/minor page fault accounting is only done on the 505 * initial attempt. If we go through a retry, it is extremely 506 * likely that the page will be found in page cache at that point. 507 */ 508 if (flags & FAULT_FLAG_ALLOW_RETRY) { 509 if (fault & VM_FAULT_MAJOR) { 510 tsk->maj_flt++; 511 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 512 regs, address); 513 } else { 514 tsk->min_flt++; 515 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 516 regs, address); 517 } 518 if (fault & VM_FAULT_RETRY) { 519 if (IS_ENABLED(CONFIG_PGSTE) && gmap && 520 (flags & FAULT_FLAG_RETRY_NOWAIT)) { 521 /* FAULT_FLAG_RETRY_NOWAIT has been set, 522 * mmap_sem has not been released */ 523 current->thread.gmap_pfault = 1; 524 fault = VM_FAULT_PFAULT; 525 goto out_up; 526 } 527 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk 528 * of starvation. */ 529 flags &= ~(FAULT_FLAG_ALLOW_RETRY | 530 FAULT_FLAG_RETRY_NOWAIT); 531 flags |= FAULT_FLAG_TRIED; 532 down_read(&mm->mmap_sem); 533 goto retry; 534 } 535 } 536 if (IS_ENABLED(CONFIG_PGSTE) && gmap) { 537 address = __gmap_link(gmap, current->thread.gmap_addr, 538 address); 539 if (address == -EFAULT) { 540 fault = VM_FAULT_BADMAP; 541 goto out_up; 542 } 543 if (address == -ENOMEM) { 544 fault = VM_FAULT_OOM; 545 goto out_up; 546 } 547 } 548 fault = 0; 549 out_up: 550 up_read(&mm->mmap_sem); 551 out: 552 return fault; 553 } 554 555 void do_protection_exception(struct pt_regs *regs) 556 { 557 unsigned long trans_exc_code; 558 int access; 559 vm_fault_t fault; 560 561 trans_exc_code = regs->int_parm_long; 562 /* 563 * Protection exceptions are suppressing, decrement psw address. 564 * The exception to this rule are aborted transactions, for these 565 * the PSW already points to the correct location. 566 */ 567 if (!(regs->int_code & 0x200)) 568 regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16); 569 /* 570 * Check for low-address protection. This needs to be treated 571 * as a special case because the translation exception code 572 * field is not guaranteed to contain valid data in this case. 573 */ 574 if (unlikely(!(trans_exc_code & 4))) { 575 do_low_address(regs); 576 return; 577 } 578 if (unlikely(MACHINE_HAS_NX && (trans_exc_code & 0x80))) { 579 regs->int_parm_long = (trans_exc_code & ~PAGE_MASK) | 580 (regs->psw.addr & PAGE_MASK); 581 access = VM_EXEC; 582 fault = VM_FAULT_BADACCESS; 583 } else { 584 access = VM_WRITE; 585 fault = do_exception(regs, access); 586 } 587 if (unlikely(fault)) 588 do_fault_error(regs, access, fault); 589 } 590 NOKPROBE_SYMBOL(do_protection_exception); 591 592 void do_dat_exception(struct pt_regs *regs) 593 { 594 int access; 595 vm_fault_t fault; 596 597 access = VM_READ | VM_EXEC | VM_WRITE; 598 fault = do_exception(regs, access); 599 if (unlikely(fault)) 600 do_fault_error(regs, access, fault); 601 } 602 NOKPROBE_SYMBOL(do_dat_exception); 603 604 #ifdef CONFIG_PFAULT 605 /* 606 * 'pfault' pseudo page faults routines. 607 */ 608 static int pfault_disable; 609 610 static int __init nopfault(char *str) 611 { 612 pfault_disable = 1; 613 return 1; 614 } 615 616 __setup("nopfault", nopfault); 617 618 struct pfault_refbk { 619 u16 refdiagc; 620 u16 reffcode; 621 u16 refdwlen; 622 u16 refversn; 623 u64 refgaddr; 624 u64 refselmk; 625 u64 refcmpmk; 626 u64 reserved; 627 } __attribute__ ((packed, aligned(8))); 628 629 static struct pfault_refbk pfault_init_refbk = { 630 .refdiagc = 0x258, 631 .reffcode = 0, 632 .refdwlen = 5, 633 .refversn = 2, 634 .refgaddr = __LC_LPP, 635 .refselmk = 1ULL << 48, 636 .refcmpmk = 1ULL << 48, 637 .reserved = __PF_RES_FIELD 638 }; 639 640 int pfault_init(void) 641 { 642 int rc; 643 644 if (pfault_disable) 645 return -1; 646 diag_stat_inc(DIAG_STAT_X258); 647 asm volatile( 648 " diag %1,%0,0x258\n" 649 "0: j 2f\n" 650 "1: la %0,8\n" 651 "2:\n" 652 EX_TABLE(0b,1b) 653 : "=d" (rc) 654 : "a" (&pfault_init_refbk), "m" (pfault_init_refbk) : "cc"); 655 return rc; 656 } 657 658 static struct pfault_refbk pfault_fini_refbk = { 659 .refdiagc = 0x258, 660 .reffcode = 1, 661 .refdwlen = 5, 662 .refversn = 2, 663 }; 664 665 void pfault_fini(void) 666 { 667 668 if (pfault_disable) 669 return; 670 diag_stat_inc(DIAG_STAT_X258); 671 asm volatile( 672 " diag %0,0,0x258\n" 673 "0: nopr %%r7\n" 674 EX_TABLE(0b,0b) 675 : : "a" (&pfault_fini_refbk), "m" (pfault_fini_refbk) : "cc"); 676 } 677 678 static DEFINE_SPINLOCK(pfault_lock); 679 static LIST_HEAD(pfault_list); 680 681 #define PF_COMPLETE 0x0080 682 683 /* 684 * The mechanism of our pfault code: if Linux is running as guest, runs a user 685 * space process and the user space process accesses a page that the host has 686 * paged out we get a pfault interrupt. 687 * 688 * This allows us, within the guest, to schedule a different process. Without 689 * this mechanism the host would have to suspend the whole virtual cpu until 690 * the page has been paged in. 691 * 692 * So when we get such an interrupt then we set the state of the current task 693 * to uninterruptible and also set the need_resched flag. Both happens within 694 * interrupt context(!). If we later on want to return to user space we 695 * recognize the need_resched flag and then call schedule(). It's not very 696 * obvious how this works... 697 * 698 * Of course we have a lot of additional fun with the completion interrupt (-> 699 * host signals that a page of a process has been paged in and the process can 700 * continue to run). This interrupt can arrive on any cpu and, since we have 701 * virtual cpus, actually appear before the interrupt that signals that a page 702 * is missing. 703 */ 704 static void pfault_interrupt(struct ext_code ext_code, 705 unsigned int param32, unsigned long param64) 706 { 707 struct task_struct *tsk; 708 __u16 subcode; 709 pid_t pid; 710 711 /* 712 * Get the external interruption subcode & pfault initial/completion 713 * signal bit. VM stores this in the 'cpu address' field associated 714 * with the external interrupt. 715 */ 716 subcode = ext_code.subcode; 717 if ((subcode & 0xff00) != __SUBCODE_MASK) 718 return; 719 inc_irq_stat(IRQEXT_PFL); 720 /* Get the token (= pid of the affected task). */ 721 pid = param64 & LPP_PID_MASK; 722 rcu_read_lock(); 723 tsk = find_task_by_pid_ns(pid, &init_pid_ns); 724 if (tsk) 725 get_task_struct(tsk); 726 rcu_read_unlock(); 727 if (!tsk) 728 return; 729 spin_lock(&pfault_lock); 730 if (subcode & PF_COMPLETE) { 731 /* signal bit is set -> a page has been swapped in by VM */ 732 if (tsk->thread.pfault_wait == 1) { 733 /* Initial interrupt was faster than the completion 734 * interrupt. pfault_wait is valid. Set pfault_wait 735 * back to zero and wake up the process. This can 736 * safely be done because the task is still sleeping 737 * and can't produce new pfaults. */ 738 tsk->thread.pfault_wait = 0; 739 list_del(&tsk->thread.list); 740 wake_up_process(tsk); 741 put_task_struct(tsk); 742 } else { 743 /* Completion interrupt was faster than initial 744 * interrupt. Set pfault_wait to -1 so the initial 745 * interrupt doesn't put the task to sleep. 746 * If the task is not running, ignore the completion 747 * interrupt since it must be a leftover of a PFAULT 748 * CANCEL operation which didn't remove all pending 749 * completion interrupts. */ 750 if (tsk->state == TASK_RUNNING) 751 tsk->thread.pfault_wait = -1; 752 } 753 } else { 754 /* signal bit not set -> a real page is missing. */ 755 if (WARN_ON_ONCE(tsk != current)) 756 goto out; 757 if (tsk->thread.pfault_wait == 1) { 758 /* Already on the list with a reference: put to sleep */ 759 goto block; 760 } else if (tsk->thread.pfault_wait == -1) { 761 /* Completion interrupt was faster than the initial 762 * interrupt (pfault_wait == -1). Set pfault_wait 763 * back to zero and exit. */ 764 tsk->thread.pfault_wait = 0; 765 } else { 766 /* Initial interrupt arrived before completion 767 * interrupt. Let the task sleep. 768 * An extra task reference is needed since a different 769 * cpu may set the task state to TASK_RUNNING again 770 * before the scheduler is reached. */ 771 get_task_struct(tsk); 772 tsk->thread.pfault_wait = 1; 773 list_add(&tsk->thread.list, &pfault_list); 774 block: 775 /* Since this must be a userspace fault, there 776 * is no kernel task state to trample. Rely on the 777 * return to userspace schedule() to block. */ 778 __set_current_state(TASK_UNINTERRUPTIBLE); 779 set_tsk_need_resched(tsk); 780 set_preempt_need_resched(); 781 } 782 } 783 out: 784 spin_unlock(&pfault_lock); 785 put_task_struct(tsk); 786 } 787 788 static int pfault_cpu_dead(unsigned int cpu) 789 { 790 struct thread_struct *thread, *next; 791 struct task_struct *tsk; 792 793 spin_lock_irq(&pfault_lock); 794 list_for_each_entry_safe(thread, next, &pfault_list, list) { 795 thread->pfault_wait = 0; 796 list_del(&thread->list); 797 tsk = container_of(thread, struct task_struct, thread); 798 wake_up_process(tsk); 799 put_task_struct(tsk); 800 } 801 spin_unlock_irq(&pfault_lock); 802 return 0; 803 } 804 805 static int __init pfault_irq_init(void) 806 { 807 int rc; 808 809 rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt); 810 if (rc) 811 goto out_extint; 812 rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP; 813 if (rc) 814 goto out_pfault; 815 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL); 816 cpuhp_setup_state_nocalls(CPUHP_S390_PFAULT_DEAD, "s390/pfault:dead", 817 NULL, pfault_cpu_dead); 818 return 0; 819 820 out_pfault: 821 unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt); 822 out_extint: 823 pfault_disable = 1; 824 return rc; 825 } 826 early_initcall(pfault_irq_init); 827 828 #endif /* CONFIG_PFAULT */ 829