]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
nptl: support thread stacks that grow up
authorCarlos O'Donell <carlos@systemhalted.org>
Fri, 16 Aug 2013 18:57:59 +0000 (14:57 -0400)
committerMike Frysinger <vapier@gentoo.org>
Wed, 5 Aug 2015 10:44:54 +0000 (06:44 -0400)
http://bugs.gentoo.org/301642

nptl/allocatestack.c
nptl/pthread_create.c
nptl/pthread_getattr_np.c

index c56a4df12c06c986b863a98b349c2d73a8a9f815..d97c89a3c99b38c48092549454059ba3e23427fe 100644 (file)
@@ -373,6 +373,13 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
   if (__glibc_unlikely (attr->flags & ATTR_FLAG_STACKADDR))
     {
       uintptr_t adj;
+      char *stackaddr = (char *) attr->stackaddr;
+
+      /* Assume the same layout as the _STACK_GROWS_DOWN case, with struct
+        pthread at the top of the stack block.  Later we adjust the guard
+        location and stack address to match the _STACK_GROWS_UP case.  */
+      if (_STACK_GROWS_UP)
+       stackaddr += attr->stacksize;
 
       /* If the user also specified the size of the stack make sure it
         is large enough.  */
@@ -382,11 +389,11 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
 
       /* Adjust stack size for alignment of the TLS block.  */
 #if TLS_TCB_AT_TP
-      adj = ((uintptr_t) attr->stackaddr - TLS_TCB_SIZE)
+      adj = ((uintptr_t) stackaddr - TLS_TCB_SIZE)
            & __static_tls_align_m1;
       assert (size > adj + TLS_TCB_SIZE);
 #elif TLS_DTV_AT_TP
-      adj = ((uintptr_t) attr->stackaddr - __static_tls_size)
+      adj = ((uintptr_t) stackaddr - __static_tls_size)
            & __static_tls_align_m1;
       assert (size > adj);
 #endif
@@ -396,10 +403,10 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
         the stack.  It is the user's responsibility to do this if it
         is wanted.  */
 #if TLS_TCB_AT_TP
-      pd = (struct pthread *) ((uintptr_t) attr->stackaddr
+      pd = (struct pthread *) ((uintptr_t) stackaddr
                               - TLS_TCB_SIZE - adj);
 #elif TLS_DTV_AT_TP
-      pd = (struct pthread *) (((uintptr_t) attr->stackaddr
+      pd = (struct pthread *) (((uintptr_t) stackaddr
                                - __static_tls_size - adj)
                               - TLS_PRE_TCB_SIZE);
 #endif
@@ -411,7 +418,7 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
       pd->specific[0] = pd->specific_1stblock;
 
       /* Remember the stack-related values.  */
-      pd->stackblock = (char *) attr->stackaddr - size;
+      pd->stackblock = (char *) stackaddr - size;
       pd->stackblock_size = size;
 
       /* This is a user-provided stack.  It will not be queued in the
@@ -635,7 +642,7 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
          char *guard = mem + (((size - guardsize) / 2) & ~pagesize_m1);
 #elif _STACK_GROWS_DOWN
          char *guard = mem;
-# elif _STACK_GROWS_UP
+#elif _STACK_GROWS_UP
          char *guard = (char *) (((uintptr_t) pd - guardsize) & ~pagesize_m1);
 #endif
          if (mprotect (guard, guardsize, PROT_NONE) != 0)
@@ -731,7 +738,6 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
   *stack = stacktop;
 #elif _STACK_GROWS_UP
   *stack = pd->stackblock;
-  assert (*stack > 0);
 #endif
 
   return 0;
index d10f4ea8004e1d8f3a268b95cc0f8d93b8d89867..b08ba45cf94e719f122f8a65d455ea2ad639c0c7 100644 (file)
@@ -428,12 +428,25 @@ START_THREAD_DEFN
 #ifdef _STACK_GROWS_DOWN
   char *sp = CURRENT_STACK_FRAME;
   size_t freesize = (sp - (char *) pd->stackblock) & ~pagesize_m1;
-#else
-# error "to do"
-#endif
   assert (freesize < pd->stackblock_size);
   if (freesize > PTHREAD_STACK_MIN)
     __madvise (pd->stackblock, freesize - PTHREAD_STACK_MIN, MADV_DONTNEED);
+#else
+  /* Page aligned start of memory to free (higher than or equal
+     to current sp plus the minimum stack size).  */
+  void *freeblock = (void*)((size_t)(CURRENT_STACK_FRAME
+                                    + PTHREAD_STACK_MIN
+                                    + pagesize_m1)
+                                   & ~pagesize_m1);
+  char *free_end = (char *) (((uintptr_t) pd - pd->guardsize) & ~pagesize_m1);
+  /* Is there any space to free?  */
+  if (free_end > (char *)freeblock)
+    {
+      size_t freesize = (size_t)(free_end - (char *)freeblock);
+      assert (freesize < pd->stackblock_size);
+      __madvise (freeblock, freesize, MADV_DONTNEED);
+    }
+#endif
 
   /* If the thread is detached free the TCB.  */
   if (IS_DETACHED (pd))
index 52a4602730b646162d7de3118f8046a58c8fc438..9834302ec5f76c46c00c1b7a60b2076a3c5259aa 100644 (file)
@@ -60,7 +60,11 @@ pthread_getattr_np (thread_id, attr)
   if (__glibc_likely (thread->stackblock != NULL))
     {
       iattr->stacksize = thread->stackblock_size;
+#if _STACK_GROWS_DOWN
       iattr->stackaddr = (char *) thread->stackblock + iattr->stacksize;
+#else
+      iattr->stackaddr = (char *) thread->stackblock;
+#endif
     }
   else
     {
@@ -105,7 +109,9 @@ pthread_getattr_np (thread_id, attr)
 
              char *line = NULL;
              size_t linelen = 0;
+#if _STACK_GROWS_DOWN
              uintptr_t last_to = 0;
+#endif
 
              while (! feof_unlocked (fp))
                {
@@ -129,17 +135,24 @@ pthread_getattr_np (thread_id, attr)
                         stack extension request.  */
                      iattr->stacksize = (iattr->stacksize
                                          & -(intptr_t) GLRO(dl_pagesize));
-
+#if _STACK_GROWS_DOWN
                      /* The limit might be too high.  */
                      if ((size_t) iattr->stacksize
                          > (size_t) iattr->stackaddr - last_to)
                        iattr->stacksize = (size_t) iattr->stackaddr - last_to;
-
+#else
+                     /* The limit might be too high.  */
+                     if ((size_t) iattr->stacksize
+                         > to - (size_t) iattr->stackaddr)
+                       iattr->stacksize = to - (size_t) iattr->stackaddr;
+#endif
                      /* We succeed and no need to look further.  */
                      ret = 0;
                      break;
                    }
+#if _STACK_GROWS_DOWN
                  last_to = to;
+#endif
                }
 
              free (line);