All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oleksandr Tyshchenko <olekstysh@gmail.com>
To: xen-devel@lists.xenproject.org
Cc: sstabellini@kernel.org, Wei Liu <wl@xen.org>,
	Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>,
	George Dunlap <George.Dunlap@eu.citrix.com>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	Ian Jackson <ian.jackson@eu.citrix.com>, Tim Deegan <tim@xen.org>,
	Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>,
	julien.grall@arm.com, Jan Beulich <jbeulich@suse.com>,
	Volodymyr_Babchuk@epam.com
Subject: [Xen-devel] [PATCH V3 3/8] xen/common: Introduce _xrealloc function
Date: Tue, 20 Aug 2019 21:09:42 +0300	[thread overview]
Message-ID: <1566324587-3442-4-git-send-email-olekstysh@gmail.com> (raw)
In-Reply-To: <1566324587-3442-1-git-send-email-olekstysh@gmail.com>

From: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>

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 doesn't fit in already allocated space.

Subsequent patch will add type-safe helper macros.

Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
CC: Andrew Cooper <andrew.cooper3@citrix.com>
CC: George Dunlap <George.Dunlap@eu.citrix.com>
CC: Ian Jackson <ian.jackson@eu.citrix.com>
CC: Jan Beulich <jbeulich@suse.com>
CC: Julien Grall <julien.grall@arm.com>
CC: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
CC: Stefano Stabellini <sstabellini@kernel.org>
CC: Tim Deegan <tim@xen.org>
CC: Wei Liu <wl@xen.org>

---
Changes since RFC:
   - 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 | 45 +++++++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/xmalloc.h |  1 +
 2 files changed, 46 insertions(+)

diff --git a/xen/common/xmalloc_tlsf.c b/xen/common/xmalloc_tlsf.c
index e98ad65..eecae2e 100644
--- a/xen/common/xmalloc_tlsf.c
+++ b/xen/common/xmalloc_tlsf.c
@@ -598,6 +598,51 @@ 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;
+        b = (struct bhdr *)((char *)ptr - BHDR_OVERHEAD);
+        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 ) /* fits in current block */
+        return ptr;
+
+    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)
-- 
2.7.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2019-08-20 18:10 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-20 18:09 [Xen-devel] [PATCH V3 0/8] iommu/arm: Add Renesas IPMMU-VMSA support + Linux's iommu_fwspec Oleksandr Tyshchenko
2019-08-20 18:09 ` [Xen-devel] [PATCH V3 1/8] iommu/arm: Add iommu_helpers.c file to keep common for IOMMUs stuff Oleksandr Tyshchenko
2019-09-09 11:45   ` Julien Grall
2019-09-09 14:12     ` Oleksandr
2019-08-20 18:09 ` [Xen-devel] [PATCH V3 2/8] iommu/arm: Add ability to handle deferred probing request Oleksandr Tyshchenko
2019-09-09 12:24   ` Julien Grall
2019-09-09 14:19     ` Oleksandr
2019-08-20 18:09 ` Oleksandr Tyshchenko [this message]
2019-08-21  8:09   ` [Xen-devel] [PATCH V3 3/8] xen/common: Introduce _xrealloc function Paul Durrant
2019-08-21 14:36     ` Oleksandr
2019-08-21 15:47       ` Paul Durrant
2019-08-21 17:04         ` Oleksandr
2019-08-20 18:09 ` [Xen-devel] [PATCH V3 4/8] xen/common: Introduce xrealloc_flex_struct() helper macros Oleksandr Tyshchenko
2019-08-27 13:28   ` Jan Beulich
2019-08-28 18:23     ` Oleksandr
2019-08-29  7:21       ` Jan Beulich
2019-08-29 19:04         ` Oleksandr
2019-08-20 18:09 ` [Xen-devel] [PATCH V3 5/8] iommu/arm: Add lightweight iommu_fwspec support Oleksandr Tyshchenko
2019-09-09 14:36   ` Julien Grall
2019-09-09 14:41     ` Oleksandr
2019-08-20 18:09 ` [Xen-devel] [PATCH V3 6/8] iommu: Add of_xlate callback Oleksandr Tyshchenko
2019-08-27 13:30   ` Jan Beulich
2019-08-27 14:59     ` Oleksandr
2019-08-27 15:11       ` Jan Beulich
2019-09-09 12:37         ` Julien Grall
2019-09-09 14:28           ` Oleksandr
2019-08-20 18:09 ` [Xen-devel] [PATCH V3 7/8] iommu/arm: Introduce iommu_add_dt_device API Oleksandr Tyshchenko
2019-09-09 15:04   ` Julien Grall
2019-09-09 15:48     ` Oleksandr
2019-09-10 13:34       ` Oleksandr
2019-09-10 14:30         ` Julien Grall
2019-08-20 18:09 ` [Xen-devel] [PATCH V3 8/8] iommu/arm: Add Renesas IPMMU-VMSA support Oleksandr Tyshchenko
2019-08-29  8:37   ` Yoshihiro Shimoda
2019-08-29 10:56     ` Oleksandr
2019-09-02  7:04       ` Yoshihiro Shimoda
2019-09-09 14:33         ` Oleksandr
2019-08-29 11:19   ` Oleksandr
2019-09-09 21:24   ` Julien Grall
2019-09-10 11:04     ` Oleksandr
2019-09-10 14:31       ` Julien Grall
2019-09-11 15:29         ` Oleksandr

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=1566324587-3442-4-git-send-email-olekstysh@gmail.com \
    --to=olekstysh@gmail.com \
    --cc=George.Dunlap@eu.citrix.com \
    --cc=Volodymyr_Babchuk@epam.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=jbeulich@suse.com \
    --cc=julien.grall@arm.com \
    --cc=konrad.wilk@oracle.com \
    --cc=oleksandr_tyshchenko@epam.com \
    --cc=sstabellini@kernel.org \
    --cc=tim@xen.org \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.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.