mm: proof-of-concept memory compaction without isolation
Subsystems (8)
cover mm: proof-of-concept memory compaction without isolation
This is incomplete implementation of non-isolating memory migration and compaction. It's alive!
The main reason -- it can preserve lru order during compaction.
Also it makes implementation of migration for various types of pages: zram, balloon, ptes, kernel stacks [ Why not? I've already migrated them accidentally and kernel have crashed in very funny places ] much easier: owner just have to set page->mappingw with valid method a_ops->migratepage.
---
Konstantin Khlebnikov (6):
pagevec: segmented page vectors
mm/migrate: move putback of old page out of unmap_and_move mm/cma: repalce reclaim_clean_pages_from_list with try_to_reclaim_page mm/migrate: page migration without page isolation mm/compaction: use migration without isolation
mm/migrate: preserve lru order if possible
include/linux/migrate.h | 4 + include/linux/mm.h | 1 include/linux/pagevec.h | 48 ++++++++- include/trace/events/compaction.h | 12 +- mm/compaction.c | 205 +++++++++++++++++++++---------------- mm/filemap.c | 20 ++++ mm/internal.h | 12 +- mm/migrate.c | 141 +++++++++++++++++++++---- mm/page_alloc.c | 35 ++++-- mm/swap.c | 69 ++++++++++++ mm/vmscan.c | 42 +------- 11 files changed, 410 insertions(+), 179 deletions(-)
--
Konstantin
--
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>
RFC PATCH 1/6 pagevec: segmented page vectors
This patch adds helpers for linking several page vectors into segmented chain and macro for iterating over this chain.
For linking it uses space formerly used for field 'cold' which is now stored as a lower bit in pointer to the next segment.
--- include/linux/pagevec.h | 48 ++++++++++++++++++++++++++++++++++++++++++++--- mm/swap.c | 44 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 5 deletions(-)
include/linux/pagevec.h +45 -3
@@ -16,7 +16,7 @@ struct address_space;struct pagevec {unsigned long nr; -unsigned long cold; +unsigned long _next;struct page *pages[PAGEVEC_SIZE ]; };
@@ -32,11 +32,23 @@ unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,unsigned pagevec_lookup_tag (struct pagevec *pvec,struct address_space *mapping,pgoff_t *index,int tag,unsigned nr_pages); +struct pagevec *pagevec_extend (struct pagevec *pvec,gfp_t gfpmask); +void pagevec_shrink (struct pagevec *pvec); -static inline void pagevec_init (struct pagevec *pvec,int cold) +static inline bool pagevec_cold (struct pagevec *pvec) +{ +return pvec->_next &1 ; +} + +static inline struct pagevec *pagevec_next (struct pagevec *pvec) +{ +return (struct pagevec *)(pvec->_next & ~1ul ); +} + +static inline void pagevec_init (struct pagevec *pvec,bool cold) { pvec->nr =0 ; - pvec->cold = cold; + pvec->_next = cold; }static inline void pagevec_reinit (struct pagevec *pvec)
@@ -69,4 +81,34 @@ static inline void pagevec_release(struct pagevec *pvec) __pagevec_release(pvec); } +/** +* pagevec_for_each_page - iterate over all pages in single page vector * +* @pv pointer to page vector +* @i int variable used as index +* @page pointer to struct page +*/ +#define pagevec_for_each_page (pv, i, page) \ + for (i = 0; (i < (pv)->nr) && (page = (pv)->pages[i], true); i++) + +/** +* pagevec_for_each_vec - iterate over all segments in page vector +* @pv pointer to head page vector +* @v pointer for current page vector segment +*/ +#define pagevec_for_each_vec (pv, v) \ + for (v = (pv); v; v = pagevec_next(v)) + +/** +* pagevec_for_each_vec_and_page - iterate over all pages in segmented vector +* @pv pointer to head page vector +* @v pointer for current page vector segment +* @i int variable used index in current segment +* @page pointer to struct page +* +* Warning: this is double loop, "break" does not work. +*/ +#define pagevec_for_each_vec_and_page (pv, v, i, page) \ + pagevec_for_each_vec(pv, v) \ + pagevec_for_each_page(v, i, page) +#endif /* _LINUX_PAGEVEC_H */
mm/swap.c +42 -2
68 lines not on this page · raw message
--
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>
RFC PATCH 2/6 mm/migrate: move putback of old page out of unmap_and_move
In reply to Konstantin KhlebnikovThis is preparation for migrating non-isolated pages.
--- mm/migrate.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-)
mm/migrate.c +14 -20
@@ -939,19 +939,6 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page, rc =__unmap_and_move (page, newpage, force, mode); out: -if (rc != -EAGAIN ) { -/* -* A page that has been migrated has all references -* removed and will be freed. A page that has not been -* migrated will have kepts its references and be -* restored. -*/ -list_del (&page->lru); -dec_zone_page_state (page,NR_ISOLATED_ANON + -page_is_file_cache (page)); -putback_lru_page (page); - } -/* * If migration was not successful and there's a freeing callback, use * it. Otherwise, putback_lru_page() will drop the reference grabbed
@@ -1011,10 +998,8 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,* tables or check whether the hugepage is pmd-based or not before * kicking migration. */ -if (!hugepage_migration_supported (page_hstate (hpage))) { -putback_active_hugepage (hpage); +if (!hugepage_migration_supported (page_hstate (hpage)))return -ENOSYS ; - } new_hpage =get_new_page (hpage, private, &result);if (!new_hpage)
@@ -1051,9 +1036,6 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,unlock_page (hpage); out: -if (rc != -EAGAIN ) -putback_active_hugepage (hpage); -/* * If migration was not successful and there's a freeing callback, use * it. Otherwise, put_page() will drop the reference grabbed during
@@ -1129,7 +1111,8 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, goto out;case -EAGAIN : retry++; -break ; +/* Keep that page for next pass */ +continue ;case MIGRATEPAGE_SUCCESS : nr_succeeded++;break ;
@@ -1143,6 +1126,17 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, nr_failed++;break ; } + +/* Putback migrated or permanently failed page. */ +if (PageHuge (page)) { +putback_active_hugepage (page); + }else { +list_del (&page->lru); +if (unlikely (isolated_balloon_page (page))) +balloon_page_putback (page); +else +putback_lru_page (page); + } } } rc = nr_failed + retry;
--
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>
RFC PATCH 3/6 mm/cma: repalce reclaim_clean_pages_from_list with try_to_reclaim_page
In reply to Konstantin KhlebnikovThis gives almost the same behavior but makes code much less ugly. Reclaimer works only with isolated pages, try_to_reclaim_page doesn't require that. Of course it fails if page is currently isolated by somebody else because in this case page has elevated refcount.
--- include/linux/mm.h | 1 + mm/filemap.c | 20 ++++++++++++++++++++ mm/internal.h | 2 -- mm/page_alloc.c | 13 +++++++++---- mm/vmscan.c | 42 +++++------------------------------------- 5 files changed, 35 insertions(+), 43 deletions(-)
include/linux/mm.h +1 -0
@@ -1204,6 +1204,7 @@ int get_kernel_page(unsigned long start, int write, struct page **pages);struct page *get_dump_page (unsigned long addr);extern int try_to_release_page (struct page * page,gfp_t gfp_mask); +extern int try_to_reclaim_page (struct page *page);extern void do_invalidatepage (struct page *page,unsigned int offset,unsigned int length);
mm/filemap.c +20 -0
@@ -2663,3 +2663,23 @@ int try_to_release_page(struct page *page, gfp_t gfp_mask) }EXPORT_SYMBOL (try_to_release_page); + +/** +* try_to_reclaim_page() - unmap and invalidate clean page cache page +* +* @page: the page which the kernel is trying to free +*/ +int try_to_reclaim_page (struct page *page) +{ +int ret; + +if (PageDirty (page) ||PageWriteback (page)) +return 0 ; +if (!trylock_page (page)) +return 0 ; +if (page_mapped (page)) +try_to_unmap (page,TTU_UNMAP |TTU_IGNORE_ACCESS ); + ret =invalidate_inode_page (page); +unlock_page (page); +return ret; +}
mm/internal.h +0 -2
@@ -416,8 +416,6 @@ extern unsigned long vm_mmap_pgoff(struct file *, unsigned long, unsigned long, unsigned long);extern void set_pageblock_order (void ); -unsigned long reclaim_clean_pages_from_list (struct zone *zone, -struct list_head *page_list);/* The ALLOC_WMARK bits are used as an index to zone->watermark */ #define ALLOC_WMARK_MIN WMARK_MIN#define ALLOC_WMARK_LOW WMARK_LOW
mm/page_alloc.c +9 -4
@@ -6341,9 +6341,9 @@ static int __alloc_contig_migrate_range(struct compact_control *cc, unsigned long start, unsigned long end) {/* This function is based on compact_zone() from compaction.c. */ -unsigned long nr_reclaimed;unsigned long pfn = start;unsigned int tries =0 ; +struct page *page;int ret =0 ;migrate_prep ();
@@ -6367,9 +6367,14 @@ static int __alloc_contig_migrate_range(struct compact_control *cc,break ; } - nr_reclaimed =reclaim_clean_pages_from_list (cc->zone, - &cc->migratepages); - cc->nr_migratepages -= nr_reclaimed; +/* +* Try to reclaim clean page cache pages. +* Migration simply skips pages where page_count == 1. +*/ +list_for_each_entry (page, &cc->migratepages, lru) { +if (!PageAnon (page)) +try_to_reclaim_page (page); + } ret =migrate_pages (&cc->migratepages, alloc_migrate_target,NULL ,0 , cc->mode,MR_CMA );
mm/vmscan.c +5 -37
77 lines not on this page · raw message
--
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>
RFC PATCH 4/6 mm/migrate: page migration without page isolation
In reply to Konstantin Khlebnikovmigrate_pagevec() does the same job as migrate_pages() but it works with chained page vector instead of list of isolated pages.
--- include/linux/migrate.h | 4 ++ mm/migrate.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+)
include/linux/migrate.h +4 -0
@@ -33,6 +33,10 @@ extern int migrate_page(struct address_space *, struct page *, struct page *, enum migrate_mode);extern int migrate_pages (struct list_head *l,new_page_t new,free_page_t free,unsigned long private,enum migrate_mode mode,int reason); +struct pagevec ; +extern int migrate_pagevec (struct pagevec *pages,new_page_t get_new_page, +free_page_t put_new_page,unsigned long private, +enum migrate_mode mode,int reason);extern int migrate_prep (void );extern int migrate_prep_local (void );
mm/migrate.c +98 -0
104 lines not on this page · raw message
--
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>
RFC PATCH 5/6 mm/compaction: use migration without isolation
In reply to Konstantin KhlebnikovTODO
* fix interaction with too_many_isolated (account pages as pinned?)
* rename the rest of isolated_* stuff, pages are not isolated
* fix racy check page->mapping->a_ops->migratepage
--- include/trace/events/compaction.h | 12 +- mm/compaction.c | 205 +++++++++++++++++++++---------------- mm/internal.h | 9 +- mm/migrate.c | 2 mm/page_alloc.c | 24 ++-- 5 files changed, 142 insertions(+), 110 deletions(-)
include/trace/events/compaction.h +8 -4
@@ -66,7 +66,7 @@ TRACE_EVENT(mm_compaction_migratepages, TP_PROTO(unsigned long nr_all, int migrate_rc, - struct list_head *migratepages), + struct pagevec *migratepages), TP_ARGS(nr_all, migrate_rc, migratepages),
@@ -77,7 +77,9 @@ TRACE_EVENT(mm_compaction_migratepages, TP_fast_assign( unsigned long nr_failed = 0; -struct list_head *page_lru; +struct pagevec *pvec; +struct page *page; +int i;/* * migrate_pages() returns either a non-negative number
@@ -88,8 +90,10 @@ TRACE_EVENT(mm_compaction_migratepages,if (migrate_rc >=0 ) nr_failed = migrate_rc;else -list_for_each (page_lru, migratepages) - nr_failed++; +pagevec_for_each_vec_and_page (migratepages, + pvec, i, page) +if (page_count (page) !=1 ) + nr_failed++; __entry->nr_migrated = nr_all - nr_failed; __entry->nr_failed = nr_failed;
mm/compaction.c +114 -91
385 lines not on this page · raw message
mm/internal.h +6 -3
@@ -13,6 +13,7 @@#include <linux/fs.h> #include <linux/mm.h> +#include <linux/pagevec.h> void free_pgtables (struct mmu_gather *tlb,struct vm_area_struct *start_vma,unsigned long floor,unsigned long ceiling);
@@ -176,11 +177,12 @@ extern int user_min_free_kbytes;*/ struct compact_control {struct list_head freepages;/* List of free pages to migrate to */ -struct list_head migratepages;/* List of pages being migrated */ +struct pagevec migratepages;/* Vector of pages being migrated */ +struct pagevec *migratepages_tail;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 migrate_pfn;/* isolate_migratepages search base */ +unsigned long migrate_pfn;/* collect_migratepages search base */ enum migrate_mode mode;/* Async or sync migration mode */ bool ignore_skip_hint;/* Scan blocks even if marked skip */ int order;/* order a direct compactor needs */
@@ -198,8 +200,9 @@ unsigned long isolate_freepages_range(struct compact_control *cc, unsigned long start_pfn, unsigned long end_pfn);unsigned long -isolate_migratepages_range (struct compact_control *cc, +collect_migratepages_range (struct compact_control *cc,unsigned long low_pfn,unsigned long end_pfn); +void release_migratepages (struct compact_control *cc);int find_suitable_fallback (struct free_area *area,unsigned int order,int migratetype,bool only_stealable,bool *can_steal);
mm/migrate.c +1 -1
@@ -76,7 +76,7 @@ int migrate_prep_local(void)* from where they were once taken off for compaction/migration. * * This function shall be used whenever the isolated pageset has been -* built from lru, balloon, hugetlbfs page. See isolate_migratepages_range() +* built from lru, balloon, hugetlbfs page. See collect_migratepages_range() * and isolate_huge_page(). */ void putback_movable_pages (struct list_head *l)
mm/page_alloc.c +13 -11
@@ -6343,20 +6343,21 @@ static int __alloc_contig_migrate_range(struct compact_control *cc,/* This function is based on compact_zone() from compaction.c. */ unsigned long pfn = start;unsigned int tries =0 ; +struct pagevec *pvec;struct page *page; -int ret =0 ; +int index, ret =0 ;migrate_prep (); -while (pfn < end || !list_empty (&cc->migratepages)) { +while (pfn < end ||pagevec_count (&cc->migratepages)) {if (fatal_signal_pending (current)) { ret = -EINTR ;break ; } -if (list_empty (&cc->migratepages)) { +if (!pagevec_count (&cc->migratepages)) { cc->nr_migratepages =0 ; - pfn =isolate_migratepages_range (cc, pfn, end); + pfn =collect_migratepages_range (cc, pfn, end);if (!pfn) { ret = -EINTR ;break ;
@@ -6371,18 +6372,18 @@ static int __alloc_contig_migrate_range(struct compact_control *cc,* Try to reclaim clean page cache pages. * Migration simply skips pages where page_count == 1. */ -list_for_each_entry (page, &cc->migratepages, lru) { +pagevec_for_each_vec_and_page (&cc->migratepages, + pvec, index, page) {if (!PageAnon (page))try_to_reclaim_page (page); } - ret =migrate_pages (&cc->migratepages, alloc_migrate_target, -NULL ,0 , cc->mode,MR_CMA ); + ret =migrate_pagevec (&cc->migratepages, alloc_migrate_target, +NULL ,0 , cc->mode,MR_CMA ); } -if (ret <0 ) { -putback_movable_pages (&cc->migratepages); +release_migratepages (cc); +if (ret <0 )return ret; - }return 0 ; }
@@ -6419,7 +6420,8 @@ int alloc_contig_range(unsigned long start, unsigned long end, .mode = MIGRATE_SYNC, .ignore_skip_hint = true, }; -INIT_LIST_HEAD (&cc.migratepages); +pagevec_init (&cc.migratepages,0 ); + cc.migratepages_tail = &cc.migratepages;/* * What we do here is we mark all pageblocks in range as
--
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>
RFC PATCH 6/6 mm/migrate: preserve lru order if possible
In reply to Konstantin KhlebnikovTODO
* link old and new pages and insert them as a batch later
--- mm/internal.h | 1 + mm/migrate.c | 7 +++++-- mm/swap.c | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-)
mm/internal.h +1 -0
@@ -99,6 +99,7 @@ extern unsigned long highest_memmap_pfn;*/ extern int isolate_lru_page (struct page *page);extern void putback_lru_page (struct page *page); +extern void insert_lru_page (struct page *page,struct page *pos);extern bool zone_reclaimable (struct zone *zone);/*
mm/migrate.c +5 -2
@@ -947,10 +947,13 @@ out:if (rc !=MIGRATEPAGE_SUCCESS && put_new_page) {ClearPageSwapBacked (newpage);put_new_page (newpage, private); - }else if (unlikely (__is_movable_balloon_page (newpage))) { + }else if (rc !=MIGRATEPAGE_SUCCESS || +unlikely (__is_movable_balloon_page (newpage))) {/* drop our reference, page already in the balloon */ put_page (newpage); - }else + }else if (PageLRU (page)) +insert_lru_page (newpage, page); +else putback_lru_page (newpage);if (result) {
mm/swap.c +25 -0
@@ -518,6 +518,31 @@ static void __activate_page(struct page *page, struct lruvec *lruvec, } } +void insert_lru_page (struct page *page,struct page *pos) +{ +struct zone *zone =page_zone (page); +int lru =page_lru_base_type (page); +struct lruvec *lruvec; +unsigned long flags; + + if (page_evictable(page) && lru == page_lru(pos) && +#ifdef CONFIG_MEMCG + page->mem_cgroup == pos->mem_cgroup && +#endif + zone == page_zone(pos)) { +spin_lock_irqsave (&zone->lru_lock, flags); + lruvec =mem_cgroup_page_lruvec (page, zone); +SetPageLRU (page); +add_page_to_lru_list (page, lruvec, lru); +trace_mm_lru_insertion (page, lru); +if (PageLRU (pos) && + lruvec ==mem_cgroup_page_lruvec (pos, zone)) +list_move (&page->lru, &pos->lru); +spin_unlock_irqrestore (&zone->lru_lock, flags); + } else +putback_lru_page (page ); +} +#ifdef CONFIG_SMP static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
--
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 4 lines · Konstantin Khlebnikov
This is incomplete implementation of non-isolating memory migration and compaction. It's alive!
The main reason -- it can preserve lru order during compaction.
That's nice, and there's also another benefit - no lru_lock taken during migration scanner.
So I think it's worth pursuing. But after brief checking, I'm not sure it can work as it is (but maybe I just overlooked something). What prevents somebody else to isolate the old page from the lru while you are migrating it? Especially in Patch 6, it appears that a PageLRU() is tested without any lock and only then insert_lru_page() takes the lru_lock to do something. This seems racy to me.
Quoted 30 lines
Also it makes implementation of migration for various types of pages: zram, balloon, ptes, kernel stacks [ Why not? I've already migrated them accidentally and kernel have crashed in very funny places ] much easier: owner just have to set
page->mappingwwith valid methoda_ops->migratepage.---
Konstantin Khlebnikov (6):
pagevec: segmented page vectors
mm/migrate: move putback of old page out ofunmap_and_movemm/cma: repalcereclaim_clean_pages_from_listwithtry_to_reclaim_pagemm/migrate: page migration without page isolation mm/compaction: use migration without isolation
mm/migrate: preserve lru order if possibleinclude/linux/migrate.h | 4 + include/linux/mm.h | 1 include/linux/pagevec.h | 48 ++++++++- include/trace/events/compaction.h | 12 +- mm/compaction.c | 205 +++++++++++++++++++++---------------- mm/filemap.c | 20 ++++ mm/internal.h | 12 +- mm/migrate.c | 141 +++++++++++++++++++++---- mm/page_alloc.c | 35 ++++-- mm/swap.c | 69 ++++++++++++ mm/vmscan.c | 42 +------- 11 files changed, 410 insertions(+), 179 deletions(-)
Quoted 9 lines
--
Konstantin--
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>
--
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>