Skip to contentSkip to conversation

fw_cfg: *prequel* to cleanup series

PATCH In review Gabriel L. Somlo 3 participants 6 messages Jun 8, 2015 – Jun 9, 2015

lore
Share & tools
lore
#0 lore raw

cover fw_cfg: *prequel* to cleanup series

The fw_cfg cleanup series exposed a memory leak occurring on ppc and sparc machine types.

This series adds a safe fw_cfg integer-type update function (1/2), and modifies ppc and sparc callbacks to use it instead of the current leaky method (2/2).

This should preferably go in *before* "fw_cfg: remove support for guest-side data writes", which would otherwise cause certain ppc and sparc command lines to crash.

Once these are reviewed (also, see additional questions in 1/1), should I re-spin the whole set, or keep these two as a separate, independent series?

Thanks,
Gabriel

Gabriel L. Somlo (2):
fw_cfg: add fw_cfg_modify_i16 (update) method
fw_cfg: fix FW_CFG_BOOT_DEVICE value update on ppc and sparc[64]

 hw/nvram/fw_cfg.c         | 10 ++++++++++
 hw/ppc/mac_newworld.c     |  2 +-
 hw/ppc/mac_oldworld.c     |  2 +-
 hw/sparc/sun4m.c          |  2 +-
 hw/sparc64/sun4u.c        |  2 +-
 include/hw/nvram/fw_cfg.h |  1 +
 6 files changed, 15 insertions(+), 4 deletions(-)
Signature · 1 line
2.1.0
#1 lore raw

PATCH 1/2 fw_cfg: add fw_cfg_modify_i16 (update) method

Allow the ability to modify the value of an existing 16-bit integer fw_cfg item.

---

Couple of thoughts:

1. I'm thinking about pre-emptively creating _i16, _i32, and _i64 versions, but right now (for fixing sparc and ppc) we only need the _i16 version. What to do ?

2. Part of me wants to find the memory location containing the previous value and simply overwrite it, but I'll need to somehow ensure the blob being replaced was of the same size, etc., which could get hairy. So for now I'm going with the paranoid/safe version which allocates a new blob and frees the old one.

Thanks,
Gabriel

 hw/nvram/fw_cfg.c         | 10 ++++++++++
 include/hw/nvram/fw_cfg.h |  1 +
 2 files changed, 11 insertions(+)
hw/nvram/fw_cfg.c +10 -0
index 68eff77..08b5cc3 100644
@@ -484,6 +484,16 @@ void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
     fw_cfg_add_bytes(s, key, copy, sizeof(value));
 }
 
+void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value)
+{
+    uint16_t *copy, *old;
+
+    copy = g_malloc(sizeof(value));
+    *copy = cpu_to_le16(value);
+    old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
+    g_free(old);
+}
+
 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
 {
     uint32_t *copy;
include/hw/nvram/fw_cfg.h +1 -0
index 6d8a8ac..bc6c4a0 100644
@@ -67,6 +67,7 @@ typedef void (*FWCfgReadCallback)(void *opaque, uint32_t offset);
 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len);
 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value);
 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value);
+void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value);
 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value);
 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value);
 void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
Signature · 1 line
2.1.0
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] fw_cfg: add fw_cfg_modify_i16 (update) method

Quoted 11 lines · Gabriel L. Somlo

Allow the ability to modify the value of an existing 16-bit integer fw_cfg item.

---

Couple of thoughts:

1. I'm thinking about pre-emptively creating _i16, _i32, and _i64 versions, but right now (for fixing sparc and ppc) we only need the _i16 version. What to do ?

Presumably, I'll update the documentation to mention these update functions as well (one more reason to do all three versions rather than just _i16) :)

Quoted 50 lines

2. Part of me wants to find the memory location containing the previous value and simply overwrite it, but I'll need to somehow ensure the blob being replaced was of the same size, etc., which could get hairy. So for now I'm going with the paranoid/safe version which allocates a new blob and frees the old one.

Thanks,
Gabriel

 hw/nvram/fw_cfg.c         | 10 ++++++++++
 include/hw/nvram/fw_cfg.h |  1 +
 2 files changed, 11 insertions(+)
hw/nvram/fw_cfg.c +10 -0
index 68eff77..08b5cc3 100644
@@ -484,6 +484,16 @@ void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
     fw_cfg_add_bytes(s, key, copy, sizeof(value));
 }
 
+void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value)
+{
+    uint16_t *copy, *old;
+
+    copy = g_malloc(sizeof(value));
+    *copy = cpu_to_le16(value);
+    old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
+    g_free(old);
+}
+
 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
 {
     uint32_t *copy;
include/hw/nvram/fw_cfg.h +1 -0
index 6d8a8ac..bc6c4a0 100644
@@ -67,6 +67,7 @@ typedef void (*FWCfgReadCallback)(void *opaque, uint32_t offset);
 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len);
 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value);
 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value);
+void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value);
 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value);
 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value);
 void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
Signature · 1 line
2.1.0
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] fw_cfg: add fw_cfg_modify_i16 (update) method

Quoted 9 lines · Gabriel L. Somlo
Quoted 8 lines · Gabriel L. Somlo

Allow the ability to modify the value of an existing 16-bit integer fw_cfg item.

---

Couple of thoughts:

Quoted 7 lines
Quoted 3 lines

1. I'm thinking about pre-emptively creating _i16, _i32, and _i64 versions, but right now (for fixing sparc and ppc) we only need the _i16 version. What to do ?

Presumably, I'll update the documentation to mention these update functions as well (one more reason to do all three versions rather than just _i16) :)

Should be easy to add this once they are actually needed. Documentation update is fine, can go incremental though.

cheers,
Gerd

#4 lore raw

PATCH 2/2 fw_cfg: fix FW_CFG_BOOT_DEVICE update on ppc and sparc

In reply to Gabriel L. Somlo

On ppc, sparc, and sparc64, the value of the FW_CFG_BOOT_DEVICE 16bit fw_cfg entry is repeatedly modified from a series of callbacks, which currently results in the previous value's dynamically allocated memory being leaked.

This patch switches updating to the new fw_cfg_modify_i16() call, which does not cause memory leaks.

---
 hw/ppc/mac_newworld.c | 2 +-
 hw/ppc/mac_oldworld.c | 2 +-
 hw/sparc/sun4m.c      | 2 +-
 hw/sparc64/sun4u.c    | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
hw/ppc/mac_newworld.c +1 -1
index a365bf9..0f3e341 100644
@@ -119,7 +119,7 @@ static const MemoryRegionOps unin_ops = {
 static void fw_cfg_boot_set(void *opaque, const char *boot_device,
                             Error **errp)
 {
-    fw_cfg_add_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
+    fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
 }
 
 static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
hw/ppc/mac_oldworld.c +1 -1
index f26133d..99879dd 100644
@@ -52,7 +52,7 @@
 static void fw_cfg_boot_set(void *opaque, const char *boot_device,
                             Error **errp)
 {
-    fw_cfg_add_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
+    fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
 }
 
 static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
hw/sparc/sun4m.c +1 -1
index 8a3599c..68ac4d8 100644
@@ -124,7 +124,7 @@ void DMA_register_channel (int nchan,
 static void fw_cfg_boot_set(void *opaque, const char *boot_device,
                             Error **errp)
 {
-    fw_cfg_add_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
+    fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
 }
 
 static void nvram_init(Nvram *nvram, uint8_t *macaddr,
hw/sparc64/sun4u.c +1 -1
index 6f34e87..30cfa0e 100644
@@ -127,7 +127,7 @@ void DMA_register_channel (int nchan,
 static void fw_cfg_boot_set(void *opaque, const char *boot_device,
                             Error **errp)
 {
-    fw_cfg_add_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
+    fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
 }
 
 static int sun4u_NVRAM_set_params(Nvram *nvram, uint16_t NVRAM_size,
Signature · 1 line
2.1.0
#5 lore raw
In reply to Gabriel L. Somlo
Quoted 7 lines · Gabriel L. Somlo

The fw_cfg cleanup series exposed a memory leak occurring on ppc and sparc machine types.

This series adds a safe fw_cfg integer-type update function (1/2), and modifies ppc and sparc callbacks to use it instead of the current leaky method (2/2).

Quoted 5 lines

This should preferably go in *before* "fw_cfg: remove support for guest-side data writes", which would otherwise cause certain ppc and sparc command lines to crash.

Once these are reviewed (also, see additional questions in 1/1), should I re-spin the whole set, or keep these two as a separate, independent series?

Keeping separate is fine, I'll order the patches via 'git rebase'.

cheers,
Gerd