From: Willy Tarreau Date: Fri, 20 Dec 2019 06:20:00 +0000 (+0100) Subject: MINOR: fd/threads: make _GET_NEXT()/_GET_PREV() use the volatile attribute X-Git-Tag: v2.2-dev1~165 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=337fb719ee93776cb399a333e04e2438427f0111;p=thirdparty%2Fhaproxy.git MINOR: fd/threads: make _GET_NEXT()/_GET_PREV() use the volatile attribute These macros are either used between atomic ops which cause the volatile to be implicit, or with an explicit volatile cast. However not having it in the macro causes some traps in the code because certain loop paths cannot safely be used without risking infinite loops if one isn't careful enough. Let's place the volatile attribute inside the macros and remove them from the explicit places to avoid this. It was verified that the output executable remains exactly the same byte-wise. --- diff --git a/src/fd.c b/src/fd.c index 6bfefdddc8..828e140a0f 100644 --- a/src/fd.c +++ b/src/fd.c @@ -115,8 +115,8 @@ int poller_wr_pipe[MAX_THREADS]; // Pipe to wake the threads volatile int ha_used_fds = 0; // Number of FD we're currently using -#define _GET_NEXT(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->next -#define _GET_PREV(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->prev +#define _GET_NEXT(fd, off) ((volatile struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->next +#define _GET_PREV(fd, off) ((volatile struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->prev /* adds fd to fd list if it was not yet in it */ void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off) { @@ -207,7 +207,7 @@ lock_self: #else lock_self_next: - next = ({ volatile int *next = &_GET_NEXT(fd, off); *next; }); + next = _GET_NEXT(fd, off); if (next == -2) goto lock_self_next; if (next <= -3) @@ -215,7 +215,7 @@ lock_self_next: if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2))) goto lock_self_next; lock_self_prev: - prev = ({ volatile int *prev = &_GET_PREV(fd, off); *prev; }); + prev = _GET_PREV(fd, off); if (prev == -2) goto lock_self_prev; if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(fd, off), &prev, -2)))