Per cgroup swap file support
Subsystems (10)
cover Per cgroup swap file support
This series of patches adds support to configure a cgroup to swap to a particular file by using control file memory.swapfile.
A value of "default" in memory.swapfile indicates that this cgroup should use the default, system-wide, swap files. A value of "none" indicates that this cgroup should never swap. Other values are interpreted as the path to a private swap file that can only be used by the owner (and its children).
The swap file has to be created and swapon() has to be done on it with SWAP_FLAG_PRIVATE, before it can be used. This flag ensures that the swap file is private and does not get used by others.
Jamie Liu (1):
swap: do not store private swap files on swap_list
Suleiman Souhlal (2):
mm/swap: support per memory cgroup swapfiles
swap: Increase the maximum number of swap files to 8192.
Documentation/cgroups/memory.txt | 15 ++ arch/x86/include/asm/pgtable_64.h | 63 ++++++-- include/linux/memcontrol.h | 2 + include/linux/swap.h | 45 +++--- mm/memcontrol.c | 76 ++++++++++ mm/memory.c | 3 +- mm/shmem.c | 2 +- mm/swap_state.c | 2 +- mm/swapfile.c | 307 +++++++++++++++++++++++++++++++------- mm/vmscan.c | 2 +- 10 files changed, 423 insertions(+), 94 deletions(-)
Signature · 1 line
1.9.1.423.g4596e3a
PATCH 1/3 mm/swap: support per memory cgroup swapfiles
From: Suleiman Souhlal <[email protected]>
This patch adds support for per memory cgroup swap file. The swap file is marked private in swapon() with a new flag SWAP_FLAG_PRIVATE becasue only the memory cgroup (and its children) that owns it can use it (in the case of the children that don't own any swap files, they go up the hierarchy until someone who has swap file set up is found).
The path of the swap file is set by writing to memory.swapfile. Details of the API can be found in Documentation/cgroups/memory.txt.
--- Documentation/cgroups/memory.txt | 15 +++ include/linux/memcontrol.h | 2 + include/linux/swap.h | 38 +++--- mm/memcontrol.c | 76 ++++++++++++ mm/memory.c | 3 +- mm/shmem.c | 2 +- mm/swap_state.c | 2 +- mm/swapfile.c | 241 ++++++++++++++++++++++++++++++++++----- mm/vmscan.c | 2 +- 9 files changed, 331 insertions(+), 50 deletions(-)
Documentation/cgroups/memory.txt +15 -0
@@ -72,6 +72,7 @@ Brief summary of control files. memory.move_charge_at_immigrate # set/show controls of moving charges memory.oom_control # set/show oom controls. memory.numa_stat # show the number of memory usage per numa node + memory.swapfile # set/show swap file memory.kmem.limit_in_bytes # set/show hard limit for kernel memory memory.kmem.usage_in_bytes # show current kernel memory allocation
@@ -342,6 +343,20 @@ set: admin a unified view of memory, and it is also useful for people who just want to track kernel memory usage. +2.8 Private swap files + +It's possible to configure a cgroup to swap to a particular file by using +memory.swapfile. + +A value of "default" in memory.swapfile indicates that this cgroup should +use the default, system-wide, swap files. A value of "none" indicates that +this cgroup should never swap. Other values are interpreted as the path +to a private swap file. + +The swap file has to be created and swapon() has to be done on it with +SWAP_FLAG_PRIVATE, before it can be used. This flag ensures that the swap +file is private and does not get used by others. + 3. User Interface 0. Configuration
include/linux/memcontrol.h +2 -0
@@ -155,6 +155,8 @@ static inline bool task_in_memcg_oom(struct task_struct *p) }bool mem_cgroup_oom_synchronize (bool wait); +int mem_cgroup_get_page_swap_type (struct page *page); +void mem_cgroup_remove_swapfile (int type);#ifdef CONFIG_MEMCG_SWAP extern int do_swap_account;
include/linux/swap.h +22 -16
@@ -23,10 +23,11 @@ struct bio;#define SWAP_FLAG_DISCARD 0x10000/* enable discard for swap */ #define SWAP_FLAG_DISCARD_ONCE 0x20000/* discard swap area at swapon-time */ #define SWAP_FLAG_DISCARD_PAGES 0x40000/* discard page-clusters after use */ +#define SWAP_FLAG_PRIVATE 0x1000000/* set if get_swap_page should skip */ #define SWAP_FLAGS_VALID (SWAP_FLAG_PRIO_MASK | SWAP_FLAG_PREFER | \ SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | \ - SWAP_FLAG_DISCARD_PAGES) + SWAP_FLAG_DISCARD_PAGES | SWAP_FLAG_PRIVATE)static inline int current_is_kswapd (void ) {
@@ -158,8 +159,14 @@ enum {SWP_FILE = (1 <<7 ),/* set after swap_activate success */ SWP_AREA_DISCARD = (1 <<8 ),/* single-time swap area discards */ SWP_PAGE_DISCARD = (1 <<9 ),/* freed swap page-cluster discards */ +SWP_PRIVATE = (1 <<10 ),/* not for general use */ /* add others here before... */ - SWP_SCANNING = (1 << 10), /* refcount in scan_swap_map */ + SWP_SCANNING = (1 << 11), /* refcount in scan_swap_map */ +}; + +enum { +SWAP_TYPE_DEFAULT =-1 ,/* use default/global/system swap file */ +SWAP_TYPE_NONE =-2 ,/* swap is disabled */ };#define SWAP_CLUSTER_MAX 32UL
@@ -401,22 +408,19 @@ extern struct page *swapin_readahead(swp_entry_t, gfp_t, struct vm_area_struct *vma, unsigned long addr);/* linux/mm/swapfile.c */ -extern atomic_long_t nr_swap_pages; -extern long total_swap_pages; - -/* Swap 50% full? Release swapcache more aggressively.. */ -static inline bool vm_swap_full (void ) -{ -return atomic_long_read (&nr_swap_pages) *2 < total_swap_pages; -} - +extern bool vm_swap_full (struct page *page); +extern atomic_long_t nr_public_swap_pages, nr_private_swap_pages;static inline long get_nr_swap_pages (void ) { -return atomic_long_read (&nr_swap_pages); +return atomic_long_read (&nr_public_swap_pages) + +atomic_long_read (&nr_private_swap_pages); } - +extern long total_public_swap_pages, total_private_swap_pages; +#define total_swap_pages (total_public_swap_pages + total_private_swap_pages)extern void si_swapinfo (struct sysinfo *); -extern swp_entry_t get_swap_page (void ); +extern int swap_retrive_swap_device (const int *,struct seq_file *); +extern int swap_store_swap_device (const char *,int *); +extern swp_entry_t get_swap_page (struct page *);extern swp_entry_t get_swap_page_of_type (int );extern int add_swap_count_continuation (swp_entry_t ,gfp_t );extern void swap_shmem_alloc (swp_entry_t );
@@ -449,9 +453,11 @@ mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)#define swap_address_space (entry) (NULL)#define get_nr_swap_pages () 0L +#define total_public_swap_pages 0L +#define total_private_swap_pages 0L#define total_swap_pages 0L#define total_swapcache_pages () 0UL -#define vm_swap_full () 0 +#define vm_swap_full (page) 0#define si_swapinfo (val) \ do { (val)->freeswap = (val)->totalswap = 0; } while (0)
@@ -538,7 +544,7 @@ static inline int try_to_free_swap(struct page *page)return 0 ; } -static inline swp_entry_t get_swap_page (void ) +static inline swp_entry_t get_swap_page (struct page *page) {swp_entry_t entry; entry.val =0 ;
mm/memcontrol.c +76 -0
100 lines not on this page · raw message
mm/memory.c +2 -1
@@ -3136,7 +3136,8 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,mem_cgroup_commit_charge_swapin (page, ptr);swap_free (entry); -if (vm_swap_full () || (vma->vm_flags &VM_LOCKED ) ||PageMlocked (page)) +if (vm_swap_full (page) || (vma->vm_flags &VM_LOCKED ) || +PageMlocked (page))try_to_free_swap (page);unlock_page (page);if (page != swapcache) {
mm/shmem.c +1 -1
@@ -838,7 +838,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)SetPageUptodate (page); } - swap =get_swap_page (); + swap =get_swap_page (page);if (!swap.val) goto redirty;
mm/swap_state.c +1 -1
@@ -170,7 +170,7 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); - entry =get_swap_page (); + entry =get_swap_page (page);if (!entry.val)return 0 ;
mm/swapfile.c +211 -30
399 lines not on this page · raw message
mm/vmscan.c +1 -1
@@ -1095,7 +1095,7 @@ cull_mlocked: activate_locked:/* Not a candidate for swapping, so reclaim swap space. */ -if (PageSwapCache (page) &&vm_swap_full ()) +if (PageSwapCache (page) &&vm_swap_full (page))try_to_free_swap (page);VM_BUG_ON_PAGE (PageActive (page), page);SetPageActive (page);
Signature · 7 lines
1.9.1.423.g4596e3a -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 2/3 swap: do not store private swap files on swap_list
In reply to Yu ZhaoFrom: Jamie Liu <[email protected]>
swap_list is used by get_swap_page() to find public swap files to swap to; in the case that there are many private swap files and few public swap files, get_swap_page() may waste time iterating through private swap files it can't swap to. Change _enable_swap_info() to not insert private swap files onto swap_list; this improves the performance of get_swap_page() in such cases, at the cost of making swap_store_swap_device() and swapoff() minutely slower (both of which are non-critical).
--- mm/swapfile.c | 84 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 37 deletions(-)
mm/swapfile.c +47 -37
@@ -712,14 +712,14 @@ int swap_store_swap_device(const char *buf, int *_type) mapping = victim->f_mapping;spin_lock (&swap_lock); -for (type = swap_list.head; type >=0 ; type = swap_info[type]->next) { +for (type =0 ; type < nr_swapfiles; type++) { si = swap_info[type];if ((si->flags &SWP_WRITEOK ) ==SWP_WRITEOK ) {if (si->swap_file->f_mapping == mapping)break ; } } -if (type <0 ) { +if (type == nr_swapfiles) { err = -EINVAL ; }else { err =0 ;
@@ -803,10 +803,7 @@ swp_entry_t get_swap_page(struct page *page)spin_unlock (&si->lock);continue ; } -if (si->flags &SWP_PRIVATE ) { -spin_unlock (&si->lock); -continue ; - } +BUG_ON (si->flags &SWP_PRIVATE ); swap_list.next = next;
@@ -957,11 +954,12 @@ static unsigned char swap_entry_free(struct swap_info_struct *p, p->lowest_bit = offset;if (offset > p->highest_bit) p->highest_bit = offset; -set_highest_priority_index (p->type);if (p->flags &SWP_PRIVATE )atomic_long_inc (&nr_private_swap_pages); -else +else {atomic_long_inc (&nr_public_swap_pages); +set_highest_priority_index (p->type); + } p->inuse_pages--;frontswap_invalidate_page (p->type, offset);if (p->flags &SWP_BLKDEV ) {
@@ -1899,6 +1897,8 @@ static void _enable_swap_info(struct swap_info_struct *p, int prio,if (prio >=0 ) p->prio = prio; +else if (p->flags &SWP_PRIVATE ) + p->prio =0 ;else p->prio = --least_priority; p->swap_map = swap_map;
@@ -1910,19 +1910,19 @@ static void _enable_swap_info(struct swap_info_struct *p, int prio, }else {atomic_long_add (p->pages, &nr_public_swap_pages); total_public_swap_pages += p->pages; +/* insert swap space into swap_list: */ + prev =-1 ; +for (i = swap_list.head; i >=0 ; i = swap_info[i]->next) { +if (p->prio >= swap_info[i]->prio) +break ; + prev = i; + } + p->next = i; +if (prev <0 ) + swap_list.head = swap_list.next = p->type; +else + swap_info[prev]->next = p->type; } -/* insert swap space into swap_list: */ - prev =-1 ; -for (i = swap_list.head; i >=0 ; i = swap_info[i]->next) { -if (p->prio >= swap_info[i]->prio) -break ; - prev = i; - } - p->next = i; -if (prev <0 ) - swap_list.head = swap_list.next = p->type; -else - swap_info[prev]->next = p->type; } static void enable_swap_info(struct swap_info_struct *p, int prio,
@@ -1978,15 +1978,25 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) mapping = victim->f_mapping; prev =-1 ;spin_lock (&swap_lock); -for (type = swap_list.head; type >=0 ; type = swap_info[type]->next) { +for (type =0 ; type < nr_swapfiles; type++) { p = swap_info[type];if (p->flags &SWP_WRITEOK ) { -if (p->swap_file->f_mapping == mapping) +if (p->swap_file->f_mapping == mapping) { +/* Private swapfiles aren't in swap_list */ +if (p->flags &SWP_PRIVATE ) +break ; +/* Find type's predecessor in swap_list */ +for (i = swap_list.head; i >=0 ; + i = swap_info[i]->next) { +if (type == i) +break ; + prev = i; + }break ; + } } - prev = type; } -if (type <0 ) { +if (type == nr_swapfiles) { err = -EINVAL ;spin_unlock (&swap_lock); goto out_dput;
@@ -1998,24 +2008,24 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)spin_unlock (&swap_lock); goto out_dput; } -if (prev <0 ) - swap_list.head = p->next; -else - swap_info[prev]->next = p->next; -if (type == swap_list.next) { -/* just pick something that's safe... */ - swap_list.next = swap_list.head; - }spin_lock (&p->lock); -if (p->prio <0 ) { -for (i = p->next; i >=0 ; i = swap_info[i]->next) - swap_info[i]->prio = p->prio--; - least_priority++; - }if (p->flags &SWP_PRIVATE ) {atomic_long_sub (p->pages, &nr_private_swap_pages); total_private_swap_pages -= p->pages; }else { +if (prev <0 ) + swap_list.head = p->next; +else + swap_info[prev]->next = p->next; +if (type == swap_list.next) { +/* just pick something that's safe... */ + swap_list.next = swap_list.head; + } +if (p->prio <0 ) { +for (i = p->next; i >=0 ; i = swap_info[i]->next) + swap_info[i]->prio = p->prio--; + least_priority++; + }atomic_long_sub (p->pages, &nr_public_swap_pages); total_public_swap_pages -= p->pages; }
Signature · 7 lines
1.9.1.423.g4596e3a -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 3/3 swap: Increase the maximum number of swap files to 8192.
In reply to Yu ZhaoFrom: Suleiman Souhlal <[email protected]>
Allow up to 8192 swap files on x86_64. Prior to this patch the limit was 30 swap files, which is not enough if we want to use per memory cgroup swap files on a machine that has thousands of cgroups.
While this change also reduces the number of bits available for swap offsets in the PTE, it does not actually impose any new restrictions on the maximum size of swap files, as that is currently limited by the use of 32bit values in other parts of the swap code.
--- arch/x86/include/asm/pgtable_64.h | 63 ++++++++++++++++++++++++++++++--------- include/linux/swap.h | 7 +++-- 2 files changed, 54 insertions(+), 16 deletions(-)
arch/x86/include/asm/pgtable_64.h +49 -14
@@ -142,23 +142,58 @@ static inline int pgd_large(pgd_t pgd) { return 0; }#define pte_offset_map (dir, address) pte_offset_kernel((dir), (address))#define pte_unmap (pte) ((void)(pte))/* NOP */ -/* Encode and de-code a swap entry */ -#if _PAGE_BIT_FILE < _PAGE_BIT_PROTNONE -#define SWP_TYPE_BITS (_PAGE_BIT_FILE - _PAGE_BIT_PRESENT - 1) -#define SWP_OFFSET_SHIFT (_PAGE_BIT_PROTNONE + 1) -#else -#define SWP_TYPE_BITS (_PAGE_BIT_PROTNONE - _PAGE_BIT_PRESENT - 1) -#define SWP_OFFSET_SHIFT (_PAGE_BIT_FILE + 1) -#endif +/* +* Encode and de-code a swap entry +* We need to make sure we don't touch the PAGE_PRESENT, PAGE_FILE +* and PAGE_CANREAD bits. +* All bit ranges below are inclusive. +* +* Bits 1-5 and 12-19 of the PTE are the type, while bits 20-63 are the offset. +* +* This enables us to have 8192 different swap types and 44 bit offsets. +*/ +#define SWP_TYPE_BITS 13 +#define SWP_OFFSET_SHIFT 20#define MAX_SWAPFILES_CHECK () BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS) -#define __swp_type (x) (((x).val >> (_PAGE_BIT_PRESENT + 1)) \ - & ((1U << SWP_TYPE_BITS) - 1)) -#define __swp_offset (x) ((x).val >> SWP_OFFSET_SHIFT) -#define __swp_entry (type, offset) ((swp_entry_t) { \ - ((type) << (_PAGE_BIT_PRESENT + 1)) \ - | ((offset) << SWP_OFFSET_SHIFT) }) +#define __HAVE_ARCH_MAX_SWAPFILES_SHIFT +#define MAX_SWAPFILES_SHIFT 13 + +static inline unsigned long +___swp_type (unsigned long val) +{ +unsigned long type; + +/* Bits 1-5 */ + type = (val >>1 ) &0x1f ; +/* Bits 12-19 */ + type |= (val >>7 ) &0x1fe0 ; + +return type; +} + +#define __swp_type (x) (___swp_type((x).val)) +#define __swp_offset (x) ((x).val >> SWP_OFFSET_SHIFT) + +static inline unsigned long +___swp_entry (unsigned long type,pgoff_t off) +{ +unsigned long e; + +/* Bits 0-4 of type to bits 1-5 of entry */ + e = (type &0x1f ) <<1 ; +/* Bits 5-12 of type to bits 12-19 of entry */ + e |= (type &0x1fe0 ) <<7 ; +/* off to bits 20-63 */ + e |= off <<SWP_OFFSET_SHIFT ; + +return e; +} + +#define __swp_entry (type, offset) \ + ((swp_entry_t) { ___swp_entry(type, offset) }) +#define __pte_to_swp_entry (pte) ((swp_entry_t) { pte_val((pte)) })#define __swp_entry_to_pte (x) ((pte_t) { .pte = (x).val })
include/linux/swap.h +5 -2
@@ -12,6 +12,7 @@#include <linux/atomic.h> #include <linux/page-flags.h> #include <asm/page.h> +#include <asm/pgtable.h> struct notifier_block ;
@@ -42,7 +43,9 @@ static inline int current_is_kswapd(void)* on 32-bit-pgoff_t architectures. And that assumes that the architecture packs * the type/offset into the pte as 5/27 as well. */ +#ifndef __HAVE_ARCH_MAX_SWAPFILES_SHIFT#define MAX_SWAPFILES_SHIFT 5 +#endif /* * Use some of the swap files numbers for other purposes. This
@@ -221,8 +224,8 @@ struct percpu_cluster {struct swap_info_struct {unsigned long flags;/* SWP_USED etc: see above */ signed short prio;/* swap priority of this type */ -signed char type;/* strange name for an index */ -signed char next;/* next type on the swap list */ +int type;/* strange name for an index */ +int next;/* next type on the swap list */ unsigned int max;/* extent of the swap_map */ unsigned char *swap_map;/* vmalloc'ed array of usage counts */ struct swap_cluster_info *cluster_info;/* cluster info. Only for SSD */
Signature · 7 lines
1.9.1.423.g4596e3a -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Johannes Weiner
maintainerHello Yu!
Quoted 19 lines · Yu Zhao
This series of patches adds support to configure a cgroup to swap to a particular file by using control file memory.swapfile.
A value of "default" in memory.swapfile indicates that this cgroup should use the default, system-wide, swap files. A value of "none" indicates that this cgroup should never swap. Other values are interpreted as the path to a private swap file that can only be used by the owner (and its children).
The swap file has to be created and
swapon()has to be done on it withSWAP_FLAG_PRIVATE, before it can be used. This flag ensures that the swap file is private and does not get used by others.Jamie Liu (1):
swap: do not store private swap files onswap_listSuleiman Souhlal (2):
mm/swap: support per memory cgroup swapfiles
swap: Increase the maximum number of swap files to 8192.
Quoted 11 lines
Documentation/cgroups/memory.txt | 15 ++ arch/x86/include/asm/pgtable_64.h | 63 ++++++-- include/linux/memcontrol.h | 2 + include/linux/swap.h | 45 +++--- mm/memcontrol.c | 76 ++++++++++ mm/memory.c | 3 +- mm/shmem.c | 2 +- mm/swap_state.c | 2 +- mm/swapfile.c | 307 +++++++++++++++++++++++++++++++------- mm/vmscan.c | 2 +- 10 files changed, 423 insertions(+), 94 deletions(-)
For feature patches like this, please include a rationale. What is this functionality good for, and who is going to use this?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>