forked from Minki/linux
072bb0aa5e
When a user or administrator requires swap for their application, they create a swap partition and file, format it with mkswap and activate it with swapon. Swap over the network is considered as an option in diskless systems. The two likely scenarios are when blade servers are used as part of a cluster where the form factor or maintenance costs do not allow the use of disks and thin clients. The Linux Terminal Server Project recommends the use of the Network Block Device (NBD) for swap according to the manual at https://sourceforge.net/projects/ltsp/files/Docs-Admin-Guide/LTSPManual.pdf/download There is also documentation and tutorials on how to setup swap over NBD at places like https://help.ubuntu.com/community/UbuntuLTSP/EnableNBDSWAP The nbd-client also documents the use of NBD as swap. Despite this, the fact is that a machine using NBD for swap can deadlock within minutes if swap is used intensively. This patch series addresses the problem. The core issue is that network block devices do not use mempools like normal block devices do. As the host cannot control where they receive packets from, they cannot reliably work out in advance how much memory they might need. Some years ago, Peter Zijlstra developed a series of patches that supported swap over an NFS that at least one distribution is carrying within their kernels. This patch series borrows very heavily from Peter's work to support swapping over NBD as a pre-requisite to supporting swap-over-NFS. The bulk of the complexity is concerned with preserving memory that is allocated from the PFMEMALLOC reserves for use by the network layer which is needed for both NBD and NFS. Patch 1 adds knowledge of the PFMEMALLOC reserves to SLAB and SLUB to preserve access to pages allocated under low memory situations to callers that are freeing memory. Patch 2 optimises the SLUB fast path to avoid pfmemalloc checks Patch 3 introduces __GFP_MEMALLOC to allow access to the PFMEMALLOC reserves without setting PFMEMALLOC. Patch 4 opens the possibility for softirqs to use PFMEMALLOC reserves for later use by network packet processing. Patch 5 only sets page->pfmemalloc when ALLOC_NO_WATERMARKS was required Patch 6 ignores memory policies when ALLOC_NO_WATERMARKS is set. Patches 7-12 allows network processing to use PFMEMALLOC reserves when the socket has been marked as being used by the VM to clean pages. If packets are received and stored in pages that were allocated under low-memory situations and are unrelated to the VM, the packets are dropped. Patch 11 reintroduces __skb_alloc_page which the networking folk may object to but is needed in some cases to propogate pfmemalloc from a newly allocated page to an skb. If there is a strong objection, this patch can be dropped with the impact being that swap-over-network will be slower in some cases but it should not fail. Patch 13 is a micro-optimisation to avoid a function call in the common case. Patch 14 tags NBD sockets as being SOCK_MEMALLOC so they can use PFMEMALLOC if necessary. Patch 15 notes that it is still possible for the PFMEMALLOC reserve to be depleted. To prevent this, direct reclaimers get throttled on a waitqueue if 50% of the PFMEMALLOC reserves are depleted. It is expected that kswapd and the direct reclaimers already running will clean enough pages for the low watermark to be reached and the throttled processes are woken up. Patch 16 adds a statistic to track how often processes get throttled Some basic performance testing was run using kernel builds, netperf on loopback for UDP and TCP, hackbench (pipes and sockets), iozone and sysbench. Each of them were expected to use the sl*b allocators reasonably heavily but there did not appear to be significant performance variances. For testing swap-over-NBD, a machine was booted with 2G of RAM with a swapfile backed by NBD. 8*NUM_CPU processes were started that create anonymous memory mappings and read them linearly in a loop. The total size of the mappings were 4*PHYSICAL_MEMORY to use swap heavily under memory pressure. Without the patches and using SLUB, the machine locks up within minutes and runs to completion with them applied. With SLAB, the story is different as an unpatched kernel run to completion. However, the patched kernel completed the test 45% faster. MICRO 3.5.0-rc2 3.5.0-rc2 vanilla swapnbd Unrecognised test vmscan-anon-mmap-write MMTests Statistics: duration Sys Time Running Test (seconds) 197.80 173.07 User+Sys Time Running Test (seconds) 206.96 182.03 Total Elapsed Time (seconds) 3240.70 1762.09 This patch: mm: sl[au]b: add knowledge of PFMEMALLOC reserve pages Allocations of pages below the min watermark run a risk of the machine hanging due to a lack of memory. To prevent this, only callers who have PF_MEMALLOC or TIF_MEMDIE set and are not processing an interrupt are allowed to allocate with ALLOC_NO_WATERMARKS. Once they are allocated to a slab though, nothing prevents other callers consuming free objects within those slabs. This patch limits access to slab pages that were alloced from the PFMEMALLOC reserves. When this patch is applied, pages allocated from below the low watermark are returned with page->pfmemalloc set and it is up to the caller to determine how the page should be protected. SLAB restricts access to any page with page->pfmemalloc set to callers which are known to able to access the PFMEMALLOC reserve. If one is not available, an attempt is made to allocate a new page rather than use a reserve. SLUB is a bit more relaxed in that it only records if the current per-CPU page was allocated from PFMEMALLOC reserve and uses another partial slab if the caller does not have the necessary GFP or process flags. This was found to be sufficient in tests to avoid hangs due to SLUB generally maintaining smaller lists than SLAB. In low-memory conditions it does mean that !PFMEMALLOC allocators can fail a slab allocation even though free objects are available because they are being preserved for callers that are freeing pages. [a.p.zijlstra@chello.nl: Original implementation] [sebastian@breakpoint.cc: Correct order of page flag clearing] Signed-off-by: Mel Gorman <mgorman@suse.de> Cc: David Miller <davem@davemloft.net> Cc: Neil Brown <neilb@suse.de> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Mike Christie <michaelc@cs.wisc.edu> Cc: Eric B Munson <emunson@mgebm.net> Cc: Eric Dumazet <eric.dumazet@gmail.com> Cc: Sebastian Andrzej Siewior <sebastian@breakpoint.cc> Cc: Mel Gorman <mgorman@suse.de> Cc: Christoph Lameter <cl@linux.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
361 lines
10 KiB
C
361 lines
10 KiB
C
/* internal.h: mm/ internal definitions
|
|
*
|
|
* Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version
|
|
* 2 of the License, or (at your option) any later version.
|
|
*/
|
|
#ifndef __MM_INTERNAL_H
|
|
#define __MM_INTERNAL_H
|
|
|
|
#include <linux/mm.h>
|
|
|
|
void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
|
|
unsigned long floor, unsigned long ceiling);
|
|
|
|
static inline void set_page_count(struct page *page, int v)
|
|
{
|
|
atomic_set(&page->_count, v);
|
|
}
|
|
|
|
/*
|
|
* Turn a non-refcounted page (->_count == 0) into refcounted with
|
|
* a count of one.
|
|
*/
|
|
static inline void set_page_refcounted(struct page *page)
|
|
{
|
|
VM_BUG_ON(PageTail(page));
|
|
VM_BUG_ON(atomic_read(&page->_count));
|
|
set_page_count(page, 1);
|
|
}
|
|
|
|
static inline void __put_page(struct page *page)
|
|
{
|
|
atomic_dec(&page->_count);
|
|
}
|
|
|
|
static inline void __get_page_tail_foll(struct page *page,
|
|
bool get_page_head)
|
|
{
|
|
/*
|
|
* If we're getting a tail page, the elevated page->_count is
|
|
* required only in the head page and we will elevate the head
|
|
* page->_count and tail page->_mapcount.
|
|
*
|
|
* We elevate page_tail->_mapcount for tail pages to force
|
|
* page_tail->_count to be zero at all times to avoid getting
|
|
* false positives from get_page_unless_zero() with
|
|
* speculative page access (like in
|
|
* page_cache_get_speculative()) on tail pages.
|
|
*/
|
|
VM_BUG_ON(atomic_read(&page->first_page->_count) <= 0);
|
|
VM_BUG_ON(atomic_read(&page->_count) != 0);
|
|
VM_BUG_ON(page_mapcount(page) < 0);
|
|
if (get_page_head)
|
|
atomic_inc(&page->first_page->_count);
|
|
atomic_inc(&page->_mapcount);
|
|
}
|
|
|
|
/*
|
|
* This is meant to be called as the FOLL_GET operation of
|
|
* follow_page() and it must be called while holding the proper PT
|
|
* lock while the pte (or pmd_trans_huge) is still mapping the page.
|
|
*/
|
|
static inline void get_page_foll(struct page *page)
|
|
{
|
|
if (unlikely(PageTail(page)))
|
|
/*
|
|
* This is safe only because
|
|
* __split_huge_page_refcount() can't run under
|
|
* get_page_foll() because we hold the proper PT lock.
|
|
*/
|
|
__get_page_tail_foll(page, true);
|
|
else {
|
|
/*
|
|
* Getting a normal page or the head of a compound page
|
|
* requires to already have an elevated page->_count.
|
|
*/
|
|
VM_BUG_ON(atomic_read(&page->_count) <= 0);
|
|
atomic_inc(&page->_count);
|
|
}
|
|
}
|
|
|
|
extern unsigned long highest_memmap_pfn;
|
|
|
|
/*
|
|
* in mm/vmscan.c:
|
|
*/
|
|
extern int isolate_lru_page(struct page *page);
|
|
extern void putback_lru_page(struct page *page);
|
|
|
|
/*
|
|
* in mm/page_alloc.c
|
|
*/
|
|
extern void __free_pages_bootmem(struct page *page, unsigned int order);
|
|
extern void prep_compound_page(struct page *page, unsigned long order);
|
|
#ifdef CONFIG_MEMORY_FAILURE
|
|
extern bool is_free_buddy_page(struct page *page);
|
|
#endif
|
|
|
|
#if defined CONFIG_COMPACTION || defined CONFIG_CMA
|
|
|
|
/*
|
|
* in mm/compaction.c
|
|
*/
|
|
/*
|
|
* compact_control is used to track pages being migrated and the free pages
|
|
* they are being migrated to during memory compaction. The free_pfn starts
|
|
* at the end of a zone and migrate_pfn begins at the start. Movable pages
|
|
* are moved to the end of a zone during a compaction run and the run
|
|
* completes when free_pfn <= migrate_pfn
|
|
*/
|
|
struct compact_control {
|
|
struct list_head freepages; /* List of free pages to migrate to */
|
|
struct list_head migratepages; /* List of pages being migrated */
|
|
unsigned long nr_freepages; /* Number of isolated free pages */
|
|
unsigned long nr_migratepages; /* Number of pages to migrate */
|
|
unsigned long free_pfn; /* isolate_freepages search base */
|
|
unsigned long start_free_pfn; /* where we started the search */
|
|
unsigned long migrate_pfn; /* isolate_migratepages search base */
|
|
bool sync; /* Synchronous migration */
|
|
bool wrapped; /* Order > 0 compactions are
|
|
incremental, once free_pfn
|
|
and migrate_pfn meet, we restart
|
|
from the top of the zone;
|
|
remember we wrapped around. */
|
|
|
|
int order; /* order a direct compactor needs */
|
|
int migratetype; /* MOVABLE, RECLAIMABLE etc */
|
|
struct zone *zone;
|
|
};
|
|
|
|
unsigned long
|
|
isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn);
|
|
unsigned long
|
|
isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
|
|
unsigned long low_pfn, unsigned long end_pfn);
|
|
|
|
#endif
|
|
|
|
/*
|
|
* function for dealing with page's order in buddy system.
|
|
* zone->lock is already acquired when we use these.
|
|
* So, we don't need atomic page->flags operations here.
|
|
*/
|
|
static inline unsigned long page_order(struct page *page)
|
|
{
|
|
/* PageBuddy() must be checked by the caller */
|
|
return page_private(page);
|
|
}
|
|
|
|
/* mm/util.c */
|
|
void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
|
|
struct vm_area_struct *prev, struct rb_node *rb_parent);
|
|
|
|
#ifdef CONFIG_MMU
|
|
extern long mlock_vma_pages_range(struct vm_area_struct *vma,
|
|
unsigned long start, unsigned long end);
|
|
extern void munlock_vma_pages_range(struct vm_area_struct *vma,
|
|
unsigned long start, unsigned long end);
|
|
static inline void munlock_vma_pages_all(struct vm_area_struct *vma)
|
|
{
|
|
munlock_vma_pages_range(vma, vma->vm_start, vma->vm_end);
|
|
}
|
|
|
|
/*
|
|
* Called only in fault path via page_evictable() for a new page
|
|
* to determine if it's being mapped into a LOCKED vma.
|
|
* If so, mark page as mlocked.
|
|
*/
|
|
static inline int mlocked_vma_newpage(struct vm_area_struct *vma,
|
|
struct page *page)
|
|
{
|
|
VM_BUG_ON(PageLRU(page));
|
|
|
|
if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
|
|
return 0;
|
|
|
|
if (!TestSetPageMlocked(page)) {
|
|
inc_zone_page_state(page, NR_MLOCK);
|
|
count_vm_event(UNEVICTABLE_PGMLOCKED);
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
/*
|
|
* must be called with vma's mmap_sem held for read or write, and page locked.
|
|
*/
|
|
extern void mlock_vma_page(struct page *page);
|
|
extern void munlock_vma_page(struct page *page);
|
|
|
|
/*
|
|
* Clear the page's PageMlocked(). This can be useful in a situation where
|
|
* we want to unconditionally remove a page from the pagecache -- e.g.,
|
|
* on truncation or freeing.
|
|
*
|
|
* It is legal to call this function for any page, mlocked or not.
|
|
* If called for a page that is still mapped by mlocked vmas, all we do
|
|
* is revert to lazy LRU behaviour -- semantics are not broken.
|
|
*/
|
|
extern void __clear_page_mlock(struct page *page);
|
|
static inline void clear_page_mlock(struct page *page)
|
|
{
|
|
if (unlikely(TestClearPageMlocked(page)))
|
|
__clear_page_mlock(page);
|
|
}
|
|
|
|
/*
|
|
* mlock_migrate_page - called only from migrate_page_copy() to
|
|
* migrate the Mlocked page flag; update statistics.
|
|
*/
|
|
static inline void mlock_migrate_page(struct page *newpage, struct page *page)
|
|
{
|
|
if (TestClearPageMlocked(page)) {
|
|
unsigned long flags;
|
|
|
|
local_irq_save(flags);
|
|
__dec_zone_page_state(page, NR_MLOCK);
|
|
SetPageMlocked(newpage);
|
|
__inc_zone_page_state(newpage, NR_MLOCK);
|
|
local_irq_restore(flags);
|
|
}
|
|
}
|
|
|
|
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
|
|
extern unsigned long vma_address(struct page *page,
|
|
struct vm_area_struct *vma);
|
|
#endif
|
|
#else /* !CONFIG_MMU */
|
|
static inline int mlocked_vma_newpage(struct vm_area_struct *v, struct page *p)
|
|
{
|
|
return 0;
|
|
}
|
|
static inline void clear_page_mlock(struct page *page) { }
|
|
static inline void mlock_vma_page(struct page *page) { }
|
|
static inline void mlock_migrate_page(struct page *new, struct page *old) { }
|
|
|
|
#endif /* !CONFIG_MMU */
|
|
|
|
/*
|
|
* Return the mem_map entry representing the 'offset' subpage within
|
|
* the maximally aligned gigantic page 'base'. Handle any discontiguity
|
|
* in the mem_map at MAX_ORDER_NR_PAGES boundaries.
|
|
*/
|
|
static inline struct page *mem_map_offset(struct page *base, int offset)
|
|
{
|
|
if (unlikely(offset >= MAX_ORDER_NR_PAGES))
|
|
return pfn_to_page(page_to_pfn(base) + offset);
|
|
return base + offset;
|
|
}
|
|
|
|
/*
|
|
* Iterator over all subpages within the maximally aligned gigantic
|
|
* page 'base'. Handle any discontiguity in the mem_map.
|
|
*/
|
|
static inline struct page *mem_map_next(struct page *iter,
|
|
struct page *base, int offset)
|
|
{
|
|
if (unlikely((offset & (MAX_ORDER_NR_PAGES - 1)) == 0)) {
|
|
unsigned long pfn = page_to_pfn(base) + offset;
|
|
if (!pfn_valid(pfn))
|
|
return NULL;
|
|
return pfn_to_page(pfn);
|
|
}
|
|
return iter + 1;
|
|
}
|
|
|
|
/*
|
|
* FLATMEM and DISCONTIGMEM configurations use alloc_bootmem_node,
|
|
* so all functions starting at paging_init should be marked __init
|
|
* in those cases. SPARSEMEM, however, allows for memory hotplug,
|
|
* and alloc_bootmem_node is not used.
|
|
*/
|
|
#ifdef CONFIG_SPARSEMEM
|
|
#define __paginginit __meminit
|
|
#else
|
|
#define __paginginit __init
|
|
#endif
|
|
|
|
/* Returns true if the gfp_mask allows use of ALLOC_NO_WATERMARK */
|
|
bool gfp_pfmemalloc_allowed(gfp_t gfp_mask);
|
|
|
|
/* Memory initialisation debug and verification */
|
|
enum mminit_level {
|
|
MMINIT_WARNING,
|
|
MMINIT_VERIFY,
|
|
MMINIT_TRACE
|
|
};
|
|
|
|
#ifdef CONFIG_DEBUG_MEMORY_INIT
|
|
|
|
extern int mminit_loglevel;
|
|
|
|
#define mminit_dprintk(level, prefix, fmt, arg...) \
|
|
do { \
|
|
if (level < mminit_loglevel) { \
|
|
printk(level <= MMINIT_WARNING ? KERN_WARNING : KERN_DEBUG); \
|
|
printk(KERN_CONT "mminit::" prefix " " fmt, ##arg); \
|
|
} \
|
|
} while (0)
|
|
|
|
extern void mminit_verify_pageflags_layout(void);
|
|
extern void mminit_verify_page_links(struct page *page,
|
|
enum zone_type zone, unsigned long nid, unsigned long pfn);
|
|
extern void mminit_verify_zonelist(void);
|
|
|
|
#else
|
|
|
|
static inline void mminit_dprintk(enum mminit_level level,
|
|
const char *prefix, const char *fmt, ...)
|
|
{
|
|
}
|
|
|
|
static inline void mminit_verify_pageflags_layout(void)
|
|
{
|
|
}
|
|
|
|
static inline void mminit_verify_page_links(struct page *page,
|
|
enum zone_type zone, unsigned long nid, unsigned long pfn)
|
|
{
|
|
}
|
|
|
|
static inline void mminit_verify_zonelist(void)
|
|
{
|
|
}
|
|
#endif /* CONFIG_DEBUG_MEMORY_INIT */
|
|
|
|
/* mminit_validate_memmodel_limits is independent of CONFIG_DEBUG_MEMORY_INIT */
|
|
#if defined(CONFIG_SPARSEMEM)
|
|
extern void mminit_validate_memmodel_limits(unsigned long *start_pfn,
|
|
unsigned long *end_pfn);
|
|
#else
|
|
static inline void mminit_validate_memmodel_limits(unsigned long *start_pfn,
|
|
unsigned long *end_pfn)
|
|
{
|
|
}
|
|
#endif /* CONFIG_SPARSEMEM */
|
|
|
|
#define ZONE_RECLAIM_NOSCAN -2
|
|
#define ZONE_RECLAIM_FULL -1
|
|
#define ZONE_RECLAIM_SOME 0
|
|
#define ZONE_RECLAIM_SUCCESS 1
|
|
#endif
|
|
|
|
extern int hwpoison_filter(struct page *p);
|
|
|
|
extern u32 hwpoison_filter_dev_major;
|
|
extern u32 hwpoison_filter_dev_minor;
|
|
extern u64 hwpoison_filter_flags_mask;
|
|
extern u64 hwpoison_filter_flags_value;
|
|
extern u64 hwpoison_filter_memcg;
|
|
extern u32 hwpoison_filter_enable;
|
|
|
|
extern unsigned long vm_mmap_pgoff(struct file *, unsigned long,
|
|
unsigned long, unsigned long,
|
|
unsigned long, unsigned long);
|
|
|
|
extern void set_pageblock_order(void);
|