included in this merge do the following: - Kemeng Shi has contributed some compation maintenance work in the series "Fixes and cleanups to compaction". - Joel Fernandes has a patchset ("Optimize mremap during mutual alignment within PMD") which fixes an obscure issue with mremap()'s pagetable handling during a subsequent exec(), based upon an implementation which Linus suggested. - More DAMON/DAMOS maintenance and feature work from SeongJae Park i the following patch series: mm/damon: misc fixups for documents, comments and its tracepoint mm/damon: add a tracepoint for damos apply target regions mm/damon: provide pseudo-moving sum based access rate mm/damon: implement DAMOS apply intervals mm/damon/core-test: Fix memory leaks in core-test mm/damon/sysfs-schemes: Do DAMOS tried regions update for only one apply interval - In the series "Do not try to access unaccepted memory" Adrian Hunter provides some fixups for the recently-added "unaccepted memory' feature. To increase the feature's checking coverage. "Plug a few gaps where RAM is exposed without checking if it is unaccepted memory". - In the series "cleanups for lockless slab shrink" Qi Zheng has done some maintenance work which is preparation for the lockless slab shrinking code. - Qi Zheng has redone the earlier (and reverted) attempt to make slab shrinking lockless in the series "use refcount+RCU method to implement lockless slab shrink". - David Hildenbrand contributes some maintenance work for the rmap code in the series "Anon rmap cleanups". - Kefeng Wang does more folio conversions and some maintenance work in the migration code. Series "mm: migrate: more folio conversion and unification". - Matthew Wilcox has fixed an issue in the buffer_head code which was causing long stalls under some heavy memory/IO loads. Some cleanups were added on the way. Series "Add and use bdev_getblk()". - In the series "Use nth_page() in place of direct struct page manipulation" Zi Yan has fixed a potential issue with the direct manipulation of hugetlb page frames. - In the series "mm: hugetlb: Skip initialization of gigantic tail struct pages if freed by HVO" has improved our handling of gigantic pages in the hugetlb vmmemmep optimizaton code. This provides significant boot time improvements when significant amounts of gigantic pages are in use. - Matthew Wilcox has sent the series "Small hugetlb cleanups" - code rationalization and folio conversions in the hugetlb code. - Yin Fengwei has improved mlock()'s handling of large folios in the series "support large folio for mlock" - In the series "Expose swapcache stat for memcg v1" Liu Shixin has added statistics for memcg v1 users which are available (and useful) under memcg v2. - Florent Revest has enhanced the MDWE (Memory-Deny-Write-Executable) prctl so that userspace may direct the kernel to not automatically propagate the denial to child processes. The series is named "MDWE without inheritance". - Kefeng Wang has provided the series "mm: convert numa balancing functions to use a folio" which does what it says. - In the series "mm/ksm: add fork-exec support for prctl" Stefan Roesch makes is possible for a process to propagate KSM treatment across exec(). - Huang Ying has enhanced memory tiering's calculation of memory distances. This is used to permit the dax/kmem driver to use "high bandwidth memory" in addition to Optane Data Center Persistent Memory Modules (DCPMM). The series is named "memory tiering: calculate abstract distance based on ACPI HMAT" - In the series "Smart scanning mode for KSM" Stefan Roesch has optimized KSM by teaching it to retain and use some historical information from previous scans. - Yosry Ahmed has fixed some inconsistencies in memcg statistics in the series "mm: memcg: fix tracking of pending stats updates values". - In the series "Implement IOCTL to get and optionally clear info about PTEs" Peter Xu has added an ioctl to /proc/<pid>/pagemap which permits us to atomically read-then-clear page softdirty state. This is mainly used by CRIU. - Hugh Dickins contributed the series "shmem,tmpfs: general maintenance" - a bunch of relatively minor maintenance tweaks to this code. - Matthew Wilcox has increased the use of the VMA lock over file-backed page faults in the series "Handle more faults under the VMA lock". Some rationalizations of the fault path became possible as a result. - In the series "mm/rmap: convert page_move_anon_rmap() to folio_move_anon_rmap()" David Hildenbrand has implemented some cleanups and folio conversions. - In the series "various improvements to the GUP interface" Lorenzo Stoakes has simplified and improved the GUP interface with an eye to providing groundwork for future improvements. - Andrey Konovalov has sent along the series "kasan: assorted fixes and improvements" which does those things. - Some page allocator maintenance work from Kemeng Shi in the series "Two minor cleanups to break_down_buddy_pages". - In thes series "New selftest for mm" Breno Leitao has developed another MM self test which tickles a race we had between madvise() and page faults. - In the series "Add folio_end_read" Matthew Wilcox provides cleanups and an optimization to the core pagecache code. - Nhat Pham has added memcg accounting for hugetlb memory in the series "hugetlb memcg accounting". - Cleanups and rationalizations to the pagemap code from Lorenzo Stoakes, in the series "Abstract vma_merge() and split_vma()". - Audra Mitchell has fixed issues in the procfs page_owner code's new timestamping feature which was causing some misbehaviours. In the series "Fix page_owner's use of free timestamps". - Lorenzo Stoakes has fixed the handling of new mappings of sealed files in the series "permit write-sealed memfd read-only shared mappings". - Mike Kravetz has optimized the hugetlb vmemmap optimization in the series "Batch hugetlb vmemmap modification operations". - Some buffer_head folio conversions and cleanups from Matthew Wilcox in the series "Finish the create_empty_buffers() transition". - As a page allocator performance optimization Huang Ying has added automatic tuning to the allocator's per-cpu-pages feature, in the series "mm: PCP high auto-tuning". - Roman Gushchin has contributed the patchset "mm: improve performance of accounted kernel memory allocations" which improves their performance by ~30% as measured by a micro-benchmark. - folio conversions from Kefeng Wang in the series "mm: convert page cpupid functions to folios". - Some kmemleak fixups in Liu Shixin's series "Some bugfix about kmemleak". - Qi Zheng has improved our handling of memoryless nodes by keeping them off the allocation fallback list. This is done in the series "handle memoryless nodes more appropriately". - khugepaged conversions from Vishal Moola in the series "Some khugepaged folio conversions". -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQTTMBEPP41GrTpTJgfdBJ7gKXxAjgUCZULEMwAKCRDdBJ7gKXxA jhQHAQCYpD3g849x69DmHnHWHm/EHQLvQmRMDeYZI+nx/sCJOwEAw4AKg0Oemv9y FgeUPAD1oasg6CP+INZvCj34waNxwAc= =E+Y4 -----END PGP SIGNATURE----- Merge tag 'mm-stable-2023-11-01-14-33' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Pull MM updates from Andrew Morton: "Many singleton patches against the MM code. The patch series which are included in this merge do the following: - Kemeng Shi has contributed some compation maintenance work in the series 'Fixes and cleanups to compaction' - Joel Fernandes has a patchset ('Optimize mremap during mutual alignment within PMD') which fixes an obscure issue with mremap()'s pagetable handling during a subsequent exec(), based upon an implementation which Linus suggested - More DAMON/DAMOS maintenance and feature work from SeongJae Park i the following patch series: mm/damon: misc fixups for documents, comments and its tracepoint mm/damon: add a tracepoint for damos apply target regions mm/damon: provide pseudo-moving sum based access rate mm/damon: implement DAMOS apply intervals mm/damon/core-test: Fix memory leaks in core-test mm/damon/sysfs-schemes: Do DAMOS tried regions update for only one apply interval - In the series 'Do not try to access unaccepted memory' Adrian Hunter provides some fixups for the recently-added 'unaccepted memory' feature. To increase the feature's checking coverage. 'Plug a few gaps where RAM is exposed without checking if it is unaccepted memory' - In the series 'cleanups for lockless slab shrink' Qi Zheng has done some maintenance work which is preparation for the lockless slab shrinking code - Qi Zheng has redone the earlier (and reverted) attempt to make slab shrinking lockless in the series 'use refcount+RCU method to implement lockless slab shrink' - David Hildenbrand contributes some maintenance work for the rmap code in the series 'Anon rmap cleanups' - Kefeng Wang does more folio conversions and some maintenance work in the migration code. Series 'mm: migrate: more folio conversion and unification' - Matthew Wilcox has fixed an issue in the buffer_head code which was causing long stalls under some heavy memory/IO loads. Some cleanups were added on the way. Series 'Add and use bdev_getblk()' - In the series 'Use nth_page() in place of direct struct page manipulation' Zi Yan has fixed a potential issue with the direct manipulation of hugetlb page frames - In the series 'mm: hugetlb: Skip initialization of gigantic tail struct pages if freed by HVO' has improved our handling of gigantic pages in the hugetlb vmmemmep optimizaton code. This provides significant boot time improvements when significant amounts of gigantic pages are in use - Matthew Wilcox has sent the series 'Small hugetlb cleanups' - code rationalization and folio conversions in the hugetlb code - Yin Fengwei has improved mlock()'s handling of large folios in the series 'support large folio for mlock' - In the series 'Expose swapcache stat for memcg v1' Liu Shixin has added statistics for memcg v1 users which are available (and useful) under memcg v2 - Florent Revest has enhanced the MDWE (Memory-Deny-Write-Executable) prctl so that userspace may direct the kernel to not automatically propagate the denial to child processes. The series is named 'MDWE without inheritance' - Kefeng Wang has provided the series 'mm: convert numa balancing functions to use a folio' which does what it says - In the series 'mm/ksm: add fork-exec support for prctl' Stefan Roesch makes is possible for a process to propagate KSM treatment across exec() - Huang Ying has enhanced memory tiering's calculation of memory distances. This is used to permit the dax/kmem driver to use 'high bandwidth memory' in addition to Optane Data Center Persistent Memory Modules (DCPMM). The series is named 'memory tiering: calculate abstract distance based on ACPI HMAT' - In the series 'Smart scanning mode for KSM' Stefan Roesch has optimized KSM by teaching it to retain and use some historical information from previous scans - Yosry Ahmed has fixed some inconsistencies in memcg statistics in the series 'mm: memcg: fix tracking of pending stats updates values' - In the series 'Implement IOCTL to get and optionally clear info about PTEs' Peter Xu has added an ioctl to /proc/<pid>/pagemap which permits us to atomically read-then-clear page softdirty state. This is mainly used by CRIU - Hugh Dickins contributed the series 'shmem,tmpfs: general maintenance', a bunch of relatively minor maintenance tweaks to this code - Matthew Wilcox has increased the use of the VMA lock over file-backed page faults in the series 'Handle more faults under the VMA lock'. Some rationalizations of the fault path became possible as a result - In the series 'mm/rmap: convert page_move_anon_rmap() to folio_move_anon_rmap()' David Hildenbrand has implemented some cleanups and folio conversions - In the series 'various improvements to the GUP interface' Lorenzo Stoakes has simplified and improved the GUP interface with an eye to providing groundwork for future improvements - Andrey Konovalov has sent along the series 'kasan: assorted fixes and improvements' which does those things - Some page allocator maintenance work from Kemeng Shi in the series 'Two minor cleanups to break_down_buddy_pages' - In thes series 'New selftest for mm' Breno Leitao has developed another MM self test which tickles a race we had between madvise() and page faults - In the series 'Add folio_end_read' Matthew Wilcox provides cleanups and an optimization to the core pagecache code - Nhat Pham has added memcg accounting for hugetlb memory in the series 'hugetlb memcg accounting' - Cleanups and rationalizations to the pagemap code from Lorenzo Stoakes, in the series 'Abstract vma_merge() and split_vma()' - Audra Mitchell has fixed issues in the procfs page_owner code's new timestamping feature which was causing some misbehaviours. In the series 'Fix page_owner's use of free timestamps' - Lorenzo Stoakes has fixed the handling of new mappings of sealed files in the series 'permit write-sealed memfd read-only shared mappings' - Mike Kravetz has optimized the hugetlb vmemmap optimization in the series 'Batch hugetlb vmemmap modification operations' - Some buffer_head folio conversions and cleanups from Matthew Wilcox in the series 'Finish the create_empty_buffers() transition' - As a page allocator performance optimization Huang Ying has added automatic tuning to the allocator's per-cpu-pages feature, in the series 'mm: PCP high auto-tuning' - Roman Gushchin has contributed the patchset 'mm: improve performance of accounted kernel memory allocations' which improves their performance by ~30% as measured by a micro-benchmark - folio conversions from Kefeng Wang in the series 'mm: convert page cpupid functions to folios' - Some kmemleak fixups in Liu Shixin's series 'Some bugfix about kmemleak' - Qi Zheng has improved our handling of memoryless nodes by keeping them off the allocation fallback list. This is done in the series 'handle memoryless nodes more appropriately' - khugepaged conversions from Vishal Moola in the series 'Some khugepaged folio conversions'" [ bcachefs conflicts with the dynamically allocated shrinkers have been resolved as per Stephen Rothwell in https://lore.kernel.org/all/20230913093553.4290421e@canb.auug.org.au/ with help from Qi Zheng. The clone3 test filtering conflict was half-arsed by yours truly ] * tag 'mm-stable-2023-11-01-14-33' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (406 commits) mm/damon/sysfs: update monitoring target regions for online input commit mm/damon/sysfs: remove requested targets when online-commit inputs selftests: add a sanity check for zswap Documentation: maple_tree: fix word spelling error mm/vmalloc: fix the unchecked dereference warning in vread_iter() zswap: export compression failure stats Documentation: ubsan: drop "the" from article title mempolicy: migration attempt to match interleave nodes mempolicy: mmap_lock is not needed while migrating folios mempolicy: alloc_pages_mpol() for NUMA policy without vma mm: add page_rmappable_folio() wrapper mempolicy: remove confusing MPOL_MF_LAZY dead code mempolicy: mpol_shared_policy_init() without pseudo-vma mempolicy trivia: use pgoff_t in shared mempolicy tree mempolicy trivia: slightly more consistent naming mempolicy trivia: delete those ancient pr_debug()s mempolicy: fix migrate_pages(2) syscall return nr_failed kernfs: drop shared NUMA mempolicy hooks hugetlbfs: drop shared NUMA mempolicy pretence mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets() ...
223 lines
6.3 KiB
C
223 lines
6.3 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
#include <linux/efi.h>
|
|
#include <linux/memblock.h>
|
|
#include <linux/spinlock.h>
|
|
#include <linux/crash_dump.h>
|
|
#include <asm/unaccepted_memory.h>
|
|
|
|
/* Protects unaccepted memory bitmap and accepting_list */
|
|
static DEFINE_SPINLOCK(unaccepted_memory_lock);
|
|
|
|
struct accept_range {
|
|
struct list_head list;
|
|
unsigned long start;
|
|
unsigned long end;
|
|
};
|
|
|
|
static LIST_HEAD(accepting_list);
|
|
|
|
/*
|
|
* accept_memory() -- Consult bitmap and accept the memory if needed.
|
|
*
|
|
* Only memory that is explicitly marked as unaccepted in the bitmap requires
|
|
* an action. All the remaining memory is implicitly accepted and doesn't need
|
|
* acceptance.
|
|
*
|
|
* No need to accept:
|
|
* - anything if the system has no unaccepted table;
|
|
* - memory that is below phys_base;
|
|
* - memory that is above the memory that addressable by the bitmap;
|
|
*/
|
|
void accept_memory(phys_addr_t start, phys_addr_t end)
|
|
{
|
|
struct efi_unaccepted_memory *unaccepted;
|
|
unsigned long range_start, range_end;
|
|
struct accept_range range, *entry;
|
|
unsigned long flags;
|
|
u64 unit_size;
|
|
|
|
unaccepted = efi_get_unaccepted_table();
|
|
if (!unaccepted)
|
|
return;
|
|
|
|
unit_size = unaccepted->unit_size;
|
|
|
|
/*
|
|
* Only care for the part of the range that is represented
|
|
* in the bitmap.
|
|
*/
|
|
if (start < unaccepted->phys_base)
|
|
start = unaccepted->phys_base;
|
|
if (end < unaccepted->phys_base)
|
|
return;
|
|
|
|
/* Translate to offsets from the beginning of the bitmap */
|
|
start -= unaccepted->phys_base;
|
|
end -= unaccepted->phys_base;
|
|
|
|
/*
|
|
* load_unaligned_zeropad() can lead to unwanted loads across page
|
|
* boundaries. The unwanted loads are typically harmless. But, they
|
|
* might be made to totally unrelated or even unmapped memory.
|
|
* load_unaligned_zeropad() relies on exception fixup (#PF, #GP and now
|
|
* #VE) to recover from these unwanted loads.
|
|
*
|
|
* But, this approach does not work for unaccepted memory. For TDX, a
|
|
* load from unaccepted memory will not lead to a recoverable exception
|
|
* within the guest. The guest will exit to the VMM where the only
|
|
* recourse is to terminate the guest.
|
|
*
|
|
* There are two parts to fix this issue and comprehensively avoid
|
|
* access to unaccepted memory. Together these ensure that an extra
|
|
* "guard" page is accepted in addition to the memory that needs to be
|
|
* used:
|
|
*
|
|
* 1. Implicitly extend the range_contains_unaccepted_memory(start, end)
|
|
* checks up to end+unit_size if 'end' is aligned on a unit_size
|
|
* boundary.
|
|
*
|
|
* 2. Implicitly extend accept_memory(start, end) to end+unit_size if
|
|
* 'end' is aligned on a unit_size boundary. (immediately following
|
|
* this comment)
|
|
*/
|
|
if (!(end % unit_size))
|
|
end += unit_size;
|
|
|
|
/* Make sure not to overrun the bitmap */
|
|
if (end > unaccepted->size * unit_size * BITS_PER_BYTE)
|
|
end = unaccepted->size * unit_size * BITS_PER_BYTE;
|
|
|
|
range.start = start / unit_size;
|
|
range.end = DIV_ROUND_UP(end, unit_size);
|
|
retry:
|
|
spin_lock_irqsave(&unaccepted_memory_lock, flags);
|
|
|
|
/*
|
|
* Check if anybody works on accepting the same range of the memory.
|
|
*
|
|
* The check is done with unit_size granularity. It is crucial to catch
|
|
* all accept requests to the same unit_size block, even if they don't
|
|
* overlap on physical address level.
|
|
*/
|
|
list_for_each_entry(entry, &accepting_list, list) {
|
|
if (entry->end < range.start)
|
|
continue;
|
|
if (entry->start >= range.end)
|
|
continue;
|
|
|
|
/*
|
|
* Somebody else accepting the range. Or at least part of it.
|
|
*
|
|
* Drop the lock and retry until it is complete.
|
|
*/
|
|
spin_unlock_irqrestore(&unaccepted_memory_lock, flags);
|
|
goto retry;
|
|
}
|
|
|
|
/*
|
|
* Register that the range is about to be accepted.
|
|
* Make sure nobody else will accept it.
|
|
*/
|
|
list_add(&range.list, &accepting_list);
|
|
|
|
range_start = range.start;
|
|
for_each_set_bitrange_from(range_start, range_end, unaccepted->bitmap,
|
|
range.end) {
|
|
unsigned long phys_start, phys_end;
|
|
unsigned long len = range_end - range_start;
|
|
|
|
phys_start = range_start * unit_size + unaccepted->phys_base;
|
|
phys_end = range_end * unit_size + unaccepted->phys_base;
|
|
|
|
/*
|
|
* Keep interrupts disabled until the accept operation is
|
|
* complete in order to prevent deadlocks.
|
|
*
|
|
* Enabling interrupts before calling arch_accept_memory()
|
|
* creates an opportunity for an interrupt handler to request
|
|
* acceptance for the same memory. The handler will continuously
|
|
* spin with interrupts disabled, preventing other task from
|
|
* making progress with the acceptance process.
|
|
*/
|
|
spin_unlock(&unaccepted_memory_lock);
|
|
|
|
arch_accept_memory(phys_start, phys_end);
|
|
|
|
spin_lock(&unaccepted_memory_lock);
|
|
bitmap_clear(unaccepted->bitmap, range_start, len);
|
|
}
|
|
|
|
list_del(&range.list);
|
|
spin_unlock_irqrestore(&unaccepted_memory_lock, flags);
|
|
}
|
|
|
|
bool range_contains_unaccepted_memory(phys_addr_t start, phys_addr_t end)
|
|
{
|
|
struct efi_unaccepted_memory *unaccepted;
|
|
unsigned long flags;
|
|
bool ret = false;
|
|
u64 unit_size;
|
|
|
|
unaccepted = efi_get_unaccepted_table();
|
|
if (!unaccepted)
|
|
return false;
|
|
|
|
unit_size = unaccepted->unit_size;
|
|
|
|
/*
|
|
* Only care for the part of the range that is represented
|
|
* in the bitmap.
|
|
*/
|
|
if (start < unaccepted->phys_base)
|
|
start = unaccepted->phys_base;
|
|
if (end < unaccepted->phys_base)
|
|
return false;
|
|
|
|
/* Translate to offsets from the beginning of the bitmap */
|
|
start -= unaccepted->phys_base;
|
|
end -= unaccepted->phys_base;
|
|
|
|
/*
|
|
* Also consider the unaccepted state of the *next* page. See fix #1 in
|
|
* the comment on load_unaligned_zeropad() in accept_memory().
|
|
*/
|
|
if (!(end % unit_size))
|
|
end += unit_size;
|
|
|
|
/* Make sure not to overrun the bitmap */
|
|
if (end > unaccepted->size * unit_size * BITS_PER_BYTE)
|
|
end = unaccepted->size * unit_size * BITS_PER_BYTE;
|
|
|
|
spin_lock_irqsave(&unaccepted_memory_lock, flags);
|
|
while (start < end) {
|
|
if (test_bit(start / unit_size, unaccepted->bitmap)) {
|
|
ret = true;
|
|
break;
|
|
}
|
|
|
|
start += unit_size;
|
|
}
|
|
spin_unlock_irqrestore(&unaccepted_memory_lock, flags);
|
|
|
|
return ret;
|
|
}
|
|
|
|
#ifdef CONFIG_PROC_VMCORE
|
|
static bool unaccepted_memory_vmcore_pfn_is_ram(struct vmcore_cb *cb,
|
|
unsigned long pfn)
|
|
{
|
|
return !pfn_is_unaccepted_memory(pfn);
|
|
}
|
|
|
|
static struct vmcore_cb vmcore_cb = {
|
|
.pfn_is_ram = unaccepted_memory_vmcore_pfn_is_ram,
|
|
};
|
|
|
|
static int __init unaccepted_memory_init_kdump(void)
|
|
{
|
|
register_vmcore_cb(&vmcore_cb);
|
|
return 0;
|
|
}
|
|
core_initcall(unaccepted_memory_init_kdump);
|
|
#endif /* CONFIG_PROC_VMCORE */
|