From: Mike Rapoport <rppt@kernel.org>
To: Pratyush Yadav <pratyush@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Alexander Graf <graf@amazon.com>, Baoquan He <bhe@redhat.com>,
Changyuan Lyu <changyuanl@google.com>,
Chris Li <chrisl@kernel.org>, Jason Gunthorpe <jgg@nvidia.com>,
Pasha Tatashin <pasha.tatashin@soleen.com>,
kexec@lists.infradead.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 1/2] kho: add support for preserving vmalloc allocations
Date: Mon, 15 Sep 2025 17:08:00 +0300 [thread overview]
Message-ID: <aMgdwE83dKdT1K7L@kernel.org> (raw)
In-Reply-To: <mafs0ldmon784.fsf@kernel.org>
On Mon, Sep 08, 2025 at 08:12:59PM +0200, Pratyush Yadav wrote:
> On Mon, Sep 08 2025, Mike Rapoport wrote:
>
> > From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
> >
> > A vmalloc allocation is preserved using binary structure similar to
> > global KHO memory tracker. It's a linked list of pages where each page
> > is an array of physical address of pages in vmalloc area.
> >
> > kho_preserve_vmalloc() hands out the physical address of the head page
> > to the caller. This address is used as the argument to
> > kho_vmalloc_restore() to restore the mapping in the vmalloc address
> > space and populate it with the preserved pages.
> >
> > Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
...
> > @@ -742,6 +743,205 @@ int kho_preserve_phys(phys_addr_t phys, size_t size)
> > }
> > EXPORT_SYMBOL_GPL(kho_preserve_phys);
> >
> > +struct kho_vmalloc_chunk;
> > +
> > +struct kho_vmalloc_hdr {
> > + DECLARE_KHOSER_PTR(next, struct kho_vmalloc_chunk *);
> > + unsigned int total_pages; /* only valid in the first chunk */
> > + unsigned int flags; /* only valid in the first chunk */
> > + unsigned short order; /* only valid in the first chunk */
> > + unsigned short num_elms;
>
> I think it the serialization format would be cleaner if these were
> defined in a separate structure that holds the metadata instead of being
> defined in each page and then ignored in most of them.
>
> If the caller can save 8 bytes (phys addr of first page), it might as
> well save 16 instead. Something like the below perhaps?
>
> struct kho_vmalloc {
> DECLARE_KHOSER_PTR(first, struct kho_vmalloc_chunk *);
> unsigned int total_pages;
> unsigned short flags;
> unsigned short order;
> };
>
> And then kho_vmalloc_hdr becomes simply:
>
> struct kho_vmalloc_hdr {
> DECLARE_KHOSER_PTR(next, struct kho_vmalloc_chunk *);
> };
>
> You don't even need num_elms since you have the list be zero-terminated.
Agree, thanks.
> > +#define KHO_VMALLOC_FLAGS_MASK (VM_ALLOC | VM_ALLOW_HUGE_VMAP)
>
> I don't think it is a good idea to re-use VM flags. This can make adding
> more flags later down the line ugly. I think it would be better to
> define KHO_VMALLOC_FL* instead.
Ok.
> > +static void kho_vmalloc_free_chunks(struct kho_vmalloc_chunk *first_chunk)
> > +{
> > + struct kho_mem_track *track = &kho_out.ser.track;
> > + struct kho_vmalloc_chunk *chunk = first_chunk;
> > +
> > + while (chunk) {
> > + unsigned long pfn = PHYS_PFN(virt_to_phys(chunk));
> > + struct kho_vmalloc_chunk *tmp = chunk;
> > +
> > + __kho_unpreserve(track, pfn, pfn + 1);
>
> This doesn't unpreserve the pages contained in the chunk, which
> kho_preserve_vmalloc() preserved.
Will fix.
> > + while (chunk) {
> > + struct page *page;
> > +
> > + for (int i = 0; i < chunk->hdr.num_elms; i++) {
> > + phys_addr_t phys = chunk->phys[i];
> > +
> > + for (int j = 0; j < (1 << order); j++) {
> > + page = phys_to_page(phys);
> > + kho_restore_page(page, 0);
> > + pages[idx++] = page;
>
> This can buffer-overflow if the previous kernel was buggy and added too
> many pages. Perhaps keep check for this?
You mean it added more than total_pages?
But the preserve part adds exactly vm->nr_pages, so once we get it right
what bugs do you expect here?
> > + phys += PAGE_SIZE;
> > + }
> > + }
> > +
> > + page = virt_to_page(chunk);
> > + chunk = KHOSER_LOAD_PTR(chunk->hdr.next);
> > + kho_restore_page(page, 0);
> > + __free_page(page);
> > + }
> > +
> > + area = __get_vm_area_node(nr * PAGE_SIZE, align, shift, flags,
> > + VMALLOC_START, VMALLOC_END, NUMA_NO_NODE,
> > + GFP_KERNEL, __builtin_return_address(0));
> > + if (!area)
> > + goto err_free_pages_array;
> > +
> > + addr = (unsigned long)area->addr;
> > + size = get_vm_area_size(area);
> > + err = vmap_pages_range(addr, addr + size, PAGE_KERNEL, pages, shift);
> > + if (err)
> > + goto err_free_vm_area;
> > +
> > + return area->addr;
>
> You should free the pages array before returning here.
Why? They get into vm->pages.
--
Sincerely yours,
Mike.
next prev parent reply other threads:[~2025-09-15 14:08 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-08 10:35 [PATCH v3 0/2] kho: add support for preserving vmalloc allocations Mike Rapoport
2025-09-08 10:35 ` [PATCH v3 1/2] " Mike Rapoport
2025-09-08 14:14 ` Jason Gunthorpe
2025-09-15 14:01 ` Mike Rapoport
2025-09-15 14:37 ` Jason Gunthorpe
2025-09-15 16:11 ` Mike Rapoport
2025-09-08 18:12 ` Pratyush Yadav
2025-09-08 18:18 ` Jason Gunthorpe
2025-09-09 14:33 ` Pratyush Yadav
2025-09-15 14:12 ` Mike Rapoport
2025-09-15 14:43 ` Jason Gunthorpe
2025-09-15 16:36 ` Mike Rapoport
2025-09-16 13:05 ` Jason Gunthorpe
2025-09-16 13:21 ` Pratyush Yadav
2025-09-16 14:34 ` Mike Rapoport
2025-09-16 12:48 ` Pratyush Yadav
2025-09-16 14:41 ` Mike Rapoport
2025-09-15 14:08 ` Mike Rapoport [this message]
2025-09-16 12:43 ` Pratyush Yadav
2025-09-08 10:35 ` [PATCH v3 2/2] lib/test_kho: use kho_preserve_vmalloc instead of storing addresses in fdt Mike Rapoport
-- strict thread matches above, loose matches on Subject: below --
2025-09-07 7:00 [PATCH v2 0/2] kho: add support for preserving vmalloc allocations Mike Rapoport
2025-09-07 7:00 ` [PATCH v3 1/2] " Mike Rapoport
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=aMgdwE83dKdT1K7L@kernel.org \
--to=rppt@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=bhe@redhat.com \
--cc=changyuanl@google.com \
--cc=chrisl@kernel.org \
--cc=graf@amazon.com \
--cc=jgg@nvidia.com \
--cc=kexec@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=pasha.tatashin@soleen.com \
--cc=pratyush@kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.