0010-lib-ovs-thread-Ensure-that-thread-stacks-are-always-.patch 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. From 8147cec9ee8feea9440cf79365709ddc32ff57d5 Mon Sep 17 00:00:00 2001
  2. From: Alexandru Ardelean <ardeleanalex@gmail.com>
  3. Date: Thu, 4 Feb 2016 09:20:34 +0200
  4. Subject: [PATCH] lib/ovs-thread: Ensure that thread stacks are always at least
  5. 512 kB.
  6. This makes a difference for libc implementations (such as musl libc) that
  7. have a really small default pthread stack size.
  8. Will reference this discussion:
  9. http://patchwork.ozlabs.org/patch/572340/
  10. Reported-by: Robert McKay <robert@mckay.com>
  11. Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
  12. [blp@ovn.org made style changes]
  13. Signed-off-by: Ben Pfaff <blp@ovn.org>
  14. ---
  15. lib/ovs-thread.c | 29 +++++++++++++++++++++++++++++
  16. 1 file changed, 29 insertions(+)
  17. diff --git a/lib/ovs-thread.c b/lib/ovs-thread.c
  18. index 6ebda07..b0e10ee 100644
  19. --- a/lib/ovs-thread.c
  20. +++ b/lib/ovs-thread.c
  21. @@ -340,6 +340,25 @@ ovsthread_wrapper(void *aux_)
  22. return aux.start(aux.arg);
  23. }
  24. +static void
  25. +set_min_stack_size(pthread_attr_t *attr, size_t min_stacksize)
  26. +{
  27. + size_t stacksize;
  28. + int error;
  29. +
  30. + error = pthread_attr_getstacksize(attr, &stacksize);
  31. + if (error) {
  32. + ovs_abort(error, "pthread_attr_getstacksize failed");
  33. + }
  34. +
  35. + if (stacksize < min_stacksize) {
  36. + error = pthread_attr_setstacksize(attr, min_stacksize);
  37. + if (error) {
  38. + ovs_abort(error, "pthread_attr_setstacksize failed");
  39. + }
  40. + }
  41. +}
  42. +
  43. /* Starts a thread that calls 'start(arg)'. Sets the thread's name to 'name'
  44. * (suffixed by its ovsthread_id()). Returns the new thread's pthread_t. */
  45. pthread_t
  46. @@ -358,10 +377,20 @@ ovs_thread_create(const char *name, void *(*start)(void *), void *arg)
  47. aux->arg = arg;
  48. ovs_strlcpy(aux->name, name, sizeof aux->name);
  49. - error = pthread_create(&thread, NULL, ovsthread_wrapper, aux);
  50. + /* Some small systems use a default stack size as small as 80 kB, but OVS
  51. + * requires approximately 384 kB according to the following analysis:
  52. + * http://openvswitch.org/pipermail/dev/2016-January/065049.html
  53. + *
  54. + * We use 512 kB to give us some margin of error. */
  55. + pthread_attr_t attr;
  56. + pthread_attr_init(&attr);
  57. + set_min_stack_size(&attr, 512 * 1024);
  58. +
  59. + error = pthread_create(&thread, &attr, ovsthread_wrapper, aux);
  60. if (error) {
  61. ovs_abort(error, "pthread_create failed");
  62. }
  63. + pthread_attr_destroy(&attr);
  64. return thread;
  65. }
  66. --
  67. 2.1.4