PKRAM: Preserved-over-Kexec RAM
Subsystems (15)
v2 cover PKRAM: Preserved-over-Kexec RAM
This patchset implements preserved-over-kexec memory storage or PKRAM as a method for saving memory pages of the currently executing kernel so that they may be restored after kexec into a new kernel. The patches are adapted from an RFC patchset sent out in 2013 by Vladimir Davydov [1]. They introduce the PKRAM kernel API and implement its use within tmpfs, allowing tmpfs files to be preserved across kexec.
One use case for PKRAM is preserving guest memory and/or auxillary supporting data (e.g. iommu data) across kexec in support of VMM Fast Restart[2]. VMM Fast Restart is currently using PKRAM to support preserving "Keep Alive State" across reboot[3]. PKRAM provides a flexible way for doing this without requiring that the amount of memory used by a fixed size created a priori. Another use case is for databases to preserve their block caches in shared memory across reboot.
Changes since RFC v1
- Rebased onto 5.12-rc4
- Refined the API to reduce the number of calls and better support multithreading.
- Allow preserving byte data of arbitrary length (was previously limited to one page).
- Build a new memblock reserved list with the preserved ranges and then substitute it for the existing one. (Mike Rapoport)
- Use mem_avoid_overlap() to avoid kaslr stepping on preserved ranges. (Kees Cook)
-- Usage --
1) Mount tmpfs with 'pkram=NAME' option.
NAME is an arbitrary string specifying a preserved memory node. Different tmpfs trees may be saved to PKRAM if different names are passed.
# mkdir -p /mnt
# mount -t tmpfs -o pkram=mytmpfs none /mnt
2) Populate a file under /mnt
# head -c 2G /dev/urandom > /mnt/testfile
# md5sum /mnt/testfile
e281e2f019ac3bfa3bdb28aa08c4beb3 /mnt/testfile3) Remount tmpfs to preserve files.
# mount -o remount,preserve,ro /mnt
4) Load the new kernel image.
Pass the PKRAM super block pfn via 'pkram' boot option. The pfn is exported via the sysfs file /sys/kernel/pkram.
# kexec -s -l /boot/vmlinuz-$kernel --initrd=/boot/initramfs-$kernel.img \
--append="$(cat /proc/cmdline|sed -e 's/pkram=[^ ]*//g') pkram=$(cat /sys/kernel/pkram)"
5) Boot to the new kernel.
# systemctl kexec
6) Mount tmpfs with 'pkram=NAME' option.
It should find the PKRAM node with the tmpfs tree saved on previous unmount and restore it.
# mount -t tmpfs -o pkram=mytmpfs none /mnt
7) Use the restored file under /mnt
# md5sum /mnt/testfile
e281e2f019ac3bfa3bdb28aa08c4beb3 /mnt/testfile-- Implementation details --
* When a tmpfs filesystem is mounted the first time with the 'pkram=NAME' option, a shmem_pkram_info is allocated to record NAME. The shmem_pkram_info and whether the filesystem is in the preserved state are tracked by shmem_sb_info.
* A PKRAM-enabled tmpfs filesystem is saved to PKRAM on remount when the 'preserve' mount option is specified and the filesystem is read-only.
* Saving a file to PKRAM is done by walking the pages of the file and building a list of the pages and attributes needed to restore them later. The pages containing this metadata as well as the target file pages have their refcount incremented to prevent them from being freed even after the last user puts the pages (i.e. the filesystem is unmounted).
* To aid in quickly finding contiguous ranges of memory containing preserved pages a pseudo physical mapping pagetable is populated with pages as they are preserved.
* If a page to be preserved is found to be in range of memory that was previously reserved during early boot or in range of memory where the kernel will be loaded to on kexec, the page will be copied to a page outside of those ranges and the new page will be preserved. A compound page will be copied to and preserved as individual base pages.
* A single page is allocated for the PKRAM super block. For the next kernel kexec boot to find preserved memory metadata, the pfn of the PKRAM super block, which is exported via /sys/kernel/pkram, is passed in the 'pkram' boot option.
* In the newly booted kernel, PKRAM adds all preserved pages to the memblock reserve list during early boot so that they will not be recycled.
* Since kexec may load the new kernel code to any memory region, it could destroy preserved memory. When the kernel selects the memory region (kexec_file_load syscall), kexec will avoid preserved pages. When the user selects the kexec memory region to use (kexec_load syscall) , kexec load will fail if there is conflict with preserved pages. Pages preserved after a kexec kernel is loaded will be relocated if they conflict with the selected memory region.
The current implementation has some restrictions:
* Only regular tmpfs files without multiple hard links can be preserved. Save to PKRAM will abort and log an error if a directory or other file type is encountered.
* Pages for PKRAM-enabled files are prevented from swapping out to avoid the performance penalty of swapping in and the possibility of insufficient memory.
-- Patches --
The patches are broken down into the following groups:
Patches 1-22 implement the API and supporting functionality.
Patches 23-27 implement the use of PKRAM within tmpfs
The remaining patches implement optimizations to the initialization of preserved pages and to the preservation and restoration of shmem pages.
To give an idea of the improvement in performance here is an example comparison with and without these patches when saving and loading a 100G file:
Save a 100G file:
| No optimizations | Optimized (16 cpus) |
------------------------------------------------------
huge=never | 2265ms | 232ms |
------------------------------------------------------
huge=always | 58ms | 22ms |
Load a 100G file:
| No optimizations | Optimized (16 cpus) |
------------------------------------------------------
huge=never | 8833ms | 516ms |
------------------------------------------------------
huge=always | 752ms | 105ms |
Patches 28-31 Defer initialization of page structs for preserved pages
Patches 32-34 Implement multi-threading of shmem page preservation and restoration.
Patches 35-37 Implement and use an API for inserting shmem pages in bulk
Patches 38-39: Reduce contention on the LRU lock by staging and adding pages in bulk to the LRU
Patches 40-43: Reduce contention on the pagecache xarray lock by inserting pages in bulk in certain cases
[1] https://lkml.org/lkml/2013/7/1/211
[2] https://www.youtube.com/watch?v=pBsHnf93tcQ
https://static.sched.com/hosted_files/kvmforum2019/66/VMM-fast-restart_kvmforum2019.pdf
[3] https://www.youtube.com/watch?v=pBsHnf93tcQ
https://static.sched.com/hosted_files/kvmforum2020/10/Device-Keepalive-State-KVMForum2020.pdf
Anthony Yznaga (43):
mm: add PKRAM API stubs and Kconfig
mm: PKRAM: implement node load and save functions
mm: PKRAM: implement object load and save functions
mm: PKRAM: implement page stream operations
mm: PKRAM: support preserving transparent hugepages
mm: PKRAM: implement byte stream operations
mm: PKRAM: link nodes by pfn before reboot
mm: PKRAM: introduce super block
PKRAM: track preserved pages in a physical mapping pagetable
PKRAM: pass a list of preserved ranges to the next kernel
PKRAM: prepare for adding preserved ranges to memblock reserved
mm: PKRAM: reserve preserved memory at boot
PKRAM: free the preserved ranges list
PKRAM: prevent inadvertent use of a stale superblock
PKRAM: provide a way to ban pages from use by PKRAM
kexec: PKRAM: prevent kexec clobbering preserved pages in some cases
PKRAM: provide a way to check if a memory range has preserved pages
kexec: PKRAM: avoid clobbering already preserved pages
mm: PKRAM: allow preserved memory to be freed from userspace
PKRAM: disable feature when running the kdump kernel x86/KASLR: PKRAM: support physical kaslr x86/boot/compressed/64: use 1GB pages for mappings
mm: shmem: introduce shmem_insert_page
mm: shmem: enable saving to PKRAM
mm: shmem: prevent swapping of PKRAM-enabled tmpfs pages
mm: shmem: specify the mm to use when inserting pages
mm: shmem: when inserting, handle pages already charged to a memcg x86/mm/numa: add numa_isolate_memblocks()
PKRAM: ensure memblocks with preserved pages init'd for numa
memblock: PKRAM: mark memblocks that contain preserved pages memblock, mm: defer initialization of preserved pages
shmem: preserve shmem files a chunk at a time
PKRAM: atomically add and remove link pages
shmem: PKRAM: multithread preserving and restoring shmem pages
shmem: introduce shmem_insert_pages()
PKRAM: add support for loading pages in bulk
shmem: PKRAM: enable bulk loading of preserved pages into shmem
mm: implement splicing a list of pages to the LRU
shmem: optimize adding pages to the LRU in shmem_insert_pages()
shmem: initial support for adding multiple pages to pagecache
XArray: add xas_export_node() and xas_import_node()
shmem: reduce time holding xa_lock when inserting pages
PKRAM: improve index alignment of pkram_link entries
documentation/core-api/xarray.rst | 8 + arch/x86/boot/compressed/Makefile | 3 + arch/x86/boot/compressed/ident_map_64.c | 9 +- arch/x86/boot/compressed/kaslr.c | 10 +- arch/x86/boot/compressed/misc.h | 10 + arch/x86/boot/compressed/pkram.c | 109 ++ arch/x86/include/asm/numa.h | 4 + arch/x86/kernel/setup.c | 3 + arch/x86/mm/init_64.c | 2 + arch/x86/mm/numa.c | 32 +- include/linux/memblock.h | 6 + include/linux/mm.h | 2 +- include/linux/pkram.h | 120 ++ include/linux/shmem_fs.h | 28 + include/linux/swap.h | 13 + include/linux/xarray.h | 2 + kernel/kexec.c | 9 + kernel/kexec_core.c | 3 + kernel/kexec_file.c | 15 + lib/test_xarray.c | 45 + lib/xarray.c | 100 ++ mm/Kconfig | 9 + mm/Makefile | 1 + mm/memblock.c | 11 +- mm/page_alloc.c | 55 +- mm/pkram.c | 1808 +++++++++++++++++++++++++++++++ mm/pkram_pagetable.c | 376 +++++++ mm/shmem.c | 494 ++++++++- mm/shmem_pkram.c | 530 +++++++++ mm/swap.c | 86 ++ 30 files changed, 3869 insertions(+), 34 deletions(-) create mode 100644 arch/x86/boot/compressed/pkram.c create mode 100644 include/linux/pkram.h create mode 100644 mm/pkram.c create mode 100644 mm/pkram_pagetable.c create mode 100644 mm/shmem_pkram.c
Signature · 1 line
1.8.3.1
RFC v2 1/43 mm: add PKRAM API stubs and Kconfig
Preserved-across-kexec memory or PKRAM is a method for saving memory pages of the currently executing kernel and restoring them after kexec boot into a new one. This can be utilized for preserving guest VM state, large in-memory databases, process memory, etc. across reboot. While DRAM-as-PMEM or actual persistent memory could be used to accomplish these things, PKRAM provides the latency of DRAM with the flexibility of dynamically determining the amount of memory to preserve.
The proposed API:
* Preserved memory is divided into nodes which can be saved or loaded independently of each other. The nodes are identified by unique name strings. A PKRAM node is created when save is initiated by calling pkram_prepare_save(). A PKRAM node is removed when load is initiated by calling pkram_prepare_load(). See below
* A node is further divided into objects. An object represents closely coupled data in the form of a grouping of pages and/or a stream of byte data. For example, the pages and attributes of a file. After initiating an operation on a PKRAM node, PKRAM objects are initialized for saving or loading by calling pkram_prepare_save_obj() or pkram_prepare_load_obj().
* For saving/loading data from a PKRAM node/object instances of the pkram_stream and pkram_access structs are used. pkram_stream tracks the node and object being operated on while pkram_access tracks the data type and position within an object.
The pkram_stream struct is initialized by calling pkram_prepare_save() or pkram_prepare_load() and then pkram_prepare_save_obj() or pkram_prepare_load_obj().
Once a pkram_stream is fully initialized, a pkram_access struct is initialized for each data type associated with the object. After save or load of a data type for the object is complete, pkram_finish_access() is called.
After save or load is complete for the object, pkram_finish_save_obj() or pkram_finish_load_obj() must be called followed by pkram_finish_save() or pkram_finish_load() when save or load is completed for the node. If an error occurred during save, the saved data and the PKRAM node may be freed by calling pkram_discard_save() instead of pkram_finish_save().
* Both page data and byte data can separately be streamed to a PKRAM object. pkram_save_file_page() and pkram_load_file_page() are used to stream page data while pkram_write() and pkram_read() are used to stream byte data.
A sequence of operations for saving/loading data from PKRAM would look like:
* For saving data to PKRAM:
/* create a PKRAM node and do initial stream setup */
pkram_prepare_save()
/* create a PKRAM object associated with the PKRAM node and complete stream initialization */
pkram_prepare_save_obj()
/* save data to the node/object */
PKRAM_ACCESS(pa_pages,...)
PKRAM_ACCESS(pa_bytes,...)
pkram_save_file_page(pa_pages,...)[,...] /* for file pages */
pkram_write(pa_bytes,...)[,...] /* for a byte stream */
pkram_finish_access(pa_pages)
pkram_finish_access(pa_bytes)
pkram_finish_save_obj()
/* commit the save or discard and delete the node */
pkram_finish_save() /* on success, or
pkram_discard_save() * ... in case of error */* For loading data from PKRAM:
/* remove a PKRAM node from the list and do initial stream setup */
pkram_prepare_load()
/* Remove a PKRAM object from the node and complete stream initializtion for loading data from it. */
pkram_prepare_load_obj()
/* load data from the node/object */
PKRAM_ACCESS(pa_pages,...)
PKRAM_ACCESS(pa_bytes,...)
pkram_load_file_page(pa_pages,...)[,...] /* for file pages */
pkram_read(pa_bytes,...)[,...] /* for a byte stream */
*/
pkram_finish_access(pa_pages)
pkram_finish_access(pa_bytes)
/* free the object */
pkram_finish_load_obj()
/* free the node */
pkram_finish_load()--- include/linux/pkram.h | 47 +++++++++++++ mm/Kconfig | 9 +++ mm/Makefile | 1 + mm/pkram.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 236 insertions(+) create mode 100644 include/linux/pkram.h create mode 100644 mm/pkram.c
include/linux/pkram.h new file +47 -0
@@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_PKRAM_H +#define _LINUX_PKRAM_H + +#include <linux/gfp.h> +#include <linux/types.h> +#include <linux/mm_types.h> + +/** +* enum pkram_data_flags - definition of data types contained in a pkram obj +* @PKRAM_DATA_none: No data types configured +*/ +enum pkram_data_flags { + PKRAM_DATA_none =0x0 ,/* No data types configured */ +}; + +struct pkram_stream ; +struct pkram_access ; + +#define PKRAM_NAME_MAX 256/* including nul */ + +int pkram_prepare_save (struct pkram_stream *ps,const char *name, +gfp_t gfp_mask); +int pkram_prepare_save_obj (struct pkram_stream *ps,enum pkram_data_flags flags); + +void pkram_finish_save (struct pkram_stream *ps); +void pkram_finish_save_obj (struct pkram_stream *ps); +void pkram_discard_save (struct pkram_stream *ps); + +int pkram_prepare_load (struct pkram_stream *ps,const char *name); +int pkram_prepare_load_obj (struct pkram_stream *ps); + +void pkram_finish_load (struct pkram_stream *ps); +void pkram_finish_load_obj (struct pkram_stream *ps); + +#define PKRAM_ACCESS (name, stream, type) \ + struct pkram_access name + +void pkram_finish_access (struct pkram_access *pa,bool status_ok); + +int pkram_save_file_page (struct pkram_access *pa,struct page *page); +struct page *pkram_load_file_page (struct pkram_access *pa,unsigned long *index); + +ssize_t pkram_write (struct pkram_access *pa,const void *buf,size_t count); +size_t pkram_read (struct pkram_access *pa,void *buf,size_t count); + +#endif /* _LINUX_PKRAM_H */
mm/Kconfig +9 -0
@@ -872,4 +872,13 @@ config MAPPING_DIRTY_HELPERS config KMAP_LOCAL bool +config PKRAM + bool "Preserved-over-kexec memory storage" + default n + help + This option adds the kernel API that enables saving memory pages of + the currently executing kernel and restoring them after a kexec in + the newly booted one. This can be utilized for speeding up reboot by + leaving process memory and/or FS caches in-place. + endmenu
mm/Makefile +1 -0
@@ -120,3 +120,4 @@ obj-$(CONFIG_MEMFD_CREATE) += memfd.o obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o obj-$(CONFIG_PTDUMP_CORE) += ptdump.o obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o +obj-$(CONFIG_PKRAM) += pkram.o
mm/pkram.c new file +179 -0
179 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Randy Dunlap
reviewerRe: [RFC v2 01/43] mm: add PKRAM API stubs and Kconfig
Quoted 8 lines · Anthony Yznaga
Preserved-across-kexec memory or PKRAM is a method for saving memory pages of the currently executing kernel and restoring them after kexec boot into a new one. This can be utilized for preserving guest VM state, large in-memory databases, process memory, etc. across reboot. While DRAM-as-PMEM or actual persistent memory could be used to accomplish these things, PKRAM provides the latency of DRAM with the flexibility of dynamically determining the amount of memory to preserve.
...
Quoted 18 lines
Originally-by Vladimir Davydov <[email protected]>Signed-off-by Anthony Yznaga <[email protected]>--- include/linux/pkram.h | 47 +++++++++++++ mm/Kconfig | 9 +++ mm/Makefile | 1 + mm/pkram.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 236 insertions(+) create mode 100644 include/linux/pkram.h create mode 100644 mm/pkram.cmm/pkram.c new file +0
-0
mm/pkram.c line 8
Quoted 8 lines
mm/pkram.c new file +8
-0@@ -0,0 +1,179 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/err.h> +#include <linux/gfp.h> +#include <linux/kernel.h> +#include <linux/mm.h> +#include <linux/pkram.h> +#include <linux/types.h> +
Hi,
There are several doc blocks that begin with "/**" but that are not in kernel-doc format (/** means kernel-doc format when inside the kernel source tree).
Please either change those to "/*" or convert them to kernel-doc format. The latter is preferable for exported interfaces.
mm/pkram.c line 19
Quoted 11 lines
mm/pkram.c new file +11
-0+/** +* Create a preserved memory node with name @name and initialize stream @ps +* for saving data to it. +* +* @gfp_mask specifies the memory allocation mask to be used when saving data. +* +* Returns 0 on success, -errno on failure. +* +* After the save has finished, pkram_finish_save() (or pkram_discard_save() in +* case of failure) is to be called. +*/
b) from patch 00/43:
documentation/core-api/xarray.rst | 8 +
How did "documentation" become lower case (instead of Documentation)?
thanks.
Signature · 7 lines
~Randy _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
Re: [RFC v2 01/43] mm: add PKRAM API stubs and Kconfig
Quoted 38 lines · Randy Dunlap
Quoted 8 lines · Anthony Yznaga
Preserved-across-kexec memory or PKRAM is a method for saving memory pages of the currently executing kernel and restoring them after kexec boot into a new one. This can be utilized for preserving guest VM state, large in-memory databases, process memory, etc. across reboot. While DRAM-as-PMEM or actual persistent memory could be used to accomplish these things, PKRAM provides the latency of DRAM with the flexibility of dynamically determining the amount of memory to preserve.
...
Quoted 17 lines
Originally-by Vladimir Davydov <[email protected]>Signed-off-by Anthony Yznaga <[email protected]>--- include/linux/pkram.h | 47 +++++++++++++ mm/Kconfig | 9 +++ mm/Makefile | 1 + mm/pkram.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 236 insertions(+) create mode 100644 include/linux/pkram.h create mode 100644 mm/pkram.cmm/pkram.c new file +0
-0mm/pkram.c line 8
Quoted 8 lines
mm/pkram.c new file +8
-0@@ -0,0 +1,179 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/err.h> +#include <linux/gfp.h> +#include <linux/kernel.h> +#include <linux/mm.h> +#include <linux/pkram.h> +#include <linux/types.h> +Hi,
Quoted 6 lines
There are several doc blocks that begin with "/**" but that are not in kernel-doc format (/** means kernel-doc format when inside the kernel source tree).
Please either change those to "/*" or convert them to kernel-doc format. The latter is preferable for exported interfaces.
Thank you. I'll fix these up.
Quoted 13 lines
mm/pkram.c line 19
Quoted 12 lines
mm/pkram.c new file +11
-0+/** +* Create a preserved memory node with name @name and initialize stream @ps +* for saving data to it. +* +* @gfp_mask specifies the memory allocation mask to be used when saving data. +* +* Returns 0 on success, -errno on failure. +* +* After the save has finished, pkram_finish_save() (or pkram_discard_save() in +* case of failure) is to be called. +*/
Quoted 5 lines
b) from patch 00/43:
documentation/core-api/xarray.rst | 8 +How did "documentation" become lower case (instead of Documentation)?
That is odd. The patch (41) has it correct.
Anthony
Quoted 3 lines
thanks.
RFC v2 2/43 mm: PKRAM: implement node load and save functions
In reply to Anthony YznagaPreserved memory is divided into nodes which can be saved and loaded independently of each other. PKRAM nodes are kept on a list and identified by unique names. Whenever a save operation is initiated by calling pkram_prepare_save(), a new node is created and linked to the list. When the save operation has been committed by calling pkram_finish_save(), the node becomes loadable. A load operation can be then initiated by calling pkram_prepare_load() which deletes the node from the list and prepares the corresponding stream for loading data from it. After the load has been finished, the pkram_finish_load() function must be called to free the node. Nodes are also deleted when a save operation is discarded, i.e. pkram_discard_save() is called instead of pkram_finish_save().
--- include/linux/pkram.h | 8 ++- mm/pkram.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 150 insertions(+), 6 deletions(-)
include/linux/pkram.h +7 -1
@@ -6,6 +6,8 @@#include <linux/types.h> #include <linux/mm_types.h> +struct pkram_node ; +/** * enum pkram_data_flags - definition of data types contained in a pkram obj * @PKRAM_DATA_none: No data types configured
@@ -14,7 +16,11 @@ enum pkram_data_flags { PKRAM_DATA_none = 0x0, /* No data types configured */ }; -struct pkram_stream ; +struct pkram_stream { +gfp_t gfp_mask; +struct pkram_node *node; +}; +struct pkram_access ;#define PKRAM_NAME_MAX 256/* including nul */
mm/pkram.c +143 -5
198 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 3/43 mm: PKRAM: implement object load and save functions
In reply to Anthony YznagaPKRAM nodes are further divided into a list of objects. After a save operation has been initiated for a node, a save operation for an object associated with the node is initiated by calling pkram_prepare_save_obj(). A new object is created and linked to the node. The save operation for the object is committed by calling pkram_finish_save_obj(). After a load operation has been initiated, pkram_prepare_load_obj() is called to delete the next object from the node and prepare the corresponding stream for loading data from it. After the load of object has been finished, pkram_finish_load_obj() is called to free the object. Objects are also deleted when a save operation is discarded.
--- include/linux/pkram.h | 2 ++ mm/pkram.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 4 deletions(-)
include/linux/pkram.h +2 -0
@@ -7,6 +7,7 @@#include <linux/mm_types.h> struct pkram_node ; +struct pkram_obj ;/** * enum pkram_data_flags - definition of data types contained in a pkram obj
@@ -19,6 +20,7 @@ enum pkram_data_flags {struct pkram_stream {gfp_t gfp_mask;struct pkram_node *node; +struct pkram_obj *obj; };struct pkram_access ;
mm/pkram.c +68 -4
138 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 4/43 mm: PKRAM: implement page stream operations
In reply to Anthony YznagaUsing the pkram_save_file_page() function, one can populate PKRAM objects with in-memory pages which can later be loaded using the pkram_load_file_page() function. Saving a page to PKRAM is accomplished by recording its pfn and mapping index and incrementing its refcount so that it will not be freed after the last user puts it.
--- include/linux/pkram.h | 42 +++++++- mm/pkram.c | 282 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 317 insertions(+), 7 deletions(-)
include/linux/pkram.h +39 -3
@@ -8,22 +8,47 @@struct pkram_node ;struct pkram_obj ; +struct pkram_link ;/** * enum pkram_data_flags - definition of data types contained in a pkram obj * @PKRAM_DATA_none: No data types configured +* @PKRAM_DATA_pages: obj contains file page data */ enum pkram_data_flags { - PKRAM_DATA_none =0x0 ,/* No data types configured */ + PKRAM_DATA_none =0x0 ,/* No data types configured */ + PKRAM_DATA_pages =0x1 ,/* Contains file page data */ +}; + +struct pkram_data_stream { +/* List of link pages to add/remove from */ +__u64 *head_link_pfnp; +__u64 *tail_link_pfnp; + +struct pkram_link *link;/* current link */ +unsigned int entry_idx;/* next entry in link */ };struct pkram_stream {gfp_t gfp_mask;struct pkram_node *node;struct pkram_obj *obj; + +__u64 *pages_head_link_pfnp; +__u64 *pages_tail_link_pfnp; +}; + +struct pkram_pages_access { +unsigned long next_index; }; -struct pkram_access ; +struct pkram_access { +enum pkram_data_flags dtype; +struct pkram_stream *ps; +struct pkram_data_stream pds; + +struct pkram_pages_access pages; +};#define PKRAM_NAME_MAX 256/* including nul */
@@ -41,8 +66,19 @@ int pkram_prepare_save(struct pkram_stream *ps, const char *name,void pkram_finish_load (struct pkram_stream *ps);void pkram_finish_load_obj (struct pkram_stream *ps); +#define PKRAM_PDS_INIT (name, stream, type) { \ + .head_link_pfnp=(stream)->type##_head_link_pfnp, \ + .tail_link_pfnp=(stream)->type##_tail_link_pfnp, \ + } + +#define PKRAM_ACCESS_INIT (name, stream, type) { \ + .dtype = PKRAM_DATA_##type, \ + .ps = (stream), \ + .pds = PKRAM_PDS_INIT(name, stream, type), \ + } +#define PKRAM_ACCESS (name, stream, type) \ - struct pkram_access name + struct pkram_access name = PKRAM_ACCESS_INIT(name, stream, type)void pkram_finish_access (struct pkram_access *pa,bool status_ok);
mm/pkram.c +278 -4
366 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 5/43 mm: PKRAM: support preserving transparent hugepages
In reply to Anthony YznagaSupport preserving a transparent hugepage by recording the page order and a flag indicating it is a THP. Use these values when the page is restored to reconstruct the THP.
--- mm/pkram.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
mm/pkram.c +16 -4
@@ -21,6 +21,9 @@#define PKRAM_ENTRY_FLAGS_SHIFT 0x5#define PKRAM_ENTRY_FLAGS_MASK 0x7f +#define PKRAM_ENTRY_ORDER_MASK 0x1f + +#define PKRAM_PAGE_TRANS_HUGE 0x1/* page is a transparent hugepage */ /* * Keeps references to data pages saved to PKRAM.
@@ -211,7 +214,11 @@ static void pkram_add_link_entry(struct pkram_data_stream *pds, struct page *pagpkram_entry_t p;short flags =0 ; +if (PageTransHuge (page)) + flags |=PKRAM_PAGE_TRANS_HUGE ; + p =page_to_phys (page); + p |=compound_order (page); p |= ((flags &PKRAM_ENTRY_FLAGS_MASK ) <<PKRAM_ENTRY_FLAGS_SHIFT ); link->entry[pds->entry_idx] = p; pds->entry_idx++;
@@ -516,7 +523,7 @@ static int __pkram_save_page(struct pkram_access *pa, struct page *page,pkram_add_link_entry (pds, page); - pa->pages.next_index++; + pa->pages.next_index +=compound_nr (page);return 0 ; }
@@ -542,19 +549,24 @@ int pkram_save_file_page(struct pkram_access *pa, struct page *page)BUG_ON ((node->flags &PKRAM_ACCMODE_MASK ) !=PKRAM_SAVE ); -BUG_ON (PageCompound (page)); -return __pkram_save_page (pa, page, page->index); }static struct page *__pkram_prep_load_page (pkram_entry_t p) {struct page *page; +int order;short flags; flags = (p >>PKRAM_ENTRY_FLAGS_SHIFT ) &PKRAM_ENTRY_FLAGS_MASK ; page =pfn_to_page (PHYS_PFN (p)); +if (flags &PKRAM_PAGE_TRANS_HUGE ) { + order = p &PKRAM_ENTRY_ORDER_MASK ; +prep_compound_page (page, order); +prep_transhuge_page (page); + } +return page; }
@@ -588,7 +600,7 @@ static struct page *__pkram_load_page(struct pkram_access *pa, unsigned long *inif (index) { *index = pa->pages.next_index; - pa->pages.next_index++; + pa->pages.next_index +=compound_nr (page); }/* clear to avoid double free (see pkram_truncate_link()) */
Signature · 1 line
1.8.3.1
RFC v2 6/43 mm: PKRAM: implement byte stream operations
In reply to Anthony YznagaThis patch adds the ability to save an arbitrary byte streams to a a PKRAM object using pkram_write() to be restored later using pkram_read().
--- include/linux/pkram.h | 11 +++++ mm/pkram.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 130 insertions(+), 4 deletions(-)
include/linux/pkram.h +11 -0
@@ -14,10 +14,12 @@* enum pkram_data_flags - definition of data types contained in a pkram obj * @PKRAM_DATA_none: No data types configured * @PKRAM_DATA_pages: obj contains file page data +* @PKRAM_DATA_bytes: obj contains byte data */ enum pkram_data_flags { PKRAM_DATA_none =0x0 ,/* No data types configured */ PKRAM_DATA_pages =0x1 ,/* Contains file page data */ + PKRAM_DATA_bytes =0x2 ,/* Contains byte data */ };struct pkram_data_stream {
@@ -36,18 +38,27 @@ struct pkram_stream {__u64 *pages_head_link_pfnp;__u64 *pages_tail_link_pfnp; + +__u64 *bytes_head_link_pfnp; +__u64 *bytes_tail_link_pfnp; };struct pkram_pages_access {unsigned long next_index; }; +struct pkram_bytes_access { +struct page *data_page;/* current page */ +unsigned int data_offset;/* offset into current page */ +}; +struct pkram_access {enum pkram_data_flags dtype;struct pkram_stream *ps;struct pkram_data_stream pds;struct pkram_pages_access pages; +struct pkram_bytes_access bytes; };#define PKRAM_NAME_MAX 256/* including nul */
mm/pkram.c +119 -4
190 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 7/43 mm: PKRAM: link nodes by pfn before reboot
In reply to Anthony YznagaSince page structs are used for linking PKRAM nodes and cleared on boot, organize all PKRAM nodes into a list singly-linked by pfns before reboot to facilitate restoring the node list in the new kernel.
--- mm/pkram.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+)
mm/pkram.c +50 -0
@@ -2,12 +2,16 @@#include <linux/err.h> #include <linux/gfp.h> #include <linux/highmem.h> +#include <linux/init.h> #include <linux/io.h> #include <linux/kernel.h> #include <linux/list.h> #include <linux/mm.h> +#include <linux/module.h> #include <linux/mutex.h> +#include <linux/notifier.h> #include <linux/pkram.h> +#include <linux/reboot.h> #include <linux/sched.h> #include <linux/string.h> #include <linux/types.h>
@@ -62,11 +66,15 @@ struct pkram_obj {* singly-linked list of PKRAM link structures (see above), the node has a * pointer to the head of. * +* To facilitate data restore in the new kernel, before reboot all PKRAM nodes +* are organized into a list singly-linked by pfn's (see pkram_reboot()). +* * The structure occupies a memory page. */ struct pkram_node {__u32 flags;__u64 obj_pfn;/* points to the first obj of the node */ +__u64 node_pfn;/* points to the next node in the node list */ __u8 name[PKRAM_NAME_MAX ]; };
@@ -75,6 +83,10 @@ struct pkram_node {#define PKRAM_LOAD 2#define PKRAM_ACCMODE_MASK 3 +/* +* For convenience sake PKRAM nodes are kept in an auxiliary doubly-linked list +* connected through the lru field of the page struct. +*/ static LIST_HEAD (pkram_nodes);/* linked through page::lru */ static DEFINE_MUTEX (pkram_mutex);/* serializes open/close */
@@ -780,3 +792,41 @@ size_t pkram_read(struct pkram_access *pa, void *buf, size_t count) }return read_count; } + +/* +* Build the list of PKRAM nodes. +*/ +static void __pkram_reboot (void ) +{ +struct page *page; +struct pkram_node *node; +unsigned long node_pfn =0 ; + +list_for_each_entry_reverse (page, &pkram_nodes, lru) { + node =page_address (page); +if (WARN_ON (node->flags &PKRAM_ACCMODE_MASK )) +continue ; + node->node_pfn = node_pfn; + node_pfn =page_to_pfn (page); + } +} + +static int pkram_reboot (struct notifier_block *notifier, +unsigned long val,void *v) +{ +if (val !=SYS_RESTART ) +return NOTIFY_DONE ; +__pkram_reboot (); +return NOTIFY_OK ; +} + +static struct notifier_block pkram_reboot_notifier = { + .notifier_call = pkram_reboot, +}; + +static int __init pkram_init (void ) +{ +register_reboot_notifier (&pkram_reboot_notifier); +return 0 ; +} +module_init (pkram_init);
Signature · 1 line
1.8.3.1
RFC v2 8/43 mm: PKRAM: introduce super block
In reply to Anthony YznagaThe PKRAM super block is the starting point for restoring preserved memory. By providing the super block to the new kernel at boot time, preserved memory can be reserved and made available to be restored. To point the kernel to the location of the super block, one passes its pfn via the 'pkram' boot param. For that purpose, the pkram super block pfn is exported via /sys/kernel/pkram. If none is passed, any preserved memory will not be kept, and a new super block will be allocated.
--- mm/pkram.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 2 deletions(-)
mm/pkram.c +100 -2
@@ -5,15 +5,18 @@#include <linux/init.h> #include <linux/io.h> #include <linux/kernel.h> +#include <linux/kobject.h> #include <linux/list.h> #include <linux/mm.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/notifier.h> +#include <linux/pfn.h> #include <linux/pkram.h> #include <linux/reboot.h> #include <linux/sched.h> #include <linux/string.h> +#include <linux/sysfs.h> #include <linux/types.h> #include "internal.h"
@@ -84,12 +87,38 @@ struct pkram_node {#define PKRAM_ACCMODE_MASK 3/* +* The PKRAM super block contains data needed to restore the preserved memory +* structure on boot. The pointer to it (pfn) should be passed via the 'pkram' +* boot param if one wants to restore preserved data saved by the previously +* executing kernel. For that purpose the kernel exports the pfn via +* /sys/kernel/pkram. If none is passed, preserved memory if any will not be +* preserved and a new clean page will be allocated for the super block. +* +* The structure occupies a memory page. +*/ +struct pkram_super_block { +__u64 node_pfn;/* first element of the node list */ +}; + +static unsigned long pkram_sb_pfn__initdata ; +static struct pkram_super_block *pkram_sb; + +/* * For convenience sake PKRAM nodes are kept in an auxiliary doubly-linked list * connected through the lru field of the page struct. */ static LIST_HEAD (pkram_nodes);/* linked through page::lru */ static DEFINE_MUTEX (pkram_mutex);/* serializes open/close */ +/* +* The PKRAM super block pfn, see above. +*/ +static int __init parse_pkram_sb_pfn (char *arg) +{ +return kstrtoul (arg,16 , &pkram_sb_pfn); +} +early_param ("pkram" , parse_pkram_sb_pfn); +static inline struct page *pkram_alloc_page (gfp_t gfp_mask) {return alloc_page (gfp_mask);
@@ -275,6 +304,7 @@ static void pkram_stream_init(struct pkram_stream *ps,* @gfp_mask specifies the memory allocation mask to be used when saving data. * * Error values: +* %ENODEV: PKRAM not available * %ENAMETOOLONG: name len >= PKRAM_NAME_MAX * %ENOMEM: insufficient memory available * %EEXIST: node with specified name already exists
@@ -290,6 +320,9 @@ int pkram_prepare_save(struct pkram_stream *ps, const char *name, gfp_t gfp_maskstruct pkram_node *node;int err =0 ; +if (!pkram_sb) +return -ENODEV ; +if (strlen (name) >=PKRAM_NAME_MAX )return -ENAMETOOLONG ;
@@ -410,6 +443,7 @@ void pkram_discard_save(struct pkram_stream *ps)* Returns 0 on success, -errno on failure. * * Error values: +* %ENODEV: PKRAM not available * %ENOENT: node with specified name does not exist * %EBUSY: save to required node has not finished yet *
@@ -420,6 +454,9 @@ int pkram_prepare_load(struct pkram_stream *ps, const char *name)struct pkram_node *node;int err =0 ; +if (!pkram_sb) +return -ENODEV ; +mutex_lock (&pkram_mutex); node =pkram_find_node (name);if (!node) {
@@ -809,6 +846,13 @@ static void __pkram_reboot(void) node->node_pfn = node_pfn; node_pfn =page_to_pfn (page); } + +/* +* Zero out pkram_sb completely since it may have been passed from +* the previous boot. +*/ +memset (pkram_sb,0 ,PAGE_SIZE ); + pkram_sb->node_pfn = node_pfn; } static int pkram_reboot(struct notifier_block *notifier,
@@ -816,7 +860,8 @@ static int pkram_reboot(struct notifier_block *notifier, {if (val !=SYS_RESTART )return NOTIFY_DONE ; -__pkram_reboot (); +if (pkram_sb) +__pkram_reboot ();return NOTIFY_OK ; }
@@ -824,9 +869,62 @@ static int pkram_reboot(struct notifier_block *notifier, .notifier_call = pkram_reboot, }; +static ssize_t show_pkram_sb_pfn (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +unsigned long pfn = pkram_sb ?PFN_DOWN (__pa (pkram_sb)) :0 ; + +return sprintf (buf,"%lx\n" , pfn); +} + +static struct kobj_attribute pkram_sb_pfn_attr = +__ATTR (pkram,0444 , show_pkram_sb_pfn,NULL ); + +static struct attribute *pkram_attrs[] = { + &pkram_sb_pfn_attr.attr, +NULL , +}; + +static struct attribute_group pkram_attr_group = { + .attrs = pkram_attrs, +}; + +/* returns non-zero on success */ +static int __init pkram_init_sb (void ) +{ +unsigned long pfn; +struct pkram_node *node; + +if (!pkram_sb) { +struct page *page; + + page =pkram_alloc_page (GFP_KERNEL | __GFP_ZERO); +if (!page) { +pr_err ("PKRAM: Failed to allocate super block\n" ); +return 0 ; + } + pkram_sb =page_address (page); + } + +/* +* Build auxiliary doubly-linked list of nodes connected through +* page::lru for convenience sake. +*/ + pfn = pkram_sb->node_pfn; +while (pfn) { + node =pfn_to_kaddr (pfn); +pkram_insert_node (node); + pfn = node->node_pfn; + } +return 1 ; +} +static int __init pkram_init (void ) { -register_reboot_notifier (&pkram_reboot_notifier); +if (pkram_init_sb ()) { +register_reboot_notifier (&pkram_reboot_notifier); +sysfs_update_group (kernel_kobj, &pkram_attr_group); + }return 0 ; }module_init (pkram_init);
Signature · 7 lines
1.8.3.1 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
RFC v2 9/43 PKRAM: track preserved pages in a physical mapping pagetable
In reply to Anthony YznagaLater patches in this series will need a way to efficiently identify physically contiguous ranges of preserved pages independent of their virtual addresses. To facilitate this all pages to be preserved across kexec are added to a pseudo identity mapping pagetable.
The pagetable makes use of the existing architecture definitions for building a memory mapping pagetable except that a bitmap is used to represent the presence or absence of preserved pages at the PTE level.
--- mm/Makefile | 2 +- mm/pkram.c | 30 +++- mm/pkram_pagetable.c | 376 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 404 insertions(+), 4 deletions(-) create mode 100644 mm/pkram_pagetable.c
mm/Makefile +1 -1
@@ -120,4 +120,4 @@ obj-$(CONFIG_MEMFD_CREATE) += memfd.o obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o obj-$(CONFIG_PTDUMP_CORE) += ptdump.o obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o -obj-$(CONFIG_PKRAM) += pkram.o +obj-$(CONFIG_PKRAM) += pkram.o pkram_pagetable.o
mm/pkram.c +27 -3
@@ -103,6 +103,9 @@ struct pkram_super_block {static unsigned long pkram_sb_pfn__initdata ;static struct pkram_super_block *pkram_sb; +extern int pkram_add_identity_map (struct page *page); +extern void pkram_remove_identity_map (struct page *page); +/* * For convenience sake PKRAM nodes are kept in an auxiliary doubly-linked list * connected through the lru field of the page struct.
@@ -121,11 +124,24 @@ static int __init parse_pkram_sb_pfn(char *arg)static inline struct page *pkram_alloc_page (gfp_t gfp_mask) { -return alloc_page (gfp_mask); +struct page *page; +int err; + + page =alloc_page (gfp_mask); +if (page) { + err =pkram_add_identity_map (page); +if (err) { +__free_page (page); + page =NULL ; + } + } + +return page; }static inline void pkram_free_page (void *addr) { +pkram_remove_identity_map (virt_to_page (addr));free_page ((unsigned long )addr); }
@@ -163,6 +179,7 @@ static void pkram_truncate_link(struct pkram_link *link)if (!p)continue ; page =pfn_to_page (PHYS_PFN (p)); +pkram_remove_identity_map (page);put_page (page); } }
@@ -615,10 +632,15 @@ static int __pkram_save_page(struct pkram_access *pa, struct page *page,int pkram_save_file_page (struct pkram_access *pa,struct page *page) {struct pkram_node *node = pa->ps->node; +int err;BUG_ON ((node->flags &PKRAM_ACCMODE_MASK ) !=PKRAM_SAVE ); -return __pkram_save_page (pa, page, page->index); + err =__pkram_save_page (pa, page, page->index); +if (!err) + err =pkram_add_identity_map (page); + +return err; }static int __pkram_bytes_save_page (struct pkram_access *pa,struct page *page)
@@ -652,6 +674,8 @@ static struct page *__pkram_prep_load_page(pkram_entry_t p)prep_transhuge_page (page); } +pkram_remove_identity_map (page); +return page; }
@@ -898,7 +922,7 @@ static int __init pkram_init_sb(void)if (!pkram_sb) {struct page *page; - page =pkram_alloc_page (GFP_KERNEL | __GFP_ZERO); + page =alloc_page (GFP_KERNEL | __GFP_ZERO);if (!page) {pr_err ("PKRAM: Failed to allocate super block\n" );return 0 ;
mm/pkram_pagetable.c new file +376 -0
376 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 10/43 PKRAM: pass a list of preserved ranges to the next kernel
In reply to Anthony YznagaIn order to build a new memblock reserved list during boot that includes ranges preserved by the previous kernel, a list of preserved ranges is passed to the next kernel via the pkram superblock. The ranges are stored in ascending order in a linked list of pages. A more complete memblock list is not prepared to avoid possible conflicts with changes in a newer kernel and to avoid having to allocate a contiguous range larger than a page.
--- mm/pkram.c | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 176 insertions(+), 7 deletions(-)
mm/pkram.c +176 -7
@@ -86,6 +86,20 @@ struct pkram_node {#define PKRAM_LOAD 2#define PKRAM_ACCMODE_MASK 3 +struct pkram_region { +phys_addr_t base; +phys_addr_t size; +}; + +struct pkram_region_list { +__u64 prev_pfn; +__u64 next_pfn; + +struct pkram_region regions[0 ]; +}; + +#define PKRAM_REGIONS_LIST_MAX \ + ((PAGE_SIZE-sizeof(struct pkram_region_list))/sizeof(struct pkram_region))/* * The PKRAM super block contains data needed to restore the preserved memory * structure on boot. The pointer to it (pfn) should be passed via the 'pkram'
@@ -98,13 +112,20 @@ struct pkram_node {*/ struct pkram_super_block {__u64 node_pfn;/* first element of the node list */ +__u64 region_list_pfn; +__u64 nr_regions; }; +static struct pkram_region_list *pkram_regions_list; +static int pkram_init_regions_list (void ); +static unsigned long pkram_populate_regions_list (void ); +static unsigned long pkram_sb_pfn__initdata ;static struct pkram_super_block *pkram_sb;extern int pkram_add_identity_map (struct page *page);extern void pkram_remove_identity_map (struct page *page); +extern void pkram_find_preserved (unsigned long start,unsigned long end,void *private,int (*callback)(unsigned long base,unsigned long size,void *private));/* * For convenience sake PKRAM nodes are kept in an auxiliary doubly-linked list
@@ -862,21 +883,48 @@ static void __pkram_reboot(void)struct page *page;struct pkram_node *node;unsigned long node_pfn =0 ; +unsigned long rl_pfn =0 ; +unsigned long nr_regions =0 ; +int err =0 ; -list_for_each_entry_reverse (page, &pkram_nodes, lru) { - node =page_address (page); -if (WARN_ON (node->flags &PKRAM_ACCMODE_MASK )) -continue ; - node->node_pfn = node_pfn; - node_pfn =page_to_pfn (page); +if (!list_empty (&pkram_nodes)) { + err =pkram_add_identity_map (virt_to_page (pkram_sb)); +if (err) { +pr_err ("PKRAM: failed to add super block to pagetable\n" ); + goto done; + } +list_for_each_entry_reverse (page, &pkram_nodes, lru) { + node =page_address (page); +if (WARN_ON (node->flags &PKRAM_ACCMODE_MASK )) +continue ; + node->node_pfn = node_pfn; + node_pfn =page_to_pfn (page); + } + err =pkram_init_regions_list (); +if (err) { +pr_err ("PKRAM: failed to init regions list\n" ); + goto done; + } + nr_regions =pkram_populate_regions_list (); +if (IS_ERR_VALUE (nr_regions)) { + err = nr_regions; +pr_err ("PKRAM: failed to populate regions list\n" ); + goto done; + } + rl_pfn =page_to_pfn (virt_to_page (pkram_regions_list)); } +done:/* * Zero out pkram_sb completely since it may have been passed from * the previous boot. */ memset (pkram_sb,0 ,PAGE_SIZE ); - pkram_sb->node_pfn = node_pfn; +if (!err && node_pfn) { + pkram_sb->node_pfn = node_pfn; + pkram_sb->region_list_pfn = rl_pfn; + pkram_sb->nr_regions = nr_regions; + } } static int pkram_reboot(struct notifier_block *notifier,
@@ -952,3 +1000,124 @@ static int __init pkram_init(void)return 0 ; }module_init (pkram_init); + +static int count_region_cb (unsigned long base,unsigned long size,void *private) +{ +unsigned long *nr_regions = (unsigned long *)private; + + (*nr_regions)++; +return 0 ; +} + +static unsigned long pkram_count_regions (void ) +{ +unsigned long nr_regions =0 ; + +pkram_find_preserved (0 ,PHYS_ADDR_MAX , &nr_regions, count_region_cb); + +return nr_regions; +} + +/* +* To faciliate rapidly building a new memblock reserved list during boot +* with the addition of preserved memory ranges a regions list is built +* before reboot. +* The regions list is a linked list of pages with each page containing an +* array of preserved memory ranges. The ranges are stored in each page +* and across the list in address order. A linked list is used rather than +* a single contiguous range to mitigate against the possibility that a +* larger, contiguous allocation may fail due to fragmentation. +* +* Since the pages of the regions list must be preserved and the pkram +* pagetable is used to determine what ranges are preserved, the list pages +* must be allocated and represented in the pkram pagetable before they can +* be populated. Rather than recounting the number of regions after +* allocating pages and repeating until a precise number of pages are +* are allocated, the number of pages needed is estimated. +*/ +static int pkram_init_regions_list (void ) +{ +struct pkram_region_list *rl; +unsigned long nr_regions; +unsigned long nr_lpages; +struct page *page; + + nr_regions =pkram_count_regions (); + + nr_lpages =DIV_ROUND_UP (nr_regions,PKRAM_REGIONS_LIST_MAX ); + nr_regions += nr_lpages; + nr_lpages =DIV_ROUND_UP (nr_regions,PKRAM_REGIONS_LIST_MAX ); + +for (; nr_lpages; nr_lpages--) { + page =pkram_alloc_page (GFP_KERNEL | __GFP_ZERO); +if (!page) +return -ENOMEM ; + rl =page_address (page); +if (pkram_regions_list) { + rl->next_pfn =page_to_pfn (virt_to_page (pkram_regions_list)); + pkram_regions_list->prev_pfn =page_to_pfn (page); + } + pkram_regions_list = rl; + } + +return 0 ; +} + +struct pkram_regions_priv { +struct pkram_region_list *curr; +struct pkram_region_list *last; +unsigned long nr_regions; +int idx; +}; + +static int add_region_cb (unsigned long base,unsigned long size,void *private) +{ +struct pkram_regions_priv *priv; +struct pkram_region_list *rl; +int i; + + priv = (struct pkram_regions_priv *)private; + rl = priv->curr; + i = priv->idx; + +if (!rl) { +WARN_ON (1 ); +return 1 ; + } + +if (!i) + priv->last = priv->curr; + + rl->regions[i].base = base; + rl->regions[i].size = size; + + priv->nr_regions++; + i++; +if (i ==PKRAM_REGIONS_LIST_MAX ) { +u64 next_pfn = rl->next_pfn; + +if (next_pfn) + priv->curr =pfn_to_kaddr (next_pfn); +else + priv->curr =NULL ; + + i =0 ; + } + priv->idx = i; + +return 0 ; +} + +static unsigned long pkram_populate_regions_list (void ) +{ +struct pkram_regions_priv priv = { .curr = pkram_regions_list }; + +pkram_find_preserved (0 ,PHYS_ADDR_MAX , &priv, add_region_cb); + +/* +* Link the first node to the last populated one. +*/ + pkram_regions_list->prev_pfn =page_to_pfn (virt_to_page (priv.last)); + +return priv.nr_regions; +}
Signature · 1 line
1.8.3.1
RFC v2 11/43 PKRAM: prepare for adding preserved ranges to memblock reserved
In reply to Anthony YznagaCalling memblock_reserve() repeatedly to add preserved ranges is inefficient and risks clobbering preserved memory if the memblock reserved regions array must be resized. Instead, calculate the size needed to accomodate the preserved ranges, find a suitable range for a new reserved regions array that does not overlap any preserved range, and populate it with a new, merged regions array.
--- mm/pkram.c | 241 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 241 insertions(+)
mm/pkram.c +241 -0
@@ -7,6 +7,7 @@#include <linux/kernel.h> #include <linux/kobject.h> #include <linux/list.h> +#include <linux/memblock.h> #include <linux/mm.h> #include <linux/module.h> #include <linux/mutex.h>
@@ -1121,3 +1122,243 @@ static unsigned long pkram_populate_regions_list(void)return priv.nr_regions; } + +struct pkram_region *pkram_first_region (struct pkram_super_block *sb,struct pkram_region_list **rlp,int *idx) +{ +WARN_ON (!sb); +WARN_ON (!sb->region_list_pfn); + +if (!sb || !sb->region_list_pfn) +return NULL ; + + *rlp =pfn_to_kaddr (sb->region_list_pfn); + *idx =0 ; + +return &(*rlp)->regions[0 ]; +} + +struct pkram_region *pkram_next_region (struct pkram_region_list **rlp,int *idx) +{ +struct pkram_region_list *rl = *rlp; +int i = *idx; + + i++; +if (i >=PKRAM_REGIONS_LIST_MAX ) { +if (!rl->next_pfn) { +pr_err ("PKRAM: %s: no more pkram_region_list pages\n" , __func__); +return NULL ; + } + rl =pfn_to_kaddr (rl->next_pfn); + *rlp = rl; + i =0 ; + } + *idx = i; + +if (rl->regions[i].size ==0 ) +return NULL ; + +return &rl->regions[i]; +} + +struct pkram_region *pkram_first_region_topdown (struct pkram_super_block *sb,struct pkram_region_list **rlp,int *idx) +{ +struct pkram_region_list *rl; + +WARN_ON (!sb); +WARN_ON (!sb->region_list_pfn); + +if (!sb || !sb->region_list_pfn) +return NULL ; + + rl =pfn_to_kaddr (sb->region_list_pfn); +if (!rl->prev_pfn) { +WARN_ON (1 ); +return NULL ; + } + rl =pfn_to_kaddr (rl->prev_pfn); + + *rlp = rl; + + *idx = (sb->nr_regions -1 ) %PKRAM_REGIONS_LIST_MAX ; + +return &rl->regions[*idx]; +} + +struct pkram_region *pkram_next_region_topdown (struct pkram_region_list **rlp,int *idx) +{ +struct pkram_region_list *rl = *rlp; +int i = *idx; + +if (i ==0 ) { +if (!rl->prev_pfn) +return NULL ; + rl =pfn_to_kaddr (rl->prev_pfn); + *rlp = rl; + i =PKRAM_REGIONS_LIST_MAX -1 ; + }else + i--; + + *idx = i; + +return &rl->regions[i]; +} + +/* +* Use the pkram regions list to find an available block of memory that does +* not overlap with preserved pages. +*/ +phys_addr_t __init find_available_topdown (phys_addr_t size) +{ +phys_addr_t hole_start, hole_end, hole_size; +struct pkram_region_list *rl; +struct pkram_region *r; +phys_addr_t addr =0 ; +int idx; + + hole_end = memblock.current_limit; + r =pkram_first_region_topdown (pkram_sb, &rl, &idx); + +while (r) { + hole_start = r->base + r->size; + hole_size = hole_end - hole_start; + +if (hole_size >= size) { + addr =memblock_find_in_range (hole_start, hole_end, + size,PAGE_SIZE ); +if (addr) +break ; + } + + hole_end = r->base; + r =pkram_next_region_topdown (&rl, &idx); + } + +if (!addr) + addr =memblock_find_in_range (0 , hole_end, size,PAGE_SIZE ); + +return addr; +} + +int __init pkram_create_merged_reserved (struct memblock_type *new) +{ +unsigned long cnt_a; +unsigned long cnt_b; +long i, j, k; +struct memblock_region *r; +struct memblock_region *rgn; +struct pkram_region *pkr; +struct pkram_region_list *rl; +int idx; +unsigned long total_size =0 ; +unsigned long nr_preserved =0 ; + + cnt_a = memblock.reserved.cnt; + cnt_b = pkram_sb->nr_regions; + + i =0 ; + j =0 ; + k =0 ; + + pkr =pkram_first_region (pkram_sb, &rl, &idx); +if (!pkr) +return -EINVAL ; +while (i < cnt_a && j < cnt_b && pkr) { + r = &memblock.reserved.regions[i]; + rgn = &new->regions[k]; + +if (r->base + r->size <= pkr->base) { + *rgn = *r; + i++; + }else if (pkr->base + pkr->size <= r->base) { + rgn->base = pkr->base; + rgn->size = pkr->size; +memblock_set_region_node (rgn,MAX_NUMNODES ); + + nr_preserved += (rgn->size >>PAGE_SHIFT ); + pkr =pkram_next_region (&rl, &idx); + j++; + }else { +pr_err ("PKRAM: unexpected overlap:\n" ); +pr_err ("PKRAM: reserved: base=%pa,size=%pa,flags=0x%x\n" , &r->base, &r->size, (int )r->flags); +pr_err ("PKRAM: pkram: base=%pa,size=%pa\n" , &pkr->base, &pkr->size); +return -EBUSY ; + } + total_size += rgn->size; + k++; + } + +while (i < cnt_a) { + r = &memblock.reserved.regions[i]; + rgn = &new->regions[k]; + + *rgn = *r; + + total_size += rgn->size; + i++; + k++; + } +while (j < cnt_b && pkr) { + rgn = &new->regions[k]; + rgn->base = pkr->base; + rgn->size = pkr->size; +memblock_set_region_node (rgn,MAX_NUMNODES ); + + nr_preserved += (rgn->size >>PAGE_SHIFT ); + total_size += rgn->size; + pkr =pkram_next_region (&rl, &idx); + j++; + k++; + } + +WARN_ON (cnt_a + cnt_b != k); + new->cnt = cnt_a + cnt_b; + new->total_size = total_size; + +return 0 ; +} + +/* +* Reserve pages that belong to preserved memory. This is accomplished by +* merging the existing reserved ranges with the preserved ranges into +* a new, sufficiently sized memblock reserved array. +* +* This function should be called at boot time as early as possible to prevent +* preserved memory from being recycled. +*/ +int __init pkram_merge_with_reserved (void ) +{ +struct memblock_type new; +unsigned long new_max; +phys_addr_t new_size; +phys_addr_t addr; +int err; + +/* +* Need space to insert one more range into memblock.reserved +* without memblock_double_array() being called. +*/ +if (memblock.reserved.cnt == memblock.reserved.max) { +WARN_ONCE (1 ,"PKRAM: no space for new memblock list\n" ); +return -ENOMEM ; + } + + new_max = memblock.reserved.max + pkram_sb->nr_regions; + new_size =PAGE_ALIGN (sizeof (struct memblock_region ) * new_max); + + addr =find_available_topdown (new_size); +if (!addr ||memblock_reserve (addr, new_size)) +return -ENOMEM ; + + new.regions =__va (addr); + new.max = new_max; + err =pkram_create_merged_reserved (&new); +if (err) +return err; + + memblock.reserved.cnt = new.cnt; + memblock.reserved.max = new.max; + memblock.reserved.total_size = new.total_size; + memblock.reserved.regions = new.regions; + +return 0 ; +}
Signature · 1 line
1.8.3.1
RFC v2 12/43 mm: PKRAM: reserve preserved memory at boot
In reply to Anthony YznagaKeep preserved pages from being recycled during boot by adding them to the memblock reserved list during early boot. If memory reservation fails (e.g. a region has already been reserved), all preserved pages are dropped.
--- arch/x86/kernel/setup.c | 3 ++ arch/x86/mm/init_64.c | 2 ++ include/linux/pkram.h | 8 ++++++ mm/pkram.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 87 insertions(+), 2 deletions(-)
arch/x86/kernel/setup.c +3 -0
@@ -15,6 +15,7 @@#include <linux/iscsi_ibft.h> #include <linux/memblock.h> #include <linux/pci.h> +#include <linux/pkram.h> #include <linux/root_dev.h> #include <linux/hugetlb.h> #include <linux/tboot.h>
@@ -1146,6 +1147,8 @@ void __init setup_arch(char **cmdline_p)initmem_init ();dma_contiguous_reserve (max_pfn_mapped <<PAGE_SHIFT ); +pkram_reserve (); +if (boot_cpu_has (X86_FEATURE_GBPAGES ))hugetlb_cma_reserve (PUD_SHIFT -PAGE_SHIFT );
arch/x86/mm/init_64.c +2 -0
@@ -33,6 +33,7 @@#include <linux/nmi.h> #include <linux/gfp.h> #include <linux/kcore.h> +#include <linux/pkram.h> #include <asm/processor.h> #include <asm/bios_ebda.h>
@@ -1293,6 +1294,7 @@ void __init mem_init(void) after_bootmem =1 ; x86_init.hyper.init_after_bootmem (); +totalram_pages_add (pkram_reserved_pages);/* * Must be done after boot memory is put on freelist, because here we * might set fields in deferred struct pages that have not yet been
include/linux/pkram.h +8 -0
@@ -99,4 +99,12 @@ int pkram_prepare_save(struct pkram_stream *ps, const char *name,ssize_t pkram_write (struct pkram_access *pa,const void *buf,size_t count);size_t pkram_read (struct pkram_access *pa,void *buf,size_t count); +#ifdef CONFIG_PKRAM +extern unsigned long pkram_reserved_pages; +void pkram_reserve (void ); +#else +#define pkram_reserved_pages 0UL +static inline void pkram_reserve (void ) { } +#endif +#endif /* _LINUX_PKRAM_H */
mm/pkram.c +74 -2
118 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 13/43 PKRAM: free the preserved ranges list
In reply to Anthony YznagaFree the pages used to pass the preserved ranges to the new boot.
--- arch/x86/mm/init_64.c | 1 + include/linux/pkram.h | 2 ++ mm/pkram.c | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+)
arch/x86/mm/init_64.c +1 -0
@@ -1294,6 +1294,7 @@ void __init mem_init(void) after_bootmem =1 ; x86_init.hyper.init_after_bootmem (); +pkram_cleanup ();totalram_pages_add (pkram_reserved_pages);/* * Must be done after boot memory is put on freelist, because here we
include/linux/pkram.h +2 -0
@@ -102,9 +102,11 @@ int pkram_prepare_save(struct pkram_stream *ps, const char *name,#ifdef CONFIG_PKRAM extern unsigned long pkram_reserved_pages;void pkram_reserve (void ); +void pkram_cleanup (void );#else #define pkram_reserved_pages 0ULstatic inline void pkram_reserve (void ) { } +static inline void pkram_cleanup (void ) { }#endif #endif /* _LINUX_PKRAM_H */
mm/pkram.c +20 -0
@@ -1434,3 +1434,23 @@ int __init pkram_merge_with_reserved(void)return 0 ; } + +void __init pkram_cleanup (void ) +{ +struct pkram_region_list *rl; +unsigned long next_pfn; + +if (!pkram_sb || !pkram_reserved_pages) +return ; + + next_pfn = pkram_sb->region_list_pfn; + +while (next_pfn) { +struct page *page =pfn_to_page (next_pfn); + + rl =pfn_to_kaddr (next_pfn); + next_pfn = rl->next_pfn; +__free_pages_core (page,0 ); + pkram_reserved_pages--; + } +}
Signature · 1 line
1.8.3.1
RFC v2 14/43 PKRAM: prevent inadvertent use of a stale superblock
In reply to Anthony YznagaWhen pages have been saved to be preserved by the current boot, set a magic number on the super block to be validated by the next kernel.
--- mm/pkram.c | 9 +++++++++ 1 file changed, 9 insertions(+)
mm/pkram.c +9 -0
@@ -22,6 +22,7 @@#include "internal.h" +#define PKRAM_MAGIC 0x706B726D/* * Represents a reference to a data page saved to PKRAM.
@@ -112,6 +113,8 @@ struct pkram_region_list {* The structure occupies a memory page. */ struct pkram_super_block { +__u32 magic; +__u64 node_pfn;/* first element of the node list */ __u64 region_list_pfn;__u64 nr_regions;
@@ -180,6 +183,11 @@ void __init pkram_reserve(void) err =PTR_ERR (pkram_sb); goto out; } +if (pkram_sb->magic !=PKRAM_MAGIC ) { +pr_err ("PKRAM: invalid super block\n" ); + err = -EINVAL ; + goto out; + }/* An empty pkram_sb is not an error */ if (!pkram_sb->node_pfn) { pkram_sb =NULL ;
@@ -993,6 +1001,7 @@ static void __pkram_reboot(void)*/ memset (pkram_sb,0 ,PAGE_SIZE );if (!err && node_pfn) { + pkram_sb->magic =PKRAM_MAGIC ; pkram_sb->node_pfn = node_pfn; pkram_sb->region_list_pfn = rl_pfn; pkram_sb->nr_regions = nr_regions;
Signature · 1 line
1.8.3.1
RFC v2 15/43 PKRAM: provide a way to ban pages from use by PKRAM
In reply to Anthony YznagaNot all memory ranges can be used for saving preserved over-kexec data. For example, a kexec kernel may be loaded before pages are preserved. The memory regions where the kexec segments will be copied to on kexec must not contain preserved pages or else they will be clobbered.
--- include/linux/pkram.h | 2 + mm/pkram.c | 205 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 207 insertions(+)
include/linux/pkram.h +2 -0
@@ -103,10 +103,12 @@ int pkram_prepare_save(struct pkram_stream *ps, const char *name,extern unsigned long pkram_reserved_pages;void pkram_reserve (void );void pkram_cleanup (void ); +void pkram_ban_region (unsigned long start,unsigned long end);#else #define pkram_reserved_pages 0ULstatic inline void pkram_reserve (void ) { }static inline void pkram_cleanup (void ) { } +static inline void pkram_ban_region (unsigned long start,unsigned long end) { }#endif #endif /* _LINUX_PKRAM_H */
mm/pkram.c +205 -0
259 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 16/43 kexec: PKRAM: prevent kexec clobbering preserved pages in some cases
In reply to Anthony YznagaWhen loading a kernel for kexec, dynamically update the list of physical ranges that are not to be used for storing preserved pages with the ranges where kexec segments will be copied to on reboot. This ensures no pages preserved after the new kernel has been loaded will reside in these ranges on reboot.
Not yet handled is the case where pages have been preserved before a kexec kernel is loaded. This will be covered by a later patch.
--- kernel/kexec.c | 9 +++++++++ kernel/kexec_file.c | 10 ++++++++++ 2 files changed, 19 insertions(+)
kernel/kexec.c +9 -0
@@ -16,6 +16,7 @@#include <linux/syscalls.h> #include <linux/vmalloc.h> #include <linux/slab.h> +#include <linux/pkram.h> #include "kexec_internal.h"
@@ -163,6 +164,14 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments,if (ret) goto out; +for (i =0 ; i < nr_segments; i++) { +unsigned long mem = image->segment[i].mem; +size_t memsz = image->segment[i].memsz; + +if (memsz) +pkram_ban_region (PFN_DOWN (mem),PFN_UP (mem + memsz) -1 ); + } +/* Install the new kernel and uninstall the old */ image =xchg (dest_image, image);
kernel/kexec_file.c +10 -0
@@ -27,6 +27,8 @@#include <linux/kernel_read_file.h> #include <linux/syscalls.h> #include <linux/vmalloc.h> +#include <linux/pkram.h> +#include "kexec_internal.h" static int kexec_calculate_store_digests (struct kimage *image);
@@ -429,6 +431,14 @@ void kimage_file_post_load_cleanup(struct kimage *image)if (ret) goto out; +for (i =0 ; i < image->nr_segments; i++) { +unsigned long mem = image->segment[i].mem; +size_t memsz = image->segment[i].memsz; + +if (memsz) +pkram_ban_region (PFN_DOWN (mem),PFN_UP (mem + memsz) -1 ); + } +/* * Free up any temporary buffers allocated which are not needed * after image has been loaded
Signature · 1 line
1.8.3.1
RFC v2 17/43 PKRAM: provide a way to check if a memory range has preserved pages
In reply to Anthony YznagaWhen a kernel is loaded for kexec the address ranges where the kexec segments will be copied to may conflict with pages already set to be preserved. Provide a way to determine if preserved pages exist in a specified range.
--- include/linux/pkram.h | 2 ++ mm/pkram.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+)
include/linux/pkram.h +2 -0
@@ -104,11 +104,13 @@ int pkram_prepare_save(struct pkram_stream *ps, const char *name,void pkram_reserve (void );void pkram_cleanup (void );void pkram_ban_region (unsigned long start,unsigned long end); +int pkram_has_preserved_pages (unsigned long start,unsigned long end);#else #define pkram_reserved_pages 0ULstatic inline void pkram_reserve (void ) { }static inline void pkram_cleanup (void ) { }static inline void pkram_ban_region (unsigned long start,unsigned long end) { } +static inline int pkram_has_preserved_pages (unsigned long start,unsigned long end) {return 0 ; }#endif #endif /* _LINUX_PKRAM_H */
mm/pkram.c +20 -0
@@ -1668,3 +1668,23 @@ void __init pkram_cleanup(void) pkram_reserved_pages--; } } + +static int has_preserved_pages_cb (unsigned long base,unsigned long size,void *private) +{ +int *has_preserved = (int *)private; + + *has_preserved =1 ; +return 1 ; +} + +/* +* Check whether the memory range [start, end) contains preserved pages. +*/ +int pkram_has_preserved_pages (unsigned long start,unsigned long end) +{ +int has_preserved =0 ; + +pkram_find_preserved (start, end, &has_preserved, has_preserved_pages_cb); + +return has_preserved; +}
Signature · 1 line
1.8.3.1
RFC v2 18/43 kexec: PKRAM: avoid clobbering already preserved pages
In reply to Anthony YznagaEnsure destination ranges of the kexec segments do not overlap with any kernel pages marked to be preserved across kexec.
For kexec_load, return EADDRNOTAVAIL if overlap is detected.
For kexec_file_load, skip ranges containing preserved pages when seaching for available ranges to use.
--- kernel/kexec_core.c | 3 +++ kernel/kexec_file.c | 5 +++++ 2 files changed, 8 insertions(+)
kernel/kexec_core.c +3 -0
@@ -37,6 +37,7 @@#include <linux/compiler.h> #include <linux/hugetlb.h> #include <linux/objtool.h> +#include <linux/pkram.h> #include <asm/page.h> #include <asm/sections.h>
@@ -175,6 +176,8 @@ int sanity_check_segment_list(struct kimage *image)return -EADDRNOTAVAIL ;if (mend >=KEXEC_DESTINATION_MEMORY_LIMIT )return -EADDRNOTAVAIL ; +if (pkram_has_preserved_pages (mstart, mend)) +return -EADDRNOTAVAIL ; }/* Verify our destination addresses do not overlap.
kernel/kexec_file.c +5 -0
@@ -516,6 +516,11 @@ static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,continue ; } +if (pkram_has_preserved_pages (temp_start, temp_end +1 )) { + temp_start = temp_start -PAGE_SIZE ; +continue ; + } +/* We found a suitable memory range */ break ; }while (1 );
Signature · 1 line
1.8.3.1
RFC v2 19/43 mm: PKRAM: allow preserved memory to be freed from userspace
In reply to Anthony YznagaTo free all space utilized for preserved memory, one can write 0 to /sys/kernel/pkram. This will destroy all PKRAM nodes that are not currently being read or written.
--- mm/pkram.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-)
mm/pkram.c +38 -1
@@ -493,6 +493,32 @@ static void pkram_truncate_node(struct pkram_node *node) node->obj_pfn =0 ; } +/* +* Free all nodes that are not under operation. +*/ +static void pkram_truncate (void ) +{ +struct page *page, *tmp; +struct pkram_node *node; +LIST_HEAD (dispose); + +mutex_lock (&pkram_mutex); +list_for_each_entry_safe (page, tmp, &pkram_nodes, lru) { + node =page_address (page); +if (!(node->flags &PKRAM_ACCMODE_MASK )) +list_move (&page->lru, &dispose); + } +mutex_unlock (&pkram_mutex); + +while (!list_empty (&dispose)) { + page = list_first_entry(&dispose, struct page, lru); +list_del (&page->lru); + node =page_address (page); +pkram_truncate_node (node); +pkram_free_page (node); + } +} +static void pkram_add_link (struct pkram_link *link,struct pkram_data_stream *pds) {__u64 link_pfn =page_to_pfn (virt_to_page (link));
@@ -1233,8 +1259,19 @@ static ssize_t show_pkram_sb_pfn(struct kobject *kobj,return sprintf (buf,"%lx\n" , pfn); } +static ssize_t store_pkram_sb_pfn (struct kobject *kobj, +struct kobj_attribute *attr,const char *buf,size_t count) +{ +int val; + +if (kstrtoint (buf,0 , &val) || val) +return -EINVAL ; +pkram_truncate (); +return count; +} +static struct kobj_attribute pkram_sb_pfn_attr = -__ATTR (pkram,0444 , show_pkram_sb_pfn,NULL ); +__ATTR (pkram,0644 , show_pkram_sb_pfn, store_pkram_sb_pfn);static struct attribute *pkram_attrs[] = { &pkram_sb_pfn_attr.attr,
Signature · 1 line
1.8.3.1
RFC v2 20/43 PKRAM: disable feature when running the kdump kernel
In reply to Anthony YznagaThe kdump kernel should not preserve or restore pages.
--- mm/pkram.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
mm/pkram.c +6 -2
@@ -1,4 +1,5 @@// SPDX-License-Identifier: GPL-2.0 +#include <linux/crash_dump.h> #include <linux/err.h> #include <linux/gfp.h> #include <linux/highmem.h>
@@ -189,7 +190,7 @@ void __init pkram_reserve(void) {int err =0 ; -if (!pkram_sb_pfn) +if (!pkram_sb_pfn ||is_kdump_kernel ())return ;pr_info ("PKRAM: Examining preserved memory...\n" );
@@ -286,6 +287,9 @@ static void pkram_show_banned(void)int i;unsigned long n, total =0 ; +if (is_kdump_kernel ()) +return ; +pr_info ("PKRAM: banned regions:\n" );for (i =0 ; i < nr_banned; i++) { n = banned[i].end - banned[i].start +1 ;
@@ -1315,7 +1319,7 @@ static int __init pkram_init_sb(void)static int __init pkram_init (void ) { -if (pkram_init_sb ()) { +if (!is_kdump_kernel () &&pkram_init_sb ()) {register_reboot_notifier (&pkram_reboot_notifier);register_shrinker (&banned_pages_shrinker);sysfs_update_group (kernel_kobj, &pkram_attr_group);
Signature · 1 line
1.8.3.1
RFC v2 21/43 x86/KASLR: PKRAM: support physical kaslr
In reply to Anthony YznagaAvoid regions of memory that contain preserved pages when computing slots used to select where to put the decompressed kernel.
--- arch/x86/boot/compressed/Makefile | 3 ++ arch/x86/boot/compressed/kaslr.c | 10 +++- arch/x86/boot/compressed/misc.h | 10 ++++ arch/x86/boot/compressed/pkram.c | 109 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 arch/x86/boot/compressed/pkram.c
arch/x86/boot/compressed/Makefile +3 -0
@@ -93,6 +93,9 @@ ifdef CONFIG_X86_64 vmlinux-objs-y += $(obj)/mem_encrypt.o vmlinux-objs-y += $(obj)/pgtable_64.o vmlinux-objs-$(CONFIG_AMD_MEM_ENCRYPT) += $(obj)/sev-es.o +ifdef CONFIG_RANDOMIZE_BASE + vmlinux-objs-$(CONFIG_PKRAM) += $(obj)/pkram.o +endif endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o
arch/x86/boot/compressed/kaslr.c +8 -2
@@ -440,6 +440,7 @@ static bool mem_avoid_overlap(struct mem_vector *img,struct setup_data *ptr;u64 earliest = img->start + img->size;bool is_overlapping = false; +struct mem_vector avoid;for (i =0 ; i <MEM_AVOID_MAX ; i++) { if (mem_overlaps(img, &mem_avoid[i]) &&
@@ -453,8 +454,6 @@ static bool mem_avoid_overlap(struct mem_vector *img,/* Avoid all entries in the setup_data linked list. */ ptr = (struct setup_data *)(unsigned long )boot_params->hdr.setup_data;while (ptr) { -struct mem_vector avoid; - avoid.start = (unsigned long )ptr; avoid.size =sizeof (*ptr) + ptr->len;
@@ -479,6 +478,12 @@ static bool mem_avoid_overlap(struct mem_vector *img, ptr = (struct setup_data *)(unsigned long )ptr->next; } +if (pkram_has_overlap (img, &avoid) && (avoid.start < earliest)) { + *overlap = avoid; + earliest = overlap->start; + is_overlapping = true; + } +return is_overlapping; }
@@ -840,6 +845,7 @@ void choose_random_location(unsigned long input,return ; } +pkram_init (); boot_params->hdr.loadflags |=KASLR_FLAG ;if (IS_ENABLED (CONFIG_X86_32 ))
arch/x86/boot/compressed/misc.h +10 -0
@@ -116,6 +116,16 @@ static inline void console_init(void) { }#endif +#ifdef CONFIG_PKRAM +void pkram_init (void ); +int pkram_has_overlap (struct mem_vector *entry,struct mem_vector *overlap); +#else +static inline void pkram_init (void ) { } +static inline int pkram_has_overlap (struct mem_vector *entry, +struct mem_vector *overlap); +{return 0 ; } +#endif +void set_sev_encryption_mask (void );#ifdef CONFIG_AMD_MEM_ENCRYPT
arch/x86/boot/compressed/pkram.c new file +109 -0
109 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 22/43 x86/boot/compressed/64: use 1GB pages for mappings
In reply to Anthony Yznagapkram kaslr code can incur multiple page faults when it walks its preserved ranges list called via mem_avoid_overlap(). The multiple faults can easily end up using up the small number of pages available to be allocated for page table pages.
This patch hacks things so that mappings are 1GB which results in the need for far fewer page table pages. As is this breaks AMD SEV-ES which expects the mappings to be 2M. This could possibly be fixed by updating split code to split 1GB page if the aren't any other issues with using 1GB mappings.
--- arch/x86/boot/compressed/ident_map_64.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/ident_map_64.c +5 -4
@@ -95,8 +95,8 @@ static void add_identity_map(unsigned long start, unsigned long end)int ret;/* Align boundary to 2M. */ - start =round_down (start,PMD_SIZE ); - end =round_up (end,PMD_SIZE ); + start =round_down (start,PUD_SIZE ); + end =round_up (end,PUD_SIZE );if (start >= end)return ;
@@ -119,6 +119,7 @@ void initialize_identity_maps(void *rmode) mapping_info.context = &pgt_data; mapping_info.page_flag = __PAGE_KERNEL_LARGE_EXEC | sme_me_mask; mapping_info.kernpg_flag = _KERNPG_TABLE; + mapping_info.direct_gbpages = true;/* * It should be impossible for this not to already be true,
@@ -329,8 +330,8 @@ void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code) ghcb_fault =sev_es_check_ghcb_fault (address); - address &=PMD_MASK ; - end = address +PMD_SIZE ; + address &=PUD_MASK ; + end = address +PUD_SIZE ;/* * Check for unexpected error codes. Unexpected are:
Signature · 1 line
1.8.3.1
RFC v2 23/43 mm: shmem: introduce shmem_insert_page
In reply to Anthony YznagaThe function inserts a page into a shmem file at a specified offset. The page can be a regular PAGE_SIZE page or a transparent huge page. If there is something at the offset (page or swap), the function fails.
The function will be used by the next patch.
--- include/linux/shmem_fs.h | 3 ++ mm/shmem.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+)
include/linux/shmem_fs.h +3 -0
@@ -103,6 +103,9 @@ enum sgp_type {extern int shmem_getpage (struct inode *inode,pgoff_t index,struct page **pagep,enum sgp_type sgp); +extern int shmem_insert_page (struct mm_struct *mm,struct inode *inode, +pgoff_t index,struct page *page); +static inline struct page *shmem_read_mapping_page (struct address_space *mapping,pgoff_t index) {
mm/shmem.c +77 -0
83 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 24/43 mm: shmem: enable saving to PKRAM
In reply to Anthony YznagaThis patch illustrates how the PKRAM API can be used for preserving tmpfs. Two options are added to tmpfs:
The 'pkram=' option specifies the PKRAM node to load/save the filesystem tree from/to.
The 'preserve' option initiates preservation of a read-only filesystem tree.
If the 'pkram=' options is passed on mount, shmem will look for the corresponding PKRAM node and load the FS tree from it.
If the 'pkram=' options was passed on mount and the 'preserve' option is passed on remount and the filesystem is read-only, shmem will save the FS tree to the PKRAM node.
A typical usage scenario looks like:
# mount -t tmpfs -o pkram=mytmpfs none /mnt # echo something > /mnt/smth # mount -o remount ro,preserve /mnt <possibly kexec> # mount -t tmpfs -o pkram=mytmpfs none /mnt # cat /mnt/smth
Each FS tree is saved into a PKRAM node, and each file is saved into a PKRAM object. A byte stream written to the object is used for saving file metadata (name, permissions, etc) while the page stream written to the object accommodates file content pages and their offsets.
This implementation serves as a demonstration and therefore is
simplified: it supports only regular files in the root directory without multiple hard links, and it does not save swapped out files and aborts if any are found. However, it can be elaborated to fully support tmpfs.
--- include/linux/shmem_fs.h | 24 +++ mm/Makefile | 2 +- mm/shmem.c | 64 ++++++++ mm/shmem_pkram.c | 385 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 474 insertions(+), 1 deletion(-) create mode 100644 mm/shmem_pkram.c
include/linux/shmem_fs.h +24 -0
@@ -26,6 +26,11 @@ struct shmem_inode_info {struct inode vfs_inode; }; +#define SHMEM_PKRAM_NAME_MAX 128 +struct shmem_pkram_info { +char name[SHMEM_PKRAM_NAME_MAX ]; +}; +struct shmem_sb_info {unsigned long max_blocks;/* How many blocks are allowed */ struct percpu_counter used_blocks;/* How many are allocated */
@@ -43,6 +48,8 @@ struct shmem_sb_info {spinlock_t shrinklist_lock;/* Protects shrinklist */ struct list_head shrinklist;/* List of shinkable inodes */ unsigned long shrinklist_len;/* Length of shrinklist */ +struct shmem_pkram_info *pkram; +bool preserve;/* PKRAM-enabled data is preserved */ };static inline struct shmem_inode_info *SHMEM_I (struct inode *inode)
@@ -106,6 +113,23 @@ extern int shmem_getpage(struct inode *inode, pgoff_t index,extern int shmem_insert_page (struct mm_struct *mm,struct inode *inode,pgoff_t index,struct page *page); +#ifdef CONFIG_PKRAM +extern int shmem_parse_pkram (const char *str,struct shmem_pkram_info **pkram); +extern void shmem_show_pkram (struct seq_file *seq,struct shmem_pkram_info *pkram, +bool preserve); +extern int shmem_save_pkram (struct super_block *sb); +extern void shmem_load_pkram (struct super_block *sb); +extern int shmem_release_pkram (struct super_block *sb); +#else +static inline int shmem_parse_pkram (const char *str, +struct shmem_pkram_info **pkram) {return 1 ; } +static inline void shmem_show_pkram (struct seq_file *seq, +struct shmem_pkram_info *pkram,bool preserve) { } +static inline int shmem_save_pkram (struct super_block *sb) {return 0 ; } +static inline void shmem_load_pkram (struct super_block *sb) { } +static inline int shmem_release_pkram (struct super_block *sb) {return 0 ; } +#endif +static inline struct page *shmem_read_mapping_page (struct address_space *mapping,pgoff_t index) {
mm/Makefile +1 -1
@@ -120,4 +120,4 @@ obj-$(CONFIG_MEMFD_CREATE) += memfd.o obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o obj-$(CONFIG_PTDUMP_CORE) += ptdump.o obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o -obj-$(CONFIG_PKRAM) += pkram.o pkram_pagetable.o +obj-$(CONFIG_PKRAM) += pkram.o pkram_pagetable.o shmem_pkram.o
mm/shmem.c +64 -0
128 lines not on this page · raw message
mm/shmem_pkram.c new file +385 -0
385 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 25/43 mm: shmem: prevent swapping of PKRAM-enabled tmpfs pages
In reply to Anthony YznagaWork around the limitation that shmem pages must be in memory in order to be preserved by preventing them from being swapped out in the first place. Do this by marking shmem pages associated with a PKRAM node as unevictable.
--- mm/shmem.c | 2 ++ 1 file changed, 2 insertions(+)
mm/shmem.c +2 -0
@@ -2400,6 +2400,8 @@ static struct inode *shmem_get_inode(struct super_block *sb, const struct inodeINIT_LIST_HEAD (&info->swaplist);simple_xattrs_init (&info->xattrs);cache_no_acl (inode); +if (sbinfo->pkram) +mapping_set_unevictable (inode->i_mapping);switch (mode &S_IFMT ) {default :
Signature · 1 line
1.8.3.1
RFC v2 26/43 mm: shmem: specify the mm to use when inserting pages
In reply to Anthony YznagaExplicitly specify the mm to pass to shmem_insert_page() when the pkram_stream is initialized rather than use the mm of the current thread. This will allow for multiple kernel threads to target the same mm when inserting pages in parallel.
--- mm/shmem_pkram.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/shmem_pkram.c +3 -3
@@ -225,7 +225,7 @@ int shmem_save_pkram(struct super_block *sb)return err; } -static int load_file_content (struct pkram_stream *ps,struct address_space *mapping) +static int load_file_content (struct pkram_stream *ps,struct address_space *mapping,struct mm_struct *mm) {PKRAM_ACCESS (pa, ps, pages);unsigned long index;
@@ -237,7 +237,7 @@ static int load_file_content(struct pkram_stream *ps, struct address_space *mappif (!page)break ; - err =shmem_insert_page (current->mm, mapping->host, index, page); + err =shmem_insert_page (mm, mapping->host, index, page);put_page (page);cond_resched (); }while (!err);
@@ -291,7 +291,7 @@ static int load_file(struct dentry *parent, struct pkram_stream *ps, inode->i_ctime =ns_to_timespec64 (hdr.ctime);i_size_write (inode, hdr.size); - err =load_file_content (ps, inode->i_mapping); + err =load_file_content (ps, inode->i_mapping, current->mm); out_unlock:inode_unlock (d_inode (parent)); out:
Signature · 1 line
1.8.3.1
RFC v2 27/43 mm: shmem: when inserting, handle pages already charged to a memcg
In reply to Anthony YznagaIf shmem_insert_page() is called to insert a page that was preserved using PKRAM on the current boot (i.e. preserved page is restored without an intervening kexec boot), the page will still be charged to a memory cgroup because it is never freed. Don't try to charge it again.
--- mm/shmem.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
mm/shmem.c +8 -6
@@ -671,7 +671,7 @@ static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)static int shmem_add_to_page_cache (struct page *page,struct address_space *mapping,pgoff_t index,void *expected,gfp_t gfp, -struct mm_struct *charge_mm) +struct mm_struct *charge_mm,bool skipcharge) {XA_STATE_ORDER (xas, &mapping->i_pages, index,compound_order (page));unsigned long i =0 ;
@@ -688,7 +688,7 @@ static int shmem_add_to_page_cache(struct page *page, page->mapping = mapping; page->index = index; -if (!PageSwapCache (page)) { +if (!skipcharge && !PageSwapCache (page)) { error =mem_cgroup_charge (page, charge_mm, gfp);if (error) {if (PageTransHuge (page)) {
@@ -770,6 +770,7 @@ int shmem_insert_page(struct mm_struct *mm, struct inode *inode, pgoff_t index,int nr;pgoff_t hindex = index;bool on_lru =PageLRU (page); +bool ischarged =page_memcg (page) ? true : false;if (index > (MAX_LFS_FILESIZE >>PAGE_SHIFT ))return -EFBIG ;
@@ -809,7 +810,8 @@ int shmem_insert_page(struct mm_struct *mm, struct inode *inode, pgoff_t index,__SetPageReferenced (page); err =shmem_add_to_page_cache (page, mapping, hindex, -NULL , gfp &GFP_RECLAIM_MASK , mm); +NULL , gfp &GFP_RECLAIM_MASK , + mm, ischarged);if (err) goto out_unlock;
@@ -1829,7 +1831,7 @@ static int shmem_swapin_page(struct inode *inode, pgoff_t index, error =shmem_add_to_page_cache (page, mapping, index,swp_to_radix_entry (swap), gfp, - charge_mm); + charge_mm, false);if (error) goto failed;
@@ -2009,7 +2011,7 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, error =shmem_add_to_page_cache (page, mapping, hindex,NULL , gfp &GFP_RECLAIM_MASK , - charge_mm); + charge_mm, false);if (error) goto unacct;lru_cache_add (page);
@@ -2500,7 +2502,7 @@ static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, goto out_release; ret =shmem_add_to_page_cache (page, mapping, pgoff,NULL , - gfp &GFP_RECLAIM_MASK , dst_mm); + gfp &GFP_RECLAIM_MASK , dst_mm, false);if (ret) goto out_release;
Signature · 1 line
1.8.3.1
RFC v2 28/43 x86/mm/numa: add numa_isolate_memblocks()
In reply to Anthony YznagaProvide a way for a caller external to numa to ensure memblocks in the memblock reserved list do not cross node boundaries and have a node id assigned to them. This will be used by PKRAM to ensure initialization of page structs for preserved pages can be deferred and multithreaded efficiently.
--- arch/x86/include/asm/numa.h | 4 ++++ arch/x86/mm/numa.c | 32 ++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 12 deletions(-)
arch/x86/include/asm/numa.h +4 -0
@@ -41,6 +41,7 @@ static inline void set_apicid_to_node(int apicid, s16 node) }extern int numa_cpu_node (int cpu); +extern void __init numa_isolate_memblocks (void );#else /* CONFIG_NUMA */ static inline void set_apicid_to_node (int apicid,s16 node)
@@ -51,6 +52,9 @@ static inline int numa_cpu_node(int cpu) {return NUMA_NO_NODE ; } +static inline void numa_isolate_memblocks (void ) +{ +}#endif /* CONFIG_NUMA */ #ifdef CONFIG_X86_32
arch/x86/mm/numa.c +20 -12
@@ -473,6 +473,25 @@ static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)return true; } +void __init numa_isolate_memblocks (void ) +{ +int i; + +/* +* Iterate over all memory known to the x86 architecture, +* and use those ranges to set the nid in memblock.reserved. +* This will split up the memblock regions along node +* boundaries and will set the node IDs as well. +*/ +for (i =0 ; i < numa_meminfo.nr_blks; i++) { +struct numa_memblk *mb = numa_meminfo.blk + i; +int ret; + + ret =memblock_set_node (mb->start, mb->end - mb->start, &memblock.reserved, mb->nid); +WARN_ON_ONCE (ret); + } +} +/* * Mark all currently memblock-reserved physical memory (which covers the * kernel's own memory ranges) as hot-unswappable.
@@ -491,19 +510,8 @@ static void __init numa_clear_kernel_node_hotplug(void)* used by the kernel, but those regions are not split up * along node boundaries yet, and don't necessarily have their * node ID set yet either. -* -* So iterate over all memory known to the x86 architecture, -* and use those ranges to set the nid in memblock.reserved. -* This will split up the memblock regions along node -* boundaries and will set the node IDs as well. */ -for (i =0 ; i < numa_meminfo.nr_blks; i++) { -struct numa_memblk *mb = numa_meminfo.blk + i; -int ret; - - ret =memblock_set_node (mb->start, mb->end - mb->start, &memblock.reserved, mb->nid); -WARN_ON_ONCE (ret); - } +numa_isolate_memblocks ();/* * Now go over all reserved memblock regions, to construct a
Signature · 1 line
1.8.3.1
RFC v2 29/43 PKRAM: ensure memblocks with preserved pages init'd for numa
In reply to Anthony YznagaIn order to facilitate fast initialization of page structs for preserved pages, memblocks with preserved pages must not cross numa node boundaries and must have a node id assigned to them.
--- mm/pkram.c | 9 +++++++++ 1 file changed, 9 insertions(+)
mm/pkram.c +9 -0
@@ -21,6 +21,7 @@#include <linux/sysfs.h> #include <linux/types.h> +#include <asm/numa.h> #include "internal.h" #define PKRAM_MAGIC 0x706B726D
@@ -226,6 +227,14 @@ void __init pkram_reserve(void)return ; } +/* +* Fix up the reserved memblock list to ensure the +* memblock regions are split along node boundaries +* and have a node ID set. This will allow the page +* structs for the preserved pages to be initialized +* more efficiently. +*/ +numa_isolate_memblocks (); done:pr_info ("PKRAM: %lu pages reserved\n" , pkram_reserved_pages); }
Signature · 1 line
1.8.3.1
RFC v2 30/43 memblock: PKRAM: mark memblocks that contain preserved pages
In reply to Anthony YznagaTo support deferred initialization of page structs for preserved pages, separate memblocks containing preserved pages by setting a new flag when adding them to the memblock reserved list.
--- include/linux/memblock.h | 6 ++++++ mm/pkram.c | 2 ++ 2 files changed, 8 insertions(+)
include/linux/memblock.h +6 -0
@@ -37,6 +37,7 @@ enum memblock_flags {MEMBLOCK_HOTPLUG =0x1 ,/* hotpluggable region */ MEMBLOCK_MIRROR =0x2 ,/* mirrored region */ MEMBLOCK_NOMAP =0x4 ,/* don't add to kernel direct mapping */ + MEMBLOCK_PRESERVED = 0x8, /* preserved pages region */ };/**
@@ -248,6 +249,11 @@ static inline bool memblock_is_nomap(struct memblock_region *m)return m->flags &MEMBLOCK_NOMAP ; } +static inline bool memblock_is_preserved (struct memblock_region *m) +{ +return m->flags &MEMBLOCK_PRESERVED ; +} +int memblock_search_pfn_nid (unsigned long pfn,unsigned long *start_pfn,unsigned long *end_pfn); void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
mm/pkram.c +2 -0
@@ -1607,6 +1607,7 @@ int __init pkram_create_merged_reserved(struct memblock_type *new) }else if (pkr->base + pkr->size <= r->base) { rgn->base = pkr->base; rgn->size = pkr->size; + rgn->flags =MEMBLOCK_PRESERVED ;memblock_set_region_node (rgn,MAX_NUMNODES ); nr_preserved += (rgn->size >>PAGE_SHIFT );
@@ -1636,6 +1637,7 @@ int __init pkram_create_merged_reserved(struct memblock_type *new) rgn = &new->regions[k]; rgn->base = pkr->base; rgn->size = pkr->size; + rgn->flags =MEMBLOCK_PRESERVED ;memblock_set_region_node (rgn,MAX_NUMNODES ); nr_preserved += (rgn->size >>PAGE_SHIFT );
Signature · 1 line
1.8.3.1
RFC v2 31/43 memblock, mm: defer initialization of preserved pages
In reply to Anthony YznagaPreserved pages are represented in the memblock reserved list, but page structs for pages in the reserved list are initialized early while boot is single threaded which means that a large number of preserved pages can impact boot time. To mitigate, defer initialization of preserved pages by skipping them when other reserved pages are initialized and initializing them later with a separate kernel thread.
--- arch/x86/mm/init_64.c | 1 - include/linux/mm.h | 2 +- mm/memblock.c | 11 +++++++++-- mm/page_alloc.c | 55 +++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 57 insertions(+), 12 deletions(-)
arch/x86/mm/init_64.c +0 -1
@@ -1294,7 +1294,6 @@ void __init mem_init(void) after_bootmem =1 ; x86_init.hyper.init_after_bootmem (); -pkram_cleanup ();totalram_pages_add (pkram_reserved_pages);/* * Must be done after boot memory is put on freelist, because here we
include/linux/mm.h +1 -1
@@ -2337,7 +2337,7 @@ extern unsigned long free_reserved_area(void *start, void *end,extern void adjust_managed_page_count (struct page *page,long count);extern void mem_init_print_info (const char *str); -extern void reserve_bootmem_region (phys_addr_t start,phys_addr_t end); +extern void reserve_bootmem_region (phys_addr_t start,phys_addr_t end,int nid);/* Free the reserved page into the buddy system, so it gets managed. */ static inline void free_reserved_page (struct page *page)
mm/memblock.c +9 -2
@@ -2007,11 +2007,18 @@ static unsigned long __init free_low_memory_core_early(void)unsigned long count =0 ;phys_addr_t start, end;u64 i; +struct memblock_region *r;memblock_clear_hotplug (0 ,-1 ); -for_each_reserved_mem_range (i, &start, &end) -reserve_bootmem_region (start, end); +for_each_reserved_mem_region (r) { +if (IS_ENABLED (CONFIG_DEFERRED_STRUCT_PAGE_INIT ) &&memblock_is_preserved (r)) +continue ; + + start = r->base; + end = r->base + r->size; +reserve_bootmem_region (start, end,NUMA_NO_NODE ); + }/* * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
mm/page_alloc.c +47 -8
107 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 32/43 shmem: preserve shmem files a chunk at a time
In reply to Anthony YznagaTo prepare for multithreading the work to preserve a shmem file, divide the work into subranges of the total index range of the file. The chunk size is a rather arbitrary 256k indices.
--- mm/shmem_pkram.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 7 deletions(-)
mm/shmem_pkram.c +57 -7
@@ -74,16 +74,14 @@ static int save_page(struct page *page, struct pkram_access *pa)return err; } -static int save_file_content (struct pkram_stream *ps,struct address_space *mapping) +static int save_file_content_range (struct pkram_access *pa, +struct address_space *mapping, +unsigned long start,unsigned long end) { -PKRAM_ACCESS (pa, ps, pages);struct pagevec pvec; -unsigned long start, end;int err =0 ;int i; - start =0 ; - end =DIV_ROUND_UP (i_size_read (mapping->host),PAGE_SIZE );pagevec_init (&pvec);for ( ; ; ) { pvec.nr = find_get_pages_range(mapping, &start, end,
@@ -95,7 +93,7 @@ static int save_file_content(struct pkram_stream *ps, struct address_space *mapplock_page (page);BUG_ON (page->mapping != mapping); - err =save_page (page, &pa); + err =save_page (page, pa);if (PageCompound (page)) { start = page->index +compound_nr (page); i +=compound_nr (page);
@@ -113,10 +111,62 @@ static int save_file_content(struct pkram_stream *ps, struct address_space *mappcond_resched (); } -pkram_finish_access (&pa, err ==0 );return err; } +struct shmem_pkram_arg { +struct pkram_stream *ps; +struct address_space *mapping; +struct mm_struct *mm; +atomic64_t next; +}; + +unsigned long shmem_pkram_max_index_range =512 *512 ; + +static int get_save_range (unsigned long max,atomic64_t *next,unsigned long *start,unsigned long *end) +{ +unsigned long index; + + index =atomic64_fetch_add (shmem_pkram_max_index_range, next); +if (index >= max) +return -ENODATA ; + + *start = index; + *end = index + shmem_pkram_max_index_range -1 ; + +return 0 ; +} + +static int do_save_file_content (struct pkram_stream *ps, +struct address_space *mapping, +atomic64_t *next) +{ +PKRAM_ACCESS (pa, ps, pages); +unsigned long start, end, max; +int ret; + + max =DIV_ROUND_UP (i_size_read (mapping->host),PAGE_SIZE ); + +do { + ret =get_save_range (max, next, &start, &end); +if (!ret) + ret =save_file_content_range (&pa, mapping, start, end); + }while (!ret); + +if (ret == -ENODATA ) + ret =0 ; + +pkram_finish_access (&pa, ret ==0 ); +return ret; +} + +static int save_file_content (struct pkram_stream *ps,struct address_space *mapping) +{ +struct shmem_pkram_arg arg = { ps, mapping,NULL ,ATOMIC64_INIT (0 ) }; + +return do_save_file_content (arg.ps, arg.mapping, &arg.next); +} +static int save_file (struct dentry *dentry,struct pkram_stream *ps) {PKRAM_ACCESS (pa_bytes, ps, bytes);
Signature · 1 line
1.8.3.1
RFC v2 33/43 PKRAM: atomically add and remove link pages
In reply to Anthony YznagaAdd and remove pkram_link pages from a pkram_obj atomically to prepare for multithreading.
--- mm/pkram.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-)
mm/pkram.c +24 -15
@@ -535,33 +535,42 @@ static void pkram_truncate(void)static void pkram_add_link (struct pkram_link *link,struct pkram_data_stream *pds) {__u64 link_pfn =page_to_pfn (virt_to_page (link)); +__u64 *tail = pds->tail_link_pfnp; +__u64 tail_pfn; -if (!*pds->head_link_pfnp) { +do { + tail_pfn = *tail; + }while (cmpxchg64 (tail, tail_pfn, link_pfn) != tail_pfn); + +if (!tail_pfn) { *pds->head_link_pfnp = link_pfn; - *pds->tail_link_pfnp = link_pfn; }else { -struct pkram_link *tail =pfn_to_kaddr (*pds->tail_link_pfnp); +struct pkram_link *prev_tail =pfn_to_kaddr (tail_pfn); - tail->link_pfn = link_pfn; - *pds->tail_link_pfnp = link_pfn; + prev_tail->link_pfn = link_pfn; } }static struct pkram_link *pkram_remove_link (struct pkram_data_stream *pds) { -struct pkram_link *link; +__u64 *head = pds->head_link_pfnp; +__u64 head_pfn = *head; -if (!*pds->head_link_pfnp) -return NULL ; +while (head_pfn) { +struct pkram_link *link =pfn_to_kaddr (head_pfn); - link =pfn_to_kaddr (*pds->head_link_pfnp); - *pds->head_link_pfnp = link->link_pfn; -if (!*pds->head_link_pfnp) - *pds->tail_link_pfnp =0 ; -else - link->link_pfn =0 ; +if (cmpxchg64 (head, head_pfn, link->link_pfn) == head_pfn) { +if (!*head) + *pds->tail_link_pfnp =0 ; +else + link->link_pfn =0 ; +return link; + } -return link; + head_pfn = *head; + } + +return NULL ; }static struct pkram_link *pkram_new_link (struct pkram_data_stream *pds,gfp_t gfp_mask)
Signature · 1 line
1.8.3.1
RFC v2 34/43 shmem: PKRAM: multithread preserving and restoring shmem pages
In reply to Anthony YznagaImprove performance by multithreading the work to preserve and restore shmem pages.
When preserving pages each thread saves non-overlapping ranges of a file to a pkram_obj until all pages are preserved.
When restoring pages each thread loads pages using a local pkram_access.
--- mm/shmem_pkram.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 5 deletions(-)
mm/shmem_pkram.c +89 -5
@@ -115,6 +115,7 @@ static int save_file_content_range(struct pkram_access *pa, }struct shmem_pkram_arg { +int *error;struct pkram_stream *ps;struct address_space *mapping;struct mm_struct *mm;
@@ -137,6 +138,16 @@ static int get_save_range(unsigned long max, atomic64_t *next, unsigned long *streturn 0 ; } +/* Completion tracking for save_file_content_thr() threads */ +static atomic_t pkram_save_n_undone; +static DECLARE_COMPLETION (pkram_save_all_done_comp); + +static inline void pkram_save_report_one_done (void ) +{ +if (atomic_dec_and_test (&pkram_save_n_undone)) +complete (&pkram_save_all_done_comp); +} +static int do_save_file_content (struct pkram_stream *ps,struct address_space *mapping,atomic64_t *next)
@@ -160,11 +171,40 @@ static int do_save_file_content(struct pkram_stream *ps,return ret; } -static int save_file_content (struct pkram_stream *ps,struct address_space *mapping) +static int save_file_content_thr (void *data) { -struct shmem_pkram_arg arg = { ps, mapping,NULL ,ATOMIC64_INIT (0 ) }; - -return do_save_file_content (arg.ps, arg.mapping, &arg.next); +struct shmem_pkram_arg *arg = data; +int ret; + + ret =do_save_file_content (arg->ps, arg->mapping, &arg->next); +if (ret && !*arg->error) + *arg->error = ret; + +pkram_save_report_one_done (); +return 0 ; +} + +static int shmem_pkram_max_threads =16 ; + +static int save_file_content (struct pkram_stream *ps,struct address_space *mapping) + { +int err =0 ; +struct shmem_pkram_arg arg = { &err, ps, mapping,NULL ,ATOMIC64_INIT (0 ) }; +unsigned int thr, nr_threads; + + nr_threads =num_online_cpus () -1 ; + nr_threads =clamp_val (shmem_pkram_max_threads,1 , nr_threads); + +if (nr_threads ==1 ) +return do_save_file_content (arg.ps, arg.mapping, &arg.next); + +atomic_set (&pkram_save_n_undone, nr_threads); +for (thr =0 ; thr < nr_threads; thr++) +kthread_run (save_file_content_thr, &arg,"pkram_save%d" , thr); + +wait_for_completion (&pkram_save_all_done_comp); + +return err; }static int save_file (struct dentry *dentry,struct pkram_stream *ps)
@@ -275,7 +315,17 @@ int shmem_save_pkram(struct super_block *sb)return err; } -static int load_file_content (struct pkram_stream *ps,struct address_space *mapping,struct mm_struct *mm) +/* Completion tracking for load_file_content_thr() threads */ +static atomic_t pkram_load_n_undone; +static DECLARE_COMPLETION (pkram_load_all_done_comp); + +static inline void pkram_load_report_one_done (void ) +{ +if (atomic_dec_and_test (&pkram_load_n_undone)) +complete (&pkram_load_all_done_comp); +} + +static int do_load_file_content (struct pkram_stream *ps,struct address_space *mapping,struct mm_struct *mm) {PKRAM_ACCESS (pa, ps, pages);unsigned long index;
@@ -296,6 +346,40 @@ static int load_file_content(struct pkram_stream *ps, struct address_space *mappreturn err; } +static int load_file_content_thr (void *data) +{ +struct shmem_pkram_arg *arg = data; +int ret; + + ret =do_load_file_content (arg->ps, arg->mapping, arg->mm); +if (ret && !*arg->error) + *arg->error = ret; + +pkram_load_report_one_done (); +return 0 ; +} + +static int load_file_content (struct pkram_stream *ps,struct address_space *mapping,struct mm_struct *mm) +{ +int err =0 ; +struct shmem_pkram_arg arg = { &err, ps, mapping, mm }; +unsigned int thr, nr_threads; + + nr_threads =num_online_cpus () -1 ; + nr_threads =clamp_val (shmem_pkram_max_threads,1 , nr_threads); + +if (nr_threads ==1 ) +return do_load_file_content (ps, mapping, mm); + +atomic_set (&pkram_load_n_undone, nr_threads); +for (thr =0 ; thr < nr_threads; thr++) +kthread_run (load_file_content_thr, &arg,"pkram_load%d" , thr); + +wait_for_completion (&pkram_load_all_done_comp); + +return err; +} +static int load_file (struct dentry *parent,struct pkram_stream *ps,char *buf,size_t bufsize) {
Signature · 1 line
1.8.3.1
RFC v2 35/43 shmem: introduce shmem_insert_pages()
In reply to Anthony YznagaCalling shmem_insert_page() to insert one page at a time does not scale well when multiple threads are inserting pages into the same shmem segment. This is primarily due to the locking needed when adding to the pagecache and LRU but also due to contention on the shmem_inode_info lock. To address the shmem_inode_info lock and prepare for future optimizations, introduce shmem_insert_pages() which allows a caller to pass an array of pages to be inserted into a shmem segment.
--- include/linux/shmem_fs.h | 3 +- mm/shmem.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-)
include/linux/shmem_fs.h +2 -1
@@ -112,7 +112,8 @@ extern int shmem_getpage(struct inode *inode, pgoff_t index,extern int shmem_insert_page (struct mm_struct *mm,struct inode *inode,pgoff_t index,struct page *page); - +extern int shmem_insert_pages (struct mm_struct *mm,struct inode *inode, +pgoff_t index,struct page *pages[],int npages);#ifdef CONFIG_PKRAM extern int shmem_parse_pkram (const char *str,struct shmem_pkram_info **pkram); extern void shmem_show_pkram(struct seq_file *seq, struct shmem_pkram_info *pkram,
mm/shmem.c +93 -0
99 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 36/43 PKRAM: add support for loading pages in bulk
In reply to Anthony YznagaImplement a new API function, pkram_load_file_pages(), to support loading pages in bulk. A caller provided buffer not smaller than PKRAM_PAGES_BUFSIZE is populated with pages pointers that are contiguous by their original mapping index values. The number of pages in the buffer and the mapping index of the first page are provided to the caller.
--- include/linux/pkram.h | 4 ++++ mm/pkram.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+)
include/linux/pkram.h +4 -0
@@ -96,6 +96,10 @@ int pkram_prepare_save(struct pkram_stream *ps, const char *name,int pkram_save_file_page (struct pkram_access *pa,struct page *page);struct page *pkram_load_file_page (struct pkram_access *pa,unsigned long *index); +#define PKRAM_PAGES_BUFSIZE PAGE_SIZE + +int pkram_load_file_pages (struct pkram_access *pa,struct page *pages[],unsigned int *nr_pages,unsigned long *index); +ssize_t pkram_write (struct pkram_access *pa,const void *buf,size_t count);size_t pkram_read (struct pkram_access *pa,void *buf,size_t count);
mm/pkram.c +46 -0
52 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 37/43 shmem: PKRAM: enable bulk loading of preserved pages into shmem
In reply to Anthony YznagaMake use of new interfaces for loading and inserting preserved pages into a shmem file in bulk.
--- mm/shmem_pkram.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)
mm/shmem_pkram.c +17 -6
@@ -328,20 +328,31 @@ static inline void pkram_load_report_one_done(void)static int do_load_file_content (struct pkram_stream *ps,struct address_space *mapping,struct mm_struct *mm) {PKRAM_ACCESS (pa, ps, pages); +struct page **pages; +unsigned int nr_pages;unsigned long index; -struct page *page; -int err =0 ; +int i, err; + + pages =kzalloc (PKRAM_PAGES_BUFSIZE ,GFP_KERNEL ); +if (!pages) +return -ENOMEM ;do { - page =pkram_load_file_page (&pa, &index); -if (!page) + err =pkram_load_file_pages (&pa, pages, &nr_pages, &index); +if (err) { +if (err == -ENODATA ) + err =0 ;break ; + } + + err =shmem_insert_pages (mm, mapping->host, index, pages, nr_pages); - err =shmem_insert_page (mm, mapping->host, index, page); -put_page (page); +for (i =0 ; i < nr_pages; i++) +put_page (pages[i]);cond_resched (); }while (!err); +kfree (pages);pkram_finish_access (&pa, err ==0 );return err; }
Signature · 1 line
1.8.3.1
RFC v2 38/43 mm: implement splicing a list of pages to the LRU
In reply to Anthony YznagaConsiderable contention on the LRU lock happens when multiple threads are used to insert pages into a shmem file in parallel. To alleviate this provide a way for pages to be added to the same LRU to be staged so that they can be added by splicing lists and updating stats once with the lock held. For now only unevictable pages are supported.
--- include/linux/swap.h | 13 ++++++++ mm/swap.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+)
include/linux/swap.h +13 -0
@@ -351,6 +351,19 @@ extern void lru_note_cost(struct lruvec *lruvec, bool file,extern void lru_cache_add_inactive_or_unevictable (struct page *page,struct vm_area_struct *vma); +struct lru_splice { +struct list_head splice; +struct list_head *lru_head; +struct lruvec *lruvec; +enum lru_list lru; +unsigned long nr_pages[MAX_NR_ZONES ]; +unsigned long pgculled; +}; +#define LRU_SPLICE_INIT (name) { .splice = LIST_HEAD_INIT(name.splice) } +#define LRU_SPLICE (name) \ + struct lru_splice name = LRU_SPLICE_INIT(name) +extern void lru_splice_add (struct page *page,struct lru_splice *splice); +extern void add_splice_to_lru_list (struct lru_splice *splice);/* linux/mm/vmscan.c */ extern unsigned long zone_reclaimable_pages (struct zone *zone);
mm/swap.c +86 -0
92 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 39/43 shmem: optimize adding pages to the LRU in shmem_insert_pages()
In reply to Anthony YznagaReduce LRU lock contention when inserting shmem pages by staging pages to be added to the same LRU and adding them en masse.
--- mm/shmem.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
mm/shmem.c +4 -1
@@ -845,6 +845,7 @@ int shmem_insert_pages(struct mm_struct *charge_mm, struct inode *inode,struct shmem_inode_info *info =SHMEM_I (inode);struct shmem_sb_info *sbinfo =SHMEM_SB (inode->i_sb);gfp_t gfp =mapping_gfp_mask (mapping); +LRU_SPLICE (splice);int i, err;int nr =0 ;
@@ -908,7 +909,7 @@ int shmem_insert_pages(struct mm_struct *charge_mm, struct inode *inode,for (i =0 ; i < npages; i++) {if (!PageLRU (pages[i])) -lru_cache_add (pages[i]); +lru_splice_add (pages[i], &splice);flush_dcache_page (pages[i]);SetPageUptodate (pages[i]);
@@ -917,6 +918,8 @@ int shmem_insert_pages(struct mm_struct *charge_mm, struct inode *inode,unlock_page (pages[i]); } +add_splice_to_lru_list (&splice); +return 0 ; out_release:
Signature · 1 line
1.8.3.1
RFC v2 40/43 shmem: initial support for adding multiple pages to pagecache
In reply to Anthony Yznagashmem_insert_pages() currently loops over the array of pages passed to it and calls shmem_add_to_page_cache() for each one. Prepare for adding pages to the pagecache in bulk by adding and using a shmem_add_pages_to_cache() call. For now it just iterates over an array and adds pages individually, but improvements in performance when multiple threads are adding to the same pagecache are achieved by calling a new shmem_add_to_page_cache_fast() function that does not check for conflicts and drops the xarray lock before updating stats.
--- mm/shmem.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 108 insertions(+), 15 deletions(-)
mm/shmem.c +108 -15
@@ -738,6 +738,74 @@ static int shmem_add_to_page_cache(struct page *page,return error; } +static int shmem_add_to_page_cache_fast (struct page *page, +struct address_space *mapping, +pgoff_t index,gfp_t gfp, +struct mm_struct *charge_mm,bool skipcharge) +{ +XA_STATE_ORDER (xas, &mapping->i_pages, index,thp_order (page)); +unsigned long nr =thp_nr_pages (page); +unsigned long i =0 ; +int error; + +VM_BUG_ON_PAGE (PageTail (page), page); +VM_BUG_ON_PAGE (index !=round_down (index, nr), page); +VM_BUG_ON_PAGE (!PageLocked (page), page); +VM_BUG_ON_PAGE (!PageSwapBacked (page), page); + +page_ref_add (page, nr); + page->mapping = mapping; + page->index = index; + +if (!skipcharge && !PageSwapCache (page)) { + error =mem_cgroup_charge (page, charge_mm, gfp); +if (error) { +if (PageTransHuge (page)) { +count_vm_event (THP_FILE_FALLBACK ); +count_vm_event (THP_FILE_FALLBACK_CHARGE ); + } + goto error; + } + } +cgroup_throttle_swaprate (page, gfp); + +do { +xas_lock_irq (&xas); +xas_create_range (&xas); +if (xas_error (&xas)) + goto unlock; +next: +xas_store (&xas, page); +if (++i < nr) { +xas_next (&xas); + goto next; + } + mapping->nrpages += nr; +xas_unlock (&xas); +if (PageTransHuge (page)) { +count_vm_event (THP_FILE_ALLOC ); +__inc_node_page_state (page,NR_SHMEM_THPS ); + } +__mod_lruvec_page_state (page,NR_FILE_PAGES , nr); +__mod_lruvec_page_state (page,NR_SHMEM , nr); +local_irq_enable (); +break ; +unlock: +xas_unlock_irq (&xas); + }while (xas_nomem (&xas, gfp)); + +if (xas_error (&xas)) { + error =xas_error (&xas); + goto error; + } + +return 0 ; +error: + page->mapping =NULL ; +page_ref_sub (page, nr); +return error; +} +/* * Like delete_from_page_cache, but substitutes swap for page. */
@@ -759,6 +827,41 @@ static void shmem_delete_from_page_cache(struct page *page, void *radswap)BUG_ON (error); } +static int shmem_add_pages_to_cache (struct page *pages[],int npages, +struct address_space *mapping, +pgoff_t start,gfp_t gfp, +struct mm_struct *charge_mm) +{ +pgoff_t index = start; +int i, err; + + i =0 ; +while (i < npages) { +if (PageTransHuge (pages[i])) { + err =shmem_add_to_page_cache_fast (pages[i], mapping, index, gfp, charge_mm,page_memcg (pages[i]) ? true : false); +if (err) + goto out_release; + index +=thp_nr_pages (pages[i]); + i++; +continue ; + } + + err =shmem_add_to_page_cache_fast (pages[i], mapping, index, gfp, charge_mm,page_memcg (pages[i]) ? true : false); +if (err) + goto out_release; + index++; + i++; + } +return 0 ; + +out_release: +while (i < npages) { +delete_from_page_cache (pages[i]); + i--; + } +return err; +} +int shmem_insert_page (struct mm_struct *mm,struct inode *inode,pgoff_t index,struct page *page) {
@@ -889,17 +992,10 @@ int shmem_insert_pages(struct mm_struct *charge_mm, struct inode *inode,__SetPageReferenced (pages[i]); } -for (i =0 ; i < npages; i++) { -bool ischarged =page_memcg (pages[i]) ? true : false; - - err =shmem_add_to_page_cache (pages[i], mapping, index, -NULL , gfp &GFP_RECLAIM_MASK , - charge_mm, ischarged); -if (err) - goto out_release; - - index +=thp_nr_pages (pages[i]); - } + err =shmem_add_pages_to_cache (pages, npages, mapping, index, + gfp &GFP_RECLAIM_MASK , charge_mm); +if (err) + goto out_unlock;spin_lock (&info->lock); info->alloced += nr;
@@ -922,10 +1018,7 @@ int shmem_insert_pages(struct mm_struct *charge_mm, struct inode *inode,return 0 ; -out_release: -while (--i >=0 ) -delete_from_page_cache (pages[i]); - +out_unlock:for (i =0 ; i < npages; i++)unlock_page (pages[i]);
Signature · 1 line
1.8.3.1
RFC v2 41/43 XArray: add xas_export_node() and xas_import_node()
In reply to Anthony YznagaContention on the xarray lock when multiple threads are adding to the same xarray can be mitigated by providing a way to add entries in bulk.
Allow a caller to allocate and populate an xarray node outside of the target xarray and then only take the xarray lock long enough to import the node into it.
--- Documentation/core-api/xarray.rst | 8 +++ include/linux/xarray.h | 2 + lib/test_xarray.c | 45 +++++++++++++++++ lib/xarray.c | 100 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 155 insertions(+)
Documentation/core-api/xarray.rst +8 -0
@@ -444,6 +444,14 @@ called each time the XArray updates a node. This is used by the page cache workingset code to maintain its list of nodes which contain only shadow entries. +xas_export_node() is used to remove and return a node from an XArray +while xas_import_node() is used to add a node to an XArray. Together +these can be used, for example, to reduce lock contention when multiple +threads are updating an XArray by allowing a caller to allocate and +populate a node outside of the target XArray in a local XArray, export +the node, and then take the target XArray lock just long enough to import +the node. + Multi-Index Entries -------------------
include/linux/xarray.h +2 -0
@@ -1506,6 +1506,8 @@ static inline bool xas_retry(struct xa_state *xas, const void *entry)void xas_pause (struct xa_state *);void xas_create_range (struct xa_state *); +struct xa_node *xas_export_node (struct xa_state *xas); +void xas_import_node (struct xa_state *xas,struct xa_node *node);#ifdef CONFIG_XARRAY_MULTI int xa_get_order (struct xarray *,unsigned long index);
lib/test_xarray.c +45 -0
57 lines not on this page · raw message
lib/xarray.c +100 -0
112 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v2 42/43 shmem: reduce time holding xa_lock when inserting pages
In reply to Anthony YznagaRather than adding one page at a time to the page cache and taking the page cache xarray lock each time, where possible add pages in bulk by first populating an xarray node outside of the page cache before taking the lock to insert it.
When a group of pages to be inserted will fill an xarray node, add them to a local xarray, export the xarray node, and then take the lock on the page cache xarray and insert the node.
--- mm/shmem.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 156 insertions(+), 6 deletions(-)
mm/shmem.c +156 -6
@@ -827,17 +827,149 @@ static void shmem_delete_from_page_cache(struct page *page, void *radswap)BUG_ON (error); } +static int shmem_add_aligned_to_page_cache (struct page *pages[],int npages, +struct address_space *mapping, +pgoff_t index,gfp_t gfp,int order, +struct mm_struct *charge_mm) +{ +int xa_shift = order +XA_CHUNK_SHIFT - (order %XA_CHUNK_SHIFT ); +XA_STATE_ORDER (xas, &mapping->i_pages, index, xa_shift); +struct xarray xa_tmp; +/* +* Specify order so xas_create_range() only needs to be called once +* to allocate the entire range. This guarantees that xas_store() +* will not fail due to lack of memory. +* Specify index == 0 so the minimum necessary nodes are allocated. +*/ +XA_STATE_ORDER (xas_tmp, &xa_tmp,0 , xa_shift); +unsigned long nr =1UL << order; +struct xa_node *node; +int i, error; + +if (npages * nr !=1 << xa_shift) { +WARN_ONCE (1 ,"npages (%d) not aligned to xa_shift\n" , npages); +return -EINVAL ; + } +if (!IS_ALIGNED (index,1 << xa_shift)) { +WARN_ONCE (1 ,"index (%lu) not aligned to xa_shift\n" , index); +return -EINVAL ; + } + +for (i =0 ; i < npages; i++) { +bool skipcharge =page_memcg (pages[i]) ? true : false; + +VM_BUG_ON_PAGE (PageTail (pages[i]), pages[i]); +VM_BUG_ON_PAGE (!PageLocked (pages[i]), pages[i]); +VM_BUG_ON_PAGE (!PageSwapBacked (pages[i]), pages[i]); + +page_ref_add (pages[i], nr); + pages[i]->mapping = mapping; + pages[i]->index = index + (i * nr); + +if (!skipcharge && !PageSwapCache (pages[i])) { + error =mem_cgroup_charge (pages[i], charge_mm, gfp); +if (error) { +if (PageTransHuge (pages[i])) { +count_vm_event (THP_FILE_FALLBACK ); +count_vm_event (THP_FILE_FALLBACK_CHARGE ); + } + goto error; + } + } +cgroup_throttle_swaprate (pages[i], gfp); + } + +xa_init (&xa_tmp); +do { +xas_lock (&xas_tmp); +xas_create_range (&xas_tmp); +if (xas_error (&xas_tmp)) + goto unlock; +for (i =0 ; i < npages; i++) { +int j =0 ; +next: +xas_store (&xas_tmp, pages[i]); +if (++j < nr) { +xas_next (&xas_tmp); + goto next; + } +if (i < npages -1 ) +xas_next (&xas_tmp); + } +xas_set_order (&xas_tmp,0 , xa_shift); + node =xas_export_node (&xas_tmp); +unlock: +xas_unlock (&xas_tmp); + }while (xas_nomem (&xas_tmp, gfp)); + +if (xas_error (&xas_tmp)) { + error =xas_error (&xas_tmp); + i = npages -1 ; + goto error; + } + +do { +xas_lock_irq (&xas); +xas_import_node (&xas, node); +if (xas_error (&xas)) + goto unlock1; + mapping->nrpages += nr * npages; +xas_unlock (&xas); +for (i =0 ; i < npages; i++) { +__mod_lruvec_page_state (pages[i],NR_FILE_PAGES , nr); +__mod_lruvec_page_state (pages[i],NR_SHMEM , nr); +if (PageTransHuge (pages[i])) { +count_vm_event (THP_FILE_ALLOC ); +__inc_node_page_state (pages[i],NR_SHMEM_THPS ); + } + } +local_irq_enable (); +break ; +unlock1: +xas_unlock_irq (&xas); + }while (xas_nomem (&xas, gfp)); + +if (xas_error (&xas)) { + error =xas_error (&xas); + goto error; + } + +return 0 ; +error: +while (i !=0 ) { + pages[i]->mapping =NULL ; +page_ref_sub (pages[i], nr); + i--; + } +return error; +} +static int shmem_add_pages_to_cache (struct page *pages[],int npages,struct address_space *mapping,pgoff_t start,gfp_t gfp,struct mm_struct *charge_mm) {pgoff_t index = start; -int i, err; +int i, j, err; i =0 ;while (i < npages) {if (PageTransHuge (pages[i])) { +if (IS_ALIGNED (index,4096 ) && i+8 <= npages) { +for (j =1 ; j <8 ; j++) { +if (!PageTransHuge (pages[i+j])) +break ; + } +if (j ==8 ) { + err =shmem_add_aligned_to_page_cache (&pages[i],8 , mapping, index, gfp,HPAGE_PMD_ORDER , charge_mm); +if (err) + goto out_release; + index +=HPAGE_PMD_NR *8 ; + i +=8 ; +continue ; + } + } + err =shmem_add_to_page_cache_fast (pages[i], mapping, index, gfp, charge_mm,page_memcg (pages[i]) ? true : false);if (err) goto out_release;
@@ -846,11 +978,29 @@ static int shmem_add_pages_to_cache(struct page *pages[], int npages,continue ; } - err =shmem_add_to_page_cache_fast (pages[i], mapping, index, gfp, charge_mm,page_memcg (pages[i]) ? true : false); -if (err) - goto out_release; - index++; - i++; +for (j =1 ; i + j < npages; j++) { +if (PageTransHuge (pages[i + j])) +break ; + } + +while (j >0 ) { +if (IS_ALIGNED (index,64 ) && j >=64 ) { + err =shmem_add_aligned_to_page_cache (&pages[i],64 , mapping, index, gfp,0 , charge_mm); +if (err) + goto out_release; + index +=64 ; + i +=64 ; + j -=64 ; +continue ; + } + + err =shmem_add_to_page_cache_fast (pages[i], mapping, index, gfp, charge_mm,page_memcg (pages[i]) ? true : false); +if (err) + goto out_release; + index++; + i++; + j--; + } }return 0 ;
Signature · 7 lines
1.8.3.1 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
RFC v2 43/43 PKRAM: improve index alignment of pkram_link entries
In reply to Anthony YznagaTo take advantage of optimizations when adding pages to the page cache via shmem_insert_pages(), improve the likelihood that the pages array passed to shmem_insert_pages() starts on an aligned index. Do this when preserving pages by starting a new pkram_link page when the current page is aligned and the next aligned page will not fit on the pkram_link page.
--- mm/pkram.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
mm/pkram.c +12 -1
@@ -911,9 +911,20 @@ static int __pkram_save_page(struct pkram_access *pa, struct page *page, {struct pkram_data_stream *pds = &pa->pds;struct pkram_link *link = pds->link; +int align, align_cnt; + +if (PageTransHuge (page)) { + align =1 << (HPAGE_PMD_ORDER +XA_CHUNK_SHIFT - (HPAGE_PMD_ORDER %XA_CHUNK_SHIFT )); + align_cnt = align >>HPAGE_PMD_ORDER ; + }else { + align =XA_CHUNK_SIZE ; + align_cnt =XA_CHUNK_SIZE ; + }if (!link || pds->entry_idx >=PKRAM_LINK_ENTRIES_MAX || - index != pa->pages.next_index) { + index != pa->pages.next_index || + (IS_ALIGNED (index, align) && + (pds->entry_idx + align_cnt >PKRAM_LINK_ENTRIES_MAX ))) { link =pkram_new_link (pds, pa->ps->gfp_mask);if (!link)return -ENOMEM ;
Signature · 1 line
1.8.3.1
Pavel Tatashin
maintainerQuoted 7 lines · Anthony Yznaga
This patchset implements preserved-over-kexec memory storage or PKRAM as a method for saving memory pages of the currently executing kernel so that they may be restored after kexec into a new kernel. The patches are adapted from an RFC patchset sent out in 2013 by Vladimir Davydov [1]. They introduce the PKRAM kernel API and implement its use within tmpfs, allowing tmpfs files to be preserved across kexec.
Quoted 7 lines
One use case for PKRAM is preserving guest memory and/or auxillary supporting data (e.g. iommu data) across kexec in support of VMM Fast Restart[2]. VMM Fast Restart is currently using PKRAM to support preserving "Keep Alive State" across reboot[3]. PKRAM provides a flexible way for doing this without requiring that the amount of memory used by a fixed size created a priori. Another use case is for databases to preserve their block caches in shared memory across reboot.
Hi Anthony,
I have several concerns about preserving arbitrary not prereserved segments across reboot.
1. PKRAM does not work across firmware reboots
With emulated persistent memory it is possible to do reboot through firmware and not loose the preserved-memory. The firmware can be modified to mark the required ranges pages as PRAM, and Linux will treat them as such. The benefit of this is that it works for both cases kexec and reboot through firmware. The disadvantage is that you have to know in advance how much memory needs to be preserved. However, with the ability to hot-plug/hot-remove the PMEM, the second point becomes moot as it is possible to mark a large chunk of memory as PMEM if needed. I have designed something like this for one of our projects, and it is already been used in the fleet. Reboot through firmware, allows us to service firmware in addition to kernel.
2. Boot failures due to memory fragmentation
We also considered using PRAM instead of PMEM. PRAM was one of the previous attempts to do the persistent memory thing via tmpfs flag: mount -t tmpfs -o pram=mytmpfs none /mnt/crdump"; that project was never upstreamed. However, we gave up with that idea because in addition to loosing possibility to reboot through the firmware, it also adds memory fragmentation. For example, if the new kernel require larger contiguous memory chunks to be allocated during boot than the previous kernel (i.e. the next kernel has new drivers, or some debug feature enabled), the boot might simply fail because of the extra memory ranges being reserved.
3. New intra-kernel dependencies
Kexec reboot is when one Linux kernel works as a bootloader for the next one. Currently, there is very little information that is passed from the old kernel to the next kernel. Adding more information that two independent kernels must know about each other is not a very good thing from architectural point of view. It limits the flexibility of kexec.
However, we do need PKRAM and ability to preserve kernel memory across reboot for fast hypervisor updates or such. User pages can already be preserved across reboot on emulated or real persistent memory. The easiest way is via DAXFS placed on that memory.
Kernel cannot preserve its memory on PMEM across the reboot. However, functionality can be extended so kernel memory can be preserved on both emulated persistent memory or on real persistent memory. PKRAM could provide an interface to save kernel data to a file, and that file could be placed on any filesystem including DAXFS. When placed on DAXFS, that file can be used as iommu data, as it is actually located in physical memory and not moving anywhere. It is preserved across firmware/kexec reboot with having the devices survive the reboot state intact. During boot, have the device drivers that use PKRAM preserve functionality map saved files from DAXFS in order to have IOMMU functionality working again.
Thank you,
Pasha