mtd: fix build with GCC 14
[openwrt/openwrt.git] / target / linux / generic / backport-5.15 / 020-v6.3-20-mm-support-POSIX_FADV_NOREUSE.patch
1 From 686c3d4f71de9e0e7a27f03a5617a712385f90cd Mon Sep 17 00:00:00 2001
2 From: Yu Zhao <yuzhao@google.com>
3 Date: Fri, 30 Dec 2022 14:52:52 -0700
4 Subject: [PATCH 20/29] mm: support POSIX_FADV_NOREUSE
5
6 This patch adds POSIX_FADV_NOREUSE to vma_has_recency() so that the LRU
7 algorithm can ignore access to mapped files marked by this flag.
8
9 The advantages of POSIX_FADV_NOREUSE are:
10 1. Unlike MADV_SEQUENTIAL and MADV_RANDOM, it does not alter the
11 default readahead behavior.
12 2. Unlike MADV_SEQUENTIAL and MADV_RANDOM, it does not split VMAs and
13 therefore does not take mmap_lock.
14 3. Unlike MADV_COLD, setting it has a negligible cost, regardless of
15 how many pages it affects.
16
17 Its limitations are:
18 1. Like POSIX_FADV_RANDOM and POSIX_FADV_SEQUENTIAL, it currently does
19 not support range. IOW, its scope is the entire file.
20 2. It currently does not ignore access through file descriptors.
21 Specifically, for the active/inactive LRU, given a file page shared
22 by two users and one of them having set POSIX_FADV_NOREUSE on the
23 file, this page will be activated upon the second user accessing
24 it. This corner case can be covered by checking POSIX_FADV_NOREUSE
25 before calling mark_page_accessed() on the read path. But it is
26 considered not worth the effort.
27
28 There have been a few attempts to support POSIX_FADV_NOREUSE, e.g., [1].
29 This time the goal is to fill a niche: a few desktop applications, e.g.,
30 large file transferring and video encoding/decoding, want fast file
31 streaming with mmap() rather than direct IO. Among those applications, an
32 SVT-AV1 regression was reported when running with MGLRU [2]. The
33 following test can reproduce that regression.
34
35 kb=$(awk '/MemTotal/ { print $2 }' /proc/meminfo)
36 kb=$((kb - 8*1024*1024))
37
38 modprobe brd rd_nr=1 rd_size=$kb
39 dd if=/dev/zero of=/dev/ram0 bs=1M
40
41 mkfs.ext4 /dev/ram0
42 mount /dev/ram0 /mnt/
43 swapoff -a
44
45 fallocate -l 8G /mnt/swapfile
46 mkswap /mnt/swapfile
47 swapon /mnt/swapfile
48
49 wget http://ultravideo.cs.tut.fi/video/Bosphorus_3840x2160_120fps_420_8bit_YUV_Y4M.7z
50 7z e -o/mnt/ Bosphorus_3840x2160_120fps_420_8bit_YUV_Y4M.7z
51 SvtAv1EncApp --preset 12 -w 3840 -h 2160 \
52 -i /mnt/Bosphorus_3840x2160.y4m
53
54 For MGLRU, the following change showed a [9-11]% increase in FPS,
55 which makes it on par with the active/inactive LRU.
56
57 patch Source/App/EncApp/EbAppMain.c <<EOF
58 31a32
59 > #include <fcntl.h>
60 35d35
61 < #include <fcntl.h> /* _O_BINARY */
62 117a118
63 > posix_fadvise(config->mmap.fd, 0, 0, POSIX_FADV_NOREUSE);
64 EOF
65
66 [1] https://lore.kernel.org/r/1308923350-7932-1-git-send-email-andrea@betterlinux.com/
67 [2] https://openbenchmarking.org/result/2209259-PTS-MGLRU8GB57
68
69 Link: https://lkml.kernel.org/r/20221230215252.2628425-2-yuzhao@google.com
70 Signed-off-by: Yu Zhao <yuzhao@google.com>
71 Cc: Alexander Viro <viro@zeniv.linux.org.uk>
72 Cc: Andrea Righi <andrea.righi@canonical.com>
73 Cc: Johannes Weiner <hannes@cmpxchg.org>
74 Cc: Michael Larabel <Michael@MichaelLarabel.com>
75 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
76 ---
77 include/linux/fs.h | 2 ++
78 include/linux/mm_inline.h | 3 +++
79 mm/fadvise.c | 5 ++++-
80 3 files changed, 9 insertions(+), 1 deletion(-)
81
82 --- a/include/linux/fs.h
83 +++ b/include/linux/fs.h
84 @@ -167,6 +167,8 @@ typedef int (dio_iodone_t)(struct kiocb
85 /* File is stream-like */
86 #define FMODE_STREAM ((__force fmode_t)0x200000)
87
88 +#define FMODE_NOREUSE ((__force fmode_t)0x400000)
89 +
90 /* File was opened by fanotify and shouldn't generate fanotify events */
91 #define FMODE_NONOTIFY ((__force fmode_t)0x4000000)
92
93 --- a/include/linux/mm_inline.h
94 +++ b/include/linux/mm_inline.h
95 @@ -339,6 +339,9 @@ static inline bool vma_has_recency(struc
96 if (vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ))
97 return false;
98
99 + if (vma->vm_file && (vma->vm_file->f_mode & FMODE_NOREUSE))
100 + return false;
101 +
102 return true;
103 }
104
105 --- a/mm/fadvise.c
106 +++ b/mm/fadvise.c
107 @@ -80,7 +80,7 @@ int generic_fadvise(struct file *file, l
108 case POSIX_FADV_NORMAL:
109 file->f_ra.ra_pages = bdi->ra_pages;
110 spin_lock(&file->f_lock);
111 - file->f_mode &= ~FMODE_RANDOM;
112 + file->f_mode &= ~(FMODE_RANDOM | FMODE_NOREUSE);
113 spin_unlock(&file->f_lock);
114 break;
115 case POSIX_FADV_RANDOM:
116 @@ -107,6 +107,9 @@ int generic_fadvise(struct file *file, l
117 force_page_cache_readahead(mapping, file, start_index, nrpages);
118 break;
119 case POSIX_FADV_NOREUSE:
120 + spin_lock(&file->f_lock);
121 + file->f_mode |= FMODE_NOREUSE;
122 + spin_unlock(&file->f_lock);
123 break;
124 case POSIX_FADV_DONTNEED:
125 if (!inode_write_congested(mapping->host))