Re: [PATCH v2] /proc/kpagecount: return 0 for special pages that are never mapped

2018-12-10 Thread Naoya Horiguchi
On Mon, Dec 10, 2018 at 02:35:13PM -0800, Anthony Yznaga wrote:
> Certain pages that are never mapped to userspace have a type
> indicated in the page_type field of their struct pages (e.g. PG_buddy).
> page_type overlaps with _mapcount so set the count to 0 and avoid
> calling page_mapcount() for these pages.
> 
> Signed-off-by: Anthony Yznaga 
> Acked-by: Matthew Wilcox 

Looks good to me.

Reviewed-by: Naoya Horiguchi 

> ---
>   v2 - incorporated feedback from Matthew Wilcox
> 
>  fs/proc/page.c | 2 +-
>  include/linux/page-flags.h | 6 ++
>  2 files changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/proc/page.c b/fs/proc/page.c
> index 6c517b11acf8..40b05e0d4274 100644
> --- a/fs/proc/page.c
> +++ b/fs/proc/page.c
> @@ -46,7 +46,7 @@ static ssize_t kpagecount_read(struct file *file, char 
> __user *buf,
>   ppage = pfn_to_page(pfn);
>   else
>   ppage = NULL;
> - if (!ppage || PageSlab(ppage))
> + if (!ppage || PageSlab(ppage) || page_has_type(ppage))
>   pcount = 0;
>   else
>   pcount = page_mapcount(ppage);
> diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
> index 50ce1bddaf56..39b4494e29f1 100644
> --- a/include/linux/page-flags.h
> +++ b/include/linux/page-flags.h
> @@ -669,6 +669,7 @@ static inline int TestClearPageDoubleMap(struct page 
> *page)
>  
>  #define PAGE_TYPE_BASE   0xf000
>  /* Reserve   0x007f to catch underflows of page_mapcount */
> +#define PAGE_MAPCOUNT_RESERVE-128
>  #define PG_buddy 0x0080
>  #define PG_balloon   0x0100
>  #define PG_kmemcg0x0200
> @@ -677,6 +678,11 @@ static inline int TestClearPageDoubleMap(struct page 
> *page)
>  #define PageType(page, flag) \
>   ((page->page_type & (PAGE_TYPE_BASE | flag)) == PAGE_TYPE_BASE)
>  
> +static inline int page_has_type(struct page *page)
> +{
> + return (int)page->page_type < PAGE_MAPCOUNT_RESERVE;
> +}
> +
>  #define PAGE_TYPE_OPS(uname, lname)  \
>  static __always_inline int Page##uname(struct page *page)\
>  {\
> -- 
> 1.8.3.1
> 
> 


[PATCH v2] /proc/kpagecount: return 0 for special pages that are never mapped

2018-12-10 Thread Anthony Yznaga
Certain pages that are never mapped to userspace have a type
indicated in the page_type field of their struct pages (e.g. PG_buddy).
page_type overlaps with _mapcount so set the count to 0 and avoid
calling page_mapcount() for these pages.

Signed-off-by: Anthony Yznaga 
Acked-by: Matthew Wilcox 
---
v2 - incorporated feedback from Matthew Wilcox

 fs/proc/page.c | 2 +-
 include/linux/page-flags.h | 6 ++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/fs/proc/page.c b/fs/proc/page.c
index 6c517b11acf8..40b05e0d4274 100644
--- a/fs/proc/page.c
+++ b/fs/proc/page.c
@@ -46,7 +46,7 @@ static ssize_t kpagecount_read(struct file *file, char __user 
*buf,
ppage = pfn_to_page(pfn);
else
ppage = NULL;
-   if (!ppage || PageSlab(ppage))
+   if (!ppage || PageSlab(ppage) || page_has_type(ppage))
pcount = 0;
else
pcount = page_mapcount(ppage);
diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index 50ce1bddaf56..39b4494e29f1 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -669,6 +669,7 @@ static inline int TestClearPageDoubleMap(struct page *page)
 
 #define PAGE_TYPE_BASE 0xf000
 /* Reserve 0x007f to catch underflows of page_mapcount */
+#define PAGE_MAPCOUNT_RESERVE  -128
 #define PG_buddy   0x0080
 #define PG_balloon 0x0100
 #define PG_kmemcg  0x0200
@@ -677,6 +678,11 @@ static inline int TestClearPageDoubleMap(struct page *page)
 #define PageType(page, flag)   \
((page->page_type & (PAGE_TYPE_BASE | flag)) == PAGE_TYPE_BASE)
 
+static inline int page_has_type(struct page *page)
+{
+   return (int)page->page_type < PAGE_MAPCOUNT_RESERVE;
+}
+
 #define PAGE_TYPE_OPS(uname, lname)\
 static __always_inline int Page##uname(struct page *page)  \
 {  \
-- 
1.8.3.1