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>
# include <linux/writeback.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>
2023-07-17 19:02:27 +03:00
# include <linux/zswap.h>
2022-05-10 04:20:47 +03:00
# include "swap.h"
2005-04-17 02:20:36 +04:00
2023-01-25 16:34:36 +03:00
static void __end_swap_bio_write ( struct bio * bio )
2005-04-17 02:20:36 +04:00
{
2023-07-21 06:44:45 +03:00
struct folio * folio = bio_first_folio_all ( bio ) ;
2005-04-17 02:20:36 +04:00
2017-06-03 10:38:06 +03:00
if ( bio - > bi_status ) {
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
*/
2023-07-21 06:44:45 +03:00
folio_mark_dirty ( folio ) ;
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 ) ;
2023-07-21 06:44:45 +03:00
folio_clear_reclaim ( folio ) ;
2006-09-26 10:31:26 +04:00
}
2023-07-21 06:44:45 +03:00
folio_end_writeback ( folio ) ;
2023-01-25 16:34:36 +03:00
}
static void end_swap_bio_write ( struct bio * bio )
{
__end_swap_bio_write ( bio ) ;
2005-04-17 02:20:36 +04:00
bio_put ( bio ) ;
}
2023-01-25 16:34:33 +03:00
static void __end_swap_bio_read ( struct bio * bio )
2005-04-17 02:20:36 +04:00
{
2023-07-21 06:44:46 +03:00
struct folio * folio = bio_first_folio_all ( bio ) ;
2005-04-17 02:20:36 +04:00
2017-06-03 10:38:06 +03:00
if ( bio - > bi_status ) {
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 ) ;
2023-01-25 16:34:33 +03:00
} else {
2023-07-21 06:44:46 +03:00
folio_mark_uptodate ( folio ) ;
2005-04-17 02:20:36 +04:00
}
2023-07-21 06:44:46 +03:00
folio_unlock ( folio ) ;
2023-01-25 16:34:33 +03:00
}
static void end_swap_bio_read ( struct bio * bio )
{
__end_swap_bio_read ( bio ) ;
2005-04-17 02:20:36 +04:00
bio_put ( bio ) ;
}
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 )
{
2022-09-02 22:46:36 +03:00
struct folio * folio = page_folio ( page ) ;
2023-01-25 16:34:34 +03:00
int ret ;
2005-04-17 02:20:36 +04:00
2022-09-02 22:46:36 +03:00
if ( folio_free_swap ( folio ) ) {
folio_unlock ( folio ) ;
2023-01-25 16:34:34 +03:00
return 0 ;
2005-04-17 02:20:36 +04:00
}
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 .
*/
2022-09-02 22:46:36 +03:00
ret = arch_prepare_to_swap ( & folio - > page ) ;
2020-05-13 18:37:49 +03:00
if ( ret ) {
2022-09-02 22:46:36 +03:00
folio_mark_dirty ( folio ) ;
folio_unlock ( folio ) ;
2023-01-25 16:34:34 +03:00
return ret ;
2020-05-13 18:37:49 +03:00
}
2023-07-15 07:23:40 +03:00
if ( zswap_store ( folio ) ) {
2022-09-02 22:46:36 +03:00
folio_start_writeback ( folio ) ;
folio_unlock ( folio ) ;
folio_end_writeback ( folio ) ;
2023-01-25 16:34:34 +03:00
return 0 ;
2012-04-10 03:08:06 +04:00
}
zswap: memcontrol: implement zswap writeback disabling
During our experiment with zswap, we sometimes observe swap IOs due to
occasional zswap store failures and writebacks-to-swap. These swapping
IOs prevent many users who cannot tolerate swapping from adopting zswap to
save memory and improve performance where possible.
This patch adds the option to disable this behavior entirely: do not
writeback to backing swapping device when a zswap store attempt fail, and
do not write pages in the zswap pool back to the backing swap device (both
when the pool is full, and when the new zswap shrinker is called).
This new behavior can be opted-in/out on a per-cgroup basis via a new
cgroup file. By default, writebacks to swap device is enabled, which is
the previous behavior. Initially, writeback is enabled for the root
cgroup, and a newly created cgroup will inherit the current setting of its
parent.
Note that this is subtly different from setting memory.swap.max to 0, as
it still allows for pages to be stored in the zswap pool (which itself
consumes swap space in its current form).
This patch should be applied on top of the zswap shrinker series:
https://lore.kernel.org/linux-mm/20231130194023.4102148-1-nphamcs@gmail.com/
as it also disables the zswap shrinker, a major source of zswap
writebacks.
For the most part, this feature is motivated by internal parties who
have already established their opinions regarding swapping - the
workloads that are highly sensitive to IO, and especially those who are
using servers with really slow disk performance (for instance, massive
but slow HDDs). For these folks, it's impossible to convince them to
even entertain zswap if swapping also comes as a packaged deal.
Writeback disabling is quite a useful feature in these situations - on
a mixed workloads deployment, they can disable writeback for the more
IO-sensitive workloads, and enable writeback for other background
workloads.
For instance, on a server with HDD, I allocate memories and populate
them with random values (so that zswap store will always fail), and
specify memory.high low enough to trigger reclaim. The time it takes
to allocate the memories and just read through it a couple of times
(doing silly things like computing the values' average etc.):
zswap.writeback disabled:
real 0m30.537s
user 0m23.687s
sys 0m6.637s
0 pages swapped in
0 pages swapped out
zswap.writeback enabled:
real 0m45.061s
user 0m24.310s
sys 0m8.892s
712686 pages swapped in
461093 pages swapped out
(the last two lines are from vmstat -s).
[nphamcs@gmail.com: add a comment about recurring zswap store failures leading to reclaim inefficiency]
Link: https://lkml.kernel.org/r/20231221005725.3446672-1-nphamcs@gmail.com
Link: https://lkml.kernel.org/r/20231207192406.3809579-1-nphamcs@gmail.com
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
Suggested-by: Johannes Weiner <hannes@cmpxchg.org>
Reviewed-by: Yosry Ahmed <yosryahmed@google.com>
Acked-by: Chris Li <chrisl@kernel.org>
Cc: Dan Streetman <ddstreet@ieee.org>
Cc: David Heidelberg <david@ixit.cz>
Cc: Domenico Cerasuolo <cerasuolodomenico@gmail.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Seth Jennings <sjenning@redhat.com>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vitaly Wool <vitaly.wool@konsulko.com>
Cc: Zefan Li <lizefan.x@bytedance.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-12-07 22:24:06 +03:00
if ( ! mem_cgroup_zswap_writeback_enabled ( folio_memcg ( folio ) ) ) {
folio_mark_dirty ( folio ) ;
return AOP_WRITEPAGE_ACTIVATE ;
}
2023-12-14 00:58:31 +03:00
__swap_writepage ( folio , wbc ) ;
2023-01-25 16:34:34 +03:00
return 0 ;
2013-04-30 02:08:34 +04:00
}
2023-07-21 06:44:50 +03:00
static inline void count_swpout_vm_event ( struct folio * folio )
2017-09-07 02:22:30 +03:00
{
# ifdef CONFIG_TRANSPARENT_HUGEPAGE
mm: memcg: add THP swap out info for anonymous reclaim
At present, we support per-memcg reclaim strategy, however we do not know
the number of transparent huge pages being reclaimed, as we know the
transparent huge pages need to be splited before reclaim them, and they
will bring some performance bottleneck effect. for example, when two
memcg (A & B) are doing reclaim for anonymous pages at same time, and 'A'
memcg is reclaiming a large number of transparent huge pages, we can
better analyze that the performance bottleneck will be caused by 'A'
memcg. therefore, in order to better analyze such problems, there add THP
swap out info for per-memcg.
[akpm@linux-foundation.orgL fix swap_writepage_fs(), per Johannes]
Link: https://lkml.kernel.org/r/20230913213343.GB48476@cmpxchg.org
Link: https://lkml.kernel.org/r/20230913164938.16918-1-vernhao@tencent.com
Signed-off-by: Xin Hao <vernhao@tencent.com>
Suggested-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: Muchun Song <songmuchun@bytedance.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-09-13 19:49:37 +03:00
if ( unlikely ( folio_test_pmd_mappable ( folio ) ) ) {
count_memcg_folio_events ( folio , THP_SWPOUT , 1 ) ;
2017-09-07 02:22:30 +03:00
count_vm_event ( THP_SWPOUT ) ;
mm: memcg: add THP swap out info for anonymous reclaim
At present, we support per-memcg reclaim strategy, however we do not know
the number of transparent huge pages being reclaimed, as we know the
transparent huge pages need to be splited before reclaim them, and they
will bring some performance bottleneck effect. for example, when two
memcg (A & B) are doing reclaim for anonymous pages at same time, and 'A'
memcg is reclaiming a large number of transparent huge pages, we can
better analyze that the performance bottleneck will be caused by 'A'
memcg. therefore, in order to better analyze such problems, there add THP
swap out info for per-memcg.
[akpm@linux-foundation.orgL fix swap_writepage_fs(), per Johannes]
Link: https://lkml.kernel.org/r/20230913213343.GB48476@cmpxchg.org
Link: https://lkml.kernel.org/r/20230913164938.16918-1-vernhao@tencent.com
Signed-off-by: Xin Hao <vernhao@tencent.com>
Suggested-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: Muchun Song <songmuchun@bytedance.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-09-13 19:49:37 +03:00
}
2017-09-07 02:22:30 +03:00
# endif
2023-07-21 06:44:50 +03:00
count_vm_events ( PSWPOUT , folio_nr_pages ( folio ) ) ;
2017-09-07 02:22:30 +03:00
}
2020-06-27 10:31:50 +03:00
# if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
2023-07-21 06:44:51 +03:00
static void bio_associate_blkg_from_page ( struct bio * bio , struct folio * folio )
2020-06-27 10:31:50 +03:00
{
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
2023-07-21 06:44:51 +03:00
memcg = folio_memcg ( folio ) ;
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
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
2023-07-21 06:44:51 +03:00
# define bio_associate_blkg_from_page(bio, folio) do { } while (0)
2020-06-27 10:31:50 +03:00
# 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 )
2013-04-30 02:08:34 +04:00
{
2022-05-10 04:20:48 +03:00
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 ;
}
2012-08-01 03:44:55 +04:00
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 ;
2012-08-01 03:44:55 +04: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 ;
2013-04-30 02:08:47 +04:00
set_page_dirty ( page ) ;
2013-04-30 02:08:48 +04:00
ClearPageReclaim ( page ) ;
2012-08-01 03:44:55 +04:00
}
}
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 ) ;
}
2023-12-14 00:58:32 +03:00
static void swap_writepage_fs ( struct folio * folio , struct writeback_control * wbc )
2022-05-10 04:20:48 +03:00
{
2022-05-10 04:20:49 +03:00
struct swap_iocb * sio = NULL ;
2023-12-14 00:58:32 +03:00
struct swap_info_struct * sis = swp_swap_info ( folio - > swap ) ;
2022-05-10 04:20:48 +03:00
struct file * swap_file = sis - > swap_file ;
2023-12-14 00:58:32 +03:00
loff_t pos = folio_file_pos ( folio ) ;
2022-05-10 04:20:48 +03:00
2023-12-14 00:58:32 +03:00
count_swpout_vm_event ( folio ) ;
folio_start_writeback ( folio ) ;
folio_unlock ( folio ) ;
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
}
2023-12-14 00:58:32 +03:00
bvec_set_folio ( & sio - > bvec [ sio - > pages ] , folio , folio_size ( folio ) , 0 ) ;
sio - > len + = folio_size ( folio ) ;
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 ;
2022-05-10 04:20:48 +03:00
}
2023-12-14 00:58:33 +03:00
static void swap_writepage_bdev_sync ( struct folio * folio ,
2023-01-25 16:34:35 +03:00
struct writeback_control * wbc , struct swap_info_struct * sis )
2013-04-30 02:08:34 +04:00
{
2023-01-25 16:34:36 +03:00
struct bio_vec bv ;
struct bio bio ;
2012-08-01 03:44:55 +04:00
2023-01-25 16:34:36 +03:00
bio_init ( & bio , sis - > bdev , & bv , 1 ,
REQ_OP_WRITE | REQ_SWAP | wbc_to_write_flags ( wbc ) ) ;
2023-12-14 00:58:38 +03:00
bio . bi_iter . bi_sector = swap_folio_sector ( folio ) ;
2023-12-14 00:58:33 +03:00
bio_add_folio_nofail ( & bio , folio , folio_size ( folio ) , 0 ) ;
2012-08-01 03:44:55 +04:00
2023-07-21 06:44:51 +03:00
bio_associate_blkg_from_page ( & bio , folio ) ;
2023-07-21 06:44:50 +03:00
count_swpout_vm_event ( folio ) ;
2023-01-25 16:34:36 +03:00
2023-07-21 06:44:48 +03:00
folio_start_writeback ( folio ) ;
folio_unlock ( folio ) ;
2023-01-25 16:34:36 +03:00
submit_bio_wait ( & bio ) ;
__end_swap_bio_write ( & bio ) ;
}
2023-12-14 00:58:34 +03:00
static void swap_writepage_bdev_async ( struct folio * folio ,
2023-01-25 16:34:36 +03:00
struct writeback_control * wbc , struct swap_info_struct * sis )
{
struct bio * bio ;
2014-06-05 03:07:48 +04:00
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 ) ;
2023-12-14 00:58:38 +03:00
bio - > bi_iter . bi_sector = swap_folio_sector ( folio ) ;
2022-08-11 17:17:41 +03:00
bio - > bi_end_io = end_swap_bio_write ;
2023-12-14 00:58:34 +03:00
bio_add_folio_nofail ( bio , folio , folio_size ( folio ) , 0 ) ;
2021-01-26 17:52:47 +03:00
2023-07-21 06:44:51 +03:00
bio_associate_blkg_from_page ( bio , folio ) ;
2023-07-21 06:44:50 +03:00
count_swpout_vm_event ( folio ) ;
2023-07-21 06:44:49 +03:00
folio_start_writeback ( folio ) ;
folio_unlock ( folio ) ;
2016-06-05 22:31:41 +03:00
submit_bio ( bio ) ;
2005-04-17 02:20:36 +04:00
}
2020-10-14 02:52:21 +03:00
2023-12-14 00:58:31 +03:00
void __swap_writepage ( struct folio * folio , struct writeback_control * wbc )
2023-01-25 16:34:35 +03:00
{
2023-12-14 00:58:31 +03:00
struct swap_info_struct * sis = swp_swap_info ( folio - > swap ) ;
2023-01-25 16:34:35 +03:00
2023-12-14 00:58:31 +03:00
VM_BUG_ON_FOLIO ( ! folio_test_swapcache ( folio ) , folio ) ;
2023-01-25 16:34:35 +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 ) )
2023-12-14 00:58:32 +03:00
swap_writepage_fs ( folio , wbc ) ;
2023-01-25 16:34:36 +03:00
else if ( sis - > flags & SWP_SYNCHRONOUS_IO )
2023-12-14 00:58:33 +03:00
swap_writepage_bdev_sync ( folio , wbc , sis ) ;
2023-01-25 16:34:35 +03:00
else
2023-12-14 00:58:34 +03:00
swap_writepage_bdev_async ( folio , wbc , sis ) ;
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-09-16 03:25:47 +03:00
iov_iter_bvec ( & from , ITER_SOURCE , 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 + + ) {
2023-07-21 06:44:47 +03:00
struct folio * folio = page_folio ( sio - > bvec [ p ] . bv_page ) ;
2022-05-10 04:20:49 +03:00
2023-07-21 06:44:47 +03:00
folio_mark_uptodate ( folio ) ;
folio_unlock ( folio ) ;
2022-05-10 04:20:49 +03:00
}
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 + + ) {
2023-07-21 06:44:47 +03:00
struct folio * folio = page_folio ( sio - > bvec [ p ] . bv_page ) ;
2022-05-10 04:20:49 +03:00
2023-07-21 06:44:47 +03:00
folio_unlock ( folio ) ;
2022-05-10 04:20:49 +03:00
}
pr_alert_ratelimited ( " Read-error on swap-device \n " ) ;
2022-05-10 04:20:48 +03:00
}
mempool_free ( sio , sio_pool ) ;
}
2023-12-14 00:58:39 +03:00
static void swap_read_folio_fs ( struct folio * folio , struct swap_iocb * * plug )
2022-05-10 04:20:48 +03:00
{
2023-12-14 00:58:35 +03:00
struct swap_info_struct * sis = swp_swap_info ( folio - > swap ) ;
2022-05-10 04:20:49 +03:00
struct swap_iocb * sio = NULL ;
2023-12-14 00:58:35 +03:00
loff_t pos = folio_file_pos ( folio ) ;
2022-05-10 04:20:48 +03:00
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
}
2023-12-14 00:58:35 +03:00
bvec_set_folio ( & sio - > bvec [ sio - > pages ] , folio , folio_size ( folio ) , 0 ) ;
sio - > len + = folio_size ( folio ) ;
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
}
2023-12-14 00:58:39 +03:00
static void swap_read_folio_bdev_sync ( struct folio * folio ,
2023-01-25 16:34:32 +03:00
struct swap_info_struct * sis )
2005-04-17 02:20:36 +04:00
{
2023-01-25 16:34:33 +03:00
struct bio_vec bv ;
struct bio bio ;
2012-08-01 03:44:55 +04:00
2023-01-25 16:34:33 +03:00
bio_init ( & bio , sis - > bdev , & bv , 1 , REQ_OP_READ ) ;
2023-12-14 00:58:38 +03:00
bio . bi_iter . bi_sector = swap_folio_sector ( folio ) ;
2023-12-14 00:58:36 +03:00
bio_add_folio_nofail ( & bio , folio , folio_size ( folio ) , 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 .
*/
2023-01-25 16:34:33 +03:00
get_task_struct ( current ) ;
2006-06-30 12:55:45 +04:00
count_vm_event ( PSWPIN ) ;
2023-01-25 16:34:33 +03:00
submit_bio_wait ( & bio ) ;
__end_swap_bio_read ( & bio ) ;
put_task_struct ( current ) ;
}
2023-12-14 00:58:39 +03:00
static void swap_read_folio_bdev_async ( struct folio * folio ,
2023-01-25 16:34:33 +03:00
struct swap_info_struct * sis )
2005-04-17 02:20:36 +04:00
{
struct bio * 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
2023-01-25 16:34:33 +03:00
bio = bio_alloc ( sis - > bdev , 1 , REQ_OP_READ , GFP_KERNEL ) ;
2023-12-14 00:58:38 +03:00
bio - > bi_iter . bi_sector = swap_folio_sector ( folio ) ;
2023-01-25 16:34:33 +03:00
bio - > bi_end_io = end_swap_bio_read ;
2023-12-14 00:58:37 +03:00
bio_add_folio_nofail ( bio , folio , folio_size ( folio ) , 0 ) ;
2023-01-25 16:34:33 +03:00
count_vm_event ( PSWPIN ) ;
submit_bio ( bio ) ;
2023-01-25 16:34:32 +03:00
}
2023-12-14 00:58:39 +03:00
void swap_read_folio ( struct folio * folio , bool synchronous ,
struct swap_iocb * * plug )
2023-01-25 16:34:32 +03:00
{
2023-12-14 00:58:39 +03:00
struct swap_info_struct * sis = swp_swap_info ( folio - > swap ) ;
2023-07-15 07:23:42 +03:00
bool workingset = folio_test_workingset ( folio ) ;
2019-12-01 04:58:29 +03:00
unsigned long pflags ;
2022-08-15 10:28:37 +03:00
bool in_thrashing ;
2005-04-17 02:20:36 +04:00
2023-07-15 07:23:42 +03:00
VM_BUG_ON_FOLIO ( ! folio_test_swapcache ( folio ) & & ! synchronous , folio ) ;
VM_BUG_ON_FOLIO ( ! folio_test_locked ( folio ) , folio ) ;
VM_BUG_ON_FOLIO ( folio_test_uptodate ( folio ) , folio ) ;
2019-12-01 04:58:29 +03:00
/*
2022-08-15 10:28:37 +03:00
* Count submission time as memory stall and delay . When the device
* is congested , or the submitting cgroup IO - throttled , submission
* can be a significant part of overall IO time .
2019-12-01 04:58:29 +03:00
*/
2022-08-15 10:28:37 +03:00
if ( workingset ) {
delayacct_thrashing_start ( & in_thrashing ) ;
2022-03-23 00:46:30 +03:00
psi_memstall_enter ( & pflags ) ;
2022-08-15 10:28:37 +03:00
}
2022-01-20 05:10:02 +03:00
delayacct_swapin_start ( ) ;
2019-12-01 04:58:29 +03:00
2023-07-15 07:23:43 +03:00
if ( zswap_load ( folio ) ) {
2023-07-15 07:23:42 +03:00
folio_mark_uptodate ( folio ) ;
folio_unlock ( folio ) ;
2023-01-25 16:34:32 +03:00
} else if ( data_race ( sis - > flags & SWP_FS_OPS ) ) {
2023-12-14 00:58:39 +03:00
swap_read_folio_fs ( folio , plug ) ;
2023-01-25 16:34:36 +03:00
} else if ( synchronous | | ( sis - > flags & SWP_SYNCHRONOUS_IO ) ) {
2023-12-14 00:58:39 +03:00
swap_read_folio_bdev_sync ( folio , sis ) ;
2023-01-25 16:34:32 +03:00
} else {
2023-12-14 00:58:39 +03:00
swap_read_folio_bdev_async ( folio , sis ) ;
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
}
2022-08-15 10:28:37 +03:00
if ( workingset ) {
delayacct_thrashing_end ( & in_thrashing ) ;
2022-03-23 00:46:30 +03:00
psi_memstall_leave ( & pflags ) ;
2022-08-15 10:28:37 +03:00
}
2022-01-20 05:10:02 +03:00
delayacct_swapin_end ( ) ;
2005-04-17 02:20:36 +04:00
}
2012-08-01 03:44:55 +04:00
2022-05-10 04:20:49 +03:00
void __swap_read_unplug ( struct swap_iocb * sio )
2012-08-01 03:44:55 +04:00
{
2022-05-10 04:20:49 +03:00
struct iov_iter from ;
struct address_space * mapping = sio - > iocb . ki_filp - > f_mapping ;
int ret ;
2016-10-08 03:00:52 +03:00
2022-09-16 03:25:47 +03:00
iov_iter_bvec ( & from , ITER_DEST , 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 ) ;
2012-08-01 03:44:55 +04:00
}