Create threads in detached state in userspace.
[zfs.git] / lib / libzpool / kernel.c
index 69fbd44..c38efd0 100644 (file)
@@ -141,7 +141,7 @@ zk_thread_helper(void *arg)
 
 kthread_t *
 zk_thread_create(caddr_t stk, size_t stksize, thread_func_t func, void *arg,
-             size_t len, proc_t *pp, int state, pri_t pri)
+             size_t len, proc_t *pp, int state, pri_t pri, int detachstate)
 {
        kthread_t *kt;
        pthread_attr_t attr;
@@ -159,9 +159,14 @@ zk_thread_create(caddr_t stk, size_t stksize, thread_func_t func, void *arg,
         *
         * We reduce the default stack size in userspace, to ensure
         * we observe stack overruns in user space as well as in
-        * kernel space.  PTHREAD_STACK_MIN is the minimum stack
-        * required for a NULL procedure in user space and is added
-        * in to the stack requirements.
+        * kernel space. In practice we can't set the userspace stack
+        * size to 8k because differences in stack usage between kernel
+        * space and userspace could lead to spurious stack overflows
+        * (especially when debugging is enabled). Nevertheless, we try
+        * to set it to the lowest value that works (currently 8k*4).
+        * PTHREAD_STACK_MIN is the minimum stack required for a NULL
+        * procedure in user space and is added in to the stack
+        * requirements.
         *
         * Some buggy NPTL threading implementations include the
         * guard area within the stack size allocations.  In
@@ -170,12 +175,13 @@ zk_thread_create(caddr_t stk, size_t stksize, thread_func_t func, void *arg,
         * on Linux.
         */
 
-       stack = PTHREAD_STACK_MIN + MAX(stksize, STACK_SIZE) +
+       stack = PTHREAD_STACK_MIN + MAX(stksize, STACK_SIZE) * 4 +
                        EXTRA_GUARD_BYTES;
 
        VERIFY3S(pthread_attr_init(&attr), ==, 0);
        VERIFY3S(pthread_attr_setstacksize(&attr, stack), ==, 0);
        VERIFY3S(pthread_attr_setguardsize(&attr, PAGESIZE), ==, 0);
+       VERIFY3S(pthread_attr_setdetachstate(&attr, detachstate), ==, 0);
 
        VERIFY3S(pthread_create(&kt->t_tid, &attr, &zk_thread_helper, kt),
            ==, 0);