From: David Hildenbrand <david@redhat.com>
To: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org,
David Hildenbrand <david@redhat.com>,
Andrey Ryabinin <aryabinin@virtuozzo.com>,
Alexander Potapenko <glider@google.com>,
Dmitry Vyukov <dvyukov@google.com>,
kasan-dev@googlegroups.com
Subject: [PATCH v1 03/10] kasan: prepare for online/offline of different start/size
Date: Wed, 23 May 2018 17:11:44 +0200 [thread overview]
Message-ID: <20180523151151.6730-4-david@redhat.com> (raw)
In-Reply-To: <20180523151151.6730-1-david@redhat.com>
The memory notifier has an important restriction right now: it only
works if offline_pages() is called with the same parameters as
online_pages().
To overcome this restriction, let's handle it per section. We could do
it in smaller granularity, but then we get more vm_area overhead and
cannot check that cleanly for actual online parts.
A section is marked online as soon as at least one page is online.
Similarly, a section is marked offline as soon as all pages are offline.
So handling it on a per-section basis allows us to be more flexible. We
asssume here, that a section is not split between boot and hotplug
memory.
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: kasan-dev@googlegroups.com
Signed-off-by: David Hildenbrand <david@redhat.com>
---
mm/kasan/kasan.c | 107 ++++++++++++++++++++++++++++++-----------------
1 file changed, 69 insertions(+), 38 deletions(-)
diff --git a/mm/kasan/kasan.c b/mm/kasan/kasan.c
index a8b85706e2d6..901601a562a9 100644
--- a/mm/kasan/kasan.c
+++ b/mm/kasan/kasan.c
@@ -827,62 +827,93 @@ static bool shadow_mapped(unsigned long addr)
return !pte_none(*pte);
}
-static int __meminit kasan_mem_notifier(struct notifier_block *nb,
- unsigned long action, void *data)
+static void kasan_offline_pages(unsigned long start_pfn, unsigned long nr_pages)
{
- struct memory_notify *mem_data = data;
- unsigned long nr_shadow_pages, start_kaddr, shadow_start;
- unsigned long shadow_end, shadow_size;
+ unsigned long start = SECTION_ALIGN_DOWN(start_pfn);
+ unsigned long end = SECTION_ALIGN_UP(start_pfn + nr_pages);
+ unsigned long pfn;
- nr_shadow_pages = mem_data->nr_pages >> KASAN_SHADOW_SCALE_SHIFT;
- start_kaddr = (unsigned long)pfn_to_kaddr(mem_data->start_pfn);
- shadow_start = (unsigned long)kasan_mem_to_shadow((void *)start_kaddr);
- shadow_size = nr_shadow_pages << PAGE_SHIFT;
- shadow_end = shadow_start + shadow_size;
+ for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
+ void *addr, *shadow_start;
+ struct vm_struct *vm;
- if (WARN_ON(mem_data->nr_pages % KASAN_SHADOW_SCALE_SIZE) ||
- WARN_ON(start_kaddr % (KASAN_SHADOW_SCALE_SIZE << PAGE_SHIFT)))
- return NOTIFY_BAD;
+ /* still online? nothing to do then */
+ if (online_section_nr(pfn_to_section_nr(pfn)))
+ continue;
- switch (action) {
- case MEM_GOING_ONLINE: {
- void *ret;
+ addr = pfn_to_kaddr(pfn);
+ shadow_start = kasan_mem_to_shadow(addr);
+
+ /*
+ * Only hot-added memory has a vm_area. Freeing shadow mapped
+ * during boot would be tricky, so we'll just have to keep it.
+ */
+ vm = find_vm_area(shadow_start);
+ if (vm)
+ vfree(shadow_start);
+ }
+}
+
+static int kasan_online_pages(unsigned long start_pfn, unsigned long nr_pages)
+{
+ unsigned long start = SECTION_ALIGN_DOWN(start_pfn);
+ unsigned long end = SECTION_ALIGN_UP(start_pfn + nr_pages);
+ unsigned long pfn;
+
+ for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
+ unsigned long shadow_start, shadow_size;
+ void *addr, *ret;
+
+ /* already online? nothing to do then */
+ if (online_section_nr(pfn_to_section_nr(pfn)))
+ continue;
+
+ addr = pfn_to_kaddr(pfn);
+ shadow_size = (PAGES_PER_SECTION << PAGE_SHIFT) >>
+ KASAN_SHADOW_SCALE_SHIFT;
+ shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
/*
* If shadow is mapped already than it must have been mapped
- * during the boot. This could happen if we onlining previously
+ * during boot. This could happen if we're onlining previously
* offlined memory.
*/
if (shadow_mapped(shadow_start))
- return NOTIFY_OK;
+ continue;
ret = __vmalloc_node_range(shadow_size, PAGE_SIZE, shadow_start,
- shadow_end, GFP_KERNEL,
- PAGE_KERNEL, VM_NO_GUARD,
- pfn_to_nid(mem_data->start_pfn),
- __builtin_return_address(0));
+ shadow_start + shadow_size,
+ GFP_KERNEL, PAGE_KERNEL, VM_NO_GUARD,
+ pfn_to_nid(pfn),
+ __builtin_return_address(0));
if (!ret)
- return NOTIFY_BAD;
-
+ goto out_free;
kmemleak_ignore(ret);
- return NOTIFY_OK;
}
- case MEM_CANCEL_ONLINE:
- case MEM_OFFLINE: {
- struct vm_struct *vm;
+ return 0;
+out_free:
+ kasan_offline_pages(start_pfn, nr_pages);
+ return -ENOMEM;
+}
- /*
- * Only hot-added memory have vm_area. Freeing shadow
- * mapped during boot would be tricky, so we'll just
- * have to keep it.
- */
- vm = find_vm_area((void *)shadow_start);
- if (vm)
- vfree((void *)shadow_start);
- }
+static int __meminit kasan_mem_notifier(struct notifier_block *nb,
+ unsigned long action, void *data)
+{
+ struct memory_notify *mem_data = data;
+ int ret = 0;
+
+ switch (action) {
+ case MEM_GOING_ONLINE:
+ ret = kasan_online_pages(mem_data->start_pfn,
+ mem_data->nr_pages);
+ break;
+ case MEM_CANCEL_ONLINE:
+ case MEM_OFFLINE:
+ kasan_offline_pages(mem_data->start_pfn, mem_data->nr_pages);
+ break;
}
- return NOTIFY_OK;
+ return notifier_from_errno(ret);
}
static int __init kasan_memhotplug_init(void)
--
2.17.0
next prev parent reply other threads:[~2018-05-23 15:12 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-05-23 15:11 [PATCH v1 00/10] mm: online/offline 4MB chunks controlled by device driver David Hildenbrand
2018-05-23 15:11 ` [PATCH v1 01/10] mm: introduce and use PageOffline() David Hildenbrand
2018-05-23 15:11 ` [PATCH v1 02/10] mm/page_ext.c: support online/offline of memory < section size David Hildenbrand
2018-05-23 15:11 ` David Hildenbrand [this message]
2018-05-23 15:11 ` [PATCH v1 04/10] kdump: include PAGE_OFFLINE_MAPCOUNT_VALUE in VMCOREINFO David Hildenbrand
2018-05-23 15:11 ` [PATCH v1 05/10] mm/memory_hotplug: limit offline_pages() to sizes we can actually handle David Hildenbrand
2018-05-23 15:11 ` [PATCH v1 06/10] mm/memory_hotplug: onlining pages can only fail due to notifiers David Hildenbrand
2018-05-23 15:11 ` [PATCH v1 07/10] mm/memory_hotplug: print only with DEBUG_VM in online/offline_pages() David Hildenbrand
2018-05-23 15:11 ` [PATCH v1 08/10] mm/memory_hotplug: allow to control onlining/offlining of memory by a driver David Hildenbrand
2018-05-23 15:11 ` [PATCH v1 09/10] mm/memory_hotplug: teach offline_pages() to not try forever David Hildenbrand
2018-05-24 14:39 ` Michal Hocko
2018-05-24 20:36 ` David Hildenbrand
2018-05-23 15:11 ` [PATCH v1 10/10] mm/memory_hotplug: allow online/offline memory by a kernel module David Hildenbrand
2018-05-23 19:51 ` Christoph Hellwig
2018-05-24 5:59 ` David Hildenbrand
2018-05-24 7:53 ` [PATCH v1 00/10] mm: online/offline 4MB chunks controlled by device driver Michal Hocko
2018-05-24 8:31 ` David Hildenbrand
2018-05-24 8:56 ` Dave Young
2018-05-24 9:14 ` David Hildenbrand
2018-05-28 8:28 ` Dave Young
2018-05-28 10:03 ` David Hildenbrand
2018-05-24 9:31 ` Michal Hocko
2018-05-24 10:45 ` David Hildenbrand
2018-05-24 12:03 ` Michal Hocko
2018-05-24 14:04 ` David Hildenbrand
2018-05-24 14:22 ` Michal Hocko
2018-05-24 21:07 ` David Hildenbrand
2018-06-11 11:53 ` David Hildenbrand
2018-06-11 11:56 ` Michal Hocko
2018-06-11 12:33 ` David Hildenbrand
2018-07-16 19:48 ` David Hildenbrand
2018-07-16 20:05 ` Michal Hocko
2018-07-18 9:56 ` David Hildenbrand
2018-07-18 11:23 ` Michal Hocko
2018-07-18 13:19 ` Michal Hocko
2018-07-18 13:39 ` David Hildenbrand
2018-07-18 13:43 ` Michal Hocko
2018-07-18 13:47 ` David Hildenbrand
2018-07-18 13:56 ` Michal Hocko
2018-05-25 15:08 ` David Hildenbrand
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180523151151.6730-4-david@redhat.com \
--to=david@redhat.com \
--cc=aryabinin@virtuozzo.com \
--cc=dvyukov@google.com \
--cc=glider@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).