Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * shctx.h - shared context management functions for SSL |
| 3 | * |
| 4 | * Copyright (C) 2011-2012 EXCELIANCE |
| 5 | * |
| 6 | * Author: Emeric Brun - emeric@exceliance.fr |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #ifndef SHCTX_H |
| 15 | #define SHCTX_H |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 16 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 17 | #include <common/mini-clist.h> |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 18 | #include <types/shctx.h> |
| 19 | |
Willy Tarreau | a1bd1fa | 2019-03-29 17:26:33 +0100 | [diff] [blame] | 20 | #include <inttypes.h> |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 21 | |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 22 | #ifndef USE_PRIVATE_CACHE |
| 23 | #ifdef USE_PTHREAD_PSHARED |
| 24 | #include <pthread.h> |
| 25 | #else |
| 26 | #ifdef USE_SYSCALL_FUTEX |
| 27 | #include <unistd.h> |
| 28 | #include <linux/futex.h> |
| 29 | #include <sys/syscall.h> |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 30 | #endif |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 31 | #endif |
Emeric Brun | 786991e | 2012-11-26 18:37:12 +0100 | [diff] [blame] | 32 | #endif |
| 33 | |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 34 | int shctx_init(struct shared_context **orig_shctx, |
Frédéric Lécaille | b80bc27 | 2018-10-25 20:31:40 +0200 | [diff] [blame] | 35 | int maxblocks, int blocksize, unsigned int maxobjsz, |
| 36 | int extra, int shared); |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 37 | struct shared_block *shctx_row_reserve_hot(struct shared_context *shctx, |
| 38 | struct shared_block *last, int data_len); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 39 | void shctx_row_inc_hot(struct shared_context *shctx, struct shared_block *first); |
| 40 | void shctx_row_dec_hot(struct shared_context *shctx, struct shared_block *first); |
| 41 | int shctx_row_data_append(struct shared_context *shctx, |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 42 | struct shared_block *first, struct shared_block *from, |
| 43 | unsigned char *data, int len); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 44 | int shctx_row_data_get(struct shared_context *shctx, struct shared_block *first, |
| 45 | unsigned char *dst, int offset, int len); |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 46 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 47 | |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 48 | /* Lock functions */ |
| 49 | |
| 50 | #if defined (USE_PRIVATE_CACHE) |
| 51 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 52 | #define shctx_lock(shctx) |
| 53 | #define shctx_unlock(shctx) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 54 | |
| 55 | #elif defined (USE_PTHREAD_PSHARED) |
| 56 | extern int use_shared_mem; |
| 57 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 58 | #define shctx_lock(shctx) if (use_shared_mem) pthread_mutex_lock(&shctx->mutex) |
| 59 | #define shctx_unlock(shctx) if (use_shared_mem) pthread_mutex_unlock(&shctx->mutex) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 60 | |
| 61 | #else |
| 62 | extern int use_shared_mem; |
| 63 | |
| 64 | #ifdef USE_SYSCALL_FUTEX |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 65 | static inline void _shctx_wait4lock(unsigned int *count, unsigned int *uaddr, int value) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 66 | { |
| 67 | syscall(SYS_futex, uaddr, FUTEX_WAIT, value, NULL, 0, 0); |
| 68 | } |
| 69 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 70 | static inline void _shctx_awakelocker(unsigned int *uaddr) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 71 | { |
| 72 | syscall(SYS_futex, uaddr, FUTEX_WAKE, 1, NULL, 0, 0); |
| 73 | } |
| 74 | |
| 75 | #else /* internal spin lock */ |
| 76 | |
| 77 | #if defined (__i486__) || defined (__i586__) || defined (__i686__) || defined (__x86_64__) |
| 78 | static inline void relax() |
| 79 | { |
| 80 | __asm volatile("rep;nop\n" ::: "memory"); |
| 81 | } |
| 82 | #else /* if no x86_64 or i586 arch: use less optimized but generic asm */ |
| 83 | static inline void relax() |
| 84 | { |
| 85 | __asm volatile("" ::: "memory"); |
| 86 | } |
| 87 | #endif |
| 88 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 89 | static inline void _shctx_wait4lock(unsigned int *count, unsigned int *uaddr, int value) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 90 | { |
| 91 | int i; |
| 92 | |
| 93 | for (i = 0; i < *count; i++) { |
| 94 | relax(); |
| 95 | relax(); |
Willy Tarreau | 7f76791 | 2020-05-01 13:05:29 +0200 | [diff] [blame] | 96 | if (*uaddr != value) |
| 97 | return; |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 98 | } |
Willy Tarreau | 62d5e9c | 2020-05-01 13:15:32 +0200 | [diff] [blame] | 99 | *count = (unsigned char)((*count << 1) + 1); |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 100 | } |
| 101 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 102 | #define _shctx_awakelocker(a) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 103 | |
| 104 | #endif |
| 105 | |
| 106 | #if defined (__i486__) || defined (__i586__) || defined (__i686__) || defined (__x86_64__) |
| 107 | static inline unsigned int xchg(unsigned int *ptr, unsigned int x) |
| 108 | { |
| 109 | __asm volatile("lock xchgl %0,%1" |
| 110 | : "=r" (x), "+m" (*ptr) |
| 111 | : "0" (x) |
| 112 | : "memory"); |
| 113 | return x; |
| 114 | } |
| 115 | |
| 116 | static inline unsigned int cmpxchg(unsigned int *ptr, unsigned int old, unsigned int new) |
| 117 | { |
| 118 | unsigned int ret; |
| 119 | |
| 120 | __asm volatile("lock cmpxchgl %2,%1" |
| 121 | : "=a" (ret), "+m" (*ptr) |
| 122 | : "r" (new), "0" (old) |
| 123 | : "memory"); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static inline unsigned char atomic_dec(unsigned int *ptr) |
| 128 | { |
| 129 | unsigned char ret; |
| 130 | __asm volatile("lock decl %0\n" |
| 131 | "setne %1\n" |
| 132 | : "+m" (*ptr), "=qm" (ret) |
| 133 | : |
| 134 | : "memory"); |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | #else /* if no x86_64 or i586 arch: use less optimized gcc >= 4.1 built-ins */ |
| 139 | static inline unsigned int xchg(unsigned int *ptr, unsigned int x) |
| 140 | { |
| 141 | return __sync_lock_test_and_set(ptr, x); |
| 142 | } |
| 143 | |
| 144 | static inline unsigned int cmpxchg(unsigned int *ptr, unsigned int old, unsigned int new) |
| 145 | { |
| 146 | return __sync_val_compare_and_swap(ptr, old, new); |
| 147 | } |
| 148 | |
| 149 | static inline unsigned char atomic_dec(unsigned int *ptr) |
| 150 | { |
| 151 | return __sync_sub_and_fetch(ptr, 1) ? 1 : 0; |
| 152 | } |
| 153 | |
| 154 | #endif |
| 155 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 156 | static inline void _shctx_lock(struct shared_context *shctx) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 157 | { |
| 158 | unsigned int x; |
Willy Tarreau | 62d5e9c | 2020-05-01 13:15:32 +0200 | [diff] [blame] | 159 | unsigned int count = 3; |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 160 | |
| 161 | x = cmpxchg(&shctx->waiters, 0, 1); |
| 162 | if (x) { |
| 163 | if (x != 2) |
| 164 | x = xchg(&shctx->waiters, 2); |
| 165 | |
| 166 | while (x) { |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 167 | _shctx_wait4lock(&count, &shctx->waiters, 2); |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 168 | x = xchg(&shctx->waiters, 2); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 173 | static inline void _shctx_unlock(struct shared_context *shctx) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 174 | { |
| 175 | if (atomic_dec(&shctx->waiters)) { |
| 176 | shctx->waiters = 0; |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 177 | _shctx_awakelocker(&shctx->waiters); |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 181 | #define shctx_lock(shctx) if (use_shared_mem) _shctx_lock(shctx) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 182 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 183 | #define shctx_unlock(shctx) if (use_shared_mem) _shctx_unlock(shctx) |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 184 | |
| 185 | #endif |
| 186 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 187 | /* List Macros */ |
| 188 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 189 | /* |
| 190 | * Insert <s> block after <head> which is not necessarily the head of a list, |
| 191 | * so between <head> and the next element after <head>. |
| 192 | */ |
| 193 | static inline void shctx_block_append_hot(struct shared_context *shctx, |
| 194 | struct list *head, |
| 195 | struct shared_block *s) |
| 196 | { |
| 197 | shctx->nbav--; |
| 198 | LIST_DEL(&s->list); |
| 199 | LIST_ADD(head, &s->list); |
| 200 | } |
| 201 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 202 | static inline void shctx_block_set_hot(struct shared_context *shctx, |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 203 | struct shared_block *s) |
| 204 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 205 | shctx->nbav--; |
| 206 | LIST_DEL(&s->list); |
| 207 | LIST_ADDQ(&shctx->hot, &s->list); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 208 | } |
| 209 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 210 | static inline void shctx_block_set_avail(struct shared_context *shctx, |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 211 | struct shared_block *s) |
| 212 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 213 | shctx->nbav++; |
| 214 | LIST_DEL(&s->list); |
| 215 | LIST_ADDQ(&shctx->avail, &s->list); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 216 | } |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 217 | |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 218 | #endif /* SHCTX_H */ |
| 219 | |