padata.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. int padata_start(struct padata_instance *pinst);
  19. void padata_stop(struct padata_instance *pinst);
  20. These functions are setting or clearing the "PADATA_INIT" flag;
  21. if that flag is not set, other functions will refuse to work.
  22. padata_start returns zero on success (flag set) or -EINVAL if the
  23. padata cpumask contains no active cpu (flag not set).
  24. padata_stop clears the flag and blocks until the padata instance
  25. is unused.
  26. The list of CPUs to be used can be adjusted with these functions:
  27. int padata_set_cpumask(struct padata_instance *pinst,
  28. cpumask_var_t cpumask);
  29. int padata_add_cpu(struct padata_instance *pinst, int cpu);
  30. int padata_remove_cpu(struct padata_instance *pinst, int cpu);
  31. Changing the CPU mask has the look of an expensive operation, though, so it
  32. probably should not be done with great frequency.
  33. Actually submitting work to the padata instance requires the creation of a
  34. padata_priv structure:
  35. struct padata_priv {
  36. /* Other stuff here... */
  37. void (*parallel)(struct padata_priv *padata);
  38. void (*serial)(struct padata_priv *padata);
  39. };
  40. This structure will almost certainly be embedded within some larger
  41. structure specific to the work to be done. Most its fields are private to
  42. padata, but the structure should be zeroed at initialization time, and the
  43. parallel() and serial() functions should be provided. Those functions will
  44. be called in the process of getting the work done as we will see
  45. momentarily.
  46. The submission of work is done with:
  47. int padata_do_parallel(struct padata_instance *pinst,
  48. struct padata_priv *padata, int cb_cpu);
  49. The pinst and padata structures must be set up as described above; cb_cpu
  50. specifies which CPU will be used for the final callback when the work is
  51. done; it must be in the current instance's CPU mask. The return value from
  52. padata_do_parallel() is zero on success, indicating that the work is in
  53. progress. -EBUSY means that somebody, somewhere else is messing with the
  54. instance's CPU mask, while -EINVAL is a complaint about cb_cpu not being
  55. in that CPU mask or about a not running instance.
  56. Each task submitted to padata_do_parallel() will, in turn, be passed to
  57. exactly one call to the above-mentioned parallel() function, on one CPU, so
  58. true parallelism is achieved by submitting multiple tasks. Despite the
  59. fact that the workqueue is used to make these calls, parallel() is run with
  60. software interrupts disabled and thus cannot sleep. The parallel()
  61. function gets the padata_priv structure pointer as its lone parameter;
  62. information about the actual work to be done is probably obtained by using
  63. container_of() to find the enclosing structure.
  64. Note that parallel() has no return value; the padata subsystem assumes that
  65. parallel() will take responsibility for the task from this point. The work
  66. need not be completed during this call, but, if parallel() leaves work
  67. outstanding, it should be prepared to be called again with a new job before
  68. the previous one completes. When a task does complete, parallel() (or
  69. whatever function actually finishes the job) should inform padata of the
  70. fact with a call to:
  71. void padata_do_serial(struct padata_priv *padata);
  72. At some point in the future, padata_do_serial() will trigger a call to the
  73. serial() function in the padata_priv structure. That call will happen on
  74. the CPU requested in the initial call to padata_do_parallel(); it, too, is
  75. done through the workqueue, but with local software interrupts disabled.
  76. Note that this call may be deferred for a while since the padata code takes
  77. pains to ensure that tasks are completed in the order in which they were
  78. submitted.
  79. The one remaining function in the padata API should be called to clean up
  80. when a padata instance is no longer needed:
  81. void padata_free(struct padata_instance *pinst);
  82. This function will busy-wait while any remaining tasks are completed, so it
  83. might be best not to call it while there is work outstanding. Shutting
  84. down the workqueue, if necessary, should be done separately.