Skip to contentSkip to conversation

Support non-lru page migration

PATCH Superseded Minchan Kim 5 participants 62 messages May 20, 2016 – Jun 30, 2016

Subsystems (14)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Minchan Kim

author maintainer
#0 lore raw

v6 cover Support non-lru page migration

Recently, I got many reports about perfermance degradation in embedded system(Android mobile phone, webOS TV and so on) and easy fork fail.

The problem was fragmentation caused by zram and GPU driver mainly. With memory pressure, their pages were spread out all of pageblock and it cannot be migrated with current compaction algorithm which supports only LRU pages. In the end, compaction cannot work well so reclaimer shrinks all of working set pages. It made system very slow and even to fail to fork easily which requires order-[2 or 3] allocations.

Other pain point is that they cannot use CMA memory space so when OOM kill happens, I can see many free pages in CMA area, which is not memory efficient. In our product which has big CMA memory, it reclaims zones too exccessively to allocate GPU and zram page although there are lots of free space in CMA so system becomes very slow easily.

To solve these problem, this patch tries to add facility to migrate non-lru pages via introducing new functions and page flags to help migration.

struct address_space_operations {
	..
	..
	bool (*isolate_page)(struct page *, isolate_mode_t);
	void (*putback_page)(struct page *);
	..
}

new page flags

PG_movable
PG_isolated

For details, please read description in "mm: migrate: support non-lru movable page migration".

Originally, Gioh Kim had tried to support this feature but he moved so I took over the work. I took many code from his work and changed a little bit and Konstantin Khlebnikov helped Gioh a lot so he should deserve to have many credit, too.

And I should mention Chulmin who have tested this patchset heavily so I can find many bugs from him. :)

Thanks, Gioh, Konstantin and Chulmin!

This patchset consists of five parts.

1. clean up migration
mm: use put_page to free page instead of putback_lru_page

2. add non-lru page migration feature
mm: migrate: support non-lru movable page migration

3. rework KVM memory-ballooning
mm: balloon: use general non-lru movable page feature

4. zsmalloc refactoring for preparing page migration
zsmalloc: keep max_object in size_class
zsmalloc: use bit_spin_lock
zsmalloc: use accessor
zsmalloc: factor page chain functionality out
zsmalloc: introduce zspage structure
zsmalloc: separate free_zspage from putback_zspage
zsmalloc: use freeobj for index

5. zsmalloc page migration
zsmalloc: page migration support
zram: use __GFP_MOVABLE for memory allocation

* From v5
* rebase on next-20160520
* move utility functions to compaction.c and export - Sergey
* zsmalloc dobule free fix - Sergey
* add additional Reviewed-by for zsmalloc - Sergey

* From v4
* rebase on mmotm-2016-05-05-17-19
* fix huge object migration - Chulmin
* !CONFIG_COMPACTION support for zsmalloc

* From v3
* rebase on mmotm-2016-04-06-20-40
* fix swap_info deadlock - Chulmin
* race without page_lock - Vlastimil
* no use page._mapcount for potential user-mapped page driver - Vlastimil
* fix and enhance doc/description - Vlastimil
* use page->mapping lower bits to represent PG_movable
* make driver side's rule simple.

* From v2
* rebase on mmotm-2016-03-29-15-54-16
* check PageMovable before lock_page - Joonsoo
* check PageMovable before PageIsolated checking - Joonsoo
* add more description about rule

* From v1
* rebase on v4.5-mmotm-2016-03-17-15-04
* reordering patches to merge clean-up patches first
* add Acked-by/Reviewed-by from Vlastimil and Sergey
* use each own mount model instead of reusing anon_inode_fs - Al Viro
* small changes - YiPing, Gioh

Cc Vlastimil Babka <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc John Einar Reitan <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Konstantin Khlebnikov <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Rafael Aquini <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
Cc Gioh Kim <[email protected]>
Cc Chan Gyun Jeong <[email protected]>
Cc Sangseok Lee <[email protected]>
Cc Kyeongdon Kim <[email protected]>
Cc Chulmin Kim <[email protected]>

Minchan Kim (12):
mm: use put_page to free page instead of putback_lru_page
mm: migrate: support non-lru movable page migration
mm: balloon: use general non-lru movable page feature
zsmalloc: keep max_object in size_class
zsmalloc: use bit_spin_lock
zsmalloc: use accessor
zsmalloc: factor page chain functionality out
zsmalloc: introduce zspage structure
zsmalloc: separate free_zspage from putback_zspage
zsmalloc: use freeobj for index
zsmalloc: page migration support
zram: use __GFP_MOVABLE for memory allocation

 Documentation/filesystems/Locking  |    4 +
 Documentation/filesystems/vfs.txt  |   11 +
 Documentation/vm/page_migration    |  107 ++-
 drivers/block/zram/zram_drv.c      |    6 +-
 drivers/virtio/virtio_balloon.c    |   54 +-
 include/linux/balloon_compaction.h |   53 +-
 include/linux/compaction.h         |   17 +
 include/linux/fs.h                 |    2 +
 include/linux/ksm.h                |    3 +-
 include/linux/migrate.h            |    2 +
 include/linux/mm.h                 |    1 +
 include/linux/page-flags.h         |   33 +-
 include/uapi/linux/magic.h         |    2 +
 mm/balloon_compaction.c            |   94 +--
 mm/compaction.c                    |   76 +-
 mm/ksm.c                           |    4 +-
 mm/migrate.c                       |  256 +++++--
 mm/page_alloc.c                    |    2 +-
 mm/util.c                          |    6 +-
 mm/vmscan.c                        |    2 +-
 mm/zsmalloc.c                      | 1351 +++++++++++++++++++++++++-----------
 21 files changed, 1475 insertions(+), 611 deletions(-)
Signature · 1 line
1.9.1
author maintainer
#1 lore raw

PATCH v6 1/12 mm: use put_page to free page instead of putback_lru_page

Procedure of page migration is as follows:

First of all, it should isolate a page from LRU and try to migrate the page. If it is successful, it releases the page for freeing. Otherwise, it should put the page back to LRU list.

For LRU pages, we have used putback_lru_page for both freeing and putback to LRU list. It's okay because put_page is aware of LRU list so if it releases last refcount of the page, it removes the page from LRU list. However, It makes unnecessary operations (e.g., lru_cache_add, pagevec and flags operations. It would be not significant but no worth to do) and harder to support new non-lru page migration because put_page isn't aware of non-lru page's data structure.

To solve the problem, we can add new hook in put_page with PageMovable flags check but it can increase overhead in hot path and needs new locking scheme to stabilize the flag check with put_page.

So, this patch cleans it up to divide two semantic(ie, put and putback). If migration is successful, use put_page instead of putback_lru_page and use putback_lru_page only on failure. That makes code more readable and doesn't add overhead in put_page.

Comment from Vlastimil
"Yeah, and compaction (perhaps also other migration users) has to drain the lru pvec... Getting rid of this stuff is worth even by itself."

Cc Rik van Riel <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Naoya Horiguchi <[email protected]>
---
 mm/migrate.c | 64 +++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 40 insertions(+), 24 deletions(-)
mm/migrate.c +40 -24
index 9baf41c877ff..2666f28b5236 100644
@@ -913,6 +913,19 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		put_anon_vma(anon_vma);
 	unlock_page(page);
 out:
+	/*
+	 * If migration is successful, decrease refcount of the newpage
+	 * which will not free the page because new page owner increased
+	 * refcounter. As well, if it is LRU page, add the page to LRU
+	 * list in here.
+	 */
+	if (rc == MIGRATEPAGE_SUCCESS) {
+		if (unlikely(__is_movable_balloon_page(newpage)))
+			put_page(newpage);
+		else
+			putback_lru_page(newpage);
+	}
+
 	return rc;
 }
 
@@ -946,6 +959,12 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page,
 
 	if (page_count(page) == 1) {
 		/* page was freed from under us. So we are done. */
+		ClearPageActive(page);
+		ClearPageUnevictable(page);
+		if (put_new_page)
+			put_new_page(newpage, private);
+		else
+			put_page(newpage);
 		goto out;
 	}
 
@@ -958,10 +977,8 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page,
 	}
 
 	rc = __unmap_and_move(page, newpage, force, mode);
-	if (rc == MIGRATEPAGE_SUCCESS) {
-		put_new_page = NULL;
+	if (rc == MIGRATEPAGE_SUCCESS)
 		set_page_owner_migrate_reason(newpage, reason);
-	}
 
 out:
 	if (rc != -EAGAIN) {
@@ -974,34 +991,33 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page,
 		list_del(&page->lru);
 		dec_zone_page_state(page, NR_ISOLATED_ANON +
 				page_is_file_cache(page));
-		/* Soft-offlined page shouldn't go through lru cache list */
-		if (reason == MR_MEMORY_FAILURE && rc == MIGRATEPAGE_SUCCESS) {
+	}
+
+	/*
+	 * If migration is successful, releases reference grabbed during
+	 * isolation. Otherwise, restore the page to right list unless
+	 * we want to retry.
+	 */
+	if (rc == MIGRATEPAGE_SUCCESS) {
+		put_page(page);
+		if (reason == MR_MEMORY_FAILURE) {
 			/*
-			 * With this release, we free successfully migrated
-			 * page and set PG_HWPoison on just freed page
-			 * intentionally. Although it's rather weird, it's how
-			 * HWPoison flag works at the moment.
+			 * Set PG_HWPoison on just freed page
+			 * intentionally. Although it's rather weird,
+			 * it's how HWPoison flag works at the moment.
 			 */
-			put_page(page);
 			if (!test_set_page_hwpoison(page))
 				num_poisoned_pages_inc();
-		} else
+		}
+	} else {
+		if (rc != -EAGAIN)
 			putback_lru_page(page);
+		if (put_new_page)
+			put_new_page(newpage, private);
+		else
+			put_page(newpage);
 	}
 
-	/*
-	 * If migration was not successful and there's a freeing callback, use
-	 * it.  Otherwise, putback_lru_page() will drop the reference grabbed
-	 * during isolation.
-	 */
-	if (put_new_page)
-		put_new_page(newpage, private);
-	else if (unlikely(__is_movable_balloon_page(newpage))) {
-		/* drop our reference, page already in the balloon */
-		put_page(newpage);
-	} else
-		putback_lru_page(newpage);
-
 	if (result) {
 		if (rc)
 			*result = rc;
Signature · 7 lines
1.9.1

--
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>
author maintainer
#2 lore raw

PATCH v6 2/12 mm: migrate: support non-lru movable page migration

In reply to Minchan Kim

We have allowed migration for only LRU pages until now and it was enough to make high-order pages. But recently, embedded system(e.g., webOS, android) uses lots of non-movable pages(e.g., zram, GPU memory) so we have seen several reports about troubles of small high-order allocation. For fixing the problem, there were several efforts (e,g,. enhance compaction algorithm, SLUB fallback to 0-order page, reserved memory, vmalloc and so on) but if there are lots of non-movable pages in system, their solutions are void in the long run.

So, this patch is to support facility to change non-movable pages with movable. For the feature, this patch introduces functions related to migration to address_space_operations as well as some page flags.

If a driver want to make own pages movable, it should define three functions which are function pointers of struct address_space_operations.

1. bool (*isolate_page) (struct page *page, isolate_mode_t mode);

What VM expects on isolate_page function of driver is to return *true* if driver isolates page successfully. On returing true, VM marks the page as PG_isolated so concurrent isolation in several CPUs skip the page for isolation. If a driver cannot isolate the page, it should return *false*.

Once page is successfully isolated, VM uses page.lru fields so driver shouldn't expect to preserve values in that fields.

2. int (*migratepage) (struct address_space *mapping,

		struct page *newpage, struct page *oldpage, enum migrate_mode);

After isolation, VM calls migratepage of driver with isolated page. The function of migratepage is to move content of the old page to new page and set up fields of struct page newpage. Keep in mind that you should clear PG_movable of oldpage via __ClearPageMovable under page_lock if you migrated the oldpage successfully and returns 0.
If driver cannot migrate the page at the moment, driver can return -EAGAIN. On -EAGAIN, VM will retry page migration in a short time because VM interprets -EAGAIN as "temporal migration failure". On returning any error except -EAGAIN, VM will give up the page migration without retrying in this time.

Driver shouldn't touch page.lru field VM using in the functions.

3. void (*putback_page)(struct page *);

If migration fails on isolated page, VM should return the isolated page to the driver so VM calls driver's putback_page with migration failed page. In this function, driver should put the isolated page back to the own data structure.

4. non-lru movable page flags

There are two page flags for supporting non-lru movable page.

* PG_movable

Driver should use the below function to make page movable under page_lock.

void __SetPageMovable(struct page *page, struct address_space *mapping)

It needs argument of address_space for registering migration family functions which will be called by VM. Exactly speaking, PG_movable is not a real flag of struct page. Rather than, VM reuses page->mapping's lower bits to represent it.

	#define PAGE_MAPPING_MOVABLE 0x2
	page->mapping = page->mapping | PAGE_MAPPING_MOVABLE;

so driver shouldn't access page->mapping directly. Instead, driver should use page_mapping which mask off the low two bits of page->mapping so it can get right struct address_space.

For testing of non-lru movable page, VM supports __PageMovable function. However, it doesn't guarantee to identify non-lru movable page because page->mapping field is unified with other variables in struct page. As well, if driver releases the page after isolation by VM, page->mapping doesn't have stable value although it has PAGE_MAPPING_MOVABLE (Look at __ClearPageMovable). But __PageMovable is cheap to catch whether page is LRU or non-lru movable once the page has been isolated. Because LRU pages never can have PAGE_MAPPING_MOVABLE in page->mapping. It is also good for just peeking to test non-lru movable pages before more expensive checking with lock_page in pfn scanning to select victim.

For guaranteeing non-lru movable page, VM provides PageMovable function. Unlike __PageMovable, PageMovable functions validates page->mapping and mapping->a_ops->isolate_page under lock_page. The lock_page prevents sudden destroying of page->mapping.

Driver using __SetPageMovable should clear the flag via __ClearMovablePage under page_lock before the releasing the page.

* PG_isolated

To prevent concurrent isolation among several CPUs, VM marks isolated page as PG_isolated under lock_page. So if a CPU encounters PG_isolated non-lru movable page, it can skip it. Driver doesn't need to manipulate the flag because VM will set/clear it automatically. Keep in mind that if driver sees PG_isolated page, it means the page have been isolated by VM so it shouldn't touch page.lru field.
PG_isolated is alias with PG_reclaim flag so driver shouldn't use the flag for own purpose.

Cc Rik van Riel <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Rafael Aquini <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc John Einar Reitan <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
---
 Documentation/filesystems/Locking |   4 +
 Documentation/filesystems/vfs.txt |  11 +++
 Documentation/vm/page_migration   | 107 ++++++++++++++++++++-
 include/linux/compaction.h        |  17 ++++
 include/linux/fs.h                |   2 +
 include/linux/ksm.h               |   3 +-
 include/linux/migrate.h           |   2 +
 include/linux/mm.h                |   1 +
 include/linux/page-flags.h        |  33 +++++--
 mm/compaction.c                   |  82 ++++++++++++----
 mm/ksm.c                          |   4 +-
 mm/migrate.c                      | 191 ++++++++++++++++++++++++++++++++++----
 mm/page_alloc.c                   |   2 +-
 mm/util.c                         |   6 +-
 14 files changed, 411 insertions(+), 54 deletions(-)
Documentation/filesystems/Locking +4 -0
index 75eea7ce3d7c..dda6e3f8e203 100644
@@ -195,7 +195,9 @@ unlocks and drops the reference.
 	int (*releasepage) (struct page *, int);
 	void (*freepage)(struct page *);
 	int (*direct_IO)(struct kiocb *, struct iov_iter *iter);
+	bool (*isolate_page) (struct page *, isolate_mode_t);
 	int (*migratepage)(struct address_space *, struct page *, struct page *);
+	void (*putback_page) (struct page *);
 	int (*launder_page)(struct page *);
 	int (*is_partially_uptodate)(struct page *, unsigned long, unsigned long);
 	int (*error_remove_page)(struct address_space *, struct page *);
@@ -219,7 +221,9 @@ invalidatepage:		yes
 releasepage:		yes
 freepage:		yes
 direct_IO:
+isolate_page:		yes
 migratepage:		yes (both)
+putback_page:		yes
 launder_page:		yes
 is_partially_uptodate:	yes
 error_remove_page:	yes
Documentation/filesystems/vfs.txt +11 -0
index c61a223ef3ff..900360cbcdae 100644
@@ -592,9 +592,14 @@ struct address_space_operations {
 	int (*releasepage) (struct page *, int);
 	void (*freepage)(struct page *);
 	ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter);
+	/* isolate a page for migration */
+	bool (*isolate_page) (struct page *, isolate_mode_t);
 	/* migrate the contents of a page to the specified target */
 	int (*migratepage) (struct page *, struct page *);
+	/* put migration-failed page back to right list */
+	void (*putback_page) (struct page *);
 	int (*launder_page) (struct page *);
+
 	int (*is_partially_uptodate) (struct page *, unsigned long,
 					unsigned long);
 	void (*is_dirty_writeback) (struct page *, bool *, bool *);
@@ -747,6 +752,10 @@ struct address_space_operations {
         and transfer data directly between the storage and the
         application's address space.
 
+  isolate_page: Called by the VM when isolating a movable non-lru page.
+	If page is successfully isolated, VM marks the page as PG_isolated
+	via __SetPageIsolated.
+
   migrate_page:  This is used to compact the physical memory usage.
         If the VM wants to relocate a page (maybe off a memory card
         that is signalling imminent failure) it will pass a new page
@@ -754,6 +763,8 @@ struct address_space_operations {
 	transfer any private data across and update any references
         that it has to the page.
 
+  putback_page: Called by the VM when isolated page's migration fails.
+
   launder_page: Called before freeing a page - it writes back the dirty page. To
   	prevent redirtying the page, it is kept locked during the whole
 	operation.
Documentation/vm/page_migration +106 -1

111 lines not on this page · raw message

include/linux/compaction.h +17 -0
index a58c852a268f..c6b47c861cea 100644
@@ -54,6 +54,9 @@ enum compact_result {
 struct alloc_context; /* in mm/internal.h */
 
 #ifdef CONFIG_COMPACTION
+extern int PageMovable(struct page *page);
+extern void __SetPageMovable(struct page *page, struct address_space *mapping);
+extern void __ClearPageMovable(struct page *page);
 extern int sysctl_compact_memory;
 extern int sysctl_compaction_handler(struct ctl_table *table, int write,
 			void __user *buffer, size_t *length, loff_t *ppos);
@@ -151,6 +154,19 @@ extern void kcompactd_stop(int nid);
 extern void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx);
 
 #else
+static inline int PageMovable(struct page *page)
+{
+	return 0;
+}
+static inline void __SetPageMovable(struct page *page,
+			struct address_space *mapping)
+{
+}
+
+static inline void __ClearPageMovable(struct page *page)
+{
+}
+
 static inline enum compact_result try_to_compact_pages(gfp_t gfp_mask,
 			unsigned int order, int alloc_flags,
 			const struct alloc_context *ac,
@@ -212,6 +228,7 @@ static inline void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_i
 #endif /* CONFIG_COMPACTION */
 
 #if defined(CONFIG_COMPACTION) && defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
+struct node;
 extern int compaction_register_node(struct node *node);
 extern void compaction_unregister_node(struct node *node);
 
include/linux/fs.h +2 -0
index c9cc1f699dc1..6a2ce439ea42 100644
@@ -402,6 +402,8 @@ struct address_space_operations {
 	 */
 	int (*migratepage) (struct address_space *,
 			struct page *, struct page *, enum migrate_mode);
+	bool (*isolate_page)(struct page *, isolate_mode_t);
+	void (*putback_page)(struct page *);
 	int (*launder_page) (struct page *);
 	int (*is_partially_uptodate) (struct page *, unsigned long,
 					unsigned long);
include/linux/ksm.h +1 -2
index 7ae216a39c9e..481c8c4627ca 100644
@@ -43,8 +43,7 @@ static inline struct stable_node *page_stable_node(struct page *page)
 static inline void set_page_stable_node(struct page *page,
 					struct stable_node *stable_node)
 {
-	page->mapping = (void *)stable_node +
-				(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
+	page->mapping = (void *)((unsigned long)stable_node | PAGE_MAPPING_KSM);
 }
 
 /*
include/linux/migrate.h +2 -0
index 9b50325e4ddf..404fbfefeb33 100644
@@ -37,6 +37,8 @@ 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);
+extern bool isolate_movable_page(struct page *page, isolate_mode_t mode);
+extern void putback_movable_page(struct page *page);
 
 extern int migrate_prep(void);
 extern int migrate_prep_local(void);
include/linux/mm.h +1 -0
index a00ec816233a..33eaec57e997 100644
@@ -1035,6 +1035,7 @@ static inline pgoff_t page_file_index(struct page *page)
 }
 
 bool page_mapped(struct page *page);
+struct address_space *page_mapping(struct page *page);
 
 /*
  * Return true only if the page has been allocated with
include/linux/page-flags.h +23 -10
index e5a32445f930..f8a2c4881608 100644
@@ -129,6 +129,9 @@ enum pageflags {
 
 	/* Compound pages. Stored in first tail page's flags */
 	PG_double_map = PG_private_2,
+
+	/* non-lru isolated movable page */
+	PG_isolated = PG_reclaim,
 };
 
 #ifndef __GENERATING_BOUNDS_H
@@ -357,29 +360,37 @@ PAGEFLAG(Idle, idle, PF_ANY)
  * with the PAGE_MAPPING_ANON bit set to distinguish it.  See rmap.h.
  *
  * On an anonymous page in a VM_MERGEABLE area, if CONFIG_KSM is enabled,
- * the PAGE_MAPPING_KSM bit may be set along with the PAGE_MAPPING_ANON bit;
- * and then page->mapping points, not to an anon_vma, but to a private
+ * the PAGE_MAPPING_MOVABLE bit may be set along with the PAGE_MAPPING_ANON
+ * bit; and then page->mapping points, not to an anon_vma, but to a private
  * structure which KSM associates with that merged page.  See ksm.h.
  *
- * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is currently never used.
+ * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is used for non-lru movable
+ * page and then page->mapping points a struct address_space.
  *
  * Please note that, confusingly, "page_mapping" refers to the inode
  * address_space which maps the page from disk; whereas "page_mapped"
  * refers to user virtual address space into which the page is mapped.
  */
-#define PAGE_MAPPING_ANON	1
-#define PAGE_MAPPING_KSM	2
-#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM)
+#define PAGE_MAPPING_ANON	0x1
+#define PAGE_MAPPING_MOVABLE	0x2
+#define PAGE_MAPPING_KSM	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
+#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
 
-static __always_inline int PageAnonHead(struct page *page)
+static __always_inline int PageMappingFlag(struct page *page)
 {
-	return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
+	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) != 0;
 }
 
 static __always_inline int PageAnon(struct page *page)
 {
 	page = compound_head(page);
-	return PageAnonHead(page);
+	return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
+}
+
+static __always_inline int __PageMovable(struct page *page)
+{
+	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
+				PAGE_MAPPING_MOVABLE;
 }
 
 #ifdef CONFIG_KSM
@@ -393,7 +404,7 @@ static __always_inline int PageKsm(struct page *page)
 {
 	page = compound_head(page);
 	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
-				(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
+				PAGE_MAPPING_KSM;
 }
 #else
 TESTPAGEFLAG_FALSE(Ksm)
@@ -641,6 +652,8 @@ static inline void __ClearPageBalloon(struct page *page)
 	atomic_set(&page->_mapcount, -1);
 }
 
+__PAGEFLAG(Isolated, isolated, PF_ANY);
+
 /*
  * If network-based swap is enabled, sl*b must keep track of whether pages
  * were allocated from pfmemalloc reserves.
mm/compaction.c +66 -16

101 lines not on this page · raw message

mm/ksm.c +2 -2
index 4786b4150f62..35b8aef867a9 100644
@@ -532,8 +532,8 @@ static struct page *get_ksm_page(struct stable_node *stable_node, bool lock_it)
 	void *expected_mapping;
 	unsigned long kpfn;
 
-	expected_mapping = (void *)stable_node +
-				(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
+	expected_mapping = (void *)((unsigned long)stable_node |
+					PAGE_MAPPING_KSM);
 again:
 	kpfn = READ_ONCE(stable_node->kpfn);
 	page = pfn_to_page(kpfn);
mm/migrate.c +171 -20

266 lines not on this page · raw message

mm/page_alloc.c +1 -1
index f8f3bfc435ee..26868bbaecce 100644
@@ -1008,7 +1008,7 @@ static __always_inline bool free_pages_prepare(struct page *page,
 			(page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
 		}
 	}
-	if (PageAnonHead(page))
+	if (PageMappingFlag(page))
 		page->mapping = NULL;
 	if (check_free)
 		bad += free_pages_check(page);
mm/util.c +4 -2
index 224d36e43a94..a04ccff7cc17 100644
@@ -399,10 +399,12 @@ struct address_space *page_mapping(struct page *page)
 	}
 
 	mapping = page->mapping;
-	if ((unsigned long)mapping & PAGE_MAPPING_FLAGS)
+	if ((unsigned long)mapping & PAGE_MAPPING_ANON)
 		return NULL;
-	return mapping;
+
+	return (void *)((unsigned long)mapping & ~PAGE_MAPPING_FLAGS);
 }
+EXPORT_SYMBOL(page_mapping);
 
 /* Slow path of page_mapcount() for compound pages */
 int __page_mapcount(struct page *page)
Signature · 1 line
1.9.1
author maintainer
#3 lore raw

PATCH v6 2/12 mm: migrate: support non-lru movable page migration

In reply to Minchan Kim

We have allowed migration for only LRU pages until now and it was enough to make high-order pages. But recently, embedded system(e.g., webOS, android) uses lots of non-movable pages(e.g., zram, GPU memory) so we have seen several reports about troubles of small high-order allocation. For fixing the problem, there were several efforts (e,g,. enhance compaction algorithm, SLUB fallback to 0-order page, reserved memory, vmalloc and so on) but if there are lots of non-movable pages in system, their solutions are void in the long run.

So, this patch is to support facility to change non-movable pages with movable. For the feature, this patch introduces functions related to migration to address_space_operations as well as some page flags.

If a driver want to make own pages movable, it should define three functions which are function pointers of struct address_space_operations.

1. bool (*isolate_page) (struct page *page, isolate_mode_t mode);

What VM expects on isolate_page function of driver is to return *true* if driver isolates page successfully. On returing true, VM marks the page as PG_isolated so concurrent isolation in several CPUs skip the page for isolation. If a driver cannot isolate the page, it should return *false*.

Once page is successfully isolated, VM uses page.lru fields so driver shouldn't expect to preserve values in that fields.

2. int (*migratepage) (struct address_space *mapping,

		struct page *newpage, struct page *oldpage, enum migrate_mode);

After isolation, VM calls migratepage of driver with isolated page. The function of migratepage is to move content of the old page to new page and set up fields of struct page newpage. Keep in mind that you should clear PG_movable of oldpage via __ClearPageMovable under page_lock if you migrated the oldpage successfully and returns 0.
If driver cannot migrate the page at the moment, driver can return -EAGAIN. On -EAGAIN, VM will retry page migration in a short time because VM interprets -EAGAIN as "temporal migration failure". On returning any error except -EAGAIN, VM will give up the page migration without retrying in this time.

Driver shouldn't touch page.lru field VM using in the functions.

3. void (*putback_page)(struct page *);

If migration fails on isolated page, VM should return the isolated page to the driver so VM calls driver's putback_page with migration failed page. In this function, driver should put the isolated page back to the own data structure.

4. non-lru movable page flags

There are two page flags for supporting non-lru movable page.

* PG_movable

Driver should use the below function to make page movable under page_lock.

void __SetPageMovable(struct page *page, struct address_space *mapping)

It needs argument of address_space for registering migration family functions which will be called by VM. Exactly speaking, PG_movable is not a real flag of struct page. Rather than, VM reuses page->mapping's lower bits to represent it.

	#define PAGE_MAPPING_MOVABLE 0x2
	page->mapping = page->mapping | PAGE_MAPPING_MOVABLE;

so driver shouldn't access page->mapping directly. Instead, driver should use page_mapping which mask off the low two bits of page->mapping so it can get right struct address_space.

For testing of non-lru movable page, VM supports __PageMovable function. However, it doesn't guarantee to identify non-lru movable page because page->mapping field is unified with other variables in struct page. As well, if driver releases the page after isolation by VM, page->mapping doesn't have stable value although it has PAGE_MAPPING_MOVABLE (Look at __ClearPageMovable). But __PageMovable is cheap to catch whether page is LRU or non-lru movable once the page has been isolated. Because LRU pages never can have PAGE_MAPPING_MOVABLE in page->mapping. It is also good for just peeking to test non-lru movable pages before more expensive checking with lock_page in pfn scanning to select victim.

For guaranteeing non-lru movable page, VM provides PageMovable function. Unlike __PageMovable, PageMovable functions validates page->mapping and mapping->a_ops->isolate_page under lock_page. The lock_page prevents sudden destroying of page->mapping.

Driver using __SetPageMovable should clear the flag via __ClearMovablePage under page_lock before the releasing the page.

* PG_isolated

To prevent concurrent isolation among several CPUs, VM marks isolated page as PG_isolated under lock_page. So if a CPU encounters PG_isolated non-lru movable page, it can skip it. Driver doesn't need to manipulate the flag because VM will set/clear it automatically. Keep in mind that if driver sees PG_isolated page, it means the page have been isolated by VM so it shouldn't touch page.lru field.
PG_isolated is alias with PG_reclaim flag so driver shouldn't use the flag for own purpose.

Cc Rik van Riel <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Rafael Aquini <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc John Einar Reitan <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
---
 Documentation/filesystems/Locking |   4 +
 Documentation/filesystems/vfs.txt |  11 +++
 Documentation/vm/page_migration   | 107 ++++++++++++++++++++-
 include/linux/compaction.h        |  17 ++++
 include/linux/fs.h                |   2 +
 include/linux/ksm.h               |   3 +-
 include/linux/migrate.h           |   2 +
 include/linux/mm.h                |   1 +
 include/linux/page-flags.h        |  33 +++++--
 mm/compaction.c                   |  82 ++++++++++++----
 mm/ksm.c                          |   4 +-
 mm/migrate.c                      | 191 ++++++++++++++++++++++++++++++++++----
 mm/page_alloc.c                   |   2 +-
 mm/util.c                         |   6 +-
 14 files changed, 411 insertions(+), 54 deletions(-)
Documentation/filesystems/Locking +4 -0
index 75eea7ce3d7c..dda6e3f8e203 100644
@@ -195,7 +195,9 @@ unlocks and drops the reference.
 	int (*releasepage) (struct page *, int);
 	void (*freepage)(struct page *);
 	int (*direct_IO)(struct kiocb *, struct iov_iter *iter);
+	bool (*isolate_page) (struct page *, isolate_mode_t);
 	int (*migratepage)(struct address_space *, struct page *, struct page *);
+	void (*putback_page) (struct page *);
 	int (*launder_page)(struct page *);
 	int (*is_partially_uptodate)(struct page *, unsigned long, unsigned long);
 	int (*error_remove_page)(struct address_space *, struct page *);
@@ -219,7 +221,9 @@ invalidatepage:		yes
 releasepage:		yes
 freepage:		yes
 direct_IO:
+isolate_page:		yes
 migratepage:		yes (both)
+putback_page:		yes
 launder_page:		yes
 is_partially_uptodate:	yes
 error_remove_page:	yes
Documentation/filesystems/vfs.txt +11 -0
index c61a223ef3ff..900360cbcdae 100644
@@ -592,9 +592,14 @@ struct address_space_operations {
 	int (*releasepage) (struct page *, int);
 	void (*freepage)(struct page *);
 	ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter);
+	/* isolate a page for migration */
+	bool (*isolate_page) (struct page *, isolate_mode_t);
 	/* migrate the contents of a page to the specified target */
 	int (*migratepage) (struct page *, struct page *);
+	/* put migration-failed page back to right list */
+	void (*putback_page) (struct page *);
 	int (*launder_page) (struct page *);
+
 	int (*is_partially_uptodate) (struct page *, unsigned long,
 					unsigned long);
 	void (*is_dirty_writeback) (struct page *, bool *, bool *);
@@ -747,6 +752,10 @@ struct address_space_operations {
         and transfer data directly between the storage and the
         application's address space.
 
+  isolate_page: Called by the VM when isolating a movable non-lru page.
+	If page is successfully isolated, VM marks the page as PG_isolated
+	via __SetPageIsolated.
+
   migrate_page:  This is used to compact the physical memory usage.
         If the VM wants to relocate a page (maybe off a memory card
         that is signalling imminent failure) it will pass a new page
@@ -754,6 +763,8 @@ struct address_space_operations {
 	transfer any private data across and update any references
         that it has to the page.
 
+  putback_page: Called by the VM when isolated page's migration fails.
+
   launder_page: Called before freeing a page - it writes back the dirty page. To
   	prevent redirtying the page, it is kept locked during the whole
 	operation.
Documentation/vm/page_migration +106 -1

111 lines not on this page · raw message

include/linux/compaction.h +17 -0
index a58c852a268f..c6b47c861cea 100644
@@ -54,6 +54,9 @@ enum compact_result {
 struct alloc_context; /* in mm/internal.h */
 
 #ifdef CONFIG_COMPACTION
+extern int PageMovable(struct page *page);
+extern void __SetPageMovable(struct page *page, struct address_space *mapping);
+extern void __ClearPageMovable(struct page *page);
 extern int sysctl_compact_memory;
 extern int sysctl_compaction_handler(struct ctl_table *table, int write,
 			void __user *buffer, size_t *length, loff_t *ppos);
@@ -151,6 +154,19 @@ extern void kcompactd_stop(int nid);
 extern void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx);
 
 #else
+static inline int PageMovable(struct page *page)
+{
+	return 0;
+}
+static inline void __SetPageMovable(struct page *page,
+			struct address_space *mapping)
+{
+}
+
+static inline void __ClearPageMovable(struct page *page)
+{
+}
+
 static inline enum compact_result try_to_compact_pages(gfp_t gfp_mask,
 			unsigned int order, int alloc_flags,
 			const struct alloc_context *ac,
@@ -212,6 +228,7 @@ static inline void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_i
 #endif /* CONFIG_COMPACTION */
 
 #if defined(CONFIG_COMPACTION) && defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
+struct node;
 extern int compaction_register_node(struct node *node);
 extern void compaction_unregister_node(struct node *node);
 
include/linux/fs.h +2 -0
index c9cc1f699dc1..6a2ce439ea42 100644
@@ -402,6 +402,8 @@ struct address_space_operations {
 	 */
 	int (*migratepage) (struct address_space *,
 			struct page *, struct page *, enum migrate_mode);
+	bool (*isolate_page)(struct page *, isolate_mode_t);
+	void (*putback_page)(struct page *);
 	int (*launder_page) (struct page *);
 	int (*is_partially_uptodate) (struct page *, unsigned long,
 					unsigned long);
include/linux/ksm.h +1 -2
index 7ae216a39c9e..481c8c4627ca 100644
@@ -43,8 +43,7 @@ static inline struct stable_node *page_stable_node(struct page *page)
 static inline void set_page_stable_node(struct page *page,
 					struct stable_node *stable_node)
 {
-	page->mapping = (void *)stable_node +
-				(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
+	page->mapping = (void *)((unsigned long)stable_node | PAGE_MAPPING_KSM);
 }
 
 /*
include/linux/migrate.h +2 -0
index 9b50325e4ddf..404fbfefeb33 100644
@@ -37,6 +37,8 @@ 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);
+extern bool isolate_movable_page(struct page *page, isolate_mode_t mode);
+extern void putback_movable_page(struct page *page);
 
 extern int migrate_prep(void);
 extern int migrate_prep_local(void);
include/linux/mm.h +1 -0
index a00ec816233a..33eaec57e997 100644
@@ -1035,6 +1035,7 @@ static inline pgoff_t page_file_index(struct page *page)
 }
 
 bool page_mapped(struct page *page);
+struct address_space *page_mapping(struct page *page);
 
 /*
  * Return true only if the page has been allocated with
include/linux/page-flags.h +23 -10
index e5a32445f930..f8a2c4881608 100644
@@ -129,6 +129,9 @@ enum pageflags {
 
 	/* Compound pages. Stored in first tail page's flags */
 	PG_double_map = PG_private_2,
+
+	/* non-lru isolated movable page */
+	PG_isolated = PG_reclaim,
 };
 
 #ifndef __GENERATING_BOUNDS_H
@@ -357,29 +360,37 @@ PAGEFLAG(Idle, idle, PF_ANY)
  * with the PAGE_MAPPING_ANON bit set to distinguish it.  See rmap.h.
  *
  * On an anonymous page in a VM_MERGEABLE area, if CONFIG_KSM is enabled,
- * the PAGE_MAPPING_KSM bit may be set along with the PAGE_MAPPING_ANON bit;
- * and then page->mapping points, not to an anon_vma, but to a private
+ * the PAGE_MAPPING_MOVABLE bit may be set along with the PAGE_MAPPING_ANON
+ * bit; and then page->mapping points, not to an anon_vma, but to a private
  * structure which KSM associates with that merged page.  See ksm.h.
  *
- * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is currently never used.
+ * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is used for non-lru movable
+ * page and then page->mapping points a struct address_space.
  *
  * Please note that, confusingly, "page_mapping" refers to the inode
  * address_space which maps the page from disk; whereas "page_mapped"
  * refers to user virtual address space into which the page is mapped.
  */
-#define PAGE_MAPPING_ANON	1
-#define PAGE_MAPPING_KSM	2
-#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM)
+#define PAGE_MAPPING_ANON	0x1
+#define PAGE_MAPPING_MOVABLE	0x2
+#define PAGE_MAPPING_KSM	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
+#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
 
-static __always_inline int PageAnonHead(struct page *page)
+static __always_inline int PageMappingFlag(struct page *page)
 {
-	return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
+	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) != 0;
 }
 
 static __always_inline int PageAnon(struct page *page)
 {
 	page = compound_head(page);
-	return PageAnonHead(page);
+	return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
+}
+
+static __always_inline int __PageMovable(struct page *page)
+{
+	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
+				PAGE_MAPPING_MOVABLE;
 }
 
 #ifdef CONFIG_KSM
@@ -393,7 +404,7 @@ static __always_inline int PageKsm(struct page *page)
 {
 	page = compound_head(page);
 	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
-				(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
+				PAGE_MAPPING_KSM;
 }
 #else
 TESTPAGEFLAG_FALSE(Ksm)
@@ -641,6 +652,8 @@ static inline void __ClearPageBalloon(struct page *page)
 	atomic_set(&page->_mapcount, -1);
 }
 
+__PAGEFLAG(Isolated, isolated, PF_ANY);
+
 /*
  * If network-based swap is enabled, sl*b must keep track of whether pages
  * were allocated from pfmemalloc reserves.
mm/compaction.c +66 -16

101 lines not on this page · raw message

mm/ksm.c +2 -2
index 4786b4150f62..35b8aef867a9 100644
@@ -532,8 +532,8 @@ static struct page *get_ksm_page(struct stable_node *stable_node, bool lock_it)
 	void *expected_mapping;
 	unsigned long kpfn;
 
-	expected_mapping = (void *)stable_node +
-				(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
+	expected_mapping = (void *)((unsigned long)stable_node |
+					PAGE_MAPPING_KSM);
 again:
 	kpfn = READ_ONCE(stable_node->kpfn);
 	page = pfn_to_page(kpfn);
mm/migrate.c +171 -20

266 lines not on this page · raw message

mm/page_alloc.c +1 -1
index f8f3bfc435ee..26868bbaecce 100644
@@ -1008,7 +1008,7 @@ static __always_inline bool free_pages_prepare(struct page *page,
 			(page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
 		}
 	}
-	if (PageAnonHead(page))
+	if (PageMappingFlag(page))
 		page->mapping = NULL;
 	if (check_free)
 		bad += free_pages_check(page);
mm/util.c +4 -2
index 224d36e43a94..a04ccff7cc17 100644
@@ -399,10 +399,12 @@ struct address_space *page_mapping(struct page *page)
 	}
 
 	mapping = page->mapping;
-	if ((unsigned long)mapping & PAGE_MAPPING_FLAGS)
+	if ((unsigned long)mapping & PAGE_MAPPING_ANON)
 		return NULL;
-	return mapping;
+
+	return (void *)((unsigned long)mapping & ~PAGE_MAPPING_FLAGS);
 }
+EXPORT_SYMBOL(page_mapping);
 
 /* Slow path of page_mapcount() for compound pages */
 int __page_mapcount(struct page *page)
Signature · 6 lines
1.9.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#4 lore raw

Re: [PATCH v6 02/12] mm: migrate: support non-lru movable page migration

Quoted 56 lines · Minchan Kim

We have allowed migration for only LRU pages until now and it was enough to make high-order pages. But recently, embedded system(e.g., webOS, android) uses lots of non-movable pages(e.g., zram, GPU memory) so we have seen several reports about troubles of small high-order allocation. For fixing the problem, there were several efforts (e,g,. enhance compaction algorithm, SLUB fallback to 0-order page, reserved memory, vmalloc and so on) but if there are lots of non-movable pages in system, their solutions are void in the long run.

So, this patch is to support facility to change non-movable pages with movable. For the feature, this patch introduces functions related to migration to address_space_operations as well as some page flags.

If a driver want to make own pages movable, it should define three functions which are function pointers of struct address_space_operations.

1. bool (*isolate_page) (struct page *page, isolate_mode_t mode);

What VM expects on isolate_page function of driver is to return *true* if driver isolates page successfully. On returing true, VM marks the page as PG_isolated so concurrent isolation in several CPUs skip the page for isolation. If a driver cannot isolate the page, it should return *false*.

Once page is successfully isolated, VM uses page.lru fields so driver shouldn't expect to preserve values in that fields.

2. int (*migratepage) (struct address_space *mapping,

		struct page *newpage, struct page *oldpage, enum migrate_mode);

After isolation, VM calls migratepage of driver with isolated page. The function of migratepage is to move content of the old page to new page and set up fields of struct page newpage. Keep in mind that you should clear PG_movable of oldpage via __ClearPageMovable under page_lock if you migrated the oldpage successfully and returns 0.
If driver cannot migrate the page at the moment, driver can return -EAGAIN. On -EAGAIN, VM will retry page migration in a short time because VM interprets -EAGAIN as "temporal migration failure". On returning any error except -EAGAIN, VM will give up the page migration without retrying in this time.

Driver shouldn't touch page.lru field VM using in the functions.

3. void (*putback_page)(struct page *);

If migration fails on isolated page, VM should return the isolated page to the driver so VM calls driver's putback_page with migration failed page. In this function, driver should put the isolated page back to the own data structure.

4. non-lru movable page flags

There are two page flags for supporting non-lru movable page.

* PG_movable

Driver should use the below function to make page movable under page_lock.

Quoted 8 lines

void __SetPageMovable(struct page *page, struct address_space *mapping)

It needs argument of address_space for registering migration family functions which will be called by VM. Exactly speaking, PG_movable is not a real flag of struct page. Rather than, VM reuses page->mapping's lower bits to represent it.

	#define PAGE_MAPPING_MOVABLE 0x2
	page->mapping = page->mapping | PAGE_MAPPING_MOVABLE;

Interesting, let's see how that works out...

Overal this looks much better than the last version I checked!

[...]

Quoted 18 lines
include/linux/page-flags.h +4 -3
@@ -357,29 +360,37 @@ PAGEFLAG(Idle, idle, PF_ANY)
  * with the PAGE_MAPPING_ANON bit set to distinguish it.  See rmap.h.
  *
  * On an anonymous page in a VM_MERGEABLE area, if CONFIG_KSM is enabled,
- * the PAGE_MAPPING_KSM bit may be set along with the PAGE_MAPPING_ANON bit;
- * and then page->mapping points, not to an anon_vma, but to a private
+ * the PAGE_MAPPING_MOVABLE bit may be set along with the PAGE_MAPPING_ANON
+ * bit; and then page->mapping points, not to an anon_vma, but to a private
  * structure which KSM associates with that merged page.  See ksm.h.
  *
- * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is currently never used.
+ * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is used for non-lru movable
+ * page and then page->mapping points a struct address_space.
  *
  * Please note that, confusingly, "page_mapping" refers to the inode
  * address_space which maps the page from disk; whereas "page_mapped"
  * refers to user virtual address space into which the page is mapped.
  */

include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 379

Quoted 10 lines
include/linux/page-flags.h +5 -4
-#define PAGE_MAPPING_ANON	1
-#define PAGE_MAPPING_KSM	2
-#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM)
+#define PAGE_MAPPING_ANON	0x1
+#define PAGE_MAPPING_MOVABLE	0x2
+#define PAGE_MAPPING_KSM	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
+#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
 
-static __always_inline int PageAnonHead(struct page *page)
+static __always_inline int PageMappingFlag(struct page *page)

PageMappingFlags()?

[...]

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 88

Quoted 13 lines
mm/compaction.c +5 -0
index 1427366ad673..2d6862d0df60 100644
@@ -81,6 +81,41 @@ static inline bool migrate_async_suitable(int migratetype)
 
 #ifdef CONFIG_COMPACTION
 
+int PageMovable(struct page *page)
+{
+	struct address_space *mapping;
+
+	WARN_ON(!PageLocked(page));

Why not VM_BUG_ON_PAGE as elsewhere?

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 90

Quoted 2 lines
mm/compaction.c +2 -0
+	if (!__PageMovable(page))
+		goto out;

Just return 0.

Quoted 13 lines
mm/compaction.c +13 -0
+
+	mapping = page_mapping(page);
+	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
+		return 1;
+out:
+	return 0;
+}
+EXPORT_SYMBOL(PageMovable);
+
+void __SetPageMovable(struct page *page, struct address_space *mapping)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 113

Quoted 10 lines
mm/compaction.c +10 -0
+	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
+}
+EXPORT_SYMBOL(__SetPageMovable);
+
+void __ClearPageMovable(struct page *page)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE(!PageMovable(page), page);
+	VM_BUG_ON_PAGE(!((unsigned long)page->mapping & PAGE_MAPPING_MOVABLE),
+				page);

The last line sounds redundant, PageMovable() already checked this via __PageMovable()

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 115

Quoted 2 lines
mm/compaction.c +2 -0
+	page->mapping = (void *)((unsigned long)page->mapping &
+				PAGE_MAPPING_MOVABLE);

This should be negated to clear... use ~PAGE_MAPPING_MOVABLE ?

Quoted 13 lines
mm/compaction.c +3 -3
+}
+EXPORT_SYMBOL(__ClearPageMovable);
+
 /* Do not skip compaction more than 64 times */
 #define COMPACT_MAX_DEFER_SHIFT 6
 
@@ -735,21 +770,6 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 		}
 
 		/*
-		 * Check may be lockless but that's ok as we recheck later.
-		 * It's possible to migrate LRU pages and balloon pages
-		 * Skip any other type of page

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 750

Quoted 10 lines
mm/compaction.c +0 -10
-		 */
-		is_lru = PageLRU(page);
-		if (!is_lru) {
-			if (unlikely(balloon_page_movable(page))) {
-				if (balloon_page_isolate(page)) {
-					/* Successfully isolated */
-					goto isolate_success;
-				}
-			}
-		}

So this effectively prevents movable compound pages from being migrated. Are you sure no users of this functionality are going to have compound pages? I assumed that they could, and so made the code like this, with the is_lru variable (which is redundant after your change).

Quoted 22 lines
mm/compaction.c +13 -2
-		/*
 		 * Regardless of being on LRU, compound pages such as THP and
 		 * hugetlbfs are not to be compacted. We can potentially save
 		 * a lot of iterations if we skip them at once. The check is
@@ -765,8 +785,38 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 			goto isolate_fail;
 		}
 
-		if (!is_lru)
+		/*
+		 * Check may be lockless but that's ok as we recheck later.
+		 * It's possible to migrate LRU and non-lru movable pages.
+		 * Skip any other type of page
+		 */
+		is_lru = PageLRU(page);
+		if (!is_lru) {
+			if (unlikely(balloon_page_movable(page))) {
+				if (balloon_page_isolate(page)) {
+					/* Successfully isolated */
+					goto isolate_success;
+				}
+			}

[...]

Quoted 31 lines
mm/compaction.c +31 -0
+bool isolate_movable_page(struct page *page, isolate_mode_t mode)
+{
+	struct address_space *mapping;
+
+	/*
+	 * Avoid burning cycles with pages that are yet under __free_pages(),
+	 * or just got freed under us.
+	 *
+	 * In case we 'win' a race for a movable page being freed under us and
+	 * raise its refcount preventing __free_pages() from doing its job
+	 * the put_page() at the end of this block will take care of
+	 * release this page, thus avoiding a nasty leakage.
+	 */
+	if (unlikely(!get_page_unless_zero(page)))
+		goto out;
+
+	/*
+	 * Check PageMovable before holding a PG_lock because page's owner
+	 * assumes anybody doesn't touch PG_lock of newly allocated page
+	 * so unconditionally grapping the lock ruins page's owner side.
+	 */
+	if (unlikely(!__PageMovable(page)))
+		goto out_putpage;
+	/*
+	 * As movable pages are not isolated from LRU lists, concurrent
+	 * compaction threads can race against page migration functions
+	 * as well as race against the releasing a page.
+	 *
+	 * In order to avoid having an already isolated movable page
+	 * being (wrongly) re-isolated while it is under migration,
+	 * or to avoid attempting to isolate pages being released,

mm/compaction.c

Quoted 10 lines
mm/compaction.c +10 -0
+	 * lets be sure we have the page lock
+	 * before proceeding with the movable page isolation steps.
+	 */
+	if (unlikely(!trylock_page(page)))
+		goto out_putpage;
+
+	if (!PageMovable(page) || PageIsolated(page))
+		goto out_no_isolated;
+
+	mapping = page_mapping(page);

Hmm so on first tail page of a THP compound page, page->mapping will alias with compound_mapcount. That can easily have a value matching PageMovable flags and we'll proceed and start inspecting the compound head in page_mapping()... maybe it's not a big deal, or we better check and skip PageTail first, must think about it more...

[...]

Quoted 19 lines
mm/compaction.c +2 -8
@@ -755,33 +844,69 @@ static int move_to_new_page(struct page *newpage, struct page *page,
 				enum migrate_mode mode)
 {
 	struct address_space *mapping;
-	int rc;
+	int rc = -EAGAIN;
+	bool is_lru = !__PageMovable(page);
 
 	VM_BUG_ON_PAGE(!PageLocked(page), page);
 	VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
 
 	mapping = page_mapping(page);
-	if (!mapping)
-		rc = migrate_page(mapping, newpage, page, mode);
-	else if (mapping->a_ops->migratepage)
-		/*
-		 * Most pages have a mapping and most filesystems provide a
-		 * migratepage callback. Anonymous pages are part of swap
-		 * space which also has its own migratepage callback. This

mm/compaction.c static int move_to_new_page(struct page *newpage, struct page *page, · line 858

Quoted 10 lines
mm/compaction.c +5 -5
-		 * is the most common path for page migration.
-		 */
-		rc = mapping->a_ops->migratepage(mapping, newpage, page, mode);
-	else
-		rc = fallback_migrate_page(mapping, newpage, page, mode);
+	/*
+	 * In case of non-lru page, it could be released after
+	 * isolation step. In that case, we shouldn't try
+	 * fallback migration which is designed for LRU pages.
+	 */

Hmm but is_lru was determined from !__PageMovable() above, also well after the isolation step. So if the driver already released it, we wouldn't detect it? And this function is all under same page lock, so if __PageMovable was true above, so will be PageMovable below?

Quoted 21 lines
mm/compaction.c +21 -0
+	if (unlikely(!is_lru)) {
+		VM_BUG_ON_PAGE(!PageIsolated(page), page);
+		if (!PageMovable(page)) {
+			rc = MIGRATEPAGE_SUCCESS;
+			__ClearPageIsolated(page);
+			goto out;
+		}
+	}
+
+	if (likely(is_lru)) {
+		if (!mapping)
+			rc = migrate_page(mapping, newpage, page, mode);
+		else if (mapping->a_ops->migratepage)
+			/*
+			 * Most pages have a mapping and most filesystems
+			 * provide a migratepage callback. Anonymous pages
+			 * are part of swap space which also has its own
+			 * migratepage callback. This is the most common path
+			 * for page migration.
+			 */
+			rc = mapping->a_ops->migratepage(mapping, newpage,

mm/compaction.c static int move_to_new_page(struct page *newpage, struct page *page, · line 889

Quoted 10 lines
mm/compaction.c +10 -0
+							page, mode);
+		else
+			rc = fallback_migrate_page(mapping, newpage,
+							page, mode);
+	} else {
+		rc = mapping->a_ops->migratepage(mapping, newpage,
+						page, mode);
+		WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
+			!PageIsolated(page));
+	}

Why split the !is_lru handling in two places?

Quoted 9 lines
mm/compaction.c +2 -1
 	/*
 	 * When successful, old pagecache page->mapping must be cleared before
 	 * page is freed; but stats require that PageAnon be left as PageAnon.
 	 */
 	if (rc == MIGRATEPAGE_SUCCESS) {
-		if (!PageAnon(page))
+		if (__PageMovable(page)) {
+			VM_BUG_ON_PAGE(!PageIsolated(page), page);

mm/compaction.c static int move_to_new_page(struct page *newpage, struct page *page, · line 906

Quoted 10 lines
mm/compaction.c +9 -0
+
+			/*
+			 * We clear PG_movable under page_lock so any compactor
+			 * cannot try to migrate this page.
+			 */
+			__ClearPageIsolated(page);
+		}
+
+		if (!((unsigned long)page->mapping & PAGE_MAPPING_FLAGS))
 			page->mapping = NULL;

The two lines above make little sense to me without a comment. Should the condition be negated, even?

#5 lore raw

Re: [PATCH v6 02/12] mm: migrate: support non-lru movable page migration

In reply to Minchan Kim
Quoted 56 lines · Minchan Kim

We have allowed migration for only LRU pages until now and it was enough to make high-order pages. But recently, embedded system(e.g., webOS, android) uses lots of non-movable pages(e.g., zram, GPU memory) so we have seen several reports about troubles of small high-order allocation. For fixing the problem, there were several efforts (e,g,. enhance compaction algorithm, SLUB fallback to 0-order page, reserved memory, vmalloc and so on) but if there are lots of non-movable pages in system, their solutions are void in the long run.

So, this patch is to support facility to change non-movable pages with movable. For the feature, this patch introduces functions related to migration to address_space_operations as well as some page flags.

If a driver want to make own pages movable, it should define three functions which are function pointers of struct address_space_operations.

1. bool (*isolate_page) (struct page *page, isolate_mode_t mode);

What VM expects on isolate_page function of driver is to return *true* if driver isolates page successfully. On returing true, VM marks the page as PG_isolated so concurrent isolation in several CPUs skip the page for isolation. If a driver cannot isolate the page, it should return *false*.

Once page is successfully isolated, VM uses page.lru fields so driver shouldn't expect to preserve values in that fields.

2. int (*migratepage) (struct address_space *mapping,

		struct page *newpage, struct page *oldpage, enum migrate_mode);

After isolation, VM calls migratepage of driver with isolated page. The function of migratepage is to move content of the old page to new page and set up fields of struct page newpage. Keep in mind that you should clear PG_movable of oldpage via __ClearPageMovable under page_lock if you migrated the oldpage successfully and returns 0.
If driver cannot migrate the page at the moment, driver can return -EAGAIN. On -EAGAIN, VM will retry page migration in a short time because VM interprets -EAGAIN as "temporal migration failure". On returning any error except -EAGAIN, VM will give up the page migration without retrying in this time.

Driver shouldn't touch page.lru field VM using in the functions.

3. void (*putback_page)(struct page *);

If migration fails on isolated page, VM should return the isolated page to the driver so VM calls driver's putback_page with migration failed page. In this function, driver should put the isolated page back to the own data structure.

4. non-lru movable page flags

There are two page flags for supporting non-lru movable page.

* PG_movable

Driver should use the below function to make page movable under page_lock.

Quoted 8 lines

void __SetPageMovable(struct page *page, struct address_space *mapping)

It needs argument of address_space for registering migration family functions which will be called by VM. Exactly speaking, PG_movable is not a real flag of struct page. Rather than, VM reuses page->mapping's lower bits to represent it.

	#define PAGE_MAPPING_MOVABLE 0x2
	page->mapping = page->mapping | PAGE_MAPPING_MOVABLE;

Interesting, let's see how that works out...

Overal this looks much better than the last version I checked!

[...]

Quoted 18 lines
include/linux/page-flags.h +4 -3
@@ -357,29 +360,37 @@ PAGEFLAG(Idle, idle, PF_ANY)
  * with the PAGE_MAPPING_ANON bit set to distinguish it.  See rmap.h.
  *
  * On an anonymous page in a VM_MERGEABLE area, if CONFIG_KSM is enabled,
- * the PAGE_MAPPING_KSM bit may be set along with the PAGE_MAPPING_ANON bit;
- * and then page->mapping points, not to an anon_vma, but to a private
+ * the PAGE_MAPPING_MOVABLE bit may be set along with the PAGE_MAPPING_ANON
+ * bit; and then page->mapping points, not to an anon_vma, but to a private
  * structure which KSM associates with that merged page.  See ksm.h.
  *
- * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is currently never used.
+ * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is used for non-lru movable
+ * page and then page->mapping points a struct address_space.
  *
  * Please note that, confusingly, "page_mapping" refers to the inode
  * address_space which maps the page from disk; whereas "page_mapped"
  * refers to user virtual address space into which the page is mapped.
  */

include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 379

Quoted 10 lines
include/linux/page-flags.h +5 -4
-#define PAGE_MAPPING_ANON	1
-#define PAGE_MAPPING_KSM	2
-#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM)
+#define PAGE_MAPPING_ANON	0x1
+#define PAGE_MAPPING_MOVABLE	0x2
+#define PAGE_MAPPING_KSM	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
+#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
 
-static __always_inline int PageAnonHead(struct page *page)
+static __always_inline int PageMappingFlag(struct page *page)

PageMappingFlags()?

[...]

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 88

Quoted 13 lines
mm/compaction.c +5 -0
index 1427366ad673..2d6862d0df60 100644
@@ -81,6 +81,41 @@ static inline bool migrate_async_suitable(int migratetype)
 
 #ifdef CONFIG_COMPACTION
 
+int PageMovable(struct page *page)
+{
+	struct address_space *mapping;
+
+	WARN_ON(!PageLocked(page));

Why not VM_BUG_ON_PAGE as elsewhere?

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 90

Quoted 2 lines
mm/compaction.c +2 -0
+	if (!__PageMovable(page))
+		goto out;

Just return 0.

Quoted 13 lines
mm/compaction.c +13 -0
+
+	mapping = page_mapping(page);
+	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
+		return 1;
+out:
+	return 0;
+}
+EXPORT_SYMBOL(PageMovable);
+
+void __SetPageMovable(struct page *page, struct address_space *mapping)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 113

Quoted 10 lines
mm/compaction.c +10 -0
+	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
+}
+EXPORT_SYMBOL(__SetPageMovable);
+
+void __ClearPageMovable(struct page *page)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE(!PageMovable(page), page);
+	VM_BUG_ON_PAGE(!((unsigned long)page->mapping & PAGE_MAPPING_MOVABLE),
+				page);

The last line sounds redundant, PageMovable() already checked this via __PageMovable()

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 115

Quoted 2 lines
mm/compaction.c +2 -0
+	page->mapping = (void *)((unsigned long)page->mapping &
+				PAGE_MAPPING_MOVABLE);

This should be negated to clear... use ~PAGE_MAPPING_MOVABLE ?

Quoted 13 lines
mm/compaction.c +3 -3
+}
+EXPORT_SYMBOL(__ClearPageMovable);
+
 /* Do not skip compaction more than 64 times */
 #define COMPACT_MAX_DEFER_SHIFT 6
 
@@ -735,21 +770,6 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 		}
 
 		/*
-		 * Check may be lockless but that's ok as we recheck later.
-		 * It's possible to migrate LRU pages and balloon pages
-		 * Skip any other type of page

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 750

Quoted 10 lines
mm/compaction.c +0 -10
-		 */
-		is_lru = PageLRU(page);
-		if (!is_lru) {
-			if (unlikely(balloon_page_movable(page))) {
-				if (balloon_page_isolate(page)) {
-					/* Successfully isolated */
-					goto isolate_success;
-				}
-			}
-		}

So this effectively prevents movable compound pages from being migrated. Are you sure no users of this functionality are going to have compound pages? I assumed that they could, and so made the code like this, with the is_lru variable (which is redundant after your change).

Quoted 22 lines
mm/compaction.c +13 -2
-		/*
 		 * Regardless of being on LRU, compound pages such as THP and
 		 * hugetlbfs are not to be compacted. We can potentially save
 		 * a lot of iterations if we skip them at once. The check is
@@ -765,8 +785,38 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 			goto isolate_fail;
 		}
 
-		if (!is_lru)
+		/*
+		 * Check may be lockless but that's ok as we recheck later.
+		 * It's possible to migrate LRU and non-lru movable pages.
+		 * Skip any other type of page
+		 */
+		is_lru = PageLRU(page);
+		if (!is_lru) {
+			if (unlikely(balloon_page_movable(page))) {
+				if (balloon_page_isolate(page)) {
+					/* Successfully isolated */
+					goto isolate_success;
+				}
+			}

[...]

Quoted 31 lines
mm/compaction.c +31 -0
+bool isolate_movable_page(struct page *page, isolate_mode_t mode)
+{
+	struct address_space *mapping;
+
+	/*
+	 * Avoid burning cycles with pages that are yet under __free_pages(),
+	 * or just got freed under us.
+	 *
+	 * In case we 'win' a race for a movable page being freed under us and
+	 * raise its refcount preventing __free_pages() from doing its job
+	 * the put_page() at the end of this block will take care of
+	 * release this page, thus avoiding a nasty leakage.
+	 */
+	if (unlikely(!get_page_unless_zero(page)))
+		goto out;
+
+	/*
+	 * Check PageMovable before holding a PG_lock because page's owner
+	 * assumes anybody doesn't touch PG_lock of newly allocated page
+	 * so unconditionally grapping the lock ruins page's owner side.
+	 */
+	if (unlikely(!__PageMovable(page)))
+		goto out_putpage;
+	/*
+	 * As movable pages are not isolated from LRU lists, concurrent
+	 * compaction threads can race against page migration functions
+	 * as well as race against the releasing a page.
+	 *
+	 * In order to avoid having an already isolated movable page
+	 * being (wrongly) re-isolated while it is under migration,
+	 * or to avoid attempting to isolate pages being released,

mm/compaction.c

Quoted 10 lines
mm/compaction.c +10 -0
+	 * lets be sure we have the page lock
+	 * before proceeding with the movable page isolation steps.
+	 */
+	if (unlikely(!trylock_page(page)))
+		goto out_putpage;
+
+	if (!PageMovable(page) || PageIsolated(page))
+		goto out_no_isolated;
+
+	mapping = page_mapping(page);

Hmm so on first tail page of a THP compound page, page->mapping will alias with compound_mapcount. That can easily have a value matching PageMovable flags and we'll proceed and start inspecting the compound head in page_mapping()... maybe it's not a big deal, or we better check and skip PageTail first, must think about it more...

[...]

Quoted 19 lines
mm/compaction.c +2 -8
@@ -755,33 +844,69 @@ static int move_to_new_page(struct page *newpage, struct page *page,
 				enum migrate_mode mode)
 {
 	struct address_space *mapping;
-	int rc;
+	int rc = -EAGAIN;
+	bool is_lru = !__PageMovable(page);
 
 	VM_BUG_ON_PAGE(!PageLocked(page), page);
 	VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
 
 	mapping = page_mapping(page);
-	if (!mapping)
-		rc = migrate_page(mapping, newpage, page, mode);
-	else if (mapping->a_ops->migratepage)
-		/*
-		 * Most pages have a mapping and most filesystems provide a
-		 * migratepage callback. Anonymous pages are part of swap
-		 * space which also has its own migratepage callback. This

mm/compaction.c static int move_to_new_page(struct page *newpage, struct page *page, · line 858

Quoted 10 lines
mm/compaction.c +5 -5
-		 * is the most common path for page migration.
-		 */
-		rc = mapping->a_ops->migratepage(mapping, newpage, page, mode);
-	else
-		rc = fallback_migrate_page(mapping, newpage, page, mode);
+	/*
+	 * In case of non-lru page, it could be released after
+	 * isolation step. In that case, we shouldn't try
+	 * fallback migration which is designed for LRU pages.
+	 */

Hmm but is_lru was determined from !__PageMovable() above, also well after the isolation step. So if the driver already released it, we wouldn't detect it? And this function is all under same page lock, so if __PageMovable was true above, so will be PageMovable below?

Quoted 21 lines
mm/compaction.c +21 -0
+	if (unlikely(!is_lru)) {
+		VM_BUG_ON_PAGE(!PageIsolated(page), page);
+		if (!PageMovable(page)) {
+			rc = MIGRATEPAGE_SUCCESS;
+			__ClearPageIsolated(page);
+			goto out;
+		}
+	}
+
+	if (likely(is_lru)) {
+		if (!mapping)
+			rc = migrate_page(mapping, newpage, page, mode);
+		else if (mapping->a_ops->migratepage)
+			/*
+			 * Most pages have a mapping and most filesystems
+			 * provide a migratepage callback. Anonymous pages
+			 * are part of swap space which also has its own
+			 * migratepage callback. This is the most common path
+			 * for page migration.
+			 */
+			rc = mapping->a_ops->migratepage(mapping, newpage,

mm/compaction.c static int move_to_new_page(struct page *newpage, struct page *page, · line 889

Quoted 10 lines
mm/compaction.c +10 -0
+							page, mode);
+		else
+			rc = fallback_migrate_page(mapping, newpage,
+							page, mode);
+	} else {
+		rc = mapping->a_ops->migratepage(mapping, newpage,
+						page, mode);
+		WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
+			!PageIsolated(page));
+	}

Why split the !is_lru handling in two places?

Quoted 9 lines
mm/compaction.c +2 -1
 	/*
 	 * When successful, old pagecache page->mapping must be cleared before
 	 * page is freed; but stats require that PageAnon be left as PageAnon.
 	 */
 	if (rc == MIGRATEPAGE_SUCCESS) {
-		if (!PageAnon(page))
+		if (__PageMovable(page)) {
+			VM_BUG_ON_PAGE(!PageIsolated(page), page);

mm/compaction.c static int move_to_new_page(struct page *newpage, struct page *page, · line 906

Quoted 10 lines
mm/compaction.c +9 -0
+
+			/*
+			 * We clear PG_movable under page_lock so any compactor
+			 * cannot try to migrate this page.
+			 */
+			__ClearPageIsolated(page);
+		}
+
+		if (!((unsigned long)page->mapping & PAGE_MAPPING_FLAGS))
 			page->mapping = NULL;

The two lines above make little sense to me without a comment. Should the condition be negated, even?

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

author maintainer
#6 lore raw

Re: [PATCH v6 02/12] mm: migrate: support non-lru movable page migration

Quoted 63 lines · Vlastimil Babka
Quoted 62 lines · Minchan Kim

We have allowed migration for only LRU pages until now and it was enough to make high-order pages. But recently, embedded system(e.g., webOS, android) uses lots of non-movable pages(e.g., zram, GPU memory) so we have seen several reports about troubles of small high-order allocation. For fixing the problem, there were several efforts (e,g,. enhance compaction algorithm, SLUB fallback to 0-order page, reserved memory, vmalloc and so on) but if there are lots of non-movable pages in system, their solutions are void in the long run.

So, this patch is to support facility to change non-movable pages with movable. For the feature, this patch introduces functions related to migration to address_space_operations as well as some page flags.

If a driver want to make own pages movable, it should define three functions which are function pointers of struct address_space_operations.

1. bool (*isolate_page) (struct page *page, isolate_mode_t mode);

What VM expects on isolate_page function of driver is to return *true* if driver isolates page successfully. On returing true, VM marks the page as PG_isolated so concurrent isolation in several CPUs skip the page for isolation. If a driver cannot isolate the page, it should return *false*.

Once page is successfully isolated, VM uses page.lru fields so driver shouldn't expect to preserve values in that fields.

2. int (*migratepage) (struct address_space *mapping,

		struct page *newpage, struct page *oldpage, enum migrate_mode);

After isolation, VM calls migratepage of driver with isolated page. The function of migratepage is to move content of the old page to new page and set up fields of struct page newpage. Keep in mind that you should clear PG_movable of oldpage via __ClearPageMovable under page_lock if you migrated the oldpage successfully and returns 0.
If driver cannot migrate the page at the moment, driver can return -EAGAIN. On -EAGAIN, VM will retry page migration in a short time because VM interprets -EAGAIN as "temporal migration failure". On returning any error except -EAGAIN, VM will give up the page migration without retrying in this time.

Driver shouldn't touch page.lru field VM using in the functions.

3. void (*putback_page)(struct page *);

If migration fails on isolated page, VM should return the isolated page to the driver so VM calls driver's putback_page with migration failed page. In this function, driver should put the isolated page back to the own data structure.

4. non-lru movable page flags

There are two page flags for supporting non-lru movable page.

* PG_movable

Driver should use the below function to make page movable under page_lock.

void __SetPageMovable(struct page *page, struct address_space *mapping)

It needs argument of address_space for registering migration family functions which will be called by VM. Exactly speaking, PG_movable is not a real flag of struct page. Rather than, VM reuses page->mapping's lower bits to represent it.

Quoted 6 lines
Quoted 2 lines
	#define PAGE_MAPPING_MOVABLE 0x2
	page->mapping = page->mapping | PAGE_MAPPING_MOVABLE;

Interesting, let's see how that works out...

Overal this looks much better than the last version I checked!

Thanks.

Quoted 29 lines

[...]

Quoted 26 lines
include/linux/page-flags.h +8 -6
@@ -357,29 +360,37 @@ PAGEFLAG(Idle, idle, PF_ANY)
 * with the PAGE_MAPPING_ANON bit set to distinguish it.  See rmap.h.
 *
 * On an anonymous page in a VM_MERGEABLE area, if CONFIG_KSM is enabled,
- * the PAGE_MAPPING_KSM bit may be set along with the PAGE_MAPPING_ANON bit;
- * and then page->mapping points, not to an anon_vma, but to a private
+ * the PAGE_MAPPING_MOVABLE bit may be set along with the PAGE_MAPPING_ANON
+ * bit; and then page->mapping points, not to an anon_vma, but to a private
 * structure which KSM associates with that merged page.  See ksm.h.
 *
- * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is currently never used.
+ * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is used for non-lru movable
+ * page and then page->mapping points a struct address_space.
 *
 * Please note that, confusingly, "page_mapping" refers to the inode
 * address_space which maps the page from disk; whereas "page_mapped"
 * refers to user virtual address space into which the page is mapped.
 */
-#define PAGE_MAPPING_ANON	1
-#define PAGE_MAPPING_KSM	2
-#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM)
+#define PAGE_MAPPING_ANON	0x1
+#define PAGE_MAPPING_MOVABLE	0x2
+#define PAGE_MAPPING_KSM	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
+#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
 
Quoted 4 lines

include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 379

Quoted 2 lines
include/linux/page-flags.h +1 -1
-static __always_inline int PageAnonHead(struct page *page)
+static __always_inline int PageMappingFlag(struct page *page)

PageMappingFlags()?

Yeb.

Quoted 11 lines

[...]

Quoted 8 lines
mm/compaction.c +0 -0
index 1427366ad673..2d6862d0df60 100644
@@ -81,6 +81,41 @@ static inline bool migrate_async_suitable(int migratetype)
 
 #ifdef CONFIG_COMPACTION
 
Quoted 7 lines

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 88

Quoted 5 lines
mm/compaction.c +5 -0
+int PageMovable(struct page *page)
+{
+	struct address_space *mapping;
+
+	WARN_ON(!PageLocked(page));

Why not VM_BUG_ON_PAGE as elsewhere?

I have backported this patchset to huge old kernel which doesn't have VM_BUG_ON_PAGE and forgot to correct it beforre sending mainline.

Thanks.

Quoted 5 lines

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 90

Quoted 3 lines
mm/compaction.c +2 -0
+	if (!__PageMovable(page))
+		goto out;

Just return 0.

Maybe I love goto. You realized me. I will split up will her.

Quoted 19 lines
Quoted 19 lines
mm/compaction.c +18 -0
+
+	mapping = page_mapping(page);
+	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
+		return 1;
+out:
+	return 0;
+}
+EXPORT_SYMBOL(PageMovable);
+
+void __SetPageMovable(struct page *page, struct address_space *mapping)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
+	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
+}
+EXPORT_SYMBOL(__SetPageMovable);
+
+void __ClearPageMovable(struct page *page)
Quoted 8 lines

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 113

Quoted 5 lines
mm/compaction.c +5 -0
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE(!PageMovable(page), page);
+	VM_BUG_ON_PAGE(!((unsigned long)page->mapping & PAGE_MAPPING_MOVABLE),
+				page);

The last line sounds redundant, PageMovable() already checked this via __PageMovable()

Yeb.

Quoted 6 lines

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 115

Quoted 4 lines
mm/compaction.c +2 -0
+	page->mapping = (void *)((unsigned long)page->mapping &
+				PAGE_MAPPING_MOVABLE);

This should be negated to clear... use ~PAGE_MAPPING_MOVABLE ?

No.

The intention is to clear only mapping value but PAGE_MAPPING_MOVABLE flag. So, any new migration trial will be failed because PageMovable checks page's mapping value but ongoing migraion handling can catch whether it's movable page or not with the type bit.

For example, we need to keep the type bit to handle putback the page. With parallel freeing(e.g., __ClearPageMovable) from the owner after isolating the page, migration will be failed and put it back. Then, we need to identify movable to clear PG_isolate and shouldn't call mapping->a_ops->putback_page. For that, we need to keep the type bit until the page is return to buddy.

Quoted 22 lines
Quoted 22 lines
mm/compaction.c +3 -11
+}
+EXPORT_SYMBOL(__ClearPageMovable);
+
 /* Do not skip compaction more than 64 times */
 #define COMPACT_MAX_DEFER_SHIFT 6
 
@@ -735,21 +770,6 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 		}
 
 		/*
-		 * Check may be lockless but that's ok as we recheck later.
-		 * It's possible to migrate LRU pages and balloon pages
-		 * Skip any other type of page
-		 */
-		is_lru = PageLRU(page);
-		if (!is_lru) {
-			if (unlikely(balloon_page_movable(page))) {
-				if (balloon_page_isolate(page)) {
-					/* Successfully isolated */
-					goto isolate_success;
-				}
Quoted 8 lines

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 750

Quoted 2 lines
mm/compaction.c +0 -2
-			}
-		}

So this effectively prevents movable compound pages from being migrated. Are you sure no users of this functionality are going to have compound pages? I assumed that they could, and so made the code like this, with the is_lru variable (which is redundant after your change).

This implementation at the moment disables effectively non-lru compound page migration but I'm not a god so I can't make sure no one doesn't want it in future. If someone want it, we can support it then because this work doesn't prevent it by design.

Quoted 65 lines
Quoted 23 lines
mm/compaction.c +13 -2
-		/*
 		 * Regardless of being on LRU, compound pages such as THP and
 		 * hugetlbfs are not to be compacted. We can potentially save
 		 * a lot of iterations if we skip them at once. The check is
@@ -765,8 +785,38 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 			goto isolate_fail;
 		}
 
-		if (!is_lru)
+		/*
+		 * Check may be lockless but that's ok as we recheck later.
+		 * It's possible to migrate LRU and non-lru movable pages.
+		 * Skip any other type of page
+		 */
+		is_lru = PageLRU(page);
+		if (!is_lru) {
+			if (unlikely(balloon_page_movable(page))) {
+				if (balloon_page_isolate(page)) {
+					/* Successfully isolated */
+					goto isolate_success;
+				}
+			}

[...]

Quoted 39 lines
mm/compaction.c +39 -0
+bool isolate_movable_page(struct page *page, isolate_mode_t mode)
+{
+	struct address_space *mapping;
+
+	/*
+	 * Avoid burning cycles with pages that are yet under __free_pages(),
+	 * or just got freed under us.
+	 *
+	 * In case we 'win' a race for a movable page being freed under us and
+	 * raise its refcount preventing __free_pages() from doing its job
+	 * the put_page() at the end of this block will take care of
+	 * release this page, thus avoiding a nasty leakage.
+	 */
+	if (unlikely(!get_page_unless_zero(page)))
+		goto out;
+
+	/*
+	 * Check PageMovable before holding a PG_lock because page's owner
+	 * assumes anybody doesn't touch PG_lock of newly allocated page
+	 * so unconditionally grapping the lock ruins page's owner side.
+	 */
+	if (unlikely(!__PageMovable(page)))
+		goto out_putpage;
+	/*
+	 * As movable pages are not isolated from LRU lists, concurrent
+	 * compaction threads can race against page migration functions
+	 * as well as race against the releasing a page.
+	 *
+	 * In order to avoid having an already isolated movable page
+	 * being (wrongly) re-isolated while it is under migration,
+	 * or to avoid attempting to isolate pages being released,
+	 * lets be sure we have the page lock
+	 * before proceeding with the movable page isolation steps.
+	 */
+	if (unlikely(!trylock_page(page)))
+		goto out_putpage;
+
+	if (!PageMovable(page) || PageIsolated(page))
+		goto out_no_isolated;
Quoted 8 lines

mm/compaction.c

Quoted 2 lines
mm/compaction.c +2 -0
+
+	mapping = page_mapping(page);

Hmm so on first tail page of a THP compound page, page->mapping will alias with compound_mapcount. That can easily have a value matching PageMovable flags and we'll proceed and start inspecting the compound head in page_mapping()... maybe it's not a big deal, or we better check and skip PageTail first, must think about it more...

I thouht PageCompound check right before isolate_movable_page in isolate_migratepages_block will filter it out mostly but yeah
it is racy without zone->lru_lock so it could reach to isolate_movable_page. However, PageMovable check in there investigates mapping, mapping->a_ops, and a_ops->isolate_page to verify whether it's movable page or not.

I thought it's sufficient to filter THP page.

Quoted 29 lines

[...]

Quoted 26 lines
mm/compaction.c +4 -13
@@ -755,33 +844,69 @@ static int move_to_new_page(struct page *newpage, struct page *page,
 				enum migrate_mode mode)
 {
 	struct address_space *mapping;
-	int rc;
+	int rc = -EAGAIN;
+	bool is_lru = !__PageMovable(page);
 
 	VM_BUG_ON_PAGE(!PageLocked(page), page);
 	VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
 
 	mapping = page_mapping(page);
-	if (!mapping)
-		rc = migrate_page(mapping, newpage, page, mode);
-	else if (mapping->a_ops->migratepage)
-		/*
-		 * Most pages have a mapping and most filesystems provide a
-		 * migratepage callback. Anonymous pages are part of swap
-		 * space which also has its own migratepage callback. This
-		 * is the most common path for page migration.
-		 */
-		rc = mapping->a_ops->migratepage(mapping, newpage, page, mode);
-	else
-		rc = fallback_migrate_page(mapping, newpage, page, mode);
+	/*
+	 * In case of non-lru page, it could be released after
Quoted 8 lines

mm/compaction.c static int move_to_new_page(struct page *newpage, struct page *page, · line 858

Quoted 3 lines
mm/compaction.c +3 -0
+	 * isolation step. In that case, we shouldn't try
+	 * fallback migration which is designed for LRU pages.
+	 */

Hmm but is_lru was determined from !__PageMovable() above, also well after the isolation step. So if the driver already released it, we wouldn't detect it? And this function is all under same page lock, so if __PageMovable was true above, so will be PageMovable below?

You are missing what I mentioned above.
We should keep the type bit to catch what you are saying(i.e., driver already released).

__PageMovable just checks PAGE_MAPPING_MOVABLE flag and PageMovable checks page->mapping valid while __ClearPageMovable reset only valid vaule of mapping, not PAGE_MAPPING_MOVABLE flag.

I wrote it down in Documentation/vm/page_migration.

"For testing of non-lru movable page, VM supports __PageMovable function. However, it doesn't guarantee to identify non-lru movable page because page->mapping field is unified with other variables in struct page. As well, if driver releases the page after isolation by VM, page->mapping doesn't have stable value although it has PAGE_MAPPING_MOVABLE (Look at __ClearPageMovable). But __PageMovable is cheap to catch whether page is LRU or non-lru movable once the page has been isolated. Because LRU pages never can have PAGE_MAPPING_MOVABLE in page->mapping. It is also good for just peeking to test non-lru movable pages before more expensive checking with lock_page in pfn scanning to select victim.

For guaranteeing non-lru movable page, VM provides PageMovable function. Unlike __PageMovable, PageMovable functions validates page->mapping and mapping->a_ops->isolate_page under lock_page. The lock_page prevents sudden destroying of page->mapping.

Driver using __SetPageMovable should clear the flag via __ClearMovablePage under page_lock before the releasing the page."

Quoted 26 lines
Quoted 26 lines
mm/compaction.c +25 -0
+	if (unlikely(!is_lru)) {
+		VM_BUG_ON_PAGE(!PageIsolated(page), page);
+		if (!PageMovable(page)) {
+			rc = MIGRATEPAGE_SUCCESS;
+			__ClearPageIsolated(page);
+			goto out;
+		}
+	}
+
+	if (likely(is_lru)) {
+		if (!mapping)
+			rc = migrate_page(mapping, newpage, page, mode);
+		else if (mapping->a_ops->migratepage)
+			/*
+			 * Most pages have a mapping and most filesystems
+			 * provide a migratepage callback. Anonymous pages
+			 * are part of swap space which also has its own
+			 * migratepage callback. This is the most common path
+			 * for page migration.
+			 */
+			rc = mapping->a_ops->migratepage(mapping, newpage,
+							page, mode);
+		else
+			rc = fallback_migrate_page(mapping, newpage,
+							page, mode);
Quoted 8 lines

mm/compaction.c static int move_to_new_page(struct page *newpage, struct page *page, · line 889

Quoted 6 lines
mm/compaction.c +6 -0
+	} else {
+		rc = mapping->a_ops->migratepage(mapping, newpage,
+						page, mode);
+		WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
+			!PageIsolated(page));
+	}

Why split the !is_lru handling in two places?

Fixed.

Quoted 14 lines
Quoted 14 lines
mm/compaction.c +6 -1
 	/*
 	 * When successful, old pagecache page->mapping must be cleared before
 	 * page is freed; but stats require that PageAnon be left as PageAnon.
 	 */
 	if (rc == MIGRATEPAGE_SUCCESS) {
-		if (!PageAnon(page))
+		if (__PageMovable(page)) {
+			VM_BUG_ON_PAGE(!PageIsolated(page), page);
+
+			/*
+			 * We clear PG_movable under page_lock so any compactor
+			 * cannot try to migrate this page.
Quoted 8 lines

mm/compaction.c static int move_to_new_page(struct page *newpage, struct page *page, · line 906

Quoted 6 lines
mm/compaction.c +5 -0
+			 */
+			__ClearPageIsolated(page);
+		}
+
+		if (!((unsigned long)page->mapping & PAGE_MAPPING_FLAGS))
 			page->mapping = NULL;

The two lines above make little sense to me without a comment.

I folded this.

@@ -901,7 +901,12 @@ static int move_to_new_page(struct page *newpage, struct page *page,

                        __ClearPageIsolated(page);
                }

- if (!((unsigned long)page->mapping & PAGE_MAPPING_FLAGS))
+ /*

+                * Anonymous and movable page->mapping will be cleard by
+                * free_pages_prepare so don't reset it here for keeping
+                * the type to work PageAnon, for example.
+                */
+               if (!PageMappingFlags(page))
                        page->mapping = NULL;
        }

out:

Quoted 1 line

Should the condition be negated, even?

No.

Quoted 2 lines

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#7 lore raw

Re: [PATCH v6 02/12] mm: migrate: support non-lru movable page migration

Quoted 7 lines · Minchan Kim
Quoted 6 lines

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 115

Quoted 4 lines
mm/compaction.c +2 -0
+	page->mapping = (void *)((unsigned long)page->mapping &
+				PAGE_MAPPING_MOVABLE);

This should be negated to clear... use ~PAGE_MAPPING_MOVABLE ?

Quoted 6 lines

No.

The intention is to clear only mapping value but PAGE_MAPPING_MOVABLE flag. So, any new migration trial will be failed because PageMovable checks page's mapping value but ongoing migraion handling can catch whether it's movable page or not with the type bit.

Oh, OK, I got that wrong. I'll point out in the reply to the v6v2 what misled me :)

Quoted 11 lines
Quoted 6 lines

So this effectively prevents movable compound pages from being migrated. Are you sure no users of this functionality are going to have compound pages? I assumed that they could, and so made the code like this, with the is_lru variable (which is redundant after your change).

This implementation at the moment disables effectively non-lru compound page migration but I'm not a god so I can't make sure no one doesn't want it in future. If someone want it, we can support it then because this work doesn't prevent it by design.

Oh well. As long as the balloon pages or zsmalloc don't already use compound pages...

Quoted 8 lines

I thouht PageCompound check right before isolate_movable_page in isolate_migratepages_block will filter it out mostly but yeah
it is racy without zone->lru_lock so it could reach to isolate_movable_page. However, PageMovable check in there investigates mapping, mapping->a_ops, and a_ops->isolate_page to verify whether it's movable page or not.

I thought it's sufficient to filter THP page.

I guess, yeah.

Quoted 59 lines
Quoted 29 lines

[...]

Quoted 26 lines
mm/migrate.c +4 -13
@@ -755,33 +844,69 @@ static int move_to_new_page(struct page *newpage, struct page *page,
 				enum migrate_mode mode)
 {
 	struct address_space *mapping;
-	int rc;
+	int rc = -EAGAIN;
+	bool is_lru = !__PageMovable(page);
 
 	VM_BUG_ON_PAGE(!PageLocked(page), page);
 	VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
 
 	mapping = page_mapping(page);
-	if (!mapping)
-		rc = migrate_page(mapping, newpage, page, mode);
-	else if (mapping->a_ops->migratepage)
-		/*
-		 * Most pages have a mapping and most filesystems provide a
-		 * migratepage callback. Anonymous pages are part of swap
-		 * space which also has its own migratepage callback. This
-		 * is the most common path for page migration.
-		 */
-		rc = mapping->a_ops->migratepage(mapping, newpage, page, mode);
-	else
-		rc = fallback_migrate_page(mapping, newpage, page, mode);
+	/*
+	 * In case of non-lru page, it could be released after
Quoted 8 lines

mm/migrate.c static int move_to_new_page(struct page *newpage, struct page *page, · line 858

Quoted 3 lines
mm/migrate.c +3 -0
+	 * isolation step. In that case, we shouldn't try
+	 * fallback migration which is designed for LRU pages.
+	 */

Hmm but is_lru was determined from !__PageMovable() above, also well after the isolation step. So if the driver already released it, we wouldn't detect it? And this function is all under same page lock, so if __PageMovable was true above, so will be PageMovable below?

You are missing what I mentioned above.
We should keep the type bit to catch what you are saying(i.e., driver already released).

__PageMovable just checks PAGE_MAPPING_MOVABLE flag and PageMovable checks page->mapping valid while __ClearPageMovable reset only valid vaule of mapping, not PAGE_MAPPING_MOVABLE flag.

I wrote it down in Documentation/vm/page_migration.

"For testing of non-lru movable page, VM supports __PageMovable function. However, it doesn't guarantee to identify non-lru movable page because page->mapping field is unified with other variables in struct page. As well, if driver releases the page after isolation by VM, page->mapping doesn't have stable value although it has PAGE_MAPPING_MOVABLE (Look at __ClearPageMovable). But __PageMovable is cheap to catch whether page is LRU or non-lru movable once the page has been isolated. Because LRU pages never can have PAGE_MAPPING_MOVABLE in page->mapping. It is also good for just peeking to test non-lru movable pages before more expensive checking with lock_page in pfn scanning to select victim.

Quoted 7 lines

For guaranteeing non-lru movable page, VM provides PageMovable function. Unlike __PageMovable, PageMovable functions validates page->mapping and mapping->a_ops->isolate_page under lock_page. The lock_page prevents sudden destroying of page->mapping.

Driver using __SetPageMovable should clear the flag via __ClearMovablePage under page_lock before the releasing the page."

Right, I get it now.

Quoted 10 lines
Quoted 4 lines

mm/migrate.c static int move_to_new_page(struct page *newpage, struct page *page, · line 907

Quoted 2 lines
mm/migrate.c +1 -0
+		if (!((unsigned long)page->mapping & PAGE_MAPPING_FLAGS))
 			page->mapping = NULL;

The two lines above make little sense to me without a comment.

I folded this.

(quoted diff) +0 -0
@@ -901,7 +901,12 @@ static int move_to_new_page(struct page *newpage, struct page *page,
                        __ClearPageIsolated(page);
                }

Review static int move_to_new_page(struct page *newpage, struct page *page, · line 911

Quoted 10 lines
(quoted diff) +6 -1
 
-               if (!((unsigned long)page->mapping & PAGE_MAPPING_FLAGS))
+               /*
+                * Anonymous and movable page->mapping will be cleard by
+                * free_pages_prepare so don't reset it here for keeping
+                * the type to work PageAnon, for example.
+                */
+               if (!PageMappingFlags(page))
                        page->mapping = NULL;
        }

Thanks.

author maintainer
#8 lore raw

Re: [PATCH v6 02/12] mm: migrate: support non-lru movable page migration

In reply to Vlastimil Babka
Quoted 63 lines · Vlastimil Babka
Quoted 62 lines · Minchan Kim

We have allowed migration for only LRU pages until now and it was enough to make high-order pages. But recently, embedded system(e.g., webOS, android) uses lots of non-movable pages(e.g., zram, GPU memory) so we have seen several reports about troubles of small high-order allocation. For fixing the problem, there were several efforts (e,g,. enhance compaction algorithm, SLUB fallback to 0-order page, reserved memory, vmalloc and so on) but if there are lots of non-movable pages in system, their solutions are void in the long run.

So, this patch is to support facility to change non-movable pages with movable. For the feature, this patch introduces functions related to migration to address_space_operations as well as some page flags.

If a driver want to make own pages movable, it should define three functions which are function pointers of struct address_space_operations.

1. bool (*isolate_page) (struct page *page, isolate_mode_t mode);

What VM expects on isolate_page function of driver is to return *true* if driver isolates page successfully. On returing true, VM marks the page as PG_isolated so concurrent isolation in several CPUs skip the page for isolation. If a driver cannot isolate the page, it should return *false*.

Once page is successfully isolated, VM uses page.lru fields so driver shouldn't expect to preserve values in that fields.

2. int (*migratepage) (struct address_space *mapping,

		struct page *newpage, struct page *oldpage, enum migrate_mode);

After isolation, VM calls migratepage of driver with isolated page. The function of migratepage is to move content of the old page to new page and set up fields of struct page newpage. Keep in mind that you should clear PG_movable of oldpage via __ClearPageMovable under page_lock if you migrated the oldpage successfully and returns 0.
If driver cannot migrate the page at the moment, driver can return -EAGAIN. On -EAGAIN, VM will retry page migration in a short time because VM interprets -EAGAIN as "temporal migration failure". On returning any error except -EAGAIN, VM will give up the page migration without retrying in this time.

Driver shouldn't touch page.lru field VM using in the functions.

3. void (*putback_page)(struct page *);

If migration fails on isolated page, VM should return the isolated page to the driver so VM calls driver's putback_page with migration failed page. In this function, driver should put the isolated page back to the own data structure.

4. non-lru movable page flags

There are two page flags for supporting non-lru movable page.

* PG_movable

Driver should use the below function to make page movable under page_lock.

void __SetPageMovable(struct page *page, struct address_space *mapping)

It needs argument of address_space for registering migration family functions which will be called by VM. Exactly speaking, PG_movable is not a real flag of struct page. Rather than, VM reuses page->mapping's lower bits to represent it.

Quoted 6 lines
Quoted 2 lines
	#define PAGE_MAPPING_MOVABLE 0x2
	page->mapping = page->mapping | PAGE_MAPPING_MOVABLE;

Interesting, let's see how that works out...

Overal this looks much better than the last version I checked!

Thanks.

Quoted 29 lines

[...]

Quoted 26 lines
include/linux/page-flags.h +8 -6
@@ -357,29 +360,37 @@ PAGEFLAG(Idle, idle, PF_ANY)
 * with the PAGE_MAPPING_ANON bit set to distinguish it.  See rmap.h.
 *
 * On an anonymous page in a VM_MERGEABLE area, if CONFIG_KSM is enabled,
- * the PAGE_MAPPING_KSM bit may be set along with the PAGE_MAPPING_ANON bit;
- * and then page->mapping points, not to an anon_vma, but to a private
+ * the PAGE_MAPPING_MOVABLE bit may be set along with the PAGE_MAPPING_ANON
+ * bit; and then page->mapping points, not to an anon_vma, but to a private
 * structure which KSM associates with that merged page.  See ksm.h.
 *
- * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is currently never used.
+ * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is used for non-lru movable
+ * page and then page->mapping points a struct address_space.
 *
 * Please note that, confusingly, "page_mapping" refers to the inode
 * address_space which maps the page from disk; whereas "page_mapped"
 * refers to user virtual address space into which the page is mapped.
 */
-#define PAGE_MAPPING_ANON	1
-#define PAGE_MAPPING_KSM	2
-#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM)
+#define PAGE_MAPPING_ANON	0x1
+#define PAGE_MAPPING_MOVABLE	0x2
+#define PAGE_MAPPING_KSM	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
+#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
 
Quoted 4 lines

include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 379

Quoted 2 lines
include/linux/page-flags.h +1 -1
-static __always_inline int PageAnonHead(struct page *page)
+static __always_inline int PageMappingFlag(struct page *page)

PageMappingFlags()?

Yeb.

Quoted 11 lines

[...]

Quoted 8 lines
mm/compaction.c +0 -0
index 1427366ad673..2d6862d0df60 100644
@@ -81,6 +81,41 @@ static inline bool migrate_async_suitable(int migratetype)
 
 #ifdef CONFIG_COMPACTION
 
Quoted 7 lines

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 88

Quoted 5 lines
mm/compaction.c +5 -0
+int PageMovable(struct page *page)
+{
+	struct address_space *mapping;
+
+	WARN_ON(!PageLocked(page));

Why not VM_BUG_ON_PAGE as elsewhere?

I have backported this patchset to huge old kernel which doesn't have VM_BUG_ON_PAGE and forgot to correct it beforre sending mainline.

Thanks.

Quoted 5 lines

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 90

Quoted 3 lines
mm/compaction.c +2 -0
+	if (!__PageMovable(page))
+		goto out;

Just return 0.

Maybe I love goto. You realized me. I will split up will her.

Quoted 19 lines
Quoted 19 lines
mm/compaction.c +18 -0
+
+	mapping = page_mapping(page);
+	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
+		return 1;
+out:
+	return 0;
+}
+EXPORT_SYMBOL(PageMovable);
+
+void __SetPageMovable(struct page *page, struct address_space *mapping)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
+	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
+}
+EXPORT_SYMBOL(__SetPageMovable);
+
+void __ClearPageMovable(struct page *page)
Quoted 8 lines

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 113

Quoted 5 lines
mm/compaction.c +5 -0
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE(!PageMovable(page), page);
+	VM_BUG_ON_PAGE(!((unsigned long)page->mapping & PAGE_MAPPING_MOVABLE),
+				page);

The last line sounds redundant, PageMovable() already checked this via __PageMovable()

Yeb.

Quoted 6 lines

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 115

Quoted 4 lines
mm/compaction.c +2 -0
+	page->mapping = (void *)((unsigned long)page->mapping &
+				PAGE_MAPPING_MOVABLE);

This should be negated to clear... use ~PAGE_MAPPING_MOVABLE ?

No.

The intention is to clear only mapping value but PAGE_MAPPING_MOVABLE flag. So, any new migration trial will be failed because PageMovable checks page's mapping value but ongoing migraion handling can catch whether it's movable page or not with the type bit.

For example, we need to keep the type bit to handle putback the page. With parallel freeing(e.g., __ClearPageMovable) from the owner after isolating the page, migration will be failed and put it back. Then, we need to identify movable to clear PG_isolate and shouldn't call mapping->a_ops->putback_page. For that, we need to keep the type bit until the page is return to buddy.

Quoted 22 lines
Quoted 22 lines
mm/compaction.c +3 -11
+}
+EXPORT_SYMBOL(__ClearPageMovable);
+
 /* Do not skip compaction more than 64 times */
 #define COMPACT_MAX_DEFER_SHIFT 6
 
@@ -735,21 +770,6 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 		}
 
 		/*
-		 * Check may be lockless but that's ok as we recheck later.
-		 * It's possible to migrate LRU pages and balloon pages
-		 * Skip any other type of page
-		 */
-		is_lru = PageLRU(page);
-		if (!is_lru) {
-			if (unlikely(balloon_page_movable(page))) {
-				if (balloon_page_isolate(page)) {
-					/* Successfully isolated */
-					goto isolate_success;
-				}
Quoted 8 lines

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 750

Quoted 2 lines
mm/compaction.c +0 -2
-			}
-		}

So this effectively prevents movable compound pages from being migrated. Are you sure no users of this functionality are going to have compound pages? I assumed that they could, and so made the code like this, with the is_lru variable (which is redundant after your change).

This implementation at the moment disables effectively non-lru compound page migration but I'm not a god so I can't make sure no one doesn't want it in future. If someone want it, we can support it then because this work doesn't prevent it by design.

Quoted 65 lines
Quoted 23 lines
mm/compaction.c +13 -2
-		/*
 		 * Regardless of being on LRU, compound pages such as THP and
 		 * hugetlbfs are not to be compacted. We can potentially save
 		 * a lot of iterations if we skip them at once. The check is
@@ -765,8 +785,38 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 			goto isolate_fail;
 		}
 
-		if (!is_lru)
+		/*
+		 * Check may be lockless but that's ok as we recheck later.
+		 * It's possible to migrate LRU and non-lru movable pages.
+		 * Skip any other type of page
+		 */
+		is_lru = PageLRU(page);
+		if (!is_lru) {
+			if (unlikely(balloon_page_movable(page))) {
+				if (balloon_page_isolate(page)) {
+					/* Successfully isolated */
+					goto isolate_success;
+				}
+			}

[...]

Quoted 39 lines
mm/compaction.c +39 -0
+bool isolate_movable_page(struct page *page, isolate_mode_t mode)
+{
+	struct address_space *mapping;
+
+	/*
+	 * Avoid burning cycles with pages that are yet under __free_pages(),
+	 * or just got freed under us.
+	 *
+	 * In case we 'win' a race for a movable page being freed under us and
+	 * raise its refcount preventing __free_pages() from doing its job
+	 * the put_page() at the end of this block will take care of
+	 * release this page, thus avoiding a nasty leakage.
+	 */
+	if (unlikely(!get_page_unless_zero(page)))
+		goto out;
+
+	/*
+	 * Check PageMovable before holding a PG_lock because page's owner
+	 * assumes anybody doesn't touch PG_lock of newly allocated page
+	 * so unconditionally grapping the lock ruins page's owner side.
+	 */
+	if (unlikely(!__PageMovable(page)))
+		goto out_putpage;
+	/*
+	 * As movable pages are not isolated from LRU lists, concurrent
+	 * compaction threads can race against page migration functions
+	 * as well as race against the releasing a page.
+	 *
+	 * In order to avoid having an already isolated movable page
+	 * being (wrongly) re-isolated while it is under migration,
+	 * or to avoid attempting to isolate pages being released,
+	 * lets be sure we have the page lock
+	 * before proceeding with the movable page isolation steps.
+	 */
+	if (unlikely(!trylock_page(page)))
+		goto out_putpage;
+
+	if (!PageMovable(page) || PageIsolated(page))
+		goto out_no_isolated;
Quoted 8 lines

mm/compaction.c

Quoted 2 lines
mm/compaction.c +2 -0
+
+	mapping = page_mapping(page);

Hmm so on first tail page of a THP compound page, page->mapping will alias with compound_mapcount. That can easily have a value matching PageMovable flags and we'll proceed and start inspecting the compound head in page_mapping()... maybe it's not a big deal, or we better check and skip PageTail first, must think about it more...

I thouht PageCompound check right before isolate_movable_page in isolate_migratepages_block will filter it out mostly but yeah
it is racy without zone->lru_lock so it could reach to isolate_movable_page. However, PageMovable check in there investigates mapping, mapping->a_ops, and a_ops->isolate_page to verify whether it's movable page or not.

I thought it's sufficient to filter THP page.

Quoted 29 lines

[...]

Quoted 26 lines
mm/compaction.c +4 -13
@@ -755,33 +844,69 @@ static int move_to_new_page(struct page *newpage, struct page *page,
 				enum migrate_mode mode)
 {
 	struct address_space *mapping;
-	int rc;
+	int rc = -EAGAIN;
+	bool is_lru = !__PageMovable(page);
 
 	VM_BUG_ON_PAGE(!PageLocked(page), page);
 	VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
 
 	mapping = page_mapping(page);
-	if (!mapping)
-		rc = migrate_page(mapping, newpage, page, mode);
-	else if (mapping->a_ops->migratepage)
-		/*
-		 * Most pages have a mapping and most filesystems provide a
-		 * migratepage callback. Anonymous pages are part of swap
-		 * space which also has its own migratepage callback. This
-		 * is the most common path for page migration.
-		 */
-		rc = mapping->a_ops->migratepage(mapping, newpage, page, mode);
-	else
-		rc = fallback_migrate_page(mapping, newpage, page, mode);
+	/*
+	 * In case of non-lru page, it could be released after
Quoted 8 lines

mm/compaction.c static int move_to_new_page(struct page *newpage, struct page *page, · line 858

Quoted 3 lines
mm/compaction.c +3 -0
+	 * isolation step. In that case, we shouldn't try
+	 * fallback migration which is designed for LRU pages.
+	 */

Hmm but is_lru was determined from !__PageMovable() above, also well after the isolation step. So if the driver already released it, we wouldn't detect it? And this function is all under same page lock, so if __PageMovable was true above, so will be PageMovable below?

You are missing what I mentioned above.
We should keep the type bit to catch what you are saying(i.e., driver already released).

__PageMovable just checks PAGE_MAPPING_MOVABLE flag and PageMovable checks page->mapping valid while __ClearPageMovable reset only valid vaule of mapping, not PAGE_MAPPING_MOVABLE flag.

I wrote it down in Documentation/vm/page_migration.

"For testing of non-lru movable page, VM supports __PageMovable function. However, it doesn't guarantee to identify non-lru movable page because page->mapping field is unified with other variables in struct page. As well, if driver releases the page after isolation by VM, page->mapping doesn't have stable value although it has PAGE_MAPPING_MOVABLE (Look at __ClearPageMovable). But __PageMovable is cheap to catch whether page is LRU or non-lru movable once the page has been isolated. Because LRU pages never can have PAGE_MAPPING_MOVABLE in page->mapping. It is also good for just peeking to test non-lru movable pages before more expensive checking with lock_page in pfn scanning to select victim.

For guaranteeing non-lru movable page, VM provides PageMovable function. Unlike __PageMovable, PageMovable functions validates page->mapping and mapping->a_ops->isolate_page under lock_page. The lock_page prevents sudden destroying of page->mapping.

Driver using __SetPageMovable should clear the flag via __ClearMovablePage under page_lock before the releasing the page."

Quoted 26 lines
Quoted 26 lines
mm/compaction.c +25 -0
+	if (unlikely(!is_lru)) {
+		VM_BUG_ON_PAGE(!PageIsolated(page), page);
+		if (!PageMovable(page)) {
+			rc = MIGRATEPAGE_SUCCESS;
+			__ClearPageIsolated(page);
+			goto out;
+		}
+	}
+
+	if (likely(is_lru)) {
+		if (!mapping)
+			rc = migrate_page(mapping, newpage, page, mode);
+		else if (mapping->a_ops->migratepage)
+			/*
+			 * Most pages have a mapping and most filesystems
+			 * provide a migratepage callback. Anonymous pages
+			 * are part of swap space which also has its own
+			 * migratepage callback. This is the most common path
+			 * for page migration.
+			 */
+			rc = mapping->a_ops->migratepage(mapping, newpage,
+							page, mode);
+		else
+			rc = fallback_migrate_page(mapping, newpage,
+							page, mode);
Quoted 8 lines

mm/compaction.c static int move_to_new_page(struct page *newpage, struct page *page, · line 889

Quoted 6 lines
mm/compaction.c +6 -0
+	} else {
+		rc = mapping->a_ops->migratepage(mapping, newpage,
+						page, mode);
+		WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
+			!PageIsolated(page));
+	}

Why split the !is_lru handling in two places?

Fixed.

Quoted 14 lines
Quoted 14 lines
mm/compaction.c +6 -1
 	/*
 	 * When successful, old pagecache page->mapping must be cleared before
 	 * page is freed; but stats require that PageAnon be left as PageAnon.
 	 */
 	if (rc == MIGRATEPAGE_SUCCESS) {
-		if (!PageAnon(page))
+		if (__PageMovable(page)) {
+			VM_BUG_ON_PAGE(!PageIsolated(page), page);
+
+			/*
+			 * We clear PG_movable under page_lock so any compactor
+			 * cannot try to migrate this page.
Quoted 8 lines

mm/compaction.c static int move_to_new_page(struct page *newpage, struct page *page, · line 906

Quoted 6 lines
mm/compaction.c +5 -0
+			 */
+			__ClearPageIsolated(page);
+		}
+
+		if (!((unsigned long)page->mapping & PAGE_MAPPING_FLAGS))
 			page->mapping = NULL;

The two lines above make little sense to me without a comment.

I folded this.

@@ -901,7 +901,12 @@ static int move_to_new_page(struct page *newpage, struct page *page,

                        __ClearPageIsolated(page);
                }

- if (!((unsigned long)page->mapping & PAGE_MAPPING_FLAGS))
+ /*

+                * Anonymous and movable page->mapping will be cleard by
+                * free_pages_prepare so don't reset it here for keeping
+                * the type to work PageAnon, for example.
+                */
+               if (!PageMappingFlags(page))
                        page->mapping = NULL;
        }

out:

Quoted 1 line

Should the condition be negated, even?

No.

Quoted 2 lines
author maintainer
#9 lore raw

PATCH v6v2 02/12] mm: migrate: support non-lru movable page migration

In reply to Minchan Kim

Per Vlastimil's review comment,

Vlastimil, I updated based on your comment. Please review this. If everything is done, I will send v7 rebased on recent mmotm.

Thanks for the review!

#10 lore raw

Re: PATCH v6v2 02/12] mm: migrate: support non-lru movable page migration

Quoted 5 lines · Minchan Kim

After isolation, VM calls migratepage of driver with isolated page. The function of migratepage is to move content of the old page to new page and set up fields of struct page newpage. Keep in mind that you should clear PG_movable of oldpage via __ClearPageMovable under page_lock if you migrated the oldpage successfully and returns 0.

This "clear PG_movable" is one of the reasons I was confused about what __ClearPageMovable() really does. There's no actual "PG_movable" page flag and the function doesn't clear even the actual mapping flag :) Also same thing in the Documentation/ part.

Something like "... you should indicate to the VM that the oldpage is no longer movable via __ClearPageMovable() ..."?

Quoted 28 lines
mm/compaction.c +22 -0
@@ -81,6 +81,39 @@ static inline bool migrate_async_suitable(int migratetype)
 
 #ifdef CONFIG_COMPACTION
 
+int PageMovable(struct page *page)
+{
+	struct address_space *mapping;
+
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	if (!__PageMovable(page))
+		return 0;
+
+	mapping = page_mapping(page);
+	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(PageMovable);
+
+void __SetPageMovable(struct page *page, struct address_space *mapping)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
+	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
+}

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 115

Quoted 10 lines
mm/compaction.c +10 -0
+EXPORT_SYMBOL(__SetPageMovable);
+
+void __ClearPageMovable(struct page *page)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE(!PageMovable(page), page);
+	page->mapping = (void *)((unsigned long)page->mapping &
+				PAGE_MAPPING_MOVABLE);
+}
+EXPORT_SYMBOL(__ClearPageMovable);

The second confusing thing is that the function is named __ClearPageMovable(), but what it really clears is the mapping pointer, which is not at all the opposite of what __SetPageMovable() does.

I know it's explained in the documentation, but it also deserves a comment here so it doesn't confuse everyone who looks at it. Even better would be a less confusing name for the function, but I can't offer one right now.

mm/util.c struct address_space *page_mapping(struct page *page) · line 401

Quoted 8 lines
mm/util.c +0 -0
index 917e0e3d0f8e..b756ee36f7f0 100644
@@ -399,10 +399,12 @@ struct address_space *page_mapping(struct page *page)
 	}
 
 	mapping = page->mapping;

I'd probably use READ_ONCE() here to be safe. Not all callers are under page lock?

mm/util.c struct address_space *page_mapping(struct page *page) · line 410

Quoted 12 lines
mm/util.c +4 -2
-	if ((unsigned long)mapping & PAGE_MAPPING_FLAGS)
+	if ((unsigned long)mapping & PAGE_MAPPING_ANON)
 		return NULL;
-	return mapping;
+
+	return (void *)((unsigned long)mapping & ~PAGE_MAPPING_FLAGS);
 }
+EXPORT_SYMBOL(page_mapping);
 
 /* Slow path of page_mapcount() for compound pages */
 int __page_mapcount(struct page *page)

--
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>

author maintainer
#11 lore raw

Re: PATCH v6v2 02/12] mm: migrate: support non-lru movable page migration

Quoted 7 lines · Vlastimil Babka
Quoted 5 lines · Minchan Kim

After isolation, VM calls migratepage of driver with isolated page. The function of migratepage is to move content of the old page to new page and set up fields of struct page newpage. Keep in mind that you should clear PG_movable of oldpage via __ClearPageMovable under page_lock if you migrated the oldpage successfully and returns 0.

Quoted 7 lines

This "clear PG_movable" is one of the reasons I was confused about what __ClearPageMovable() really does. There's no actual "PG_movable" page flag and the function doesn't clear even the actual mapping flag :) Also same thing in the Documentation/ part.

Something like "... you should indicate to the VM that the oldpage is no longer movable via __ClearPageMovable() ..."?

It's better. I will change it.

Quoted 45 lines
Quoted 29 lines
mm/compaction.c +22 -0
@@ -81,6 +81,39 @@ static inline bool migrate_async_suitable(int migratetype)
 
 #ifdef CONFIG_COMPACTION
 
+int PageMovable(struct page *page)
+{
+	struct address_space *mapping;
+
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	if (!__PageMovable(page))
+		return 0;
+
+	mapping = page_mapping(page);
+	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(PageMovable);
+
+void __SetPageMovable(struct page *page, struct address_space *mapping)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
+	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
+}

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 115

Quoted 10 lines
mm/compaction.c +10 -0
+EXPORT_SYMBOL(__SetPageMovable);
+
+void __ClearPageMovable(struct page *page)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE(!PageMovable(page), page);
+	page->mapping = (void *)((unsigned long)page->mapping &
+				PAGE_MAPPING_MOVABLE);
+}
+EXPORT_SYMBOL(__ClearPageMovable);

The second confusing thing is that the function is named __ClearPageMovable(), but what it really clears is the mapping pointer,
which is not at all the opposite of what __SetPageMovable() does.

Quoted 4 lines

I know it's explained in the documentation, but it also deserves a comment here so it doesn't confuse everyone who looks at it. Even better would be a less confusing name for the function, but I can't offer one right now.

To me, __ClearPageMovable naming is suitable for user POV. It effectively makes the page unmovable. The confusion is just caused by the implementation and I don't prefer exported API depends on the implementation. So I want to add just comment.

I didn't add comment above the function because I don't want to export internal implementation to the user. I think they don't need to know it.

index a7df2ae71f2a..d1d2063b4fd9 100644

mm/compaction.c +5 -0
@@ -108,6 +108,11 @@ void __ClearPageMovable(struct page *page)
 {
        VM_BUG_ON_PAGE(!PageLocked(page), page);
        VM_BUG_ON_PAGE(!PageMovable(page), page);
+       /*
+        * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
+        * flag so that VM can catch up released page by driver after isolation.
+        * With it, VM migration doesn't try to put it back.
+        */
        page->mapping = (void *)((unsigned long)page->mapping &
                                PAGE_MAPPING_MOVABLE);
 
Quoted 8 lines
Quoted 8 lines
mm/util.c +0 -0
index 917e0e3d0f8e..b756ee36f7f0 100644
@@ -399,10 +399,12 @@ struct address_space *page_mapping(struct page *page)
 	}
 
Quoted 4 lines

mm/util.c struct address_space *page_mapping(struct page *page) · line 401

Quoted 1 line
mm/util.c +0 -0
 	mapping = page->mapping;

I'd probably use READ_ONCE() here to be safe. Not all callers are under page lock?

I don't understand. Yeah, all caller are not under page lock but at least, new user of movable pages should call it under page_lock. Yeah, I will write the rule down in document.
In this case, what kinds of problem do you see?

Quoted 10 lines
Quoted 10 lines
mm/util.c +4 -2
-	if ((unsigned long)mapping & PAGE_MAPPING_FLAGS)
+	if ((unsigned long)mapping & PAGE_MAPPING_ANON)
 		return NULL;
-	return mapping;
+
+	return (void *)((unsigned long)mapping & ~PAGE_MAPPING_FLAGS);
 }
+EXPORT_SYMBOL(page_mapping);
 
Quoted 4 lines

mm/util.c struct address_space *page_mapping(struct page *page) · line 410

Quoted 3 lines
mm/util.c +0 -0
 /* Slow path of page_mapcount() for compound pages */
 int __page_mapcount(struct page *page)

--
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>

#12 lore raw

Re: PATCH v6v2 02/12] mm: migrate: support non-lru movable page migration

Quoted 61 lines · Minchan Kim
Quoted 44 lines
Quoted 28 lines
mm/compaction.c +22 -0
@@ -81,6 +81,39 @@ static inline bool migrate_async_suitable(int migratetype)
 
 #ifdef CONFIG_COMPACTION
 
+int PageMovable(struct page *page)
+{
+	struct address_space *mapping;
+
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	if (!__PageMovable(page))
+		return 0;
+
+	mapping = page_mapping(page);
+	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(PageMovable);
+
+void __SetPageMovable(struct page *page, struct address_space *mapping)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
+	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
+}

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 115

Quoted 10 lines
mm/compaction.c +10 -0
+EXPORT_SYMBOL(__SetPageMovable);
+
+void __ClearPageMovable(struct page *page)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE(!PageMovable(page), page);
+	page->mapping = (void *)((unsigned long)page->mapping &
+				PAGE_MAPPING_MOVABLE);
+}
+EXPORT_SYMBOL(__ClearPageMovable);

The second confusing thing is that the function is named __ClearPageMovable(), but what it really clears is the mapping pointer,
which is not at all the opposite of what __SetPageMovable() does.

Quoted 4 lines

I know it's explained in the documentation, but it also deserves a comment here so it doesn't confuse everyone who looks at it. Even better would be a less confusing name for the function, but I can't offer one right now.

To me, __ClearPageMovable naming is suitable for user POV. It effectively makes the page unmovable. The confusion is just caused by the implementation and I don't prefer exported API depends on the implementation. So I want to add just comment.

I didn't add comment above the function because I don't want to export internal implementation to the user. I think they don't need to know it.

index a7df2ae71f2a..d1d2063b4fd9 100644

mm/compaction.c void __ClearPageMovable(struct page *page) · line 117

Quoted 10 lines
mm/compaction.c +5 -0
@@ -108,6 +108,11 @@ void __ClearPageMovable(struct page *page)
 {
        VM_BUG_ON_PAGE(!PageLocked(page), page);
        VM_BUG_ON_PAGE(!PageMovable(page), page);
+       /*
+        * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
+        * flag so that VM can catch up released page by driver after isolation.
+        * With it, VM migration doesn't try to put it back.
+        */
        page->mapping = (void *)((unsigned long)page->mapping &
                                PAGE_MAPPING_MOVABLE);

OK, that's fine!

Quoted 13 lines
Quoted 8 lines
Quoted 8 lines
mm/util.c +0 -0
index 917e0e3d0f8e..b756ee36f7f0 100644
@@ -399,10 +399,12 @@ struct address_space *page_mapping(struct page *page)
 	}
 
Quoted 4 lines

mm/util.c struct address_space *page_mapping(struct page *page) · line 401

Quoted 1 line
mm/util.c +0 -0
 	mapping = page->mapping;

I'd probably use READ_ONCE() here to be safe. Not all callers are under page lock?

Quoted 4 lines

I don't understand. Yeah, all caller are not under page lock but at least, new user of movable pages should call it under page_lock. Yeah, I will write the rule down in document.
In this case, what kinds of problem do you see?

After more thinking, probably none. It wouldn't prevent any extra races. Sorry for the noise.

#13 lore raw

Re: PATCH v6v2 02/12] mm: migrate: support non-lru movable page migration

In reply to Minchan Kim
Quoted 61 lines · Minchan Kim
Quoted 44 lines
Quoted 28 lines
mm/compaction.c +22 -0
@@ -81,6 +81,39 @@ static inline bool migrate_async_suitable(int migratetype)
 
 #ifdef CONFIG_COMPACTION
 
+int PageMovable(struct page *page)
+{
+	struct address_space *mapping;
+
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	if (!__PageMovable(page))
+		return 0;
+
+	mapping = page_mapping(page);
+	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(PageMovable);
+
+void __SetPageMovable(struct page *page, struct address_space *mapping)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
+	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
+}

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 115

Quoted 10 lines
mm/compaction.c +10 -0
+EXPORT_SYMBOL(__SetPageMovable);
+
+void __ClearPageMovable(struct page *page)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE(!PageMovable(page), page);
+	page->mapping = (void *)((unsigned long)page->mapping &
+				PAGE_MAPPING_MOVABLE);
+}
+EXPORT_SYMBOL(__ClearPageMovable);

The second confusing thing is that the function is named __ClearPageMovable(), but what it really clears is the mapping pointer,
which is not at all the opposite of what __SetPageMovable() does.

Quoted 4 lines

I know it's explained in the documentation, but it also deserves a comment here so it doesn't confuse everyone who looks at it. Even better would be a less confusing name for the function, but I can't offer one right now.

To me, __ClearPageMovable naming is suitable for user POV. It effectively makes the page unmovable. The confusion is just caused by the implementation and I don't prefer exported API depends on the implementation. So I want to add just comment.

I didn't add comment above the function because I don't want to export internal implementation to the user. I think they don't need to know it.

index a7df2ae71f2a..d1d2063b4fd9 100644

mm/compaction.c void __ClearPageMovable(struct page *page) · line 117

Quoted 10 lines
mm/compaction.c +5 -0
@@ -108,6 +108,11 @@ void __ClearPageMovable(struct page *page)
 {
        VM_BUG_ON_PAGE(!PageLocked(page), page);
        VM_BUG_ON_PAGE(!PageMovable(page), page);
+       /*
+        * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
+        * flag so that VM can catch up released page by driver after isolation.
+        * With it, VM migration doesn't try to put it back.
+        */
        page->mapping = (void *)((unsigned long)page->mapping &
                                PAGE_MAPPING_MOVABLE);

OK, that's fine!

Quoted 13 lines
Quoted 8 lines
Quoted 8 lines
mm/util.c +0 -0
index 917e0e3d0f8e..b756ee36f7f0 100644
@@ -399,10 +399,12 @@ struct address_space *page_mapping(struct page *page)
 	}
 
Quoted 4 lines

mm/util.c struct address_space *page_mapping(struct page *page) · line 401

Quoted 1 line
mm/util.c +0 -0
 	mapping = page->mapping;

I'd probably use READ_ONCE() here to be safe. Not all callers are under page lock?

Quoted 4 lines

I don't understand. Yeah, all caller are not under page lock but at least, new user of movable pages should call it under page_lock. Yeah, I will write the rule down in document.
In this case, what kinds of problem do you see?

After more thinking, probably none. It wouldn't prevent any extra races. Sorry for the noise.

#14 lore raw

Re: PATCH v6v2 02/12] mm: migrate: support non-lru movable page migration

In reply to Minchan Kim
Quoted 5 lines · Minchan Kim

After isolation, VM calls migratepage of driver with isolated page. The function of migratepage is to move content of the old page to new page and set up fields of struct page newpage. Keep in mind that you should clear PG_movable of oldpage via __ClearPageMovable under page_lock if you migrated the oldpage successfully and returns 0.

This "clear PG_movable" is one of the reasons I was confused about what __ClearPageMovable() really does. There's no actual "PG_movable" page flag and the function doesn't clear even the actual mapping flag :) Also same thing in the Documentation/ part.

Something like "... you should indicate to the VM that the oldpage is no longer movable via __ClearPageMovable() ..."?

Quoted 28 lines
mm/compaction.c +22 -0
@@ -81,6 +81,39 @@ static inline bool migrate_async_suitable(int migratetype)
 
 #ifdef CONFIG_COMPACTION
 
+int PageMovable(struct page *page)
+{
+	struct address_space *mapping;
+
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	if (!__PageMovable(page))
+		return 0;
+
+	mapping = page_mapping(page);
+	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(PageMovable);
+
+void __SetPageMovable(struct page *page, struct address_space *mapping)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
+	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
+}

mm/compaction.c static inline bool migrate_async_suitable(int migratetype) · line 115

Quoted 10 lines
mm/compaction.c +10 -0
+EXPORT_SYMBOL(__SetPageMovable);
+
+void __ClearPageMovable(struct page *page)
+{
+	VM_BUG_ON_PAGE(!PageLocked(page), page);
+	VM_BUG_ON_PAGE(!PageMovable(page), page);
+	page->mapping = (void *)((unsigned long)page->mapping &
+				PAGE_MAPPING_MOVABLE);
+}
+EXPORT_SYMBOL(__ClearPageMovable);

The second confusing thing is that the function is named __ClearPageMovable(), but what it really clears is the mapping pointer, which is not at all the opposite of what __SetPageMovable() does.

I know it's explained in the documentation, but it also deserves a comment here so it doesn't confuse everyone who looks at it. Even better would be a less confusing name for the function, but I can't offer one right now.

mm/util.c struct address_space *page_mapping(struct page *page) · line 401

Quoted 8 lines
mm/util.c +0 -0
index 917e0e3d0f8e..b756ee36f7f0 100644
@@ -399,10 +399,12 @@ struct address_space *page_mapping(struct page *page)
 	}
 
 	mapping = page->mapping;

I'd probably use READ_ONCE() here to be safe. Not all callers are under page lock?

Quoted 12 lines
mm/util.c +4 -2
-	if ((unsigned long)mapping & PAGE_MAPPING_FLAGS)
+	if ((unsigned long)mapping & PAGE_MAPPING_ANON)
 		return NULL;
-	return mapping;
+
+	return (void *)((unsigned long)mapping & ~PAGE_MAPPING_FLAGS);
 }
+EXPORT_SYMBOL(page_mapping);
 
 /* Slow path of page_mapcount() for compound pages */
 int __page_mapcount(struct page *page)
author maintainer
#15 lore raw

PATCH v6 2/12 mm: migrate: support non-lru movable page migration

In reply to Minchan Kim

Per Vlastimi's review comment.

Thanks for the detail review, Vlastimi!
If you have another concern, feel free to say.
After I resolve all thing, I will send v7 rebased on recent mmotm.

Acked-by on v6 2/12
#16 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

Quoted 4 lines · Minchan Kim

Per Vlastimi's review comment.

Thanks for the detail review, Vlastimi!
If you have another concern, feel free to say.

I don't for now :)

[...]

Quoted 5 lines
Cc Rik van Riel <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Quoted 8 lines
Cc Rafael Aquini <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc John Einar Reitan <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
author maintainer
#17 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

Quoted 24 lines · Vlastimil Babka
Quoted 4 lines · Minchan Kim

Per Vlastimi's review comment.

Thanks for the detail review, Vlastimi!
If you have another concern, feel free to say.

I don't for now :)

[...]

Quoted 5 lines
Cc Rik van Riel <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Quoted 8 lines
Cc Rafael Aquini <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc John Einar Reitan <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
Quoted 1 line

Thanks for the review, Vlastimil!

--
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>

author maintainer
#18 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

In reply to Vlastimil Babka
Quoted 24 lines · Vlastimil Babka
Quoted 4 lines · Minchan Kim

Per Vlastimi's review comment.

Thanks for the detail review, Vlastimi!
If you have another concern, feel free to say.

I don't for now :)

[...]

Quoted 5 lines
Cc Rik van Riel <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Quoted 8 lines
Cc Rafael Aquini <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc John Einar Reitan <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
Quoted 1 line

Thanks for the review, Vlastimil!

Acked-by on v6 2/12
#19 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

In reply to Minchan Kim
Quoted 4 lines · Minchan Kim

Per Vlastimi's review comment.

Thanks for the detail review, Vlastimi!
If you have another concern, feel free to say.

I don't for now :)

[...]

Quoted 5 lines
Cc Rik van Riel <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Quoted 8 lines
Cc Rafael Aquini <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc John Einar Reitan <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
#20 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

In reply to Minchan Kim
Quoted 9 lines · Minchan Kim
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 8 lines
(quoted diff) +5 -0
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Regards
Anshuman

--
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>

author maintainer
#21 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

Hi,

Quoted 21 lines · Anshuman Khandual
Quoted 9 lines · Minchan Kim
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 8 lines
(quoted diff) +5 -0
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

Quoted 5 lines

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

It would be helpful for merging when I wll send patchset.

Thanks!

--
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>

#22 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

Quoted 29 lines · Minchan Kim

Hi,

Quoted 21 lines · Anshuman Khandual
Quoted 21 lines · Minchan Kim
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
Quoted 5 lines

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 5 lines
(quoted diff) +5 -0
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

Quoted 5 lines
Quoted 5 lines

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Quoted 4 lines

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

Sure.

My driver has privately managed non LRU pages which gets mapped into user space process page table through f_ops->mmap() and vmops->fault() which then updates the file RMAP (page->mapping->i_mmap) through page_add_file_rmap(page). One thing to note here is that the page->mapping eventually points to struct address_space (file->f_mapping) which belongs to the character device file (created using mknod) which we are using for establishing the mmap() regions in the user space.

Now as per this new framework, all the page's are to be made __SetPageMovable before passing the list down to migrate_pages(). Now __SetPageMovable() takes *new* struct address_space as an argument and replaces the existing page->mapping. Now thats the problem, we have lost all our connection to the existing file RMAP information. This stands as a problem when we try to migrate these non LRU pages which are PTE mapped. The rmap_walk_file() never finds them in the VMA, skips all the migrate PTE steps and then the migration eventually fails.

Seems like assigning a new struct address_space to the page through __SetPageMovable() is the source of the problem. Can it take the existing (file->f_mapping) as an argument in there ? Sure, but then can we override file system generic ->isolate(), ->putback(), ->migratepages() functions ? I dont think so. I am sure, there must be some work around to fix this problem for the driver. But we need to rethink this framework from supporting these mapped non LRU pages point of view.

I might be missing something here, feel free to point out.

- Anshuman

author maintainer
#23 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

Quoted 30 lines · Anshuman Khandual
Quoted 29 lines · Minchan Kim

Hi,

Quoted 26 lines · Anshuman Khandual
Quoted 26 lines · Minchan Kim
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
Quoted 5 lines

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 5 lines
(quoted diff) +5 -0
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Quoted 6 lines
Quoted 4 lines

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

Sure.

Thanks a lot!

Quoted 4 lines

My driver has privately managed non LRU pages which gets mapped into user space process page table through f_ops->mmap() and vmops->fault() which then updates the file RMAP (page->mapping->i_mmap) through page_add_file_rmap(page). One thing

Hmm, page_add_file_rmap is not exported function. How does your driver can use it? Do you use vm_insert_pfn?
What type your vma is? VM_PFNMMAP or VM_MIXEDMAP?

I want to make dummy driver to simulate your case.
It would be very helpful to implement/test pte-mapped non-lru page migration feature. That's why I ask now.

Quoted 8 lines

to note here is that the page->mapping eventually points to struct address_space (file->f_mapping) which belongs to the character device file (created using mknod) which we are using for establishing the mmap() regions in the user space.

Now as per this new framework, all the page's are to be made __SetPageMovable before passing the list down to migrate_pages(). Now __SetPageMovable() takes *new* struct address_space as an argument and replaces the existing page->mapping. Now thats the problem, we have lost all our connection to the existing file RMAP information. This

We could change __SetPageMovable doesn't need mapping argument. Instead, it just marks PAGE_MAPPING_MOVABLE into page->mapping. For that, user should take care of setting page->mapping earlier than marking the flag.

Quoted 6 lines

stands as a problem when we try to migrate these non LRU pages which are PTE mapped. The rmap_walk_file() never finds them in the VMA, skips all the migrate PTE steps and then the migration eventually fails.

Seems like assigning a new struct address_space to the page through __SetPageMovable() is the source of the problem. Can it take the existing (file->f_mapping) as an argument

We can set existing file->f_mapping under the page_lock.

Quoted 1 line

in there ? Sure, but then can we override file system generic ->isolate(), ->putback(),

I don't get it. Why does it override file system generic functions?

Quoted 8 lines

->migratepages() functions ? I dont think so. I am sure, there must be some work around to fix this problem for the driver. But we need to rethink this framework from supporting these mapped non LRU pages point of view.

I might be missing something here, feel free to point out.

- Anshuman

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#24 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

Quoted 38 lines · Minchan Kim
Quoted 30 lines · Anshuman Khandual
Quoted 29 lines · Minchan Kim

Hi,

Quoted 26 lines · Anshuman Khandual
Quoted 26 lines · Minchan Kim
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
Quoted 5 lines

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 5 lines
(quoted diff) +5 -0
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Quoted 6 lines
Quoted 4 lines

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

Sure.

Quoted 8 lines

Thanks a lot!

Quoted 4 lines

My driver has privately managed non LRU pages which gets mapped into user space process page table through f_ops->mmap() and vmops->fault() which then updates the file RMAP (page->mapping->i_mmap) through page_add_file_rmap(page). One thing

Hmm, page_add_file_rmap is not exported function. How does your driver can use it?

Its not using the function directly, I just re-iterated the sequence of functions above. (do_set_pte -> page_add_file_rmap) gets called after we grab the page from driver through (__do_fault->vma->vm_ops->fault()).

Quoted 2 lines

Do you use vm_insert_pfn?
What type your vma is? VM_PFNMMAP or VM_MIXEDMAP?

I dont use vm_insert_pfn(). Here is the sequence of events how the user space VMA gets the non LRU pages from the driver.

- Driver registers a character device with 'struct file_operations' binding
- Then the 'fops->mmap()' just binds the incoming 'struct vma' with a 'struct vm_operations_struct' which provides the 'vmops->fault()' routine which basically traps all page faults on the VMA and provides one page at a time through a driver specific allocation routine which hands over non LRU pages

The VMA is not anything special as such. Its what we get when we try to do a simple mmap() on a file descriptor pointing to a character device. I can figure out all the VM_* flags it holds after creation.

Quoted 2 lines

I want to make dummy driver to simulate your case.

Sure. I hope the above mentioned steps will help you but in case you need more information, please do let me know.

Quoted 12 lines

It would be very helpful to implement/test pte-mapped non-lru page migration feature. That's why I ask now.

Quoted 8 lines

to note here is that the page->mapping eventually points to struct address_space (file->f_mapping) which belongs to the character device file (created using mknod) which we are using for establishing the mmap() regions in the user space.

Now as per this new framework, all the page's are to be made __SetPageMovable before passing the list down to migrate_pages(). Now __SetPageMovable() takes *new* struct address_space as an argument and replaces the existing page->mapping. Now thats the problem, we have lost all our connection to the existing file RMAP information. This

Quoted 4 lines

We could change __SetPageMovable doesn't need mapping argument. Instead, it just marks PAGE_MAPPING_MOVABLE into page->mapping. For that, user should take care of setting page->mapping earlier than marking the flag.

Sounds like a good idea, that way we dont loose the reverse mapping information.

Quoted 9 lines
Quoted 7 lines

stands as a problem when we try to migrate these non LRU pages which are PTE mapped. The rmap_walk_file() never finds them in the VMA, skips all the migrate PTE steps and then the migration eventually fails.

Seems like assigning a new struct address_space to the page through __SetPageMovable() is the source of the problem. Can it take the existing (file->f_mapping) as an argument

We can set existing file->f_mapping under the page_lock.

Thats another option along with what you mentioned above.

Quoted 4 lines
Quoted 2 lines

in there ? Sure, but then can we override file system generic ->isolate(), ->putback(),

I don't get it. Why does it override file system generic functions?

Sure it does not, it was just an wild idea to over come the problem.

author maintainer
#25 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

Quoted 69 lines · Anshuman Khandual
Quoted 38 lines · Minchan Kim
Quoted 30 lines · Anshuman Khandual
Quoted 29 lines · Minchan Kim

Hi,

Quoted 26 lines · Anshuman Khandual
Quoted 26 lines · Minchan Kim
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
Quoted 5 lines

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 5 lines
(quoted diff) +5 -0
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Quoted 6 lines
Quoted 4 lines

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

Sure.

Quoted 8 lines

Thanks a lot!

Quoted 4 lines

My driver has privately managed non LRU pages which gets mapped into user space process page table through f_ops->mmap() and vmops->fault() which then updates the file RMAP (page->mapping->i_mmap) through page_add_file_rmap(page). One thing

Hmm, page_add_file_rmap is not exported function. How does your driver can use it?

Its not using the function directly, I just re-iterated the sequence of functions above. (do_set_pte -> page_add_file_rmap) gets called after we grab the page from driver through (__do_fault->vma->vm_ops->fault()).

Quoted 2 lines

Do you use vm_insert_pfn?
What type your vma is? VM_PFNMMAP or VM_MIXEDMAP?

I dont use vm_insert_pfn(). Here is the sequence of events how the user space VMA gets the non LRU pages from the driver.

- Driver registers a character device with 'struct file_operations' binding
- Then the 'fops->mmap()' just binds the incoming 'struct vma' with a 'struct vm_operations_struct' which provides the 'vmops->fault()' routine which basically traps all page faults on the VMA and provides one page at a time through a driver specific allocation routine which hands over non LRU pages

The VMA is not anything special as such. Its what we get when we try to do a simple mmap() on a file descriptor pointing to a character device. I can figure out all the VM_* flags it holds after creation.

Quoted 1 line
Quoted 4 lines
Quoted 1 line

I want to make dummy driver to simulate your case.

Sure. I hope the above mentioned steps will help you but in case you need more information, please do let me know.

I got understood now. :)
I will test it with dummy driver and will Cc'ed when I send a patch.

Thanks.

--
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>

#26 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

Quoted 70 lines · Minchan Kim
Quoted 69 lines · Anshuman Khandual
Quoted 38 lines · Minchan Kim
Quoted 30 lines · Anshuman Khandual
Quoted 29 lines · Minchan Kim

Hi,

Quoted 26 lines · Anshuman Khandual
Quoted 26 lines · Minchan Kim
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
Quoted 5 lines

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 5 lines
(quoted diff) +5 -0
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Quoted 6 lines
Quoted 4 lines

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

Sure.

Quoted 8 lines

Thanks a lot!

Quoted 4 lines

My driver has privately managed non LRU pages which gets mapped into user space process page table through f_ops->mmap() and vmops->fault() which then updates the file RMAP (page->mapping->i_mmap) through page_add_file_rmap(page). One thing

Hmm, page_add_file_rmap is not exported function. How does your driver can use it?

Its not using the function directly, I just re-iterated the sequence of functions above. (do_set_pte -> page_add_file_rmap) gets called after we grab the page from driver through (__do_fault->vma->vm_ops->fault()).

Quoted 2 lines

Do you use vm_insert_pfn?
What type your vma is? VM_PFNMMAP or VM_MIXEDMAP?

I dont use vm_insert_pfn(). Here is the sequence of events how the user space VMA gets the non LRU pages from the driver.

- Driver registers a character device with 'struct file_operations' binding
- Then the 'fops->mmap()' just binds the incoming 'struct vma' with a 'struct vm_operations_struct' which provides the 'vmops->fault()' routine which basically traps all page faults on the VMA and provides one page at a time through a driver specific allocation routine which hands over non LRU pages

The VMA is not anything special as such. Its what we get when we try to do a simple mmap() on a file descriptor pointing to a character device. I can figure out all the VM_* flags it holds after creation.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I want to make dummy driver to simulate your case.

Sure. I hope the above mentioned steps will help you but in case you need more information, please do let me know.

I got understood now. :)
I will test it with dummy driver and will Cc'ed when I send a patch.

Hello Minchan,

Do you have any updates on this ? The V7 of the series still has this limitation. Did you get a chance to test the driver out ? I am still concerned about how to handle the struct address_space override problem within the struct page.

- Anshuman

--
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>

author maintainer
#27 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

Quoted 76 lines · Anshuman Khandual
Quoted 75 lines · Minchan Kim
Quoted 73 lines · Anshuman Khandual
Quoted 38 lines · Minchan Kim
Quoted 30 lines · Anshuman Khandual
Quoted 29 lines · Minchan Kim

Hi,

Quoted 26 lines · Anshuman Khandual
Quoted 26 lines · Minchan Kim
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
Quoted 5 lines

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 5 lines
(quoted diff) +5 -0
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Quoted 6 lines
Quoted 4 lines

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

Sure.

Quoted 8 lines

Thanks a lot!

Quoted 4 lines

My driver has privately managed non LRU pages which gets mapped into user space process page table through f_ops->mmap() and vmops->fault() which then updates the file RMAP (page->mapping->i_mmap) through page_add_file_rmap(page). One thing

Hmm, page_add_file_rmap is not exported function. How does your driver can use it?

Its not using the function directly, I just re-iterated the sequence of functions above. (do_set_pte -> page_add_file_rmap) gets called after we grab the page from driver through (__do_fault->vma->vm_ops->fault()).

Quoted 2 lines

Do you use vm_insert_pfn?
What type your vma is? VM_PFNMMAP or VM_MIXEDMAP?

I dont use vm_insert_pfn(). Here is the sequence of events how the user space VMA gets the non LRU pages from the driver.

- Driver registers a character device with 'struct file_operations' binding
- Then the 'fops->mmap()' just binds the incoming 'struct vma' with a 'struct vm_operations_struct' which provides the 'vmops->fault()' routine which basically traps all page faults on the VMA and provides one page at a time through a driver specific allocation routine which hands over non LRU pages

The VMA is not anything special as such. Its what we get when we try to do a simple mmap() on a file descriptor pointing to a character device. I can figure out all the VM_* flags it holds after creation.

Quoted 2 lines

I want to make dummy driver to simulate your case.

Sure. I hope the above mentioned steps will help you but in case you need more information, please do let me know.

Quoted 8 lines
Quoted 2 lines

I got understood now. :)
I will test it with dummy driver and will Cc'ed when I send a patch.

Hello Minchan,

Do you have any updates on this ? The V7 of the series still has this limitation. Did you get a chance to test the driver out ? I am still concerned about how to handle the struct address_space override problem within the struct page.

Hi Anshuman,

Slow but I am working on that. :) However, as I said, I want to do it after soft landing of current non-lru-no-mapped page migration to solve current real field issues.

About the overriding problem of non-lru-mapped-page, I implemented dummy driver as miscellaneous device and in test_mmap(file_operations.mmap), I changed a_ops with my address_space_operations.

int test_mmap(struct file *filp, struct vm_area_struct *vma)

{
        filp->f_mapping->a_ops = &test_aops;
        vma->vm_ops = &test_vm_ops;
        vma->vm_private_data = filp->private_data;
        return 0;
}

test_aops should have *set_page_dirty* overriding.

static int test_set_pag_dirty(struct page *page)

{
        if (!PageDirty(page))
                SetPageDirty*page);
        return 0;
}

Otherwise, it goes BUG_ON during radix tree operation because currently try_to_unmap is designed for file-lru pages which lives in page cache so it propagates page table dirty bit to PG_dirty flag of struct page by set_page_dirty. And set_page_dirty want to mark dirty tag in radix tree node but it's character driver so the page cache doesn't have it. That's why we encounter BUG_ON in radix tree operation. Anyway, to test, I implemented set_page_dirty in my dummy driver.

With only that, it doesn't work because I need to modify migrate.c to work non-lru-mapped-page and changing PG_isolated flag which is override of PG_reclaim which is cleared in set_page_dirty.

With that, it seems to work. But I'm not saying it's right model now for device drivers. In runtime, replacing filp->f_mapping->a_ops with custom a_ops of own driver seems to be hacky to me.
So, I'm considering now new pseudo fs "movable_inode" which will support

struct file *movable_inode_getfile(const char *name,
const struct file_operations *fop, const struct address_space_operations *a_ops)

{
        struct path path;
        struct qstr this;
        struct inode *inode;
        struct super_block *sb;

        this.name = name;
        this.len = strlen(name);
        this.hash = 0;
        sb = movable_mnt.mnt_sb;
        patch.denty = d_alloc_pseudo(movable_inode_mnt->mnt_sb, &this);
        patch.mnt = mntget(movable_inode_mnt);
        
        inode = new_inode(sb);
        ..
        ..
        inode->i_mapping->a_ops = a_ops;
        d_instantiate(path.dentry, inode);

        return alloc_file(&path, FMODE_WRITE | FMODE_READ, f_op);
}

And in our driver, we can change vma->vm_file with new one.

int test_mmap(struct file *filp, struct vm_area_structd *vma)

{
        struct file *newfile = movable_inode_getfile("[test"],
                                filep->f_op, &test_aops);
        vma->vm_file = newfile;
        ..
        ..
}

When I read mmap_region in mm/mmap.c, it's reasonable usecase which dirver's mmap changes vma->vm_file with own file.

Anyway, it needs many subtle changes in mm/vfs/driver side so need to review from each maintainers related subsystem so I want to not be hurry.

Thanks.

Quoted 3 lines

- Anshuman

--
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>

#28 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

Quoted 82 lines · Minchan Kim
Quoted 81 lines · Anshuman Khandual
Quoted 70 lines · Minchan Kim
Quoted 69 lines · Anshuman Khandual
Quoted 38 lines · Minchan Kim
Quoted 30 lines · Anshuman Khandual
Quoted 29 lines · Minchan Kim

Hi,

Quoted 26 lines · Anshuman Khandual
Quoted 26 lines · Minchan Kim
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
Quoted 5 lines

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 5 lines
(quoted diff) +5 -0
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Quoted 6 lines
Quoted 4 lines

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

Sure.

Quoted 8 lines

Thanks a lot!

Quoted 4 lines

My driver has privately managed non LRU pages which gets mapped into user space process page table through f_ops->mmap() and vmops->fault() which then updates the file RMAP (page->mapping->i_mmap) through page_add_file_rmap(page). One thing

Hmm, page_add_file_rmap is not exported function. How does your driver can use it?

Its not using the function directly, I just re-iterated the sequence of functions above. (do_set_pte -> page_add_file_rmap) gets called after we grab the page from driver through (__do_fault->vma->vm_ops->fault()).

Quoted 2 lines

Do you use vm_insert_pfn?
What type your vma is? VM_PFNMMAP or VM_MIXEDMAP?

I dont use vm_insert_pfn(). Here is the sequence of events how the user space VMA gets the non LRU pages from the driver.

- Driver registers a character device with 'struct file_operations' binding
- Then the 'fops->mmap()' just binds the incoming 'struct vma' with a 'struct vm_operations_struct' which provides the 'vmops->fault()' routine which basically traps all page faults on the VMA and provides one page at a time through a driver specific allocation routine which hands over non LRU pages

The VMA is not anything special as such. Its what we get when we try to do a simple mmap() on a file descriptor pointing to a character device. I can figure out all the VM_* flags it holds after creation.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I want to make dummy driver to simulate your case.

Sure. I hope the above mentioned steps will help you but in case you need more information, please do let me know.

I got understood now. :)
I will test it with dummy driver and will Cc'ed when I send a patch.

Hello Minchan,

Quoted 7 lines
Quoted 3 lines

Do you have any updates on this ? The V7 of the series still has this limitation. Did you get a chance to test the driver out ? I am still concerned about how to handle the struct address_space override problem within the struct page.

Hi Anshuman,

Slow but I am working on that. :) However, as I said, I want to do it

I really appreciate. Was just curious about the problem and any potential solution we can look into.

Quoted 2 lines

after soft landing of current non-lru-no-mapped page migration to solve current real field issues.

yeah it makes sense.

Quoted 5 lines

About the overriding problem of non-lru-mapped-page, I implemented dummy driver as miscellaneous device and in test_mmap(file_operations.mmap), I changed a_ops with my address_space_operations.

Quoted 8 lines

int test_mmap(struct file *filp, struct vm_area_struct *vma)

{
        filp->f_mapping->a_ops = &test_aops;
        vma->vm_ops = &test_vm_ops;
        vma->vm_private_data = filp->private_data;
        return 0;
}

Okay.

Quoted 9 lines

test_aops should have *set_page_dirty* overriding.

static int test_set_pag_dirty(struct page *page)

{
        if (!PageDirty(page))
                SetPageDirty*page);
        return 0;
}
Quoted 8 lines

Otherwise, it goes BUG_ON during radix tree operation because currently try_to_unmap is designed for file-lru pages which lives in page cache so it propagates page table dirty bit to PG_dirty flag of struct page by set_page_dirty. And set_page_dirty want to mark dirty tag in radix tree node but it's character driver so the page cache doesn't have it. That's why we encounter BUG_ON in radix tree operation. Anyway, to test, I implemented set_page_dirty in my dummy driver.

Okay and the above test_set_page_dirty() example is sufficient ?

Quoted 4 lines

With only that, it doesn't work because I need to modify migrate.c to work non-lru-mapped-page and changing PG_isolated flag which is override of PG_reclaim which is cleared in set_page_dirty.

Got it, so what changes you did ? Implemented PG_isolated differently not by overriding PG_reclaim or something else ? Yes set_page_dirty indeed clears the PG_reclaim flag.

Quoted 2 lines

With that, it seems to work. But I'm not saying it's right model now

So the mapped pages migration was successful ? Even after overloading filp->f_mapping->a_ops = &test_aops, we still have the RMAP information intact with filp->f_mappinp pointed interval tree. But would really like to see the code changes.

Quoted 2 lines

for device drivers. In runtime, replacing filp->f_mapping->a_ops with custom a_ops of own driver seems to be hacky to me.

Yeah I thought so.

Quoted 39 lines

So, I'm considering now new pseudo fs "movable_inode" which will support

struct file *movable_inode_getfile(const char *name,
const struct file_operations *fop, const struct address_space_operations *a_ops)

{
        struct path path;
        struct qstr this;
        struct inode *inode;
        struct super_block *sb;

        this.name = name;
        this.len = strlen(name);
        this.hash = 0;
        sb = movable_mnt.mnt_sb;
        patch.denty = d_alloc_pseudo(movable_inode_mnt->mnt_sb, &this);
        patch.mnt = mntget(movable_inode_mnt);
        
        inode = new_inode(sb);
        ..
        ..
        inode->i_mapping->a_ops = a_ops;
        d_instantiate(path.dentry, inode);

        return alloc_file(&path, FMODE_WRITE | FMODE_READ, f_op);
}

And in our driver, we can change vma->vm_file with new one.

int test_mmap(struct file *filp, struct vm_area_structd *vma)

{
        struct file *newfile = movable_inode_getfile("[test"],
                                filep->f_op, &test_aops);
        vma->vm_file = newfile;
        ..
        ..
}
Quoted 2 lines

When I read mmap_region in mm/mmap.c, it's reasonable usecase which dirver's mmap changes vma->vm_file with own file.

I will look into these details.

Quoted 3 lines

Anyway, it needs many subtle changes in mm/vfs/driver side so need to review from each maintainers related subsystem so I want to not be hurry.

Sure, makes sense. Mean while it will be really great if you could share your code changes as described above, so that I can try them out.

--
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>

author maintainer
#29 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

On Thu, Jun 30, 2016 at 11:26:45AM +0530, Anshuman Khandual wrote:

<snip>

Quoted 49 lines
Quoted 6 lines
Quoted 2 lines

Did you get a chance to test the driver out ? I am still concerned about how to handle the struct address_space override problem within the struct page.

Hi Anshuman,

Slow but I am working on that. :) However, as I said, I want to do it

I really appreciate. Was just curious about the problem and any potential solution we can look into.

Quoted 2 lines

after soft landing of current non-lru-no-mapped page migration to solve current real field issues.

yeah it makes sense.

Quoted 5 lines

About the overriding problem of non-lru-mapped-page, I implemented dummy driver as miscellaneous device and in test_mmap(file_operations.mmap), I changed a_ops with my address_space_operations.

Quoted 8 lines

int test_mmap(struct file *filp, struct vm_area_struct *vma)

{
        filp->f_mapping->a_ops = &test_aops;
        vma->vm_ops = &test_vm_ops;
        vma->vm_private_data = filp->private_data;
        return 0;
}

Okay.

Quoted 9 lines

test_aops should have *set_page_dirty* overriding.

static int test_set_pag_dirty(struct page *page)

{
        if (!PageDirty(page))
                SetPageDirty*page);
        return 0;
}
Quoted 8 lines

Otherwise, it goes BUG_ON during radix tree operation because currently try_to_unmap is designed for file-lru pages which lives in page cache so it propagates page table dirty bit to PG_dirty flag of struct page by set_page_dirty. And set_page_dirty want to mark dirty tag in radix tree node but it's character driver so the page cache doesn't have it. That's why we encounter BUG_ON in radix tree operation. Anyway, to test, I implemented set_page_dirty in my dummy driver.

Quoted 1 line

Okay and the above test_set_page_dirty() example is sufficient ?

I guess just return 0 is sufficeint without any dirting a page.

Quoted 65 lines
Quoted 5 lines

With only that, it doesn't work because I need to modify migrate.c to work non-lru-mapped-page and changing PG_isolated flag which is override of PG_reclaim which is cleared in set_page_dirty.

Got it, so what changes you did ? Implemented PG_isolated differently not by overriding PG_reclaim or something else ? Yes set_page_dirty indeed clears the PG_reclaim flag.

Quoted 2 lines

With that, it seems to work. But I'm not saying it's right model now

So the mapped pages migration was successful ? Even after overloading filp->f_mapping->a_ops = &test_aops, we still have the RMAP information intact with filp->f_mappinp pointed interval tree. But would really like to see the code changes.

Quoted 2 lines

for device drivers. In runtime, replacing filp->f_mapping->a_ops with custom a_ops of own driver seems to be hacky to me.

Yeah I thought so.

Quoted 39 lines

So, I'm considering now new pseudo fs "movable_inode" which will support

struct file *movable_inode_getfile(const char *name,
const struct file_operations *fop, const struct address_space_operations *a_ops)

{
        struct path path;
        struct qstr this;
        struct inode *inode;
        struct super_block *sb;

        this.name = name;
        this.len = strlen(name);
        this.hash = 0;
        sb = movable_mnt.mnt_sb;
        patch.denty = d_alloc_pseudo(movable_inode_mnt->mnt_sb, &this);
        patch.mnt = mntget(movable_inode_mnt);
        
        inode = new_inode(sb);
        ..
        ..
        inode->i_mapping->a_ops = a_ops;
        d_instantiate(path.dentry, inode);

        return alloc_file(&path, FMODE_WRITE | FMODE_READ, f_op);
}

And in our driver, we can change vma->vm_file with new one.

int test_mmap(struct file *filp, struct vm_area_structd *vma)

{
        struct file *newfile = movable_inode_getfile("[test"],
                                filep->f_op, &test_aops);
        vma->vm_file = newfile;
        ..
        ..
}
Quoted 2 lines

When I read mmap_region in mm/mmap.c, it's reasonable usecase which dirver's mmap changes vma->vm_file with own file.

Quoted 9 lines

I will look into these details.

Quoted 3 lines

Anyway, it needs many subtle changes in mm/vfs/driver side so need to review from each maintainers related subsystem so I want to not be hurry.

Sure, makes sense. Mean while it will be really great if you could share your code changes as described above, so that I can try them out.

It's almost done for draft version and I'm doing stress test now and fortunately, doesn't see the problem until now.

I will send you when I'm ready.

Thanks.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

author maintainer
#30 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

In reply to Anshuman Khandual

On Thu, Jun 30, 2016 at 11:26:45AM +0530, Anshuman Khandual wrote:

<snip>

Quoted 49 lines
Quoted 6 lines
Quoted 2 lines

Did you get a chance to test the driver out ? I am still concerned about how to handle the struct address_space override problem within the struct page.

Hi Anshuman,

Slow but I am working on that. :) However, as I said, I want to do it

I really appreciate. Was just curious about the problem and any potential solution we can look into.

Quoted 2 lines

after soft landing of current non-lru-no-mapped page migration to solve current real field issues.

yeah it makes sense.

Quoted 5 lines

About the overriding problem of non-lru-mapped-page, I implemented dummy driver as miscellaneous device and in test_mmap(file_operations.mmap), I changed a_ops with my address_space_operations.

Quoted 8 lines

int test_mmap(struct file *filp, struct vm_area_struct *vma)

{
        filp->f_mapping->a_ops = &test_aops;
        vma->vm_ops = &test_vm_ops;
        vma->vm_private_data = filp->private_data;
        return 0;
}

Okay.

Quoted 9 lines

test_aops should have *set_page_dirty* overriding.

static int test_set_pag_dirty(struct page *page)

{
        if (!PageDirty(page))
                SetPageDirty*page);
        return 0;
}
Quoted 8 lines

Otherwise, it goes BUG_ON during radix tree operation because currently try_to_unmap is designed for file-lru pages which lives in page cache so it propagates page table dirty bit to PG_dirty flag of struct page by set_page_dirty. And set_page_dirty want to mark dirty tag in radix tree node but it's character driver so the page cache doesn't have it. That's why we encounter BUG_ON in radix tree operation. Anyway, to test, I implemented set_page_dirty in my dummy driver.

Quoted 1 line

Okay and the above test_set_page_dirty() example is sufficient ?

I guess just return 0 is sufficeint without any dirting a page.

Quoted 65 lines
Quoted 5 lines

With only that, it doesn't work because I need to modify migrate.c to work non-lru-mapped-page and changing PG_isolated flag which is override of PG_reclaim which is cleared in set_page_dirty.

Got it, so what changes you did ? Implemented PG_isolated differently not by overriding PG_reclaim or something else ? Yes set_page_dirty indeed clears the PG_reclaim flag.

Quoted 2 lines

With that, it seems to work. But I'm not saying it's right model now

So the mapped pages migration was successful ? Even after overloading filp->f_mapping->a_ops = &test_aops, we still have the RMAP information intact with filp->f_mappinp pointed interval tree. But would really like to see the code changes.

Quoted 2 lines

for device drivers. In runtime, replacing filp->f_mapping->a_ops with custom a_ops of own driver seems to be hacky to me.

Yeah I thought so.

Quoted 39 lines

So, I'm considering now new pseudo fs "movable_inode" which will support

struct file *movable_inode_getfile(const char *name,
const struct file_operations *fop, const struct address_space_operations *a_ops)

{
        struct path path;
        struct qstr this;
        struct inode *inode;
        struct super_block *sb;

        this.name = name;
        this.len = strlen(name);
        this.hash = 0;
        sb = movable_mnt.mnt_sb;
        patch.denty = d_alloc_pseudo(movable_inode_mnt->mnt_sb, &this);
        patch.mnt = mntget(movable_inode_mnt);
        
        inode = new_inode(sb);
        ..
        ..
        inode->i_mapping->a_ops = a_ops;
        d_instantiate(path.dentry, inode);

        return alloc_file(&path, FMODE_WRITE | FMODE_READ, f_op);
}

And in our driver, we can change vma->vm_file with new one.

int test_mmap(struct file *filp, struct vm_area_structd *vma)

{
        struct file *newfile = movable_inode_getfile("[test"],
                                filep->f_op, &test_aops);
        vma->vm_file = newfile;
        ..
        ..
}
Quoted 2 lines

When I read mmap_region in mm/mmap.c, it's reasonable usecase which dirver's mmap changes vma->vm_file with own file.

Quoted 9 lines

I will look into these details.

Quoted 3 lines

Anyway, it needs many subtle changes in mm/vfs/driver side so need to review from each maintainers related subsystem so I want to not be hurry.

Sure, makes sense. Mean while it will be really great if you could share your code changes as described above, so that I can try them out.

It's almost done for draft version and I'm doing stress test now and fortunately, doesn't see the problem until now.

I will send you when I'm ready.

Thanks.

#31 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

In reply to Minchan Kim
Quoted 82 lines · Minchan Kim
Quoted 81 lines · Anshuman Khandual
Quoted 70 lines · Minchan Kim
Quoted 69 lines · Anshuman Khandual
Quoted 38 lines · Minchan Kim
Quoted 30 lines · Anshuman Khandual
Quoted 29 lines · Minchan Kim

Hi,

Quoted 26 lines · Anshuman Khandual
Quoted 26 lines · Minchan Kim
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
Quoted 5 lines

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 5 lines
(quoted diff) +5 -0
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Quoted 6 lines
Quoted 4 lines

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

Sure.

Quoted 8 lines

Thanks a lot!

Quoted 4 lines

My driver has privately managed non LRU pages which gets mapped into user space process page table through f_ops->mmap() and vmops->fault() which then updates the file RMAP (page->mapping->i_mmap) through page_add_file_rmap(page). One thing

Hmm, page_add_file_rmap is not exported function. How does your driver can use it?

Its not using the function directly, I just re-iterated the sequence of functions above. (do_set_pte -> page_add_file_rmap) gets called after we grab the page from driver through (__do_fault->vma->vm_ops->fault()).

Quoted 2 lines

Do you use vm_insert_pfn?
What type your vma is? VM_PFNMMAP or VM_MIXEDMAP?

I dont use vm_insert_pfn(). Here is the sequence of events how the user space VMA gets the non LRU pages from the driver.

- Driver registers a character device with 'struct file_operations' binding
- Then the 'fops->mmap()' just binds the incoming 'struct vma' with a 'struct vm_operations_struct' which provides the 'vmops->fault()' routine which basically traps all page faults on the VMA and provides one page at a time through a driver specific allocation routine which hands over non LRU pages

The VMA is not anything special as such. Its what we get when we try to do a simple mmap() on a file descriptor pointing to a character device. I can figure out all the VM_* flags it holds after creation.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I want to make dummy driver to simulate your case.

Sure. I hope the above mentioned steps will help you but in case you need more information, please do let me know.

I got understood now. :)
I will test it with dummy driver and will Cc'ed when I send a patch.

Hello Minchan,

Quoted 7 lines
Quoted 3 lines

Do you have any updates on this ? The V7 of the series still has this limitation. Did you get a chance to test the driver out ? I am still concerned about how to handle the struct address_space override problem within the struct page.

Hi Anshuman,

Slow but I am working on that. :) However, as I said, I want to do it

I really appreciate. Was just curious about the problem and any potential solution we can look into.

Quoted 2 lines

after soft landing of current non-lru-no-mapped page migration to solve current real field issues.

yeah it makes sense.

Quoted 5 lines

About the overriding problem of non-lru-mapped-page, I implemented dummy driver as miscellaneous device and in test_mmap(file_operations.mmap), I changed a_ops with my address_space_operations.

Quoted 8 lines

int test_mmap(struct file *filp, struct vm_area_struct *vma)

{
        filp->f_mapping->a_ops = &test_aops;
        vma->vm_ops = &test_vm_ops;
        vma->vm_private_data = filp->private_data;
        return 0;
}

Okay.

Quoted 9 lines

test_aops should have *set_page_dirty* overriding.

static int test_set_pag_dirty(struct page *page)

{
        if (!PageDirty(page))
                SetPageDirty*page);
        return 0;
}
Quoted 8 lines

Otherwise, it goes BUG_ON during radix tree operation because currently try_to_unmap is designed for file-lru pages which lives in page cache so it propagates page table dirty bit to PG_dirty flag of struct page by set_page_dirty. And set_page_dirty want to mark dirty tag in radix tree node but it's character driver so the page cache doesn't have it. That's why we encounter BUG_ON in radix tree operation. Anyway, to test, I implemented set_page_dirty in my dummy driver.

Okay and the above test_set_page_dirty() example is sufficient ?

Quoted 4 lines

With only that, it doesn't work because I need to modify migrate.c to work non-lru-mapped-page and changing PG_isolated flag which is override of PG_reclaim which is cleared in set_page_dirty.

Got it, so what changes you did ? Implemented PG_isolated differently not by overriding PG_reclaim or something else ? Yes set_page_dirty indeed clears the PG_reclaim flag.

Quoted 2 lines

With that, it seems to work. But I'm not saying it's right model now

So the mapped pages migration was successful ? Even after overloading filp->f_mapping->a_ops = &test_aops, we still have the RMAP information intact with filp->f_mappinp pointed interval tree. But would really like to see the code changes.

Quoted 2 lines

for device drivers. In runtime, replacing filp->f_mapping->a_ops with custom a_ops of own driver seems to be hacky to me.

Yeah I thought so.

Quoted 39 lines

So, I'm considering now new pseudo fs "movable_inode" which will support

struct file *movable_inode_getfile(const char *name,
const struct file_operations *fop, const struct address_space_operations *a_ops)

{
        struct path path;
        struct qstr this;
        struct inode *inode;
        struct super_block *sb;

        this.name = name;
        this.len = strlen(name);
        this.hash = 0;
        sb = movable_mnt.mnt_sb;
        patch.denty = d_alloc_pseudo(movable_inode_mnt->mnt_sb, &this);
        patch.mnt = mntget(movable_inode_mnt);
        
        inode = new_inode(sb);
        ..
        ..
        inode->i_mapping->a_ops = a_ops;
        d_instantiate(path.dentry, inode);

        return alloc_file(&path, FMODE_WRITE | FMODE_READ, f_op);
}

And in our driver, we can change vma->vm_file with new one.

int test_mmap(struct file *filp, struct vm_area_structd *vma)

{
        struct file *newfile = movable_inode_getfile("[test"],
                                filep->f_op, &test_aops);
        vma->vm_file = newfile;
        ..
        ..
}
Quoted 2 lines

When I read mmap_region in mm/mmap.c, it's reasonable usecase which dirver's mmap changes vma->vm_file with own file.

I will look into these details.

Quoted 3 lines

Anyway, it needs many subtle changes in mm/vfs/driver side so need to review from each maintainers related subsystem so I want to not be hurry.

Sure, makes sense. Mean while it will be really great if you could share your code changes as described above, so that I can try them out.

author maintainer
#32 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

In reply to Anshuman Khandual
Quoted 76 lines · Anshuman Khandual
Quoted 75 lines · Minchan Kim
Quoted 73 lines · Anshuman Khandual
Quoted 38 lines · Minchan Kim
Quoted 30 lines · Anshuman Khandual
Quoted 29 lines · Minchan Kim

Hi,

Quoted 26 lines · Anshuman Khandual
Quoted 26 lines · Minchan Kim
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
Quoted 5 lines

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 5 lines
(quoted diff) +5 -0
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Quoted 6 lines
Quoted 4 lines

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

Sure.

Quoted 8 lines

Thanks a lot!

Quoted 4 lines

My driver has privately managed non LRU pages which gets mapped into user space process page table through f_ops->mmap() and vmops->fault() which then updates the file RMAP (page->mapping->i_mmap) through page_add_file_rmap(page). One thing

Hmm, page_add_file_rmap is not exported function. How does your driver can use it?

Its not using the function directly, I just re-iterated the sequence of functions above. (do_set_pte -> page_add_file_rmap) gets called after we grab the page from driver through (__do_fault->vma->vm_ops->fault()).

Quoted 2 lines

Do you use vm_insert_pfn?
What type your vma is? VM_PFNMMAP or VM_MIXEDMAP?

I dont use vm_insert_pfn(). Here is the sequence of events how the user space VMA gets the non LRU pages from the driver.

- Driver registers a character device with 'struct file_operations' binding
- Then the 'fops->mmap()' just binds the incoming 'struct vma' with a 'struct vm_operations_struct' which provides the 'vmops->fault()' routine which basically traps all page faults on the VMA and provides one page at a time through a driver specific allocation routine which hands over non LRU pages

The VMA is not anything special as such. Its what we get when we try to do a simple mmap() on a file descriptor pointing to a character device. I can figure out all the VM_* flags it holds after creation.

Quoted 2 lines

I want to make dummy driver to simulate your case.

Sure. I hope the above mentioned steps will help you but in case you need more information, please do let me know.

Quoted 8 lines
Quoted 2 lines

I got understood now. :)
I will test it with dummy driver and will Cc'ed when I send a patch.

Hello Minchan,

Do you have any updates on this ? The V7 of the series still has this limitation. Did you get a chance to test the driver out ? I am still concerned about how to handle the struct address_space override problem within the struct page.

Hi Anshuman,

Slow but I am working on that. :) However, as I said, I want to do it after soft landing of current non-lru-no-mapped page migration to solve current real field issues.

About the overriding problem of non-lru-mapped-page, I implemented dummy driver as miscellaneous device and in test_mmap(file_operations.mmap), I changed a_ops with my address_space_operations.

int test_mmap(struct file *filp, struct vm_area_struct *vma)

{
        filp->f_mapping->a_ops = &test_aops;
        vma->vm_ops = &test_vm_ops;
        vma->vm_private_data = filp->private_data;
        return 0;
}

test_aops should have *set_page_dirty* overriding.

static int test_set_pag_dirty(struct page *page)

{
        if (!PageDirty(page))
                SetPageDirty*page);
        return 0;
}

Otherwise, it goes BUG_ON during radix tree operation because currently try_to_unmap is designed for file-lru pages which lives in page cache so it propagates page table dirty bit to PG_dirty flag of struct page by set_page_dirty. And set_page_dirty want to mark dirty tag in radix tree node but it's character driver so the page cache doesn't have it. That's why we encounter BUG_ON in radix tree operation. Anyway, to test, I implemented set_page_dirty in my dummy driver.

With only that, it doesn't work because I need to modify migrate.c to work non-lru-mapped-page and changing PG_isolated flag which is override of PG_reclaim which is cleared in set_page_dirty.

With that, it seems to work. But I'm not saying it's right model now for device drivers. In runtime, replacing filp->f_mapping->a_ops with custom a_ops of own driver seems to be hacky to me.
So, I'm considering now new pseudo fs "movable_inode" which will support

struct file *movable_inode_getfile(const char *name,
const struct file_operations *fop, const struct address_space_operations *a_ops)

{
        struct path path;
        struct qstr this;
        struct inode *inode;
        struct super_block *sb;

        this.name = name;
        this.len = strlen(name);
        this.hash = 0;
        sb = movable_mnt.mnt_sb;
        patch.denty = d_alloc_pseudo(movable_inode_mnt->mnt_sb, &this);
        patch.mnt = mntget(movable_inode_mnt);
        
        inode = new_inode(sb);
        ..
        ..
        inode->i_mapping->a_ops = a_ops;
        d_instantiate(path.dentry, inode);

        return alloc_file(&path, FMODE_WRITE | FMODE_READ, f_op);
}

And in our driver, we can change vma->vm_file with new one.

int test_mmap(struct file *filp, struct vm_area_structd *vma)

{
        struct file *newfile = movable_inode_getfile("[test"],
                                filep->f_op, &test_aops);
        vma->vm_file = newfile;
        ..
        ..
}

When I read mmap_region in mm/mmap.c, it's reasonable usecase which dirver's mmap changes vma->vm_file with own file.

Anyway, it needs many subtle changes in mm/vfs/driver side so need to review from each maintainers related subsystem so I want to not be hurry.

Thanks.

Quoted 3 lines

- Anshuman

#33 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

In reply to Minchan Kim
Quoted 70 lines · Minchan Kim
Quoted 69 lines · Anshuman Khandual
Quoted 38 lines · Minchan Kim
Quoted 30 lines · Anshuman Khandual
Quoted 29 lines · Minchan Kim

Hi,

Quoted 26 lines · Anshuman Khandual
Quoted 26 lines · Minchan Kim
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
Quoted 5 lines

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 5 lines
(quoted diff) +5 -0
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Quoted 6 lines
Quoted 4 lines

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

Sure.

Quoted 8 lines

Thanks a lot!

Quoted 4 lines

My driver has privately managed non LRU pages which gets mapped into user space process page table through f_ops->mmap() and vmops->fault() which then updates the file RMAP (page->mapping->i_mmap) through page_add_file_rmap(page). One thing

Hmm, page_add_file_rmap is not exported function. How does your driver can use it?

Its not using the function directly, I just re-iterated the sequence of functions above. (do_set_pte -> page_add_file_rmap) gets called after we grab the page from driver through (__do_fault->vma->vm_ops->fault()).

Quoted 2 lines

Do you use vm_insert_pfn?
What type your vma is? VM_PFNMMAP or VM_MIXEDMAP?

I dont use vm_insert_pfn(). Here is the sequence of events how the user space VMA gets the non LRU pages from the driver.

- Driver registers a character device with 'struct file_operations' binding
- Then the 'fops->mmap()' just binds the incoming 'struct vma' with a 'struct vm_operations_struct' which provides the 'vmops->fault()' routine which basically traps all page faults on the VMA and provides one page at a time through a driver specific allocation routine which hands over non LRU pages

The VMA is not anything special as such. Its what we get when we try to do a simple mmap() on a file descriptor pointing to a character device. I can figure out all the VM_* flags it holds after creation.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I want to make dummy driver to simulate your case.

Sure. I hope the above mentioned steps will help you but in case you need more information, please do let me know.

I got understood now. :)
I will test it with dummy driver and will Cc'ed when I send a patch.

Hello Minchan,

Do you have any updates on this ? The V7 of the series still has this limitation. Did you get a chance to test the driver out ? I am still concerned about how to handle the struct address_space override problem within the struct page.

- Anshuman

author maintainer
#34 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

In reply to Anshuman Khandual
Quoted 69 lines · Anshuman Khandual
Quoted 38 lines · Minchan Kim
Quoted 30 lines · Anshuman Khandual
Quoted 29 lines · Minchan Kim

Hi,

Quoted 26 lines · Anshuman Khandual
Quoted 26 lines · Minchan Kim
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
Quoted 5 lines

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 5 lines
(quoted diff) +5 -0
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Quoted 6 lines
Quoted 4 lines

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

Sure.

Quoted 8 lines

Thanks a lot!

Quoted 4 lines

My driver has privately managed non LRU pages which gets mapped into user space process page table through f_ops->mmap() and vmops->fault() which then updates the file RMAP (page->mapping->i_mmap) through page_add_file_rmap(page). One thing

Hmm, page_add_file_rmap is not exported function. How does your driver can use it?

Its not using the function directly, I just re-iterated the sequence of functions above. (do_set_pte -> page_add_file_rmap) gets called after we grab the page from driver through (__do_fault->vma->vm_ops->fault()).

Quoted 2 lines

Do you use vm_insert_pfn?
What type your vma is? VM_PFNMMAP or VM_MIXEDMAP?

I dont use vm_insert_pfn(). Here is the sequence of events how the user space VMA gets the non LRU pages from the driver.

- Driver registers a character device with 'struct file_operations' binding
- Then the 'fops->mmap()' just binds the incoming 'struct vma' with a 'struct vm_operations_struct' which provides the 'vmops->fault()' routine which basically traps all page faults on the VMA and provides one page at a time through a driver specific allocation routine which hands over non LRU pages

The VMA is not anything special as such. Its what we get when we try to do a simple mmap() on a file descriptor pointing to a character device. I can figure out all the VM_* flags it holds after creation.

Quoted 1 line
Quoted 4 lines
Quoted 1 line

I want to make dummy driver to simulate your case.

Sure. I hope the above mentioned steps will help you but in case you need more information, please do let me know.

I got understood now. :)
I will test it with dummy driver and will Cc'ed when I send a patch.

Thanks.

author maintainer
#35 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

In reply to Anshuman Khandual
Quoted 30 lines · Anshuman Khandual
Quoted 29 lines · Minchan Kim

Hi,

Quoted 26 lines · Anshuman Khandual
Quoted 26 lines · Minchan Kim
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
Quoted 5 lines

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 5 lines
(quoted diff) +5 -0
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Quoted 6 lines
Quoted 4 lines

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

Sure.

Thanks a lot!

Quoted 4 lines

My driver has privately managed non LRU pages which gets mapped into user space process page table through f_ops->mmap() and vmops->fault() which then updates the file RMAP (page->mapping->i_mmap) through page_add_file_rmap(page). One thing

Hmm, page_add_file_rmap is not exported function. How does your driver can use it? Do you use vm_insert_pfn?
What type your vma is? VM_PFNMMAP or VM_MIXEDMAP?

I want to make dummy driver to simulate your case.
It would be very helpful to implement/test pte-mapped non-lru page migration feature. That's why I ask now.

Quoted 8 lines

to note here is that the page->mapping eventually points to struct address_space (file->f_mapping) which belongs to the character device file (created using mknod) which we are using for establishing the mmap() regions in the user space.

Now as per this new framework, all the page's are to be made __SetPageMovable before passing the list down to migrate_pages(). Now __SetPageMovable() takes *new* struct address_space as an argument and replaces the existing page->mapping. Now thats the problem, we have lost all our connection to the existing file RMAP information. This

We could change __SetPageMovable doesn't need mapping argument. Instead, it just marks PAGE_MAPPING_MOVABLE into page->mapping. For that, user should take care of setting page->mapping earlier than marking the flag.

Quoted 6 lines

stands as a problem when we try to migrate these non LRU pages which are PTE mapped. The rmap_walk_file() never finds them in the VMA, skips all the migrate PTE steps and then the migration eventually fails.

Seems like assigning a new struct address_space to the page through __SetPageMovable() is the source of the problem. Can it take the existing (file->f_mapping) as an argument

We can set existing file->f_mapping under the page_lock.

Quoted 1 line

in there ? Sure, but then can we override file system generic ->isolate(), ->putback(),

I don't get it. Why does it override file system generic functions?

Quoted 8 lines

->migratepages() functions ? I dont think so. I am sure, there must be some work around to fix this problem for the driver. But we need to rethink this framework from supporting these mapped non LRU pages point of view.

I might be missing something here, feel free to point out.

- Anshuman

author maintainer
#36 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

In reply to Anshuman Khandual

Hi,

Quoted 21 lines · Anshuman Khandual
Quoted 9 lines · Minchan Kim
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 8 lines
(quoted diff) +5 -0
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

Quoted 5 lines

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

You're right. Unfortunately, it doesn't support right now but surely, it's my TODO after landing this work.

Could you share your usecase?

It would be helpful for merging when I wll send patchset.

Thanks!

#37 lore raw

Re: [PATCH v6v3 02/12] mm: migrate: support non-lru movable page migration

In reply to Minchan Kim
Quoted 9 lines · Minchan Kim
(quoted diff) +1 -0
@@ -791,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	int rc = -EAGAIN;
 	int page_was_mapped = 0;
 	struct anon_vma *anon_vma = NULL;
+	bool is_lru = !__PageMovable(page);
 
 	if (!trylock_page(page)) {
 		if (!force || mode == MIGRATE_ASYNC)

Review static int __unmap_and_move(struct page *page, struct page *newpage, · line 1009

Quoted 8 lines
(quoted diff) +5 -0
@@ -871,6 +1002,11 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		goto out_unlock_both;
 	}
 
+	if (unlikely(!is_lru)) {
+		rc = move_to_new_page(newpage, page, mode);
+		goto out_unlock_both;
+	}
+

Hello Minchan,

I might be missing something here but does this implementation support the scenario where these non LRU pages owned by the driver mapped as PTE into process page table ? Because the "goto out_unlock_both" statement above skips all the PTE unmap, putting a migration PTE and removing the migration PTE steps.

Regards
Anshuman

author maintainer
#38 lore raw

PATCH v6 2/12 mm: migrate: support non-lru movable page migration

In reply to Minchan Kim

Per Vlastimi's review comment.

Thanks for the detail review, Vlastimi!
If you have another concern, feel free to say.
After I resolve all thing, I will send v7 rebased on recent mmotm.

From b14aaeeeeb2d3ac0702c7b2eec36409d74406d43 Mon Sep 17 00:00:00 2001
From: Minchan Kim <[email protected]>
Date: Fri, 8 Apr 2016 10:34:49 +0900
Subject: [PATCH] mm: migrate: support non-lru movable page migration

We have allowed migration for only LRU pages until now and it was enough to make high-order pages. But recently, embedded system(e.g., webOS, android) uses lots of non-movable pages(e.g., zram, GPU memory) so we have seen several reports about troubles of small high-order allocation. For fixing the problem, there were several efforts (e,g,. enhance compaction algorithm, SLUB fallback to 0-order page, reserved memory, vmalloc and so on) but if there are lots of non-movable pages in system, their solutions are void in the long run.

So, this patch is to support facility to change non-movable pages with movable. For the feature, this patch introduces functions related to migration to address_space_operations as well as some page flags.

If a driver want to make own pages movable, it should define three functions which are function pointers of struct address_space_operations.

1. bool (*isolate_page) (struct page *page, isolate_mode_t mode);

What VM expects on isolate_page function of driver is to return *true* if driver isolates page successfully. On returing true, VM marks the page as PG_isolated so concurrent isolation in several CPUs skip the page for isolation. If a driver cannot isolate the page, it should return *false*.

Once page is successfully isolated, VM uses page.lru fields so driver shouldn't expect to preserve values in that fields.

2. int (*migratepage) (struct address_space *mapping,

		struct page *newpage, struct page *oldpage, enum migrate_mode);

After isolation, VM calls migratepage of driver with isolated page. The function of migratepage is to move content of the old page to new page and set up fields of struct page newpage. Keep in mind that you should indicate to the VM the oldpage is no longer movable via __ClearPageMovable() under page_lock if you migrated the oldpage successfully and returns 0. If driver cannot migrate the page at the moment, driver can return -EAGAIN. On -EAGAIN, VM will retry page migration in a short time because VM interprets -EAGAIN as "temporal migration failure". On returning any error except -EAGAIN, VM will give up the page migration without retrying in this time.

Driver shouldn't touch page.lru field VM using in the functions.

3. void (*putback_page)(struct page *);

If migration fails on isolated page, VM should return the isolated page to the driver so VM calls driver's putback_page with migration failed page. In this function, driver should put the isolated page back to the own data structure.

4. non-lru movable page flags

There are two page flags for supporting non-lru movable page.

* PG_movable

Driver should use the below function to make page movable under page_lock.

void __SetPageMovable(struct page *page, struct address_space *mapping)

It needs argument of address_space for registering migration family functions which will be called by VM. Exactly speaking, PG_movable is not a real flag of struct page. Rather than, VM reuses page->mapping's lower bits to represent it.

	#define PAGE_MAPPING_MOVABLE 0x2
	page->mapping = page->mapping | PAGE_MAPPING_MOVABLE;

so driver shouldn't access page->mapping directly. Instead, driver should use page_mapping which mask off the low two bits of page->mapping so it can get right struct address_space.

For testing of non-lru movable page, VM supports __PageMovable function. However, it doesn't guarantee to identify non-lru movable page because page->mapping field is unified with other variables in struct page. As well, if driver releases the page after isolation by VM, page->mapping doesn't have stable value although it has PAGE_MAPPING_MOVABLE (Look at __ClearPageMovable). But __PageMovable is cheap to catch whether page is LRU or non-lru movable once the page has been isolated. Because LRU pages never can have PAGE_MAPPING_MOVABLE in page->mapping. It is also good for just peeking to test non-lru movable pages before more expensive checking with lock_page in pfn scanning to select victim.

For guaranteeing non-lru movable page, VM provides PageMovable function. Unlike __PageMovable, PageMovable functions validates page->mapping and mapping->a_ops->isolate_page under lock_page. The lock_page prevents sudden destroying of page->mapping.

Driver using __SetPageMovable should clear the flag via __ClearMovablePage under page_lock before the releasing the page.

* PG_isolated

To prevent concurrent isolation among several CPUs, VM marks isolated page as PG_isolated under lock_page. So if a CPU encounters PG_isolated non-lru movable page, it can skip it. Driver doesn't need to manipulate the flag because VM will set/clear it automatically. Keep in mind that if driver sees PG_isolated page, it means the page have been isolated by VM so it shouldn't touch page.lru field.
PG_isolated is alias with PG_reclaim flag so driver shouldn't use the flag for own purpose.

Cc Rik van Riel <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Rafael Aquini <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc John Einar Reitan <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
---
 Documentation/filesystems/Locking |   4 +
 Documentation/filesystems/vfs.txt |  11 +++
 Documentation/vm/page_migration   | 107 ++++++++++++++++++++-
 include/linux/compaction.h        |  17 ++++
 include/linux/fs.h                |   2 +
 include/linux/ksm.h               |   3 +-
 include/linux/migrate.h           |   2 +
 include/linux/mm.h                |   1 +
 include/linux/page-flags.h        |  33 +++++--
 mm/compaction.c                   |  85 +++++++++++++----
 mm/ksm.c                          |   4 +-
 mm/migrate.c                      | 192 ++++++++++++++++++++++++++++++++++----
 mm/page_alloc.c                   |   2 +-
 mm/util.c                         |   6 +-
 14 files changed, 417 insertions(+), 52 deletions(-)
Documentation/filesystems/Locking +4 -0
index af7c030a0368..3991a976cf43 100644
@@ -195,7 +195,9 @@ unlocks and drops the reference.
 	int (*releasepage) (struct page *, int);
 	void (*freepage)(struct page *);
 	int (*direct_IO)(struct kiocb *, struct iov_iter *iter);
+	bool (*isolate_page) (struct page *, isolate_mode_t);
 	int (*migratepage)(struct address_space *, struct page *, struct page *);
+	void (*putback_page) (struct page *);
 	int (*launder_page)(struct page *);
 	int (*is_partially_uptodate)(struct page *, unsigned long, unsigned long);
 	int (*error_remove_page)(struct address_space *, struct page *);
@@ -219,7 +221,9 @@ invalidatepage:		yes
 releasepage:		yes
 freepage:		yes
 direct_IO:
+isolate_page:		yes
 migratepage:		yes (both)
+putback_page:		yes
 launder_page:		yes
 is_partially_uptodate:	yes
 error_remove_page:	yes
Documentation/filesystems/vfs.txt +11 -0
index 19366fef2652..9d4ae317fdcb 100644
@@ -591,9 +591,14 @@ struct address_space_operations {
 	int (*releasepage) (struct page *, int);
 	void (*freepage)(struct page *);
 	ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter);
+	/* isolate a page for migration */
+	bool (*isolate_page) (struct page *, isolate_mode_t);
 	/* migrate the contents of a page to the specified target */
 	int (*migratepage) (struct page *, struct page *);
+	/* put migration-failed page back to right list */
+	void (*putback_page) (struct page *);
 	int (*launder_page) (struct page *);
+
 	int (*is_partially_uptodate) (struct page *, unsigned long,
 					unsigned long);
 	void (*is_dirty_writeback) (struct page *, bool *, bool *);
@@ -739,6 +744,10 @@ struct address_space_operations {
         and transfer data directly between the storage and the
         application's address space.
 
+  isolate_page: Called by the VM when isolating a movable non-lru page.
+	If page is successfully isolated, VM marks the page as PG_isolated
+	via __SetPageIsolated.
+
   migrate_page:  This is used to compact the physical memory usage.
         If the VM wants to relocate a page (maybe off a memory card
         that is signalling imminent failure) it will pass a new page
@@ -746,6 +755,8 @@ struct address_space_operations {
 	transfer any private data across and update any references
         that it has to the page.
 
+  putback_page: Called by the VM when isolated page's migration fails.
+
   launder_page: Called before freeing a page - it writes back the dirty page. To
   	prevent redirtying the page, it is kept locked during the whole
 	operation.
Documentation/vm/page_migration +106 -1

111 lines not on this page · raw message

include/linux/compaction.h +17 -0
index a58c852a268f..c6b47c861cea 100644
@@ -54,6 +54,9 @@ enum compact_result {
 struct alloc_context; /* in mm/internal.h */
 
 #ifdef CONFIG_COMPACTION
+extern int PageMovable(struct page *page);
+extern void __SetPageMovable(struct page *page, struct address_space *mapping);
+extern void __ClearPageMovable(struct page *page);
 extern int sysctl_compact_memory;
 extern int sysctl_compaction_handler(struct ctl_table *table, int write,
 			void __user *buffer, size_t *length, loff_t *ppos);
@@ -151,6 +154,19 @@ extern void kcompactd_stop(int nid);
 extern void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx);
 
 #else
+static inline int PageMovable(struct page *page)
+{
+	return 0;
+}
+static inline void __SetPageMovable(struct page *page,
+			struct address_space *mapping)
+{
+}
+
+static inline void __ClearPageMovable(struct page *page)
+{
+}
+
 static inline enum compact_result try_to_compact_pages(gfp_t gfp_mask,
 			unsigned int order, int alloc_flags,
 			const struct alloc_context *ac,
@@ -212,6 +228,7 @@ static inline void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_i
 #endif /* CONFIG_COMPACTION */
 
 #if defined(CONFIG_COMPACTION) && defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
+struct node;
 extern int compaction_register_node(struct node *node);
 extern void compaction_unregister_node(struct node *node);
 
include/linux/fs.h +2 -0
index 0cfdf2aec8f7..39ef97414033 100644
@@ -402,6 +402,8 @@ struct address_space_operations {
 	 */
 	int (*migratepage) (struct address_space *,
 			struct page *, struct page *, enum migrate_mode);
+	bool (*isolate_page)(struct page *, isolate_mode_t);
+	void (*putback_page)(struct page *);
 	int (*launder_page) (struct page *);
 	int (*is_partially_uptodate) (struct page *, unsigned long,
 					unsigned long);
include/linux/ksm.h +1 -2
index 7ae216a39c9e..481c8c4627ca 100644
@@ -43,8 +43,7 @@ static inline struct stable_node *page_stable_node(struct page *page)
 static inline void set_page_stable_node(struct page *page,
 					struct stable_node *stable_node)
 {
-	page->mapping = (void *)stable_node +
-				(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
+	page->mapping = (void *)((unsigned long)stable_node | PAGE_MAPPING_KSM);
 }
 
 /*
include/linux/migrate.h +2 -0
index 9b50325e4ddf..404fbfefeb33 100644
@@ -37,6 +37,8 @@ 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);
+extern bool isolate_movable_page(struct page *page, isolate_mode_t mode);
+extern void putback_movable_page(struct page *page);
 
 extern int migrate_prep(void);
 extern int migrate_prep_local(void);
include/linux/mm.h +1 -0
index a00ec816233a..33eaec57e997 100644
@@ -1035,6 +1035,7 @@ static inline pgoff_t page_file_index(struct page *page)
 }
 
 bool page_mapped(struct page *page);
+struct address_space *page_mapping(struct page *page);
 
 /*
  * Return true only if the page has been allocated with
include/linux/page-flags.h +23 -10
index e5a32445f930..f36dbb3a3060 100644
@@ -129,6 +129,9 @@ enum pageflags {
 
 	/* Compound pages. Stored in first tail page's flags */
 	PG_double_map = PG_private_2,
+
+	/* non-lru isolated movable page */
+	PG_isolated = PG_reclaim,
 };
 
 #ifndef __GENERATING_BOUNDS_H
@@ -357,29 +360,37 @@ PAGEFLAG(Idle, idle, PF_ANY)
  * with the PAGE_MAPPING_ANON bit set to distinguish it.  See rmap.h.
  *
  * On an anonymous page in a VM_MERGEABLE area, if CONFIG_KSM is enabled,
- * the PAGE_MAPPING_KSM bit may be set along with the PAGE_MAPPING_ANON bit;
- * and then page->mapping points, not to an anon_vma, but to a private
+ * the PAGE_MAPPING_MOVABLE bit may be set along with the PAGE_MAPPING_ANON
+ * bit; and then page->mapping points, not to an anon_vma, but to a private
  * structure which KSM associates with that merged page.  See ksm.h.
  *
- * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is currently never used.
+ * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is used for non-lru movable
+ * page and then page->mapping points a struct address_space.
  *
  * Please note that, confusingly, "page_mapping" refers to the inode
  * address_space which maps the page from disk; whereas "page_mapped"
  * refers to user virtual address space into which the page is mapped.
  */
-#define PAGE_MAPPING_ANON	1
-#define PAGE_MAPPING_KSM	2
-#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM)
+#define PAGE_MAPPING_ANON	0x1
+#define PAGE_MAPPING_MOVABLE	0x2
+#define PAGE_MAPPING_KSM	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
+#define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
 
-static __always_inline int PageAnonHead(struct page *page)
+static __always_inline int PageMappingFlags(struct page *page)
 {
-	return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
+	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) != 0;
 }
 
 static __always_inline int PageAnon(struct page *page)
 {
 	page = compound_head(page);
-	return PageAnonHead(page);
+	return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
+}
+
+static __always_inline int __PageMovable(struct page *page)
+{
+	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
+				PAGE_MAPPING_MOVABLE;
 }
 
 #ifdef CONFIG_KSM
@@ -393,7 +404,7 @@ static __always_inline int PageKsm(struct page *page)
 {
 	page = compound_head(page);
 	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
-				(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
+				PAGE_MAPPING_KSM;
 }
 #else
 TESTPAGEFLAG_FALSE(Ksm)
@@ -641,6 +652,8 @@ static inline void __ClearPageBalloon(struct page *page)
 	atomic_set(&page->_mapcount, -1);
 }
 
+__PAGEFLAG(Isolated, isolated, PF_ANY);
+
 /*
  * If network-based swap is enabled, sl*b must keep track of whether pages
  * were allocated from pfmemalloc reserves.
mm/compaction.c +69 -16

104 lines not on this page · raw message

mm/ksm.c +2 -2
index 4786b4150f62..35b8aef867a9 100644
@@ -532,8 +532,8 @@ static struct page *get_ksm_page(struct stable_node *stable_node, bool lock_it)
 	void *expected_mapping;
 	unsigned long kpfn;
 
-	expected_mapping = (void *)stable_node +
-				(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
+	expected_mapping = (void *)((unsigned long)stable_node |
+					PAGE_MAPPING_KSM);
 again:
 	kpfn = READ_ONCE(stable_node->kpfn);
 	page = pfn_to_page(kpfn);
mm/migrate.c +174 -18

269 lines not on this page · raw message

mm/page_alloc.c +1 -1
index d27e8b968ac3..4d0ba8f6cfee 100644
@@ -1014,7 +1014,7 @@ static __always_inline bool free_pages_prepare(struct page *page,
 			(page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
 		}
 	}
-	if (PageAnonHead(page))
+	if (PageMappingFlags(page))
 		page->mapping = NULL;
 	if (check_free)
 		bad += free_pages_check(page);
mm/util.c +4 -2
index 917e0e3d0f8e..b756ee36f7f0 100644
@@ -399,10 +399,12 @@ struct address_space *page_mapping(struct page *page)
 	}
 
 	mapping = page->mapping;
-	if ((unsigned long)mapping & PAGE_MAPPING_FLAGS)
+	if ((unsigned long)mapping & PAGE_MAPPING_ANON)
 		return NULL;
-	return mapping;
+
+	return (void *)((unsigned long)mapping & ~PAGE_MAPPING_FLAGS);
 }
+EXPORT_SYMBOL(page_mapping);
 
 /* Slow path of page_mapcount() for compound pages */
 int __page_mapcount(struct page *page)
Signature · 1 line
1.9.1
author maintainer
#39 lore raw

PATCH v6 3/12 mm: balloon: use general non-lru movable page feature

In reply to Minchan Kim

Now, VM has a feature to migrate non-lru movable pages so balloon doesn't need custom migration hooks in migrate.c and compaction.c. Instead, this patch implements page->mapping->a_ops->{isolate|migrate|putback} functions.

With that, we could remove hooks for ballooning in general migration functions and make balloon compaction simple.

Cc Rafael Aquini <[email protected]>
Cc Konstantin Khlebnikov <[email protected]>
---
 drivers/virtio/virtio_balloon.c    | 54 +++++++++++++++++++---
 include/linux/balloon_compaction.h | 53 +++++++--------------
 include/uapi/linux/magic.h         |  1 +
 mm/balloon_compaction.c            | 94 +++++++-------------------------------
 mm/compaction.c                    |  7 ---
 mm/migrate.c                       | 19 +-------
 mm/vmscan.c                        |  2 +-
 7 files changed, 85 insertions(+), 145 deletions(-)
drivers/virtio/virtio_balloon.c +48 -6
index 476c0e3a7150..88d5609375de 100644
@@ -30,6 +30,7 @@
 #include <linux/oom.h>
 #include <linux/wait.h>
 #include <linux/mm.h>
+#include <linux/mount.h>
 
 /*
  * Balloon device works in 4K page units.  So each page is pointed to by
@@ -45,6 +46,10 @@ static int oom_pages = OOM_VBALLOON_DEFAULT_PAGES;
 module_param(oom_pages, int, S_IRUSR | S_IWUSR);
 MODULE_PARM_DESC(oom_pages, "pages to free on OOM");
 
+#ifdef CONFIG_BALLOON_COMPACTION
+static struct vfsmount *balloon_mnt;
+#endif
+
 struct virtio_balloon {
 	struct virtio_device *vdev;
 	struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
@@ -488,8 +493,26 @@ static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info,
 
 	put_page(page); /* balloon reference */
 
-	return MIGRATEPAGE_SUCCESS;
+	return 0;
 }
+
+static struct dentry *balloon_mount(struct file_system_type *fs_type,
+		int flags, const char *dev_name, void *data)
+{
+	static const struct dentry_operations ops = {
+		.d_dname = simple_dname,
+	};
+
+	return mount_pseudo(fs_type, "balloon-kvm:", NULL, &ops,
+				BALLOON_KVM_MAGIC);
+}
+
+static struct file_system_type balloon_fs = {
+	.name           = "balloon-kvm",
+	.mount          = balloon_mount,
+	.kill_sb        = kill_anon_super,
+};
+
 #endif /* CONFIG_BALLOON_COMPACTION */
 
 static int virtballoon_probe(struct virtio_device *vdev)
@@ -519,9 +542,6 @@ static int virtballoon_probe(struct virtio_device *vdev)
 	vb->vdev = vdev;
 
 	balloon_devinfo_init(&vb->vb_dev_info);
-#ifdef CONFIG_BALLOON_COMPACTION
-	vb->vb_dev_info.migratepage = virtballoon_migratepage;
-#endif
 
 	err = init_vqs(vb);
 	if (err)
@@ -531,13 +551,33 @@ static int virtballoon_probe(struct virtio_device *vdev)
 	vb->nb.priority = VIRTBALLOON_OOM_NOTIFY_PRIORITY;
 	err = register_oom_notifier(&vb->nb);
 	if (err < 0)
-		goto out_oom_notify;
+		goto out_del_vqs;
+
+#ifdef CONFIG_BALLOON_COMPACTION
+	balloon_mnt = kern_mount(&balloon_fs);
+	if (IS_ERR(balloon_mnt)) {
+		err = PTR_ERR(balloon_mnt);
+		unregister_oom_notifier(&vb->nb);
+		goto out_del_vqs;
+	}
+
+	vb->vb_dev_info.migratepage = virtballoon_migratepage;
+	vb->vb_dev_info.inode = alloc_anon_inode(balloon_mnt->mnt_sb);
+	if (IS_ERR(vb->vb_dev_info.inode)) {
+		err = PTR_ERR(vb->vb_dev_info.inode);
+		kern_unmount(balloon_mnt);
+		unregister_oom_notifier(&vb->nb);
+		vb->vb_dev_info.inode = NULL;
+		goto out_del_vqs;
+	}
+	vb->vb_dev_info.inode->i_mapping->a_ops = &balloon_aops;
+#endif
 
 	virtio_device_ready(vdev);
 
 	return 0;
 
-out_oom_notify:
+out_del_vqs:
 	vdev->config->del_vqs(vdev);
 out_free_vb:
 	kfree(vb);
@@ -571,6 +611,8 @@ static void virtballoon_remove(struct virtio_device *vdev)
 	cancel_work_sync(&vb->update_balloon_stats_work);
 
 	remove_common(vb);
+	if (vb->vb_dev_info.inode)
+		iput(vb->vb_dev_info.inode);
 	kfree(vb);
 }
 
include/linux/balloon_compaction.h +16 -37

94 lines not on this page · raw message

include/uapi/linux/magic.h +1 -0
index 546b38886e11..d829ce63529d 100644
@@ -80,5 +80,6 @@
 #define BPF_FS_MAGIC		0xcafe4a11
 /* Since UDF 2.01 is ISO 13346 based... */
 #define UDF_SUPER_MAGIC		0x15013346
+#define BALLOON_KVM_MAGIC	0x13661366
 
 #endif /* __LINUX_MAGIC_H__ */
mm/balloon_compaction.c +17 -77

134 lines not on this page · raw message

mm/compaction.c +0 -7
index 2d6862d0df60..95877987b3c5 100644
@@ -792,13 +792,6 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 		 */
 		is_lru = PageLRU(page);
 		if (!is_lru) {
-			if (unlikely(balloon_page_movable(page))) {
-				if (balloon_page_isolate(page)) {
-					/* Successfully isolated */
-					goto isolate_success;
-				}
-			}
-
 			/*
 			 * __PageMovable can return false positive so we need
 			 * to verify it under page_lock.
mm/migrate.c +2 -17
index 57559ca7c904..d1b3acf9b9a9 100644
@@ -168,14 +168,12 @@ void putback_movable_pages(struct list_head *l)
 		list_del(&page->lru);
 		dec_zone_page_state(page, NR_ISOLATED_ANON +
 				page_is_file_cache(page));
-		if (unlikely(isolated_balloon_page(page))) {
-			balloon_page_putback(page);
 		/*
 		 * We isolated non-lru movable page so here we can use
 		 * __PageMovable because LRU page's mapping cannot have
 		 * PAGE_MAPPING_MOVABLE.
 		 */
-		} else if (unlikely(__PageMovable(page))) {
+		if (unlikely(__PageMovable(page))) {
 			VM_BUG_ON_PAGE(!PageIsolated(page), page);
 			lock_page(page);
 			if (PageMovable(page))
@@ -985,18 +983,6 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	if (unlikely(!trylock_page(newpage)))
 		goto out_unlock;
 
-	if (unlikely(isolated_balloon_page(page))) {
-		/*
-		 * A ballooned page does not need any special attention from
-		 * physical to virtual reverse mapping procedures.
-		 * Skip any attempt to unmap PTEs or to remap swap cache,
-		 * in order to avoid burning cycles at rmap level, and perform
-		 * the page migration right away (proteced by page lock).
-		 */
-		rc = balloon_page_migrate(newpage, page, mode);
-		goto out_unlock_both;
-	}
-
 	if (unlikely(!is_lru)) {
 		rc = move_to_new_page(newpage, page, mode);
 		goto out_unlock_both;
@@ -1051,8 +1037,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	 * list in here.
 	 */
 	if (rc == MIGRATEPAGE_SUCCESS) {
-		if (unlikely(__is_movable_balloon_page(newpage) ||
-				__PageMovable(newpage)))
+		if (unlikely(__PageMovable(newpage)))
 			put_page(newpage);
 		else
 			putback_lru_page(newpage);
mm/vmscan.c +1 -1
index c4a2f4512fca..93ba33789ac6 100644
@@ -1254,7 +1254,7 @@ unsigned long reclaim_clean_pages_from_list(struct zone *zone,
 
 	list_for_each_entry_safe(page, next, page_list, lru) {
 		if (page_is_file_cache(page) && !PageDirty(page) &&
-		    !isolated_balloon_page(page)) {
+		    !__PageMovable(page)) {
 			ClearPageActive(page);
 			list_move(&page->lru, &clean_pages);
 		}
Signature · 1 line
1.9.1
author maintainer
#40 lore raw

PATCH v6 3/12 mm: balloon: use general non-lru movable page feature

In reply to Minchan Kim

Now, VM has a feature to migrate non-lru movable pages so balloon doesn't need custom migration hooks in migrate.c and compaction.c. Instead, this patch implements page->mapping->a_ops->{isolate|migrate|putback} functions.

With that, we could remove hooks for ballooning in general migration functions and make balloon compaction simple.

Cc Rafael Aquini <[email protected]>
Cc Konstantin Khlebnikov <[email protected]>
---
 drivers/virtio/virtio_balloon.c    | 54 +++++++++++++++++++---
 include/linux/balloon_compaction.h | 53 +++++++--------------
 include/uapi/linux/magic.h         |  1 +
 mm/balloon_compaction.c            | 94 +++++++-------------------------------
 mm/compaction.c                    |  7 ---
 mm/migrate.c                       | 19 +-------
 mm/vmscan.c                        |  2 +-
 7 files changed, 85 insertions(+), 145 deletions(-)
drivers/virtio/virtio_balloon.c +48 -6
index 476c0e3a7150..88d5609375de 100644
@@ -30,6 +30,7 @@
 #include <linux/oom.h>
 #include <linux/wait.h>
 #include <linux/mm.h>
+#include <linux/mount.h>
 
 /*
  * Balloon device works in 4K page units.  So each page is pointed to by
@@ -45,6 +46,10 @@ static int oom_pages = OOM_VBALLOON_DEFAULT_PAGES;
 module_param(oom_pages, int, S_IRUSR | S_IWUSR);
 MODULE_PARM_DESC(oom_pages, "pages to free on OOM");
 
+#ifdef CONFIG_BALLOON_COMPACTION
+static struct vfsmount *balloon_mnt;
+#endif
+
 struct virtio_balloon {
 	struct virtio_device *vdev;
 	struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
@@ -488,8 +493,26 @@ static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info,
 
 	put_page(page); /* balloon reference */
 
-	return MIGRATEPAGE_SUCCESS;
+	return 0;
 }
+
+static struct dentry *balloon_mount(struct file_system_type *fs_type,
+		int flags, const char *dev_name, void *data)
+{
+	static const struct dentry_operations ops = {
+		.d_dname = simple_dname,
+	};
+
+	return mount_pseudo(fs_type, "balloon-kvm:", NULL, &ops,
+				BALLOON_KVM_MAGIC);
+}
+
+static struct file_system_type balloon_fs = {
+	.name           = "balloon-kvm",
+	.mount          = balloon_mount,
+	.kill_sb        = kill_anon_super,
+};
+
 #endif /* CONFIG_BALLOON_COMPACTION */
 
 static int virtballoon_probe(struct virtio_device *vdev)
@@ -519,9 +542,6 @@ static int virtballoon_probe(struct virtio_device *vdev)
 	vb->vdev = vdev;
 
 	balloon_devinfo_init(&vb->vb_dev_info);
-#ifdef CONFIG_BALLOON_COMPACTION
-	vb->vb_dev_info.migratepage = virtballoon_migratepage;
-#endif
 
 	err = init_vqs(vb);
 	if (err)
@@ -531,13 +551,33 @@ static int virtballoon_probe(struct virtio_device *vdev)
 	vb->nb.priority = VIRTBALLOON_OOM_NOTIFY_PRIORITY;
 	err = register_oom_notifier(&vb->nb);
 	if (err < 0)
-		goto out_oom_notify;
+		goto out_del_vqs;
+
+#ifdef CONFIG_BALLOON_COMPACTION
+	balloon_mnt = kern_mount(&balloon_fs);
+	if (IS_ERR(balloon_mnt)) {
+		err = PTR_ERR(balloon_mnt);
+		unregister_oom_notifier(&vb->nb);
+		goto out_del_vqs;
+	}
+
+	vb->vb_dev_info.migratepage = virtballoon_migratepage;
+	vb->vb_dev_info.inode = alloc_anon_inode(balloon_mnt->mnt_sb);
+	if (IS_ERR(vb->vb_dev_info.inode)) {
+		err = PTR_ERR(vb->vb_dev_info.inode);
+		kern_unmount(balloon_mnt);
+		unregister_oom_notifier(&vb->nb);
+		vb->vb_dev_info.inode = NULL;
+		goto out_del_vqs;
+	}
+	vb->vb_dev_info.inode->i_mapping->a_ops = &balloon_aops;
+#endif
 
 	virtio_device_ready(vdev);
 
 	return 0;
 
-out_oom_notify:
+out_del_vqs:
 	vdev->config->del_vqs(vdev);
 out_free_vb:
 	kfree(vb);
@@ -571,6 +611,8 @@ static void virtballoon_remove(struct virtio_device *vdev)
 	cancel_work_sync(&vb->update_balloon_stats_work);
 
 	remove_common(vb);
+	if (vb->vb_dev_info.inode)
+		iput(vb->vb_dev_info.inode);
 	kfree(vb);
 }
 
include/linux/balloon_compaction.h +16 -37

94 lines not on this page · raw message

include/uapi/linux/magic.h +1 -0
index 546b38886e11..d829ce63529d 100644
@@ -80,5 +80,6 @@
 #define BPF_FS_MAGIC		0xcafe4a11
 /* Since UDF 2.01 is ISO 13346 based... */
 #define UDF_SUPER_MAGIC		0x15013346
+#define BALLOON_KVM_MAGIC	0x13661366
 
 #endif /* __LINUX_MAGIC_H__ */
mm/balloon_compaction.c +17 -77

134 lines not on this page · raw message

mm/compaction.c +0 -7
index 2d6862d0df60..95877987b3c5 100644
@@ -792,13 +792,6 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 		 */
 		is_lru = PageLRU(page);
 		if (!is_lru) {
-			if (unlikely(balloon_page_movable(page))) {
-				if (balloon_page_isolate(page)) {
-					/* Successfully isolated */
-					goto isolate_success;
-				}
-			}
-
 			/*
 			 * __PageMovable can return false positive so we need
 			 * to verify it under page_lock.
mm/migrate.c +2 -17
index 57559ca7c904..d1b3acf9b9a9 100644
@@ -168,14 +168,12 @@ void putback_movable_pages(struct list_head *l)
 		list_del(&page->lru);
 		dec_zone_page_state(page, NR_ISOLATED_ANON +
 				page_is_file_cache(page));
-		if (unlikely(isolated_balloon_page(page))) {
-			balloon_page_putback(page);
 		/*
 		 * We isolated non-lru movable page so here we can use
 		 * __PageMovable because LRU page's mapping cannot have
 		 * PAGE_MAPPING_MOVABLE.
 		 */
-		} else if (unlikely(__PageMovable(page))) {
+		if (unlikely(__PageMovable(page))) {
 			VM_BUG_ON_PAGE(!PageIsolated(page), page);
 			lock_page(page);
 			if (PageMovable(page))
@@ -985,18 +983,6 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	if (unlikely(!trylock_page(newpage)))
 		goto out_unlock;
 
-	if (unlikely(isolated_balloon_page(page))) {
-		/*
-		 * A ballooned page does not need any special attention from
-		 * physical to virtual reverse mapping procedures.
-		 * Skip any attempt to unmap PTEs or to remap swap cache,
-		 * in order to avoid burning cycles at rmap level, and perform
-		 * the page migration right away (proteced by page lock).
-		 */
-		rc = balloon_page_migrate(newpage, page, mode);
-		goto out_unlock_both;
-	}
-
 	if (unlikely(!is_lru)) {
 		rc = move_to_new_page(newpage, page, mode);
 		goto out_unlock_both;
@@ -1051,8 +1037,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	 * list in here.
 	 */
 	if (rc == MIGRATEPAGE_SUCCESS) {
-		if (unlikely(__is_movable_balloon_page(newpage) ||
-				__PageMovable(newpage)))
+		if (unlikely(__PageMovable(newpage)))
 			put_page(newpage);
 		else
 			putback_lru_page(newpage);
mm/vmscan.c +1 -1
index c4a2f4512fca..93ba33789ac6 100644
@@ -1254,7 +1254,7 @@ unsigned long reclaim_clean_pages_from_list(struct zone *zone,
 
 	list_for_each_entry_safe(page, next, page_list, lru) {
 		if (page_is_file_cache(page) && !PageDirty(page) &&
-		    !isolated_balloon_page(page)) {
+		    !__PageMovable(page)) {
 			ClearPageActive(page);
 			list_move(&page->lru, &clean_pages);
 		}
Signature · 7 lines
1.9.1

--
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>
Acked-by on v6 3/12
#41 lore raw

Re: [PATCH v6 03/12] mm: balloon: use general non-lru movable page feature

Quoted 5 lines · Minchan Kim

Now, VM has a feature to migrate non-lru movable pages so balloon doesn't need custom migration hooks in migrate.c and compaction.c. Instead, this patch implements page->mapping->a_ops->{isolate|migrate|putback} functions.

Quoted 8 lines

With that, we could remove hooks for ballooning in general migration functions and make balloon compaction simple.

Cc Rafael Aquini <[email protected]>
Cc Konstantin Khlebnikov <[email protected]>

Except for the inode/pseudofs stuff which I'm not familiar with,

Acked-by on v6 3/12
#42 lore raw

Re: [PATCH v6 03/12] mm: balloon: use general non-lru movable page feature

In reply to Minchan Kim
Quoted 5 lines · Minchan Kim

Now, VM has a feature to migrate non-lru movable pages so balloon doesn't need custom migration hooks in migrate.c and compaction.c. Instead, this patch implements page->mapping->a_ops->{isolate|migrate|putback} functions.

Quoted 8 lines

With that, we could remove hooks for ballooning in general migration functions and make balloon compaction simple.

Cc Rafael Aquini <[email protected]>
Cc Konstantin Khlebnikov <[email protected]>

Except for the inode/pseudofs stuff which I'm not familiar with,

--
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>

author maintainer
#43 lore raw

PATCH v6 4/12 zsmalloc: keep max_object in size_class

In reply to Minchan Kim

Every zspage in a size_class has same number of max objects so we could move it to a size_class.

---
 mm/zsmalloc.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
mm/zsmalloc.c +15 -17
index 72698db958e7..73a1eb4ffddf 100644
@@ -32,8 +32,6 @@
  *	page->freelist: points to the first free object in zspage.
  *		Free objects are linked together using in-place
  *		metadata.
- *	page->objects: maximum number of objects we can store in this
- *		zspage (class->zspage_order * PAGE_SIZE / class->size)
  *	page->lru: links together first pages of various zspages.
  *		Basically forming list of zspages in a fullness group.
  *	page->mapping: class index and fullness group of the zspage
@@ -211,6 +209,7 @@ struct size_class {
 	 * of ZS_ALIGN.
 	 */
 	int size;
+	int objs_per_zspage;
 	unsigned int index;
 
 	struct zs_size_stat stats;
@@ -627,21 +626,22 @@ static inline void zs_pool_stat_destroy(struct zs_pool *pool)
  * the pool (not yet implemented). This function returns fullness
  * status of the given page.
  */
-static enum fullness_group get_fullness_group(struct page *first_page)
+static enum fullness_group get_fullness_group(struct size_class *class,
+						struct page *first_page)
 {
-	int inuse, max_objects;
+	int inuse, objs_per_zspage;
 	enum fullness_group fg;
 
 	VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
 
 	inuse = first_page->inuse;
-	max_objects = first_page->objects;
+	objs_per_zspage = class->objs_per_zspage;
 
 	if (inuse == 0)
 		fg = ZS_EMPTY;
-	else if (inuse == max_objects)
+	else if (inuse == objs_per_zspage)
 		fg = ZS_FULL;
-	else if (inuse <= 3 * max_objects / fullness_threshold_frac)
+	else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
 		fg = ZS_ALMOST_EMPTY;
 	else
 		fg = ZS_ALMOST_FULL;
@@ -728,7 +728,7 @@ static enum fullness_group fix_fullness_group(struct size_class *class,
 	enum fullness_group currfg, newfg;
 
 	get_zspage_mapping(first_page, &class_idx, &currfg);
-	newfg = get_fullness_group(first_page);
+	newfg = get_fullness_group(class, first_page);
 	if (newfg == currfg)
 		goto out;
 
@@ -1008,9 +1008,6 @@ static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
 	init_zspage(class, first_page);
 
 	first_page->freelist = location_to_obj(first_page, 0);
-	/* Maximum number of objects we can store in this zspage */
-	first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
-
 	error = 0; /* Success */
 
 cleanup:
@@ -1238,11 +1235,11 @@ static bool can_merge(struct size_class *prev, int size, int pages_per_zspage)
 	return true;
 }
 
-static bool zspage_full(struct page *first_page)
+static bool zspage_full(struct size_class *class, struct page *first_page)
 {
 	VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
 
-	return first_page->inuse == first_page->objects;
+	return first_page->inuse == class->objs_per_zspage;
 }
 
 unsigned long zs_get_total_pages(struct zs_pool *pool)
@@ -1628,7 +1625,7 @@ static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
 		}
 
 		/* Stop if there is no more space */
-		if (zspage_full(d_page)) {
+		if (zspage_full(class, d_page)) {
 			unpin_tag(handle);
 			ret = -ENOMEM;
 			break;
@@ -1687,7 +1684,7 @@ static enum fullness_group putback_zspage(struct zs_pool *pool,
 {
 	enum fullness_group fullness;
 
-	fullness = get_fullness_group(first_page);
+	fullness = get_fullness_group(class, first_page);
 	insert_zspage(class, fullness, first_page);
 	set_zspage_mapping(first_page, class->index, fullness);
 
@@ -1939,8 +1936,9 @@ struct zs_pool *zs_create_pool(const char *name)
 		class->size = size;
 		class->index = i;
 		class->pages_per_zspage = pages_per_zspage;
-		if (pages_per_zspage == 1 &&
-			get_maxobj_per_zspage(size, pages_per_zspage) == 1)
+		class->objs_per_zspage = class->pages_per_zspage *
+						PAGE_SIZE / class->size;
+		if (pages_per_zspage == 1 && class->objs_per_zspage == 1)
 			class->huge = true;
 		spin_lock_init(&class->lock);
 		pool->size_class[i] = class;
Signature · 7 lines
1.9.1

--
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>
author maintainer
#44 lore raw

PATCH v6 5/12 zsmalloc: use bit_spin_lock

In reply to Minchan Kim

Use kernel standard bit spin-lock instead of custom mess. Even, it has a bug which doesn't disable preemption. The reason we don't have any problem is that we have used it during preemption disable section by class->lock spinlock. So no need to go to stable.

---
 mm/zsmalloc.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
mm/zsmalloc.c +3 -7
index 73a1eb4ffddf..9adaeef578b6 100644
@@ -864,21 +864,17 @@ static unsigned long obj_idx_to_offset(struct page *page,
 
 static inline int trypin_tag(unsigned long handle)
 {
-	unsigned long *ptr = (unsigned long *)handle;
-
-	return !test_and_set_bit_lock(HANDLE_PIN_BIT, ptr);
+	return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
 }
 
 static void pin_tag(unsigned long handle)
 {
-	while (!trypin_tag(handle));
+	bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
 }
 
 static void unpin_tag(unsigned long handle)
 {
-	unsigned long *ptr = (unsigned long *)handle;
-
-	clear_bit_unlock(HANDLE_PIN_BIT, ptr);
+	bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
 }
 
 static void reset_page(struct page *page)
Signature · 7 lines
1.9.1

--
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>
author maintainer
#45 lore raw

PATCH v6 6/12 zsmalloc: use accessor

In reply to Minchan Kim

Upcoming patch will change how to encode zspage meta so for easy review, this patch wraps code to access metadata as accessor.

---
 mm/zsmalloc.c | 82 +++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 60 insertions(+), 22 deletions(-)
mm/zsmalloc.c +60 -22
index 9adaeef578b6..cb342882cde3 100644
@@ -266,10 +266,14 @@ struct zs_pool {
  * A zspage's class index and fullness group
  * are encoded in its (first)page->mapping
  */
-#define CLASS_IDX_BITS	28
 #define FULLNESS_BITS	4
-#define CLASS_IDX_MASK	((1 << CLASS_IDX_BITS) - 1)
-#define FULLNESS_MASK	((1 << FULLNESS_BITS) - 1)
+#define CLASS_BITS	28
+
+#define FULLNESS_SHIFT	0
+#define CLASS_SHIFT	(FULLNESS_SHIFT + FULLNESS_BITS)
+
+#define FULLNESS_MASK	((1UL << FULLNESS_BITS) - 1)
+#define CLASS_MASK	((1UL << CLASS_BITS) - 1)
 
 struct mapping_area {
 #ifdef CONFIG_PGTABLE_MAPPING
@@ -416,6 +420,41 @@ static int is_last_page(struct page *page)
 	return PagePrivate2(page);
 }
 
+static inline int get_zspage_inuse(struct page *first_page)
+{
+	return first_page->inuse;
+}
+
+static inline void set_zspage_inuse(struct page *first_page, int val)
+{
+	first_page->inuse = val;
+}
+
+static inline void mod_zspage_inuse(struct page *first_page, int val)
+{
+	first_page->inuse += val;
+}
+
+static inline int get_first_obj_offset(struct page *page)
+{
+	return page->index;
+}
+
+static inline void set_first_obj_offset(struct page *page, int offset)
+{
+	page->index = offset;
+}
+
+static inline unsigned long get_freeobj(struct page *first_page)
+{
+	return (unsigned long)first_page->freelist;
+}
+
+static inline void set_freeobj(struct page *first_page, unsigned long obj)
+{
+	first_page->freelist = (void *)obj;
+}
+
 static void get_zspage_mapping(struct page *first_page,
 				unsigned int *class_idx,
 				enum fullness_group *fullness)
@@ -424,8 +463,8 @@ static void get_zspage_mapping(struct page *first_page,
 	VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
 
 	m = (unsigned long)first_page->mapping;
-	*fullness = m & FULLNESS_MASK;
-	*class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
+	*fullness = (m >> FULLNESS_SHIFT) & FULLNESS_MASK;
+	*class_idx = (m >> CLASS_SHIFT) & CLASS_MASK;
 }
 
 static void set_zspage_mapping(struct page *first_page,
@@ -435,8 +474,7 @@ static void set_zspage_mapping(struct page *first_page,
 	unsigned long m;
 	VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
 
-	m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
-			(fullness & FULLNESS_MASK);
+	m = (class_idx << CLASS_SHIFT) | (fullness << FULLNESS_SHIFT);
 	first_page->mapping = (struct address_space *)m;
 }
 
@@ -634,7 +672,7 @@ static enum fullness_group get_fullness_group(struct size_class *class,
 
 	VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
 
-	inuse = first_page->inuse;
+	inuse = get_zspage_inuse(first_page);
 	objs_per_zspage = class->objs_per_zspage;
 
 	if (inuse == 0)
@@ -680,7 +718,7 @@ static void insert_zspage(struct size_class *class,
 	 * empty/full. Put pages with higher ->inuse first.
 	 */
 	list_add_tail(&first_page->lru, &(*head)->lru);
-	if (first_page->inuse >= (*head)->inuse)
+	if (get_zspage_inuse(first_page) >= get_zspage_inuse(*head))
 		*head = first_page;
 }
 
@@ -857,7 +895,7 @@ static unsigned long obj_idx_to_offset(struct page *page,
 	unsigned long off = 0;
 
 	if (!is_first_page(page))
-		off = page->index;
+		off = get_first_obj_offset(page);
 
 	return off + obj_idx * class_size;
 }
@@ -892,7 +930,7 @@ static void free_zspage(struct page *first_page)
 	struct page *nextp, *tmp, *head_extra;
 
 	VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
-	VM_BUG_ON_PAGE(first_page->inuse, first_page);
+	VM_BUG_ON_PAGE(get_zspage_inuse(first_page), first_page);
 
 	head_extra = (struct page *)page_private(first_page);
 
@@ -933,7 +971,7 @@ static void init_zspage(struct size_class *class, struct page *first_page)
 		 * head of corresponding zspage's freelist.
 		 */
 		if (page != first_page)
-			page->index = off;
+			set_first_obj_offset(page, off);
 
 		vaddr = kmap_atomic(page);
 		link = (struct link_free *)vaddr + off / sizeof(*link);
@@ -988,7 +1026,7 @@ static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
 			SetPagePrivate(page);
 			set_page_private(page, 0);
 			first_page = page;
-			first_page->inuse = 0;
+			set_zspage_inuse(first_page, 0);
 		}
 		if (i == 1)
 			set_page_private(first_page, (unsigned long)page);
@@ -1003,7 +1041,7 @@ static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
 
 	init_zspage(class, first_page);
 
-	first_page->freelist = location_to_obj(first_page, 0);
+	set_freeobj(first_page,	(unsigned long)location_to_obj(first_page, 0));
 	error = 0; /* Success */
 
 cleanup:
@@ -1235,7 +1273,7 @@ static bool zspage_full(struct size_class *class, struct page *first_page)
 {
 	VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
 
-	return first_page->inuse == class->objs_per_zspage;
+	return get_zspage_inuse(first_page) == class->objs_per_zspage;
 }
 
 unsigned long zs_get_total_pages(struct zs_pool *pool)
@@ -1354,13 +1392,13 @@ static unsigned long obj_malloc(struct size_class *class,
 	void *vaddr;
 
 	handle |= OBJ_ALLOCATED_TAG;
-	obj = (unsigned long)first_page->freelist;
+	obj = get_freeobj(first_page);
 	obj_to_location(obj, &m_page, &m_objidx);
 	m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
 
 	vaddr = kmap_atomic(m_page);
 	link = (struct link_free *)vaddr + m_offset / sizeof(*link);
-	first_page->freelist = link->next;
+	set_freeobj(first_page, (unsigned long)link->next);
 	if (!class->huge)
 		/* record handle in the header of allocated chunk */
 		link->handle = handle;
@@ -1368,7 +1406,7 @@ static unsigned long obj_malloc(struct size_class *class,
 		/* record handle in first_page->private */
 		set_page_private(first_page, handle);
 	kunmap_atomic(vaddr);
-	first_page->inuse++;
+	mod_zspage_inuse(first_page, 1);
 	zs_stat_inc(class, OBJ_USED, 1);
 
 	return obj;
@@ -1448,12 +1486,12 @@ static void obj_free(struct size_class *class, unsigned long obj)
 
 	/* Insert this object in containing zspage's freelist */
 	link = (struct link_free *)(vaddr + f_offset);
-	link->next = first_page->freelist;
+	link->next = (void *)get_freeobj(first_page);
 	if (class->huge)
 		set_page_private(first_page, 0);
 	kunmap_atomic(vaddr);
-	first_page->freelist = (void *)obj;
-	first_page->inuse--;
+	set_freeobj(first_page, obj);
+	mod_zspage_inuse(first_page, -1);
 	zs_stat_dec(class, OBJ_USED, 1);
 }
 
@@ -1569,7 +1607,7 @@ static unsigned long find_alloced_obj(struct size_class *class,
 	void *addr = kmap_atomic(page);
 
 	if (!is_first_page(page))
-		offset = page->index;
+		offset = get_first_obj_offset(page);
 	offset += class->size * index;
 
 	while (offset < PAGE_SIZE) {
Signature · 7 lines
1.9.1

--
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>
author maintainer
#46 lore raw

PATCH v6 7/12 zsmalloc: factor page chain functionality out

In reply to Minchan Kim

For page migration, we need to create page chain of zspage dynamically so this patch factors it out from alloc_zspage.

---
 mm/zsmalloc.c | 59 +++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 35 insertions(+), 24 deletions(-)
mm/zsmalloc.c +35 -24
index cb342882cde3..2d1e4961905e 100644
@@ -956,7 +956,8 @@ static void init_zspage(struct size_class *class, struct page *first_page)
 	unsigned long off = 0;
 	struct page *page = first_page;
 
-	VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
+	first_page->freelist = NULL;
+	set_zspage_inuse(first_page, 0);
 
 	while (page) {
 		struct page *next_page;
@@ -992,15 +993,16 @@ static void init_zspage(struct size_class *class, struct page *first_page)
 		page = next_page;
 		off %= PAGE_SIZE;
 	}
+
+	set_freeobj(first_page, (unsigned long)location_to_obj(first_page, 0));
 }
 
-/*
- * Allocate a zspage for the given size class
- */
-static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
+static void create_page_chain(struct page *pages[], int nr_pages)
 {
-	int i, error;
-	struct page *first_page = NULL, *uninitialized_var(prev_page);
+	int i;
+	struct page *page;
+	struct page *prev_page = NULL;
+	struct page *first_page = NULL;
 
 	/*
 	 * Allocate individual pages and link them together as:
@@ -1013,20 +1015,14 @@ static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
 	 * (i.e. no other sub-page has this flag set) and PG_private_2 to
 	 * identify the last page.
 	 */
-	error = -ENOMEM;
-	for (i = 0; i < class->pages_per_zspage; i++) {
-		struct page *page;
-
-		page = alloc_page(flags);
-		if (!page)
-			goto cleanup;
+	for (i = 0; i < nr_pages; i++) {
+		page = pages[i];
 
 		INIT_LIST_HEAD(&page->lru);
-		if (i == 0) {	/* first page */
+		if (i == 0) {
 			SetPagePrivate(page);
 			set_page_private(page, 0);
 			first_page = page;
-			set_zspage_inuse(first_page, 0);
 		}
 		if (i == 1)
 			set_page_private(first_page, (unsigned long)page);
@@ -1034,22 +1030,37 @@ static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
 			set_page_private(page, (unsigned long)first_page);
 		if (i >= 2)
 			list_add(&page->lru, &prev_page->lru);
-		if (i == class->pages_per_zspage - 1)	/* last page */
+		if (i == nr_pages - 1)
 			SetPagePrivate2(page);
 		prev_page = page;
 	}
+}
 
-	init_zspage(class, first_page);
+/*
+ * Allocate a zspage for the given size class
+ */
+static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
+{
+	int i;
+	struct page *first_page = NULL;
+	struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
 
-	set_freeobj(first_page,	(unsigned long)location_to_obj(first_page, 0));
-	error = 0; /* Success */
+	for (i = 0; i < class->pages_per_zspage; i++) {
+		struct page *page;
 
-cleanup:
-	if (unlikely(error) && first_page) {
-		free_zspage(first_page);
-		first_page = NULL;
+		page = alloc_page(flags);
+		if (!page) {
+			while (--i >= 0)
+				__free_page(pages[i]);
+			return NULL;
+		}
+		pages[i] = page;
 	}
 
+	create_page_chain(pages, class->pages_per_zspage);
+	first_page = pages[0];
+	init_zspage(class, first_page);
+
 	return first_page;
 }
 
Signature · 7 lines
1.9.1

--
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>
author maintainer
#47 lore raw

PATCH v6 8/12 zsmalloc: introduce zspage structure

In reply to Minchan Kim

We have squeezed meta data of zspage into first page's descriptor. So, to get meta data from subpage, we should get first page first of all. But it makes trouble to implment page migration feature of zsmalloc because any place where to get first page from subpage can be raced with first page migration. IOW, first page it got could be stale. For preventing it, I have tried several approahces but it made code complicated so finally, I concluded to separate metadata from first page. Of course, it consumes more memory. IOW, 16bytes per zspage on 32bit at the moment. It means we lost 1% at *worst case*(40B/4096B) which is not bad I think at the cost of maintenance.

Cc Sergey Senozhatsky <[email protected]>
---
 mm/compaction.c |   1 -
 mm/zsmalloc.c   | 531 ++++++++++++++++++++++++++------------------------------
 2 files changed, 242 insertions(+), 290 deletions(-)
mm/compaction.c +0 -1
index 95877987b3c5..8e5ff00e7adc 100644
@@ -15,7 +15,6 @@
 #include <linux/backing-dev.h>
 #include <linux/sysctl.h>
 #include <linux/sysfs.h>
-#include <linux/balloon_compaction.h>
 #include <linux/page-isolation.h>
 #include <linux/kasan.h>
 #include <linux/kthread.h>
mm/zsmalloc.c +242 -289

1,029 lines not on this page · raw message

Signature · 7 lines
1.9.1

--
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>
author maintainer
#48 lore raw

PATCH v6 9/12 zsmalloc: separate free_zspage from putback_zspage

In reply to Minchan Kim

Currently, putback_zspage does free zspage under class->lock if fullness become ZS_EMPTY but it makes trouble to implement locking scheme for new zspage migration.
So, this patch is to separate free_zspage from putback_zspage and free zspage out of class->lock which is preparation for zspage migration.

---
 mm/zsmalloc.c | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)
mm/zsmalloc.c +11 -16
index b24842bd4537..8fc16cc4d76d 100644
@@ -1683,14 +1683,12 @@ static struct zspage *isolate_zspage(struct size_class *class, bool source)
 
 /*
  * putback_zspage - add @zspage into right class's fullness list
- * @pool: target pool
  * @class: destination class
  * @zspage: target page
  *
  * Return @zspage's fullness_group
  */
-static enum fullness_group putback_zspage(struct zs_pool *pool,
-			struct size_class *class,
+static enum fullness_group putback_zspage(struct size_class *class,
 			struct zspage *zspage)
 {
 	enum fullness_group fullness;
@@ -1699,15 +1697,6 @@ static enum fullness_group putback_zspage(struct zs_pool *pool,
 	insert_zspage(class, zspage, fullness);
 	set_zspage_mapping(zspage, class->index, fullness);
 
-	if (fullness == ZS_EMPTY) {
-		zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
-			class->size, class->pages_per_zspage));
-		atomic_long_sub(class->pages_per_zspage,
-				&pool->pages_allocated);
-
-		free_zspage(pool, zspage);
-	}
-
 	return fullness;
 }
 
@@ -1756,23 +1745,29 @@ static void __zs_compact(struct zs_pool *pool, struct size_class *class)
 			if (!migrate_zspage(pool, class, &cc))
 				break;
 
-			putback_zspage(pool, class, dst_zspage);
+			putback_zspage(class, dst_zspage);
 		}
 
 		/* Stop if we couldn't find slot */
 		if (dst_zspage == NULL)
 			break;
 
-		putback_zspage(pool, class, dst_zspage);
-		if (putback_zspage(pool, class, src_zspage) == ZS_EMPTY)
+		putback_zspage(class, dst_zspage);
+		if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
+			zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
+					class->size, class->pages_per_zspage));
+			atomic_long_sub(class->pages_per_zspage,
+					&pool->pages_allocated);
+			free_zspage(pool, src_zspage);
 			pool->stats.pages_compacted += class->pages_per_zspage;
+		}
 		spin_unlock(&class->lock);
 		cond_resched();
 		spin_lock(&class->lock);
 	}
 
 	if (src_zspage)
-		putback_zspage(pool, class, src_zspage);
+		putback_zspage(class, src_zspage);
 
 	spin_unlock(&class->lock);
 }
Signature · 7 lines
1.9.1

--
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>
author maintainer
#49 lore raw

PATCH v6 10/12 zsmalloc: use freeobj for index

In reply to Minchan Kim

Zsmalloc stores first free object's <PFN, obj_idx> position into freeobj in each zspage. If we change it with index from first_page instead of position, it makes page migration simple because we don't need to correct other entries for linked list if a page is migrated out.

Cc Sergey Senozhatsky <[email protected]>
---
 mm/zsmalloc.c | 139 ++++++++++++++++++++++++++++++----------------------------
 1 file changed, 73 insertions(+), 66 deletions(-)
mm/zsmalloc.c +73 -66
index 8fc16cc4d76d..a5da5edfc1d0 100644
@@ -69,9 +69,7 @@
  * Object location (<PFN>, <obj_idx>) is encoded as
  * as single (unsigned long) handle value.
  *
- * Note that object index <obj_idx> is relative to system
- * page <PFN> it is stored in, so for each sub-page belonging
- * to a zspage, obj_idx starts with 0.
+ * Note that object index <obj_idx> starts from 0.
  *
  * This is made more complicated by various memory models and PAE.
  */
@@ -212,10 +210,10 @@ struct size_class {
 struct link_free {
 	union {
 		/*
-		 * Position of next free chunk (encodes <PFN, obj_idx>)
+		 * Free object index;
 		 * It's valid for non-allocated object
 		 */
-		void *next;
+		unsigned long next;
 		/*
 		 * Handle of allocated object.
 		 */
@@ -259,7 +257,7 @@ struct zspage {
 		unsigned int class:CLASS_BITS;
 	};
 	unsigned int inuse;
-	void *freeobj;
+	unsigned int freeobj;
 	struct page *first_page;
 	struct list_head list; /* fullness list */
 };
@@ -457,14 +455,14 @@ static inline void set_first_obj_offset(struct page *page, int offset)
 	page->index = offset;
 }
 
-static inline unsigned long get_freeobj(struct zspage *zspage)
+static inline unsigned int get_freeobj(struct zspage *zspage)
 {
-	return (unsigned long)zspage->freeobj;
+	return zspage->freeobj;
 }
 
-static inline void set_freeobj(struct zspage *zspage, unsigned long obj)
+static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
 {
-	zspage->freeobj = (void *)obj;
+	zspage->freeobj = obj;
 }
 
 static void get_zspage_mapping(struct zspage *zspage,
@@ -806,6 +804,10 @@ static int get_pages_per_zspage(int class_size)
 	return max_usedpc_order;
 }
 
+static struct page *get_first_page(struct zspage *zspage)
+{
+	return zspage->first_page;
+}
 
 static struct zspage *get_zspage(struct page *page)
 {
@@ -817,37 +819,33 @@ static struct page *get_next_page(struct page *page)
 	return page->next;
 }
 
-/*
- * Encode <page, obj_idx> as a single handle value.
- * We use the least bit of handle for tagging.
+/**
+ * obj_to_location - get (<page>, <obj_idx>) from encoded object value
+ * @page: page object resides in zspage
+ * @obj_idx: object index
  */
-static void *location_to_obj(struct page *page, unsigned long obj_idx)
+static void obj_to_location(unsigned long obj, struct page **page,
+				unsigned int *obj_idx)
 {
-	unsigned long obj;
+	obj >>= OBJ_TAG_BITS;
+	*page = pfn_to_page(obj >> OBJ_INDEX_BITS);
+	*obj_idx = (obj & OBJ_INDEX_MASK);
+}
 
-	if (!page) {
-		VM_BUG_ON(obj_idx);
-		return NULL;
-	}
+/**
+ * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
+ * @page: page object resides in zspage
+ * @obj_idx: object index
+ */
+static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
+{
+	unsigned long obj;
 
 	obj = page_to_pfn(page) << OBJ_INDEX_BITS;
-	obj |= ((obj_idx) & OBJ_INDEX_MASK);
+	obj |= obj_idx & OBJ_INDEX_MASK;
 	obj <<= OBJ_TAG_BITS;
 
-	return (void *)obj;
-}
-
-/*
- * Decode <page, obj_idx> pair from the given object handle. We adjust the
- * decoded obj_idx back to its original value since it was adjusted in
- * location_to_obj().
- */
-static void obj_to_location(unsigned long obj, struct page **page,
-				unsigned long *obj_idx)
-{
-	obj >>= OBJ_TAG_BITS;
-	*page = pfn_to_page(obj >> OBJ_INDEX_BITS);
-	*obj_idx = (obj & OBJ_INDEX_MASK);
+	return obj;
 }
 
 static unsigned long handle_to_obj(unsigned long handle)
@@ -865,16 +863,6 @@ static unsigned long obj_to_head(struct size_class *class, struct page *page,
 		return *(unsigned long *)obj;
 }
 
-static unsigned long obj_idx_to_offset(struct page *page,
-				unsigned long obj_idx, int class_size)
-{
-	unsigned long off;
-
-	off = get_first_obj_offset(page);
-
-	return off + obj_idx * class_size;
-}
-
 static inline int trypin_tag(unsigned long handle)
 {
 	return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
@@ -918,13 +906,13 @@ static void free_zspage(struct zs_pool *pool, struct zspage *zspage)
 /* Initialize a newly allocated zspage */
 static void init_zspage(struct size_class *class, struct zspage *zspage)
 {
+	unsigned int freeobj = 1;
 	unsigned long off = 0;
 	struct page *page = zspage->first_page;
 
 	while (page) {
 		struct page *next_page;
 		struct link_free *link;
-		unsigned int i = 1;
 		void *vaddr;
 
 		set_first_obj_offset(page, off);
@@ -933,7 +921,7 @@ static void init_zspage(struct size_class *class, struct zspage *zspage)
 		link = (struct link_free *)vaddr + off / sizeof(*link);
 
 		while ((off += class->size) < PAGE_SIZE) {
-			link->next = location_to_obj(page, i++);
+			link->next = freeobj++ << OBJ_ALLOCATED_TAG;
 			link += class->size / sizeof(*link);
 		}
 
@@ -943,14 +931,21 @@ static void init_zspage(struct size_class *class, struct zspage *zspage)
 		 * page (if present)
 		 */
 		next_page = get_next_page(page);
-		link->next = location_to_obj(next_page, 0);
+		if (next_page) {
+			link->next = freeobj++ << OBJ_ALLOCATED_TAG;
+		} else {
+			/*
+			 * Reset OBJ_ALLOCATED_TAG bit to last link to tell
+			 * whether it's allocated object or not.
+			 */
+			link->next = -1 << OBJ_ALLOCATED_TAG;
+		}
 		kunmap_atomic(vaddr);
 		page = next_page;
 		off %= PAGE_SIZE;
 	}
 
-	set_freeobj(zspage,
-		(unsigned long)location_to_obj(zspage->first_page, 0));
+	set_freeobj(zspage, 0);
 }
 
 static void create_page_chain(struct zspage *zspage, struct page *pages[],
@@ -1267,7 +1262,8 @@ void *zs_map_object(struct zs_pool *pool, unsigned long handle,
 {
 	struct zspage *zspage;
 	struct page *page;
-	unsigned long obj, obj_idx, off;
+	unsigned long obj, off;
+	unsigned int obj_idx;
 
 	unsigned int class_idx;
 	enum fullness_group fg;
@@ -1291,7 +1287,7 @@ void *zs_map_object(struct zs_pool *pool, unsigned long handle,
 	zspage = get_zspage(page);
 	get_zspage_mapping(zspage, &class_idx, &fg);
 	class = pool->size_class[class_idx];
-	off = obj_idx_to_offset(page, obj_idx, class->size);
+	off = (class->size * obj_idx) & ~PAGE_MASK;
 
 	area = &get_cpu_var(zs_map_area);
 	area->vm_mm = mm;
@@ -1320,7 +1316,8 @@ void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
 {
 	struct zspage *zspage;
 	struct page *page;
-	unsigned long obj, obj_idx, off;
+	unsigned long obj, off;
+	unsigned int obj_idx;
 
 	unsigned int class_idx;
 	enum fullness_group fg;
@@ -1332,7 +1329,7 @@ void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
 	zspage = get_zspage(page);
 	get_zspage_mapping(zspage, &class_idx, &fg);
 	class = pool->size_class[class_idx];
-	off = obj_idx_to_offset(page, obj_idx, class->size);
+	off = (class->size * obj_idx) & ~PAGE_MASK;
 
 	area = this_cpu_ptr(&zs_map_area);
 	if (off + class->size <= PAGE_SIZE)
@@ -1354,21 +1351,28 @@ EXPORT_SYMBOL_GPL(zs_unmap_object);
 static unsigned long obj_malloc(struct size_class *class,
 				struct zspage *zspage, unsigned long handle)
 {
+	int i, nr_page, offset;
 	unsigned long obj;
 	struct link_free *link;
 
 	struct page *m_page;
-	unsigned long m_objidx, m_offset;
+	unsigned long m_offset;
 	void *vaddr;
 
 	handle |= OBJ_ALLOCATED_TAG;
 	obj = get_freeobj(zspage);
-	obj_to_location(obj, &m_page, &m_objidx);
-	m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
+
+	offset = obj * class->size;
+	nr_page = offset >> PAGE_SHIFT;
+	m_offset = offset & ~PAGE_MASK;
+	m_page = get_first_page(zspage);
+
+	for (i = 0; i < nr_page; i++)
+		m_page = get_next_page(m_page);
 
 	vaddr = kmap_atomic(m_page);
 	link = (struct link_free *)vaddr + m_offset / sizeof(*link);
-	set_freeobj(zspage, (unsigned long)link->next);
+	set_freeobj(zspage, link->next >> OBJ_ALLOCATED_TAG);
 	if (!class->huge)
 		/* record handle in the header of allocated chunk */
 		link->handle = handle;
@@ -1380,6 +1384,8 @@ static unsigned long obj_malloc(struct size_class *class,
 	mod_zspage_inuse(zspage, 1);
 	zs_stat_inc(class, OBJ_USED, 1);
 
+	obj = location_to_obj(m_page, obj);
+
 	return obj;
 }
 
@@ -1445,22 +1451,22 @@ static void obj_free(struct size_class *class, unsigned long obj)
 	struct link_free *link;
 	struct zspage *zspage;
 	struct page *f_page;
-	unsigned long f_objidx, f_offset;
+	unsigned long f_offset;
+	unsigned int f_objidx;
 	void *vaddr;
 
 	obj &= ~OBJ_ALLOCATED_TAG;
 	obj_to_location(obj, &f_page, &f_objidx);
+	f_offset = (class->size * f_objidx) & ~PAGE_MASK;
 	zspage = get_zspage(f_page);
 
-	f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
-
 	vaddr = kmap_atomic(f_page);
 
 	/* Insert this object in containing zspage's freelist */
 	link = (struct link_free *)(vaddr + f_offset);
-	link->next = (void *)get_freeobj(zspage);
+	link->next = get_freeobj(zspage) << OBJ_ALLOCATED_TAG;
 	kunmap_atomic(vaddr);
-	set_freeobj(zspage, obj);
+	set_freeobj(zspage, f_objidx);
 	mod_zspage_inuse(zspage, -1);
 	zs_stat_dec(class, OBJ_USED, 1);
 }
@@ -1469,7 +1475,8 @@ void zs_free(struct zs_pool *pool, unsigned long handle)
 {
 	struct zspage *zspage;
 	struct page *f_page;
-	unsigned long obj, f_objidx;
+	unsigned long obj;
+	unsigned int f_objidx;
 	int class_idx;
 	struct size_class *class;
 	enum fullness_group fullness;
@@ -1506,7 +1513,7 @@ static void zs_object_copy(struct size_class *class, unsigned long dst,
 				unsigned long src)
 {
 	struct page *s_page, *d_page;
-	unsigned long s_objidx, d_objidx;
+	unsigned int s_objidx, d_objidx;
 	unsigned long s_off, d_off;
 	void *s_addr, *d_addr;
 	int s_size, d_size, size;
@@ -1517,8 +1524,8 @@ static void zs_object_copy(struct size_class *class, unsigned long dst,
 	obj_to_location(src, &s_page, &s_objidx);
 	obj_to_location(dst, &d_page, &d_objidx);
 
-	s_off = obj_idx_to_offset(s_page, s_objidx, class->size);
-	d_off = obj_idx_to_offset(d_page, d_objidx, class->size);
+	s_off = (class->size * s_objidx) & ~PAGE_MASK;
+	d_off = (class->size * d_objidx) & ~PAGE_MASK;
 
 	if (s_off + class->size > PAGE_SIZE)
 		s_size = PAGE_SIZE - s_off;
Signature · 7 lines
1.9.1

--
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>
author maintainer
#50 lore raw

PATCH v6 11/12 zsmalloc: page migration support

In reply to Minchan Kim

This patch introduces run-time migration feature for zspage.

For migration, VM uses page.lru field so it would be better to not use page.next field which is unified with page.lru for own purpose. For that, firstly, we can get first object offset of the page via runtime calculation instead of using page.index so we can use page.index as link for page chaining instead of page.next.

In case of huge object, it stores handle to page.index instead of next link of page chaining because huge object doesn't need to next link for page chaining. So get_next_page need to identify huge object to return NULL. For it, this patch uses PG_owner_priv_1 flag of the page flag.

For migration, it supports three functions

* zs_page_isolate

It isolates a zspage which includes a subpage VM want to migrate from class so anyone cannot allocate new object from the zspage.

We could try to isolate a zspage by the number of subpage so subsequent isolation trial of other subpage of the zpsage shouldn't fail. For that, we introduce zspage.isolated count. With that, zs_page_isolate can know whether zspage is already isolated or not for migration so if it is isolated for migration, subsequent isolation trial can be successful without trying further isolation.

* zs_page_migrate

First of all, it holds write-side zspage->lock to prevent migrate other subpage in zspage. Then, lock all objects in the page VM want to migrate. The reason we should lock all objects in the page is due to race between zs_map_object and zs_page_migrate.

zs_map_object zs_page_migrate

pin_tag(handle)
obj = handle_to_obj(handle)
obj_to_location(obj, &page, &obj_idx);

					write_lock(&zspage->lock)
					if (!trypin_tag(handle))
						goto unpin_object

zspage = get_zspage(page);
read_lock(&zspage->lock);

If zs_page_migrate doesn't do trypin_tag, zs_map_object's page can be stale by migration so it goes crash.

If it locks all of objects successfully, it copies content from old page to new one, finally, create new zspage chain with new page. And if it's last isolated subpage in the zspage, put the zspage back to class.

* zs_page_putback

It returns isolated zspage to right fullness_group list if it fails to migrate a page. If it find a zspage is ZS_EMPTY, it queues zspage freeing to workqueue. See below about async zspage freeing.

This patch introduces asynchronous zspage free. The reason to need it is we need page_lock to clear PG_movable but unfortunately, zs_free path should be atomic so the apporach is try to grab page_lock. If it got page_lock of all of pages successfully, it can free zspage immediately. Otherwise, it queues free request and free zspage via workqueue in process context.

If zs_free finds the zspage is isolated when it try to free zspage, it delays the freeing until zs_page_putback finds it so it will free free the zspage finally.

In this patch, we expand fullness_list from ZS_EMPTY to ZS_FULL. First of all, it will use ZS_EMPTY list for delay freeing. And with adding ZS_FULL list, it makes to identify whether zspage is isolated or not via list_empty(&zspage->list) test.

Cc Sergey Senozhatsky <[email protected]>
---
 include/uapi/linux/magic.h |   1 +
 mm/zsmalloc.c              | 795 ++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 674 insertions(+), 122 deletions(-)
include/uapi/linux/magic.h +1 -0
index d829ce63529d..e398beac67b8 100644
@@ -81,5 +81,6 @@
 /* Since UDF 2.01 is ISO 13346 based... */
 #define UDF_SUPER_MAGIC		0x15013346
 #define BALLOON_KVM_MAGIC	0x13661366
+#define ZSMALLOC_MAGIC		0x58295829
 
 #endif /* __LINUX_MAGIC_H__ */
mm/zsmalloc.c +673 -122

1,217 lines not on this page · raw message

Signature · 1 line
1.9.1
#51 lore raw

Re: [PATCH v6 11/12] zsmalloc: page migration support

On (05/20/16 23:23), Minchan Kim wrote:
[..]

mm/zsmalloc.c static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage) · line 480

Quoted 5 lines
mm/zsmalloc.c +5 -0
+static int get_zspage_isolation(struct zspage *zspage)
+{
+	return zspage->isolated;
+}
+

may be is_zspage_isolated()?

[..]

Quoted 15 lines
mm/zsmalloc.c +2 -4
@@ -502,23 +556,19 @@ static int get_size_class_index(int size)
 static inline void zs_stat_inc(struct size_class *class,
 				enum zs_stat_type type, unsigned long cnt)
 {
-	if (type < NR_ZS_STAT_TYPE)
-		class->stats.objs[type] += cnt;
+	class->stats.objs[type] += cnt;
 }
 
 static inline void zs_stat_dec(struct size_class *class,
 				enum zs_stat_type type, unsigned long cnt)
 {
-	if (type < NR_ZS_STAT_TYPE)
-		class->stats.objs[type] -= cnt;
+	class->stats.objs[type] -= cnt;

mm/zsmalloc.c static int get_size_class_index(int size) · line 572

Quoted 10 lines
mm/zsmalloc.c +1 -3
 }
 
 static inline unsigned long zs_stat_get(struct size_class *class,
 				enum zs_stat_type type)
 {
-	if (type < NR_ZS_STAT_TYPE)
-		return class->stats.objs[type];
-	return 0;
+	return class->stats.objs[type];
 }

hmm... the ordering of STAT types and those if-conditions were here for a reason:

commit 6fe5186f0c7c18a8beb6d96c21e2390df7a12375

Author: Sergey Senozhatsky <[email protected]>
Date:   Fri Nov 6 16:29:38 2015 -0800

zsmalloc: reduce size_class memory usage

Each struct size_class' contains struct zs_size_stat': an array of NR_ZS_STAT_TYPE unsigned long'. For zsmalloc built with no CONFIG_ZSMALLOC_STAT this results in a waste of 2 * sizeof(unsigned long)' per-class.

The patch removes unneeded `struct zs_size_stat' members by redefining NR_ZS_STAT_TYPE (max stat idx in array).

Since both NR_ZS_STAT_TYPE and zs_stat_type are compile time constants, GCC can eliminate zs_stat_inc()/zs_stat_dec() calls that use zs_stat_type larger than NR_ZS_STAT_TYPE: CLASS_ALMOST_EMPTY and CLASS_ALMOST_FULL at the moment.

./scripts/bloat-o-meter mm/zsmalloc.o.old mm/zsmalloc.o.new add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-39 (-39)
function old new delta
fix_fullness_group 97 94 -3
insert_zspage 100 86 -14
remove_zspage 141 119 -22

To summarize:
a) each class now uses less memory
b) we avoid a number of dec/inc stats (a minor optimization, but still).

The gain will increase once we introduce additional stats.

so it helped to eliminate instructions at compile time from a very hot path for !CONFIG_ZSMALLOC_STAT builds (which is 99% of the builds I think, I doubt anyone apart from us is using ZSMALLOC_STAT).

[..]

Quoted 8 lines
mm/zsmalloc.c +6 -1
+static int get_first_obj_offset(struct size_class *class,
+				struct page *first_page, struct page *page)
 {
-	return page->next;
+	int pos, bound;
+	int page_idx = 0;
+	int ofs = 0;
+	struct page *cursor = first_page;

mm/zsmalloc.c static int get_pages_per_zspage(int class_size) · line 885

Quoted 10 lines
mm/zsmalloc.c +10 -0
+
+	if (first_page == page)
+		goto out;
+
+	while (page != cursor) {
+		page_idx++;
+		cursor = get_next_page(cursor);
+	}
+
+	bound = PAGE_SIZE * page_idx;

'bound' not used.

mm/zsmalloc.c static int get_pages_per_zspage(int class_size) · line 888

Quoted 3 lines
mm/zsmalloc.c +3 -0
+	pos = (((class->objs_per_zspage * class->size) *
+		page_idx / class->pages_per_zspage) / class->size
+	      ) * class->size;

something went wrong with the indentation here :)

so... it's

	(((class->objs_per_zspage * class->size) * page_idx / class->pages_per_zspage) / class->size ) * class->size;

the last ' / class->size ) * class->size' can be dropped, I think.

[..]

mm/zsmalloc.c static enum fullness_group putback_zspage(struct size_class *class, · line 2076

Quoted 5 lines
mm/zsmalloc.c +5 -0
+		pos += class->size;
+	}
+
+	/*
+	 * Here, any user cannot access all objects in the zspage so let's move.

"no one can access any object" ?

[..]

mm/zsmalloc.c static enum fullness_group putback_zspage(struct size_class *class, · line 2162

Quoted 10 lines
mm/zsmalloc.c +10 -0
+	spin_lock(&class->lock);
+	dec_zspage_isolation(zspage);
+	if (!get_zspage_isolation(zspage)) {
+		fg = putback_zspage(class, zspage);
+		/*
+		 * Due to page_lock, we cannot free zspage immediately
+		 * so let's defer.
+		 */
+		if (fg == ZS_EMPTY)
+			schedule_work(&pool->free_work);

hm... zsmalloc is getting sooo complex now.

`system_wq' -- can we have problems here when the system is getting low on memory and workers are getting increasingly busy trying to allocate the memory for some other purposes?

_theoretically_ zsmalloc can stack a number of ready-to-release zspages, which won't be accessible to zsmalloc, nor will they be released. how likely is this? hm, can zsmalloc take zspages from that deferred release list when it wants to allocate a new zspage?

do you also want to kick the deferred page release from the shrinker callback, for example?

	-ss

--
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>

author maintainer
#52 lore raw

Re: [PATCH v6 11/12] zsmalloc: page migration support

Quoted 9 lines · Sergey Senozhatsky

On (05/20/16 23:23), Minchan Kim wrote:
[..]

mm/zsmalloc.c static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage) · line 480

Quoted 5 lines
mm/zsmalloc.c +5 -0
+static int get_zspage_isolation(struct zspage *zspage)
+{
+	return zspage->isolated;
+}
+

may be is_zspage_isolated()?

Now, it would be better. I will change it.

Quoted 62 lines

[..]

Quoted 15 lines
mm/zsmalloc.c +2 -4
@@ -502,23 +556,19 @@ static int get_size_class_index(int size)
 static inline void zs_stat_inc(struct size_class *class,
 				enum zs_stat_type type, unsigned long cnt)
 {
-	if (type < NR_ZS_STAT_TYPE)
-		class->stats.objs[type] += cnt;
+	class->stats.objs[type] += cnt;
 }
 
 static inline void zs_stat_dec(struct size_class *class,
 				enum zs_stat_type type, unsigned long cnt)
 {
-	if (type < NR_ZS_STAT_TYPE)
-		class->stats.objs[type] -= cnt;
+	class->stats.objs[type] -= cnt;

mm/zsmalloc.c static int get_size_class_index(int size) · line 572

Quoted 10 lines
mm/zsmalloc.c +1 -3
 }
 
 static inline unsigned long zs_stat_get(struct size_class *class,
 				enum zs_stat_type type)
 {
-	if (type < NR_ZS_STAT_TYPE)
-		return class->stats.objs[type];
-	return 0;
+	return class->stats.objs[type];
 }

hmm... the ordering of STAT types and those if-conditions were here for a reason:

commit 6fe5186f0c7c18a8beb6d96c21e2390df7a12375

Author: Sergey Senozhatsky <[email protected]>
Date:   Fri Nov 6 16:29:38 2015 -0800

zsmalloc: reduce size_class memory usage

Each struct size_class' contains struct zs_size_stat': an array of NR_ZS_STAT_TYPE unsigned long'. For zsmalloc built with no CONFIG_ZSMALLOC_STAT this results in a waste of 2 * sizeof(unsigned long)' per-class.

The patch removes unneeded `struct zs_size_stat' members by redefining NR_ZS_STAT_TYPE (max stat idx in array).

Since both NR_ZS_STAT_TYPE and zs_stat_type are compile time constants, GCC can eliminate zs_stat_inc()/zs_stat_dec() calls that use zs_stat_type larger than NR_ZS_STAT_TYPE: CLASS_ALMOST_EMPTY and CLASS_ALMOST_FULL at the moment.

./scripts/bloat-o-meter mm/zsmalloc.o.old mm/zsmalloc.o.new add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-39 (-39)
function old new delta
fix_fullness_group 97 94 -3
insert_zspage 100 86 -14
remove_zspage 141 119 -22

To summarize:
a) each class now uses less memory
b) we avoid a number of dec/inc stats (a minor optimization, but still).

Quoted 5 lines

The gain will increase once we introduce additional stats.

so it helped to eliminate instructions at compile time from a very hot path for !CONFIG_ZSMALLOC_STAT builds (which is 99% of the builds I think, I doubt anyone apart from us is using ZSMALLOC_STAT).

Most important point to me is that it makes code *simple* at the cost of addtional wasting memory. Now, every zspage lives in *a* list so we don't need to check zspage groupness to use list_empty of zspage. I'm not sure how you feel it makes code simple a lot. However, while I implement page migration logic, the check with condition that zspage's groupness is either almost_empty and almost_full is really bogus and tricky to me so I should debug several time to find what's wrong.

Compared to old, zsmalloc is complicated day by day so I want to weight on *simple* for easy maintainance.

One more note:
Now, ZS_EMPTY is used as pool. Look at find_get_zspage. So adding "empty" column in ZSMALLOC_STAT might be worth but I wanted to handle it as another topic.

So if you don't feel strong the saving is really huge, I want to go with this. And if we are adding more wasted memory in future, let's handle it then.

About CONFIG_ZSMALLOC_STAT, It might be off-topic. Frankly speaking, I have guided production team to enable it because when I profile the overhead caused by ZSMALLOC_STAT, there is no performance lost in real workload. However, the stat gives more detailed useful information.

Quoted 15 lines

[..]

Quoted 12 lines
mm/zsmalloc.c +10 -1
+static int get_first_obj_offset(struct size_class *class,
+				struct page *first_page, struct page *page)
 {
-	return page->next;
+	int pos, bound;
+	int page_idx = 0;
+	int ofs = 0;
+	struct page *cursor = first_page;
+
+	if (first_page == page)
+		goto out;
+
Quoted 8 lines

mm/zsmalloc.c static int get_pages_per_zspage(int class_size) · line 885

Quoted 6 lines
mm/zsmalloc.c +6 -0
+	while (page != cursor) {
+		page_idx++;
+		cursor = get_next_page(cursor);
+	}
+
+	bound = PAGE_SIZE * page_idx;

'bound' not used.

-_-;;

Quoted 9 lines

mm/zsmalloc.c static int get_pages_per_zspage(int class_size) · line 888

Quoted 5 lines
mm/zsmalloc.c +3 -0
+	pos = (((class->objs_per_zspage * class->size) *
+		page_idx / class->pages_per_zspage) / class->size
+	      ) * class->size;

something went wrong with the indentation here :)

Quoted 5 lines

so... it's

	(((class->objs_per_zspage * class->size) * page_idx / class->pages_per_zspage) / class->size ) * class->size;

the last ' / class->size ) * class->size' can be dropped, I think.

You prove I didn't learn math.
Will drop it.

Quoted 23 lines

[..]

mm/zsmalloc.c static enum fullness_group putback_zspage(struct size_class *class, · line 2076

Quoted 5 lines
mm/zsmalloc.c +5 -0
+		pos += class->size;
+	}
+
+	/*
+	 * Here, any user cannot access all objects in the zspage so let's move.

"no one can access any object" ?

[..]

mm/zsmalloc.c static enum fullness_group putback_zspage(struct size_class *class, · line 2162

Quoted 10 lines
mm/zsmalloc.c +10 -0
+	spin_lock(&class->lock);
+	dec_zspage_isolation(zspage);
+	if (!get_zspage_isolation(zspage)) {
+		fg = putback_zspage(class, zspage);
+		/*
+		 * Due to page_lock, we cannot free zspage immediately
+		 * so let's defer.
+		 */
+		if (fg == ZS_EMPTY)
+			schedule_work(&pool->free_work);

hm... zsmalloc is getting sooo complex now.

Quoted 8 lines

`system_wq' -- can we have problems here when the system is getting low on memory and workers are getting increasingly busy trying to allocate the memory for some other purposes?

_theoretically_ zsmalloc can stack a number of ready-to-release zspages, which won't be accessible to zsmalloc, nor will they be released. how likely is this? hm, can zsmalloc take zspages from that deferred release list when it wants to allocate a new zspage?

Done.

Quoted 3 lines

do you also want to kick the deferred page release from the shrinker callback, for example?

Yeb, it can be. I will do it at next revision. :)
Thanks!

Quoted 2 lines
	-ss

--
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>

#53 lore raw

Re: [PATCH v6 11/12] zsmalloc: page migration support

Hello,

On (05/24/16 15:28), Minchan Kim wrote:
[..]

Quoted 12 lines

Most important point to me is that it makes code *simple* at the cost of addtional wasting memory. Now, every zspage lives in *a* list so we don't need to check zspage groupness to use list_empty of zspage. I'm not sure how you feel it makes code simple a lot. However, while I implement page migration logic, the check with condition that zspage's groupness is either almost_empty and almost_full is really bogus and tricky to me so I should debug several time to find what's wrong.

Compared to old, zsmalloc is complicated day by day so I want to weight on *simple* for easy maintainance.

Quoted 8 lines

One more note:
Now, ZS_EMPTY is used as pool. Look at find_get_zspage. So adding "empty" column in ZSMALLOC_STAT might be worth but I wanted to handle it as another topic.

So if you don't feel strong the saving is really huge, I want to go with this. And if we are adding more wasted memory in future, let's handle it then.

oh, sure, all those micro-optimizations can be done later, off the series.

Quoted 5 lines

About CONFIG_ZSMALLOC_STAT, It might be off-topic. Frankly speaking, I have guided production team to enable it because when I profile the overhead caused by ZSMALLOC_STAT, there is no performance lost in real workload. However, the stat gives more detailed useful information.

ok, agree.
good to know that you use stats in production, by the way.

[..]

Quoted 7 lines
Quoted 7 lines

mm/zsmalloc.c static int get_pages_per_zspage(int class_size) · line 888

Quoted 3 lines
mm/zsmalloc.c +3 -0
+	pos = (((class->objs_per_zspage * class->size) *
+		page_idx / class->pages_per_zspage) / class->size
+	      ) * class->size;

something went wrong with the indentation here :)

Quoted 8 lines
Quoted 5 lines

so... it's

	(((class->objs_per_zspage * class->size) * page_idx / class->pages_per_zspage) / class->size ) * class->size;

the last ' / class->size ) * class->size' can be dropped, I think.

You prove I didn't learn math.
Will drop it.

haha, no, that wasn't the point :) great job with the series!

[..]

Quoted 6 lines
Quoted 6 lines

hm... zsmalloc is getting sooo complex now.

`system_wq' -- can we have problems here when the system is getting low on memory and workers are getting increasingly busy trying to allocate the memory for some other purposes?

Quoted 6 lines
Quoted 4 lines

_theoretically_ zsmalloc can stack a number of ready-to-release zspages, which won't be accessible to zsmalloc, nor will they be released. how likely is this? hm, can zsmalloc take zspages from that deferred release list when it wants to allocate a new zspage?

Done.

oh, good. that was a purely theoretical thing, and to continue with the theories, I assume that zs_malloc() will improve with this change. the sort of kind of problem with zs_malloc(), *I think*, is that we release the class ->lock after failed find_get_zspage():

	handle = cache_alloc_handle(pool, gfp);
	if (!handle)
		return 0;

	zspage = find_get_zspage(class);
	if (likely(zspage)) {
		obj = obj_malloc(class, zspage, handle);
		[..]
		spin_unlock(&class->lock);

		return handle;
	}

	spin_unlock(&class->lock);

	zspage = alloc_zspage(pool, class, gfp);
	if (!zspage) {
		cache_free_handle(pool, handle);
		return 0;
	}

	spin_lock(&class->lock);
	obj = obj_malloc(class, zspage, handle);
	[..]
	spin_unlock(&class->lock);

_theoretically_, on a not-really-huge system, let's say 64 CPUs for example, we can have 64 write paths trying to store objects of size OBJ_SZ to a ZS_FULL class-OBJSZ. the write path (each of them) will fail on find_get_zspage(), unlock the class ->lock (so another write path will have its chance to fail on find_get_zspage()), alloc_zspage(), create a page chain, spin on class ->lock to add the new zspage to the class. so we can end up allocating up to 64 zspages, each of them will carry N PAGE_SIZE pages. those zspages, at least at the beginning, will store only one object per-zspage; which will blastoff the internal fragmentation and can cause more compaction/migration/etc later on. well, it's a bit pessimistic, but I think to _some extent_ this scenario is quite possible.

I assume that this "pick an already marked for release zspage" thing is happening as a fast path within the first class ->lock section, so the rest of concurrent write requests that are spinning on the class ->lock at the moment will see a zspage, instead of !find_get_zspage().

	-ss

--
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>

author maintainer
#54 lore raw

Re: [PATCH v6 11/12] zsmalloc: page migration support

Quoted 115 lines · Sergey Senozhatsky

Hello,

On (05/24/16 15:28), Minchan Kim wrote:
[..]

Quoted 12 lines

Most important point to me is that it makes code *simple* at the cost of addtional wasting memory. Now, every zspage lives in *a* list so we don't need to check zspage groupness to use list_empty of zspage. I'm not sure how you feel it makes code simple a lot. However, while I implement page migration logic, the check with condition that zspage's groupness is either almost_empty and almost_full is really bogus and tricky to me so I should debug several time to find what's wrong.

Compared to old, zsmalloc is complicated day by day so I want to weight on *simple* for easy maintainance.

Quoted 8 lines

One more note:
Now, ZS_EMPTY is used as pool. Look at find_get_zspage. So adding "empty" column in ZSMALLOC_STAT might be worth but I wanted to handle it as another topic.

So if you don't feel strong the saving is really huge, I want to go with this. And if we are adding more wasted memory in future, let's handle it then.

oh, sure, all those micro-optimizations can be done later, off the series.

Quoted 5 lines

About CONFIG_ZSMALLOC_STAT, It might be off-topic. Frankly speaking, I have guided production team to enable it because when I profile the overhead caused by ZSMALLOC_STAT, there is no performance lost in real workload. However, the stat gives more detailed useful information.

ok, agree.
good to know that you use stats in production, by the way.

[..]

Quoted 7 lines
Quoted 7 lines

mm/zsmalloc.c static int get_pages_per_zspage(int class_size) · line 888

Quoted 3 lines
mm/zsmalloc.c +3 -0
+	pos = (((class->objs_per_zspage * class->size) *
+		page_idx / class->pages_per_zspage) / class->size
+	      ) * class->size;

something went wrong with the indentation here :)

Quoted 8 lines
Quoted 5 lines

so... it's

	(((class->objs_per_zspage * class->size) * page_idx / class->pages_per_zspage) / class->size ) * class->size;

the last ' / class->size ) * class->size' can be dropped, I think.

You prove I didn't learn math.
Will drop it.

haha, no, that wasn't the point :) great job with the series!

[..]

Quoted 6 lines
Quoted 6 lines

hm... zsmalloc is getting sooo complex now.

`system_wq' -- can we have problems here when the system is getting low on memory and workers are getting increasingly busy trying to allocate the memory for some other purposes?

Quoted 6 lines
Quoted 4 lines

_theoretically_ zsmalloc can stack a number of ready-to-release zspages, which won't be accessible to zsmalloc, nor will they be released. how likely is this? hm, can zsmalloc take zspages from that deferred release list when it wants to allocate a new zspage?

Done.

oh, good. that was a purely theoretical thing, and to continue with the theories, I assume that zs_malloc() will improve with this change. the sort of kind of problem with zs_malloc(), *I think*, is that we release the class ->lock after failed find_get_zspage():

	handle = cache_alloc_handle(pool, gfp);
	if (!handle)
		return 0;

	zspage = find_get_zspage(class);
	if (likely(zspage)) {
		obj = obj_malloc(class, zspage, handle);
		[..]
		spin_unlock(&class->lock);

		return handle;
	}

	spin_unlock(&class->lock);

	zspage = alloc_zspage(pool, class, gfp);
	if (!zspage) {
		cache_free_handle(pool, handle);
		return 0;
	}

	spin_lock(&class->lock);
	obj = obj_malloc(class, zspage, handle);
	[..]
	spin_unlock(&class->lock);

_theoretically_, on a not-really-huge system, let's say 64 CPUs for example, we can have 64 write paths trying to store objects of size OBJ_SZ to a ZS_FULL class-OBJSZ. the write path (each of them) will fail on find_get_zspage(), unlock the class ->lock (so another write path will have its chance to fail on find_get_zspage()), alloc_zspage(), create a page chain, spin on class ->lock to add the new zspage to the class. so we can end up allocating up to 64 zspages, each of them will carry N PAGE_SIZE pages. those zspages, at least at the beginning, will store only one object per-zspage; which will blastoff the internal fragmentation and can cause more compaction/migration/etc later on. well, it's a bit pessimistic, but I think to _some extent_ this scenario is quite possible.

Quoted 4 lines

I assume that this "pick an already marked for release zspage" thing is happening as a fast path within the first class ->lock section, so the rest of concurrent write requests that are spinning on the class ->lock at the moment will see a zspage, instead of !find_get_zspage().

As well, we would reduce page alloc/free cost although it's not expensive compared to comp overhead. :)

Thanks for giving the thought!

--
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>

author maintainer
#55 lore raw

Re: [PATCH v6 11/12] zsmalloc: page migration support

In reply to Minchan Kim

Hello Sergey,

On Tue, May 24, 2016 at 03:28:01PM +0900, Minchan Kim wrote:
<snip>

Quoted 11 lines
Quoted 10 lines

hm... zsmalloc is getting sooo complex now.

`system_wq' -- can we have problems here when the system is getting low on memory and workers are getting increasingly busy trying to allocate the memory for some other purposes?

_theoretically_ zsmalloc can stack a number of ready-to-release zspages, which won't be accessible to zsmalloc, nor will they be released. how likely is this? hm, can zsmalloc take zspages from that deferred release list when it wants to allocate a new zspage?

Quoted 9 lines

Done.

Quoted 3 lines

do you also want to kick the deferred page release from the shrinker callback, for example?

Yeb, it can be. I will do it at next revision. :)
Thanks!

I tried it now but I feel strongly we want to fix shrinker first. Now, shrinker doesn't consider VM's request(i.e., sc->nr_to_scan) but shrink all objects which could make latency huge.

I want to fix it as another issue and then adding ZS_EMPTY pool pages purging logic based on it because many works for zsmalloc stucked with this patchset now which churns old code heavily. :(

--
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>

#56 lore raw

Re: [PATCH v6 11/12] zsmalloc: page migration support

Hello Minchan,

On (05/25/16 14:14), Minchan Kim wrote:
[..]

Quoted 10 lines
Quoted 6 lines
Quoted 2 lines

do you also want to kick the deferred page release from the shrinker callback, for example?

Yeb, it can be. I will do it at next revision. :)
Thanks!

I tried it now but I feel strongly we want to fix shrinker first. Now, shrinker doesn't consider VM's request(i.e., sc->nr_to_scan) but shrink all objects which could make latency huge.

hm... may be.

I only briefly thought about it a while ago; and have no real data on hands. it was something as follows:
between zs_shrinker_count() and zs_shrinker_scan() a lot can change; and _theoretically_ attempting to zs_shrinker_scan() even a smaller sc->nr_to_scan still may result in a "full" pool scan, taking all of the classes ->locks one by one just because classes are not the same as a moment ago. which is even more probable, I think, once the system is getting low on memory and begins to swap out, for instance. because in the latter case we increase the number of writes to zspool and, thus, reduce its chances to be compacted. if the system would still demand free memory, then it'd keep calling zs_shrinker_count() and zs_shrinker_scan() on us; at some point, I think, zs_shrinker_count() would start returning 0. ...if some of the classes would have huge fragmentation then we'd keep these class' ->locks for some time, moving objects. other than that we probably would just iterate the classes.

purely theoretical.

do you have any numbers?

hm, probably it makes sense to change it. but if the change will replace "1 full pool scan" to "2 scans of 1/2 of pool's classes", then I'm less sure.

Quoted 3 lines

I want to fix it as another issue and then adding ZS_EMPTY pool pages purging logic based on it because many works for zsmalloc stucked with this patchset now which churns old code heavily. :(

	-ss

--
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>

author maintainer
#57 lore raw

Re: [PATCH v6 11/12] zsmalloc: page migration support

Hi Sergey,

Quoted 34 lines · Sergey Senozhatsky

Hello Minchan,

On (05/25/16 14:14), Minchan Kim wrote:
[..]

Quoted 10 lines
Quoted 6 lines
Quoted 2 lines

do you also want to kick the deferred page release from the shrinker callback, for example?

Yeb, it can be. I will do it at next revision. :)
Thanks!

I tried it now but I feel strongly we want to fix shrinker first. Now, shrinker doesn't consider VM's request(i.e., sc->nr_to_scan) but shrink all objects which could make latency huge.

hm... may be.

I only briefly thought about it a while ago; and have no real data on hands. it was something as follows:
between zs_shrinker_count() and zs_shrinker_scan() a lot can change; and _theoretically_ attempting to zs_shrinker_scan() even a smaller sc->nr_to_scan still may result in a "full" pool scan, taking all of the classes ->locks one by one just because classes are not the same as a moment ago. which is even more probable, I think, once the system is getting low on memory and begins to swap out, for instance. because in the latter case we increase the number of writes to zspool and, thus, reduce its chances to be compacted. if the system would still demand free memory, then it'd keep calling zs_shrinker_count() and zs_shrinker_scan() on us; at some point, I think, zs_shrinker_count() would start returning 0. ...if some of the classes would have huge fragmentation then we'd keep these class' ->locks for some time, moving objects. other than that we probably would just iterate the classes.

Quoted 3 lines

purely theoretical.

do you have any numbers?

Unfortunately, I don't have now. However, I don't feel we need a data for that because *unbounded work* within VM interaction context is bad. ;-)

Quoted 4 lines

hm, probably it makes sense to change it. but if the change will replace "1 full pool scan" to "2 scans of 1/2 of pool's classes", then I'm less sure.

Other shrinker is on same page. They have *cache* which is helpful for performance but if it's not hot, it can lose performance if memory pressure is severe. For the balance, comprimise approach is shrinker.

We can see fragement space in zspage as wasted memory which is bad on the other hand we can see it as cache to store upcoming compressed page. So, too much freeing can hurt the performance so, let's obey VM's rule. If memory pressure goes severe, they want to reclaim more pages with proportional of memory pressure.

Quoted 6 lines
Quoted 4 lines

I want to fix it as another issue and then adding ZS_EMPTY pool pages purging logic based on it because many works for zsmalloc stucked with this patchset now which churns old code heavily. :(

	-ss

--
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>

#58 lore raw

Re: [PATCH v6 11/12] zsmalloc: page migration support

Hello Minchan,

On (05/26/16 09:32), Minchan Kim wrote:
[..]

Quoted 2 lines

Unfortunately, I don't have now. However, I don't feel we need a data for that because *unbounded work* within VM interaction context is bad. ;-)

fair enough, even though the shrinker doesn't put any constraints here.

Quoted 8 lines
Quoted 3 lines

hm, probably it makes sense to change it. but if the change will replace "1 full pool scan" to "2 scans of 1/2 of pool's classes", then I'm less sure.

Other shrinker is on same page. They have *cache* which is helpful for performance but if it's not hot, it can lose performance if memory pressure is severe. For the balance, comprimise approach is shrinker.

Quoted 5 lines

We can see fragement space in zspage as wasted memory which is bad on the other hand we can see it as cache to store upcoming compressed page. So, too much freeing can hurt the performance so, let's obey VM's rule. If memory pressure goes severe, they want to reclaim more pages with proportional of memory pressure.

I agree that the unused memory has a dual nature here. we don't really do the "compact too much" thing, in a sense that we very rarely compact classes to be ZS_FULL. ~2-3% of the cases (if I recall the testing results correctly). but reducing the class ->lock pressure is a nice thing on its own. so yeah, let's do it!

btw, I've uploaded zram-fio test script to
https://github.com/sergey-senozhatsky/zram-perf-test

it's very minimalistic and half baked, but can be used to some degree. open to patches, improvements, etc.

	-ss

--
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>

author maintainer
#59 lore raw

Re: [PATCH v6 11/12] zsmalloc: page migration support

On Thu, May 26, 2016 at 09:59:26AM +0900, Sergey Senozhatsky wrote:
<snip>

Quoted 5 lines

btw, I've uploaded zram-fio test script to
https://github.com/sergey-senozhatsky/zram-perf-test

it's very minimalistic and half baked, but can be used to some degree. open to patches, improvements, etc.

Awesome!
Let's enhance it as zram benchmark tool.
Maybe I will help something. :)

Thanks.

--
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>

author maintainer
#61 lore raw

PATCH v6 12/12 zram: use __GFP_MOVABLE for memory allocation

In reply to Minchan Kim

Zsmalloc is ready for page migration so zram can use __GFP_MOVABLE from now on.

I did test to see how it helps to make higher order pages. Test scenario is as follows.

KVM guest, 1G memory, ext4 formated zram block device,

for i in seq 1 8;
do

        dd if=/dev/vda1 of=mnt/test$i.txt bs=128M count=1 &

done

wait pidof dd

for i in seq 1 2 8;
do
rm -rf mnt/test$i.txt
done
fstrim -v mnt

echo "init"
cat /proc/buddyinfo

echo "compaction"
echo 1 > /proc/sys/vm/compact_memory
cat /proc/buddyinfo

old:

init

Node 0, zone      DMA    208    120     51     41     11      0      0      0      0      0      0
Node 0, zone    DMA32  16380  13777   9184   3805    789     54      3      0      0      0      0
compaction
Node 0, zone      DMA    132     82     40     39     16      2      1      0      0      0      0
Node 0, zone    DMA32   5219   5526   4969   3455   1831    677    139     15      0      0      0

new:

init

Node 0, zone      DMA    379    115     97     19      2      0      0      0      0      0      0
Node 0, zone    DMA32  18891  16774  10862   3947    637     21      0      0      0      0      0
compaction  1
Node 0, zone      DMA    214     66     87     29     10      3      0      0      0      0      0
Node 0, zone    DMA32   1612   3139   3154   2469   1745    990    384     94      7      0      0

As you can see, compaction made so many high-order pages. Yay!

---
 drivers/block/zram/zram_drv.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/block/zram/zram_drv.c +4 -2
index 8fcad8b761f1..ccf1bddd09ca 100644
@@ -732,7 +732,8 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
 		handle = zs_malloc(meta->mem_pool, clen,
 				__GFP_KSWAPD_RECLAIM |
 				__GFP_NOWARN |
-				__GFP_HIGHMEM);
+				__GFP_HIGHMEM |
+				__GFP_MOVABLE);
 	if (!handle) {
 		zcomp_strm_release(zram->comp, zstrm);
 		zstrm = NULL;
@@ -740,7 +741,8 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
 		atomic64_inc(&zram->stats.writestall);
 
 		handle = zs_malloc(meta->mem_pool, clen,
-				GFP_NOIO | __GFP_HIGHMEM);
+				GFP_NOIO | __GFP_HIGHMEM |
+				__GFP_MOVABLE);
 		if (handle)
 			goto compress_again;
 
Signature · 7 lines
1.9.1

--
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>