Skip to contentSkip to conversation

block: replace kmalloc and then memcpy with kmemdup

PATCH In review Mihnea Dobrescu-Balaur 3 participants 3 messages Mar 11, 2013

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: replace kmalloc and then memcpy with kmemdup

---
 drivers/block/xen-blkfront.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/block/xen-blkfront.c +1 -2
index c3dae2e..9620644 100644
@@ -1203,11 +1203,10 @@ static int blkif_recover(struct blkfront_info *info)
 	int j;
 
 	/* Stage 1: Make a safe copy of the shadow state. */
-	copy = kmalloc(sizeof(info->shadow),
+	copy = kmemdup(info->shadow, sizeof(info->shadow),
 		       GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
 	if (!copy)
 		return -ENOMEM;
-	memcpy(copy, info->shadow, sizeof(info->shadow));
 
 	/* Stage 2: Set up free list. */
 	memset(&info->shadow, 0, sizeof(info->shadow));
Signature · 1 line
1.7.10.4
#1 lore raw

On Mon, Mar 11, 2013 at 01:23:36PM +0200, Mihnea Dobrescu-Balaur wrote:

Are there performance improvements to doing it this way?

Quoted 25 lines
---
 drivers/block/xen-blkfront.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/block/xen-blkfront.c +1 -2
index c3dae2e..9620644 100644
@@ -1203,11 +1203,10 @@ static int blkif_recover(struct blkfront_info *info)
 	int j;
 
 	/* Stage 1: Make a safe copy of the shadow state. */
-	copy = kmalloc(sizeof(info->shadow),
+	copy = kmemdup(info->shadow, sizeof(info->shadow),
 		       GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
 	if (!copy)
 		return -ENOMEM;
-	memcpy(copy, info->shadow, sizeof(info->shadow));
 
 	/* Stage 2: Set up free list. */
 	memset(&info->shadow, 0, sizeof(info->shadow));
Signature · 1 line
1.7.10.4
#2 lore raw

Hi Konrad,

The performance is the same using kmemdup, but we have the following advantages:
* code is cleaner
* kmemdup adds additional debugging info useful for tracking the real place where memory was allocated [1] [2].

thanks,
Daniel.

[1] http://lxr.linux.no/#linux+v3.8.2/mm/util.c#L61
[2] http://lxr.linux.no/#linux+v3.8.2/include/linux/slab.h#L363

Quoted 33 lines · Konrad Rzeszutek Wilk

On Mon, Mar 11, 2013 at 01:23:36PM +0200, Mihnea Dobrescu-Balaur wrote:

Are there performance improvements to doing it this way?

Quoted 23 lines
---
 drivers/block/xen-blkfront.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/block/xen-blkfront.c +1 -3
index c3dae2e..9620644 100644
@@ -1203,11 +1203,10 @@ static int blkif_recover(struct blkfront_info *info)
      int j;
 
      /* Stage 1: Make a safe copy of the shadow state. */
-     copy = kmalloc(sizeof(info->shadow),
+     copy = kmemdup(info->shadow, sizeof(info->shadow),
                     GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
      if (!copy)
              return -ENOMEM;
-     memcpy(copy, info->shadow, sizeof(info->shadow));
 
      /* Stage 2: Set up free list. */
      memset(&info->shadow, 0, sizeof(info->shadow));
--
Quoted 2 lines

1.7.10.4

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization