Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * padata.h - header for the padata parallelization interface
0004  *
0005  * Copyright (C) 2008, 2009 secunet Security Networks AG
0006  * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
0007  *
0008  * Copyright (c) 2020 Oracle and/or its affiliates.
0009  * Author: Daniel Jordan <daniel.m.jordan@oracle.com>
0010  */
0011 
0012 #ifndef PADATA_H
0013 #define PADATA_H
0014 
0015 #include <linux/refcount.h>
0016 #include <linux/compiler_types.h>
0017 #include <linux/workqueue.h>
0018 #include <linux/spinlock.h>
0019 #include <linux/list.h>
0020 #include <linux/kobject.h>
0021 
0022 #define PADATA_CPU_SERIAL   0x01
0023 #define PADATA_CPU_PARALLEL 0x02
0024 
0025 /**
0026  * struct padata_priv - Represents one job
0027  *
0028  * @list: List entry, to attach to the padata lists.
0029  * @pd: Pointer to the internal control structure.
0030  * @cb_cpu: Callback cpu for serializatioon.
0031  * @seq_nr: Sequence number of the parallelized data object.
0032  * @info: Used to pass information from the parallel to the serial function.
0033  * @parallel: Parallel execution function.
0034  * @serial: Serial complete function.
0035  */
0036 struct padata_priv {
0037     struct list_head    list;
0038     struct parallel_data    *pd;
0039     int         cb_cpu;
0040     unsigned int        seq_nr;
0041     int         info;
0042     void                    (*parallel)(struct padata_priv *padata);
0043     void                    (*serial)(struct padata_priv *padata);
0044 };
0045 
0046 /**
0047  * struct padata_list - one per work type per CPU
0048  *
0049  * @list: List head.
0050  * @lock: List lock.
0051  */
0052 struct padata_list {
0053     struct list_head        list;
0054     spinlock_t              lock;
0055 };
0056 
0057 /**
0058 * struct padata_serial_queue - The percpu padata serial queue
0059 *
0060 * @serial: List to wait for serialization after reordering.
0061 * @work: work struct for serialization.
0062 * @pd: Backpointer to the internal control structure.
0063 */
0064 struct padata_serial_queue {
0065        struct padata_list    serial;
0066        struct work_struct    work;
0067        struct parallel_data *pd;
0068 };
0069 
0070 /**
0071  * struct padata_cpumask - The cpumasks for the parallel/serial workers
0072  *
0073  * @pcpu: cpumask for the parallel workers.
0074  * @cbcpu: cpumask for the serial (callback) workers.
0075  */
0076 struct padata_cpumask {
0077     cpumask_var_t   pcpu;
0078     cpumask_var_t   cbcpu;
0079 };
0080 
0081 /**
0082  * struct parallel_data - Internal control structure, covers everything
0083  * that depends on the cpumask in use.
0084  *
0085  * @ps: padata_shell object.
0086  * @reorder_list: percpu reorder lists
0087  * @squeue: percpu padata queues used for serialuzation.
0088  * @refcnt: Number of objects holding a reference on this parallel_data.
0089  * @seq_nr: Sequence number of the parallelized data object.
0090  * @processed: Number of already processed objects.
0091  * @cpu: Next CPU to be processed.
0092  * @cpumask: The cpumasks in use for parallel and serial workers.
0093  * @reorder_work: work struct for reordering.
0094  * @lock: Reorder lock.
0095  */
0096 struct parallel_data {
0097     struct padata_shell     *ps;
0098     struct padata_list      __percpu *reorder_list;
0099     struct padata_serial_queue  __percpu *squeue;
0100     refcount_t          refcnt;
0101     unsigned int            seq_nr;
0102     unsigned int            processed;
0103     int             cpu;
0104     struct padata_cpumask       cpumask;
0105     struct work_struct      reorder_work;
0106     spinlock_t                      ____cacheline_aligned lock;
0107 };
0108 
0109 /**
0110  * struct padata_shell - Wrapper around struct parallel_data, its
0111  * purpose is to allow the underlying control structure to be replaced
0112  * on the fly using RCU.
0113  *
0114  * @pinst: padat instance.
0115  * @pd: Actual parallel_data structure which may be substituted on the fly.
0116  * @opd: Pointer to old pd to be freed by padata_replace.
0117  * @list: List entry in padata_instance list.
0118  */
0119 struct padata_shell {
0120     struct padata_instance      *pinst;
0121     struct parallel_data __rcu  *pd;
0122     struct parallel_data        *opd;
0123     struct list_head        list;
0124 };
0125 
0126 /**
0127  * struct padata_mt_job - represents one multithreaded job
0128  *
0129  * @thread_fn: Called for each chunk of work that a padata thread does.
0130  * @fn_arg: The thread function argument.
0131  * @start: The start of the job (units are job-specific).
0132  * @size: size of this node's work (units are job-specific).
0133  * @align: Ranges passed to the thread function fall on this boundary, with the
0134  *         possible exceptions of the beginning and end of the job.
0135  * @min_chunk: The minimum chunk size in job-specific units.  This allows
0136  *             the client to communicate the minimum amount of work that's
0137  *             appropriate for one worker thread to do at once.
0138  * @max_threads: Max threads to use for the job, actual number may be less
0139  *               depending on task size and minimum chunk size.
0140  */
0141 struct padata_mt_job {
0142     void (*thread_fn)(unsigned long start, unsigned long end, void *arg);
0143     void            *fn_arg;
0144     unsigned long       start;
0145     unsigned long       size;
0146     unsigned long       align;
0147     unsigned long       min_chunk;
0148     int         max_threads;
0149 };
0150 
0151 /**
0152  * struct padata_instance - The overall control structure.
0153  *
0154  * @cpu_online_node: Linkage for CPU online callback.
0155  * @cpu_dead_node: Linkage for CPU offline callback.
0156  * @parallel_wq: The workqueue used for parallel work.
0157  * @serial_wq: The workqueue used for serial work.
0158  * @pslist: List of padata_shell objects attached to this instance.
0159  * @cpumask: User supplied cpumasks for parallel and serial works.
0160  * @kobj: padata instance kernel object.
0161  * @lock: padata instance lock.
0162  * @flags: padata flags.
0163  */
0164 struct padata_instance {
0165     struct hlist_node       cpu_online_node;
0166     struct hlist_node       cpu_dead_node;
0167     struct workqueue_struct     *parallel_wq;
0168     struct workqueue_struct     *serial_wq;
0169     struct list_head        pslist;
0170     struct padata_cpumask       cpumask;
0171     struct kobject                   kobj;
0172     struct mutex             lock;
0173     u8               flags;
0174 #define PADATA_INIT 1
0175 #define PADATA_RESET    2
0176 #define PADATA_INVALID  4
0177 };
0178 
0179 #ifdef CONFIG_PADATA
0180 extern void __init padata_init(void);
0181 #else
0182 static inline void __init padata_init(void) {}
0183 #endif
0184 
0185 extern struct padata_instance *padata_alloc(const char *name);
0186 extern void padata_free(struct padata_instance *pinst);
0187 extern struct padata_shell *padata_alloc_shell(struct padata_instance *pinst);
0188 extern void padata_free_shell(struct padata_shell *ps);
0189 extern int padata_do_parallel(struct padata_shell *ps,
0190                   struct padata_priv *padata, int *cb_cpu);
0191 extern void padata_do_serial(struct padata_priv *padata);
0192 extern void __init padata_do_multithreaded(struct padata_mt_job *job);
0193 extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
0194                   cpumask_var_t cpumask);
0195 #endif