diff mbox series

mm, soft-offline: convert parameter to pfn

Message ID 20191016070924.GA10178@hori.linux.bs1.fc.nec.co.jp (mailing list archive)
State New, archived
Headers show
Series mm, soft-offline: convert parameter to pfn | expand

Commit Message

Naoya Horiguchi Oct. 16, 2019, 7:09 a.m. UTC
Hi,

I wrote a simple cleanup for parameter of soft_offline_page(),
based on thread https://lkml.org/lkml/2019/10/11/57.

I know that we need more cleanup on hwpoison-inject, but I think
that will be mentioned in re-write patchset Oscar is preparing now.
So let me shared only this part as a separate one now.

Thanks,
Naoya Horiguchi
---
From: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Date: Wed, 16 Oct 2019 15:49:00 +0900
Subject: [PATCH] mm, soft-offline: convert parameter to pfn

Currently soft_offline_page() receives struct page, and its sibling
memory_failure() receives pfn. This discrepancy looks weird and makes
precheck on pfn validity tricky. So let's align them.

Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
---
 drivers/base/memory.c | 2 +-
 include/linux/mm.h    | 2 +-
 mm/madvise.c          | 2 +-
 mm/memory-failure.c   | 8 ++++----
 4 files changed, 7 insertions(+), 7 deletions(-)

Comments

David Hildenbrand Oct. 16, 2019, 7:56 a.m. UTC | #1
On 16.10.19 09:09, Naoya Horiguchi wrote:
> Hi,
> 
> I wrote a simple cleanup for parameter of soft_offline_page(),
> based on thread https://lkml.org/lkml/2019/10/11/57.
> 
> I know that we need more cleanup on hwpoison-inject, but I think
> that will be mentioned in re-write patchset Oscar is preparing now.
> So let me shared only this part as a separate one now.
> 
> Thanks,
> Naoya Horiguchi
> ---
> From: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
> Date: Wed, 16 Oct 2019 15:49:00 +0900
> Subject: [PATCH] mm, soft-offline: convert parameter to pfn
> 
> Currently soft_offline_page() receives struct page, and its sibling
> memory_failure() receives pfn. This discrepancy looks weird and makes
> precheck on pfn validity tricky. So let's align them.
> 
> Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
> ---
>   drivers/base/memory.c | 2 +-
>   include/linux/mm.h    | 2 +-
>   mm/madvise.c          | 2 +-
>   mm/memory-failure.c   | 8 ++++----
>   4 files changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> index e5485c22ef77..04e469c82852 100644
> --- a/drivers/base/memory.c
> +++ b/drivers/base/memory.c
> @@ -540,7 +540,7 @@ static ssize_t soft_offline_page_store(struct device *dev,
>   	pfn >>= PAGE_SHIFT;
>   	if (!pfn_valid(pfn))
>   		return -ENXIO;
> -	ret = soft_offline_page(pfn_to_page(pfn));
> +	ret = soft_offline_page(pfn);
>   	return ret == 0 ? count : ret;
>   }
>   
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 3eba26324ff1..0a452020edf5 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -2783,7 +2783,7 @@ extern int sysctl_memory_failure_early_kill;
>   extern int sysctl_memory_failure_recovery;
>   extern void shake_page(struct page *p, int access);
>   extern atomic_long_t num_poisoned_pages __read_mostly;
> -extern int soft_offline_page(struct page *page);
> +extern int soft_offline_page(unsigned long pfn);
>   
>   
>   /*
> diff --git a/mm/madvise.c b/mm/madvise.c
> index fd221b610b52..df198d1e5e2e 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -893,7 +893,7 @@ static int madvise_inject_error(int behavior,
>   		if (behavior == MADV_SOFT_OFFLINE) {
>   			pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
>   				 pfn, start);
> -			ret = soft_offline_page(page);
> +			ret = soft_offline_page(pfn);
>   			if (ret)
>   				return ret;
>   		} else {
> diff --git a/mm/memory-failure.c b/mm/memory-failure.c
> index 4f16e0a7e7cc..eb4fd5e8d5e1 100644
> --- a/mm/memory-failure.c
> +++ b/mm/memory-failure.c
> @@ -1514,7 +1514,7 @@ static void memory_failure_work_func(struct work_struct *work)
>   		if (!gotten)
>   			break;
>   		if (entry.flags & MF_SOFT_OFFLINE)
> -			soft_offline_page(pfn_to_page(entry.pfn));
> +			soft_offline_page(entry.pfn);
>   		else
>   			memory_failure(entry.pfn, entry.flags);
>   	}
> @@ -1822,7 +1822,7 @@ static int soft_offline_free_page(struct page *page)
>   
>   /**
>    * soft_offline_page - Soft offline a page.
> - * @page: page to offline
> + * @pfn: pfn to soft-offline
>    *
>    * Returns 0 on success, otherwise negated errno.
>    *
> @@ -1841,10 +1841,10 @@ static int soft_offline_free_page(struct page *page)
>    * This is not a 100% solution for all memory, but tries to be
>    * ``good enough'' for the majority of memory.
>    */
> -int soft_offline_page(struct page *page)
> +int soft_offline_page(unsigned long pfn)
>   {
>   	int ret;
> -	unsigned long pfn = page_to_pfn(page);
> +	struct page *page = pfn_to_page(pfn);
>   
>   	if (is_zone_device_page(page)) {
>   		pr_debug_ratelimited("soft_offline: %#lx page is device page\n",
> 

I think you should rebase that patch on linux-next (where the  
pfn_to_online_page() check is in place). I assume you'll want to move  
the pfn_to_online_page() check into soft_offline_page() then as well?
Naoya Horiguchi Oct. 16, 2019, 8:27 a.m. UTC | #2
On Wed, Oct 16, 2019 at 09:56:19AM +0200, David Hildenbrand wrote:
> On 16.10.19 09:09, Naoya Horiguchi wrote:
> > Hi,
> > 
> > I wrote a simple cleanup for parameter of soft_offline_page(),
> > based on thread https://lkml.org/lkml/2019/10/11/57.
> > 
> > I know that we need more cleanup on hwpoison-inject, but I think
> > that will be mentioned in re-write patchset Oscar is preparing now.
> > So let me shared only this part as a separate one now.
...
> 
> I think you should rebase that patch on linux-next (where the
> pfn_to_online_page() check is in place). I assume you'll want to move the
> pfn_to_online_page() check into soft_offline_page() then as well?

I rebased to next-20191016. And yes, we will move pfn_to_online_page()
into soft offline code.  It seems that we can also move pfn_valid(),
but is simply moving like below good enough for you?

  @@ -1877,11 +1877,17 @@ static int soft_offline_free_page(struct page *page)
    * This is not a 100% solution for all memory, but tries to be
    * ``good enough'' for the majority of memory.
    */
  -int soft_offline_page(struct page *page, int flags)
  +int soft_offline_page(unsigned long pfn, int flags)
   {
   	int ret;
  -	unsigned long pfn = page_to_pfn(page);
  +	struct page *page;
   
  +	if (!pfn_valid(pfn))
  +		return -ENXIO;
  +	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
  +	if (!pfn_to_online_page(pfn))
  +		return -EIO;
  +	page = pfn_to_page(pfn);
   	if (is_zone_device_page(page)) {
   		pr_debug_ratelimited("soft_offline: %#lx page is device page\n",
   				pfn);
  -- 

Or we might have an option to do as memory_failure() does like below:

  int memory_failure(unsigned long pfn, int flags)
  {
          ....
          p = pfn_to_online_page(pfn);
          if (!p) {
                  if (pfn_valid(pfn)) {
                          pgmap = get_dev_pagemap(pfn, NULL);
                          if (pgmap)
                                  return memory_failure_dev_pagemap(pfn, flags,
                                                                    pgmap);
                  }
                  pr_err("Memory failure: %#lx: memory outside kernel control\n",
                          pfn);
                  return -ENXIO;
          }

Thanks,
Naoya Horiguchi
David Hildenbrand Oct. 16, 2019, 8:34 a.m. UTC | #3
On 16.10.19 10:27, Naoya Horiguchi wrote:
> On Wed, Oct 16, 2019 at 09:56:19AM +0200, David Hildenbrand wrote:
>> On 16.10.19 09:09, Naoya Horiguchi wrote:
>>> Hi,
>>>
>>> I wrote a simple cleanup for parameter of soft_offline_page(),
>>> based on thread https://lkml.org/lkml/2019/10/11/57.
>>>
>>> I know that we need more cleanup on hwpoison-inject, but I think
>>> that will be mentioned in re-write patchset Oscar is preparing now.
>>> So let me shared only this part as a separate one now.
> ...
>>
>> I think you should rebase that patch on linux-next (where the
>> pfn_to_online_page() check is in place). I assume you'll want to move the
>> pfn_to_online_page() check into soft_offline_page() then as well?
> 
> I rebased to next-20191016. And yes, we will move pfn_to_online_page()
> into soft offline code.  It seems that we can also move pfn_valid(),
> but is simply moving like below good enough for you?

At least I can't am the patch to current next/master (due to  
pfn_to_online_page()).

> 
>    @@ -1877,11 +1877,17 @@ static int soft_offline_free_page(struct page *page)
>      * This is not a 100% solution for all memory, but tries to be
>      * ``good enough'' for the majority of memory.
>      */
>    -int soft_offline_page(struct page *page, int flags)
>    +int soft_offline_page(unsigned long pfn, int flags)
>     {
>     	int ret;
>    -	unsigned long pfn = page_to_pfn(page);
>    +	struct page *page;
>     
>    +	if (!pfn_valid(pfn))
>    +		return -ENXIO;
>    +	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
>    +	if (!pfn_to_online_page(pfn))
>    +		return -EIO;
>    +	page = pfn_to_page(pfn);
>     	if (is_zone_device_page(page)) {
>     		pr_debug_ratelimited("soft_offline: %#lx page is device page\n",
>     				pfn);
>    --
> 
> Or we might have an option to do as memory_failure() does like below:

In contrast to soft offlining, memory failure can deal with devmem. So I  
think the above makes sense.
Naoya Horiguchi Oct. 16, 2019, 8:54 a.m. UTC | #4
On Wed, Oct 16, 2019 at 10:34:52AM +0200, David Hildenbrand wrote:
> On 16.10.19 10:27, Naoya Horiguchi wrote:
> > On Wed, Oct 16, 2019 at 09:56:19AM +0200, David Hildenbrand wrote:
> > > On 16.10.19 09:09, Naoya Horiguchi wrote:
> > > > Hi,
> > > > 
> > > > I wrote a simple cleanup for parameter of soft_offline_page(),
> > > > based on thread https://lkml.org/lkml/2019/10/11/57.
> > > > 
> > > > I know that we need more cleanup on hwpoison-inject, but I think
> > > > that will be mentioned in re-write patchset Oscar is preparing now.
> > > > So let me shared only this part as a separate one now.
> > ...
> > > 
> > > I think you should rebase that patch on linux-next (where the
> > > pfn_to_online_page() check is in place). I assume you'll want to move the
> > > pfn_to_online_page() check into soft_offline_page() then as well?
> > 
> > I rebased to next-20191016. And yes, we will move pfn_to_online_page()
> > into soft offline code.  It seems that we can also move pfn_valid(),
> > but is simply moving like below good enough for you?
> 
> At least I can't am the patch to current next/master (due to
> pfn_to_online_page()).
> 
> > 
> >    @@ -1877,11 +1877,17 @@ static int soft_offline_free_page(struct page *page)
> >      * This is not a 100% solution for all memory, but tries to be
> >      * ``good enough'' for the majority of memory.
> >      */
> >    -int soft_offline_page(struct page *page, int flags)
> >    +int soft_offline_page(unsigned long pfn, int flags)
> >     {
> >     	int ret;
> >    -	unsigned long pfn = page_to_pfn(page);
> >    +	struct page *page;
> >    +	if (!pfn_valid(pfn))
> >    +		return -ENXIO;
> >    +	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
> >    +	if (!pfn_to_online_page(pfn))
> >    +		return -EIO;
> >    +	page = pfn_to_page(pfn);
> >     	if (is_zone_device_page(page)) {
> >     		pr_debug_ratelimited("soft_offline: %#lx page is device page\n",
> >     				pfn);
> >    --
> > 
> > Or we might have an option to do as memory_failure() does like below:
> 
> In contrast to soft offlining, memory failure can deal with devmem. So I
> think the above makes sense.

OK, so here's the revised one.

Thanks,
Naoya Horiguchi
---
From: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Date: Wed, 16 Oct 2019 17:00:33 +0900
Subject: [PATCH] mm, soft-offline: convert parameter to pfn

Currently soft_offline_page() receives struct page, and its sibling
memory_failure() receives pfn. This discrepancy looks weird and makes
precheck on pfn validity tricky. So let's align them.

Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
---
 drivers/base/memory.c |  7 +------
 include/linux/mm.h    |  2 +-
 mm/madvise.c          |  2 +-
 mm/memory-failure.c   | 14 ++++++++++----
 4 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index 55907c27075b..a757d9ed88a7 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -538,12 +538,7 @@ static ssize_t soft_offline_page_store(struct device *dev,
 	if (kstrtoull(buf, 0, &pfn) < 0)
 		return -EINVAL;
 	pfn >>= PAGE_SHIFT;
-	if (!pfn_valid(pfn))
-		return -ENXIO;
-	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
-	if (!pfn_to_online_page(pfn))
-		return -EIO;
-	ret = soft_offline_page(pfn_to_page(pfn), 0);
+	ret = soft_offline_page(pfn, 0);
 	return ret == 0 ? count : ret;
 }
 
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 44d058723db9..fd360d208346 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2794,7 +2794,7 @@ extern int sysctl_memory_failure_early_kill;
 extern int sysctl_memory_failure_recovery;
 extern void shake_page(struct page *p, int access);
 extern atomic_long_t num_poisoned_pages __read_mostly;
-extern int soft_offline_page(struct page *page, int flags);
+extern int soft_offline_page(unsigned long pfn, int flags);
 
 
 /*
diff --git a/mm/madvise.c b/mm/madvise.c
index 2be9f3fdb05e..99dd06fecfa9 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -887,7 +887,7 @@ static int madvise_inject_error(int behavior,
 			pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
 					pfn, start);
 
-			ret = soft_offline_page(page, MF_COUNT_INCREASED);
+			ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
 			if (ret)
 				return ret;
 			continue;
diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index 05c8c6df25e6..bdf408d7f65c 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -1476,7 +1476,7 @@ static void memory_failure_work_func(struct work_struct *work)
 		if (!gotten)
 			break;
 		if (entry.flags & MF_SOFT_OFFLINE)
-			soft_offline_page(pfn_to_page(entry.pfn), entry.flags);
+			soft_offline_page(entry.pfn, entry.flags);
 		else
 			memory_failure(entry.pfn, entry.flags);
 	}
@@ -1857,7 +1857,7 @@ static int soft_offline_free_page(struct page *page)
 
 /**
  * soft_offline_page - Soft offline a page.
- * @page: page to offline
+ * @pfn: pfn to soft-offline
  * @flags: flags. Same as memory_failure().
  *
  * Returns 0 on success, otherwise negated errno.
@@ -1877,11 +1877,17 @@ static int soft_offline_free_page(struct page *page)
  * This is not a 100% solution for all memory, but tries to be
  * ``good enough'' for the majority of memory.
  */
-int soft_offline_page(struct page *page, int flags)
+int soft_offline_page(unsigned long pfn, int flags)
 {
 	int ret;
-	unsigned long pfn = page_to_pfn(page);
+	struct page *page;
 
+	if (!pfn_valid(pfn))
+		return -ENXIO;
+	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
+	page = pfn_to_online_page(pfn);
+	if (!page)
+		return -EIO;
 	if (is_zone_device_page(page)) {
 		pr_debug_ratelimited("soft_offline: %#lx page is device page\n",
 				pfn);
David Hildenbrand Oct. 16, 2019, 8:57 a.m. UTC | #5
On 16.10.19 10:54, Naoya Horiguchi wrote:
> On Wed, Oct 16, 2019 at 10:34:52AM +0200, David Hildenbrand wrote:
>> On 16.10.19 10:27, Naoya Horiguchi wrote:
>>> On Wed, Oct 16, 2019 at 09:56:19AM +0200, David Hildenbrand wrote:
>>>> On 16.10.19 09:09, Naoya Horiguchi wrote:
>>>>> Hi,
>>>>>
>>>>> I wrote a simple cleanup for parameter of soft_offline_page(),
>>>>> based on thread https://lkml.org/lkml/2019/10/11/57.
>>>>>
>>>>> I know that we need more cleanup on hwpoison-inject, but I think
>>>>> that will be mentioned in re-write patchset Oscar is preparing now.
>>>>> So let me shared only this part as a separate one now.
>>> ...
>>>>
>>>> I think you should rebase that patch on linux-next (where the
>>>> pfn_to_online_page() check is in place). I assume you'll want to move the
>>>> pfn_to_online_page() check into soft_offline_page() then as well?
>>>
>>> I rebased to next-20191016. And yes, we will move pfn_to_online_page()
>>> into soft offline code.  It seems that we can also move pfn_valid(),
>>> but is simply moving like below good enough for you?
>>
>> At least I can't am the patch to current next/master (due to
>> pfn_to_online_page()).

Could also be that my "git am" skills failed as the mail was not a  
proper patch itself :)

>>
>>>
>>>     @@ -1877,11 +1877,17 @@ static int soft_offline_free_page(struct page *page)
>>>       * This is not a 100% solution for all memory, but tries to be
>>>       * ``good enough'' for the majority of memory.
>>>       */
>>>     -int soft_offline_page(struct page *page, int flags)
>>>     +int soft_offline_page(unsigned long pfn, int flags)
>>>      {
>>>      	int ret;
>>>     -	unsigned long pfn = page_to_pfn(page);
>>>     +	struct page *page;
>>>     +	if (!pfn_valid(pfn))
>>>     +		return -ENXIO;
>>>     +	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
>>>     +	if (!pfn_to_online_page(pfn))
>>>     +		return -EIO;
>>>     +	page = pfn_to_page(pfn);
>>>      	if (is_zone_device_page(page)) {
>>>      		pr_debug_ratelimited("soft_offline: %#lx page is device page\n",
>>>      				pfn);
>>>     --
>>>
>>> Or we might have an option to do as memory_failure() does like below:
>>
>> In contrast to soft offlining, memory failure can deal with devmem. So I
>> think the above makes sense.
> 
> OK, so here's the revised one.
> 
> Thanks,
> Naoya Horiguchi
> ---
> From: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
> Date: Wed, 16 Oct 2019 17:00:33 +0900
> Subject: [PATCH] mm, soft-offline: convert parameter to pfn
> 
> Currently soft_offline_page() receives struct page, and its sibling
> memory_failure() receives pfn. This discrepancy looks weird and makes
> precheck on pfn validity tricky. So let's align them.
> 
> Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
> ---
>   drivers/base/memory.c |  7 +------
>   include/linux/mm.h    |  2 +-
>   mm/madvise.c          |  2 +-
>   mm/memory-failure.c   | 14 ++++++++++----
>   4 files changed, 13 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> index 55907c27075b..a757d9ed88a7 100644
> --- a/drivers/base/memory.c
> +++ b/drivers/base/memory.c
> @@ -538,12 +538,7 @@ static ssize_t soft_offline_page_store(struct device *dev,
>   	if (kstrtoull(buf, 0, &pfn) < 0)
>   		return -EINVAL;
>   	pfn >>= PAGE_SHIFT;
> -	if (!pfn_valid(pfn))
> -		return -ENXIO;
> -	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
> -	if (!pfn_to_online_page(pfn))
> -		return -EIO;
> -	ret = soft_offline_page(pfn_to_page(pfn), 0);
> +	ret = soft_offline_page(pfn, 0);
>   	return ret == 0 ? count : ret;
>   }
>   
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 44d058723db9..fd360d208346 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -2794,7 +2794,7 @@ extern int sysctl_memory_failure_early_kill;
>   extern int sysctl_memory_failure_recovery;
>   extern void shake_page(struct page *p, int access);
>   extern atomic_long_t num_poisoned_pages __read_mostly;
> -extern int soft_offline_page(struct page *page, int flags);
> +extern int soft_offline_page(unsigned long pfn, int flags);
>   
>   
>   /*
> diff --git a/mm/madvise.c b/mm/madvise.c
> index 2be9f3fdb05e..99dd06fecfa9 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -887,7 +887,7 @@ static int madvise_inject_error(int behavior,
>   			pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
>   					pfn, start);
>   
> -			ret = soft_offline_page(page, MF_COUNT_INCREASED);
> +			ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
>   			if (ret)
>   				return ret;
>   			continue;
> diff --git a/mm/memory-failure.c b/mm/memory-failure.c
> index 05c8c6df25e6..bdf408d7f65c 100644
> --- a/mm/memory-failure.c
> +++ b/mm/memory-failure.c
> @@ -1476,7 +1476,7 @@ static void memory_failure_work_func(struct work_struct *work)
>   		if (!gotten)
>   			break;
>   		if (entry.flags & MF_SOFT_OFFLINE)
> -			soft_offline_page(pfn_to_page(entry.pfn), entry.flags);
> +			soft_offline_page(entry.pfn, entry.flags);
>   		else
>   			memory_failure(entry.pfn, entry.flags);
>   	}
> @@ -1857,7 +1857,7 @@ static int soft_offline_free_page(struct page *page)
>   
>   /**
>    * soft_offline_page - Soft offline a page.
> - * @page: page to offline
> + * @pfn: pfn to soft-offline
>    * @flags: flags. Same as memory_failure().
>    *
>    * Returns 0 on success, otherwise negated errno.
> @@ -1877,11 +1877,17 @@ static int soft_offline_free_page(struct page *page)
>    * This is not a 100% solution for all memory, but tries to be
>    * ``good enough'' for the majority of memory.
>    */
> -int soft_offline_page(struct page *page, int flags)
> +int soft_offline_page(unsigned long pfn, int flags)
>   {
>   	int ret;
> -	unsigned long pfn = page_to_pfn(page);
> +	struct page *page;
>   
> +	if (!pfn_valid(pfn))
> +		return -ENXIO;
> +	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
> +	page = pfn_to_online_page(pfn);
> +	if (!page)
> +		return -EIO;
>   	if (is_zone_device_page(page)) {

-> this is now no longer possible! So you can drop the whole if  
(is_zone_device....) case

>   		pr_debug_ratelimited("soft_offline: %#lx page is device page\n",
>   				pfn);
> 

Apart from that, looks good to me.
Naoya Horiguchi Oct. 16, 2019, 11:47 p.m. UTC | #6
On Wed, Oct 16, 2019 at 10:57:57AM +0200, David Hildenbrand wrote:
> On 16.10.19 10:54, Naoya Horiguchi wrote:
> >On Wed, Oct 16, 2019 at 10:34:52AM +0200, David Hildenbrand wrote:
> >>On 16.10.19 10:27, Naoya Horiguchi wrote:
> >>>On Wed, Oct 16, 2019 at 09:56:19AM +0200, David Hildenbrand wrote:
> >>>>On 16.10.19 09:09, Naoya Horiguchi wrote:
> >>>>>Hi,
> >>>>>
> >>>>>I wrote a simple cleanup for parameter of soft_offline_page(),
> >>>>>based on thread https://lkml.org/lkml/2019/10/11/57.
> >>>>>
> >>>>>I know that we need more cleanup on hwpoison-inject, but I think
> >>>>>that will be mentioned in re-write patchset Oscar is preparing now.
> >>>>>So let me shared only this part as a separate one now.
> >>>...
> >>>>
> >>>>I think you should rebase that patch on linux-next (where the
> >>>>pfn_to_online_page() check is in place). I assume you'll want to move the
> >>>>pfn_to_online_page() check into soft_offline_page() then as well?
> >>>
> >>>I rebased to next-20191016. And yes, we will move pfn_to_online_page()
> >>>into soft offline code.  It seems that we can also move pfn_valid(),
> >>>but is simply moving like below good enough for you?
> >>
> >>At least I can't am the patch to current next/master (due to
> >>pfn_to_online_page()).
> 
> Could also be that my "git am" skills failed as the mail was not a
> proper patch itself :)

Sorry for the inconvenience, my company email system breaks original
message by introducing quoted-printable format ('=20' or '=3D').
Most mail client usually handles it but git-am doesn't.
I give up using it and send via smtp.gmail.com.

> >@@ -1877,11 +1877,17 @@ static int soft_offline_free_page(struct page *page)
> >   * This is not a 100% solution for all memory, but tries to be
> >   * ``good enough'' for the majority of memory.
> >   */
> >-int soft_offline_page(struct page *page, int flags)
> >+int soft_offline_page(unsigned long pfn, int flags)
> >  {
> >  	int ret;
> >-	unsigned long pfn = page_to_pfn(page);
> >+	struct page *page;
> >+	if (!pfn_valid(pfn))
> >+		return -ENXIO;
> >+	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
> >+	page = pfn_to_online_page(pfn);
> >+	if (!page)
> >+		return -EIO;
> >  	if (is_zone_device_page(page)) {
> 
> -> this is now no longer possible! So you can drop the whole if
> (is_zone_device....) case

OK, thanks. I updated it.

Thanks,
Naoya Horiguchi
---
From 5faf227839b578726fe7f5ff414a153abb3b3a31 Mon Sep 17 00:00:00 2001
From: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Date: Thu, 17 Oct 2019 08:40:53 +0900
Subject: [PATCH] mm, soft-offline: convert parameter to pfn

Currently soft_offline_page() receives struct page, and its sibling
memory_failure() receives pfn. This discrepancy looks weird and makes
precheck on pfn validity tricky. So let's align them.

Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
---
 drivers/base/memory.c |  7 +------
 include/linux/mm.h    |  2 +-
 mm/madvise.c          |  2 +-
 mm/memory-failure.c   | 19 +++++++++----------
 4 files changed, 12 insertions(+), 18 deletions(-)

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index 55907c27075b..a757d9ed88a7 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -538,12 +538,7 @@ static ssize_t soft_offline_page_store(struct device *dev,
 	if (kstrtoull(buf, 0, &pfn) < 0)
 		return -EINVAL;
 	pfn >>= PAGE_SHIFT;
-	if (!pfn_valid(pfn))
-		return -ENXIO;
-	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
-	if (!pfn_to_online_page(pfn))
-		return -EIO;
-	ret = soft_offline_page(pfn_to_page(pfn), 0);
+	ret = soft_offline_page(pfn, 0);
 	return ret == 0 ? count : ret;
 }
 
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 44d058723db9..fd360d208346 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2794,7 +2794,7 @@ extern int sysctl_memory_failure_early_kill;
 extern int sysctl_memory_failure_recovery;
 extern void shake_page(struct page *p, int access);
 extern atomic_long_t num_poisoned_pages __read_mostly;
-extern int soft_offline_page(struct page *page, int flags);
+extern int soft_offline_page(unsigned long pfn, int flags);
 
 
 /*
diff --git a/mm/madvise.c b/mm/madvise.c
index 2be9f3fdb05e..99dd06fecfa9 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -887,7 +887,7 @@ static int madvise_inject_error(int behavior,
 			pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
 					pfn, start);
 
-			ret = soft_offline_page(page, MF_COUNT_INCREASED);
+			ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
 			if (ret)
 				return ret;
 			continue;
diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index 05c8c6df25e6..af2712004a4d 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -1476,7 +1476,7 @@ static void memory_failure_work_func(struct work_struct *work)
 		if (!gotten)
 			break;
 		if (entry.flags & MF_SOFT_OFFLINE)
-			soft_offline_page(pfn_to_page(entry.pfn), entry.flags);
+			soft_offline_page(entry.pfn, entry.flags);
 		else
 			memory_failure(entry.pfn, entry.flags);
 	}
@@ -1857,7 +1857,7 @@ static int soft_offline_free_page(struct page *page)
 
 /**
  * soft_offline_page - Soft offline a page.
- * @page: page to offline
+ * @pfn: pfn to soft-offline
  * @flags: flags. Same as memory_failure().
  *
  * Returns 0 on success, otherwise negated errno.
@@ -1877,18 +1877,17 @@ static int soft_offline_free_page(struct page *page)
  * This is not a 100% solution for all memory, but tries to be
  * ``good enough'' for the majority of memory.
  */
-int soft_offline_page(struct page *page, int flags)
+int soft_offline_page(unsigned long pfn, int flags)
 {
 	int ret;
-	unsigned long pfn = page_to_pfn(page);
+	struct page *page;
 
-	if (is_zone_device_page(page)) {
-		pr_debug_ratelimited("soft_offline: %#lx page is device page\n",
-				pfn);
-		if (flags & MF_COUNT_INCREASED)
-			put_page(page);
+	if (!pfn_valid(pfn))
+		return -ENXIO;
+	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
+	page = pfn_to_online_page(pfn);
+	if (!page)
 		return -EIO;
-	}
 
 	if (PageHWPoison(page)) {
 		pr_info("soft offline: %#lx page already poisoned\n", pfn);
David Hildenbrand Oct. 17, 2019, 7:16 a.m. UTC | #7
On 17.10.19 01:47, Naoya Horiguchi wrote:
> On Wed, Oct 16, 2019 at 10:57:57AM +0200, David Hildenbrand wrote:
>> On 16.10.19 10:54, Naoya Horiguchi wrote:
>>> On Wed, Oct 16, 2019 at 10:34:52AM +0200, David Hildenbrand wrote:
>>>> On 16.10.19 10:27, Naoya Horiguchi wrote:
>>>>> On Wed, Oct 16, 2019 at 09:56:19AM +0200, David Hildenbrand wrote:
>>>>>> On 16.10.19 09:09, Naoya Horiguchi wrote:
>>>>>>> Hi,
>>>>>>>
>>>>>>> I wrote a simple cleanup for parameter of soft_offline_page(),
>>>>>>> based on thread https://lkml.org/lkml/2019/10/11/57.
>>>>>>>
>>>>>>> I know that we need more cleanup on hwpoison-inject, but I think
>>>>>>> that will be mentioned in re-write patchset Oscar is preparing now.
>>>>>>> So let me shared only this part as a separate one now.
>>>>> ...
>>>>>>
>>>>>> I think you should rebase that patch on linux-next (where the
>>>>>> pfn_to_online_page() check is in place). I assume you'll want to move the
>>>>>> pfn_to_online_page() check into soft_offline_page() then as well?
>>>>>
>>>>> I rebased to next-20191016. And yes, we will move pfn_to_online_page()
>>>>> into soft offline code.  It seems that we can also move pfn_valid(),
>>>>> but is simply moving like below good enough for you?
>>>>
>>>> At least I can't am the patch to current next/master (due to
>>>> pfn_to_online_page()).
>>
>> Could also be that my "git am" skills failed as the mail was not a
>> proper patch itself :)
> 
> Sorry for the inconvenience, my company email system breaks original
> message by introducing quoted-printable format ('=20' or '=3D').
> Most mail client usually handles it but git-am doesn't.
> I give up using it and send via smtp.gmail.com.
> 
>>> @@ -1877,11 +1877,17 @@ static int soft_offline_free_page(struct page *page)
>>>    * This is not a 100% solution for all memory, but tries to be
>>>    * ``good enough'' for the majority of memory.
>>>    */
>>> -int soft_offline_page(struct page *page, int flags)
>>> +int soft_offline_page(unsigned long pfn, int flags)
>>>   {
>>>   	int ret;
>>> -	unsigned long pfn = page_to_pfn(page);
>>> +	struct page *page;
>>> +	if (!pfn_valid(pfn))
>>> +		return -ENXIO;
>>> +	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
>>> +	page = pfn_to_online_page(pfn);
>>> +	if (!page)
>>> +		return -EIO;
>>>   	if (is_zone_device_page(page)) {
>>
>> -> this is now no longer possible! So you can drop the whole if
>> (is_zone_device....) case
> 
> OK, thanks. I updated it.
> 
> Thanks,
> Naoya Horiguchi
> ---
>  From 5faf227839b578726fe7f5ff414a153abb3b3a31 Mon Sep 17 00:00:00 2001
> From: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
> Date: Thu, 17 Oct 2019 08:40:53 +0900
> Subject: [PATCH] mm, soft-offline: convert parameter to pfn
> 
> Currently soft_offline_page() receives struct page, and its sibling
> memory_failure() receives pfn. This discrepancy looks weird and makes
> precheck on pfn validity tricky. So let's align them.
> 
> Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
> ---
>   drivers/base/memory.c |  7 +------
>   include/linux/mm.h    |  2 +-
>   mm/madvise.c          |  2 +-
>   mm/memory-failure.c   | 19 +++++++++----------
>   4 files changed, 12 insertions(+), 18 deletions(-)
> 
> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> index 55907c27075b..a757d9ed88a7 100644
> --- a/drivers/base/memory.c
> +++ b/drivers/base/memory.c
> @@ -538,12 +538,7 @@ static ssize_t soft_offline_page_store(struct device *dev,
>   	if (kstrtoull(buf, 0, &pfn) < 0)
>   		return -EINVAL;
>   	pfn >>= PAGE_SHIFT;
> -	if (!pfn_valid(pfn))
> -		return -ENXIO;
> -	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
> -	if (!pfn_to_online_page(pfn))
> -		return -EIO;
> -	ret = soft_offline_page(pfn_to_page(pfn), 0);
> +	ret = soft_offline_page(pfn, 0);
>   	return ret == 0 ? count : ret;
>   }
>   
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 44d058723db9..fd360d208346 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -2794,7 +2794,7 @@ extern int sysctl_memory_failure_early_kill;
>   extern int sysctl_memory_failure_recovery;
>   extern void shake_page(struct page *p, int access);
>   extern atomic_long_t num_poisoned_pages __read_mostly;
> -extern int soft_offline_page(struct page *page, int flags);
> +extern int soft_offline_page(unsigned long pfn, int flags);
>   
>   
>   /*
> diff --git a/mm/madvise.c b/mm/madvise.c
> index 2be9f3fdb05e..99dd06fecfa9 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -887,7 +887,7 @@ static int madvise_inject_error(int behavior,
>   			pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
>   					pfn, start);
>   
> -			ret = soft_offline_page(page, MF_COUNT_INCREASED);
> +			ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
>   			if (ret)
>   				return ret;
>   			continue;
> diff --git a/mm/memory-failure.c b/mm/memory-failure.c
> index 05c8c6df25e6..af2712004a4d 100644
> --- a/mm/memory-failure.c
> +++ b/mm/memory-failure.c
> @@ -1476,7 +1476,7 @@ static void memory_failure_work_func(struct work_struct *work)
>   		if (!gotten)
>   			break;
>   		if (entry.flags & MF_SOFT_OFFLINE)
> -			soft_offline_page(pfn_to_page(entry.pfn), entry.flags);
> +			soft_offline_page(entry.pfn, entry.flags);
>   		else
>   			memory_failure(entry.pfn, entry.flags);
>   	}
> @@ -1857,7 +1857,7 @@ static int soft_offline_free_page(struct page *page)
>   
>   /**
>    * soft_offline_page - Soft offline a page.
> - * @page: page to offline
> + * @pfn: pfn to soft-offline
>    * @flags: flags. Same as memory_failure().
>    *
>    * Returns 0 on success, otherwise negated errno.
> @@ -1877,18 +1877,17 @@ static int soft_offline_free_page(struct page *page)
>    * This is not a 100% solution for all memory, but tries to be
>    * ``good enough'' for the majority of memory.
>    */
> -int soft_offline_page(struct page *page, int flags)
> +int soft_offline_page(unsigned long pfn, int flags)
>   {
>   	int ret;
> -	unsigned long pfn = page_to_pfn(page);
> +	struct page *page;
>   
> -	if (is_zone_device_page(page)) {
> -		pr_debug_ratelimited("soft_offline: %#lx page is device page\n",
> -				pfn);
> -		if (flags & MF_COUNT_INCREASED)
> -			put_page(page);
> +	if (!pfn_valid(pfn))
> +		return -ENXIO;
> +	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
> +	page = pfn_to_online_page(pfn);
> +	if (!page)

If you pass in a PFN with MF_COUNT_INCREASED via mm/madvise.c, you would  
now no longer do a put_page(page) in case of ZONE_DEVICE (!page =  
pfn_to_online_page(pfn);)

something like this

page = pfn_to_online_page(pfn);
if (!page) {
	/*
	 * With MF_COUNT_INCREASED, we can use pfn_to_page() directly
	 * (esp., ZONE_DEVICE).
	 */
	if (flags & MF_COUNT_INCREASED)
		put_page(pfn_to_page(page));
	return -EIO;
}

For !pfn_valid(pfn), this is not relevant.

>   		return -EIO;
> -	}
>   
>   	if (PageHWPoison(page)) {
>   		pr_info("soft offline: %#lx page already poisoned\n", pfn);
>
Naoya Horiguchi Oct. 17, 2019, 7:50 a.m. UTC | #8
On Thu, Oct 17, 2019 at 09:16:42AM +0200, David Hildenbrand wrote:
> On 17.10.19 01:47, Naoya Horiguchi wrote:
> > On Wed, Oct 16, 2019 at 10:57:57AM +0200, David Hildenbrand wrote:
> > > On 16.10.19 10:54, Naoya Horiguchi wrote:
> > > > On Wed, Oct 16, 2019 at 10:34:52AM +0200, David Hildenbrand wrote:
> > > > > On 16.10.19 10:27, Naoya Horiguchi wrote:
> > > > > > On Wed, Oct 16, 2019 at 09:56:19AM +0200, David Hildenbrand wrote:
> > > > > > > On 16.10.19 09:09, Naoya Horiguchi wrote:
> > > > > > > > Hi,
> > > > > > > > 
> > > > > > > > I wrote a simple cleanup for parameter of soft_offline_page(),
> > > > > > > > based on thread https://lkml.org/lkml/2019/10/11/57.
> > > > > > > > 
> > > > > > > > I know that we need more cleanup on hwpoison-inject, but I think
> > > > > > > > that will be mentioned in re-write patchset Oscar is preparing now.
> > > > > > > > So let me shared only this part as a separate one now.
> > > > > > ...
> > > > > > > 
> > > > > > > I think you should rebase that patch on linux-next (where the
> > > > > > > pfn_to_online_page() check is in place). I assume you'll want to move the
> > > > > > > pfn_to_online_page() check into soft_offline_page() then as well?
> > > > > > 
> > > > > > I rebased to next-20191016. And yes, we will move pfn_to_online_page()
> > > > > > into soft offline code.  It seems that we can also move pfn_valid(),
> > > > > > but is simply moving like below good enough for you?
> > > > > 
> > > > > At least I can't am the patch to current next/master (due to
> > > > > pfn_to_online_page()).
> > > 
> > > Could also be that my "git am" skills failed as the mail was not a
> > > proper patch itself :)
> > 
> > Sorry for the inconvenience, my company email system breaks original
> > message by introducing quoted-printable format ('=20' or '=3D').
> > Most mail client usually handles it but git-am doesn't.
> > I give up using it and send via smtp.gmail.com.
> > 
> > > > @@ -1877,11 +1877,17 @@ static int soft_offline_free_page(struct page *page)
> > > >    * This is not a 100% solution for all memory, but tries to be
> > > >    * ``good enough'' for the majority of memory.
> > > >    */
> > > > -int soft_offline_page(struct page *page, int flags)
> > > > +int soft_offline_page(unsigned long pfn, int flags)
> > > >   {
> > > >   	int ret;
> > > > -	unsigned long pfn = page_to_pfn(page);
> > > > +	struct page *page;
> > > > +	if (!pfn_valid(pfn))
> > > > +		return -ENXIO;
> > > > +	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
> > > > +	page = pfn_to_online_page(pfn);
> > > > +	if (!page)
> > > > +		return -EIO;
> > > >   	if (is_zone_device_page(page)) {
> > > 
> > > -> this is now no longer possible! So you can drop the whole if
> > > (is_zone_device....) case
> > 
> > OK, thanks. I updated it.
> > 
> > Thanks,
> > Naoya Horiguchi
> > ---
> >  From 5faf227839b578726fe7f5ff414a153abb3b3a31 Mon Sep 17 00:00:00 2001
> > From: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
> > Date: Thu, 17 Oct 2019 08:40:53 +0900
> > Subject: [PATCH] mm, soft-offline: convert parameter to pfn
> > 
> > Currently soft_offline_page() receives struct page, and its sibling
> > memory_failure() receives pfn. This discrepancy looks weird and makes
> > precheck on pfn validity tricky. So let's align them.
> > 
> > Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
> > ---
> >   drivers/base/memory.c |  7 +------
> >   include/linux/mm.h    |  2 +-
> >   mm/madvise.c          |  2 +-
> >   mm/memory-failure.c   | 19 +++++++++----------
> >   4 files changed, 12 insertions(+), 18 deletions(-)
> > 
> > diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> > index 55907c27075b..a757d9ed88a7 100644
> > --- a/drivers/base/memory.c
> > +++ b/drivers/base/memory.c
> > @@ -538,12 +538,7 @@ static ssize_t soft_offline_page_store(struct device *dev,
> >   	if (kstrtoull(buf, 0, &pfn) < 0)
> >   		return -EINVAL;
> >   	pfn >>= PAGE_SHIFT;
> > -	if (!pfn_valid(pfn))
> > -		return -ENXIO;
> > -	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
> > -	if (!pfn_to_online_page(pfn))
> > -		return -EIO;
> > -	ret = soft_offline_page(pfn_to_page(pfn), 0);
> > +	ret = soft_offline_page(pfn, 0);
> >   	return ret == 0 ? count : ret;
> >   }
> > diff --git a/include/linux/mm.h b/include/linux/mm.h
> > index 44d058723db9..fd360d208346 100644
> > --- a/include/linux/mm.h
> > +++ b/include/linux/mm.h
> > @@ -2794,7 +2794,7 @@ extern int sysctl_memory_failure_early_kill;
> >   extern int sysctl_memory_failure_recovery;
> >   extern void shake_page(struct page *p, int access);
> >   extern atomic_long_t num_poisoned_pages __read_mostly;
> > -extern int soft_offline_page(struct page *page, int flags);
> > +extern int soft_offline_page(unsigned long pfn, int flags);
> >   /*
> > diff --git a/mm/madvise.c b/mm/madvise.c
> > index 2be9f3fdb05e..99dd06fecfa9 100644
> > --- a/mm/madvise.c
> > +++ b/mm/madvise.c
> > @@ -887,7 +887,7 @@ static int madvise_inject_error(int behavior,
> >   			pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
> >   					pfn, start);
> > -			ret = soft_offline_page(page, MF_COUNT_INCREASED);
> > +			ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
> >   			if (ret)
> >   				return ret;
> >   			continue;
> > diff --git a/mm/memory-failure.c b/mm/memory-failure.c
> > index 05c8c6df25e6..af2712004a4d 100644
> > --- a/mm/memory-failure.c
> > +++ b/mm/memory-failure.c
> > @@ -1476,7 +1476,7 @@ static void memory_failure_work_func(struct work_struct *work)
> >   		if (!gotten)
> >   			break;
> >   		if (entry.flags & MF_SOFT_OFFLINE)
> > -			soft_offline_page(pfn_to_page(entry.pfn), entry.flags);
> > +			soft_offline_page(entry.pfn, entry.flags);
> >   		else
> >   			memory_failure(entry.pfn, entry.flags);
> >   	}
> > @@ -1857,7 +1857,7 @@ static int soft_offline_free_page(struct page *page)
> >   /**
> >    * soft_offline_page - Soft offline a page.
> > - * @page: page to offline
> > + * @pfn: pfn to soft-offline
> >    * @flags: flags. Same as memory_failure().
> >    *
> >    * Returns 0 on success, otherwise negated errno.
> > @@ -1877,18 +1877,17 @@ static int soft_offline_free_page(struct page *page)
> >    * This is not a 100% solution for all memory, but tries to be
> >    * ``good enough'' for the majority of memory.
> >    */
> > -int soft_offline_page(struct page *page, int flags)
> > +int soft_offline_page(unsigned long pfn, int flags)
> >   {
> >   	int ret;
> > -	unsigned long pfn = page_to_pfn(page);
> > +	struct page *page;
> > -	if (is_zone_device_page(page)) {
> > -		pr_debug_ratelimited("soft_offline: %#lx page is device page\n",
> > -				pfn);
> > -		if (flags & MF_COUNT_INCREASED)
> > -			put_page(page);
> > +	if (!pfn_valid(pfn))
> > +		return -ENXIO;
> > +	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
> > +	page = pfn_to_online_page(pfn);
> > +	if (!page)
> 
> If you pass in a PFN with MF_COUNT_INCREASED via mm/madvise.c, you would now
> no longer do a put_page(page) in case of ZONE_DEVICE (!page =
> pfn_to_online_page(pfn);)

Yes, right.

> 
> something like this
> 
> page = pfn_to_online_page(pfn);
> if (!page) {
> 	/*
> 	 * With MF_COUNT_INCREASED, we can use pfn_to_page() directly
> 	 * (esp., ZONE_DEVICE).
> 	 */
> 	if (flags & MF_COUNT_INCREASED)
> 		put_page(pfn_to_page(page));
> 	return -EIO;
> }
> 
> For !pfn_valid(pfn), this is not relevant.

Actually I guess that !pfn_valid() never happens when called from
madvise_inject_error(), because madvise_inject_error() gets pfn via
get_user_pages_fast() which only returns valid page for valid pfn.

And we plan to remove MF_COUNT_INCREASED by Oscar's re-design work,
so I start feeling that this patch should come on top of his tree.

Thanks,
Naoya Horiguchi
David Hildenbrand Oct. 17, 2019, 8:02 a.m. UTC | #9
On 17.10.19 09:50, Naoya Horiguchi wrote:
> On Thu, Oct 17, 2019 at 09:16:42AM +0200, David Hildenbrand wrote:
>> On 17.10.19 01:47, Naoya Horiguchi wrote:
>>> On Wed, Oct 16, 2019 at 10:57:57AM +0200, David Hildenbrand wrote:
>>>> On 16.10.19 10:54, Naoya Horiguchi wrote:
>>>>> On Wed, Oct 16, 2019 at 10:34:52AM +0200, David Hildenbrand wrote:
>>>>>> On 16.10.19 10:27, Naoya Horiguchi wrote:
>>>>>>> On Wed, Oct 16, 2019 at 09:56:19AM +0200, David Hildenbrand wrote:
>>>>>>>> On 16.10.19 09:09, Naoya Horiguchi wrote:
>>>>>>>>> Hi,
>>>>>>>>>
>>>>>>>>> I wrote a simple cleanup for parameter of soft_offline_page(),
>>>>>>>>> based on thread https://lkml.org/lkml/2019/10/11/57.
>>>>>>>>>
>>>>>>>>> I know that we need more cleanup on hwpoison-inject, but I think
>>>>>>>>> that will be mentioned in re-write patchset Oscar is preparing now.
>>>>>>>>> So let me shared only this part as a separate one now.
>>>>>>> ...
>>>>>>>>
>>>>>>>> I think you should rebase that patch on linux-next (where the
>>>>>>>> pfn_to_online_page() check is in place). I assume you'll want to move the
>>>>>>>> pfn_to_online_page() check into soft_offline_page() then as well?
>>>>>>>
>>>>>>> I rebased to next-20191016. And yes, we will move pfn_to_online_page()
>>>>>>> into soft offline code.  It seems that we can also move pfn_valid(),
>>>>>>> but is simply moving like below good enough for you?
>>>>>>
>>>>>> At least I can't am the patch to current next/master (due to
>>>>>> pfn_to_online_page()).
>>>>
>>>> Could also be that my "git am" skills failed as the mail was not a
>>>> proper patch itself :)
>>>
>>> Sorry for the inconvenience, my company email system breaks original
>>> message by introducing quoted-printable format ('=20' or '=3D').
>>> Most mail client usually handles it but git-am doesn't.
>>> I give up using it and send via smtp.gmail.com.
>>>
>>>>> @@ -1877,11 +1877,17 @@ static int soft_offline_free_page(struct page *page)
>>>>>     * This is not a 100% solution for all memory, but tries to be
>>>>>     * ``good enough'' for the majority of memory.
>>>>>     */
>>>>> -int soft_offline_page(struct page *page, int flags)
>>>>> +int soft_offline_page(unsigned long pfn, int flags)
>>>>>    {
>>>>>    	int ret;
>>>>> -	unsigned long pfn = page_to_pfn(page);
>>>>> +	struct page *page;
>>>>> +	if (!pfn_valid(pfn))
>>>>> +		return -ENXIO;
>>>>> +	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
>>>>> +	page = pfn_to_online_page(pfn);
>>>>> +	if (!page)
>>>>> +		return -EIO;
>>>>>    	if (is_zone_device_page(page)) {
>>>>
>>>> -> this is now no longer possible! So you can drop the whole if
>>>> (is_zone_device....) case
>>>
>>> OK, thanks. I updated it.
>>>
>>> Thanks,
>>> Naoya Horiguchi
>>> ---
>>>   From 5faf227839b578726fe7f5ff414a153abb3b3a31 Mon Sep 17 00:00:00 2001
>>> From: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
>>> Date: Thu, 17 Oct 2019 08:40:53 +0900
>>> Subject: [PATCH] mm, soft-offline: convert parameter to pfn
>>>
>>> Currently soft_offline_page() receives struct page, and its sibling
>>> memory_failure() receives pfn. This discrepancy looks weird and makes
>>> precheck on pfn validity tricky. So let's align them.
>>>
>>> Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
>>> ---
>>>    drivers/base/memory.c |  7 +------
>>>    include/linux/mm.h    |  2 +-
>>>    mm/madvise.c          |  2 +-
>>>    mm/memory-failure.c   | 19 +++++++++----------
>>>    4 files changed, 12 insertions(+), 18 deletions(-)
>>>
>>> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
>>> index 55907c27075b..a757d9ed88a7 100644
>>> --- a/drivers/base/memory.c
>>> +++ b/drivers/base/memory.c
>>> @@ -538,12 +538,7 @@ static ssize_t soft_offline_page_store(struct device *dev,
>>>    	if (kstrtoull(buf, 0, &pfn) < 0)
>>>    		return -EINVAL;
>>>    	pfn >>= PAGE_SHIFT;
>>> -	if (!pfn_valid(pfn))
>>> -		return -ENXIO;
>>> -	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
>>> -	if (!pfn_to_online_page(pfn))
>>> -		return -EIO;
>>> -	ret = soft_offline_page(pfn_to_page(pfn), 0);
>>> +	ret = soft_offline_page(pfn, 0);
>>>    	return ret == 0 ? count : ret;
>>>    }
>>> diff --git a/include/linux/mm.h b/include/linux/mm.h
>>> index 44d058723db9..fd360d208346 100644
>>> --- a/include/linux/mm.h
>>> +++ b/include/linux/mm.h
>>> @@ -2794,7 +2794,7 @@ extern int sysctl_memory_failure_early_kill;
>>>    extern int sysctl_memory_failure_recovery;
>>>    extern void shake_page(struct page *p, int access);
>>>    extern atomic_long_t num_poisoned_pages __read_mostly;
>>> -extern int soft_offline_page(struct page *page, int flags);
>>> +extern int soft_offline_page(unsigned long pfn, int flags);
>>>    /*
>>> diff --git a/mm/madvise.c b/mm/madvise.c
>>> index 2be9f3fdb05e..99dd06fecfa9 100644
>>> --- a/mm/madvise.c
>>> +++ b/mm/madvise.c
>>> @@ -887,7 +887,7 @@ static int madvise_inject_error(int behavior,
>>>    			pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
>>>    					pfn, start);
>>> -			ret = soft_offline_page(page, MF_COUNT_INCREASED);
>>> +			ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
>>>    			if (ret)
>>>    				return ret;
>>>    			continue;
>>> diff --git a/mm/memory-failure.c b/mm/memory-failure.c
>>> index 05c8c6df25e6..af2712004a4d 100644
>>> --- a/mm/memory-failure.c
>>> +++ b/mm/memory-failure.c
>>> @@ -1476,7 +1476,7 @@ static void memory_failure_work_func(struct work_struct *work)
>>>    		if (!gotten)
>>>    			break;
>>>    		if (entry.flags & MF_SOFT_OFFLINE)
>>> -			soft_offline_page(pfn_to_page(entry.pfn), entry.flags);
>>> +			soft_offline_page(entry.pfn, entry.flags);
>>>    		else
>>>    			memory_failure(entry.pfn, entry.flags);
>>>    	}
>>> @@ -1857,7 +1857,7 @@ static int soft_offline_free_page(struct page *page)
>>>    /**
>>>     * soft_offline_page - Soft offline a page.
>>> - * @page: page to offline
>>> + * @pfn: pfn to soft-offline
>>>     * @flags: flags. Same as memory_failure().
>>>     *
>>>     * Returns 0 on success, otherwise negated errno.
>>> @@ -1877,18 +1877,17 @@ static int soft_offline_free_page(struct page *page)
>>>     * This is not a 100% solution for all memory, but tries to be
>>>     * ``good enough'' for the majority of memory.
>>>     */
>>> -int soft_offline_page(struct page *page, int flags)
>>> +int soft_offline_page(unsigned long pfn, int flags)
>>>    {
>>>    	int ret;
>>> -	unsigned long pfn = page_to_pfn(page);
>>> +	struct page *page;
>>> -	if (is_zone_device_page(page)) {
>>> -		pr_debug_ratelimited("soft_offline: %#lx page is device page\n",
>>> -				pfn);
>>> -		if (flags & MF_COUNT_INCREASED)
>>> -			put_page(page);
>>> +	if (!pfn_valid(pfn))
>>> +		return -ENXIO;
>>> +	/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
>>> +	page = pfn_to_online_page(pfn);
>>> +	if (!page)
>>
>> If you pass in a PFN with MF_COUNT_INCREASED via mm/madvise.c, you would now
>> no longer do a put_page(page) in case of ZONE_DEVICE (!page =
>> pfn_to_online_page(pfn);)
> 
> Yes, right.
> 
>>
>> something like this
>>
>> page = pfn_to_online_page(pfn);
>> if (!page) {
>> 	/*
>> 	 * With MF_COUNT_INCREASED, we can use pfn_to_page() directly
>> 	 * (esp., ZONE_DEVICE).
>> 	 */
>> 	if (flags & MF_COUNT_INCREASED)
>> 		put_page(pfn_to_page(page));
>> 	return -EIO;
>> }
>>
>> For !pfn_valid(pfn), this is not relevant.
> 
> Actually I guess that !pfn_valid() never happens when called from
> madvise_inject_error(), because madvise_inject_error() gets pfn via
> get_user_pages_fast() which only returns valid page for valid pfn.
> 
> And we plan to remove MF_COUNT_INCREASED by Oscar's re-design work,
> so I start feeling that this patch should come on top of his tree.

I agree, let's clean that up first.
Oscar Salvador Oct. 17, 2019, 8:03 a.m. UTC | #10
On Thu, Oct 17, 2019 at 07:50:18AM +0000, Naoya Horiguchi wrote:
> Actually I guess that !pfn_valid() never happens when called from
> madvise_inject_error(), because madvise_inject_error() gets pfn via
> get_user_pages_fast() which only returns valid page for valid pfn.
> 
> And we plan to remove MF_COUNT_INCREASED by Oscar's re-design work,
> so I start feeling that this patch should come on top of his tree.

Hi Naoya,

I am pretty much done with my testing.
If you feel like, I can take the patch and add it on top of [1]
, then I will do some more testing and, if nothing pops up, I will
send it upstream.

[1] https://github.com/leberus/linux/tree/hwpoison-v2

> 
> Thanks,
> Naoya Horiguchi
Naoya Horiguchi Oct. 17, 2019, 8:07 a.m. UTC | #11
On Thu, Oct 17, 2019 at 10:03:21AM +0200, Oscar Salvador wrote:
> On Thu, Oct 17, 2019 at 07:50:18AM +0000, Naoya Horiguchi wrote:
> > Actually I guess that !pfn_valid() never happens when called from
> > madvise_inject_error(), because madvise_inject_error() gets pfn via
> > get_user_pages_fast() which only returns valid page for valid pfn.
> > 
> > And we plan to remove MF_COUNT_INCREASED by Oscar's re-design work,
> > so I start feeling that this patch should come on top of his tree.
> 
> Hi Naoya,
> 
> I am pretty much done with my testing.
> If you feel like, I can take the patch and add it on top of [1]
> , then I will do some more testing and, if nothing pops up, I will
> send it upstream.

Hi Oscar,
Yes, please take it, thank you for speaking up.

> 
> [1] https://github.com/leberus/linux/tree/hwpoison-v2
diff mbox series

Patch

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index e5485c22ef77..04e469c82852 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -540,7 +540,7 @@  static ssize_t soft_offline_page_store(struct device *dev,
 	pfn >>= PAGE_SHIFT;
 	if (!pfn_valid(pfn))
 		return -ENXIO;
-	ret = soft_offline_page(pfn_to_page(pfn));
+	ret = soft_offline_page(pfn);
 	return ret == 0 ? count : ret;
 }
 
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 3eba26324ff1..0a452020edf5 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2783,7 +2783,7 @@  extern int sysctl_memory_failure_early_kill;
 extern int sysctl_memory_failure_recovery;
 extern void shake_page(struct page *p, int access);
 extern atomic_long_t num_poisoned_pages __read_mostly;
-extern int soft_offline_page(struct page *page);
+extern int soft_offline_page(unsigned long pfn);
 
 
 /*
diff --git a/mm/madvise.c b/mm/madvise.c
index fd221b610b52..df198d1e5e2e 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -893,7 +893,7 @@  static int madvise_inject_error(int behavior,
 		if (behavior == MADV_SOFT_OFFLINE) {
 			pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
 				 pfn, start);
-			ret = soft_offline_page(page);
+			ret = soft_offline_page(pfn);
 			if (ret)
 				return ret;
 		} else {
diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index 4f16e0a7e7cc..eb4fd5e8d5e1 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -1514,7 +1514,7 @@  static void memory_failure_work_func(struct work_struct *work)
 		if (!gotten)
 			break;
 		if (entry.flags & MF_SOFT_OFFLINE)
-			soft_offline_page(pfn_to_page(entry.pfn));
+			soft_offline_page(entry.pfn);
 		else
 			memory_failure(entry.pfn, entry.flags);
 	}
@@ -1822,7 +1822,7 @@  static int soft_offline_free_page(struct page *page)
 
 /**
  * soft_offline_page - Soft offline a page.
- * @page: page to offline
+ * @pfn: pfn to soft-offline
  *
  * Returns 0 on success, otherwise negated errno.
  *
@@ -1841,10 +1841,10 @@  static int soft_offline_free_page(struct page *page)
  * This is not a 100% solution for all memory, but tries to be
  * ``good enough'' for the majority of memory.
  */
-int soft_offline_page(struct page *page)
+int soft_offline_page(unsigned long pfn)
 {
 	int ret;
-	unsigned long pfn = page_to_pfn(page);
+	struct page *page = pfn_to_page(pfn);
 
 	if (is_zone_device_page(page)) {
 		pr_debug_ratelimited("soft_offline: %#lx page is device page\n",