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

kthread: add kthread_stop_put

Add a kthread_stop_put() helper that stops a thread and puts its task
struct. Use it to replace the various instances of kthread_stop()
followed by put_task_struct().

Remove the kthread_stop_put() macro in usbip that is similar but doesn't
return the result of kthread_stop().

[agruenba@redhat.com: fix kerneldoc comment]
Link: https://lkml.kernel.org/r/20230911111730.2565537-1-agruenba@redhat.com
[akpm@linux-foundation.org: document kthread_stop_put()'s argument]
Link: https://lkml.kernel.org/r/20230907234048.2499820-1-agruenba@redhat.com
Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Andreas Gruenbacher and committed by
Andrew Morton
6309727e ed5378a3

+38 -46
+1 -2
drivers/accel/ivpu/ivpu_job.c
··· 618 618 619 619 void ivpu_job_done_thread_fini(struct ivpu_device *vdev) 620 620 { 621 - kthread_stop(vdev->job_done_thread); 622 - put_task_struct(vdev->job_done_thread); 621 + kthread_stop_put(vdev->job_done_thread); 623 622 }
+4 -8
drivers/dma-buf/st-dma-fence-chain.c
··· 476 476 for (i = 0; i < ncpus; i++) { 477 477 int ret; 478 478 479 - ret = kthread_stop(threads[i]); 479 + ret = kthread_stop_put(threads[i]); 480 480 if (ret && !err) 481 481 err = ret; 482 - put_task_struct(threads[i]); 483 482 } 484 483 kfree(threads); 485 484 ··· 590 591 for (i = 0; i < fc.chain_length; i++) 591 592 dma_fence_signal(fc.fences[i]); 592 593 593 - err = kthread_stop(tsk); 594 - put_task_struct(tsk); 594 + err = kthread_stop_put(tsk); 595 595 596 596 err: 597 597 fence_chains_fini(&fc); ··· 619 621 for (i = fc.chain_length; i--; ) 620 622 dma_fence_signal(fc.fences[i]); 621 623 622 - err = kthread_stop(tsk); 623 - put_task_struct(tsk); 624 + err = kthread_stop_put(tsk); 624 625 625 626 err: 626 627 fence_chains_fini(&fc); ··· 666 669 for (i = 0; i < fc.chain_length; i++) 667 670 dma_fence_signal(fc.fences[i]); 668 671 669 - err = kthread_stop(tsk); 670 - put_task_struct(tsk); 672 + err = kthread_stop_put(tsk); 671 673 672 674 err: 673 675 fence_chains_fini(&fc);
+1 -3
drivers/dma-buf/st-dma-fence.c
··· 548 548 for (i = 0; i < ARRAY_SIZE(t); i++) { 549 549 int err; 550 550 551 - err = kthread_stop(t[i].task); 551 + err = kthread_stop_put(t[i].task); 552 552 if (err && !ret) 553 553 ret = err; 554 - 555 - put_task_struct(t[i].task); 556 554 } 557 555 } 558 556
+1 -3
drivers/gpu/drm/i915/gt/selftest_migrate.c
··· 719 719 if (IS_ERR_OR_NULL(tsk)) 720 720 continue; 721 721 722 - status = kthread_stop(tsk); 722 + status = kthread_stop_put(tsk); 723 723 if (status && !err) 724 724 err = status; 725 - 726 - put_task_struct(tsk); 727 725 } 728 726 729 727 kfree(thread);
+1 -2
drivers/net/xen-netback/interface.c
··· 672 672 static void xenvif_disconnect_queue(struct xenvif_queue *queue) 673 673 { 674 674 if (queue->task) { 675 - kthread_stop(queue->task); 676 - put_task_struct(queue->task); 675 + kthread_stop_put(queue->task); 677 676 queue->task = NULL; 678 677 } 679 678
-6
drivers/usb/usbip/usbip_common.h
··· 298 298 __k; \ 299 299 }) 300 300 301 - #define kthread_stop_put(k) \ 302 - do { \ 303 - kthread_stop(k); \ 304 - put_task_struct(k); \ 305 - } while (0) 306 - 307 301 /* usbip_common.c */ 308 302 void usbip_dump_urb(struct urb *purb); 309 303 void usbip_dump_header(struct usbip_header *pdu);
+3 -6
fs/gfs2/ops_fstype.c
··· 1126 1126 return 0; 1127 1127 1128 1128 fail: 1129 - kthread_stop(sdp->sd_logd_process); 1130 - put_task_struct(sdp->sd_logd_process); 1129 + kthread_stop_put(sdp->sd_logd_process); 1131 1130 sdp->sd_logd_process = NULL; 1132 1131 return error; 1133 1132 } ··· 1134 1135 void gfs2_destroy_threads(struct gfs2_sbd *sdp) 1135 1136 { 1136 1137 if (sdp->sd_logd_process) { 1137 - kthread_stop(sdp->sd_logd_process); 1138 - put_task_struct(sdp->sd_logd_process); 1138 + kthread_stop_put(sdp->sd_logd_process); 1139 1139 sdp->sd_logd_process = NULL; 1140 1140 } 1141 1141 if (sdp->sd_quotad_process) { 1142 - kthread_stop(sdp->sd_quotad_process); 1143 - put_task_struct(sdp->sd_quotad_process); 1142 + kthread_stop_put(sdp->sd_quotad_process); 1144 1143 sdp->sd_quotad_process = NULL; 1145 1144 } 1146 1145 }
+1
include/linux/kthread.h
··· 86 86 void kthread_bind(struct task_struct *k, unsigned int cpu); 87 87 void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask); 88 88 int kthread_stop(struct task_struct *k); 89 + int kthread_stop_put(struct task_struct *k); 89 90 bool kthread_should_stop(void); 90 91 bool kthread_should_park(void); 91 92 bool kthread_should_stop_or_park(void);
+5 -10
kernel/irq/manage.c
··· 1852 1852 struct task_struct *t = new->thread; 1853 1853 1854 1854 new->thread = NULL; 1855 - kthread_stop(t); 1856 - put_task_struct(t); 1855 + kthread_stop_put(t); 1857 1856 } 1858 1857 if (new->secondary && new->secondary->thread) { 1859 1858 struct task_struct *t = new->secondary->thread; 1860 1859 1861 1860 new->secondary->thread = NULL; 1862 - kthread_stop(t); 1863 - put_task_struct(t); 1861 + kthread_stop_put(t); 1864 1862 } 1865 1863 out_mput: 1866 1864 module_put(desc->owner); ··· 1969 1971 * the same bit to a newly requested action. 1970 1972 */ 1971 1973 if (action->thread) { 1972 - kthread_stop(action->thread); 1973 - put_task_struct(action->thread); 1974 - if (action->secondary && action->secondary->thread) { 1975 - kthread_stop(action->secondary->thread); 1976 - put_task_struct(action->secondary->thread); 1977 - } 1974 + kthread_stop_put(action->thread); 1975 + if (action->secondary && action->secondary->thread) 1976 + kthread_stop_put(action->secondary->thread); 1978 1977 } 1979 1978 1980 1979 /* Last action releases resources */
+18
kernel/kthread.c
··· 715 715 } 716 716 EXPORT_SYMBOL(kthread_stop); 717 717 718 + /** 719 + * kthread_stop_put - stop a thread and put its task struct 720 + * @k: thread created by kthread_create(). 721 + * 722 + * Stops a thread created by kthread_create() and put its task_struct. 723 + * Only use when holding an extra task struct reference obtained by 724 + * calling get_task_struct(). 725 + */ 726 + int kthread_stop_put(struct task_struct *k) 727 + { 728 + int ret; 729 + 730 + ret = kthread_stop(k); 731 + put_task_struct(k); 732 + return ret; 733 + } 734 + EXPORT_SYMBOL(kthread_stop_put); 735 + 718 736 int kthreadd(void *unused) 719 737 { 720 738 struct task_struct *tsk = current;
+1 -2
kernel/smpboot.c
··· 272 272 struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu); 273 273 274 274 if (tsk) { 275 - kthread_stop(tsk); 276 - put_task_struct(tsk); 275 + kthread_stop_put(tsk); 277 276 *per_cpu_ptr(ht->store, cpu) = NULL; 278 277 } 279 278 }
+1 -2
mm/damon/core.c
··· 699 699 if (tsk) { 700 700 get_task_struct(tsk); 701 701 mutex_unlock(&ctx->kdamond_lock); 702 - kthread_stop(tsk); 703 - put_task_struct(tsk); 702 + kthread_stop_put(tsk); 704 703 return 0; 705 704 } 706 705 mutex_unlock(&ctx->kdamond_lock);
+1 -2
net/core/pktgen.c
··· 3982 3982 list_for_each_safe(q, n, &list) { 3983 3983 t = list_entry(q, struct pktgen_thread, th_list); 3984 3984 list_del(&t->th_list); 3985 - kthread_stop(t->tsk); 3986 - put_task_struct(t->tsk); 3985 + kthread_stop_put(t->tsk); 3987 3986 kfree(t); 3988 3987 } 3989 3988