From nobody Sun Nov 10 22:25:54 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1569496949; cv=none; d=zoho.com; s=zohoarc; b=DM5KISDonFJ/4y0V30oeq5VObMaMa8HG1iSNR0ZNGYio8lhfKNU0Y5rTqFYtEHPIasStlOteF08/27UUsmacoF39MBFMzmN/LVqlwnQKMQdgsBYGzVCgu3hF72T0Fvp5XQJXXcDe1XvmkBj2WnVcIzYns9EkVibHUvGOU0SkPAQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1569496949; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=ck39LKQAvIGEBuAwhz1hvC71jjEbn3TVHd4UGlFtG8Q=; b=RY456boXrz9+rxu3hulcHDLs/1nmAEYRD5e+jDOxPE22sAka2vfsgakx/i3RmjadbElIDPQPWiJEPz2bw3Ql2gSH8HNxSDYm8m2Wdce1JC6KWQOtzA1HtOINw+4a7XXUt8fscOQnPhppfqodyKbKr/T3NWIeLddcRtkw1ZTtb9g= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1569496949730174.63272439868865; Thu, 26 Sep 2019 04:22:29 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iDRpk-0008MJ-8I; Thu, 26 Sep 2019 11:21:16 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iDRpi-0008KA-Up for xen-devel@lists.xenproject.org; Thu, 26 Sep 2019 11:21:14 +0000 Received: from mail-lj1-x243.google.com (unknown [2a00:1450:4864:20::243]) by localhost (Halon) with ESMTPS id b5fcc440-e04f-11e9-8628-bc764e2007e4; Thu, 26 Sep 2019 11:20:57 +0000 (UTC) Received: by mail-lj1-x243.google.com with SMTP id j19so1779500lja.1 for ; Thu, 26 Sep 2019 04:20:57 -0700 (PDT) Received: from otyshchenko.kyiv.epam.com (ll-22.209.223.85.sovam.net.ua. [85.223.209.22]) by smtp.gmail.com with ESMTPSA id n4sm526810lfi.63.2019.09.26.04.20.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 26 Sep 2019 04:20:55 -0700 (PDT) X-Inumbo-ID: b5fcc440-e04f-11e9-8628-bc764e2007e4 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=0xbudc/HCLzyOpGQnpxjx2YsmpcxWIW14AxzgKs21n8=; b=sJl6yxyQmI2gEjZ2odWCzoIaGQy7bypnuK0jIckZEHMu80svlZvQqnHZQe2f7k2QZF PUmMw0+BxMLF/Q8thpzycPj+KmpDxDNtidAjQ22hp0IQ9vT0HM51xNln38eywDn6zO3f BxZg7YXZD1YWWNw5J6ofrFybHyjsxaGrwth1y0tJa4QG0R0kxJxGDBlSQ+WKDXmX9s3R r8K/G5DuWP+bLTd1JUuvJJKi+URWJ9g1zjcoVvRbikGw9wUt7rLugXmEMc00CKhqpXFJ RZERqwgVm0GthSDi/W2mmqExPK7XUbmruNjmmrBWywDJJL/e0wbpzbCiJXjS9q7fOpJi TGfQ== 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=0xbudc/HCLzyOpGQnpxjx2YsmpcxWIW14AxzgKs21n8=; b=fv8ZjXFYDHAhOqg7NHzl08go/S0h+c8oNaYWhV3RQn1kXWzoB8HvL67t+0uocpAgfH OICMDFilOzt5sCnTUD+LRiWbhmfnoeu8sayhXdDRjzeldkDhFUyMZX+TS2yIHr9kkhiG FoB7y/7O3sf4OM8E8r27nwGXxc4xmKdLVLN9i7Hi34Zowori8WKta53Ak8ZnhClaYVaW arc1UX3pguntJ3Fr1E66zRfPoVZSx4FtUIFICKvd2CCfgSTfREHXc+0zMD0xp/HWO6yU ZFuMjtPg4FvAsXcBPvMMHJTMcJT6bkdsQoeQAnZ2EX2jtsjCSumDUfCPZ3pQLQ3H2sUB 2aww== X-Gm-Message-State: APjAAAVNl5faswiW251F6/gMjHY9N0nXRtw7AZxRyQBTUYN3+FuNTxK+ ChKawaaaFNPRxcCEL8edV+0YVasNO2c= X-Google-Smtp-Source: APXvYqw+dSxa1heh1KyG+X237dfFH79nFG3ChXG4O9C99GDWuEEStju/fF4F516QPMwo6TnqfZv6mw== X-Received: by 2002:a2e:9615:: with SMTP id v21mr2207141ljh.46.1569496856534; Thu, 26 Sep 2019 04:20:56 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Date: Thu, 26 Sep 2019 14:20:29 +0300 Message-Id: <1569496834-7796-4-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1569496834-7796-1-git-send-email-olekstysh@gmail.com> References: <1569496834-7796-1-git-send-email-olekstysh@gmail.com> Subject: [Xen-devel] [PATCH V6 3/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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) 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 Reviewed-by: Jan Beulich --- Changes V5 -> V6: - change uint32_t to unsigned int for "pad" in add_padding() - change back to _xmalloc - move tmp_size declaration/calculation to the corresponding block - use "!" over "=3D=3D 0" Changes V4 -> V5: - avoid possible truncation with allocations of 4GiB or above - introduce helper functions add(strip)_padding to avoid duplicating the code - omit the unnecessary casts, change u32 to uint32_t when moving the code - use _xzalloc instead of _xmalloc to get the tail portion zeroed - update pointer according to the requsted alignment - compared against "size" instead of "tmp_size" for the allocations above PAGE_SIZE 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 | 114 ++++++++++++++++++++++++++++++++++++++----= ---- xen/include/xen/xmalloc.h | 1 + 2 files changed, 97 insertions(+), 18 deletions(-) diff --git a/xen/common/xmalloc_tlsf.c b/xen/common/xmalloc_tlsf.c index 1e8d72d..7ab2b3b 100644 --- a/xen/common/xmalloc_tlsf.c +++ b/xen/common/xmalloc_tlsf.c @@ -549,10 +549,40 @@ static void tlsf_init(void) * xmalloc() */ =20 +static void *strip_padding(void *p) +{ + struct bhdr *b =3D p - BHDR_OVERHEAD; + + if ( b->size & FREE_BLOCK ) + { + p -=3D b->size & ~FREE_BLOCK; + b =3D p - BHDR_OVERHEAD; + ASSERT(!(b->size & FREE_BLOCK)); + } + + return p; +} + +static void *add_padding(void *p, unsigned long align) +{ + unsigned int pad; + + if ( (pad =3D -(long)p & (align - 1)) !=3D 0 ) + { + void *q =3D p + pad; + struct bhdr *b =3D q - BHDR_OVERHEAD; + + ASSERT(q > p); + b->size =3D pad | FREE_BLOCK; + p =3D q; + } + + return p; +} + void *_xmalloc(unsigned long size, unsigned long align) { void *p =3D NULL; - u32 pad; =20 ASSERT(!in_irq()); =20 @@ -573,14 +603,7 @@ void *_xmalloc(unsigned long size, unsigned long align) return xmalloc_whole_pages(size - align + MEM_ALIGN, align); =20 /* Add alignment padding. */ - if ( (pad =3D -(long)p & (align - 1)) !=3D 0 ) - { - char *q =3D (char *)p + pad; - struct bhdr *b =3D (struct bhdr *)(q - BHDR_OVERHEAD); - ASSERT(q > (char *)p); - b->size =3D pad | FREE_BLOCK; - p =3D q; - } + p =3D add_padding(p, align); =20 ASSERT(((unsigned long)p & (align - 1)) =3D=3D 0); return p; @@ -593,10 +616,71 @@ void *_xzalloc(unsigned long size, unsigned long alig= n) return p ? memset(p, 0, size) : p; } =20 -void xfree(void *p) +void *_xrealloc(void *ptr, unsigned long size, unsigned long align) { - struct bhdr *b; + unsigned long curr_size; + void *p; + + if ( !size ) + { + xfree(ptr); + return ZERO_BLOCK_PTR; + } =20 + if ( ptr =3D=3D NULL || ptr =3D=3D ZERO_BLOCK_PTR ) + return _xmalloc(size, align); + + ASSERT(!(align & (align - 1))); + if ( align < MEM_ALIGN ) + align =3D MEM_ALIGN; + + if ( !((unsigned long)ptr & (PAGE_SIZE - 1)) ) + { + curr_size =3D (unsigned long)PFN_ORDER(virt_to_page(ptr)) << PAGE_= SHIFT; + + if ( size <=3D curr_size && !((unsigned long)ptr & (align - 1)) ) + return ptr; + } + else + { + unsigned long tmp_size; + struct bhdr *b; + + tmp_size =3D size + align - MEM_ALIGN; + + if ( tmp_size < PAGE_SIZE ) + tmp_size =3D (tmp_size < MIN_BLOCK_SIZE) ? MIN_BLOCK_SIZE : + ROUNDUP_SIZE(tmp_size); + + /* Strip alignment padding. */ + p =3D strip_padding(ptr); + + b =3D p - BHDR_OVERHEAD; + curr_size =3D b->size & BLOCK_SIZE_MASK; + + if ( tmp_size <=3D curr_size ) + { + /* Add alignment padding. */ + p =3D add_padding(p, align); + + ASSERT(!((unsigned long)p & (align - 1))); + + return p; + } + } + + p =3D _xmalloc(size, align); + if ( p ) + { + memcpy(p, ptr, min(curr_size, size)); + xfree(ptr); + } + + return p; +} + +void xfree(void *p) +{ if ( p =3D=3D NULL || p =3D=3D ZERO_BLOCK_PTR ) return; =20 @@ -621,13 +705,7 @@ void xfree(void *p) } =20 /* Strip alignment padding. */ - b =3D (struct bhdr *)((char *)p - BHDR_OVERHEAD); - if ( b->size & FREE_BLOCK ) - { - p =3D (char *)p - (b->size & ~FREE_BLOCK); - b =3D (struct bhdr *)((char *)p - BHDR_OVERHEAD); - ASSERT(!(b->size & FREE_BLOCK)); - } + p =3D strip_padding(p); =20 xmem_pool_free(p, xenpool); } 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); =20 static inline void *_xmalloc_array( unsigned long size, unsigned long align, unsigned long num) --=20 2.7.4 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel