diff --git a/tools/linuxfs/preload/sysv.c b/tools/linuxfs/preload/sysv.c index dc349c232a..4d3752f854 100644 --- a/tools/linuxfs/preload/sysv.c +++ b/tools/linuxfs/preload/sysv.c @@ -403,6 +403,9 @@ struct msg_queue { struct { uint64_t seq; long mtype; size_t len; char data[2048]; } slot[MSG_SLOTS]; }; +struct msg_map { int id; int refs; struct msg_queue *q; }; +static struct msg_map g_msgs[MAX_OBJECTS]; + static struct msg_queue *msg_map(int id) { int fd = open_by_id("msg", id); if (fd < 0) return NULL; @@ -415,6 +418,47 @@ static struct msg_queue *msg_map(int id) { return NULL; } +static int msg_removed(struct msg_queue *q) { return __atomic_load_n(&q->magic, __ATOMIC_ACQUIRE) != MSG_MAGIC; } + +static struct msg_map *msg_get(int id) { + struct msg_map *found = NULL, *spare = NULL, *idle = NULL; + pthread_mutex_lock(&g_lock); + for (int i = 0; i < MAX_OBJECTS; i++) { + struct msg_map *e = &g_msgs[i]; + if (e->q && !e->refs && msg_removed(e->q)) { + munmap(e->q, sizeof(*e->q)); + e->q = NULL; + } + if (!e->q) { + if (!spare) spare = e; + } else if (e->id == id && !msg_removed(e->q)) { + found = e; + } else if (!e->refs && !idle) { + idle = e; + } + } + if (!found && !spare && idle) { + munmap(idle->q, sizeof(*idle->q)); + idle->q = NULL; + spare = idle; + } + if (!found && spare && (spare->q = msg_map(id))) { + spare->id = id; + found = spare; + } else if (!found && !spare) { + errno = ENOSPC; + } + if (found) found->refs++; + pthread_mutex_unlock(&g_lock); + return found; +} + +static void msg_put(struct msg_map *m) { + pthread_mutex_lock(&g_lock); + m->refs--; + pthread_mutex_unlock(&g_lock); +} + static void msg_lock(struct msg_queue *q) { for (int32_t owner = 0; !atomic_compare_exchange_strong(&q->lock, &owner, getpid()); owner = 0) { if (kill(owner, 0) != 0 && errno == ESRCH) atomic_compare_exchange_strong(&q->lock, &owner, 0); @@ -446,12 +490,19 @@ int msgget(key_t key, int msgflg) { } int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg) { - struct msg_queue *q = msg_map(msqid); - if (!q) return -1; - if (msgsz > sizeof(q->slot[0].data) || *(const long *)msgp < 1) { munmap(q, sizeof(*q)); errno = EINVAL; return -1; } + struct msg_map *m = msg_get(msqid); + if (!m) return -1; + struct msg_queue *q = m->q; + if (msgsz > sizeof(q->slot[0].data) || *(const long *)msgp < 1) { msg_put(m); errno = EINVAL; return -1; } for (;;) { msg_lock(q); int32_t seen = atomic_load(&q->changed); + if (msg_removed(q)) { + msg_unlock(q, 0); + msg_put(m); + errno = EIDRM; + return -1; + } for (int i = 0; i < MSG_SLOTS; i++) { if (q->slot[i].seq) continue; q->slot[i].mtype = *(const long *)msgp; @@ -459,12 +510,12 @@ int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg) { memcpy(q->slot[i].data, (const long *)msgp + 1, msgsz); q->slot[i].seq = ++q->next_seq; msg_unlock(q, 1); - munmap(q, sizeof(*q)); + msg_put(m); return 0; } msg_unlock(q, 0); if ((msgflg & IPC_NOWAIT) || (futex(&q->changed, FUTEX_WAIT, seen, NULL) != 0 && errno == EINTR)) { - munmap(q, sizeof(*q)); + msg_put(m); if (msgflg & IPC_NOWAIT) errno = EAGAIN; return -1; } @@ -473,11 +524,18 @@ int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg) { ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg) { if (msgtyp < 0) { errno = EINVAL; return -1; } - struct msg_queue *q = msg_map(msqid); - if (!q) return -1; + struct msg_map *m = msg_get(msqid); + if (!m) return -1; + struct msg_queue *q = m->q; for (;;) { msg_lock(q); int32_t seen = atomic_load(&q->changed); + if (msg_removed(q)) { + msg_unlock(q, 0); + msg_put(m); + errno = EIDRM; + return -1; + } int best = -1; for (int i = 0; i < MSG_SLOTS; i++) if (q->slot[i].seq && (!msgtyp || q->slot[i].mtype == msgtyp) && (best < 0 || q->slot[i].seq < q->slot[best].seq)) @@ -488,12 +546,12 @@ ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg) { memcpy((long *)msgp + 1, q->slot[best].data, n); q->slot[best].seq = 0; msg_unlock(q, 1); - munmap(q, sizeof(*q)); + msg_put(m); return (ssize_t)n; } msg_unlock(q, 0); if ((msgflg & IPC_NOWAIT) || (futex(&q->changed, FUTEX_WAIT, seen, NULL) != 0 && errno == EINTR)) { - munmap(q, sizeof(*q)); + msg_put(m); if (msgflg & IPC_NOWAIT) errno = ENOMSG; return -1; } @@ -502,15 +560,28 @@ ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg) { int msgctl(int msqid, int cmd, struct msqid_ds *buf) { if (cmd != IPC_RMID) { errno = EINVAL; return -1; } + struct msg_queue *q = msg_map(msqid); + if (!q) return -1; pthread_mutex_lock(&g_lock); int rc = unlink_by_id("msg", msqid); pthread_mutex_unlock(&g_lock); + int saved = errno; + if (rc == 0) { + __atomic_store_n(&q->magic, 0, __ATOMIC_SEQ_CST); + atomic_fetch_add(&q->changed, 1); + futex(&q->changed, FUTEX_WAKE, INT_MAX, NULL); + } + munmap(q, sizeof(*q)); + errno = saved; return rc; } static void sysv_lock_before_fork(void) { pthread_mutex_lock(&g_lock); } static void sysv_unlock_after_fork(void) { pthread_mutex_unlock(&g_lock); } -static void sysv_reset_after_fork(void) { pthread_mutex_init(&g_lock, NULL); } +static void sysv_reset_after_fork(void) { + pthread_mutex_init(&g_lock, NULL); + for (int i = 0; i < MAX_OBJECTS; i++) g_msgs[i].refs = 0; +} __attribute__((constructor)) static void install_sysv_fork_handlers(void) { pthread_atfork(sysv_lock_before_fork, sysv_unlock_after_fork, sysv_reset_after_fork); diff --git a/tools/tests/sysv_msg_harness.c b/tools/tests/sysv_msg_harness.c new file mode 100644 index 0000000000..e35bc1d073 --- /dev/null +++ b/tools/tests/sysv_msg_harness.c @@ -0,0 +1,88 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +struct note { long mtype; char text[32]; }; + +static int count_lines(const char *path) { + FILE *f = fopen(path, "r"); + if (!f) return -1; + int n = 0, c; + while ((c = fgetc(f)) != EOF) n += c == '\n'; + fclose(f); + return n; +} + +static void *wait_for_note(void *arg) { + struct note n; + long rc = msgrcv(*(int *)arg, &n, sizeof(n.text), 0, 0); + return (void *)(rc < 0 && errno == EIDRM ? 1L : 0L); +} + +int main(void) { + int q = msgget(0x5eed0002, IPC_CREAT | IPC_EXCL | 0600); + if (q < 0) { perror("msgget"); return 1; } + struct note n; + if (msgrcv(q, &n, sizeof(n.text), 0, IPC_NOWAIT) != -1 || errno != ENOMSG) { printf("FAIL: empty queue\n"); return 1; } + pid_t child = fork(); + if (child == 0) { + struct note a = {2, "second"}, b = {1, "first"}; + return msgsnd(q, &a, sizeof(a.text), 0) || msgsnd(q, &b, sizeof(b.text), 0); + } + int status; + waitpid(child, &status, 0); + if (!WIFEXITED(status) || WEXITSTATUS(status)) { printf("FAIL: child send\n"); return 1; } + if (msgrcv(q, &n, sizeof(n.text), 1, 0) < 0 || n.mtype != 1 || strcmp(n.text, "first")) { printf("FAIL: typed receive\n"); return 1; } + if (msgrcv(q, &n, sizeof(n.text), 0, 0) < 0 || n.mtype != 2 || strcmp(n.text, "second")) { printf("FAIL: any receive\n"); return 1; } + int maps = count_lines("/proc/self/maps"); + for (int i = 0; i < 2000; i++) { + struct note p = {3, "poll"}; + if (msgsnd(q, &p, sizeof(p.text), 0) || msgrcv(q, &n, sizeof(n.text), 0, 0) < 0 || + msgrcv(q, &n, sizeof(n.text), 0, IPC_NOWAIT) != -1) { printf("FAIL: round %d\n", i); return 1; } + } + if (count_lines("/proc/self/maps") != maps) { printf("FAIL: mappings grew\n"); return 1; } + pthread_t waiter; + pthread_create(&waiter, NULL, wait_for_note, &q); + usleep(100000); + if (msgctl(q, IPC_RMID, NULL) != 0) { perror("msgctl"); return 1; } + void *woken; + pthread_join(waiter, &woken); + if (!woken) { printf("FAIL: waiter not told the queue was removed\n"); return 1; } + if (msgsnd(q, &n, sizeof(n.text), IPC_NOWAIT) != -1) { printf("FAIL: send to a removed queue\n"); return 1; } + int again = msgget(0x5eed0002, IPC_CREAT | 0600); + struct note r = {4, "again"}; + if (again < 0 || msgsnd(again, &r, sizeof(r.text), 0) || msgrcv(again, &n, sizeof(n.text), 0, 0) < 0 || strcmp(n.text, "again")) { + printf("FAIL: queue made again after removal\n"); + return 1; + } + msgctl(again, IPC_RMID, NULL); + int watched = msgget(0x5eed0003, IPC_CREAT | 0600); + pid_t blocked = fork(); + if (blocked == 0) return msgrcv(watched, &n, sizeof(n.text), 0, 0) < 0 && errno == EIDRM ? 0 : 1; + int filler[64]; + for (int i = 0; i < 64; i++) { + struct note f = {5, "fill"}; + filler[i] = msgget(0x5eed0100 + i, IPC_CREAT | 0600); + if (filler[i] < 0 || msgsnd(filler[i], &f, sizeof(f.text), 0)) { printf("FAIL: filler %d\n", i); return 1; } + } + usleep(100000); + if (msgctl(watched, IPC_RMID, NULL) != 0) { perror("msgctl with a full cache"); return 1; } + waitpid(blocked, &status, 0); + if (!WIFEXITED(status) || WEXITSTATUS(status)) { printf("FAIL: waiter not woken with a full cache\n"); return 1; } + int extra = msgget(0x5eed0200, IPC_CREAT | 0600); + struct note x = {6, "extra"}; + if (extra < 0 || msgsnd(extra, &x, sizeof(x.text), 0) || msgrcv(extra, &n, sizeof(n.text), 6, 0) < 0 || strcmp(n.text, "extra")) { + printf("FAIL: queue past a full cache\n"); + return 1; + } + msgctl(extra, IPC_RMID, NULL); + for (int i = 0; i < 64; i++) msgctl(filler[i], IPC_RMID, NULL); + printf("PASS: sysv msg\n"); + return 0; +} diff --git a/tools/tests/test_sysv_preload.py b/tools/tests/test_sysv_preload.py index 75828567eb..fc8b8cead8 100644 --- a/tools/tests/test_sysv_preload.py +++ b/tools/tests/test_sysv_preload.py @@ -20,6 +20,15 @@ class SysvPreloadTest(unittest.TestCase): result = subprocess.run([binary], capture_output=True, text=True, timeout=10, env=env) self.assertIn('PASS: sysv otime', result.stdout, result.stdout + result.stderr) + def test_message_queue_mapping_is_reused_and_removal_wakes_waiters(self): + with tempfile.TemporaryDirectory() as folder: + binary = str(Path(folder) / 'test-sysv-msg') + subprocess.run(['gcc', '-Wall', '-Wextra', '-O2', '-pthread', '-o', binary, + str(Path(__file__).resolve().parent / 'sysv_msg_harness.c'), str(PRELOAD / 'sysv.c')], check=True) + env = dict(os.environ, BL_SYSV_DIR=folder) + result = subprocess.run([binary], capture_output=True, text=True, timeout=20, env=env) + self.assertIn('PASS: sysv msg', result.stdout, result.stdout + result.stderr) + if __name__ == '__main__': unittest.main()