Re: [PATCH v11 4/4] fw_cfg: write vmcoreinfo details

2018-02-02 Thread Marc-Andre Lureau
Hi

On Fri, Feb 2, 2018 at 3:44 AM, Michael S. Tsirkin  wrote:
> On Thu, Feb 01, 2018 at 02:03:00PM +0100, Marc-André Lureau wrote:
>> @@ -314,6 +359,37 @@ struct fw_cfg_sysfs_entry {
>>   struct device *dev;
>>  };
>>
>> +#ifdef CONFIG_CRASH_CORE
>> +static ssize_t write_vmcoreinfo(struct device *dev, const struct 
>> fw_cfg_file *f)
>> +{
>> + struct vmci {
>> + __le16 host_format;
>> + __le16 guest_format;
>> + __le32 size;
>> + __le64 paddr;
>> + } __packed;
>> + static struct vmci *data;
>> + ssize_t ret;
>> +
>> + data = kmalloc(sizeof(struct vmci), GFP_KERNEL);
>> + if (!data)
>> + return -ENOMEM;
>> +
>> + *data = (struct vmci) {
>> + .guest_format = cpu_to_le16(VMCOREINFO_FORMAT_ELF),
>> + .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
>> + .paddr = cpu_to_le64(paddr_vmcoreinfo_note())
>> + };
>> + /* spare ourself reading host format support for now since we
>> +  * don't know what else to format - host may ignore ours
>> +  */
>> + ret = fw_cfg_write_blob(dev, f->select, data, 0, sizeof(struct vmci));
>> +
>> + kfree(data);
>> + return ret;
>> +}
>> +#endif /* CONFIG_CRASH_CORE */
>> +
>>  /* get fw_cfg_sysfs_entry from kobject member */
>>  static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
>>  {
>
> kmalloc during crash is I think a bad idea.
> How about preallocating on probe?

It doesn't kmalloc during crash, it kmalloc during boot. As such, I
don't see a reason to change it.

>
> diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c
> index 33e0256..ffd81d1 100644
> --- a/drivers/firmware/qemu_fw_cfg.c
> +++ b/drivers/firmware/qemu_fw_cfg.c
> @@ -363,22 +363,33 @@ struct fw_cfg_sysfs_entry {
>  };
>
>  #ifdef CONFIG_CRASH_CORE
> +
> +struct vmci {
> +   __le16 host_format;
> +   __le16 guest_format;
> +   __le32 size;
> +   __le64 paddr;
> +} *fw_cfg_vmcore_data;
> +
> +static int fw_cfg_vmcore_init(void)
> +{
> +   fw_cfg_vmcore_data = kmalloc(sizeof(*fw_cfg_vmcore_data), GFP_KERNEL);
> +   if (!fw_cfg_vmcore_data)
> +   return -ENOMEM;
> +   return 0;
> +}
> +
> +static int fw_cfg_vmcore_cleanup(void)
> +{
> +   kfree(fw_cfg_vmcore_data);
> +}
> +
>  static ssize_t write_vmcoreinfo(struct device *dev, const struct fw_cfg_file 
> *f)
>  {
> -   struct vmci {
> -   __le16 host_format;
> -   __le16 guest_format;
> -   __le32 size;
> -   __le64 paddr;
> -   } __packed;
> static struct vmci *data;
> ssize_t ret;
>
> -   data = kmalloc(sizeof(struct vmci), GFP_KERNEL);
> -   if (!data)
> -   return -ENOMEM;
> -
> -   *data = (struct vmci) {
> +   *fw_cfg_vmcore_data = (struct vmci) {
> .guest_format = cpu_to_le16(VMCOREINFO_FORMAT_ELF),
> .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
> .paddr = cpu_to_le64(paddr_vmcoreinfo_note())
> @@ -386,11 +397,23 @@ static ssize_t write_vmcoreinfo(struct device *dev, 
> const struct fw_cfg_file *f)
> /* spare ourself reading host format support for now since we
>  * don't know what else to format - host may ignore ours
>  */
> -   ret = fw_cfg_write_blob(dev, f->select, data, 0, sizeof(struct vmci));
> +   ret = fw_cfg_write_blob(dev, f->select, fw_cfg_vmcore_data, 0,
> +   sizeof(*fw_cfg_vmcore_data));
>
> -   kfree(data);
> return ret;
>  }
> +
> +#else
> +
> +static int fw_cfg_vmcore_init(void)
> +{
> +   return 0;
> +}
> +
> +static int fw_cfg_vmcore_cleanup(void)
> +{
> +}
> +
>  #endif /* CONFIG_CRASH_CORE */
>
>  /* get fw_cfg_sysfs_entry from kobject member */
> @@ -725,11 +748,15 @@ static int fw_cfg_sysfs_probe(struct platform_device 
> *pdev)
> if (fw_cfg_sel_ko)
> return -EBUSY;
>
> +   err = fw_cfg_vmcore_init();
> +   if (err)
> +   goto err_sel;
> +
> /* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */
> err = -ENOMEM;
> fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko);
> if (!fw_cfg_sel_ko)
> -   goto err_sel;
> +   goto err_vmcore;
> fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, 
> fw_cfg_top_ko);
> if (!fw_cfg_fname_kset)
> goto err_name;
> @@ -764,6 +791,8 @@ static int fw_cfg_sysfs_probe(struct platform_device 
> *pdev)
> fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
>  err_name:
> fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
> +err_vmcore:
> +   fw_cfg_vmcore_cleanup();
>  err_sel:
> return err;
>  }
> @@ -776,6 +805,7 @@ static int fw_cfg_sysfs_remove(struct platform_device 
> *pdev)
> fw_cfg_io_cleanup();
> 

Re: [PATCH v11 4/4] fw_cfg: write vmcoreinfo details

2018-02-02 Thread Marc-Andre Lureau
Hi

On Fri, Feb 2, 2018 at 3:44 AM, Michael S. Tsirkin  wrote:
> On Thu, Feb 01, 2018 at 02:03:00PM +0100, Marc-André Lureau wrote:
>> @@ -314,6 +359,37 @@ struct fw_cfg_sysfs_entry {
>>   struct device *dev;
>>  };
>>
>> +#ifdef CONFIG_CRASH_CORE
>> +static ssize_t write_vmcoreinfo(struct device *dev, const struct 
>> fw_cfg_file *f)
>> +{
>> + struct vmci {
>> + __le16 host_format;
>> + __le16 guest_format;
>> + __le32 size;
>> + __le64 paddr;
>> + } __packed;
>> + static struct vmci *data;
>> + ssize_t ret;
>> +
>> + data = kmalloc(sizeof(struct vmci), GFP_KERNEL);
>> + if (!data)
>> + return -ENOMEM;
>> +
>> + *data = (struct vmci) {
>> + .guest_format = cpu_to_le16(VMCOREINFO_FORMAT_ELF),
>> + .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
>> + .paddr = cpu_to_le64(paddr_vmcoreinfo_note())
>> + };
>> + /* spare ourself reading host format support for now since we
>> +  * don't know what else to format - host may ignore ours
>> +  */
>> + ret = fw_cfg_write_blob(dev, f->select, data, 0, sizeof(struct vmci));
>> +
>> + kfree(data);
>> + return ret;
>> +}
>> +#endif /* CONFIG_CRASH_CORE */
>> +
>>  /* get fw_cfg_sysfs_entry from kobject member */
>>  static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
>>  {
>
> kmalloc during crash is I think a bad idea.
> How about preallocating on probe?

It doesn't kmalloc during crash, it kmalloc during boot. As such, I
don't see a reason to change it.

>
> diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c
> index 33e0256..ffd81d1 100644
> --- a/drivers/firmware/qemu_fw_cfg.c
> +++ b/drivers/firmware/qemu_fw_cfg.c
> @@ -363,22 +363,33 @@ struct fw_cfg_sysfs_entry {
>  };
>
>  #ifdef CONFIG_CRASH_CORE
> +
> +struct vmci {
> +   __le16 host_format;
> +   __le16 guest_format;
> +   __le32 size;
> +   __le64 paddr;
> +} *fw_cfg_vmcore_data;
> +
> +static int fw_cfg_vmcore_init(void)
> +{
> +   fw_cfg_vmcore_data = kmalloc(sizeof(*fw_cfg_vmcore_data), GFP_KERNEL);
> +   if (!fw_cfg_vmcore_data)
> +   return -ENOMEM;
> +   return 0;
> +}
> +
> +static int fw_cfg_vmcore_cleanup(void)
> +{
> +   kfree(fw_cfg_vmcore_data);
> +}
> +
>  static ssize_t write_vmcoreinfo(struct device *dev, const struct fw_cfg_file 
> *f)
>  {
> -   struct vmci {
> -   __le16 host_format;
> -   __le16 guest_format;
> -   __le32 size;
> -   __le64 paddr;
> -   } __packed;
> static struct vmci *data;
> ssize_t ret;
>
> -   data = kmalloc(sizeof(struct vmci), GFP_KERNEL);
> -   if (!data)
> -   return -ENOMEM;
> -
> -   *data = (struct vmci) {
> +   *fw_cfg_vmcore_data = (struct vmci) {
> .guest_format = cpu_to_le16(VMCOREINFO_FORMAT_ELF),
> .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
> .paddr = cpu_to_le64(paddr_vmcoreinfo_note())
> @@ -386,11 +397,23 @@ static ssize_t write_vmcoreinfo(struct device *dev, 
> const struct fw_cfg_file *f)
> /* spare ourself reading host format support for now since we
>  * don't know what else to format - host may ignore ours
>  */
> -   ret = fw_cfg_write_blob(dev, f->select, data, 0, sizeof(struct vmci));
> +   ret = fw_cfg_write_blob(dev, f->select, fw_cfg_vmcore_data, 0,
> +   sizeof(*fw_cfg_vmcore_data));
>
> -   kfree(data);
> return ret;
>  }
> +
> +#else
> +
> +static int fw_cfg_vmcore_init(void)
> +{
> +   return 0;
> +}
> +
> +static int fw_cfg_vmcore_cleanup(void)
> +{
> +}
> +
>  #endif /* CONFIG_CRASH_CORE */
>
>  /* get fw_cfg_sysfs_entry from kobject member */
> @@ -725,11 +748,15 @@ static int fw_cfg_sysfs_probe(struct platform_device 
> *pdev)
> if (fw_cfg_sel_ko)
> return -EBUSY;
>
> +   err = fw_cfg_vmcore_init();
> +   if (err)
> +   goto err_sel;
> +
> /* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */
> err = -ENOMEM;
> fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko);
> if (!fw_cfg_sel_ko)
> -   goto err_sel;
> +   goto err_vmcore;
> fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, 
> fw_cfg_top_ko);
> if (!fw_cfg_fname_kset)
> goto err_name;
> @@ -764,6 +791,8 @@ static int fw_cfg_sysfs_probe(struct platform_device 
> *pdev)
> fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
>  err_name:
> fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
> +err_vmcore:
> +   fw_cfg_vmcore_cleanup();
>  err_sel:
> return err;
>  }
> @@ -776,6 +805,7 @@ static int fw_cfg_sysfs_remove(struct platform_device 
> *pdev)
> fw_cfg_io_cleanup();
> fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
>   

Re: [PATCH v11 4/4] fw_cfg: write vmcoreinfo details

2018-02-01 Thread Michael S. Tsirkin
On Thu, Feb 01, 2018 at 02:03:00PM +0100, Marc-André Lureau wrote:
> @@ -314,6 +359,37 @@ struct fw_cfg_sysfs_entry {
>   struct device *dev;
>  };
>  
> +#ifdef CONFIG_CRASH_CORE
> +static ssize_t write_vmcoreinfo(struct device *dev, const struct fw_cfg_file 
> *f)
> +{
> + struct vmci {
> + __le16 host_format;
> + __le16 guest_format;
> + __le32 size;
> + __le64 paddr;
> + } __packed;
> + static struct vmci *data;
> + ssize_t ret;
> +
> + data = kmalloc(sizeof(struct vmci), GFP_KERNEL);
> + if (!data)
> + return -ENOMEM;
> +
> + *data = (struct vmci) {
> + .guest_format = cpu_to_le16(VMCOREINFO_FORMAT_ELF),
> + .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
> + .paddr = cpu_to_le64(paddr_vmcoreinfo_note())
> + };
> + /* spare ourself reading host format support for now since we
> +  * don't know what else to format - host may ignore ours
> +  */
> + ret = fw_cfg_write_blob(dev, f->select, data, 0, sizeof(struct vmci));
> +
> + kfree(data);
> + return ret;
> +}
> +#endif /* CONFIG_CRASH_CORE */
> +
>  /* get fw_cfg_sysfs_entry from kobject member */
>  static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
>  {

kmalloc during crash is I think a bad idea.
How about preallocating on probe?

diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c
index 33e0256..ffd81d1 100644
--- a/drivers/firmware/qemu_fw_cfg.c
+++ b/drivers/firmware/qemu_fw_cfg.c
@@ -363,22 +363,33 @@ struct fw_cfg_sysfs_entry {
 };
 
 #ifdef CONFIG_CRASH_CORE
+
+struct vmci {
+   __le16 host_format;
+   __le16 guest_format;
+   __le32 size;
+   __le64 paddr;
+} *fw_cfg_vmcore_data;
+
+static int fw_cfg_vmcore_init(void)
+{
+   fw_cfg_vmcore_data = kmalloc(sizeof(*fw_cfg_vmcore_data), GFP_KERNEL);
+   if (!fw_cfg_vmcore_data)
+   return -ENOMEM;
+   return 0;
+}
+
+static int fw_cfg_vmcore_cleanup(void)
+{
+   kfree(fw_cfg_vmcore_data);
+}
+
 static ssize_t write_vmcoreinfo(struct device *dev, const struct fw_cfg_file 
*f)
 {
-   struct vmci {
-   __le16 host_format;
-   __le16 guest_format;
-   __le32 size;
-   __le64 paddr;
-   } __packed;
static struct vmci *data;
ssize_t ret;
 
-   data = kmalloc(sizeof(struct vmci), GFP_KERNEL);
-   if (!data)
-   return -ENOMEM;
-
-   *data = (struct vmci) {
+   *fw_cfg_vmcore_data = (struct vmci) {
.guest_format = cpu_to_le16(VMCOREINFO_FORMAT_ELF),
.size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
.paddr = cpu_to_le64(paddr_vmcoreinfo_note())
@@ -386,11 +397,23 @@ static ssize_t write_vmcoreinfo(struct device *dev, const 
struct fw_cfg_file *f)
/* spare ourself reading host format support for now since we
 * don't know what else to format - host may ignore ours
 */
-   ret = fw_cfg_write_blob(dev, f->select, data, 0, sizeof(struct vmci));
+   ret = fw_cfg_write_blob(dev, f->select, fw_cfg_vmcore_data, 0,
+   sizeof(*fw_cfg_vmcore_data));
 
-   kfree(data);
return ret;
 }
+
+#else
+
+static int fw_cfg_vmcore_init(void)
+{
+   return 0;
+}
+
+static int fw_cfg_vmcore_cleanup(void)
+{
+}
+
 #endif /* CONFIG_CRASH_CORE */
 
 /* get fw_cfg_sysfs_entry from kobject member */
@@ -725,11 +748,15 @@ static int fw_cfg_sysfs_probe(struct platform_device 
*pdev)
if (fw_cfg_sel_ko)
return -EBUSY;
 
+   err = fw_cfg_vmcore_init();
+   if (err)
+   goto err_sel;
+
/* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */
err = -ENOMEM;
fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko);
if (!fw_cfg_sel_ko)
-   goto err_sel;
+   goto err_vmcore;
fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, fw_cfg_top_ko);
if (!fw_cfg_fname_kset)
goto err_name;
@@ -764,6 +791,8 @@ static int fw_cfg_sysfs_probe(struct platform_device *pdev)
fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
 err_name:
fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
+err_vmcore:
+   fw_cfg_vmcore_cleanup();
 err_sel:
return err;
 }
@@ -776,6 +805,7 @@ static int fw_cfg_sysfs_remove(struct platform_device *pdev)
fw_cfg_io_cleanup();
fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
+   fw_cfg_vmcore_cleanup();
return 0;
 }
 


Re: [PATCH v11 4/4] fw_cfg: write vmcoreinfo details

2018-02-01 Thread Michael S. Tsirkin
On Thu, Feb 01, 2018 at 02:03:00PM +0100, Marc-André Lureau wrote:
> @@ -314,6 +359,37 @@ struct fw_cfg_sysfs_entry {
>   struct device *dev;
>  };
>  
> +#ifdef CONFIG_CRASH_CORE
> +static ssize_t write_vmcoreinfo(struct device *dev, const struct fw_cfg_file 
> *f)
> +{
> + struct vmci {
> + __le16 host_format;
> + __le16 guest_format;
> + __le32 size;
> + __le64 paddr;
> + } __packed;
> + static struct vmci *data;
> + ssize_t ret;
> +
> + data = kmalloc(sizeof(struct vmci), GFP_KERNEL);
> + if (!data)
> + return -ENOMEM;
> +
> + *data = (struct vmci) {
> + .guest_format = cpu_to_le16(VMCOREINFO_FORMAT_ELF),
> + .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
> + .paddr = cpu_to_le64(paddr_vmcoreinfo_note())
> + };
> + /* spare ourself reading host format support for now since we
> +  * don't know what else to format - host may ignore ours
> +  */
> + ret = fw_cfg_write_blob(dev, f->select, data, 0, sizeof(struct vmci));
> +
> + kfree(data);
> + return ret;
> +}
> +#endif /* CONFIG_CRASH_CORE */
> +
>  /* get fw_cfg_sysfs_entry from kobject member */
>  static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
>  {

kmalloc during crash is I think a bad idea.
How about preallocating on probe?

diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c
index 33e0256..ffd81d1 100644
--- a/drivers/firmware/qemu_fw_cfg.c
+++ b/drivers/firmware/qemu_fw_cfg.c
@@ -363,22 +363,33 @@ struct fw_cfg_sysfs_entry {
 };
 
 #ifdef CONFIG_CRASH_CORE
+
+struct vmci {
+   __le16 host_format;
+   __le16 guest_format;
+   __le32 size;
+   __le64 paddr;
+} *fw_cfg_vmcore_data;
+
+static int fw_cfg_vmcore_init(void)
+{
+   fw_cfg_vmcore_data = kmalloc(sizeof(*fw_cfg_vmcore_data), GFP_KERNEL);
+   if (!fw_cfg_vmcore_data)
+   return -ENOMEM;
+   return 0;
+}
+
+static int fw_cfg_vmcore_cleanup(void)
+{
+   kfree(fw_cfg_vmcore_data);
+}
+
 static ssize_t write_vmcoreinfo(struct device *dev, const struct fw_cfg_file 
*f)
 {
-   struct vmci {
-   __le16 host_format;
-   __le16 guest_format;
-   __le32 size;
-   __le64 paddr;
-   } __packed;
static struct vmci *data;
ssize_t ret;
 
-   data = kmalloc(sizeof(struct vmci), GFP_KERNEL);
-   if (!data)
-   return -ENOMEM;
-
-   *data = (struct vmci) {
+   *fw_cfg_vmcore_data = (struct vmci) {
.guest_format = cpu_to_le16(VMCOREINFO_FORMAT_ELF),
.size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
.paddr = cpu_to_le64(paddr_vmcoreinfo_note())
@@ -386,11 +397,23 @@ static ssize_t write_vmcoreinfo(struct device *dev, const 
struct fw_cfg_file *f)
/* spare ourself reading host format support for now since we
 * don't know what else to format - host may ignore ours
 */
-   ret = fw_cfg_write_blob(dev, f->select, data, 0, sizeof(struct vmci));
+   ret = fw_cfg_write_blob(dev, f->select, fw_cfg_vmcore_data, 0,
+   sizeof(*fw_cfg_vmcore_data));
 
-   kfree(data);
return ret;
 }
+
+#else
+
+static int fw_cfg_vmcore_init(void)
+{
+   return 0;
+}
+
+static int fw_cfg_vmcore_cleanup(void)
+{
+}
+
 #endif /* CONFIG_CRASH_CORE */
 
 /* get fw_cfg_sysfs_entry from kobject member */
@@ -725,11 +748,15 @@ static int fw_cfg_sysfs_probe(struct platform_device 
*pdev)
if (fw_cfg_sel_ko)
return -EBUSY;
 
+   err = fw_cfg_vmcore_init();
+   if (err)
+   goto err_sel;
+
/* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */
err = -ENOMEM;
fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko);
if (!fw_cfg_sel_ko)
-   goto err_sel;
+   goto err_vmcore;
fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, fw_cfg_top_ko);
if (!fw_cfg_fname_kset)
goto err_name;
@@ -764,6 +791,8 @@ static int fw_cfg_sysfs_probe(struct platform_device *pdev)
fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
 err_name:
fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
+err_vmcore:
+   fw_cfg_vmcore_cleanup();
 err_sel:
return err;
 }
@@ -776,6 +805,7 @@ static int fw_cfg_sysfs_remove(struct platform_device *pdev)
fw_cfg_io_cleanup();
fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
+   fw_cfg_vmcore_cleanup();
return 0;
 }
 


[PATCH v11 4/4] fw_cfg: write vmcoreinfo details

2018-02-01 Thread Marc-André Lureau
If the "etc/vmcoreinfo" fw_cfg file is present and we are not running
the kdump kernel, write the addr/size of the vmcoreinfo ELF note.

Signed-off-by: Marc-André Lureau 
Reviewed-by: Gabriel Somlo 
---
 drivers/firmware/qemu_fw_cfg.c | 84 ++
 1 file changed, 84 insertions(+)

diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c
index 686f0e839858..3b3cf6222c97 100644
--- a/drivers/firmware/qemu_fw_cfg.c
+++ b/drivers/firmware/qemu_fw_cfg.c
@@ -34,6 +34,8 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 
 MODULE_AUTHOR("Gabriel L. Somlo ");
 MODULE_DESCRIPTION("QEMU fw_cfg sysfs support");
@@ -57,6 +59,8 @@ MODULE_LICENSE("GPL");
 /* fw_cfg "file name" is up to 56 characters (including terminating nul) */
 #define FW_CFG_MAX_FILE_PATH 56
 
+#define VMCOREINFO_FORMAT_ELF 0x1
+
 /* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */
 static u32 fw_cfg_rev;
 
@@ -195,6 +199,47 @@ static ssize_t fw_cfg_read_blob(struct device *dev, u16 
key,
return ret;
 }
 
+#ifdef CONFIG_CRASH_CORE
+/* write chunk of given fw_cfg blob (caller responsible for sanity-check) */
+static ssize_t fw_cfg_write_blob(struct device *dev, u16 key,
+void *buf, loff_t pos, size_t count)
+{
+   u32 glk = -1U;
+   acpi_status status;
+   ssize_t ret = count;
+
+   /* If we have ACPI, ensure mutual exclusion against any potential
+* device access by the firmware, e.g. via AML methods:
+*/
+   status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, );
+   if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
+   /* Should never get here */
+   WARN(1, "%s: Failed to lock ACPI!\n", __func__);
+   return -EINVAL;
+   }
+
+   mutex_lock(_cfg_dev_lock);
+   if (pos == 0) {
+   ret = fw_cfg_dma_transfer(dev, buf, count, key << 16
+ | FW_CFG_DMA_CTL_SELECT
+ | FW_CFG_DMA_CTL_WRITE);
+   } else {
+   iowrite16(fw_cfg_sel_endianness(key), fw_cfg_reg_ctrl);
+   ret = fw_cfg_dma_transfer(dev, NULL, pos, FW_CFG_DMA_CTL_SKIP);
+   if (ret < 0)
+   goto end;
+   ret = fw_cfg_dma_transfer(dev, buf, count, 
FW_CFG_DMA_CTL_WRITE);
+   }
+
+end:
+   mutex_unlock(_cfg_dev_lock);
+
+   acpi_release_global_lock(glk);
+
+   return ret;
+}
+#endif /* CONFIG_CRASH_CORE */
+
 /* clean up fw_cfg device i/o */
 static void fw_cfg_io_cleanup(void)
 {
@@ -314,6 +359,37 @@ struct fw_cfg_sysfs_entry {
struct device *dev;
 };
 
+#ifdef CONFIG_CRASH_CORE
+static ssize_t write_vmcoreinfo(struct device *dev, const struct fw_cfg_file 
*f)
+{
+   struct vmci {
+   __le16 host_format;
+   __le16 guest_format;
+   __le32 size;
+   __le64 paddr;
+   } __packed;
+   static struct vmci *data;
+   ssize_t ret;
+
+   data = kmalloc(sizeof(struct vmci), GFP_KERNEL);
+   if (!data)
+   return -ENOMEM;
+
+   *data = (struct vmci) {
+   .guest_format = cpu_to_le16(VMCOREINFO_FORMAT_ELF),
+   .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
+   .paddr = cpu_to_le64(paddr_vmcoreinfo_note())
+   };
+   /* spare ourself reading host format support for now since we
+* don't know what else to format - host may ignore ours
+*/
+   ret = fw_cfg_write_blob(dev, f->select, data, 0, sizeof(struct vmci));
+
+   kfree(data);
+   return ret;
+}
+#endif /* CONFIG_CRASH_CORE */
+
 /* get fw_cfg_sysfs_entry from kobject member */
 static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
 {
@@ -554,6 +630,14 @@ static int fw_cfg_register_file(struct device *dev, const 
struct fw_cfg_file *f)
int err;
struct fw_cfg_sysfs_entry *entry;
 
+#ifdef CONFIG_CRASH_CORE
+   if (fw_cfg_dma_enabled() &&
+   strcmp(f->name, "etc/vmcoreinfo") == 0 && !is_kdump_kernel()) {
+   if (write_vmcoreinfo(dev, f) < 0)
+   pr_warn("fw_cfg: failed to write vmcoreinfo");
+   }
+#endif
+
/* allocate new entry */
entry = kzalloc(sizeof(*entry), GFP_KERNEL);
if (!entry)
-- 
2.16.0.rc1.1.gef27df75a1



[PATCH v11 4/4] fw_cfg: write vmcoreinfo details

2018-02-01 Thread Marc-André Lureau
If the "etc/vmcoreinfo" fw_cfg file is present and we are not running
the kdump kernel, write the addr/size of the vmcoreinfo ELF note.

Signed-off-by: Marc-André Lureau 
Reviewed-by: Gabriel Somlo 
---
 drivers/firmware/qemu_fw_cfg.c | 84 ++
 1 file changed, 84 insertions(+)

diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c
index 686f0e839858..3b3cf6222c97 100644
--- a/drivers/firmware/qemu_fw_cfg.c
+++ b/drivers/firmware/qemu_fw_cfg.c
@@ -34,6 +34,8 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 
 MODULE_AUTHOR("Gabriel L. Somlo ");
 MODULE_DESCRIPTION("QEMU fw_cfg sysfs support");
@@ -57,6 +59,8 @@ MODULE_LICENSE("GPL");
 /* fw_cfg "file name" is up to 56 characters (including terminating nul) */
 #define FW_CFG_MAX_FILE_PATH 56
 
+#define VMCOREINFO_FORMAT_ELF 0x1
+
 /* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */
 static u32 fw_cfg_rev;
 
@@ -195,6 +199,47 @@ static ssize_t fw_cfg_read_blob(struct device *dev, u16 
key,
return ret;
 }
 
+#ifdef CONFIG_CRASH_CORE
+/* write chunk of given fw_cfg blob (caller responsible for sanity-check) */
+static ssize_t fw_cfg_write_blob(struct device *dev, u16 key,
+void *buf, loff_t pos, size_t count)
+{
+   u32 glk = -1U;
+   acpi_status status;
+   ssize_t ret = count;
+
+   /* If we have ACPI, ensure mutual exclusion against any potential
+* device access by the firmware, e.g. via AML methods:
+*/
+   status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, );
+   if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
+   /* Should never get here */
+   WARN(1, "%s: Failed to lock ACPI!\n", __func__);
+   return -EINVAL;
+   }
+
+   mutex_lock(_cfg_dev_lock);
+   if (pos == 0) {
+   ret = fw_cfg_dma_transfer(dev, buf, count, key << 16
+ | FW_CFG_DMA_CTL_SELECT
+ | FW_CFG_DMA_CTL_WRITE);
+   } else {
+   iowrite16(fw_cfg_sel_endianness(key), fw_cfg_reg_ctrl);
+   ret = fw_cfg_dma_transfer(dev, NULL, pos, FW_CFG_DMA_CTL_SKIP);
+   if (ret < 0)
+   goto end;
+   ret = fw_cfg_dma_transfer(dev, buf, count, 
FW_CFG_DMA_CTL_WRITE);
+   }
+
+end:
+   mutex_unlock(_cfg_dev_lock);
+
+   acpi_release_global_lock(glk);
+
+   return ret;
+}
+#endif /* CONFIG_CRASH_CORE */
+
 /* clean up fw_cfg device i/o */
 static void fw_cfg_io_cleanup(void)
 {
@@ -314,6 +359,37 @@ struct fw_cfg_sysfs_entry {
struct device *dev;
 };
 
+#ifdef CONFIG_CRASH_CORE
+static ssize_t write_vmcoreinfo(struct device *dev, const struct fw_cfg_file 
*f)
+{
+   struct vmci {
+   __le16 host_format;
+   __le16 guest_format;
+   __le32 size;
+   __le64 paddr;
+   } __packed;
+   static struct vmci *data;
+   ssize_t ret;
+
+   data = kmalloc(sizeof(struct vmci), GFP_KERNEL);
+   if (!data)
+   return -ENOMEM;
+
+   *data = (struct vmci) {
+   .guest_format = cpu_to_le16(VMCOREINFO_FORMAT_ELF),
+   .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
+   .paddr = cpu_to_le64(paddr_vmcoreinfo_note())
+   };
+   /* spare ourself reading host format support for now since we
+* don't know what else to format - host may ignore ours
+*/
+   ret = fw_cfg_write_blob(dev, f->select, data, 0, sizeof(struct vmci));
+
+   kfree(data);
+   return ret;
+}
+#endif /* CONFIG_CRASH_CORE */
+
 /* get fw_cfg_sysfs_entry from kobject member */
 static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
 {
@@ -554,6 +630,14 @@ static int fw_cfg_register_file(struct device *dev, const 
struct fw_cfg_file *f)
int err;
struct fw_cfg_sysfs_entry *entry;
 
+#ifdef CONFIG_CRASH_CORE
+   if (fw_cfg_dma_enabled() &&
+   strcmp(f->name, "etc/vmcoreinfo") == 0 && !is_kdump_kernel()) {
+   if (write_vmcoreinfo(dev, f) < 0)
+   pr_warn("fw_cfg: failed to write vmcoreinfo");
+   }
+#endif
+
/* allocate new entry */
entry = kzalloc(sizeof(*entry), GFP_KERNEL);
if (!entry)
-- 
2.16.0.rc1.1.gef27df75a1