2015-08-25 13:32:05

by Yaowei Bai

[permalink] [raw]
Subject: [PATCH v2] mm/page_alloc: add a helper function to check page before alloc/free

The major portion of check_new_page() and free_pages_check() are same,
introduce a helper function check_one_page() for simplification.

Change in v2:
- use bad_flags as parameter directly per Michal Hocko

Signed-off-by: Yaowei Bai <[email protected]>
---
mm/page_alloc.c | 49 ++++++++++++++++++-------------------------------
1 file changed, 18 insertions(+), 31 deletions(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 5b5240b..0a0acdb 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -707,10 +707,9 @@ out:
zone->free_area[order].nr_free++;
}

-static inline int free_pages_check(struct page *page)
+static inline int check_one_page(struct page *page, unsigned long bad_flags)
{
const char *bad_reason = NULL;
- unsigned long bad_flags = 0;

if (unlikely(page_mapcount(page)))
bad_reason = "nonzero mapcount";
@@ -718,9 +717,11 @@ static inline int free_pages_check(struct page *page)
bad_reason = "non-NULL mapping";
if (unlikely(atomic_read(&page->_count) != 0))
bad_reason = "nonzero _count";
- if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_FREE)) {
- bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set";
- bad_flags = PAGE_FLAGS_CHECK_AT_FREE;
+ if (unlikely(page->flags & bad_flags)) {
+ if (bad_flags == PAGE_FLAGS_CHECK_AT_PREP)
+ bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set";
+ else if (bad_flags == PAGE_FLAGS_CHECK_AT_FREE)
+ bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag set";
}
#ifdef CONFIG_MEMCG
if (unlikely(page->mem_cgroup))
@@ -730,6 +731,17 @@ static inline int free_pages_check(struct page *page)
bad_page(page, bad_reason, bad_flags);
return 1;
}
+ return 0;
+}
+
+static inline int free_pages_check(struct page *page)
+{
+ int ret = 0;
+
+ ret = check_one_page(page, PAGE_FLAGS_CHECK_AT_FREE);
+ if (ret)
+ return ret;
+
page_cpupid_reset_last(page);
if (page->flags & PAGE_FLAGS_CHECK_AT_PREP)
page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
@@ -1287,32 +1299,7 @@ static inline void expand(struct zone *zone, struct page *page,
*/
static inline int check_new_page(struct page *page)
{
- const char *bad_reason = NULL;
- unsigned long bad_flags = 0;
-
- if (unlikely(page_mapcount(page)))
- bad_reason = "nonzero mapcount";
- if (unlikely(page->mapping != NULL))
- bad_reason = "non-NULL mapping";
- if (unlikely(atomic_read(&page->_count) != 0))
- bad_reason = "nonzero _count";
- if (unlikely(page->flags & __PG_HWPOISON)) {
- bad_reason = "HWPoisoned (hardware-corrupted)";
- bad_flags = __PG_HWPOISON;
- }
- if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_PREP)) {
- bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set";
- bad_flags = PAGE_FLAGS_CHECK_AT_PREP;
- }
-#ifdef CONFIG_MEMCG
- if (unlikely(page->mem_cgroup))
- bad_reason = "page still charged to cgroup";
-#endif
- if (unlikely(bad_reason)) {
- bad_page(page, bad_reason, bad_flags);
- return 1;
- }
- return 0;
+ return check_one_page(page, PAGE_FLAGS_CHECK_AT_PREP);
}

static int prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
--
1.9.1


2015-08-25 14:03:26

by Michal Hocko

[permalink] [raw]
Subject: Re: [PATCH v2] mm/page_alloc: add a helper function to check page before alloc/free

On Tue 25-08-15 21:26:30, Yaowei Bai wrote:
[...]
> static inline int check_new_page(struct page *page)
> {
> - const char *bad_reason = NULL;
> - unsigned long bad_flags = 0;
> -
> - if (unlikely(page_mapcount(page)))
> - bad_reason = "nonzero mapcount";
> - if (unlikely(page->mapping != NULL))
> - bad_reason = "non-NULL mapping";
> - if (unlikely(atomic_read(&page->_count) != 0))
> - bad_reason = "nonzero _count";
> - if (unlikely(page->flags & __PG_HWPOISON)) {
> - bad_reason = "HWPoisoned (hardware-corrupted)";
> - bad_flags = __PG_HWPOISON;
> - }

You have removed this check AFAICS. Now looking at 39ad4f19671d ("mm:
check __PG_HWPOISON separately from PAGE_FLAGS_CHECK_AT_*") I am not
sure it is correct to check it in the free path as it was removed from
the mask by this commit.

> - if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_PREP)) {
> - bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set";
> - bad_flags = PAGE_FLAGS_CHECK_AT_PREP;
> - }
> -#ifdef CONFIG_MEMCG
> - if (unlikely(page->mem_cgroup))
> - bad_reason = "page still charged to cgroup";
> -#endif
> - if (unlikely(bad_reason)) {
> - bad_page(page, bad_reason, bad_flags);
> - return 1;
> - }
> - return 0;
> + return check_one_page(page, PAGE_FLAGS_CHECK_AT_PREP);
> }
>
> static int prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
> --
> 1.9.1
>

--
Michal Hocko
SUSE Labs

2015-08-26 14:54:49

by Yaowei Bai

[permalink] [raw]
Subject: Re: [PATCH v2] mm/page_alloc: add a helper function to check page before alloc/free

On Tue, Aug 25, 2015 at 04:03:22PM +0200, Michal Hocko wrote:
> On Tue 25-08-15 21:26:30, Yaowei Bai wrote:
> [...]
> > static inline int check_new_page(struct page *page)
> > {
> > - const char *bad_reason = NULL;
> > - unsigned long bad_flags = 0;
> > -
> > - if (unlikely(page_mapcount(page)))
> > - bad_reason = "nonzero mapcount";
> > - if (unlikely(page->mapping != NULL))
> > - bad_reason = "non-NULL mapping";
> > - if (unlikely(atomic_read(&page->_count) != 0))
> > - bad_reason = "nonzero _count";
> > - if (unlikely(page->flags & __PG_HWPOISON)) {
> > - bad_reason = "HWPoisoned (hardware-corrupted)";
> > - bad_flags = __PG_HWPOISON;
> > - }
>
> You have removed this check AFAICS. Now looking at 39ad4f19671d ("mm:

I missed that check mistakenly, it should be there.

> check __PG_HWPOISON separately from PAGE_FLAGS_CHECK_AT_*") I am not
> sure it is correct to check it in the free path as it was removed from
> the mask by this commit.

I investigated the commit you mentioned above. AFAICS, that commit assumes
that __PG_HWPOISON check should be performed in the alloc path, while in
the free path it doesn't need to. So there is a bit different in alloc/free
paths.
So Michal, do you think there is any obvious points to refactor these
two functions still? Anyway, appreciate your reviewing.

>
> > - if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_PREP)) {
> > - bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set";
> > - bad_flags = PAGE_FLAGS_CHECK_AT_PREP;
> > - }
> > -#ifdef CONFIG_MEMCG
> > - if (unlikely(page->mem_cgroup))
> > - bad_reason = "page still charged to cgroup";
> > -#endif
> > - if (unlikely(bad_reason)) {
> > - bad_page(page, bad_reason, bad_flags);
> > - return 1;
> > - }
> > - return 0;
> > + return check_one_page(page, PAGE_FLAGS_CHECK_AT_PREP);
> > }
> >
> > static int prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
> > --
> > 1.9.1
> >
>
> --
> Michal Hocko
> SUSE Labs

2015-08-27 13:02:21

by Yaowei Bai

[permalink] [raw]
Subject: Re: [PATCH v2] mm/page_alloc: add a helper function to check page before alloc/free

On Tue, Aug 25, 2015 at 04:03:22PM +0200, Michal Hocko wrote:
> On Tue 25-08-15 21:26:30, Yaowei Bai wrote:
> [...]
> > static inline int check_new_page(struct page *page)
> > {
> > - const char *bad_reason = NULL;
> > - unsigned long bad_flags = 0;
> > -
> > - if (unlikely(page_mapcount(page)))
> > - bad_reason = "nonzero mapcount";
> > - if (unlikely(page->mapping != NULL))
> > - bad_reason = "non-NULL mapping";
> > - if (unlikely(atomic_read(&page->_count) != 0))
> > - bad_reason = "nonzero _count";
> > - if (unlikely(page->flags & __PG_HWPOISON)) {
> > - bad_reason = "HWPoisoned (hardware-corrupted)";
> > - bad_flags = __PG_HWPOISON;
> > - }
>
> You have removed this check AFAICS. Now looking at 39ad4f19671d ("mm:
> check __PG_HWPOISON separately from PAGE_FLAGS_CHECK_AT_*") I am not
> sure it is correct to check it in the free path as it was removed from
> the mask by this commit.

I just refactored these two function and it looks well, will resend it soon.

>
> > - if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_PREP)) {
> > - bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set";
> > - bad_flags = PAGE_FLAGS_CHECK_AT_PREP;
> > - }
> > -#ifdef CONFIG_MEMCG
> > - if (unlikely(page->mem_cgroup))
> > - bad_reason = "page still charged to cgroup";
> > -#endif
> > - if (unlikely(bad_reason)) {
> > - bad_page(page, bad_reason, bad_flags);
> > - return 1;
> > - }
> > - return 0;
> > + return check_one_page(page, PAGE_FLAGS_CHECK_AT_PREP);
> > }
> >
> > static int prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
> > --
> > 1.9.1
> >
>
> --
> Michal Hocko
> SUSE Labs