Support volatile range for anon vma
Subsystems (9)
RFC 1/1 Support volatile range for anon vma
This patch introudce new madvise behavior MADV_VOLATILE and MADV_NOVOLATILE for anonymous pages. It's different with John Stultz's version which considers only tmpfs. This patch cannot cover John's one so if below idea is proved as reasonable maybe, we can unify both concepts and I hope interface could become madvise/fadvise.
Rationale is following as.
Many allocators call munmap(2) when user call free(3) if ptr is in mmaped area. But munmap isn't cheap because it have to clean up all pte entries and unlinking a vma so overhead would be increased linearly by mmaped area's size.
Volatile conecept of Robert Love could be very useful for reducing free overhead. Allocators can do madvise(MADV_VOLATILE) instead of munmap(2). madvise(MADV_VOLATILE|NOVOLATILE) is very cheap opeartion because it just mark the flag in VMA and if memory pressure happens, VM can discard pages of volatile VMA instead of swapping out when volatile pages is selected as victim by normal VM aging policy.
Allocator should call madvise(MADV_NOVOLATILE) before allocating that area to user. Otherwise, accessing of volatile range will meet SIGBUS error.
The downside is that we have to age anon lru list although we don't have swap but I think it's a trade-off for getting good feature. Even, we had done it two years ago until merge [1] and I believe free(3) performance gain will beat loss of anon lru aging's overead once all of allocator start to use madvise.
I hope seeing opinions from others before diving into glibc or bionic. Welcome to any comment.
[1] 74e3f3c3, vmscan: prevent background aging of anon page in no swap system
--- include/asm-generic/mman-common.h | 3 + include/linux/mm.h | 8 ++- include/linux/mm_types.h | 5 ++ include/linux/rmap.h | 24 ++++++- mm/ksm.c | 4 +- mm/madvise.c | 32 +++++++++- mm/memory.c | 2 + mm/migrate.c | 6 +- mm/rmap.c | 127 +++++++++++++++++++++++++++++++++++-- mm/vmscan.c | 3 + 10 files changed, 202 insertions(+), 12 deletions(-)
include/asm-generic/mman-common.h +3 -0
@@ -34,6 +34,9 @@#define MADV_SEQUENTIAL 2/* expect sequential page references */ #define MADV_WILLNEED 3/* will need these pages */ #define MADV_DONTNEED 4/* don't need these pages */ +#define MADV_VOLATILE 5/* pages will disappear suddenly */ +#define MADV_NOVOLATILE 6/* pages will not disappear */ +/* common parameters: try to keep these consistent across architectures */ #define MADV_REMOVE 9/* remove these pages & resources */
include/linux/mm.h +7 -1
@@ -120,6 +120,12 @@ extern unsigned int kobjsize(const void *objp);#define VM_PFN_AT_MMAP 0x40000000/* PFNMAP vma that is fully mapped at mmap time */ #define VM_MERGEABLE 0x80000000/* KSM may merge identical pages */ +/* +* Recently, Konstantin removed a few flags but not merged yet +* so we will get a room for new flag for supporting 32 bit. Thanks, Konstantin!. +*/ +#define VM_VOLATILE 0x100000000 +/* Bits set in the VMA until the stack is in its final location */ #define VM_STACK_INCOMPLETE_SETUP (VM_RAND_READ | VM_SEQ_READ)
@@ -143,7 +149,7 @@ extern unsigned int kobjsize(const void *objp);* Special vmas that are non-mergable, non-mlock()able. * Note: mm/huge_memory.c VM_NO_THP depends on this definition. */ -#define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_RESERVED | VM_PFNMAP) +#define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_RESERVED | VM_PFNMAP | VM_VOLATILE)/* * mapping from the currently active vm_flags protection bits (the
include/linux/mm_types.h +5 -0
@@ -279,6 +279,11 @@ struct vm_area_struct {#ifdef CONFIG_NUMA struct mempolicy *vm_policy;/* NUMA policy for the VMA */ #endif +/* +* True if page in this vma is reclaimed. +* It's protected by anon_vma->mutex. +*/ +bool purged; };struct core_thread {
include/linux/rmap.h +23 -1
@@ -67,6 +67,10 @@ struct anon_vma_chain {struct list_head same_anon_vma;/* locked by anon_vma->mutex */ }; + +void volatile_lock (struct vm_area_struct *vma); +void volatile_unlock (struct vm_area_struct *vma); +#ifdef CONFIG_MMU static inline void get_anon_vma (struct anon_vma *anon_vma) {
@@ -170,12 +174,14 @@ enum ttu_flags {TTU_IGNORE_MLOCK = (1 <<8 ),/* ignore mlock */ TTU_IGNORE_ACCESS = (1 <<9 ),/* don't age */ TTU_IGNORE_HWPOISON = (1 <<10 ),/* corrupted page is recoverable */ + TTU_IGNORE_VOLATILE = (1 << 11),/* ignore volatile */ };#define TTU_ACTION (x) ((x) & TTU_ACTION_MASK)int try_to_unmap (struct page *,enum ttu_flags flags);int try_to_unmap_one (struct page *,struct vm_area_struct *, -unsigned long address,enum ttu_flags flags); +unsigned long address,enum ttu_flags flags, +bool *is_volatile);/* * Called from mm/filemap_xip.c to unmap empty zero page
@@ -194,6 +200,21 @@ static inline pte_t *page_check_address(struct page *page, struct mm_struct *mm,return ptep; } +pte_t *__page_check_volatile_address (struct page *,struct mm_struct *, +unsigned long ,spinlock_t **); + +static inline pte_t *page_check_volatile_address (struct page *page, +struct mm_struct *mm, +unsigned long address, +spinlock_t **ptlp) +{ +pte_t *ptep; + +__cond_lock (*ptlp, ptep =__page_check_volatile_address (page, + mm, address, ptlp)); +return ptep; +} +/* * Used by swapoff to help locate where page is expected in vma. */
@@ -257,5 +278,6 @@ static inline int page_mkclean(struct page *page)#define SWAP_AGAIN 1#define SWAP_FAIL 2#define SWAP_MLOCK 3 +#define SWAP_DISCARD 4#endif /* _LINUX_RMAP_H */
mm/ksm.c +3 -1
@@ -1653,6 +1653,7 @@ int try_to_unmap_ksm(struct page *page, enum ttu_flags flags)struct rmap_item *rmap_item;int ret =SWAP_AGAIN ;int search_new_forks =0 ; +bool dummy_volatile;VM_BUG_ON (!PageKsm (page));VM_BUG_ON (!PageLocked (page));
@@ -1682,7 +1683,8 @@ again:continue ; ret =try_to_unmap_one (page, vma, - rmap_item->address, flags); + rmap_item->address, flags, + &dummy_volatile);if (ret !=SWAP_AGAIN || !page_mapped (page)) {anon_vma_unlock (anon_vma); goto out;
mm/madvise.c +30 -2
@@ -86,6 +86,22 @@ static long madvise_behavior(struct vm_area_struct * vma,if (error) goto out;break ; +case MADV_VOLATILE : +if (vma->vm_flags &VM_LOCKED ) { + error = -EINVAL ; + goto out; + } + new_flags |=VM_VOLATILE ; + vma->purged = false; +break ; +case MADV_NOVOLATILE : +if (!(vma->vm_flags &VM_VOLATILE )) { + error = -EINVAL ; + goto out; + } + + new_flags &= ~VM_VOLATILE ; +break ; }if (new_flags == vma->vm_flags) {
@@ -118,9 +134,15 @@ static long madvise_behavior(struct vm_area_struct * vma, success:/* * vm_flags is protected by the mmap_sem held in write mode. +* In case of VOLATILE, we need volatile_lock, additionally. */ +if (behavior ==MADV_NOVOLATILE || behavior ==MADV_VOLATILE ) +volatile_lock (vma); vma->vm_flags = new_flags; - +if (behavior ==MADV_NOVOLATILE ) + error = vma->purged; +if (behavior ==MADV_NOVOLATILE || behavior ==MADV_VOLATILE ) +volatile_unlock (vma); out:if (error == -ENOMEM ) error = -EAGAIN ;
@@ -310,6 +332,8 @@ madvise_behavior_valid(int behavior)#endif case MADV_DONTDUMP :case MADV_DODUMP : +case MADV_VOLATILE : +case MADV_NOVOLATILE :return 1 ;default :
@@ -383,7 +407,11 @@ SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)if (start & ~PAGE_MASK ) goto out; - len = (len_in + ~PAGE_MASK ) &PAGE_MASK ; + +if (behavior !=MADV_VOLATILE && behavior !=MADV_NOVOLATILE ) + len = (len_in + ~PAGE_MASK ) &PAGE_MASK ; +else + len = len_in &PAGE_MASK ;/* Check to see whether len was rounded up from small -ve to zero */ if (len_in && !len)
mm/memory.c +2 -0
@@ -3441,6 +3441,8 @@ int handle_pte_fault(struct mm_struct *mm, entry = *pte;if (!pte_present (entry)) {if (pte_none (entry)) { +if (unlikely (vma->vm_flags &VM_VOLATILE )) +return VM_FAULT_SIGBUS ;if (vma->vm_ops) {if (likely (vma->vm_ops->fault)) return do_linear_fault(mm, vma, address,
mm/migrate.c +4 -2
@@ -800,7 +800,8 @@ static int __unmap_and_move(struct page *page, struct page *newpage, }/* Establish migration ptes or remove ptes */ -try_to_unmap (page,TTU_MIGRATION |TTU_IGNORE_MLOCK |TTU_IGNORE_ACCESS ); +try_to_unmap (page,TTU_MIGRATION |TTU_IGNORE_MLOCK | +TTU_IGNORE_ACCESS |TTU_IGNORE_VOLATILE ); skip_unmap:if (!page_mapped (page))
@@ -915,7 +916,8 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,if (PageAnon (hpage)) anon_vma =page_get_anon_vma (hpage); -try_to_unmap (hpage,TTU_MIGRATION |TTU_IGNORE_MLOCK |TTU_IGNORE_ACCESS ); +try_to_unmap (hpage,TTU_MIGRATION |TTU_IGNORE_MLOCK | +TTU_IGNORE_ACCESS |TTU_IGNORE_VOLATILE );if (!page_mapped (hpage)) rc =move_to_new_page (new_hpage, hpage,1 , mode);
mm/rmap.c +122 -5
201 lines not on this page · raw message
mm/vmscan.c +3 -0
@@ -789,6 +789,8 @@ static unsigned long shrink_page_list(struct list_head *page_list,*/ if (page_mapped (page) && mapping) {switch (try_to_unmap (page,TTU_UNMAP )) { +case SWAP_DISCARD : + goto discard_page;case SWAP_FAIL : goto activate_locked;case SWAP_AGAIN :
@@ -857,6 +859,7 @@ static unsigned long shrink_page_list(struct list_head *page_list, } } +discard_page:/* * If the page has buffers, try to free the buffer mappings * associated with this page. If we succeed we try to free
Signature · 7 lines
1.7.9.5 -- 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>
include/linux/mm_types.h struct vm_area_struct { · line 283
Quoted 3 lines · Minchan Kim
#endif
include/linux/mm_types.h +2
-0+/* +* True if page in this vma is reclaimed.
What does that mean? All pages in the vma have been cleared out?
include/linux/rmap.h enum ttu_flags { · line 184
Quoted 9 lines
include/linux/rmap.h +3
-1+ TTU_IGNORE_VOLATILE = (1 << 11),/* ignore volatile */ };#define TTU_ACTION (x) ((x) & TTU_ACTION_MASK)int try_to_unmap (struct page *,enum ttu_flags flags);int try_to_unmap_one (struct page *,struct vm_area_struct *, -unsigned long address,enum ttu_flags flags); +unsigned long address,enum ttu_flags flags, +bool *is_volatile);
You already pass a vma pointer in. Why do you need to pass a volatile flag in? Looks like unecessary churn.
--
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>
Hi Christoph,
Quoted 7 lines · Christoph Lameter
include/linux/mm_types.h struct vm_area_struct { · line 283
Quoted 3 lines · Minchan Kim
#endif
include/linux/mm_types.h +2
-0+/* +* True if page in this vma is reclaimed. What does that mean? All pages in the vma have been cleared out?
It means at least, more than one is reclaimed.
Comment should have been cleared.
Quoted 5 lines
Quoted 5 lines
include/linux/rmap.h +1
-0+ TTU_IGNORE_VOLATILE = (1 << 11),/* ignore volatile */ };#define TTU_ACTION (x) ((x) & TTU_ACTION_MASK)
Quoted 8 lines
include/linux/rmap.h enum ttu_flags { · line 184
Quoted 5 lines
int
try_to_unmap(struct page *, enum ttu_flags flags); int try_to_unmap_one(struct page *, structvm_area_struct*,include/linux/rmap.h +2
-1- unsigned long address, enum ttu_flags flags); + unsigned long address, enum ttu_flags flags, + bool *is_volatile);You already pass a vma pointer in. Why do you need to pass a volatile flag in? Looks like unecessary churn.
You mean we can use vma->purged instead of is_volatile passing?
The is_volatile is just checking for that all of vmas share the page are volatile ones. Then, vma->purged is just checking for that the page is zapped in the vma. If one of vma share the page isn't volatile, we can't zap.
BTW, Christoph, what do you think about the goal of the patch which changes munmap(2) to madvise(2) when user calls free(3) in user allocator like glibc? I guess it would improve system performance very well.
But as I wrote down in description, downside of the patch is that we have to age anon lru although we don't have swap. But gain via the patch is bigger than loss via aging of anon lru when memory pressure happens. I don't see other downside other than it. What do you think about it?
(I didn't implement anon lru aging in case of no-swap but it's trivial once we decide)
Thanks for the review, Christoph
Quoted 6 lines
--
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>
Signature · 8 lines
Kind regards, Minchan Kim -- 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>
Quoted 7 lines · Minchan Kim
I guess it would improve system performance very well.
But as I wrote down in description, downside of the patch is that we have to age anon lru although we don't have swap. But gain via the patch is bigger than loss via aging of anon lru when memory pressure happens. I don't see other downside other than it. What do you think about it?
(I didn't implement anon lru aging in case of no-swap but it's trivial once we decide)
I am a bit confused like some of the others as to why this patch is necessary since we already have DONT_NEED.
--
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>
Hi Christoph,
Quoted 11 lines · Christoph Lameter
Quoted 7 lines · Minchan Kim
I guess it would improve system performance very well.
But as I wrote down in description, downside of the patch is that we have to age anon lru although we don't have swap. But gain via the patch is bigger than loss via aging of anon lru when memory pressure happens. I don't see other downside other than it. What do you think about it?
(I didn't implement anon lru aging in case of no-swap but it's trivial once we decide)
Quoted 2 lines
I am a bit confused like some of the others as to why this patch is necessary since we already have
DONT_NEED.
Totally, my fault. I should have written clearly.
DONT_NEED have to zap all pte entries/tlb flush when system call happens so DONT_NEED isn't cheap.
Even, later if user accesses address again, page fault happens.
This patch is to remove above two overheads.
while I discussed with KOSAKI, I found there was trial of simillar goal by Rik. https://lkml.org/lkml/2007/4/17/53
But as I look over the code, it seems to have a cost about setting PG_lazyfree on all pages of range which isn't in my implementation.
Anyway, I would like to know where Rik's patch wasn't merged at that time.
Quoted 6 lines
--
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>
Signature · 8 lines
Kind regards, Minchan Kim -- 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>