License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 17:07:57 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* linux/mm/page_io.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
|
|
|
|
*
|
|
|
|
* Swap reorganised 29.12.95,
|
|
|
|
* Asynchronous swapping added 30.12.95. Stephen Tweedie
|
|
|
|
* Removed race in async swapping. 14.4.1996. Bruno Haible
|
|
|
|
* Add swap of shared pages through the page cache. 20.2.1998. Stephen Tweedie
|
|
|
|
* Always use brw_page, life becomes simpler. 12 May 1998 Eric Biederman
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/kernel_stat.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
|
|
|
#include <linux/gfp.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/swap.h>
|
|
|
|
#include <linux/bio.h>
|
|
|
|
#include <linux/swapops.h>
|
2012-08-01 03:44:55 +04:00
|
|
|
#include <linux/buffer_head.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <linux/writeback.h>
|
2012-04-10 03:08:06 +04:00
|
|
|
#include <linux/frontswap.h>
|
2013-07-04 02:01:24 +04:00
|
|
|
#include <linux/blkdev.h>
|
2019-12-01 04:58:29 +03:00
|
|
|
#include <linux/psi.h>
|
2015-02-22 19:58:50 +03:00
|
|
|
#include <linux/uio.h>
|
2017-08-02 23:32:09 +03:00
|
|
|
#include <linux/sched/task.h>
|
2022-01-20 05:10:02 +03:00
|
|
|
#include <linux/delayacct.h>
|
2022-05-10 04:20:47 +03:00
|
|
|
#include "swap.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2015-07-20 16:29:37 +03:00
|
|
|
void end_swap_bio_write(struct bio *bio)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2017-12-18 15:22:04 +03:00
|
|
|
struct page *page = bio_first_page_all(bio);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2017-06-03 10:38:06 +03:00
|
|
|
if (bio->bi_status) {
|
2005-04-17 02:20:36 +04:00
|
|
|
SetPageError(page);
|
2006-09-26 10:31:26 +04:00
|
|
|
/*
|
|
|
|
* We failed to write the page out to swap-space.
|
|
|
|
* Re-dirty the page in order to avoid it being reclaimed.
|
|
|
|
* Also print a dire warning that things will go BAD (tm)
|
|
|
|
* very quickly.
|
|
|
|
*
|
2020-12-08 09:25:39 +03:00
|
|
|
* Also clear PG_reclaim to avoid folio_rotate_reclaimable()
|
2006-09-26 10:31:26 +04:00
|
|
|
*/
|
|
|
|
set_page_dirty(page);
|
2021-02-24 23:03:01 +03:00
|
|
|
pr_alert_ratelimited("Write-error on swap-device (%u:%u:%llu)\n",
|
|
|
|
MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
|
|
|
|
(unsigned long long)bio->bi_iter.bi_sector);
|
2006-09-26 10:31:26 +04:00
|
|
|
ClearPageReclaim(page);
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
end_page_writeback(page);
|
|
|
|
bio_put(bio);
|
|
|
|
}
|
|
|
|
|
2015-07-20 16:29:37 +03:00
|
|
|
static void end_swap_bio_read(struct bio *bio)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2017-12-18 15:22:04 +03:00
|
|
|
struct page *page = bio_first_page_all(bio);
|
swap: add block io poll in swapin path
For fast flash disk, async IO could introduce overhead because of
context switch. block-mq now supports IO poll, which improves
performance and latency a lot. swapin is a good place to use this
technique, because the task is waiting for the swapin page to continue
execution.
In my virtual machine, directly read 4k data from a NVMe with iopoll is
about 60% better than that without poll. With iopoll support in swapin
patch, my microbenchmark (a task does random memory write) is about
10%~25% faster. CPU utilization increases a lot though, 2x and even 3x
CPU utilization. This will depend on disk speed.
While iopoll in swapin isn't intended for all usage cases, it's a win
for latency sensistive workloads with high speed swap disk. block layer
has knob to control poll in runtime. If poll isn't enabled in block
layer, there should be no noticeable change in swapin.
I got a chance to run the same test in a NVMe with DRAM as the media.
In simple fio IO test, blkpoll boosts 50% performance in single thread
test and ~20% in 8 threads test. So this is the base line. In above
swap test, blkpoll boosts ~27% performance in single thread test.
blkpoll uses 2x CPU time though.
If we enable hybid polling, the performance gain has very slight drop
but CPU time is only 50% worse than that without blkpoll. Also we can
adjust parameter of hybid poll, with it, the CPU time penality is
reduced further. In 8 threads test, blkpoll doesn't help though. The
performance is similar to that without blkpoll, but cpu utilization is
similar too. There is lock contention in swap path. The cpu time
spending on blkpoll isn't high. So overall, blkpoll swapin isn't worse
than that without it.
The swapin readahead might read several pages in in the same time and
form a big IO request. Since the IO will take longer time, it doesn't
make sense to do poll, so the patch only does iopoll for single page
swapin.
[akpm@linux-foundation.org: coding-style fixes]
Link: http://lkml.kernel.org/r/070c3c3e40b711e7b1390002c991e86a-b5408f0@7511894063d3764ff01ea8111f5a004d7dd700ed078797c204a24e620ddb965c
Signed-off-by: Shaohua Li <shli@fb.com>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Huang Ying <ying.huang@intel.com>
Cc: Jens Axboe <axboe@fb.com>
Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-07-11 01:47:11 +03:00
|
|
|
struct task_struct *waiter = bio->bi_private;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2017-06-03 10:38:06 +03:00
|
|
|
if (bio->bi_status) {
|
2005-04-17 02:20:36 +04:00
|
|
|
SetPageError(page);
|
|
|
|
ClearPageUptodate(page);
|
2021-02-24 23:03:01 +03:00
|
|
|
pr_alert_ratelimited("Read-error on swap-device (%u:%u:%llu)\n",
|
|
|
|
MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
|
|
|
|
(unsigned long long)bio->bi_iter.bi_sector);
|
2013-07-04 02:01:24 +04:00
|
|
|
goto out;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2013-07-04 02:01:24 +04:00
|
|
|
|
|
|
|
SetPageUptodate(page);
|
|
|
|
out:
|
2005-04-17 02:20:36 +04:00
|
|
|
unlock_page(page);
|
swap: add block io poll in swapin path
For fast flash disk, async IO could introduce overhead because of
context switch. block-mq now supports IO poll, which improves
performance and latency a lot. swapin is a good place to use this
technique, because the task is waiting for the swapin page to continue
execution.
In my virtual machine, directly read 4k data from a NVMe with iopoll is
about 60% better than that without poll. With iopoll support in swapin
patch, my microbenchmark (a task does random memory write) is about
10%~25% faster. CPU utilization increases a lot though, 2x and even 3x
CPU utilization. This will depend on disk speed.
While iopoll in swapin isn't intended for all usage cases, it's a win
for latency sensistive workloads with high speed swap disk. block layer
has knob to control poll in runtime. If poll isn't enabled in block
layer, there should be no noticeable change in swapin.
I got a chance to run the same test in a NVMe with DRAM as the media.
In simple fio IO test, blkpoll boosts 50% performance in single thread
test and ~20% in 8 threads test. So this is the base line. In above
swap test, blkpoll boosts ~27% performance in single thread test.
blkpoll uses 2x CPU time though.
If we enable hybid polling, the performance gain has very slight drop
but CPU time is only 50% worse than that without blkpoll. Also we can
adjust parameter of hybid poll, with it, the CPU time penality is
reduced further. In 8 threads test, blkpoll doesn't help though. The
performance is similar to that without blkpoll, but cpu utilization is
similar too. There is lock contention in swap path. The cpu time
spending on blkpoll isn't high. So overall, blkpoll swapin isn't worse
than that without it.
The swapin readahead might read several pages in in the same time and
form a big IO request. Since the IO will take longer time, it doesn't
make sense to do poll, so the patch only does iopoll for single page
swapin.
[akpm@linux-foundation.org: coding-style fixes]
Link: http://lkml.kernel.org/r/070c3c3e40b711e7b1390002c991e86a-b5408f0@7511894063d3764ff01ea8111f5a004d7dd700ed078797c204a24e620ddb965c
Signed-off-by: Shaohua Li <shli@fb.com>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Huang Ying <ying.huang@intel.com>
Cc: Jens Axboe <axboe@fb.com>
Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-07-11 01:47:11 +03:00
|
|
|
WRITE_ONCE(bio->bi_private, NULL);
|
2005-04-17 02:20:36 +04:00
|
|
|
bio_put(bio);
|
2019-07-05 01:14:49 +03:00
|
|
|
if (waiter) {
|
|
|
|
blk_wake_io_task(waiter);
|
|
|
|
put_task_struct(waiter);
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2012-08-01 03:44:57 +04:00
|
|
|
int generic_swapfile_activate(struct swap_info_struct *sis,
|
|
|
|
struct file *swap_file,
|
|
|
|
sector_t *span)
|
|
|
|
{
|
|
|
|
struct address_space *mapping = swap_file->f_mapping;
|
|
|
|
struct inode *inode = mapping->host;
|
|
|
|
unsigned blocks_per_page;
|
|
|
|
unsigned long page_no;
|
|
|
|
unsigned blkbits;
|
|
|
|
sector_t probe_block;
|
|
|
|
sector_t last_block;
|
|
|
|
sector_t lowest_block = -1;
|
|
|
|
sector_t highest_block = 0;
|
|
|
|
int nr_extents = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
blkbits = inode->i_blkbits;
|
|
|
|
blocks_per_page = PAGE_SIZE >> blkbits;
|
|
|
|
|
|
|
|
/*
|
mm, swap: use rbtree for swap_extent
swap_extent is used to map swap page offset to backing device's block
offset. For a continuous block range, one swap_extent is used and all
these swap_extents are managed in a linked list.
These swap_extents are used by map_swap_entry() during swap's read and
write path. To find out the backing device's block offset for a page
offset, the swap_extent list will be traversed linearly, with
curr_swap_extent being used as a cache to speed up the search.
This works well as long as swap_extents are not huge or when the number
of processes that access swap device are few, but when the swap device
has many extents and there are a number of processes accessing the swap
device concurrently, it can be a problem. On one of our servers, the
disk's remaining size is tight:
$df -h
Filesystem Size Used Avail Use% Mounted on
... ...
/dev/nvme0n1p1 1.8T 1.3T 504G 72% /home/t4
When creating a 80G swapfile there, there are as many as 84656 swap
extents. The end result is, kernel spends abou 30% time in
map_swap_entry() and swap throughput is only 70MB/s.
As a comparison, when I used smaller sized swapfile, like 4G whose
swap_extent dropped to 2000, swap throughput is back to 400-500MB/s and
map_swap_entry() is about 3%.
One downside of using rbtree for swap_extent is, 'struct rbtree' takes
24 bytes while 'struct list_head' takes 16 bytes, that's 8 bytes more
for each swap_extent. For a swapfile that has 80k swap_extents, that
means 625KiB more memory consumed.
Test:
Since it's not possible to reboot that server, I can not test this patch
diretly there. Instead, I tested it on another server with NVMe disk.
I created a 20G swapfile on an NVMe backed XFS fs. By default, the
filesystem is quite clean and the created swapfile has only 2 extents.
Testing vanilla and this patch shows no obvious performance difference
when swapfile is not fragmented.
To see the patch's effects, I used some tweaks to manually fragment the
swapfile by breaking the extent at 1M boundary. This made the swapfile
have 20K extents.
nr_task=4
kernel swapout(KB/s) map_swap_entry(perf) swapin(KB/s) map_swap_entry(perf)
vanilla 165191 90.77% 171798 90.21%
patched 858993 +420% 2.16% 715827 +317% 0.77%
nr_task=8
kernel swapout(KB/s) map_swap_entry(perf) swapin(KB/s) map_swap_entry(perf)
vanilla 306783 92.19% 318145 87.76%
patched 954437 +211% 2.35% 1073741 +237% 1.57%
swapout: the throughput of swap out, in KB/s, higher is better 1st
map_swap_entry: cpu cycles percent sampled by perf swapin: the
throughput of swap in, in KB/s, higher is better. 2nd map_swap_entry:
cpu cycles percent sampled by perf
nr_task=1 doesn't show any difference, this is due to the curr_swap_extent
can be effectively used to cache the correct swap extent for single task
workload.
[akpm@linux-foundation.org: s/BUG_ON(1)/BUG()/]
Link: http://lkml.kernel.org/r/20190523142404.GA181@aaronlu
Signed-off-by: Aaron Lu <ziqian.lzq@antfin.com>
Cc: Huang Ying <ying.huang@intel.com>
Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-07-12 06:55:41 +03:00
|
|
|
* Map all the blocks into the extent tree. This code doesn't try
|
2012-08-01 03:44:57 +04:00
|
|
|
* to be very smart.
|
|
|
|
*/
|
|
|
|
probe_block = 0;
|
|
|
|
page_no = 0;
|
|
|
|
last_block = i_size_read(inode) >> blkbits;
|
|
|
|
while ((probe_block + blocks_per_page) <= last_block &&
|
|
|
|
page_no < sis->max) {
|
|
|
|
unsigned block_in_page;
|
|
|
|
sector_t first_block;
|
|
|
|
|
2016-07-29 01:48:47 +03:00
|
|
|
cond_resched();
|
|
|
|
|
2020-01-09 16:30:41 +03:00
|
|
|
first_block = probe_block;
|
|
|
|
ret = bmap(inode, &first_block);
|
|
|
|
if (ret || !first_block)
|
2012-08-01 03:44:57 +04:00
|
|
|
goto bad_bmap;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It must be PAGE_SIZE aligned on-disk
|
|
|
|
*/
|
|
|
|
if (first_block & (blocks_per_page - 1)) {
|
|
|
|
probe_block++;
|
|
|
|
goto reprobe;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (block_in_page = 1; block_in_page < blocks_per_page;
|
|
|
|
block_in_page++) {
|
|
|
|
sector_t block;
|
|
|
|
|
2020-01-09 16:30:41 +03:00
|
|
|
block = probe_block + block_in_page;
|
|
|
|
ret = bmap(inode, &block);
|
|
|
|
if (ret || !block)
|
2012-08-01 03:44:57 +04:00
|
|
|
goto bad_bmap;
|
2020-01-09 16:30:41 +03:00
|
|
|
|
2012-08-01 03:44:57 +04:00
|
|
|
if (block != first_block + block_in_page) {
|
|
|
|
/* Discontiguity */
|
|
|
|
probe_block++;
|
|
|
|
goto reprobe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
first_block >>= (PAGE_SHIFT - blkbits);
|
|
|
|
if (page_no) { /* exclude the header page */
|
|
|
|
if (first_block < lowest_block)
|
|
|
|
lowest_block = first_block;
|
|
|
|
if (first_block > highest_block)
|
|
|
|
highest_block = first_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
|
|
|
|
*/
|
|
|
|
ret = add_swap_extent(sis, page_no, 1, first_block);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
nr_extents += ret;
|
|
|
|
page_no++;
|
|
|
|
probe_block += blocks_per_page;
|
|
|
|
reprobe:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ret = nr_extents;
|
|
|
|
*span = 1 + highest_block - lowest_block;
|
|
|
|
if (page_no == 0)
|
|
|
|
page_no = 1; /* force Empty message */
|
|
|
|
sis->max = page_no;
|
|
|
|
sis->pages = page_no - 1;
|
|
|
|
sis->highest_bit = page_no - 1;
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
bad_bmap:
|
2016-03-18 00:19:50 +03:00
|
|
|
pr_err("swapon: swapfile has holes\n");
|
2012-08-01 03:44:57 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* We may have stale swap cache pages in memory: notice
|
|
|
|
* them here and get rid of the unnecessary final write.
|
|
|
|
*/
|
|
|
|
int swap_writepage(struct page *page, struct writeback_control *wbc)
|
|
|
|
{
|
2013-04-30 02:08:34 +04:00
|
|
|
int ret = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2009-01-07 01:39:36 +03:00
|
|
|
if (try_to_free_swap(page)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
unlock_page(page);
|
|
|
|
goto out;
|
|
|
|
}
|
2020-05-13 18:37:49 +03:00
|
|
|
/*
|
|
|
|
* Arch code may have to preserve more data than just the page
|
|
|
|
* contents, e.g. memory tags.
|
|
|
|
*/
|
|
|
|
ret = arch_prepare_to_swap(page);
|
|
|
|
if (ret) {
|
|
|
|
set_page_dirty(page);
|
|
|
|
unlock_page(page);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-05-15 19:32:15 +04:00
|
|
|
if (frontswap_store(page) == 0) {
|
2012-04-10 03:08:06 +04:00
|
|
|
set_page_writeback(page);
|
|
|
|
unlock_page(page);
|
|
|
|
end_page_writeback(page);
|
|
|
|
goto out;
|
|
|
|
}
|
2013-04-30 02:08:35 +04:00
|
|
|
ret = __swap_writepage(page, wbc, end_swap_bio_write);
|
2013-04-30 02:08:34 +04:00
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-07 02:22:30 +03:00
|
|
|
static inline void count_swpout_vm_event(struct page *page)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
|
|
|
|
if (unlikely(PageTransHuge(page)))
|
|
|
|
count_vm_event(THP_SWPOUT);
|
|
|
|
#endif
|
2020-08-15 03:30:37 +03:00
|
|
|
count_vm_events(PSWPOUT, thp_nr_pages(page));
|
2017-09-07 02:22:30 +03:00
|
|
|
}
|
|
|
|
|
2020-06-27 10:31:50 +03:00
|
|
|
#if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
|
|
|
|
static void bio_associate_blkg_from_page(struct bio *bio, struct page *page)
|
|
|
|
{
|
|
|
|
struct cgroup_subsys_state *css;
|
mm: memcontrol: Use helpers to read page's memcg data
Patch series "mm: allow mapping accounted kernel pages to userspace", v6.
Currently a non-slab kernel page which has been charged to a memory cgroup
can't be mapped to userspace. The underlying reason is simple: PageKmemcg
flag is defined as a page type (like buddy, offline, etc), so it takes a
bit from a page->mapped counter. Pages with a type set can't be mapped to
userspace.
But in general the kmemcg flag has nothing to do with mapping to
userspace. It only means that the page has been accounted by the page
allocator, so it has to be properly uncharged on release.
Some bpf maps are mapping the vmalloc-based memory to userspace, and their
memory can't be accounted because of this implementation detail.
This patchset removes this limitation by moving the PageKmemcg flag into
one of the free bits of the page->mem_cgroup pointer. Also it formalizes
accesses to the page->mem_cgroup and page->obj_cgroups using new helpers,
adds several checks and removes a couple of obsolete functions. As the
result the code became more robust with fewer open-coded bit tricks.
This patch (of 4):
Currently there are many open-coded reads of the page->mem_cgroup pointer,
as well as a couple of read helpers, which are barely used.
It creates an obstacle on a way to reuse some bits of the pointer for
storing additional bits of information. In fact, we already do this for
slab pages, where the last bit indicates that a pointer has an attached
vector of objcg pointers instead of a regular memcg pointer.
This commits uses 2 existing helpers and introduces a new helper to
converts all read sides to calls of these helpers:
struct mem_cgroup *page_memcg(struct page *page);
struct mem_cgroup *page_memcg_rcu(struct page *page);
struct mem_cgroup *page_memcg_check(struct page *page);
page_memcg_check() is intended to be used in cases when the page can be a
slab page and have a memcg pointer pointing at objcg vector. It does
check the lowest bit, and if set, returns NULL. page_memcg() contains a
VM_BUG_ON_PAGE() check for the page not being a slab page.
To make sure nobody uses a direct access, struct page's
mem_cgroup/obj_cgroups is converted to unsigned long memcg_data.
Signed-off-by: Roman Gushchin <guro@fb.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Link: https://lkml.kernel.org/r/20201027001657.3398190-1-guro@fb.com
Link: https://lkml.kernel.org/r/20201027001657.3398190-2-guro@fb.com
Link: https://lore.kernel.org/bpf/20201201215900.3569844-2-guro@fb.com
2020-12-02 00:58:27 +03:00
|
|
|
struct mem_cgroup *memcg;
|
2020-06-27 10:31:50 +03:00
|
|
|
|
mm: memcontrol: Use helpers to read page's memcg data
Patch series "mm: allow mapping accounted kernel pages to userspace", v6.
Currently a non-slab kernel page which has been charged to a memory cgroup
can't be mapped to userspace. The underlying reason is simple: PageKmemcg
flag is defined as a page type (like buddy, offline, etc), so it takes a
bit from a page->mapped counter. Pages with a type set can't be mapped to
userspace.
But in general the kmemcg flag has nothing to do with mapping to
userspace. It only means that the page has been accounted by the page
allocator, so it has to be properly uncharged on release.
Some bpf maps are mapping the vmalloc-based memory to userspace, and their
memory can't be accounted because of this implementation detail.
This patchset removes this limitation by moving the PageKmemcg flag into
one of the free bits of the page->mem_cgroup pointer. Also it formalizes
accesses to the page->mem_cgroup and page->obj_cgroups using new helpers,
adds several checks and removes a couple of obsolete functions. As the
result the code became more robust with fewer open-coded bit tricks.
This patch (of 4):
Currently there are many open-coded reads of the page->mem_cgroup pointer,
as well as a couple of read helpers, which are barely used.
It creates an obstacle on a way to reuse some bits of the pointer for
storing additional bits of information. In fact, we already do this for
slab pages, where the last bit indicates that a pointer has an attached
vector of objcg pointers instead of a regular memcg pointer.
This commits uses 2 existing helpers and introduces a new helper to
converts all read sides to calls of these helpers:
struct mem_cgroup *page_memcg(struct page *page);
struct mem_cgroup *page_memcg_rcu(struct page *page);
struct mem_cgroup *page_memcg_check(struct page *page);
page_memcg_check() is intended to be used in cases when the page can be a
slab page and have a memcg pointer pointing at objcg vector. It does
check the lowest bit, and if set, returns NULL. page_memcg() contains a
VM_BUG_ON_PAGE() check for the page not being a slab page.
To make sure nobody uses a direct access, struct page's
mem_cgroup/obj_cgroups is converted to unsigned long memcg_data.
Signed-off-by: Roman Gushchin <guro@fb.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Link: https://lkml.kernel.org/r/20201027001657.3398190-1-guro@fb.com
Link: https://lkml.kernel.org/r/20201027001657.3398190-2-guro@fb.com
Link: https://lore.kernel.org/bpf/20201201215900.3569844-2-guro@fb.com
2020-12-02 00:58:27 +03:00
|
|
|
memcg = page_memcg(page);
|
|
|
|
if (!memcg)
|
2020-06-27 10:31:50 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
mm: memcontrol: Use helpers to read page's memcg data
Patch series "mm: allow mapping accounted kernel pages to userspace", v6.
Currently a non-slab kernel page which has been charged to a memory cgroup
can't be mapped to userspace. The underlying reason is simple: PageKmemcg
flag is defined as a page type (like buddy, offline, etc), so it takes a
bit from a page->mapped counter. Pages with a type set can't be mapped to
userspace.
But in general the kmemcg flag has nothing to do with mapping to
userspace. It only means that the page has been accounted by the page
allocator, so it has to be properly uncharged on release.
Some bpf maps are mapping the vmalloc-based memory to userspace, and their
memory can't be accounted because of this implementation detail.
This patchset removes this limitation by moving the PageKmemcg flag into
one of the free bits of the page->mem_cgroup pointer. Also it formalizes
accesses to the page->mem_cgroup and page->obj_cgroups using new helpers,
adds several checks and removes a couple of obsolete functions. As the
result the code became more robust with fewer open-coded bit tricks.
This patch (of 4):
Currently there are many open-coded reads of the page->mem_cgroup pointer,
as well as a couple of read helpers, which are barely used.
It creates an obstacle on a way to reuse some bits of the pointer for
storing additional bits of information. In fact, we already do this for
slab pages, where the last bit indicates that a pointer has an attached
vector of objcg pointers instead of a regular memcg pointer.
This commits uses 2 existing helpers and introduces a new helper to
converts all read sides to calls of these helpers:
struct mem_cgroup *page_memcg(struct page *page);
struct mem_cgroup *page_memcg_rcu(struct page *page);
struct mem_cgroup *page_memcg_check(struct page *page);
page_memcg_check() is intended to be used in cases when the page can be a
slab page and have a memcg pointer pointing at objcg vector. It does
check the lowest bit, and if set, returns NULL. page_memcg() contains a
VM_BUG_ON_PAGE() check for the page not being a slab page.
To make sure nobody uses a direct access, struct page's
mem_cgroup/obj_cgroups is converted to unsigned long memcg_data.
Signed-off-by: Roman Gushchin <guro@fb.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Link: https://lkml.kernel.org/r/20201027001657.3398190-1-guro@fb.com
Link: https://lkml.kernel.org/r/20201027001657.3398190-2-guro@fb.com
Link: https://lore.kernel.org/bpf/20201201215900.3569844-2-guro@fb.com
2020-12-02 00:58:27 +03:00
|
|
|
css = cgroup_e_css(memcg->css.cgroup, &io_cgrp_subsys);
|
2020-06-27 10:31:50 +03:00
|
|
|
bio_associate_blkg_from_css(bio, css);
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define bio_associate_blkg_from_page(bio, page) do { } while (0)
|
|
|
|
#endif /* CONFIG_MEMCG && CONFIG_BLK_CGROUP */
|
|
|
|
|
2022-05-10 04:20:48 +03:00
|
|
|
struct swap_iocb {
|
|
|
|
struct kiocb iocb;
|
2022-05-10 04:20:49 +03:00
|
|
|
struct bio_vec bvec[SWAP_CLUSTER_MAX];
|
|
|
|
int pages;
|
2022-05-10 04:20:49 +03:00
|
|
|
int len;
|
2022-05-10 04:20:48 +03:00
|
|
|
};
|
|
|
|
static mempool_t *sio_pool;
|
|
|
|
|
|
|
|
int sio_pool_init(void)
|
|
|
|
{
|
|
|
|
if (!sio_pool) {
|
|
|
|
mempool_t *pool = mempool_create_kmalloc_pool(
|
|
|
|
SWAP_CLUSTER_MAX, sizeof(struct swap_iocb));
|
|
|
|
if (cmpxchg(&sio_pool, NULL, pool))
|
|
|
|
mempool_destroy(pool);
|
|
|
|
}
|
|
|
|
if (!sio_pool)
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-10 04:20:48 +03:00
|
|
|
static void sio_write_complete(struct kiocb *iocb, long ret)
|
|
|
|
{
|
|
|
|
struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
|
2022-05-10 04:20:49 +03:00
|
|
|
struct page *page = sio->bvec[0].bv_page;
|
2022-05-10 04:20:49 +03:00
|
|
|
int p;
|
2022-05-10 04:20:48 +03:00
|
|
|
|
2022-05-10 04:20:49 +03:00
|
|
|
if (ret != sio->len) {
|
2022-05-10 04:20:48 +03:00
|
|
|
/*
|
|
|
|
* In the case of swap-over-nfs, this can be a
|
|
|
|
* temporary failure if the system has limited
|
|
|
|
* memory for allocating transmit buffers.
|
|
|
|
* Mark the page dirty and avoid
|
|
|
|
* folio_rotate_reclaimable but rate-limit the
|
|
|
|
* messages but do not flag PageError like
|
|
|
|
* the normal direct-to-bio case as it could
|
|
|
|
* be temporary.
|
|
|
|
*/
|
|
|
|
pr_err_ratelimited("Write error %ld on dio swapfile (%llu)\n",
|
|
|
|
ret, page_file_offset(page));
|
2022-05-10 04:20:49 +03:00
|
|
|
for (p = 0; p < sio->pages; p++) {
|
|
|
|
page = sio->bvec[p].bv_page;
|
|
|
|
set_page_dirty(page);
|
|
|
|
ClearPageReclaim(page);
|
|
|
|
}
|
2022-05-10 04:20:49 +03:00
|
|
|
} else {
|
|
|
|
for (p = 0; p < sio->pages; p++)
|
|
|
|
count_swpout_vm_event(sio->bvec[p].bv_page);
|
|
|
|
}
|
2022-05-10 04:20:49 +03:00
|
|
|
|
|
|
|
for (p = 0; p < sio->pages; p++)
|
|
|
|
end_page_writeback(sio->bvec[p].bv_page);
|
|
|
|
|
2022-05-10 04:20:48 +03:00
|
|
|
mempool_free(sio, sio_pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int swap_writepage_fs(struct page *page, struct writeback_control *wbc)
|
|
|
|
{
|
2022-05-10 04:20:49 +03:00
|
|
|
struct swap_iocb *sio = NULL;
|
2022-05-10 04:20:48 +03:00
|
|
|
struct swap_info_struct *sis = page_swap_info(page);
|
|
|
|
struct file *swap_file = sis->swap_file;
|
2022-05-10 04:20:49 +03:00
|
|
|
loff_t pos = page_file_offset(page);
|
2022-05-10 04:20:48 +03:00
|
|
|
|
|
|
|
set_page_writeback(page);
|
|
|
|
unlock_page(page);
|
2022-05-10 04:20:49 +03:00
|
|
|
if (wbc->swap_plug)
|
|
|
|
sio = *wbc->swap_plug;
|
|
|
|
if (sio) {
|
|
|
|
if (sio->iocb.ki_filp != swap_file ||
|
2022-05-10 04:20:49 +03:00
|
|
|
sio->iocb.ki_pos + sio->len != pos) {
|
2022-05-10 04:20:49 +03:00
|
|
|
swap_write_unplug(sio);
|
|
|
|
sio = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!sio) {
|
|
|
|
sio = mempool_alloc(sio_pool, GFP_NOIO);
|
|
|
|
init_sync_kiocb(&sio->iocb, swap_file);
|
|
|
|
sio->iocb.ki_complete = sio_write_complete;
|
|
|
|
sio->iocb.ki_pos = pos;
|
|
|
|
sio->pages = 0;
|
2022-05-10 04:20:49 +03:00
|
|
|
sio->len = 0;
|
2022-05-10 04:20:49 +03:00
|
|
|
}
|
|
|
|
sio->bvec[sio->pages].bv_page = page;
|
2022-05-10 04:20:49 +03:00
|
|
|
sio->bvec[sio->pages].bv_len = thp_size(page);
|
2022-05-10 04:20:49 +03:00
|
|
|
sio->bvec[sio->pages].bv_offset = 0;
|
2022-05-10 04:20:49 +03:00
|
|
|
sio->len += thp_size(page);
|
2022-05-10 04:20:49 +03:00
|
|
|
sio->pages += 1;
|
|
|
|
if (sio->pages == ARRAY_SIZE(sio->bvec) || !wbc->swap_plug) {
|
|
|
|
swap_write_unplug(sio);
|
|
|
|
sio = NULL;
|
|
|
|
}
|
|
|
|
if (wbc->swap_plug)
|
|
|
|
*wbc->swap_plug = sio;
|
|
|
|
|
|
|
|
return 0;
|
2022-05-10 04:20:48 +03:00
|
|
|
}
|
|
|
|
|
2013-04-30 02:08:35 +04:00
|
|
|
int __swap_writepage(struct page *page, struct writeback_control *wbc,
|
2022-05-10 04:20:49 +03:00
|
|
|
bio_end_io_t end_write_func)
|
2013-04-30 02:08:34 +04:00
|
|
|
{
|
|
|
|
struct bio *bio;
|
2016-06-05 22:31:41 +03:00
|
|
|
int ret;
|
2013-04-30 02:08:34 +04:00
|
|
|
struct swap_info_struct *sis = page_swap_info(page);
|
2012-08-01 03:44:55 +04:00
|
|
|
|
2016-10-08 03:00:52 +03:00
|
|
|
VM_BUG_ON_PAGE(!PageSwapCache(page), page);
|
2022-05-10 04:20:48 +03:00
|
|
|
/*
|
|
|
|
* ->flags can be updated non-atomicially (scan_swap_map_slots),
|
|
|
|
* but that will never affect SWP_FS_OPS, so the data_race
|
|
|
|
* is safe.
|
|
|
|
*/
|
|
|
|
if (data_race(sis->flags & SWP_FS_OPS))
|
|
|
|
return swap_writepage_fs(page, wbc);
|
2012-08-01 03:44:55 +04:00
|
|
|
|
2014-06-05 03:07:48 +04:00
|
|
|
ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
|
|
|
|
if (!ret) {
|
2017-09-07 02:22:30 +03:00
|
|
|
count_swpout_vm_event(page);
|
2014-06-05 03:07:48 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:11:05 +03:00
|
|
|
bio = bio_alloc(sis->bdev, 1,
|
|
|
|
REQ_OP_WRITE | REQ_SWAP | wbc_to_write_flags(wbc),
|
|
|
|
GFP_NOIO);
|
2021-01-26 17:52:47 +03:00
|
|
|
bio->bi_iter.bi_sector = swap_page_sector(page);
|
|
|
|
bio->bi_end_io = end_write_func;
|
|
|
|
bio_add_page(bio, page, thp_size(page), 0);
|
|
|
|
|
2018-12-05 20:10:33 +03:00
|
|
|
bio_associate_blkg_from_page(bio, page);
|
2017-09-07 02:22:30 +03:00
|
|
|
count_swpout_vm_event(page);
|
2005-04-17 02:20:36 +04:00
|
|
|
set_page_writeback(page);
|
|
|
|
unlock_page(page);
|
2016-06-05 22:31:41 +03:00
|
|
|
submit_bio(bio);
|
2020-10-14 02:52:21 +03:00
|
|
|
|
|
|
|
return 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2022-05-10 04:20:49 +03:00
|
|
|
void swap_write_unplug(struct swap_iocb *sio)
|
|
|
|
{
|
|
|
|
struct iov_iter from;
|
|
|
|
struct address_space *mapping = sio->iocb.ki_filp->f_mapping;
|
|
|
|
int ret;
|
|
|
|
|
2022-05-10 04:20:49 +03:00
|
|
|
iov_iter_bvec(&from, WRITE, sio->bvec, sio->pages, sio->len);
|
2022-05-10 04:20:49 +03:00
|
|
|
ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
|
|
|
|
if (ret != -EIOCBQUEUED)
|
|
|
|
sio_write_complete(&sio->iocb, ret);
|
|
|
|
}
|
|
|
|
|
2022-05-10 04:20:48 +03:00
|
|
|
static void sio_read_complete(struct kiocb *iocb, long ret)
|
|
|
|
{
|
|
|
|
struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
|
2022-05-10 04:20:49 +03:00
|
|
|
int p;
|
2022-05-10 04:20:48 +03:00
|
|
|
|
2022-05-10 04:20:49 +03:00
|
|
|
if (ret == sio->len) {
|
2022-05-10 04:20:49 +03:00
|
|
|
for (p = 0; p < sio->pages; p++) {
|
|
|
|
struct page *page = sio->bvec[p].bv_page;
|
|
|
|
|
|
|
|
SetPageUptodate(page);
|
|
|
|
unlock_page(page);
|
|
|
|
}
|
|
|
|
count_vm_events(PSWPIN, sio->pages);
|
2022-05-10 04:20:48 +03:00
|
|
|
} else {
|
2022-05-10 04:20:49 +03:00
|
|
|
for (p = 0; p < sio->pages; p++) {
|
|
|
|
struct page *page = sio->bvec[p].bv_page;
|
|
|
|
|
|
|
|
SetPageError(page);
|
|
|
|
ClearPageUptodate(page);
|
|
|
|
unlock_page(page);
|
|
|
|
}
|
|
|
|
pr_alert_ratelimited("Read-error on swap-device\n");
|
2022-05-10 04:20:48 +03:00
|
|
|
}
|
|
|
|
mempool_free(sio, sio_pool);
|
|
|
|
}
|
|
|
|
|
2022-05-10 04:20:49 +03:00
|
|
|
static void swap_readpage_fs(struct page *page,
|
|
|
|
struct swap_iocb **plug)
|
2022-05-10 04:20:48 +03:00
|
|
|
{
|
|
|
|
struct swap_info_struct *sis = page_swap_info(page);
|
2022-05-10 04:20:49 +03:00
|
|
|
struct swap_iocb *sio = NULL;
|
2022-05-10 04:20:48 +03:00
|
|
|
loff_t pos = page_file_offset(page);
|
|
|
|
|
2022-05-10 04:20:49 +03:00
|
|
|
if (plug)
|
|
|
|
sio = *plug;
|
|
|
|
if (sio) {
|
|
|
|
if (sio->iocb.ki_filp != sis->swap_file ||
|
2022-05-10 04:20:49 +03:00
|
|
|
sio->iocb.ki_pos + sio->len != pos) {
|
2022-05-10 04:20:49 +03:00
|
|
|
swap_read_unplug(sio);
|
|
|
|
sio = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!sio) {
|
|
|
|
sio = mempool_alloc(sio_pool, GFP_KERNEL);
|
|
|
|
init_sync_kiocb(&sio->iocb, sis->swap_file);
|
|
|
|
sio->iocb.ki_pos = pos;
|
|
|
|
sio->iocb.ki_complete = sio_read_complete;
|
|
|
|
sio->pages = 0;
|
2022-05-10 04:20:49 +03:00
|
|
|
sio->len = 0;
|
2022-05-10 04:20:49 +03:00
|
|
|
}
|
|
|
|
sio->bvec[sio->pages].bv_page = page;
|
2022-05-10 04:20:49 +03:00
|
|
|
sio->bvec[sio->pages].bv_len = thp_size(page);
|
2022-05-10 04:20:49 +03:00
|
|
|
sio->bvec[sio->pages].bv_offset = 0;
|
2022-05-10 04:20:49 +03:00
|
|
|
sio->len += thp_size(page);
|
2022-05-10 04:20:49 +03:00
|
|
|
sio->pages += 1;
|
|
|
|
if (sio->pages == ARRAY_SIZE(sio->bvec) || !plug) {
|
|
|
|
swap_read_unplug(sio);
|
|
|
|
sio = NULL;
|
|
|
|
}
|
|
|
|
if (plug)
|
|
|
|
*plug = sio;
|
2022-05-10 04:20:48 +03:00
|
|
|
}
|
|
|
|
|
2022-05-10 04:20:49 +03:00
|
|
|
int swap_readpage(struct page *page, bool synchronous,
|
|
|
|
struct swap_iocb **plug)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct bio *bio;
|
|
|
|
int ret = 0;
|
2012-08-01 03:44:55 +04:00
|
|
|
struct swap_info_struct *sis = page_swap_info(page);
|
2022-03-23 00:46:30 +03:00
|
|
|
bool workingset = PageWorkingset(page);
|
2019-12-01 04:58:29 +03:00
|
|
|
unsigned long pflags;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2017-11-16 04:33:07 +03:00
|
|
|
VM_BUG_ON_PAGE(!PageSwapCache(page) && !synchronous, page);
|
2014-01-24 03:52:54 +04:00
|
|
|
VM_BUG_ON_PAGE(!PageLocked(page), page);
|
|
|
|
VM_BUG_ON_PAGE(PageUptodate(page), page);
|
2019-12-01 04:58:29 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Count submission time as memory stall. When the device is congested,
|
|
|
|
* or the submitting cgroup IO-throttled, submission can be a
|
|
|
|
* significant part of overall IO time.
|
|
|
|
*/
|
2022-03-23 00:46:30 +03:00
|
|
|
if (workingset)
|
|
|
|
psi_memstall_enter(&pflags);
|
2022-01-20 05:10:02 +03:00
|
|
|
delayacct_swapin_start();
|
2019-12-01 04:58:29 +03:00
|
|
|
|
2012-05-15 19:32:15 +04:00
|
|
|
if (frontswap_load(page) == 0) {
|
2012-04-10 03:08:06 +04:00
|
|
|
SetPageUptodate(page);
|
|
|
|
unlock_page(page);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-08-01 03:44:55 +04:00
|
|
|
|
2020-10-14 02:52:04 +03:00
|
|
|
if (data_race(sis->flags & SWP_FS_OPS)) {
|
2022-05-10 04:20:49 +03:00
|
|
|
swap_readpage_fs(page, plug);
|
2019-12-01 04:58:29 +03:00
|
|
|
goto out;
|
2012-08-01 03:44:55 +04:00
|
|
|
}
|
|
|
|
|
2020-09-24 09:51:37 +03:00
|
|
|
if (sis->flags & SWP_SYNCHRONOUS_IO) {
|
|
|
|
ret = bdev_read_page(sis->bdev, swap_page_sector(page), page);
|
|
|
|
if (!ret) {
|
|
|
|
count_vm_event(PSWPIN);
|
|
|
|
goto out;
|
|
|
|
}
|
2014-06-05 03:07:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2022-01-24 12:11:05 +03:00
|
|
|
bio = bio_alloc(sis->bdev, 1, REQ_OP_READ, GFP_KERNEL);
|
2021-01-26 17:52:47 +03:00
|
|
|
bio->bi_iter.bi_sector = swap_page_sector(page);
|
|
|
|
bio->bi_end_io = end_swap_bio_read;
|
|
|
|
bio_add_page(bio, page, thp_size(page), 0);
|
2017-08-02 23:32:09 +03:00
|
|
|
/*
|
|
|
|
* Keep this task valid during swap readpage because the oom killer may
|
|
|
|
* attempt to access it in the page fault retry time check.
|
|
|
|
*/
|
2019-07-05 01:14:49 +03:00
|
|
|
if (synchronous) {
|
2021-10-12 14:12:21 +03:00
|
|
|
bio->bi_opf |= REQ_POLLED;
|
2019-07-05 01:14:49 +03:00
|
|
|
get_task_struct(current);
|
|
|
|
bio->bi_private = current;
|
|
|
|
}
|
2006-06-30 12:55:45 +04:00
|
|
|
count_vm_event(PSWPIN);
|
swap: add block io poll in swapin path
For fast flash disk, async IO could introduce overhead because of
context switch. block-mq now supports IO poll, which improves
performance and latency a lot. swapin is a good place to use this
technique, because the task is waiting for the swapin page to continue
execution.
In my virtual machine, directly read 4k data from a NVMe with iopoll is
about 60% better than that without poll. With iopoll support in swapin
patch, my microbenchmark (a task does random memory write) is about
10%~25% faster. CPU utilization increases a lot though, 2x and even 3x
CPU utilization. This will depend on disk speed.
While iopoll in swapin isn't intended for all usage cases, it's a win
for latency sensistive workloads with high speed swap disk. block layer
has knob to control poll in runtime. If poll isn't enabled in block
layer, there should be no noticeable change in swapin.
I got a chance to run the same test in a NVMe with DRAM as the media.
In simple fio IO test, blkpoll boosts 50% performance in single thread
test and ~20% in 8 threads test. So this is the base line. In above
swap test, blkpoll boosts ~27% performance in single thread test.
blkpoll uses 2x CPU time though.
If we enable hybid polling, the performance gain has very slight drop
but CPU time is only 50% worse than that without blkpoll. Also we can
adjust parameter of hybid poll, with it, the CPU time penality is
reduced further. In 8 threads test, blkpoll doesn't help though. The
performance is similar to that without blkpoll, but cpu utilization is
similar too. There is lock contention in swap path. The cpu time
spending on blkpoll isn't high. So overall, blkpoll swapin isn't worse
than that without it.
The swapin readahead might read several pages in in the same time and
form a big IO request. Since the IO will take longer time, it doesn't
make sense to do poll, so the patch only does iopoll for single page
swapin.
[akpm@linux-foundation.org: coding-style fixes]
Link: http://lkml.kernel.org/r/070c3c3e40b711e7b1390002c991e86a-b5408f0@7511894063d3764ff01ea8111f5a004d7dd700ed078797c204a24e620ddb965c
Signed-off-by: Shaohua Li <shli@fb.com>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Huang Ying <ying.huang@intel.com>
Cc: Jens Axboe <axboe@fb.com>
Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-07-11 01:47:11 +03:00
|
|
|
bio_get(bio);
|
2021-10-12 14:12:24 +03:00
|
|
|
submit_bio(bio);
|
2017-11-16 04:33:07 +03:00
|
|
|
while (synchronous) {
|
2019-01-02 21:46:03 +03:00
|
|
|
set_current_state(TASK_UNINTERRUPTIBLE);
|
swap: add block io poll in swapin path
For fast flash disk, async IO could introduce overhead because of
context switch. block-mq now supports IO poll, which improves
performance and latency a lot. swapin is a good place to use this
technique, because the task is waiting for the swapin page to continue
execution.
In my virtual machine, directly read 4k data from a NVMe with iopoll is
about 60% better than that without poll. With iopoll support in swapin
patch, my microbenchmark (a task does random memory write) is about
10%~25% faster. CPU utilization increases a lot though, 2x and even 3x
CPU utilization. This will depend on disk speed.
While iopoll in swapin isn't intended for all usage cases, it's a win
for latency sensistive workloads with high speed swap disk. block layer
has knob to control poll in runtime. If poll isn't enabled in block
layer, there should be no noticeable change in swapin.
I got a chance to run the same test in a NVMe with DRAM as the media.
In simple fio IO test, blkpoll boosts 50% performance in single thread
test and ~20% in 8 threads test. So this is the base line. In above
swap test, blkpoll boosts ~27% performance in single thread test.
blkpoll uses 2x CPU time though.
If we enable hybid polling, the performance gain has very slight drop
but CPU time is only 50% worse than that without blkpoll. Also we can
adjust parameter of hybid poll, with it, the CPU time penality is
reduced further. In 8 threads test, blkpoll doesn't help though. The
performance is similar to that without blkpoll, but cpu utilization is
similar too. There is lock contention in swap path. The cpu time
spending on blkpoll isn't high. So overall, blkpoll swapin isn't worse
than that without it.
The swapin readahead might read several pages in in the same time and
form a big IO request. Since the IO will take longer time, it doesn't
make sense to do poll, so the patch only does iopoll for single page
swapin.
[akpm@linux-foundation.org: coding-style fixes]
Link: http://lkml.kernel.org/r/070c3c3e40b711e7b1390002c991e86a-b5408f0@7511894063d3764ff01ea8111f5a004d7dd700ed078797c204a24e620ddb965c
Signed-off-by: Shaohua Li <shli@fb.com>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Huang Ying <ying.huang@intel.com>
Cc: Jens Axboe <axboe@fb.com>
Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-07-11 01:47:11 +03:00
|
|
|
if (!READ_ONCE(bio->bi_private))
|
|
|
|
break;
|
|
|
|
|
2021-10-12 18:24:29 +03:00
|
|
|
if (!bio_poll(bio, NULL, 0))
|
2020-08-07 09:20:17 +03:00
|
|
|
blk_io_schedule();
|
swap: add block io poll in swapin path
For fast flash disk, async IO could introduce overhead because of
context switch. block-mq now supports IO poll, which improves
performance and latency a lot. swapin is a good place to use this
technique, because the task is waiting for the swapin page to continue
execution.
In my virtual machine, directly read 4k data from a NVMe with iopoll is
about 60% better than that without poll. With iopoll support in swapin
patch, my microbenchmark (a task does random memory write) is about
10%~25% faster. CPU utilization increases a lot though, 2x and even 3x
CPU utilization. This will depend on disk speed.
While iopoll in swapin isn't intended for all usage cases, it's a win
for latency sensistive workloads with high speed swap disk. block layer
has knob to control poll in runtime. If poll isn't enabled in block
layer, there should be no noticeable change in swapin.
I got a chance to run the same test in a NVMe with DRAM as the media.
In simple fio IO test, blkpoll boosts 50% performance in single thread
test and ~20% in 8 threads test. So this is the base line. In above
swap test, blkpoll boosts ~27% performance in single thread test.
blkpoll uses 2x CPU time though.
If we enable hybid polling, the performance gain has very slight drop
but CPU time is only 50% worse than that without blkpoll. Also we can
adjust parameter of hybid poll, with it, the CPU time penality is
reduced further. In 8 threads test, blkpoll doesn't help though. The
performance is similar to that without blkpoll, but cpu utilization is
similar too. There is lock contention in swap path. The cpu time
spending on blkpoll isn't high. So overall, blkpoll swapin isn't worse
than that without it.
The swapin readahead might read several pages in in the same time and
form a big IO request. Since the IO will take longer time, it doesn't
make sense to do poll, so the patch only does iopoll for single page
swapin.
[akpm@linux-foundation.org: coding-style fixes]
Link: http://lkml.kernel.org/r/070c3c3e40b711e7b1390002c991e86a-b5408f0@7511894063d3764ff01ea8111f5a004d7dd700ed078797c204a24e620ddb965c
Signed-off-by: Shaohua Li <shli@fb.com>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Huang Ying <ying.huang@intel.com>
Cc: Jens Axboe <axboe@fb.com>
Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-07-11 01:47:11 +03:00
|
|
|
}
|
|
|
|
__set_current_state(TASK_RUNNING);
|
|
|
|
bio_put(bio);
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
out:
|
2022-03-23 00:46:30 +03:00
|
|
|
if (workingset)
|
|
|
|
psi_memstall_leave(&pflags);
|
2022-01-20 05:10:02 +03:00
|
|
|
delayacct_swapin_end();
|
2005-04-17 02:20:36 +04:00
|
|
|
return ret;
|
|
|
|
}
|
2022-05-10 04:20:49 +03:00
|
|
|
|
|
|
|
void __swap_read_unplug(struct swap_iocb *sio)
|
|
|
|
{
|
|
|
|
struct iov_iter from;
|
|
|
|
struct address_space *mapping = sio->iocb.ki_filp->f_mapping;
|
|
|
|
int ret;
|
|
|
|
|
2022-05-10 04:20:49 +03:00
|
|
|
iov_iter_bvec(&from, READ, sio->bvec, sio->pages, sio->len);
|
2022-05-10 04:20:49 +03:00
|
|
|
ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
|
|
|
|
if (ret != -EIOCBQUEUED)
|
|
|
|
sio_read_complete(&sio->iocb, ret);
|
|
|
|
}
|