# \[patch 058/192\] virtio-mem: use page\_offline\_(start\|end) when setting PageOffline()

- From: Andrew Morton \<akpm@linux-foundation.org\>
- Date: 2021-07-01 01:50:21 
- List: linux-mm
- Page: https://ratatoskr.run/linux-mm/2021/07/6509451
- Thread: https://ratatoskr.run/linux-mm/2021/07/6509393/t.md

```text
From: David Hildenbrand <redacted>
Subject: virtio-mem: use page_offline_(start|end) when setting PageOffline()

Let's properly use page_offline_(start|end) to synchronize setting
PageOffline(), so we won't have valid page access to unplugged memory
regions from /proc/kcore.

Existing balloon implementations usually allow reading inflated memory;
doing so might result in unnecessary overhead in the hypervisor, which is
currently the case with virtio-mem.

For future virtio-mem use cases, it will be different when using shmem,
huge pages, !anonymous private mappings, ...  as backing storage for a VM.
virtio-mem unplugged memory must no longer be accessed and access might
result in undefined behavior.  There will be a virtio spec extension to
document this change, including a new feature flag indicating the changed
behavior.  We really don't want to race against PFN walkers reading random
page content.

Link: https://lkml.kernel.org/r/20210526093041.8800-6-david@redhat.com
Signed-off-by: David Hildenbrand <redacted>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Mike Rapoport <redacted>
Reviewed-by: Oscar Salvador <osalvador@suse.de>
Cc: Aili Yao <redacted>
Cc: Alexey Dobriyan <redacted>
Cc: Alex Shi <redacted>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Cc: Jason Wang <redacted>
Cc: Jiri Bohac <redacted>
Cc: "K. Y. Srinivasan" <redacted>
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Kravetz <redacted>
Cc: Naoya Horiguchi <redacted>
Cc: Roman Gushchin <redacted>
Cc: Stephen Hemminger <redacted>
Cc: Steven Price <steven.price@arm.com>
Cc: Wei Liu <wei.liu@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 drivers/virtio/virtio_mem.c |    2 ++
 1 file changed, 2 insertions(+)

--- a/drivers/virtio/virtio_mem.c~virtio-mem-use-page_offline_startend-when-setting-pageoffline
+++ a/drivers/virtio/virtio_mem.c
@@ -1065,6 +1065,7 @@ static int virtio_mem_memory_notifier_cb
 static void virtio_mem_set_fake_offline(unsigned long pfn,
 					unsigned long nr_pages, bool onlined)
 {
+	page_offline_begin();
 	for (; nr_pages--; pfn++) {
 		struct page *page = pfn_to_page(pfn);
 
@@ -1075,6 +1076,7 @@ static void virtio_mem_set_fake_offline(
 			ClearPageReserved(page);
 		}
 	}
+	page_offline_end();
 }
 
 /*
_

```
