Skip to content

Commit bdac5c2

Browse files
mhiramatrostedt
authored andcommitted
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>
1 parent 6644c65 commit bdac5c2

4 files changed

Lines changed: 27 additions & 27 deletions

File tree

‎include/linux/bootconfig.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ static inline int __init xbc_node_compose_key(struct xbc_node *node,
271271
}
272272

273273
/* 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);
275275

276276

277277
/* XBC cleanup data structures */

‎init/main.c‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ static void __init setup_boot_config(void)
409409
const char *msg;
410410
int pos;
411411
u32 size, csum;
412-
char *data, *copy, *err;
412+
char *data, *err;
413413
int ret;
414414

415415
/* Cut out the bootconfig data even if we have no bootconfig option */
@@ -442,16 +442,7 @@ static void __init setup_boot_config(void)
442442
return;
443443
}
444444

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);
455446
if (ret < 0) {
456447
if (pos < 0)
457448
pr_err("Failed to init bootconfig: %s.\n", msg);

‎lib/bootconfig.c‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ static int __init xbc_verify_tree(void)
789789
*/
790790
void __init xbc_destroy_all(void)
791791
{
792+
memblock_free_ptr(xbc_data, xbc_data_size);
792793
xbc_data = NULL;
793794
xbc_data_size = 0;
794795
xbc_node_num = 0;
@@ -799,19 +800,20 @@ void __init xbc_destroy_all(void)
799800

800801
/**
801802
* xbc_init() - Parse given XBC file and build XBC internal tree
802-
* @buf: boot config text
803+
* @data: The boot config text original data
804+
* @size: The size of @data
803805
* @emsg: A pointer of const char * to store the error message
804806
* @epos: A pointer of int to store the error position
805807
*
806-
* This parses the boot config text in @buf. @buf must be a
807-
* null terminated string and smaller than XBC_DATA_MAX.
808+
* This parses the boot config text in @data. @size must be smaller
809+
* than XBC_DATA_MAX.
808810
* Return the number of stored nodes (>0) if succeeded, or -errno
809811
* if there is any error.
810812
* In error cases, @emsg will be updated with an error message and
811813
* @epos will be updated with the error position which is the byte offset
812814
* of @buf. If the error is not a parser error, @epos will be -1.
813815
*/
814-
int __init xbc_init(char *buf, const char **emsg, int *epos)
816+
int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
815817
{
816818
char *p, *q;
817819
int ret, c;
@@ -824,28 +826,35 @@ int __init xbc_init(char *buf, const char **emsg, int *epos)
824826
*emsg = "Bootconfig is already initialized";
825827
return -EBUSY;
826828
}
827-
828-
ret = strlen(buf);
829-
if (ret > XBC_DATA_MAX - 1 || ret == 0) {
829+
if (size > XBC_DATA_MAX || size == 0) {
830830
if (emsg)
831-
*emsg = ret ? "Config data is too big" :
831+
*emsg = size ? "Config data is too big" :
832832
"Config data is empty";
833833
return -ERANGE;
834834
}
835835

836+
xbc_data = memblock_alloc(size + 1, SMP_CACHE_BYTES);
837+
if (!xbc_data) {
838+
if (emsg)
839+
*emsg = "Failed to allocate bootconfig data";
840+
return -ENOMEM;
841+
}
842+
memcpy(xbc_data, data, size);
843+
xbc_data[size] = '\0';
844+
xbc_data_size = size + 1;
845+
836846
xbc_nodes = memblock_alloc(sizeof(struct xbc_node) * XBC_NODE_MAX,
837847
SMP_CACHE_BYTES);
838848
if (!xbc_nodes) {
839849
if (emsg)
840850
*emsg = "Failed to allocate bootconfig nodes";
851+
xbc_destroy_all();
841852
return -ENOMEM;
842853
}
843854
memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
844-
xbc_data = buf;
845-
xbc_data_size = ret + 1;
846-
last_parent = NULL;
847855

848-
p = buf;
856+
last_parent = NULL;
857+
p = xbc_data;
849858
do {
850859
q = strpbrk(p, "{}=+;:\n#");
851860
if (!q) {

‎tools/bootconfig/main.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
229229
return -EINVAL;
230230
}
231231

232-
ret = xbc_init(*buf, &msg, NULL);
232+
ret = xbc_init(*buf, size, &msg, NULL);
233233
/* Wrong data */
234234
if (ret < 0) {
235235
pr_err("parse error: %s.\n", msg);
@@ -269,7 +269,7 @@ static int init_xbc_with_error(char *buf, int len)
269269
if (!copy)
270270
return -ENOMEM;
271271

272-
ret = xbc_init(buf, &msg, &pos);
272+
ret = xbc_init(buf, len, &msg, &pos);
273273
if (ret < 0)
274274
show_xbc_error(copy, msg, pos);
275275
free(copy);
@@ -382,7 +382,7 @@ static int apply_xbc(const char *path, const char *xbc_path)
382382
memcpy(data, buf, size);
383383

384384
/* Check the data format */
385-
ret = xbc_init(buf, &msg, &pos);
385+
ret = xbc_init(buf, size, &msg, &pos);
386386
if (ret < 0) {
387387
show_xbc_error(data, msg, pos);
388388
free(data);

0 commit comments

Comments
 (0)