Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

firmware: google memconsole driver fixes

The google memconsole driver is currently broken upstream, as it tries
to read memory that is described as reserved in /proc/iomem, by
dereferencing a pointer obtained through phys_to_virt(). This triggers
a kernel fault as such regions are unmapped after early boot.

The proper workaround is to use ioremap_cache() / iounmap() around such
accesses.

As some unrelated changes, I also converted some printks to use pr_info()
and added some missing __init annotations.

Tested: booted dbg build, verified I could read /sys/firmware/log

Signed-off-by: Michel Lespinasse <walken@google.com>
Acked-by: Mike Waychison <mikew@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Michel Lespinasse and committed by
Greg Kroah-Hartman
cb887591 b12b73f1

+26 -21
+26 -21
drivers/firmware/google/memconsole.c
··· 15 15 #include <linux/kobject.h> 16 16 #include <linux/module.h> 17 17 #include <linux/dmi.h> 18 + #include <linux/io.h> 18 19 #include <asm/bios_ebda.h> 19 20 20 21 #define BIOS_MEMCONSOLE_V1_MAGIC 0xDEADBABE ··· 42 41 }; 43 42 } __packed; 44 43 45 - static char *memconsole_baseaddr; 44 + static u32 memconsole_baseaddr; 46 45 static size_t memconsole_length; 47 46 48 47 static ssize_t memconsole_read(struct file *filp, struct kobject *kobp, 49 48 struct bin_attribute *bin_attr, char *buf, 50 49 loff_t pos, size_t count) 51 50 { 52 - return memory_read_from_buffer(buf, count, &pos, memconsole_baseaddr, 53 - memconsole_length); 51 + char *memconsole; 52 + ssize_t ret; 53 + 54 + memconsole = ioremap_cache(memconsole_baseaddr, memconsole_length); 55 + if (!memconsole) { 56 + pr_err("memconsole: ioremap_cache failed\n"); 57 + return -ENOMEM; 58 + } 59 + ret = memory_read_from_buffer(buf, count, &pos, memconsole, 60 + memconsole_length); 61 + iounmap(memconsole); 62 + return ret; 54 63 } 55 64 56 65 static struct bin_attribute memconsole_bin_attr = { ··· 69 58 }; 70 59 71 60 72 - static void found_v1_header(struct biosmemcon_ebda *hdr) 61 + static void __init found_v1_header(struct biosmemcon_ebda *hdr) 73 62 { 74 - printk(KERN_INFO "BIOS console v1 EBDA structure found at %p\n", hdr); 75 - printk(KERN_INFO "BIOS console buffer at 0x%.8x, " 63 + pr_info("BIOS console v1 EBDA structure found at %p\n", hdr); 64 + pr_info("BIOS console buffer at 0x%.8x, " 76 65 "start = %d, end = %d, num = %d\n", 77 66 hdr->v1.buffer_addr, hdr->v1.start, 78 67 hdr->v1.end, hdr->v1.num_chars); 79 68 80 69 memconsole_length = hdr->v1.num_chars; 81 - memconsole_baseaddr = phys_to_virt(hdr->v1.buffer_addr); 70 + memconsole_baseaddr = hdr->v1.buffer_addr; 82 71 } 83 72 84 - static void found_v2_header(struct biosmemcon_ebda *hdr) 73 + static void __init found_v2_header(struct biosmemcon_ebda *hdr) 85 74 { 86 - printk(KERN_INFO "BIOS console v2 EBDA structure found at %p\n", hdr); 87 - printk(KERN_INFO "BIOS console buffer at 0x%.8x, " 75 + pr_info("BIOS console v2 EBDA structure found at %p\n", hdr); 76 + pr_info("BIOS console buffer at 0x%.8x, " 88 77 "start = %d, end = %d, num_bytes = %d\n", 89 78 hdr->v2.buffer_addr, hdr->v2.start, 90 79 hdr->v2.end, hdr->v2.num_bytes); 91 80 92 81 memconsole_length = hdr->v2.end - hdr->v2.start; 93 - memconsole_baseaddr = phys_to_virt(hdr->v2.buffer_addr 94 - + hdr->v2.start); 82 + memconsole_baseaddr = hdr->v2.buffer_addr + hdr->v2.start; 95 83 } 96 84 97 85 /* 98 86 * Search through the EBDA for the BIOS Memory Console, and 99 87 * set the global variables to point to it. Return true if found. 100 88 */ 101 - static bool found_memconsole(void) 89 + static bool __init found_memconsole(void) 102 90 { 103 91 unsigned int address; 104 92 size_t length, cur; 105 93 106 94 address = get_bios_ebda(); 107 95 if (!address) { 108 - printk(KERN_INFO "BIOS EBDA non-existent.\n"); 96 + pr_info("BIOS EBDA non-existent.\n"); 109 97 return false; 110 98 } 111 99 ··· 132 122 } 133 123 } 134 124 135 - printk(KERN_INFO "BIOS console EBDA structure not found!\n"); 125 + pr_info("BIOS console EBDA structure not found!\n"); 136 126 return false; 137 127 } 138 128 ··· 149 139 150 140 static int __init memconsole_init(void) 151 141 { 152 - int ret; 153 - 154 142 if (!dmi_check_system(memconsole_dmi_table)) 155 143 return -ENODEV; 156 144 ··· 156 148 return -ENODEV; 157 149 158 150 memconsole_bin_attr.size = memconsole_length; 159 - 160 - ret = sysfs_create_bin_file(firmware_kobj, &memconsole_bin_attr); 161 - 162 - return ret; 151 + return sysfs_create_bin_file(firmware_kobj, &memconsole_bin_attr); 163 152 } 164 153 165 154 static void __exit memconsole_exit(void)