From: Laurent Vivier Subject: Re: [kvm-unit-tests PATCH v2 5/6] x86: lib/alloc: introduce alloc_zeroed_page Date: Thu, 3 Nov 2016 14:03:19 +0100 Message-ID: <3c5bd7c3-30ef-0699-58be-f0a41b29fb86@redhat.com> References: <1478119966-13252-1-git-send-email-drjones@redhat.com> <1478119966-13252-6-git-send-email-drjones@redhat.com> <774fe268-f7a9-813a-dd1e-91b6d2ce5d22@redhat.com> <20161103124729.wl4xghbe7wktoyqd@kamzik.brq.redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Cc: kvm@vger.kernel.org, pbonzini@redhat.com, thuth@redhat.com To: Andrew Jones Return-path: Received: from mx1.redhat.com ([209.132.183.28]:57812 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757215AbcKCNDW (ORCPT ); Thu, 3 Nov 2016 09:03:22 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6C3273F1F2 for ; Thu, 3 Nov 2016 13:03:22 +0000 (UTC) In-Reply-To: <20161103124729.wl4xghbe7wktoyqd@kamzik.brq.redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: On 03/11/2016 13:47, Andrew Jones wrote: > On Thu, Nov 03, 2016 at 01:02:03PM +0100, Laurent Vivier wrote: >> >> >> On 02/11/2016 21:52, Andrew Jones wrote: >>> This allows us to remove a bunch of memsets. >>> >>> Signed-off-by: Andrew Jones >>> --- >>> lib/alloc.c | 8 ++++++++ >>> lib/alloc.h | 1 + >>> lib/x86/vm.c | 4 +--- >>> x86/vmx.c | 19 +++++++------------ >>> x86/vmx_tests.c | 28 ++++++++++------------------ >>> 5 files changed, 27 insertions(+), 33 deletions(-) >>> >>> diff --git a/lib/alloc.c b/lib/alloc.c >>> index ce1198e2977f..d797690cc458 100644 >>> --- a/lib/alloc.c >>> +++ b/lib/alloc.c >>> @@ -191,6 +191,14 @@ void *alloc_page(void) >>> return p; >>> } >>> >>> +void *alloc_zeroed_page(void) >>> +{ >>> + void *p = alloc_page(); >> >> alloc_page() can return NULL. >> >> As most of the callers don't check the return value of >> alloc_zeroed_page(), I think you should assert() here. > > Hmm... both returning NULL (which you're right I should have done here) > and asserting have their merits. If a unit test, for whatever reason, > wanted to allocate all memory, then it would impossible to know how many > alloc_page calls that would take before needing to stop to avoid the > assert. It's much easier to loop until NULL. However, most callers won't > do that, and, as you point out below, most callers are neglecting to > check for NULL. > > So I think we need both. I propose renaming alloc_page to __alloc_page. > Unit tests that want to avoid the assert and check for NULL should use > that. But most callers should use a new alloc_page, > > void *alloc_page(void) > { > void *p = __alloc_page(); > assert(p); > return p; > } > > alloc_zeroed_page will be built on alloc_page. __alloc_page users can > zero their own pages... > > Thoughts on that? It's nice to have both. Good idea. Thanks, Laurent