padata.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. The padata parallel execution mechanism
  2. Last updated for 2.6.36
  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(struct workqueue_struct *wq,
  13. const struct cpumask *pcpumask,
  14. const struct cpumask *cbcpumask);
  15. The pcpumask describes which processors will be used to execute work
  16. submitted to this instance in parallel. The cbcpumask defines which
  17. processors are allowed to be used as the serialization callback processor.
  18. The workqueue wq is where the work will actually be done; it should be
  19. a multithreaded queue, naturally.
  20. To allocate a padata instance with the cpu_possible_mask for both
  21. cpumasks this helper function can be used:
  22. struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq);
  23. Note: Padata maintains two kinds of cpumasks internally. The user supplied
  24. cpumasks, submitted by padata_alloc/padata_alloc_possible and the 'usable'
  25. cpumasks. The usable cpumasks are always a subset of active CPUs in the
  26. user supplied cpumasks; these are the cpumasks padata actually uses. So
  27. it is legal to supply a cpumask to padata that contains offline CPUs.
  28. Once an offline CPU in the user supplied cpumask comes online, padata
  29. is going to use it.
  30. There are functions for enabling and disabling the instance:
  31. int padata_start(struct padata_instance *pinst);
  32. void padata_stop(struct padata_instance *pinst);
  33. These functions are setting or clearing the "PADATA_INIT" flag;
  34. if that flag is not set, other functions will refuse to work.
  35. padata_start returns zero on success (flag set) or -EINVAL if the
  36. padata cpumask contains no active CPU (flag not set).
  37. padata_stop clears the flag and blocks until the padata instance
  38. is unused.
  39. The list of CPUs to be used can be adjusted with these functions:
  40. int padata_set_cpumasks(struct padata_instance *pinst,
  41. cpumask_var_t pcpumask,
  42. cpumask_var_t cbcpumask);
  43. int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
  44. cpumask_var_t cpumask);
  45. int padata_add_cpu(struct padata_instance *pinst, int cpu, int mask);
  46. int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask);
  47. Changing the CPU masks are expensive operations, though, so it should not be
  48. done with great frequency.
  49. It's possible to change both cpumasks of a padata instance with
  50. padata_set_cpumasks by specifying the cpumasks for parallel execution (pcpumask)
  51. and for the serial callback function (cbcpumask). padata_set_cpumask is used to
  52. change just one of the cpumasks. Here cpumask_type is one of PADATA_CPU_SERIAL,
  53. PADATA_CPU_PARALLEL and cpumask specifies the new cpumask to use.
  54. To simply add or remove one CPU from a certain cpumask the functions
  55. padata_add_cpu/padata_remove_cpu are used. cpu specifies the CPU to add or
  56. remove and mask is one of PADATA_CPU_SERIAL, PADATA_CPU_PARALLEL.
  57. If a user is interested in padata cpumask changes, he can register to
  58. the padata cpumask change notifier:
  59. int padata_register_cpumask_notifier(struct padata_instance *pinst,
  60. struct notifier_block *nblock);
  61. To unregister from that notifier:
  62. int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
  63. struct notifier_block *nblock);
  64. The padata cpumask change notifier notifies about changes of the usable
  65. cpumasks, i.e. the subset of active CPUs in the user supplied cpumask.
  66. Padata calls the notifier chain with:
  67. blocking_notifier_call_chain(&pinst->cpumask_change_notifier,
  68. notification_mask,
  69. &pd_new->cpumask);
  70. Here cpumask_change_notifier is registered notifier, notification_mask
  71. is one of PADATA_CPU_SERIAL, PADATA_CPU_PARALLEL and cpumask is a pointer
  72. to a struct padata_cpumask that contains the new cpumask information.
  73. Actually submitting work to the padata instance requires the creation of a
  74. padata_priv structure:
  75. struct padata_priv {
  76. /* Other stuff here... */
  77. void (*parallel)(struct padata_priv *padata);
  78. void (*serial)(struct padata_priv *padata);
  79. };
  80. This structure will almost certainly be embedded within some larger
  81. structure specific to the work to be done. Most of its fields are private to
  82. padata, but the structure should be zeroed at initialisation time, and the
  83. parallel() and serial() functions should be provided. Those functions will
  84. be called in the process of getting the work done as we will see
  85. momentarily.
  86. The submission of work is done with:
  87. int padata_do_parallel(struct padata_instance *pinst,
  88. struct padata_priv *padata, int cb_cpu);
  89. The pinst and padata structures must be set up as described above; cb_cpu
  90. specifies which CPU will be used for the final callback when the work is
  91. done; it must be in the current instance's CPU mask. The return value from
  92. padata_do_parallel() is zero on success, indicating that the work is in
  93. progress. -EBUSY means that somebody, somewhere else is messing with the
  94. instance's CPU mask, while -EINVAL is a complaint about cb_cpu not being
  95. in that CPU mask or about a not running instance.
  96. Each task submitted to padata_do_parallel() will, in turn, be passed to
  97. exactly one call to the above-mentioned parallel() function, on one CPU, so
  98. true parallelism is achieved by submitting multiple tasks. Despite the
  99. fact that the workqueue is used to make these calls, parallel() is run with
  100. software interrupts disabled and thus cannot sleep. The parallel()
  101. function gets the padata_priv structure pointer as its lone parameter;
  102. information about the actual work to be done is probably obtained by using
  103. container_of() to find the enclosing structure.
  104. Note that parallel() has no return value; the padata subsystem assumes that
  105. parallel() will take responsibility for the task from this point. The work
  106. need not be completed during this call, but, if parallel() leaves work
  107. outstanding, it should be prepared to be called again with a new job before
  108. the previous one completes. When a task does complete, parallel() (or
  109. whatever function actually finishes the job) should inform padata of the
  110. fact with a call to:
  111. void padata_do_serial(struct padata_priv *padata);
  112. At some point in the future, padata_do_serial() will trigger a call to the
  113. serial() function in the padata_priv structure. That call will happen on
  114. the CPU requested in the initial call to padata_do_parallel(); it, too, is
  115. done through the workqueue, but with local software interrupts disabled.
  116. Note that this call may be deferred for a while since the padata code takes
  117. pains to ensure that tasks are completed in the order in which they were
  118. submitted.
  119. The one remaining function in the padata API should be called to clean up
  120. when a padata instance is no longer needed:
  121. void padata_free(struct padata_instance *pinst);
  122. This function will busy-wait while any remaining tasks are completed, so it
  123. might be best not to call it while there is work outstanding. Shutting
  124. down the workqueue, if necessary, should be done separately.