Bug 10686

Summary: Request for new TLS field for x86 and x86_64
Product: glibc Reporter: Ian Lance Taylor <ian>
Component: nptlAssignee: Andreas Jaeger <aj>
Status: RESOLVED FIXED    
Severity: enhancement CC: aj, carlos, fweimer, glibc-bugs
Priority: P2 Flags: fweimer: security-
Version: unspecified   
Target Milestone: 2.18   
Host: Target:
Build: Last reconfirmed:

Description Ian Lance Taylor 2009-09-23 05:23:06 UTC
In http://gcc.gnu.org/wiki/SplitStacks I describe an enhancement to gcc to
support discontiguous stacks.  This is useful for permitting a large number of
threads to flexibly share stack space.  It is possible to have many more threads
executing when they do not all have to simultaneously have the maximum required
stack space.  This is naturally most useful when using a 32-bit address space. 
I'm working on adding this to gcc.

I think the best approach to make this efficient on x86 and x86_64 will be to
store the current stack limit, less some appropriate offset, in the TCB header.
 Therefore, I would like to request a new field in the TCB header, at a fixed
offset, for x86 and x86_64.  The field should have type void * or uintptr_t.

Thanks.
Comment 1 Andreas Jaeger 2013-05-10 19:01:57 UTC
Ian, sorry for the late followup here. Do you still need a new TLS field?
Comment 2 Ian Lance Taylor 2013-05-12 17:32:00 UTC
Currently the transactional memory support has five words in the TCB.  For the split stack support on x86 and x86_64 I have been using the last of those five words, as Richard Henderson told me the TM support was not using it.  So assuming the TM folks are still agreeable, I think the simplest approach at this point would be to reduce the TM words to 4 and make the last one be a split stack word.  The TM words are the __private_tm field in tcbhead_t.
Comment 3 Carlos O'Donell 2013-05-14 07:16:09 UTC
I spoke with Richard Henderson about this today and he says he is perfectly happy to continue with the current uses of the TM words. That is to say that split stack support was granted use of the last two TM words.

It sounds like Ian only needs the last TM word for split stack support. We should make this explicit by fixing the code to have __private_tm be 4 words, and __private_ss be 1 word (or whatever you want to call it) with comments. Then we can close this issue.

I don't have time to fix this, but I did have time to double check things.

Andreas do you plan to fix this?
Comment 4 Andreas Jaeger 2013-05-14 07:25:11 UTC
Ian,

Would changing this:
  void *__private_tm[5];

to the following be ok?
  /* Reservation of some values for the TM ABI.  */
  void *__private_tm[4];
  /* GCC split stack support.  */
  void *__private_ss;


I'll create a proper patch out of this.
Comment 5 Ian Lance Taylor 2013-05-14 12:29:33 UTC
The patch in comment #4 looks great to me.

Thanks.
Comment 6 Andreas Jaeger 2013-05-15 18:22:17 UTC
committed now for glibc 2.18

commit ecbf434213c0333d81706074e4d107ac45011635
Author: Andreas Jaeger <aj@suse.de>
Date:   Wed May 15 20:20:54 2013 +0200

    Reserve new TLS field for x86 and x86_64
    
        [BZ #10686]
        * sysdeps/x86_64/tls.h (struct tcbhead_t): Add __private_ss
        field.
        * sysdeps/i386/tls.h (struct tcbhead_t): Likewise.
Comment 7 H.J. Lu 2018-06-04 13:42:53 UTC
(In reply to Ian Lance Taylor from comment #5)
> The patch in comment #4 looks great to me.
> 
> Thanks.

FYI, the wrong offset is used on i386:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85990
Comment 8 Sourceware Commits 2018-06-12 13:36:26 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, master has been updated
       via  0221ce2a90be2d40fc90f0b5dcec77a1ec013f53 (commit)
      from  e826574c985a15a500262f2fbd21c7e9259d3d11 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=0221ce2a90be2d40fc90f0b5dcec77a1ec013f53

commit 0221ce2a90be2d40fc90f0b5dcec77a1ec013f53
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Jun 12 06:23:28 2018 -0700

    i386: Change offset of __private_ss to 0x30 [BZ #23250]
    
    sysdeps/i386/nptl/tls.h has
    
    typedef struct
    {
      void *tcb;            /* Pointer to the TCB.  Not necessarily the
                               thread descriptor used by libpthread.  */
      dtv_t *dtv;
      void *self;           /* Pointer to the thread descriptor.  */
      int multiple_threads;
      uintptr_t sysinfo;
      uintptr_t stack_guard;
      uintptr_t pointer_guard;
      int gscope_flag;
      int __glibc_reserved1;
      /* Reservation of some values for the TM ABI.  */
      void *__private_tm[4];
      /* GCC split stack support.  */
      void *__private_ss;
    } tcbhead_t;
    
    The offset of __private_ss is 0x34.  But GCC defines
    
    /* We steal the last transactional memory word.  */
     #define TARGET_THREAD_SPLIT_STACK_OFFSET 0x30
    
    and libgcc/config/i386/morestack.S has
    
    	cmpl	%gs:0x30,%eax		# See if we have enough space.
    	movl	%eax,%gs:0x30		# Save the new stack boundary.
    	movl	%eax,%gs:0x30		# Save the new stack boundary.
    	movl	%ecx,%gs:0x30		# Save new stack boundary.
    	movl	%eax,%gs:0x30
    	movl	%gs:0x30,%eax
    	movl	%eax,%gs:0x30
    
    Since update TARGET_THREAD_SPLIT_STACK_OFFSET changes split stack ABI,
    this patch updates tcbhead_t to match GCC.
    
    	[BZ #23250]
    	[BZ #10686]
    	* sysdeps/i386/nptl/tls.h (tcbhead_t): Change __private_tm[4]
    	to _private_tm[3] and add __glibc_reserved2.
    	Add _Static_assert of offset of __private_ss == 0x30.
    	* sysdeps/x86_64/nptl/tls.h: Add _Static_assert of offset of
    	__private_ss == 0x40 for ILP32 and == 0x70 for LP64.

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog                 |   10 ++++++++++
 sysdeps/i386/nptl/tls.h   |    7 ++++++-
 sysdeps/x86_64/nptl/tls.h |   10 ++++++++++
 3 files changed, 26 insertions(+), 1 deletions(-)