erofs: (testing only) page leak detection

(For testing/debugging only) Detect potential page leak.

Signed-off-by: Gao Xiang <hsiangkao@redhat.com>
diff --git a/fs/erofs/compress.h b/fs/erofs/compress.h
index aea129d..1583e24 100644
--- a/fs/erofs/compress.h
+++ b/fs/erofs/compress.h
@@ -67,6 +67,8 @@
 static inline bool z_erofs_put_shortlivedpage(struct list_head *pagepool,
 					      struct page *page)
 {
+	extern atomic_t erofs_tmppagecount;
+
 	if (!z_erofs_is_shortlived_page(page))
 		return false;
 
@@ -76,6 +78,8 @@
 	} else {
 		/* follow the pcluster rule above. */
 		set_page_private(page, 0);
+
+		atomic_dec(&erofs_tmppagecount);
 		list_add(&page->lru, pagepool);
 	}
 	return true;
diff --git a/fs/erofs/utils.c b/fs/erofs/utils.c
index de9986d..0cd5e4f 100644
--- a/fs/erofs/utils.c
+++ b/fs/erofs/utils.c
@@ -5,8 +5,11 @@
  * Created by Gao Xiang <gaoxiang25@huawei.com>
  */
 #include "internal.h"
+#include "zdata.h"
 #include <linux/pagevec.h>
 
+atomic_t erofs_tmppagecount;
+
 struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp)
 {
 	struct page *page;
@@ -18,6 +21,7 @@
 	} else {
 		page = alloc_page(gfp);
 	}
+	atomic_inc(&erofs_tmppagecount);
 	return page;
 }
 
@@ -169,6 +173,15 @@
 	unsigned int freed = 0;
 	unsigned long index;
 
+
+	static unsigned int jiffies_old;
+
+	if (jiffies >= jiffies_old + HZ * 5) {
+		pr_err("cached page count: %lu tmp page count: %u\n",
+		       MNGD_MAPPING(sbi)->nrpages, atomic_read(&erofs_tmppagecount));
+		jiffies_old = jiffies;
+	}
+
 	xa_for_each(&sbi->managed_pslots, index, grp) {
 		/* try to shrink each valid workgroup */
 		if (!erofs_try_to_release_workgroup(sbi, grp))
diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c
index 6cb356c..cc09287 100644
--- a/fs/erofs/zdata.c
+++ b/fs/erofs/zdata.c
@@ -10,6 +10,8 @@
 
 #include <trace/events/erofs.h>
 
+extern atomic_t erofs_tmppagecount;
+
 /*
  * a compressed_pages[] placeholder in order to avoid
  * being filled with file pages for in-place decompression.
@@ -210,6 +212,7 @@
 			put_page(page);
 		} else if (newpage) {
 			set_page_private(newpage, 0);
+			atomic_dec(&erofs_tmppagecount);
 			list_add(&newpage->lru, pagepool);
 		}
 	}
@@ -1095,6 +1098,7 @@
 out_allocpage:
 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
+		atomic_dec(&erofs_tmppagecount);
 		list_add(&page->lru, pagepool);
 		cond_resched();
 		goto repeat;
@@ -1105,6 +1109,7 @@
 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
 		goto out;
 	}
+	atomic_dec(&erofs_tmppagecount);
 	attach_page_private(page, pcl);
 	/* drop a refcount added by allocpage (then we have 2 refs here) */
 	put_page(page);