xref: /openbmc/linux/fs/xfs/xfs_pwork.c (revision f83d436a)
140786717SDarrick J. Wong // SPDX-License-Identifier: GPL-2.0-or-later
240786717SDarrick J. Wong /*
340786717SDarrick J. Wong  * Copyright (C) 2019 Oracle.  All Rights Reserved.
440786717SDarrick J. Wong  * Author: Darrick J. Wong <darrick.wong@oracle.com>
540786717SDarrick J. Wong  */
640786717SDarrick J. Wong #include "xfs.h"
740786717SDarrick J. Wong #include "xfs_fs.h"
840786717SDarrick J. Wong #include "xfs_shared.h"
940786717SDarrick J. Wong #include "xfs_format.h"
1040786717SDarrick J. Wong #include "xfs_log_format.h"
1140786717SDarrick J. Wong #include "xfs_trans_resv.h"
1240786717SDarrick J. Wong #include "xfs_mount.h"
1340786717SDarrick J. Wong #include "xfs_trace.h"
1440786717SDarrick J. Wong #include "xfs_sysctl.h"
1540786717SDarrick J. Wong #include "xfs_pwork.h"
163e5a428bSDarrick J. Wong #include <linux/nmi.h>
1740786717SDarrick J. Wong 
1840786717SDarrick J. Wong /*
1940786717SDarrick J. Wong  * Parallel Work Queue
2040786717SDarrick J. Wong  * ===================
2140786717SDarrick J. Wong  *
2240786717SDarrick J. Wong  * Abstract away the details of running a large and "obviously" parallelizable
2340786717SDarrick J. Wong  * task across multiple CPUs.  Callers initialize the pwork control object with
2440786717SDarrick J. Wong  * a desired level of parallelization and a work function.  Next, they embed
2540786717SDarrick J. Wong  * struct xfs_pwork in whatever structure they use to pass work context to a
2640786717SDarrick J. Wong  * worker thread and queue that pwork.  The work function will be passed the
2740786717SDarrick J. Wong  * pwork item when it is run (from process context) and any returned error will
2840786717SDarrick J. Wong  * be recorded in xfs_pwork_ctl.error.  Work functions should check for errors
2940786717SDarrick J. Wong  * and abort if necessary; the non-zeroness of xfs_pwork_ctl.error does not
3040786717SDarrick J. Wong  * stop workqueue item processing.
3140786717SDarrick J. Wong  *
3240786717SDarrick J. Wong  * This is the rough equivalent of the xfsprogs workqueue code, though we can't
3340786717SDarrick J. Wong  * reuse that name here.
3440786717SDarrick J. Wong  */
3540786717SDarrick J. Wong 
3640786717SDarrick J. Wong /* Invoke our caller's function. */
3740786717SDarrick J. Wong static void
xfs_pwork_work(struct work_struct * work)3840786717SDarrick J. Wong xfs_pwork_work(
3940786717SDarrick J. Wong 	struct work_struct	*work)
4040786717SDarrick J. Wong {
4140786717SDarrick J. Wong 	struct xfs_pwork	*pwork;
4240786717SDarrick J. Wong 	struct xfs_pwork_ctl	*pctl;
4340786717SDarrick J. Wong 	int			error;
4440786717SDarrick J. Wong 
4540786717SDarrick J. Wong 	pwork = container_of(work, struct xfs_pwork, work);
4640786717SDarrick J. Wong 	pctl = pwork->pctl;
4740786717SDarrick J. Wong 	error = pctl->work_fn(pctl->mp, pwork);
4840786717SDarrick J. Wong 	if (error && !pctl->error)
4940786717SDarrick J. Wong 		pctl->error = error;
503e5a428bSDarrick J. Wong 	if (atomic_dec_and_test(&pctl->nr_work))
513e5a428bSDarrick J. Wong 		wake_up(&pctl->poll_wait);
5240786717SDarrick J. Wong }
5340786717SDarrick J. Wong 
5440786717SDarrick J. Wong /*
5540786717SDarrick J. Wong  * Set up control data for parallel work.  @work_fn is the function that will
5640786717SDarrick J. Wong  * be called.  @tag will be written into the kernel threads.  @nr_threads is
5740786717SDarrick J. Wong  * the level of parallelism desired, or 0 for no limit.
5840786717SDarrick J. Wong  */
5940786717SDarrick J. Wong int
xfs_pwork_init(struct xfs_mount * mp,struct xfs_pwork_ctl * pctl,xfs_pwork_work_fn work_fn,const char * tag)6040786717SDarrick J. Wong xfs_pwork_init(
6140786717SDarrick J. Wong 	struct xfs_mount	*mp,
6240786717SDarrick J. Wong 	struct xfs_pwork_ctl	*pctl,
6340786717SDarrick J. Wong 	xfs_pwork_work_fn	work_fn,
64*f83d436aSDarrick J. Wong 	const char		*tag)
6540786717SDarrick J. Wong {
66*f83d436aSDarrick J. Wong 	unsigned int		nr_threads = 0;
67*f83d436aSDarrick J. Wong 
6840786717SDarrick J. Wong #ifdef DEBUG
6940786717SDarrick J. Wong 	if (xfs_globals.pwork_threads >= 0)
7040786717SDarrick J. Wong 		nr_threads = xfs_globals.pwork_threads;
7140786717SDarrick J. Wong #endif
7240786717SDarrick J. Wong 	trace_xfs_pwork_init(mp, nr_threads, current->pid);
7340786717SDarrick J. Wong 
74*f83d436aSDarrick J. Wong 	pctl->wq = alloc_workqueue("%s-%d",
75*f83d436aSDarrick J. Wong 			WQ_UNBOUND | WQ_SYSFS | WQ_FREEZABLE, nr_threads, tag,
7640786717SDarrick J. Wong 			current->pid);
7740786717SDarrick J. Wong 	if (!pctl->wq)
7840786717SDarrick J. Wong 		return -ENOMEM;
7940786717SDarrick J. Wong 	pctl->work_fn = work_fn;
8040786717SDarrick J. Wong 	pctl->error = 0;
8140786717SDarrick J. Wong 	pctl->mp = mp;
823e5a428bSDarrick J. Wong 	atomic_set(&pctl->nr_work, 0);
833e5a428bSDarrick J. Wong 	init_waitqueue_head(&pctl->poll_wait);
8440786717SDarrick J. Wong 
8540786717SDarrick J. Wong 	return 0;
8640786717SDarrick J. Wong }
8740786717SDarrick J. Wong 
8840786717SDarrick J. Wong /* Queue some parallel work. */
8940786717SDarrick J. Wong void
xfs_pwork_queue(struct xfs_pwork_ctl * pctl,struct xfs_pwork * pwork)9040786717SDarrick J. Wong xfs_pwork_queue(
9140786717SDarrick J. Wong 	struct xfs_pwork_ctl	*pctl,
9240786717SDarrick J. Wong 	struct xfs_pwork	*pwork)
9340786717SDarrick J. Wong {
9440786717SDarrick J. Wong 	INIT_WORK(&pwork->work, xfs_pwork_work);
9540786717SDarrick J. Wong 	pwork->pctl = pctl;
963e5a428bSDarrick J. Wong 	atomic_inc(&pctl->nr_work);
9740786717SDarrick J. Wong 	queue_work(pctl->wq, &pwork->work);
9840786717SDarrick J. Wong }
9940786717SDarrick J. Wong 
10040786717SDarrick J. Wong /* Wait for the work to finish and tear down the control structure. */
10140786717SDarrick J. Wong int
xfs_pwork_destroy(struct xfs_pwork_ctl * pctl)10240786717SDarrick J. Wong xfs_pwork_destroy(
10340786717SDarrick J. Wong 	struct xfs_pwork_ctl	*pctl)
10440786717SDarrick J. Wong {
10540786717SDarrick J. Wong 	destroy_workqueue(pctl->wq);
10640786717SDarrick J. Wong 	pctl->wq = NULL;
10740786717SDarrick J. Wong 	return pctl->error;
10840786717SDarrick J. Wong }
10940786717SDarrick J. Wong 
11040786717SDarrick J. Wong /*
1113e5a428bSDarrick J. Wong  * Wait for the work to finish by polling completion status and touch the soft
1123e5a428bSDarrick J. Wong  * lockup watchdog.  This is for callers such as mount which hold locks.
1133e5a428bSDarrick J. Wong  */
1143e5a428bSDarrick J. Wong void
xfs_pwork_poll(struct xfs_pwork_ctl * pctl)1153e5a428bSDarrick J. Wong xfs_pwork_poll(
1163e5a428bSDarrick J. Wong 	struct xfs_pwork_ctl	*pctl)
1173e5a428bSDarrick J. Wong {
1183e5a428bSDarrick J. Wong 	while (wait_event_timeout(pctl->poll_wait,
1193e5a428bSDarrick J. Wong 				atomic_read(&pctl->nr_work) == 0, HZ) == 0)
1203e5a428bSDarrick J. Wong 		touch_softlockup_watchdog();
1213e5a428bSDarrick J. Wong }
122