diff mbox series

[v1,1/5] s390/uv: don't call wait_on_page_writeback() without a reference

Message ID 20240404163642.1125529-2-david@redhat.com (mailing list archive)
State New
Headers show
Series s390: page_mapcount(), page_has_private() and PG_arch_1 | expand

Commit Message

David Hildenbrand April 4, 2024, 4:36 p.m. UTC
wait_on_page_writeback() requires that no spinlocks are held and that
a page reference is held, as documented for folio_wait_writeback(). After
we dropped the PTL, the page could get freed concurrently. So grab a
temporary reference.

Fixes: 214d9bbcd3a6 ("s390/mm: provide memory management functions for protected KVM guests")
Signed-off-by: David Hildenbrand <david@redhat.com>
---
 arch/s390/kernel/uv.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Comments

Claudio Imbrenda April 10, 2024, 5:21 p.m. UTC | #1
On Thu,  4 Apr 2024 18:36:38 +0200
David Hildenbrand <david@redhat.com> wrote:

> wait_on_page_writeback() requires that no spinlocks are held and that
> a page reference is held, as documented for folio_wait_writeback(). After

oops

> we dropped the PTL, the page could get freed concurrently. So grab a
> temporary reference.
> 
> Fixes: 214d9bbcd3a6 ("s390/mm: provide memory management functions for protected KVM guests")
> Signed-off-by: David Hildenbrand <david@redhat.com>

Reviewed-by: Claudio Imbrenda <imbrenda@linux.ibm.com>

> ---
>  arch/s390/kernel/uv.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/arch/s390/kernel/uv.c b/arch/s390/kernel/uv.c
> index fc07bc39e698..7401838b960b 100644
> --- a/arch/s390/kernel/uv.c
> +++ b/arch/s390/kernel/uv.c
> @@ -314,6 +314,13 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
>  			rc = make_page_secure(page, uvcb);
>  			unlock_page(page);
>  		}
> +
> +		/*
> +		 * Once we drop the PTL, the page may get unmapped and
> +		 * freed immediately. We need a temporary reference.
> +		 */
> +		if (rc == -EAGAIN)
> +			get_page(page);
>  	}
>  	pte_unmap_unlock(ptep, ptelock);
>  out:
> @@ -325,6 +332,7 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
>  		 * completion, this is just a useless check, but it is safe.
>  		 */
>  		wait_on_page_writeback(page);
> +		put_page(page);
>  	} else if (rc == -EBUSY) {
>  		/*
>  		 * If we have tried a local drain and the page refcount
David Hildenbrand April 11, 2024, 8:24 a.m. UTC | #2
On 10.04.24 19:21, Claudio Imbrenda wrote:
> Reviewed-by: Claudio Imbrenda<imbrenda@linux.ibm.com>

Thanks! I'll rebase this series on top of the s390/features tree for 
now, where Willy's cleanups already reside.

If maintainers want to have that fix first, I can send it out with 
Willy's patches rebased on this fix. Whatever people prefer.
Alexander Gordeev April 11, 2024, 1:13 p.m. UTC | #3
On Thu, Apr 11, 2024 at 10:24:23AM +0200, David Hildenbrand wrote:
> Thanks! I'll rebase this series on top of the s390/features tree for now,
> where Willy's cleanups already reside.

Yes, rebase it on to of s390/features, please.

> If maintainers want to have that fix first, I can send it out with Willy's
> patches rebased on this fix. Whatever people prefer.

Many thanks!

> -- 
> Cheers,
> 
> David / dhildenb
diff mbox series

Patch

diff --git a/arch/s390/kernel/uv.c b/arch/s390/kernel/uv.c
index fc07bc39e698..7401838b960b 100644
--- a/arch/s390/kernel/uv.c
+++ b/arch/s390/kernel/uv.c
@@ -314,6 +314,13 @@  int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
 			rc = make_page_secure(page, uvcb);
 			unlock_page(page);
 		}
+
+		/*
+		 * Once we drop the PTL, the page may get unmapped and
+		 * freed immediately. We need a temporary reference.
+		 */
+		if (rc == -EAGAIN)
+			get_page(page);
 	}
 	pte_unmap_unlock(ptep, ptelock);
 out:
@@ -325,6 +332,7 @@  int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
 		 * completion, this is just a useless check, but it is safe.
 		 */
 		wait_on_page_writeback(page);
+		put_page(page);
 	} else if (rc == -EBUSY) {
 		/*
 		 * If we have tried a local drain and the page refcount