padata.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. The padata parallel execution mechanism
  2. Last updated for 2.6.34
  3. Padata is a mechanism by which the kernel can farm work out to be done in
  4. parallel on multiple CPUs while retaining the ordering of tasks. It was
  5. developed for use with the IPsec code, which needs to be able to perform
  6. encryption and decryption on large numbers of packets without reordering
  7. those packets. The crypto developers made a point of writing padata in a
  8. sufficiently general fashion that it could be put to other uses as well.
  9. The first step in using padata is to set up a padata_instance structure for
  10. overall control of how tasks are to be run:
  11. #include <linux/padata.h>
  12. struct padata_instance *padata_alloc(const struct cpumask *cpumask,
  13. struct workqueue_struct *wq);
  14. The cpumask describes which processors will be used to execute work
  15. submitted to this instance. The workqueue wq is where the work will
  16. actually be done; it should be a multithreaded queue, naturally.
  17. There are functions for enabling and disabling the instance:
  18. void padata_start(struct padata_instance *pinst);
  19. void padata_stop(struct padata_instance *pinst);
  20. These functions literally do nothing beyond setting or clearing the
  21. "padata_start() was called" flag; if that flag is not set, other functions
  22. will refuse to work.
  23. The list of CPUs to be used can be adjusted with these functions:
  24. int padata_set_cpumask(struct padata_instance *pinst,
  25. cpumask_var_t cpumask);
  26. int padata_add_cpu(struct padata_instance *pinst, int cpu);
  27. int padata_remove_cpu(struct padata_instance *pinst, int cpu);
  28. Changing the CPU mask has the look of an expensive operation, though, so it
  29. probably should not be done with great frequency.
  30. Actually submitting work to the padata instance requires the creation of a
  31. padata_priv structure:
  32. struct padata_priv {
  33. /* Other stuff here... */
  34. void (*parallel)(struct padata_priv *padata);
  35. void (*serial)(struct padata_priv *padata);
  36. };
  37. This structure will almost certainly be embedded within some larger
  38. structure specific to the work to be done. Most its fields are private to
  39. padata, but the structure should be zeroed at initialization time, and the
  40. parallel() and serial() functions should be provided. Those functions will
  41. be called in the process of getting the work done as we will see
  42. momentarily.
  43. The submission of work is done with:
  44. int padata_do_parallel(struct padata_instance *pinst,
  45. struct padata_priv *padata, int cb_cpu);
  46. The pinst and padata structures must be set up as described above; cb_cpu
  47. specifies which CPU will be used for the final callback when the work is
  48. done; it must be in the current instance's CPU mask. The return value from
  49. padata_do_parallel() is a little strange; zero is an error return
  50. indicating that the caller forgot the padata_start() formalities. -EBUSY
  51. means that somebody, somewhere else is messing with the instance's CPU
  52. mask, while -EINVAL is a complaint about cb_cpu not being in that CPU mask.
  53. If all goes well, this function will return -EINPROGRESS, indicating that
  54. the work is in progress.
  55. Each task submitted to padata_do_parallel() will, in turn, be passed to
  56. exactly one call to the above-mentioned parallel() function, on one CPU, so
  57. true parallelism is achieved by submitting multiple tasks. Despite the
  58. fact that the workqueue is used to make these calls, parallel() is run with
  59. software interrupts disabled and thus cannot sleep. The parallel()
  60. function gets the padata_priv structure pointer as its lone parameter;
  61. information about the actual work to be done is probably obtained by using
  62. container_of() to find the enclosing structure.
  63. Note that parallel() has no return value; the padata subsystem assumes that
  64. parallel() will take responsibility for the task from this point. The work
  65. need not be completed during this call, but, if parallel() leaves work
  66. outstanding, it should be prepared to be called again with a new job before
  67. the previous one completes. When a task does complete, parallel() (or
  68. whatever function actually finishes the job) should inform padata of the
  69. fact with a call to:
  70. void padata_do_serial(struct padata_priv *padata);
  71. At some point in the future, padata_do_serial() will trigger a call to the
  72. serial() function in the padata_priv structure. That call will happen on
  73. the CPU requested in the initial call to padata_do_parallel(); it, too, is
  74. done through the workqueue, but with local software interrupts disabled.
  75. Note that this call may be deferred for a while since the padata code takes
  76. pains to ensure that tasks are completed in the order in which they were
  77. submitted.
  78. The one remaining function in the padata API should be called to clean up
  79. when a padata instance is no longer needed:
  80. void padata_free(struct padata_instance *pinst);
  81. This function will busy-wait while any remaining tasks are completed, so it
  82. might be best not to call it while there is work outstanding. Shutting
  83. down the workqueue, if necessary, should be done separately.