linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/madvise: elevate page refcount while isolating in process_madvise()
@ 2021-12-15 12:23 Charan Teja Reddy
  2021-12-15 13:55 ` Matthew Wilcox
  0 siblings, 1 reply; 3+ messages in thread
From: Charan Teja Reddy @ 2021-12-15 12:23 UTC (permalink / raw)
  To: akpm, minchan, surenb, vbabka, rientjes
  Cc: linux-mm, linux-kernel, Charan Teja Reddy

The documentation of isolate_lru_page() says that, "it must be called
with an elevated refcount on the page", which is not followed while
isolating pages in process_madvise() system call with advise
MADV_PAGEOUT. Fix it.

Signed-off-by: Charan Teja Reddy <quic_charante@quicinc.com>
---
 mm/madvise.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/mm/madvise.c b/mm/madvise.c
index 0734db8..4c4a8e9 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -317,6 +317,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
 	pte_t *orig_pte, *pte, ptent;
 	spinlock_t *ptl;
 	struct page *page = NULL;
+	int ret;
 	LIST_HEAD(page_list);
 
 	if (fatal_signal_pending(current))
@@ -373,12 +374,15 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
 		ClearPageReferenced(page);
 		test_and_clear_page_young(page);
 		if (pageout) {
-			if (!isolate_lru_page(page)) {
+			get_page(page);
+			ret = isolate_lru_page(page);
+			if (!ret) {
 				if (PageUnevictable(page))
 					putback_lru_page(page);
 				else
 					list_add(&page->lru, &page_list);
 			}
+			put_page(page);
 		} else
 			deactivate_page(page);
 huge_unlock:
@@ -459,12 +463,15 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
 		ClearPageReferenced(page);
 		test_and_clear_page_young(page);
 		if (pageout) {
-			if (!isolate_lru_page(page)) {
+			get_page(page);
+			ret = isolate_lru_page(page);
+			if (!ret) {
 				if (PageUnevictable(page))
 					putback_lru_page(page);
 				else
 					list_add(&page->lru, &page_list);
 			}
+			put_page(page);
 		} else
 			deactivate_page(page);
 	}
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/madvise: elevate page refcount while isolating in process_madvise()
  2021-12-15 12:23 [PATCH] mm/madvise: elevate page refcount while isolating in process_madvise() Charan Teja Reddy
@ 2021-12-15 13:55 ` Matthew Wilcox
  2021-12-15 15:39   ` Charan Teja Kalla
  0 siblings, 1 reply; 3+ messages in thread
From: Matthew Wilcox @ 2021-12-15 13:55 UTC (permalink / raw)
  To: Charan Teja Reddy
  Cc: akpm, minchan, surenb, vbabka, rientjes, linux-mm, linux-kernel

On Wed, Dec 15, 2021 at 05:53:39PM +0530, Charan Teja Reddy wrote:
> The documentation of isolate_lru_page() says that, "it must be called
> with an elevated refcount on the page", which is not followed while
> isolating pages in process_madvise() system call with advise
> MADV_PAGEOUT. Fix it.

We hold the mmap_lock over the call to this function, so the reference
to the page from the page tables cannot go away.  There's no need to
grab an extra reference here.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/madvise: elevate page refcount while isolating in process_madvise()
  2021-12-15 13:55 ` Matthew Wilcox
@ 2021-12-15 15:39   ` Charan Teja Kalla
  0 siblings, 0 replies; 3+ messages in thread
From: Charan Teja Kalla @ 2021-12-15 15:39 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: akpm, minchan, surenb, vbabka, rientjes, linux-mm, linux-kernel


On 12/15/2021 7:25 PM, Matthew Wilcox wrote:
> On Wed, Dec 15, 2021 at 05:53:39PM +0530, Charan Teja Reddy wrote:
>> The documentation of isolate_lru_page() says that, "it must be called
>> with an elevated refcount on the page", which is not followed while
>> isolating pages in process_madvise() system call with advise
>> MADV_PAGEOUT. Fix it.
> 
> We hold the mmap_lock over the call to this function, so the reference
> to the page from the page tables cannot go away.  There's no need to
> grab an extra reference here.

Thanks Matthew for clearing such fundamentals.

> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-12-15 16:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-15 12:23 [PATCH] mm/madvise: elevate page refcount while isolating in process_madvise() Charan Teja Reddy
2021-12-15 13:55 ` Matthew Wilcox
2021-12-15 15:39   ` Charan Teja Kalla

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).