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

Configure Feed

Select the types of activity you want to include in your feed.

bootconfig: Allocate xbc_data inside xbc_init()

Allocate 'xbc_data' in the xbc_init() so that it does
not need to care about the ownership of the copied
data.

Link: https://lkml.kernel.org/r/163177339986.682366.898762699429769117.stgit@devnote2

Suggested-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>

authored by

Masami Hiramatsu and committed by
Steven Rostedt (VMware)
bdac5c2b 6644c654

+27 -27
+1 -1
include/linux/bootconfig.h
··· 271 271 } 272 272 273 273 /* XBC node initializer */ 274 - int __init xbc_init(char *buf, const char **emsg, int *epos); 274 + int __init xbc_init(const char *buf, size_t size, const char **emsg, int *epos); 275 275 276 276 277 277 /* XBC cleanup data structures */
+2 -11
init/main.c
··· 409 409 const char *msg; 410 410 int pos; 411 411 u32 size, csum; 412 - char *data, *copy, *err; 412 + char *data, *err; 413 413 int ret; 414 414 415 415 /* Cut out the bootconfig data even if we have no bootconfig option */ ··· 442 442 return; 443 443 } 444 444 445 - copy = memblock_alloc(size + 1, SMP_CACHE_BYTES); 446 - if (!copy) { 447 - pr_err("Failed to allocate memory for bootconfig\n"); 448 - return; 449 - } 450 - 451 - memcpy(copy, data, size); 452 - copy[size] = '\0'; 453 - 454 - ret = xbc_init(copy, &msg, &pos); 445 + ret = xbc_init(data, size, &msg, &pos); 455 446 if (ret < 0) { 456 447 if (pos < 0) 457 448 pr_err("Failed to init bootconfig: %s.\n", msg);
+21 -12
lib/bootconfig.c
··· 789 789 */ 790 790 void __init xbc_destroy_all(void) 791 791 { 792 + memblock_free_ptr(xbc_data, xbc_data_size); 792 793 xbc_data = NULL; 793 794 xbc_data_size = 0; 794 795 xbc_node_num = 0; ··· 800 799 801 800 /** 802 801 * xbc_init() - Parse given XBC file and build XBC internal tree 803 - * @buf: boot config text 802 + * @data: The boot config text original data 803 + * @size: The size of @data 804 804 * @emsg: A pointer of const char * to store the error message 805 805 * @epos: A pointer of int to store the error position 806 806 * 807 - * This parses the boot config text in @buf. @buf must be a 808 - * null terminated string and smaller than XBC_DATA_MAX. 807 + * This parses the boot config text in @data. @size must be smaller 808 + * than XBC_DATA_MAX. 809 809 * Return the number of stored nodes (>0) if succeeded, or -errno 810 810 * if there is any error. 811 811 * In error cases, @emsg will be updated with an error message and 812 812 * @epos will be updated with the error position which is the byte offset 813 813 * of @buf. If the error is not a parser error, @epos will be -1. 814 814 */ 815 - int __init xbc_init(char *buf, const char **emsg, int *epos) 815 + int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos) 816 816 { 817 817 char *p, *q; 818 818 int ret, c; ··· 826 824 *emsg = "Bootconfig is already initialized"; 827 825 return -EBUSY; 828 826 } 829 - 830 - ret = strlen(buf); 831 - if (ret > XBC_DATA_MAX - 1 || ret == 0) { 827 + if (size > XBC_DATA_MAX || size == 0) { 832 828 if (emsg) 833 - *emsg = ret ? "Config data is too big" : 829 + *emsg = size ? "Config data is too big" : 834 830 "Config data is empty"; 835 831 return -ERANGE; 836 832 } 833 + 834 + xbc_data = memblock_alloc(size + 1, SMP_CACHE_BYTES); 835 + if (!xbc_data) { 836 + if (emsg) 837 + *emsg = "Failed to allocate bootconfig data"; 838 + return -ENOMEM; 839 + } 840 + memcpy(xbc_data, data, size); 841 + xbc_data[size] = '\0'; 842 + xbc_data_size = size + 1; 837 843 838 844 xbc_nodes = memblock_alloc(sizeof(struct xbc_node) * XBC_NODE_MAX, 839 845 SMP_CACHE_BYTES); 840 846 if (!xbc_nodes) { 841 847 if (emsg) 842 848 *emsg = "Failed to allocate bootconfig nodes"; 849 + xbc_destroy_all(); 843 850 return -ENOMEM; 844 851 } 845 852 memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX); 846 - xbc_data = buf; 847 - xbc_data_size = ret + 1; 848 - last_parent = NULL; 849 853 850 - p = buf; 854 + last_parent = NULL; 855 + p = xbc_data; 851 856 do { 852 857 q = strpbrk(p, "{}=+;:\n#"); 853 858 if (!q) {
+3 -3
tools/bootconfig/main.c
··· 229 229 return -EINVAL; 230 230 } 231 231 232 - ret = xbc_init(*buf, &msg, NULL); 232 + ret = xbc_init(*buf, size, &msg, NULL); 233 233 /* Wrong data */ 234 234 if (ret < 0) { 235 235 pr_err("parse error: %s.\n", msg); ··· 269 269 if (!copy) 270 270 return -ENOMEM; 271 271 272 - ret = xbc_init(buf, &msg, &pos); 272 + ret = xbc_init(buf, len, &msg, &pos); 273 273 if (ret < 0) 274 274 show_xbc_error(copy, msg, pos); 275 275 free(copy); ··· 382 382 memcpy(data, buf, size); 383 383 384 384 /* Check the data format */ 385 - ret = xbc_init(buf, &msg, &pos); 385 + ret = xbc_init(buf, size, &msg, &pos); 386 386 if (ret < 0) { 387 387 show_xbc_error(data, msg, pos); 388 388 free(data);