Skip to contentSkip to conversation

mm, thp, tmpfs: support to add huge page into page cache for tmpfs

PATCH Superseded Ning Qu 2 participants 6 messages Oct 15, 2013 – Oct 16, 2013

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ning Qu

author
#0 lore raw

PATCH 2/12 mm, thp, tmpfs: support to add huge page into page cache for tmpfs

For replacing a page inside page cache, we assume the huge page has been splitted before getting here.

For adding a new page to page cache, huge page support has been added.

Also refactor the shm_add_to_page_cache function.

---
 mm/shmem.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 88 insertions(+), 9 deletions(-)
mm/shmem.c +88 -9
index a857ba8..447bd14 100644
@@ -277,27 +277,23 @@ static bool shmem_confirm_swap(struct address_space *mapping,
 }
 
 /*
- * Like add_to_page_cache_locked, but error if expected item has gone.
+ * Replace the swap entry with page cache entry
  */
-static int shmem_add_to_page_cache(struct page *page,
+static int shmem_replace_page_page_cache(struct page *page,
 				   struct address_space *mapping,
 				   pgoff_t index, gfp_t gfp, void *expected)
 {
 	int error;
 
-	VM_BUG_ON(!PageLocked(page));
-	VM_BUG_ON(!PageSwapBacked(page));
+	BUG_ON(PageTransHugeCache(page));
 
 	page_cache_get(page);
 	page->mapping = mapping;
 	page->index = index;
 
 	spin_lock_irq(&mapping->tree_lock);
-	if (!expected)
-		error = radix_tree_insert(&mapping->page_tree, index, page);
-	else
-		error = shmem_radix_tree_replace(mapping, index, expected,
-								 page);
+
+	error = shmem_radix_tree_replace(mapping, index, expected, page);
 	if (!error) {
 		mapping->nrpages++;
 		__inc_zone_page_state(page, NR_FILE_PAGES);
@@ -312,6 +308,87 @@ static int shmem_add_to_page_cache(struct page *page,
 }
 
 /*
+ * Insert new page into with page cache
+ */
+static int shmem_insert_page_page_cache(struct page *page,
+				   struct address_space *mapping,
+				   pgoff_t index, gfp_t gfp)
+{
+	int error;
+	int i, nr;
+
+	if (PageTransHugeCache(page))
+		BUILD_BUG_ON(HPAGE_CACHE_NR > RADIX_TREE_PRELOAD_NR);
+
+	nr = hpagecache_nr_pages(page);
+
+	error = radix_tree_maybe_preload_contig(nr, gfp & ~__GFP_HIGHMEM);
+	if (error)
+		return error;
+
+	spin_lock_irq(&mapping->tree_lock);
+	page_cache_get(page);
+	page->index = index;
+	page->mapping = mapping;
+	for (i = 0; i < nr; i++) {
+		error = radix_tree_insert(&mapping->page_tree,
+				index + i, page);
+		/*
+		 * In the midle of THP we can collide with small page which was
+		 * established before THP page cache is enabled or by other VMA
+		 * with bad alignement (most likely MAP_FIXED).
+		 */
+		if (error) {
+			i--; /* failed to insert anything at offset + i */
+			goto err;
+		}
+	}
+	radix_tree_preload_end();
+	mapping->nrpages += nr;
+	__mod_zone_page_state(page_zone(page), NR_FILE_PAGES, nr);
+	__mod_zone_page_state(page_zone(page), NR_SHMEM, nr);
+	if (PageTransHugeCache(page))
+		__inc_zone_page_state(page, NR_FILE_TRANSPARENT_HUGEPAGES);
+	spin_unlock_irq(&mapping->tree_lock);
+	return 0;
+err:
+	radix_tree_preload_end();
+	if (i != 0)
+		error = -ENOSPC; /* no space for a huge page */
+
+	/* Leave page->index set: truncation relies upon it */
+	page->mapping = NULL;
+	for (; i >= 0; i--)
+		radix_tree_delete(&mapping->page_tree, index + i);
+
+	spin_unlock_irq(&mapping->tree_lock);
+	page_cache_release(page);
+	return error;
+}
+
+/*
+ * Like add_to_page_cache_locked, but error if expected item has gone.
+ */
+static int shmem_add_to_page_cache(struct page *page,
+				   struct address_space *mapping,
+				   pgoff_t index, gfp_t gfp, void *expected)
+{
+	int error;
+
+	VM_BUG_ON(!PageLocked(page));
+	VM_BUG_ON(!PageSwapBacked(page));
+
+	if (expected) {
+		BUG_ON(PageTransHugeCache(page));
+		error = shmem_replace_page_page_cache(page, mapping, index, gfp,
+							expected);
+	} else
+		error = shmem_insert_page_page_cache(page, mapping, index, gfp);
+
+	return error;
+}
+
+/*
  * Like delete_from_page_cache, but substitutes swap for page.
  */
 static void shmem_delete_from_page_cache(struct page *page, void *radswap)
@@ -319,6 +396,8 @@ static void shmem_delete_from_page_cache(struct page *page, void *radswap)
 	struct address_space *mapping = page->mapping;
 	int error;
 
+	BUG_ON(PageTransHugeCache(page));
+
 	spin_lock_irq(&mapping->tree_lock);
 	error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
 	page->mapping = NULL;
Signature · 8 lines
1.8.4


--
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>
#1 lore raw
Quoted 50 lines · Ning Qu

For replacing a page inside page cache, we assume the huge page has been splitted before getting here.

For adding a new page to page cache, huge page support has been added.

Also refactor the shm_add_to_page_cache function.

---
 mm/shmem.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 88 insertions(+), 9 deletions(-)
mm/shmem.c +5 -9
index a857ba8..447bd14 100644
@@ -277,27 +277,23 @@ static bool shmem_confirm_swap(struct address_space *mapping,
 }
 
 /*
- * Like add_to_page_cache_locked, but error if expected item has gone.
+ * Replace the swap entry with page cache entry
  */
-static int shmem_add_to_page_cache(struct page *page,
+static int shmem_replace_page_page_cache(struct page *page,
 				   struct address_space *mapping,
 				   pgoff_t index, gfp_t gfp, void *expected)
 {
 	int error;
 
-	VM_BUG_ON(!PageLocked(page));
-	VM_BUG_ON(!PageSwapBacked(page));
+	BUG_ON(PageTransHugeCache(page));
 
 	page_cache_get(page);
 	page->mapping = mapping;
 	page->index = index;
 
 	spin_lock_irq(&mapping->tree_lock);
-	if (!expected)
-		error = radix_tree_insert(&mapping->page_tree, index, page);
-	else
-		error = shmem_radix_tree_replace(mapping, index, expected,
-								 page);
+
+	error = shmem_radix_tree_replace(mapping, index, expected, page);
 	if (!error) {
 		mapping->nrpages++;
 		__inc_zone_page_state(page, NR_FILE_PAGES);

mm/shmem.c static int shmem_add_to_page_cache(struct page *page, · line 316

Quoted 9 lines
mm/shmem.c +6 -0
@@ -312,6 +308,87 @@ static int shmem_add_to_page_cache(struct page *page,
 }
 
 /*
+ * Insert new page into with page cache
+ */
+static int shmem_insert_page_page_cache(struct page *page,
+				   struct address_space *mapping,
+				   pgoff_t index, gfp_t gfp)
+{

You copy-paste most of add_to_page_cache_locked() code here. Is there a way to share the code? Move common part into __add_to_page_cache_locked() or something.

Signature · 7 lines
 Kirill A. Shutemov

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

Yes, I can try. The code is pretty much similar with some minor difference.

One thing I can do is to move the spin lock part (together with the corresponding err handling into a common function.

The only problem I can see right now is we need the following additional line for shm:

__mod_zone_page_state(page_zone(page), NR_SHMEM, nr);

Which means we need to tell if it's coming from shm or not, is that OK to add additional parameter just for that? Or is there any other better way we can infer that information? Thanks!
Best wishes,

Signature · 4 lines
Ning Qu (曲宁) | Software Engineer | [email protected] | +1-408-418-6066


On Tue, Oct 15, 2013 at 3:02 AM, Kirill A. Shutemov
Quoted 65 lines · <[email protected]> wrote:
Quoted 50 lines · Ning Qu

For replacing a page inside page cache, we assume the huge page has been splitted before getting here.

For adding a new page to page cache, huge page support has been added.

Also refactor the shm_add_to_page_cache function.

---
 mm/shmem.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 88 insertions(+), 9 deletions(-)
mm/shmem.c +5 -9
index a857ba8..447bd14 100644
@@ -277,27 +277,23 @@ static bool shmem_confirm_swap(struct address_space *mapping,
 }
 
 /*
- * Like add_to_page_cache_locked, but error if expected item has gone.
+ * Replace the swap entry with page cache entry
  */
-static int shmem_add_to_page_cache(struct page *page,
+static int shmem_replace_page_page_cache(struct page *page,
                                 struct address_space *mapping,
                                 pgoff_t index, gfp_t gfp, void *expected)
 {
      int error;
 
-     VM_BUG_ON(!PageLocked(page));
-     VM_BUG_ON(!PageSwapBacked(page));
+     BUG_ON(PageTransHugeCache(page));
 
      page_cache_get(page);
      page->mapping = mapping;
      page->index = index;
 
      spin_lock_irq(&mapping->tree_lock);
-     if (!expected)
-             error = radix_tree_insert(&mapping->page_tree, index, page);
-     else
-             error = shmem_radix_tree_replace(mapping, index, expected,
-                                                              page);
+
+     error = shmem_radix_tree_replace(mapping, index, expected, page);
      if (!error) {
              mapping->nrpages++;
              __inc_zone_page_state(page, NR_FILE_PAGES);

mm/shmem.c static int shmem_add_to_page_cache(struct page *page, · line 316

Quoted 9 lines
mm/shmem.c +6 -0
@@ -312,6 +308,87 @@ static int shmem_add_to_page_cache(struct page *page,
 }
 
 /*
+ * Insert new page into with page cache
+ */
+static int shmem_insert_page_page_cache(struct page *page,
+                                struct address_space *mapping,
+                                pgoff_t index, gfp_t gfp)
+{

You copy-paste most of add_to_page_cache_locked() code here. Is there a way to share the code? Move common part into __add_to_page_cache_locked() or something.

Quoted 2 lines

--
Kirill A. Shutemov

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

#3 lore raw
Quoted 5 lines · Ning Qu

Yes, I can try. The code is pretty much similar with some minor difference.

One thing I can do is to move the spin lock part (together with the corresponding err handling into a common function.

Quoted 8 lines

The only problem I can see right now is we need the following additional line for shm:

__mod_zone_page_state(page_zone(page), NR_SHMEM, nr);

Which means we need to tell if it's coming from shm or not, is that OK to add additional parameter just for that? Or is there any other better way we can infer that information? Thanks!

I think you can account NR_SHMEM after common code succeed, don't you?

Signature · 7 lines
 Kirill A. Shutemov

--
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
#4 lore raw

Yes, I guess I can if I just put whatever inside the spin lock into a common function. Thanks!
Best wishes,

Signature · 4 lines
Ning Qu (曲宁) | Software Engineer | [email protected] | +1-408-418-6066


On Wed, Oct 16, 2013 at 5:26 AM, Kirill A. Shutemov
Quoted 19 lines · <[email protected]> wrote:
Quoted 5 lines · Ning Qu

Yes, I can try. The code is pretty much similar with some minor difference.

One thing I can do is to move the spin lock part (together with the corresponding err handling into a common function.

Quoted 8 lines

The only problem I can see right now is we need the following additional line for shm:

__mod_zone_page_state(page_zone(page), NR_SHMEM, nr);

Which means we need to tell if it's coming from shm or not, is that OK to add additional parameter just for that? Or is there any other better way we can infer that information? Thanks!

I think you can account NR_SHMEM after common code succeed, don't you?

--
Kirill A. Shutemov

author
#5 lore raw

Consider this fixed. I have extracted the common function and the new shmem_insert_page_page_cache function looks like this:

       spin_lock_irq(&mapping->tree_lock);
       error = __add_to_page_cache_locked(page, mapping, index);

       if (!error)
            __mod_zone_page_state(page_zone(page), NR_SHMEM, nr);

        radix_tree_preload_end();
        spin_unlock_irq(&mapping->tree_lock);

        if (error)
                page_cache_release(page);

        return error;

Best wishes,

Signature · 1 line
Ning Qu (曲宁) | Software Engineer | [email protected] | +1-408-418-6066
Quoted 7 lines · Ning Qu

Yes, I guess I can if I just put whatever inside the spin lock into a common function. Thanks!
Best wishes,
--
Ning Qu (曲宁) | Software Engineer | [email protected] | +1-408-418-6066

Quoted 21 lines
Quoted 19 lines · Kirill A. Shutemov
Quoted 5 lines · Ning Qu

Yes, I can try. The code is pretty much similar with some minor difference.

One thing I can do is to move the spin lock part (together with the corresponding err handling into a common function.

Quoted 8 lines

The only problem I can see right now is we need the following additional line for shm:

__mod_zone_page_state(page_zone(page), NR_SHMEM, nr);

Which means we need to tell if it's coming from shm or not, is that OK to add additional parameter just for that? Or is there any other better way we can infer that information? Thanks!

I think you can account NR_SHMEM after common code succeed, don't you?

--
Kirill A. Shutemov

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html