public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Claudio Imbrenda <imbrenda@linux.ibm.com>
To: Krish Sadhukhan <krish.sadhukhan@oracle.com>
Cc: kvm@vger.kernel.org, frankja@linux.ibm.com, david@redhat.com,
	thuth@redhat.com, pbonzini@redhat.com, cohuck@redhat.com,
	lvivier@redhat.com, nadav.amit@gmail.com
Subject: Re: [kvm-unit-tests PATCH v1 03/12] lib/vmalloc: add some asserts and improvements
Date: Mon, 4 Jan 2021 14:27:39 +0100	[thread overview]
Message-ID: <20210104142739.6f05f0c1@ibm-vm> (raw)
In-Reply-To: <80b20b32-64e6-5e69-0b0f-5d72aefe8398@oracle.com>

On Thu, 24 Dec 2020 10:16:45 -0800
Krish Sadhukhan <krish.sadhukhan@oracle.com> wrote:

> On 12/16/20 12:11 PM, Claudio Imbrenda wrote:
> > Add some asserts to make sure the state is consistent.
> >
> > Simplify and improve the readability of vm_free.
> >
> > Fixes: 3f6fee0d4da4 ("lib/vmalloc: vmalloc support for handling
> > allocation metadata")
> >
> > Signed-off-by: Claudio Imbrenda<imbrenda@linux.ibm.com>
> > ---
> >   lib/vmalloc.c | 20 +++++++++++---------
> >   1 file changed, 11 insertions(+), 9 deletions(-)
> >
> > diff --git a/lib/vmalloc.c b/lib/vmalloc.c
> > index 986a34c..7a49adf 100644
> > --- a/lib/vmalloc.c
> > +++ b/lib/vmalloc.c
> > @@ -162,13 +162,14 @@ static void *vm_memalign(size_t alignment,
> > size_t size) static void vm_free(void *mem)
> >   {
> >   	struct metadata *m;
> > -	uintptr_t ptr, end;
> > +	uintptr_t ptr, page, i;
> >   
> >   	/* the pointer is not page-aligned, it was a single-page
> > allocation */  
> 
> 
> Do we need an assert() for 'mem' if it is NULL for some reason ?

I thought the NULL case was handled in alloc.c, but I was mistaken.
In any case, the metadata will probably not match and trigger the other
asserts.

I will still add assert(mem), though, as it looks cleaner

> >   	if (!IS_ALIGNED((uintptr_t)mem, PAGE_SIZE)) {
> >   		assert(GET_MAGIC(mem) == VM_MAGIC);
> > -		ptr = virt_to_pte_phys(page_root, mem) & PAGE_MASK;
> > -		free_page(phys_to_virt(ptr));
> > +		page = virt_to_pte_phys(page_root, mem) &
> > PAGE_MASK;
> > +		assert(page);
> > +		free_page(phys_to_virt(page));
> >   		return;
> >   	}
> >   
> > @@ -176,13 +177,14 @@ static void vm_free(void *mem)
> >   	m = GET_METADATA(mem);
> >   	assert(m->magic == VM_MAGIC);
> >   	assert(m->npages > 0);
> > +	assert(m->npages < BIT_ULL(BITS_PER_LONG - PAGE_SHIFT));  
> 
> 
> NIT:  Combine the two assert()s for 'npages' perhaps ?

no, when one assert is triggered, it will print a useful message; if
you combine the two asserts you don't know why exactly it failed.

it's functionally equivalent, but it has a better user experience.

> >   	/* free all the pages including the metadata page */
> > -	ptr = (uintptr_t)mem - PAGE_SIZE;
> > -	end = ptr + m->npages * PAGE_SIZE;
> > -	for ( ; ptr < end; ptr += PAGE_SIZE)
> > -		free_page(phys_to_virt(virt_to_pte_phys(page_root,
> > (void *)ptr)));
> > -	/* free the last one separately to avoid overflow issues */
> > -	free_page(phys_to_virt(virt_to_pte_phys(page_root, (void
> > *)ptr)));
> > +	ptr = (uintptr_t)m & PAGE_MASK;
> > +	for (i = 0 ; i < m->npages + 1; i++, ptr += PAGE_SIZE) {
> > +		page = virt_to_pte_phys(page_root, (void *)ptr) &
> > PAGE_MASK;
> > +		assert(page);
> > +		free_page(phys_to_virt(page));
> > +	}
> >   }
> >   
> >   static struct alloc_ops vmalloc_ops = {  


  reply	other threads:[~2021-01-04 13:34 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-16 20:11 [kvm-unit-tests PATCH v1 00/12] Fix and improve the page allocator Claudio Imbrenda
2020-12-16 20:11 ` [kvm-unit-tests PATCH v1 01/12] lib/x86: fix page.h to include the generic header Claudio Imbrenda
2020-12-17 12:33   ` Thomas Huth
2020-12-16 20:11 ` [kvm-unit-tests PATCH v1 02/12] lib/list.h: add list_add_tail Claudio Imbrenda
2020-12-17 12:39   ` Thomas Huth
2020-12-16 20:11 ` [kvm-unit-tests PATCH v1 03/12] lib/vmalloc: add some asserts and improvements Claudio Imbrenda
2020-12-24 18:16   ` Krish Sadhukhan
2021-01-04 13:27     ` Claudio Imbrenda [this message]
2020-12-16 20:11 ` [kvm-unit-tests PATCH v1 04/12] lib/asm: Fix definitions of memory areas Claudio Imbrenda
2020-12-24 18:17   ` Krish Sadhukhan
2021-01-04 13:19     ` Claudio Imbrenda
2021-01-05  1:17       ` Krish Sadhukhan
2020-12-16 20:11 ` [kvm-unit-tests PATCH v1 05/12] lib/alloc_page: fix and improve the page allocator Claudio Imbrenda
2020-12-24 18:17   ` Krish Sadhukhan
2021-01-04 13:11     ` Claudio Imbrenda
2021-01-05  1:15       ` Krish Sadhukhan
2020-12-28 19:34   ` Sean Christopherson
2021-01-04 17:23     ` Claudio Imbrenda
2020-12-16 20:11 ` [kvm-unit-tests PATCH v1 06/12] lib/alloc.h: remove align_min from struct alloc_ops Claudio Imbrenda
2020-12-24 18:17   ` Krish Sadhukhan
2021-01-04 13:05     ` Claudio Imbrenda
2021-01-05  0:39       ` Krish Sadhukhan
2020-12-16 20:11 ` [kvm-unit-tests PATCH v1 07/12] lib/alloc_page: Optimization to skip known empty freelists Claudio Imbrenda
2020-12-16 20:11 ` [kvm-unit-tests PATCH v1 08/12] lib/alloc_page: rework metadata format Claudio Imbrenda
2020-12-16 20:11 ` [kvm-unit-tests PATCH v1 09/12] lib/alloc: replace areas with more generic flags Claudio Imbrenda
2020-12-16 20:11 ` [kvm-unit-tests PATCH v1 10/12] lib/alloc_page: Wire up ZERO_FLAG Claudio Imbrenda
2020-12-16 20:11 ` [kvm-unit-tests PATCH v1 11/12] lib/alloc_page: Properly handle requests for fresh blocks Claudio Imbrenda
2020-12-16 20:12 ` [kvm-unit-tests PATCH v1 12/12] lib/alloc_page: default flags and zero pages by default Claudio Imbrenda
2020-12-24 18:17   ` Krish Sadhukhan
2021-01-04 13:32     ` Claudio Imbrenda
2020-12-17 19:41 ` [kvm-unit-tests PATCH v1 00/12] Fix and improve the page allocator Nadav Amit
2020-12-18 14:19   ` Claudio Imbrenda
2020-12-28  6:31     ` Nadav Amit
2021-01-05 15:26       ` Claudio Imbrenda
2020-12-24 18:19 ` Krish Sadhukhan

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=20210104142739.6f05f0c1@ibm-vm \
    --to=imbrenda@linux.ibm.com \
    --cc=cohuck@redhat.com \
    --cc=david@redhat.com \
    --cc=frankja@linux.ibm.com \
    --cc=krish.sadhukhan@oracle.com \
    --cc=kvm@vger.kernel.org \
    --cc=lvivier@redhat.com \
    --cc=nadav.amit@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=thuth@redhat.com \
    /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