remove libdss from Makefile
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / mm / fadvise.c
index 702f239cd6db534b20276bce62ae1d2176bdb664..3f5f68ad57080f6909e8fcc91a516d946da5d519 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * mm/fadvise.c
  *
@@ -70,8 +71,12 @@ SYSCALL_DEFINE4(fadvise64_64, int, fd, loff_t, offset, loff_t, len, int, advice)
                goto out;
        }
 
-       /* Careful about overflows. Len == 0 means "as much as possible" */
-       endbyte = offset + len;
+       /*
+        * Careful about overflows. Len == 0 means "as much as possible".  Use
+        * unsigned math because signed overflows are undefined and UBSan
+        * complains.
+        */
+       endbyte = (u64)offset + (u64)len;
        if (!len || endbyte < len)
                endbyte = -1;
        else
@@ -126,7 +131,15 @@ SYSCALL_DEFINE4(fadvise64_64, int, fd, loff_t, offset, loff_t, len, int, advice)
                 */
                start_index = (offset+(PAGE_SIZE-1)) >> PAGE_SHIFT;
                end_index = (endbyte >> PAGE_SHIFT);
-               if ((endbyte & ~PAGE_MASK) != ~PAGE_MASK) {
+               /*
+                * The page at end_index will be inclusively discarded according
+                * by invalidate_mapping_pages(), so subtracting 1 from
+                * end_index means we will skip the last page.  But if endbyte
+                * is page aligned or is at the end of file, we should not skip
+                * that page - discarding the last page is safe enough.
+                */
+               if ((endbyte & ~PAGE_MASK) != ~PAGE_MASK &&
+                               endbyte != inode->i_size - 1) {
                        /* First page is tricky as 0 - 1 = -1, but pgoff_t
                         * is unsigned, so the end_index >= start_index
                         * check below would be true and we'll discard the whole