Skip to contentSkip to conversation

add generic callbacks into compaction

PATCH Hillf Danton 2 participants 2 messages Apr 1, 2015 1 message not in the archive Memory management

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 9 lines

I sent a patch about page allocation for less fragmentation.
http://permalink.gmane.org/gmane.linux.kernel.mm/130599

It proposes a page allocator allocates pages in the same pageblock
for the drivers to move their unmovable pages. Some drivers which comsumes many pages and increases system fragmentation use the allocator to move their pages to decrease fragmentation.

Quoted 8 lines

I think I can try another approach.
There is a compaction code for balloon pages.
But the compaction code cannot migrate pages of other drivers. If there is a generic migration framework applicable to any drivers, drivers can register their migration functions.
And the compaction can migrate movable pages and also driver's pages.

I'm not familiar with virtualization so I couldn't test this patch yet.

A RFC, no?

Quoted 33 lines

But if mm developers agree with this approach, I will complete this patch.

I would do appreciate any feedback.

---
 drivers/virtio/virtio_balloon.c    |    2 ++
 include/linux/balloon_compaction.h |   23 +++++++++---
 include/linux/fs.h                 |    3 ++
 include/linux/pagemap.h            |   26 ++++++++++++++
 mm/balloon_compaction.c            |   68 ++++++++++++++++++++++++++++++++++--
 mm/compaction.c                    |    7 ++--
 mm/migrate.c                       |   24 ++++++-------
 7 files changed, 129 insertions(+), 24 deletions(-)
drivers/virtio/virtio_balloon.c +2 -0
index 0413157..cd9b8e4 100644
@@ -486,6 +486,8 @@ static int virtballoon_probe(struct virtio_device *vdev)
 
 	balloon_devinfo_init(&vb->vb_dev_info);
 #ifdef CONFIG_BALLOON_COMPACTION
+	vb->vb_dev_info.mapping = balloon_mapping_alloc(&vb->vb_dev_info,
+							&balloon_aops);
 	vb->vb_dev_info.migratepage = virtballoon_migratepage;
 #endif
 
include/linux/balloon_compaction.h +0 -0
index 9b0a15d..0af32b3 100644

include/linux/balloon_compaction.h struct balloon_dev_info { · line 65

Quoted 4 lines
include/linux/balloon_compaction.h +1 -0
@@ -62,6 +62,7 @@ struct balloon_dev_info {
 	struct list_head pages;		/* Pages enqueued & handled to Host */
 	int (*migratepage)(struct balloon_dev_info *, struct page *newpage,
 			struct page *page, enum migrate_mode mode);
+	struct address_space *mapping;

Better if a comment line is added.

Quoted 87 lines
include/linux/balloon_compaction.h +18 -4
 };
 
 extern struct page *balloon_page_enqueue(struct balloon_dev_info *b_dev_info);
@@ -76,10 +77,22 @@ static inline void balloon_devinfo_init(struct balloon_dev_info *balloon)
 }
 
 #ifdef CONFIG_BALLOON_COMPACTION
-extern bool balloon_page_isolate(struct page *page);
-extern void balloon_page_putback(struct page *page);
-extern int balloon_page_migrate(struct page *newpage,
-				struct page *page, enum migrate_mode mode);
+extern const struct address_space_operations balloon_aops;
+extern int balloon_page_isolate(struct page *page);
+extern int balloon_page_putback(struct page *page);
+extern int balloon_page_migrate(struct address_space *mapping,
+				struct page *newpage,
+				struct page *page,
+				enum migrate_mode mode);
+
+extern struct address_space
+*balloon_mapping_alloc(struct balloon_dev_info *b_dev_info,
+		       const struct address_space_operations *a_ops);
+
+static inline void balloon_mapping_free(struct address_space *balloon_mapping)
+{
+	kfree(balloon_mapping);
+}
 
 /*
  * __is_movable_balloon_page - helper to perform @page PageBalloon tests
@@ -123,6 +136,7 @@ static inline bool isolated_balloon_page(struct page *page)
 static inline void balloon_page_insert(struct balloon_dev_info *balloon,
 				       struct page *page)
 {
+	page->mapping = balloon->mapping;
 	__SetPageBalloon(page);
 	SetPagePrivate(page);
 	set_page_private(page, (unsigned long)balloon);
@@ -139,6 +153,7 @@ static inline void balloon_page_insert(struct balloon_dev_info *balloon,
  */
 static inline void balloon_page_delete(struct page *page)
 {
+	page->mapping = NULL;
 	__ClearPageBalloon(page);
 	set_page_private(page, 0);
 	if (PagePrivate(page)) {
include/linux/fs.h +3 -0
index b4d71b5..de463b9 100644
@@ -368,6 +368,9 @@ struct address_space_operations {
 	 */
 	int (*migratepage) (struct address_space *,
 			struct page *, struct page *, enum migrate_mode);
+	int (*isolatepage)(struct page *);
+	int (*putbackpage)(struct page *);
+
 	int (*launder_page) (struct page *);
 	int (*is_partially_uptodate) (struct page *, unsigned long,
 					unsigned long);
include/linux/pagemap.h +12 -0
index 4b3736f..715b5b2 100644
@@ -25,6 +25,7 @@ enum mapping_flags {
 	AS_MM_ALL_LOCKS	= __GFP_BITS_SHIFT + 2,	/* under mm_take_all_locks() */
 	AS_UNEVICTABLE	= __GFP_BITS_SHIFT + 3,	/* e.g., ramdisk, SHM_LOCK */
 	AS_EXITING	= __GFP_BITS_SHIFT + 4, /* final truncate in progress */
+	AS_MIGRATABLE   = __GFP_BITS_SHIFT + 5,
 };
 
 static inline void mapping_set_error(struct address_space *mapping, int error)
@@ -54,6 +55,31 @@ static inline int mapping_unevictable(struct address_space *mapping)
 	return !!mapping;
 }
 
+static inline void mapping_set_migratable(struct address_space *mapping)
+{
+	set_bit(AS_MIGRATABLE, &mapping->flags);
+}
+
+static inline void mapping_clear_migratable(struct address_space *mapping)
+{
+	clear_bit(AS_MIGRATABLE, &mapping->flags);
+}
+
+static inline int __mapping_ops(struct address_space *mapping)

include/linux/pagemap.h static inline int mapping_unevictable(struct address_space *mapping) · line 78

Quoted 10 lines
include/linux/pagemap.h +10 -0
+{
+	return mapping->a_ops &&
+		mapping->a_ops->migratepage &&
+		mapping->a_ops->isolatepage &&
+		mapping->a_ops->putbackpage;
+}
+
+static inline int mapping_migratable(struct address_space *mapping)
+{
+	if (mapping && __mapping_ops(mapping))

s/__mapping_ops/__mapping_migratable/ given naming hurts brain?

Quoted 38 lines
include/linux/pagemap.h +4 -0
+		return test_bit(AS_MIGRATABLE, &mapping->flags);
+	return !!mapping;
+}
+
 static inline void mapping_set_exiting(struct address_space *mapping)
 {
 	set_bit(AS_EXITING, &mapping->flags);
mm/balloon_compaction.c +8 -2
index fcad832..2e9d635 100644
@@ -131,8 +131,11 @@ static inline void __putback_balloon_page(struct page *page)
 }
 
 /* __isolate_lru_page() counterpart for a ballooned page */
-bool balloon_page_isolate(struct page *page)
+int balloon_page_isolate(struct page *page)
 {
+	if (!balloon_page_movable(page))
+		return false;
+
 	/*
 	 * Avoid burning cycles with pages that are yet under __free_pages(),
 	 * or just got freed under us.
@@ -173,8 +176,11 @@ bool balloon_page_isolate(struct page *page)
 }
 
 /* putback_lru_page() counterpart for a ballooned page */
-void balloon_page_putback(struct page *page)
+int balloon_page_putback(struct page *page)
 {
+	if (!isolated_balloon_page(page))
+		return 0;
+
 	/*
 	 * 'lock_page()' stabilizes the page and prevents races against
 	 * concurrent isolation threads attempting to re-isolate it.

mm/balloon_compaction.c void balloon_page_putback(struct page *page) · line 204

Quoted 10 lines
mm/balloon_compaction.c +3 -1
@@ -190,15 +196,20 @@ void balloon_page_putback(struct page *page)
 		dump_page(page, "not movable balloon page");
 	}
 	unlock_page(page);
+	return 0;
 }
 
 /* move_to_new_page() counterpart for a ballooned page */
-int balloon_page_migrate(struct page *newpage,
+int balloon_page_migrate(struct address_space *mapping,
+			 struct page *newpage,

Looks the newly added mapping is not necessary, yes?

Quoted 23 lines
mm/balloon_compaction.c +11 -0
 			 struct page *page, enum migrate_mode mode)
 {
 	struct balloon_dev_info *balloon = balloon_page_device(page);
 	int rc = -EAGAIN;
 
+	if (!isolated_balloon_page(page))
+		return 0;
+
 	/*
 	 * Block others from accessing the 'newpage' when we get around to
 	 * establishing additional references. We should be the only one
@@ -218,4 +229,55 @@ int balloon_page_migrate(struct page *newpage,
 	unlock_page(newpage);
 	return rc;
 }
+
+/* define the balloon_mapping->a_ops callback to allow balloon page migration */
+const struct address_space_operations balloon_aops = {
+	.migratepage = balloon_page_migrate,
+	.isolatepage = balloon_page_isolate,
+	.putbackpage = balloon_page_putback,
+};
+EXPORT_SYMBOL_GPL(balloon_aops);
#2 lore raw

2015-04-01 i??i?? 12:46i?? Hillf Danton i?'(e??) i?' e,?:

Quoted 16 lines
Quoted 16 lines

I sent a patch about page allocation for less fragmentation.
http://permalink.gmane.org/gmane.linux.kernel.mm/130599

It proposes a page allocator allocates pages in the same pageblock
for the drivers to move their unmovable pages. Some drivers which comsumes many pages and increases system fragmentation use the allocator to move their pages to decrease fragmentation.

I think I can try another approach.
There is a compaction code for balloon pages.
But the compaction code cannot migrate pages of other drivers. If there is a generic migration framework applicable to any drivers, drivers can register their migration functions.
And the compaction can migrate movable pages and also driver's pages.

Quoted 3 lines
Quoted 1 line

I'm not familiar with virtualization so I couldn't test this patch yet.

A RFC, no?

YES, this is a RFC. It's my fault. I'm sorry.

Quoted 28 lines
Quoted 28 lines

But if mm developers agree with this approach, I will complete this patch.

I would do appreciate any feedback.

---
  drivers/virtio/virtio_balloon.c    |    2 ++
  include/linux/balloon_compaction.h |   23 +++++++++---
  include/linux/fs.h                 |    3 ++
  include/linux/pagemap.h            |   26 ++++++++++++++
  mm/balloon_compaction.c            |   68 ++++++++++++++++++++++++++++++++++--
  mm/compaction.c                    |    7 ++--
  mm/migrate.c                       |   24 ++++++-------
  7 files changed, 129 insertions(+), 24 deletions(-)
drivers/virtio/virtio_balloon.c +2 -0
index 0413157..cd9b8e4 100644
@@ -486,6 +486,8 @@ static int virtballoon_probe(struct virtio_device *vdev)
 
  	balloon_devinfo_init(&vb->vb_dev_info);
  #ifdef CONFIG_BALLOON_COMPACTION
+	vb->vb_dev_info.mapping = balloon_mapping_alloc(&vb->vb_dev_info,
+							&balloon_aops);
  	vb->vb_dev_info.migratepage = virtballoon_migratepage;
  #endif
 
Quoted 11 lines

include/linux/balloon_compaction.h struct balloon_dev_info { · line 65

Quoted 9 lines
include/linux/balloon_compaction.h +1 -0
index 9b0a15d..0af32b3 100644
@@ -62,6 +62,7 @@ struct balloon_dev_info {
  	struct list_head pages;		/* Pages enqueued & handled to Host */
  	int (*migratepage)(struct balloon_dev_info *, struct page *newpage,
  			struct page *page, enum migrate_mode mode);
+	struct address_space *mapping;

Better if a comment line is added.

I got it.

Quoted 91 lines
Quoted 91 lines
include/linux/balloon_compaction.h +18 -4
  };
 
  extern struct page *balloon_page_enqueue(struct balloon_dev_info *b_dev_info);
@@ -76,10 +77,22 @@ static inline void balloon_devinfo_init(struct balloon_dev_info *balloon)
  }
 
  #ifdef CONFIG_BALLOON_COMPACTION
-extern bool balloon_page_isolate(struct page *page);
-extern void balloon_page_putback(struct page *page);
-extern int balloon_page_migrate(struct page *newpage,
-				struct page *page, enum migrate_mode mode);
+extern const struct address_space_operations balloon_aops;
+extern int balloon_page_isolate(struct page *page);
+extern int balloon_page_putback(struct page *page);
+extern int balloon_page_migrate(struct address_space *mapping,
+				struct page *newpage,
+				struct page *page,
+				enum migrate_mode mode);
+
+extern struct address_space
+*balloon_mapping_alloc(struct balloon_dev_info *b_dev_info,
+		       const struct address_space_operations *a_ops);
+
+static inline void balloon_mapping_free(struct address_space *balloon_mapping)
+{
+	kfree(balloon_mapping);
+}
 
  /*
   * __is_movable_balloon_page - helper to perform @page PageBalloon tests
@@ -123,6 +136,7 @@ static inline bool isolated_balloon_page(struct page *page)
  static inline void balloon_page_insert(struct balloon_dev_info *balloon,
  				       struct page *page)
  {
+	page->mapping = balloon->mapping;
  	__SetPageBalloon(page);
  	SetPagePrivate(page);
  	set_page_private(page, (unsigned long)balloon);
@@ -139,6 +153,7 @@ static inline void balloon_page_insert(struct balloon_dev_info *balloon,
   */
  static inline void balloon_page_delete(struct page *page)
  {
+	page->mapping = NULL;
  	__ClearPageBalloon(page);
  	set_page_private(page, 0);
  	if (PagePrivate(page)) {
include/linux/fs.h +3 -0
index b4d71b5..de463b9 100644
@@ -368,6 +368,9 @@ struct address_space_operations {
  	 */
  	int (*migratepage) (struct address_space *,
  			struct page *, struct page *, enum migrate_mode);
+	int (*isolatepage)(struct page *);
+	int (*putbackpage)(struct page *);
+
  	int (*launder_page) (struct page *);
  	int (*is_partially_uptodate) (struct page *, unsigned long,
  					unsigned long);
include/linux/pagemap.h +16 -0
index 4b3736f..715b5b2 100644
@@ -25,6 +25,7 @@ enum mapping_flags {
  	AS_MM_ALL_LOCKS	= __GFP_BITS_SHIFT + 2,	/* under mm_take_all_locks() */
  	AS_UNEVICTABLE	= __GFP_BITS_SHIFT + 3,	/* e.g., ramdisk, SHM_LOCK */
  	AS_EXITING	= __GFP_BITS_SHIFT + 4, /* final truncate in progress */
+	AS_MIGRATABLE   = __GFP_BITS_SHIFT + 5,
  };
 
  static inline void mapping_set_error(struct address_space *mapping, int error)
@@ -54,6 +55,31 @@ static inline int mapping_unevictable(struct address_space *mapping)
  	return !!mapping;
  }
 
+static inline void mapping_set_migratable(struct address_space *mapping)
+{
+	set_bit(AS_MIGRATABLE, &mapping->flags);
+}
+
+static inline void mapping_clear_migratable(struct address_space *mapping)
+{
+	clear_bit(AS_MIGRATABLE, &mapping->flags);
+}
+
+static inline int __mapping_ops(struct address_space *mapping)
+{
+	return mapping->a_ops &&
+		mapping->a_ops->migratepage &&
+		mapping->a_ops->isolatepage &&
Quoted 8 lines

include/linux/pagemap.h static inline int mapping_unevictable(struct address_space *mapping) · line 78

Quoted 6 lines
include/linux/pagemap.h +6 -0
+		mapping->a_ops->putbackpage;
+}
+
+static inline int mapping_migratable(struct address_space *mapping)
+{
+	if (mapping && __mapping_ops(mapping))

s/__mapping_ops/__mapping_migratable/ given naming hurts brain?

Good. I'll change it. Thank you.

Quoted 45 lines
Quoted 45 lines
include/linux/pagemap.h +4 -0
+		return test_bit(AS_MIGRATABLE, &mapping->flags);
+	return !!mapping;
+}
+
  static inline void mapping_set_exiting(struct address_space *mapping)
  {
  	set_bit(AS_EXITING, &mapping->flags);
mm/balloon_compaction.c +9 -2
index fcad832..2e9d635 100644
@@ -131,8 +131,11 @@ static inline void __putback_balloon_page(struct page *page)
  }
 
  /* __isolate_lru_page() counterpart for a ballooned page */
-bool balloon_page_isolate(struct page *page)
+int balloon_page_isolate(struct page *page)
  {
+	if (!balloon_page_movable(page))
+		return false;
+
  	/*
  	 * Avoid burning cycles with pages that are yet under __free_pages(),
  	 * or just got freed under us.
@@ -173,8 +176,11 @@ bool balloon_page_isolate(struct page *page)
  }
 
  /* putback_lru_page() counterpart for a ballooned page */
-void balloon_page_putback(struct page *page)
+int balloon_page_putback(struct page *page)
  {
+	if (!isolated_balloon_page(page))
+		return 0;
+
  	/*
  	 * 'lock_page()' stabilizes the page and prevents races against
  	 * concurrent isolation threads attempting to re-isolate it.
@@ -190,15 +196,20 @@ void balloon_page_putback(struct page *page)
  		dump_page(page, "not movable balloon page");
  	}
  	unlock_page(page);
+	return 0;
  }
 
Quoted 6 lines

mm/balloon_compaction.c void balloon_page_putback(struct page *page) · line 204

Quoted 4 lines
mm/balloon_compaction.c +2 -1
  /* move_to_new_page() counterpart for a ballooned page */
-int balloon_page_migrate(struct page *newpage,
+int balloon_page_migrate(struct address_space *mapping,
+			 struct page *newpage,

Looks the newly added mapping is not necessary, yes?

Please refer to struct address_space_operations.
int (*migratepage) (struct address_space *,

		struct page *, struct page *, enum migrate_mode);
Quoted 15 lines
Quoted 15 lines
mm/balloon_compaction.c +3 -0
  			 struct page *page, enum migrate_mode mode)
  {
  	struct balloon_dev_info *balloon = balloon_page_device(page);
  	int rc = -EAGAIN;
 
+	if (!isolated_balloon_page(page))
+		return 0;
+
  	/*
  	 * Block others from accessing the 'newpage' when we get around to
  	 * establishing additional references. We should be the only one
@@ -218,4 +229,55 @@ int balloon_page_migrate(struct page *newpage,
  	unlock_page(newpage);
  	return rc;
  }
Quoted 10 lines

mm/balloon_compaction.c int balloon_page_migrate(struct page *newpage, · line 239

Quoted 8 lines
mm/balloon_compaction.c +8 -0
+
+/* define the balloon_mapping->a_ops callback to allow balloon page migration */
+const struct address_space_operations balloon_aops = {
+	.migratepage = balloon_page_migrate,
+	.isolatepage = balloon_page_isolate,
+	.putbackpage = balloon_page_putback,
+};
+EXPORT_SYMBOL_GPL(balloon_aops);

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