Revert "fs: do not prefault sys_write() user buffer pages"

This reverts commit 998ef75ddb5709bbea0bf1506cd2717348a3c647.

The commit itself does not appear to be buggy per se, but it is exposing
a bug in ext4 (and Ted thinks ext3 too, but we solved that by getting
rid of it). It's too late in the release cycle to really worry about
this, even if Dave Hansen has a patch that may actually fix the
underlying ext4 problem. We can (and should) revisit this for the next
release.

The problem is that moving the prefaulting later now exposes a special
case with partially successful writes that isn't handled correctly. And
the prefaulting likely isn't normally even that much of a performance
issue - it looks like at least one reason Dave saw this in his
performance tests is that he also ran them on Skylake that now supports
the new SMAP code, which makes the normally very cheap user space
prefaulting noticeably more expensive.

Bisected-and-acked-by: Ted Ts'o <tytso@mit.edu>
Analyzed-and-acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Changed files
+16 -18
mm
+16 -18
mm/filemap.c
··· 2473 2473 iov_iter_count(i)); 2474 2474 2475 2475 again: 2476 + /* 2477 + * Bring in the user page that we will copy from _first_. 2478 + * Otherwise there's a nasty deadlock on copying from the 2479 + * same page as we're writing to, without it being marked 2480 + * up-to-date. 2481 + * 2482 + * Not only is this an optimisation, but it is also required 2483 + * to check that the address is actually valid, when atomic 2484 + * usercopies are used, below. 2485 + */ 2486 + if (unlikely(iov_iter_fault_in_readable(i, bytes))) { 2487 + status = -EFAULT; 2488 + break; 2489 + } 2490 + 2476 2491 status = a_ops->write_begin(file, mapping, pos, bytes, flags, 2477 2492 &page, &fsdata); 2478 2493 if (unlikely(status < 0)) ··· 2495 2480 2496 2481 if (mapping_writably_mapped(mapping)) 2497 2482 flush_dcache_page(page); 2498 - /* 2499 - * 'page' is now locked. If we are trying to copy from a 2500 - * mapping of 'page' in userspace, the copy might fault and 2501 - * would need PageUptodate() to complete. But, page can not be 2502 - * made Uptodate without acquiring the page lock, which we hold. 2503 - * Deadlock. Avoid with pagefault_disable(). Fix up below with 2504 - * iov_iter_fault_in_readable(). 2505 - */ 2506 - pagefault_disable(); 2483 + 2507 2484 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes); 2508 - pagefault_enable(); 2509 2485 flush_dcache_page(page); 2510 2486 2511 2487 status = a_ops->write_end(file, mapping, pos, bytes, copied, ··· 2519 2513 */ 2520 2514 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset, 2521 2515 iov_iter_single_seg_count(i)); 2522 - /* 2523 - * This is the fallback to recover if the copy from 2524 - * userspace above faults. 2525 - */ 2526 - if (unlikely(iov_iter_fault_in_readable(i, bytes))) { 2527 - status = -EFAULT; 2528 - break; 2529 - } 2530 2516 goto again; 2531 2517 } 2532 2518 pos += copied;