dwarf_loader: Don't crash when a worker thread can't be created
PATCH 1/1 dwarf_loader: Don't crash when a worker thread can't be created
From: Trond Myklebust <[email protected]>
dwarf_cus__process_cus() stores the return value of pthread_create() in dcus->error, which the workers that are already running read. If the creation of a worker fails with EAGAIN, dwarf_cus__nextcu() returns 11 to a running worker, dwarf_loader__decode_next_cu() only treats negative values as errors, and the worker dereferences a NULL dcu. If the first pthread_create() fails, pahole exits with status 0 without encoding anything.
Kernel builds hit this intermittently, since pahole gets the make job count as -j and many pahole processes run at once during module BTF generation:
./scripts/gen-btf.sh: line 70: ... Segmentation fault (core dumped) ${PAHOLE} -J ...The job queue doesn't depend on the number of workers, so carry on with the workers that did start, and do the work in the calling thread if none did. Also stop storing a positive ENOMEM in dcus->error, and treat any value other than 0 and 1 from dwarf_cus__nextcu() as an error.
--- dwarf_loader.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-)
dwarf_loader.c +24 -12
@@ -5217,8 +5217,8 @@ static int dwarf_cus__nextcu(struct dwarf_cus *dcus, struct dwarf_cu **dcu,if (ret ==0 && *cu_die !=NULL ) { *dcu =dwarf_cus__create_cu (dcus, *cu_die, *pointer_size);if (*dcu ==NULL ) { - dcus->error =ENOMEM ; - ret =-1 ; + dcus->error = -ENOMEM ; + ret = -ENOMEM ; goto out_unlock; }// Do it here to keep all CUs in cus->cus in the same
@@ -5241,10 +5241,10 @@ static struct cu *dwarf_loader__decode_next_cu(struct dwarf_cus *dcus) err =dwarf_cus__nextcu (dcus, &dcu, &die_mem, &cu_die, &pointer_size, &offset_size); -if (err <0 ) - goto out_error; -else if (err ==1 )/* no more CUs */ +if (err ==1 )/* no more CUs */ return NULL ; +else if (err) + goto out_error; err =die__process_and_recode (cu_die, dcu->cu, dcus->conf);if (err)
@@ -5370,17 +5370,29 @@ static int dwarf_cus__process_cus(struct dwarf_cus *dcus)list_add (&job->node, &cus_processing_queue.jobs); } +/* Workers that are already running read dcus->error, so don't store +* pthread_create() errors there. The queue doesn't depend on the +* number of workers: carry on with the ones that did start, or do +* the work in this thread if none did. +*/ for (i =0 ; i < nr_workers; ++i) { - dcus->error =pthread_create (&workers[i],NULL , - dwarf_loader__worker_thread, - dcus); -if (dcus->error) - goto out_join; +int err =pthread_create (&workers[i],NULL , + dwarf_loader__worker_thread, dcus); + +if (err) { +fprintf (stderr,"pahole: started %d of %d DWARF loader threads: %s\n" , + i, nr_workers,strerror (err)); +break ; + } } - dcus->error =0 ; +if (i ==0 ) { +void *res =dwarf_loader__worker_thread (dcus); + +if (res !=NULL ) + dcus->error = (long )res; + } -out_join:while (--i >=0 ) {void *res;int err =pthread_join (workers[i], &res);
Signature · 1 line
2.56.1
Quoted 16 lines · Trond Myklebust
From: Trond Myklebust <[email protected]>
dwarf_cus__process_cus()stores the return value ofpthread_create()indcus->error, which the workers that are already running read. If the creation of a worker fails with EAGAIN,dwarf_cus__nextcu()returns 11 to a running worker,dwarf_loader__decode_next_cu()only treats negative values as errors, and the worker dereferences a NULL dcu. If the firstpthread_create()fails, pahole exits with status 0 without encoding anything.Kernel builds hit this intermittently, since pahole gets the make job count as -j and many pahole processes run at once during module BTF generation:
./scripts/gen-btf.sh: line 70: ... Segmentation fault (core dumped) ${PAHOLE} -J ...
Quoted 8 lines
The job queue doesn't depend on the number of workers, so carry on with the workers that did start, and do the work in the calling thread if none did. Also stop storing a positive ENOMEM in
dcus->error, and treat any value other than 0 and 1 fromdwarf_cus__nextcu()as an error.Fixes 4c916259c3e7 ("dwarf_loader: Multithreading with a job/worker model")Assisted-by Claude:claude-opus-5-5Signed-off-by Trond Myklebust <[email protected]>
Thanks for the fix! Applied. I've put together a test to cover this scenario to avoid regressions, will send it shortly.
Alan