Skip to contentSkip to conversation

f2fs: compress: reduce one page array alloc and free when write compressed page

RFC PATCH In review Fengnan Chang 2 participants 5 messages Jul 22, 2021 – Jul 23, 2021 F2FS file system

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 f2fs: compress: reduce one page array alloc and free when write compressed page

Don't alloc new page array to replace old, just use old page array, try to reduce one page array alloc and free when write compress page.

---
 fs/f2fs/compress.c | 14 ++------------
 fs/f2fs/f2fs.h     |  1 +
 2 files changed, 3 insertions(+), 12 deletions(-)
fs/f2fs/compress.c +2 -12
index 455561826c7d..43daafe382e7 100644
@@ -618,7 +618,6 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
 	const struct f2fs_compress_ops *cops =
 				f2fs_cops[fi->i_compress_algorithm];
 	unsigned int max_len, new_nr_cpages;
-	struct page **new_cpages;
 	u32 chksum = 0;
 	int i, ret;
 
@@ -633,6 +632,7 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
 
 	max_len = COMPRESS_HEADER_SIZE + cc->clen;
 	cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
+	cc->raw_nr_cpages = cc->nr_cpages;
 
 	cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
 	if (!cc->cpages) {
@@ -683,13 +683,6 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
 
 	new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
 
-	/* Now we're going to cut unnecessary tail pages */
-	new_cpages = page_array_alloc(cc->inode, new_nr_cpages);
-	if (!new_cpages) {
-		ret = -ENOMEM;
-		goto out_vunmap_cbuf;
-	}
-
 	/* zero out any unused part of the last page */
 	memset(&cc->cbuf->cdata[cc->clen], 0,
 			(new_nr_cpages * PAGE_SIZE) -
@@ -700,7 +693,6 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
 
 	for (i = 0; i < cc->nr_cpages; i++) {
 		if (i < new_nr_cpages) {
-			new_cpages[i] = cc->cpages[i];
 			continue;
 		}
 		f2fs_compress_free_page(cc->cpages[i]);
@@ -710,8 +702,6 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
 	if (cops->destroy_compress_ctx)
 		cops->destroy_compress_ctx(cc);
 
-	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
-	cc->cpages = new_cpages;
 	cc->nr_cpages = new_nr_cpages;
 
 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
@@ -1330,7 +1320,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
 	spin_unlock(&fi->i_size_lock);
 
 	f2fs_put_rpages(cc);
-	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
+	page_array_free(cc->inode, cc->cpages, cc->raw_nr_cpages);
 	cc->cpages = NULL;
 	f2fs_destroy_compress_ctx(cc, false);
 	return 0;
fs/f2fs/f2fs.h +1 -0
index 867f2c5d9559..8b1f84d88a65 100644
@@ -1454,6 +1454,7 @@ struct compress_ctx {
 	unsigned int nr_rpages;		/* total page number in rpages */
 	struct page **cpages;		/* pages store compressed data in cluster */
 	unsigned int nr_cpages;		/* total page number in cpages */
+	unsigned int raw_nr_cpages;	/* max total page number in cpages */
 	void *rbuf;			/* virtual mapped address on rpages */
 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
 	size_t rlen;			/* valid data length in rbuf */
Signature · 8 lines
2.29.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
maintainer
#1 lore raw
Quoted 2 lines · Fengnan Chang

Don't alloc new page array to replace old, just use old page array, try to reduce one page array alloc and free when write compress page.

Nope, see whole story in below link:

https://lore.kernel.org/patchwork/patch/1305096/

Thanks,

Quoted 73 lines
---
  fs/f2fs/compress.c | 14 ++------------
  fs/f2fs/f2fs.h     |  1 +
  2 files changed, 3 insertions(+), 12 deletions(-)
fs/f2fs/compress.c +2 -12
index 455561826c7d..43daafe382e7 100644
@@ -618,7 +618,6 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
  	const struct f2fs_compress_ops *cops =
  				f2fs_cops[fi->i_compress_algorithm];
  	unsigned int max_len, new_nr_cpages;
-	struct page **new_cpages;
  	u32 chksum = 0;
  	int i, ret;
 
@@ -633,6 +632,7 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
 
  	max_len = COMPRESS_HEADER_SIZE + cc->clen;
  	cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
+	cc->raw_nr_cpages = cc->nr_cpages;
 
  	cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
  	if (!cc->cpages) {
@@ -683,13 +683,6 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
 
  	new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
 
-	/* Now we're going to cut unnecessary tail pages */
-	new_cpages = page_array_alloc(cc->inode, new_nr_cpages);
-	if (!new_cpages) {
-		ret = -ENOMEM;
-		goto out_vunmap_cbuf;
-	}
-
  	/* zero out any unused part of the last page */
  	memset(&cc->cbuf->cdata[cc->clen], 0,
  			(new_nr_cpages * PAGE_SIZE) -
@@ -700,7 +693,6 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
 
  	for (i = 0; i < cc->nr_cpages; i++) {
  		if (i < new_nr_cpages) {
-			new_cpages[i] = cc->cpages[i];
  			continue;
  		}
  		f2fs_compress_free_page(cc->cpages[i]);
@@ -710,8 +702,6 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
  	if (cops->destroy_compress_ctx)
  		cops->destroy_compress_ctx(cc);
 
-	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
-	cc->cpages = new_cpages;
  	cc->nr_cpages = new_nr_cpages;
 
  	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
@@ -1330,7 +1320,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
  	spin_unlock(&fi->i_size_lock);
 
  	f2fs_put_rpages(cc);
-	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
+	page_array_free(cc->inode, cc->cpages, cc->raw_nr_cpages);
  	cc->cpages = NULL;
  	f2fs_destroy_compress_ctx(cc, false);
  	return 0;
fs/f2fs/f2fs.h +0 -0
index 867f2c5d9559..8b1f84d88a65 100644

fs/f2fs/f2fs.h struct compress_ctx { · line 1460

Quoted 7 lines
fs/f2fs/f2fs.h +1 -0
@@ -1454,6 +1454,7 @@ struct compress_ctx {
  	unsigned int nr_rpages;		/* total page number in rpages */
  	struct page **cpages;		/* pages store compressed data in cluster */
  	unsigned int nr_cpages;		/* total page number in cpages */
+	unsigned int raw_nr_cpages;	/* max total page number in cpages */
  	void *rbuf;			/* virtual mapped address on rpages */
  	struct compress_data *cbuf;	/* virtual mapped address on cpages */
  	size_t rlen;			/* valid data length in rbuf */

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#2 lore raw

Sorry, I didn't get your point, in my opinion, new_nr_cpages should always little than nr_cpages, is this right? So we can just use cpages, don't need to alloc new one.

Thanks.

Quoted 79 lines · Chao Yu
Quoted 2 lines · Fengnan Chang

Don't alloc new page array to replace old, just use old page array, try to reduce one page array alloc and free when write compress page.

Nope, see whole story in below link:

https://lore.kernel.org/patchwork/patch/1305096/

Thanks,

Quoted 69 lines

---
  fs/f2fs/compress.c | 14 ++------------
  fs/f2fs/f2fs.h     |  1 +

  2 files changed, 3 insertions(+), 12 deletions(-)
fs/f2fs/compress.c +0 -0
index 455561826c7d..43daafe382e7 100644
@@ -618,7 +618,6 @@ static int f2fs_compress_pages(struct compress_ctx 
 *cc)

      const struct f2fs_compress_ops *cops = f2fs_cops[fi->i_compress_algorithm]; unsigned int max_len, new_nr_cpages;
-    struct page **new_cpages;
      u32 chksum = 0;
      int i, ret;

fs/f2fs/compress.c +1 -7
@@ -633,6 +632,7 @@ static int f2fs_compress_pages(struct compress_ctx 
 *cc)
       max_len = COMPRESS_HEADER_SIZE + cc->clen;
       cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
+    cc->raw_nr_cpages = cc->nr_cpages;
       cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
       if (!cc->cpages) {
@@ -683,13 +683,6 @@ static int f2fs_compress_pages(struct 
 compress_ctx *cc)
       new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
-    /* Now we're going to cut unnecessary tail pages */
-    new_cpages = page_array_alloc(cc->inode, new_nr_cpages);
-    if (!new_cpages) {
-        ret = -ENOMEM;
-        goto out_vunmap_cbuf;
-    }
-
       /* zero out any unused part of the last page */
       memset(&cc->cbuf->cdata[cc->clen], 0,

              (new_nr_cpages * PAGE_SIZE) -

fs/f2fs/compress.c +1 -4
@@ -700,7 +693,6 @@ static int f2fs_compress_pages(struct compress_ctx 
 *cc)
       for (i = 0; i < cc->nr_cpages; i++) {
           if (i < new_nr_cpages) {
-            new_cpages[i] = cc->cpages[i];
               continue;
           }
           f2fs_compress_free_page(cc->cpages[i]);
@@ -710,8 +702,6 @@ static int f2fs_compress_pages(struct compress_ctx 
 *cc)
       if (cops->destroy_compress_ctx)
           cops->destroy_compress_ctx(cc);
-    page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
-    cc->cpages = new_cpages;
       cc->nr_cpages = new_nr_cpages;
       trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
@@ -1330,7 +1320,7 @@ static int f2fs_write_compressed_pages(struct 
 compress_ctx *cc,
       spin_unlock(&fi->i_size_lock);
       f2fs_put_rpages(cc);
-    page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
+    page_array_free(cc->inode, cc->cpages, cc->raw_nr_cpages);
       cc->cpages = NULL;
       f2fs_destroy_compress_ctx(cc, false);
       return 0;
fs/f2fs/f2fs.h +0 -0
index 867f2c5d9559..8b1f84d88a65 100644
@@ -1454,6 +1454,7 @@ struct compress_ctx {
Quoted 9 lines
Quoted 8 lines

      unsigned int nr_rpages;        /* total page number in rpages */ struct page **cpages;        /* pages store compressed data in cluster */ unsigned int nr_cpages;        /* total page number in cpages */ +    unsigned int raw_nr_cpages;    /* max total page number in cpages */ void *rbuf;            /* virtual mapped address on rpages */ struct compress_data *cbuf;    /* virtual mapped address on cpages */ size_t rlen;            /* valid data length in rbuf */

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

maintainer
#3 lore raw
Quoted 34 lines · Fengnan Chang

Sorry, I didn't get your point, in my opinion, new_nr_cpages should always little than nr_cpages, is this right? So we can just use cpages, don't need to alloc new one.

Thanks.

Quoted 26 lines · Chao Yu
Quoted 2 lines · Fengnan Chang

Don't alloc new page array to replace old, just use old page array, try to reduce one page array alloc and free when write compress page.

Nope, see whole story in below link:

https://lore.kernel.org/patchwork/patch/1305096/

Thanks,

Quoted 16 lines
---
   fs/f2fs/compress.c | 14 ++------------
   fs/f2fs/f2fs.h     |  1 +
   2 files changed, 3 insertions(+), 12 deletions(-)
fs/f2fs/compress.c +0 -0
index 455561826c7d..43daafe382e7 100644
@@ -618,7 +618,6 @@ static int f2fs_compress_pages(struct compress_ctx
 *cc)
       const struct f2fs_compress_ops *cops =
                   f2fs_cops[fi->i_compress_algorithm];
       unsigned int max_len, new_nr_cpages;
Quoted 8 lines
Quoted 8 lines

fs/f2fs/compress.c static int f2fs_compress_pages(struct compress_ctx · line 635

Quoted 8 lines
fs/f2fs/compress.c +1 -1
-    struct page **new_cpages;
       u32 chksum = 0;
       int i, ret;
@@ -633,6 +632,7 @@ static int f2fs_compress_pages(struct compress_ctx
 *cc)
       max_len = COMPRESS_HEADER_SIZE + cc->clen;
       cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
+    cc->raw_nr_cpages = cc->nr_cpages;

If raw_nr_cpags is used to store original nr_cpages, we need to call page_array_free(, cc->cpages, cc->raw_nr_cpages) in all places, can you please check whether this are any missed cases?

Thanks,

Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
fs/f2fs/compress.c +1 -11
       cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
       if (!cc->cpages) {
@@ -683,13 +683,6 @@ static int f2fs_compress_pages(struct
 compress_ctx *cc)
       new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE,
 PAGE_SIZE);
-    /* Now we're going to cut unnecessary tail pages */
-    new_cpages = page_array_alloc(cc->inode, new_nr_cpages);
-    if (!new_cpages) {
-        ret = -ENOMEM;
-        goto out_vunmap_cbuf;
-    }
-
       /* zero out any unused part of the last page */
       memset(&cc->cbuf->cdata[cc->clen], 0,
               (new_nr_cpages * PAGE_SIZE) -
@@ -700,7 +693,6 @@ static int f2fs_compress_pages(struct compress_ctx
 *cc)
       for (i = 0; i < cc->nr_cpages; i++) {
           if (i < new_nr_cpages) {
-            new_cpages[i] = cc->cpages[i];
               continue;
           }
           f2fs_compress_free_page(cc->cpages[i]);
@@ -710,8 +702,6 @@ static int f2fs_compress_pages(struct compress_ctx
 *cc)
       if (cops->destroy_compress_ctx)
           cops->destroy_compress_ctx(cc);
-    page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
-    cc->cpages = new_cpages;
       cc->nr_cpages = new_nr_cpages;
       trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
@@ -1330,7 +1320,7 @@ static int f2fs_write_compressed_pages(struct
 compress_ctx *cc,
       spin_unlock(&fi->i_size_lock);
       f2fs_put_rpages(cc);
-    page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
+    page_array_free(cc->inode, cc->cpages, cc->raw_nr_cpages);
       cc->cpages = NULL;
       f2fs_destroy_compress_ctx(cc, false);
       return 0;
fs/f2fs/f2fs.h +0 -0
index 867f2c5d9559..8b1f84d88a65 100644
@@ -1454,6 +1454,7 @@ struct compress_ctx {
       unsigned int nr_rpages;        /* total page number in rpages */
       struct page **cpages;        /* pages store compressed data in

cluster */

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

      unsigned int nr_cpages;        /* total page number in cpages */
+    unsigned int raw_nr_cpages;    /* max total page number in cpages */
      void *rbuf;            /* virtual mapped address on rpages */ struct compress_data *cbuf;    /* virtual mapped address on
cpages */
      size_t rlen;            /* valid data length in rbuf */

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#4 lore raw

ok, it seems there is one place was missed.

Thanks.

Quoted 48 lines · Chao Yu
Quoted 34 lines · Fengnan Chang

Sorry, I didn't get your point, in my opinion, new_nr_cpages should always little than nr_cpages, is this right? So we can just use cpages, don't need to alloc new one.

Thanks.

Quoted 26 lines · Chao Yu
Quoted 2 lines · Fengnan Chang

Don't alloc new page array to replace old, just use old page array, try to reduce one page array alloc and free when write compress page.

Nope, see whole story in below link:

https://lore.kernel.org/patchwork/patch/1305096/

Thanks,

Quoted 16 lines

---
   fs/f2fs/compress.c | 14 ++------------
   fs/f2fs/f2fs.h     |  1 +

   2 files changed, 3 insertions(+), 12 deletions(-)
fs/f2fs/compress.c +0 -0
index 455561826c7d..43daafe382e7 100644
@@ -618,7 +618,6 @@ static int f2fs_compress_pages(struct compress_ctx
 *cc)

       const struct f2fs_compress_ops *cops = f2fs_cops[fi->i_compress_algorithm]; unsigned int max_len, new_nr_cpages;

Quoted 8 lines
Quoted 8 lines

fs/f2fs/compress.c static int f2fs_compress_pages(struct compress_ctx · line 635

Quoted 8 lines

-    struct page **new_cpages;
       u32 chksum = 0;
       int i, ret;

fs/f2fs/compress.c +1 -0
@@ -633,6 +632,7 @@ static int f2fs_compress_pages(struct compress_ctx
 *cc)
        max_len = COMPRESS_HEADER_SIZE + cc->clen;
        cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
+    cc->raw_nr_cpages = cc->nr_cpages;

If raw_nr_cpags is used to store original nr_cpages, we need to call page_array_free(, cc->cpages, cc->raw_nr_cpages) in all places, can you please check whether this are any missed cases?

Quoted 61 lines

Thanks,

Quoted 58 lines
Quoted 58 lines
Quoted 57 lines
fs/f2fs/compress.c +0 -7
        cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
        if (!cc->cpages) {
@@ -683,13 +683,6 @@ static int f2fs_compress_pages(struct
 compress_ctx *cc)
        new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE,
 PAGE_SIZE);
-    /* Now we're going to cut unnecessary tail pages */
-    new_cpages = page_array_alloc(cc->inode, new_nr_cpages);
-    if (!new_cpages) {
-        ret = -ENOMEM;
-        goto out_vunmap_cbuf;
-    }
-
        /* zero out any unused part of the last page */
        memset(&cc->cbuf->cdata[cc->clen], 0,

               (new_nr_cpages * PAGE_SIZE) -

fs/f2fs/compress.c +1 -4
@@ -700,7 +693,6 @@ static int f2fs_compress_pages(struct compress_ctx
 *cc)
        for (i = 0; i < cc->nr_cpages; i++) {
            if (i < new_nr_cpages) {
-            new_cpages[i] = cc->cpages[i];
                continue;
            }
            f2fs_compress_free_page(cc->cpages[i]);
@@ -710,8 +702,6 @@ static int f2fs_compress_pages(struct compress_ctx
 *cc)
        if (cops->destroy_compress_ctx)
            cops->destroy_compress_ctx(cc);
-    page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
-    cc->cpages = new_cpages;
        cc->nr_cpages = new_nr_cpages;
        trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
@@ -1330,7 +1320,7 @@ static int f2fs_write_compressed_pages(struct
 compress_ctx *cc,
        spin_unlock(&fi->i_size_lock);
        f2fs_put_rpages(cc);
-    page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
+    page_array_free(cc->inode, cc->cpages, cc->raw_nr_cpages);
        cc->cpages = NULL;
        f2fs_destroy_compress_ctx(cc, false);
        return 0;
fs/f2fs/f2fs.h +0 -0
index 867f2c5d9559..8b1f84d88a65 100644
@@ -1454,6 +1454,7 @@ struct compress_ctx {

       unsigned int nr_rpages;        /* total page number in rpages */ struct page **cpages;        /* pages store compressed data in cluster */
       unsigned int nr_cpages;        /* total page number in cpages */ +    unsigned int raw_nr_cpages;    /* max total page number in cpages */
       void *rbuf;            /* virtual mapped address on rpages */ struct compress_data *cbuf;    /* virtual mapped address on cpages */
       size_t rlen;            /* valid data length in rbuf */

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel