๐Ÿ–ฅ๏ธ Computer Science/OS

[OS] Thread Local Storage (TLS)

Rosieblue 2023. 4. 11. 00:16
728x90

TLS(Thread Local Storage)๋ž€?

TLS๋Š” Thread Local Storage์˜ ์•ฝ์ž๋กœ thread๋ณ„๋กœ ๊ณ ์œ ํ•œ ์ „์—ญ/์ •์  ๋ณ€์ˆ˜๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ณต๊ฐ„์ด๋‹ค.

์Šค๋ ˆ๋“œ๋Š” ์›๋ž˜ ์Šค๋ ˆ๋“œ ๋ณ„๋กœ ๊ณ ์œ ์˜ ์Šคํƒ์„ ๊ฐ€์ง€์ง€๋งŒ, ์ „์—ญ/์ „์  ๋ณ€์ˆ˜๋Š” ํ”„๋กœ์„ธ์Šค์˜ ๋ชจ๋“  ์Šค๋ ˆ๋“œ๊ฐ€ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ์ „์—ญ/์ •์  ๋ณ€์ˆ˜๋ฅผ ์Šค๋ ˆ๋“œ ๋ณ„๋กœ ํ• ๋‹นํ•ด์ฃผ๊ณ  ์‹ถ์„ ๋•Œ TLS๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. (๋”ฐ๋ผ์„œ TLS์— ๋“ค์–ด์žˆ๋Š” ์• ๋“ค์€ ์ง€์—ญ ๋ณ€์ˆ˜์™€ ๋‹ค๋ฅด๊ฒŒ ํ•จ์ˆ˜ above์—์„œ ๋‹ค visibleํ•˜๋‹ค) TLS๋Š” ํ”„๋กœ์„ธ์Šค ์‹คํ–‰์— ํ•„์š”ํ•œ ์—ฌ๋Ÿฌ ์ •๋ณด๊ฐ€ ๋“ค์–ด์žˆ๋‹ค

 

fs๋Š” TLS๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š”๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด fs:0x28์—๋Š” ์Šคํƒ ์นด๋‚˜๋ฆฌ์˜ ๊ฐ’์œผ๋กœ ์“ฐ์ด๋Š” ๋žœ๋คํ•œ ๊ฐ’์ด ์ €์žฅ๋œ๋‹ค.

 

๋Œ€์ถฉ tls ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜์ž„ ์•„๋ž˜๋Š” ใ…‡ใ…‡

static void *
init_tls (void)
{
  /* Construct the static TLS block and the dtv for the initial
     thread.  For some platforms this will include allocating memory
     for the thread descriptor.  The memory for the TLS block will
     never be freed.  It should be allocated accordingly.  The dtv
     array can be changed if dynamic loading requires it.  */
     
  void *tcbp = _dl_allocate_tls_storage (); //_dl_allocate_tls_storage๋กœ tls๋ฅผ ์ƒ์„ฑํ•จ
  
  if (tcbp == NULL)
    _dl_fatal_printf ("\
cannot allocate TLS data structures for initial thread\n");

  /* Store for detection of the special case by __tls_get_addr
     so it knows not to pass this dtv to the normal realloc.  */     
  GL(dl_initial_dtv) = GET_DTV (tcbp); //์Œ ์ผ๋ฐ˜ realloc์œผ๋กœ ์ด๋ถ€๋ถ„ ํ• ๋‹นํ•˜์ง€์•Š๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ธ๋“ฏํ•จ
  
  /* And finally install it for the main thread.  */
  const char *lossage = TLS_INIT_TP (tcbp); //TLS_INIT_TP ๋งคํฌ๋กœ๋กœ FS๋ฅผ TLS๋กœ ์ดˆ๊ธฐํ™”ํ•จ
  //๋‚ด๋ถ€์˜ arch_prctlํ•จ์ˆ˜์— ์ธ์ž๋กœ SET_FS๋ž‘ tcbp๋กœ ๋„˜๊ฒจ์ฃผ๋Š”๋ฐ
  //์ด์นœ๊ตฌ๊ฐ€ FS๋ฅผ tcbp๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ์ž„
  
  if (__glibc_unlikely (lossage != NULL))
    _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage);
    
  tls_init_tp_called = true;
  return tcbp;
}

 

 

TLS_INIT_TP ๋งคํฌ๋กœ์ž„ ์œ„์—์„œ ์—ฌ๊ธฐ์„œ FS๋ถ€๋ถ„์„ TLS๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค๊ณ  ํ–ˆ์Œ

# define TLS_INIT_TP(thrdescr) \
  ({ void *_thrdescr = (thrdescr);                                              \
     tcbhead_t *_head = _thrdescr;                                              \
     int _result;                                                              \
                                                                              \
     _head->tcb = _thrdescr;                                                      \
     /* For now the thread descriptor is at the same address.  */              \
     _head->self = _thrdescr;                                                      \
                                                                              \
     /* It is a simple syscall to set the %fs value for the thread.  */              \
     asm volatile ("syscall"                                                      \
                   : "=a" (_result)                                              \
                   : "0" ((unsigned long int) __NR_arch_prctl),                      \
                     "D" ((unsigned long int) ARCH_SET_FS),                      \
                     "S" (_thrdescr)                                              \
                   : "memory", "cc", "r11", "cx");                              \
                                                                              \
    _result ? "cannot set %fs base address for thread-local storage" : 0;     \
  })

์–˜๋ฅผ ํ†ตํ•ด FS ์„ธ๊ทธ๋จผํŠธ ๋ ˆ์ง€์Šคํ„ฐ๋Š” ์–ด๋””๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค? ๋ฐ”๋กœ TLS๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ๋œ๋‹ค~

(FS๊ฐ€ TLS๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฑฐ์ž„ TLS๊ฐ€ FS ๊ฐ€๋ฆฌํ‚ค๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ! (์•„๋งˆ?))

 

 

References

https://en.wikipedia.org/wiki/Thread-local_storage

https://ozt88.tistory.com/37

 

Thread Local Storage

Thread Local Storage๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋‹ค๋ณด๋ฉด ๋ถˆํŽธํ•œ๊ฒŒ ์žˆ๋‹ค. ์“ฐ๋ ˆ๋“œ๋ณ„ ๊ณ ์œ ํ•œ ์ „์—ญ๋ณ€์ˆ˜(๋˜๋Š” ์ •์ ๋ณ€์ˆ˜) ์‚ฌ์šฉํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค๋Š” ๊ฒƒ. ์“ฐ๋ ˆ๋“œ๋ฅผ ๊ทธ๋ƒฅ ๋งŒ๋“ค๋ฉด ์“ฐ๋ ˆ๋“œ์—๊ฒŒ ์ฃผ์–ด์ง„ ํ˜ผ์ž๋งŒ์˜ ๊ณต๊ฐ„

ozt88.tistory.com

https://learn.microsoft.com/ko-kr/windows/win32/procthread/thread-local-storage?redirectedfrom=MSDN 

 

์Šค๋ ˆ๋“œ ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€ - Win32 apps

TLS(์Šค๋ ˆ๋“œ ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ”„๋กœ์„ธ์Šค์—์„œ ์ „์—ญ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ ์Šค๋ ˆ๋“œ์— ๋Œ€ํ•ด ๊ณ ์œ ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ์Šค๋ ˆ๋“œ๋Š” ์ธ๋ฑ์Šค๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด ์ธ

learn.microsoft.com

http://egloos.zum.com/sweeper/v/1985738

 

TLS (Thread local storage)

1. ์š”์•ฝTLS(Thread Local Storage)๋Š” ์Šค๋ ˆ๋“œ ๋ณ„๋กœ ๊ณ ์œ ํ•œ ์ €์žฅ๊ณต๊ฐ„์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.2. ๋ณธ๋ฌธ๊ฐ๊ฐ์˜ ์Šค๋ ˆ๋“œ๋Š” ๊ณ ์œ ํ•œ ์Šคํƒ์„ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— ์Šคํƒ ๋ณ€์ˆ˜(์ง€์—ญ ๋ณ€์ˆ˜)๋Š” ์Šค๋ ˆ๋“œ ๋ณ„๋กœ ๊ณ ์œ ํ•˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค

egloos.zum.com

 

tls๋Š” ์Šคํƒ๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ์ƒ์„ฑ๋จ

info thread๋กœ ์ฐ์—ˆ์„ ๋•Œ ์•„๋งˆ ๋‚˜์˜ค๋Š”๋ฐ TLS base์ธ๋“ฏ?