From patchwork Fri Sep 13 15:35:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Tyshchenko X-Patchwork-Id: 11144943 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C829814E5 for ; Fri, 13 Sep 2019 15:37:45 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A33C520CC7 for ; Fri, 13 Sep 2019 15:37:45 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZBRi1lQu" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A33C520CC7 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i8ncU-00026b-10; Fri, 13 Sep 2019 15:36:22 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i8ncS-000263-Iv for xen-devel@lists.xenproject.org; Fri, 13 Sep 2019 15:36:20 +0000 X-Inumbo-ID: 31382f28-d63c-11e9-a337-bc764e2007e4 Received: from mail-lf1-x142.google.com (unknown [2a00:1450:4864:20::142]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 31382f28-d63c-11e9-a337-bc764e2007e4; Fri, 13 Sep 2019 15:36:03 +0000 (UTC) Received: by mail-lf1-x142.google.com with SMTP id u26so4744040lfg.6 for ; Fri, 13 Sep 2019 08:36:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=fxkUwvoksIjfYu4eian3tL19eiy5Yg+x1AYWMg4u2XA=; b=ZBRi1lQu0ritDJ/l/dX+iDK3BGJIUufjL+WgelMJj9hGVPxlDJM6eCP8iNTJEFqpSJ KizAP4qb/K/vYKBn2nN4x3CdRXtQ7oSE+PhFSpvVmW9aFYYpTBkiPNwWfJuBCnKI1YWA nXegucSLKrKOJvFkSoMhKGg6wdmN6blLcWpCSSEjOeB2MGLbrvUARHgMkrepkA627WvQ XDDsRkZZYgOgXfkxuDD5IeeaRpDe8512mAcho8FeF+yXdyhOWJh6jK4bYeWUHeK+bhHs Gknt7+srDMIfDvdKcl/E85uw6aRxEQGRPcSgqZHmQvbLcQSnYrrlxjInpoQ4sNJP7+RI 0EuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=fxkUwvoksIjfYu4eian3tL19eiy5Yg+x1AYWMg4u2XA=; b=BqeNlmg5kHRBmHftcAETbOg/5OfkTRaKWQccLXji0AYbiKIxlHMSC0/KkqaxFKJsKM w4K6sPMCFc6lRJnal+yiDkwy/IDhzdiErbUh6rW5pwYBVXK/3lsBbCyMfaUkGvqjOG6j 0rpPECPJYoR23p3bL9m4n+3b/GlEf25fUV+5AsW+CuuxFDT8x4Jcw227fytABnsueUmQ VvsxUuseh+SjmohQJJgZYYrnATUehfrQHWaBNeZYz7xSKMH2DnBdxcAPRDcpDrE9aZ1W Nt3dChcls1lCkQAS/obDwCqyJyQhRd9lcsvLwD8DNV8oPyz8IbaGBsaZh5aMlRBEsfTt vmWA== X-Gm-Message-State: APjAAAUPBo8Ipz3SNcjlj9JAcEmbUQUwSoa9yN6GTO3lJ5VsUDGTGnmc g9xMDT5ef+/P1WSpdsefQkAzGSK7veM= X-Google-Smtp-Source: APXvYqzU/46uM4euSQhBERxnWDhTa5hWmHlPJcvgQbdIajOZkir1FD52opNeoYuWztbFALKGZFugow== X-Received: by 2002:ac2:4424:: with SMTP id w4mr31369679lfl.65.1568388961835; Fri, 13 Sep 2019 08:36:01 -0700 (PDT) Received: from otyshchenko.kyiv.epam.com (ll-74.141.223.85.sovam.net.ua. [85.223.141.74]) by smtp.gmail.com with ESMTPSA id c16sm6969765lfj.8.2019.09.13.08.36.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 13 Sep 2019 08:36:01 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Date: Fri, 13 Sep 2019 18:35:13 +0300 Message-Id: <1568388917-7287-5-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1568388917-7287-1-git-send-email-olekstysh@gmail.com> References: <1568388917-7287-1-git-send-email-olekstysh@gmail.com> Subject: [Xen-devel] [PATCH V4 4/8] xen/common: Introduce _xrealloc function X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: sstabellini@kernel.org, Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , Oleksandr Tyshchenko , julien.grall@arm.com, Paul Durrant , Jan Beulich , Volodymyr_Babchuk@epam.com MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Oleksandr Tyshchenko This patch introduces type-unsafe function which besides re-allocation handles the following corner cases: 1. if requested size is zero, it will behave like xfree 2. if incoming pointer is not valid (NULL or ZERO_BLOCK_PTR), it will behave like xmalloc If both pointer and size are valid the function will re-allocate and copy only if requested size and alignment don't fit in already allocated space. Subsequent patch will add type-safe helper macros. Signed-off-by: Oleksandr Tyshchenko CC: Andrew Cooper CC: George Dunlap CC: Ian Jackson CC: Jan Beulich CC: Julien Grall CC: Konrad Rzeszutek Wilk CC: Stefano Stabellini CC: Tim Deegan CC: Wei Liu CC: Paul Durrant --- Changes V3 -> V4: - add check for the alignment compatibility - properly detect current size (take into the account a possible fake alignment header) - update comment in code/patch description Changes RFC -> V3: - behave like xmalloc if incoming pointer is ZERO_BLOCK_PTR or NULL - return ZERO_BLOCK_PTR after xfree if requested size is zero - add patch description - use allocator internals to recognize current size of the incoming pointer - do not re-allocate and copy if requested size fits in already allocated space ... Original patch was initially posted by Sameer Goel: https://lists.xen.org/archives/html/xen-devel/2017-06/msg00858.html This could be considered as another attempt to add it: https://www.mail-archive.com/kexec@lists.infradead.org/msg21335.html [As it was previously discussed with Julien in IRC] The reason for this patch to be an RFC is that patch itself is not completely correct and I don't fully understand what/how should be done for this patch to be accepted. Or whether community even wants this to go in. So, to avoid bike shedding, the first target is to collect feedback. For everyone who wants more details why this is needed and where used, please see next patch of this thread: "iommu/arm: Add lightweight iommu_fwspec support" In a nutshell, the upcoming "iommu_fwspec" support on ARM is going to use xrealloc to expand an array for device IDs. We really want to have "iommu_fwspec" support which will give us a generic abstract way to add new device to the IOMMU based on the generic IOMMU DT binding. --- xen/common/xmalloc_tlsf.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++ xen/include/xen/xmalloc.h | 1 + 2 files changed, 53 insertions(+) diff --git a/xen/common/xmalloc_tlsf.c b/xen/common/xmalloc_tlsf.c index e98ad65..2b240b1 100644 --- a/xen/common/xmalloc_tlsf.c +++ b/xen/common/xmalloc_tlsf.c @@ -598,6 +598,58 @@ void *_xzalloc(unsigned long size, unsigned long align) return p ? memset(p, 0, size) : p; } +void *_xrealloc(void *ptr, unsigned long size, unsigned long align) +{ + unsigned long curr_size, tmp_size; + void *p; + + if ( !size ) + { + xfree(ptr); + return ZERO_BLOCK_PTR; + } + + if ( ptr == NULL || ptr == ZERO_BLOCK_PTR ) + return _xmalloc(size, align); + + if ( !((unsigned long)ptr & (PAGE_SIZE - 1)) ) + curr_size = PFN_ORDER(virt_to_page(ptr)) << PAGE_SHIFT; + else + { + struct bhdr *b = (struct bhdr *)((char *)ptr - BHDR_OVERHEAD); + + if ( b->size & FREE_BLOCK ) + { + p = (char *)ptr - (b->size & ~FREE_BLOCK); + b = (struct bhdr *)((char *)p - BHDR_OVERHEAD); + ASSERT(!(b->size & FREE_BLOCK)); + } + + curr_size = b->size & BLOCK_SIZE_MASK; + } + + ASSERT((align & (align - 1)) == 0); + if ( align < MEM_ALIGN ) + align = MEM_ALIGN; + tmp_size = size + align - MEM_ALIGN; + + if ( tmp_size < PAGE_SIZE ) + tmp_size = ( tmp_size < MIN_BLOCK_SIZE ) ? MIN_BLOCK_SIZE : + ROUNDUP_SIZE(tmp_size); + + if ( tmp_size <= curr_size && ((unsigned long)ptr & (align - 1)) == 0 ) + return ptr; /* the size and alignment fit in already allocated space */ + + p = _xmalloc(size, align); + if ( p ) + { + memcpy(p, ptr, min(curr_size, size)); + xfree(ptr); + } + + return p; +} + void xfree(void *p) { struct bhdr *b; diff --git a/xen/include/xen/xmalloc.h b/xen/include/xen/xmalloc.h index f075d2d..831152f 100644 --- a/xen/include/xen/xmalloc.h +++ b/xen/include/xen/xmalloc.h @@ -51,6 +51,7 @@ extern void xfree(void *); /* Underlying functions */ extern void *_xmalloc(unsigned long size, unsigned long align); extern void *_xzalloc(unsigned long size, unsigned long align); +extern void *_xrealloc(void *ptr, unsigned long size, unsigned long align); static inline void *_xmalloc_array( unsigned long size, unsigned long align, unsigned long num)