162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2019 Oracle. All Rights Reserved. 462306a36Sopenharmony_ci * Author: Darrick J. Wong <darrick.wong@oracle.com> 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci#include "xfs.h" 762306a36Sopenharmony_ci#include "xfs_fs.h" 862306a36Sopenharmony_ci#include "xfs_shared.h" 962306a36Sopenharmony_ci#include "xfs_format.h" 1062306a36Sopenharmony_ci#include "xfs_log_format.h" 1162306a36Sopenharmony_ci#include "xfs_trans_resv.h" 1262306a36Sopenharmony_ci#include "xfs_mount.h" 1362306a36Sopenharmony_ci#include "xfs_trace.h" 1462306a36Sopenharmony_ci#include "xfs_sysctl.h" 1562306a36Sopenharmony_ci#include "xfs_pwork.h" 1662306a36Sopenharmony_ci#include <linux/nmi.h> 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci/* 1962306a36Sopenharmony_ci * Parallel Work Queue 2062306a36Sopenharmony_ci * =================== 2162306a36Sopenharmony_ci * 2262306a36Sopenharmony_ci * Abstract away the details of running a large and "obviously" parallelizable 2362306a36Sopenharmony_ci * task across multiple CPUs. Callers initialize the pwork control object with 2462306a36Sopenharmony_ci * a desired level of parallelization and a work function. Next, they embed 2562306a36Sopenharmony_ci * struct xfs_pwork in whatever structure they use to pass work context to a 2662306a36Sopenharmony_ci * worker thread and queue that pwork. The work function will be passed the 2762306a36Sopenharmony_ci * pwork item when it is run (from process context) and any returned error will 2862306a36Sopenharmony_ci * be recorded in xfs_pwork_ctl.error. Work functions should check for errors 2962306a36Sopenharmony_ci * and abort if necessary; the non-zeroness of xfs_pwork_ctl.error does not 3062306a36Sopenharmony_ci * stop workqueue item processing. 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * This is the rough equivalent of the xfsprogs workqueue code, though we can't 3362306a36Sopenharmony_ci * reuse that name here. 3462306a36Sopenharmony_ci */ 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci/* Invoke our caller's function. */ 3762306a36Sopenharmony_cistatic void 3862306a36Sopenharmony_cixfs_pwork_work( 3962306a36Sopenharmony_ci struct work_struct *work) 4062306a36Sopenharmony_ci{ 4162306a36Sopenharmony_ci struct xfs_pwork *pwork; 4262306a36Sopenharmony_ci struct xfs_pwork_ctl *pctl; 4362306a36Sopenharmony_ci int error; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci pwork = container_of(work, struct xfs_pwork, work); 4662306a36Sopenharmony_ci pctl = pwork->pctl; 4762306a36Sopenharmony_ci error = pctl->work_fn(pctl->mp, pwork); 4862306a36Sopenharmony_ci if (error && !pctl->error) 4962306a36Sopenharmony_ci pctl->error = error; 5062306a36Sopenharmony_ci if (atomic_dec_and_test(&pctl->nr_work)) 5162306a36Sopenharmony_ci wake_up(&pctl->poll_wait); 5262306a36Sopenharmony_ci} 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci/* 5562306a36Sopenharmony_ci * Set up control data for parallel work. @work_fn is the function that will 5662306a36Sopenharmony_ci * be called. @tag will be written into the kernel threads. @nr_threads is 5762306a36Sopenharmony_ci * the level of parallelism desired, or 0 for no limit. 5862306a36Sopenharmony_ci */ 5962306a36Sopenharmony_ciint 6062306a36Sopenharmony_cixfs_pwork_init( 6162306a36Sopenharmony_ci struct xfs_mount *mp, 6262306a36Sopenharmony_ci struct xfs_pwork_ctl *pctl, 6362306a36Sopenharmony_ci xfs_pwork_work_fn work_fn, 6462306a36Sopenharmony_ci const char *tag) 6562306a36Sopenharmony_ci{ 6662306a36Sopenharmony_ci unsigned int nr_threads = 0; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci#ifdef DEBUG 6962306a36Sopenharmony_ci if (xfs_globals.pwork_threads >= 0) 7062306a36Sopenharmony_ci nr_threads = xfs_globals.pwork_threads; 7162306a36Sopenharmony_ci#endif 7262306a36Sopenharmony_ci trace_xfs_pwork_init(mp, nr_threads, current->pid); 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci pctl->wq = alloc_workqueue("%s-%d", 7562306a36Sopenharmony_ci WQ_UNBOUND | WQ_SYSFS | WQ_FREEZABLE, nr_threads, tag, 7662306a36Sopenharmony_ci current->pid); 7762306a36Sopenharmony_ci if (!pctl->wq) 7862306a36Sopenharmony_ci return -ENOMEM; 7962306a36Sopenharmony_ci pctl->work_fn = work_fn; 8062306a36Sopenharmony_ci pctl->error = 0; 8162306a36Sopenharmony_ci pctl->mp = mp; 8262306a36Sopenharmony_ci atomic_set(&pctl->nr_work, 0); 8362306a36Sopenharmony_ci init_waitqueue_head(&pctl->poll_wait); 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci return 0; 8662306a36Sopenharmony_ci} 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci/* Queue some parallel work. */ 8962306a36Sopenharmony_civoid 9062306a36Sopenharmony_cixfs_pwork_queue( 9162306a36Sopenharmony_ci struct xfs_pwork_ctl *pctl, 9262306a36Sopenharmony_ci struct xfs_pwork *pwork) 9362306a36Sopenharmony_ci{ 9462306a36Sopenharmony_ci INIT_WORK(&pwork->work, xfs_pwork_work); 9562306a36Sopenharmony_ci pwork->pctl = pctl; 9662306a36Sopenharmony_ci atomic_inc(&pctl->nr_work); 9762306a36Sopenharmony_ci queue_work(pctl->wq, &pwork->work); 9862306a36Sopenharmony_ci} 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci/* Wait for the work to finish and tear down the control structure. */ 10162306a36Sopenharmony_ciint 10262306a36Sopenharmony_cixfs_pwork_destroy( 10362306a36Sopenharmony_ci struct xfs_pwork_ctl *pctl) 10462306a36Sopenharmony_ci{ 10562306a36Sopenharmony_ci destroy_workqueue(pctl->wq); 10662306a36Sopenharmony_ci pctl->wq = NULL; 10762306a36Sopenharmony_ci return pctl->error; 10862306a36Sopenharmony_ci} 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci/* 11162306a36Sopenharmony_ci * Wait for the work to finish by polling completion status and touch the soft 11262306a36Sopenharmony_ci * lockup watchdog. This is for callers such as mount which hold locks. 11362306a36Sopenharmony_ci */ 11462306a36Sopenharmony_civoid 11562306a36Sopenharmony_cixfs_pwork_poll( 11662306a36Sopenharmony_ci struct xfs_pwork_ctl *pctl) 11762306a36Sopenharmony_ci{ 11862306a36Sopenharmony_ci while (wait_event_timeout(pctl->poll_wait, 11962306a36Sopenharmony_ci atomic_read(&pctl->nr_work) == 0, HZ) == 0) 12062306a36Sopenharmony_ci touch_softlockup_watchdog(); 12162306a36Sopenharmony_ci} 122