From 3e60028e006e0d64594c03e44d7bec91c89c3440 Mon Sep 17 00:00:00 2001 From: Edwin Wu Date: Thu, 26 Jun 2025 23:30:03 +0800 Subject: [PATCH] Remove redundant memory barrier The memory barrier before queuing AI work was intended to ensure that changes to the global variables 'finish' and 'turn' are visible to other CPUs. However, as documented in workqueue.h, 'queue_work()' provides the required memory-ordering guarantees: all stores preceding the call to 'queue_work()' will be visible to the CPU that executes the work before the work function begins. In this project, two AI work items take turns making moves. A work item is only queued after the other has finished executing, ensuring that at any given time, at most one AI work item is on the workqueue. Under this usage pattern, 'queue_work()' always returns true, which implies that the ordering guarantees are valid and the work was not already queued. Therefore, extra memory barriers are unnecessary. Tested by executing over 1000 games to verify that the turn ordering is always correct (i.e., strictly alternating between O and X). --- main.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/main.c b/main.c index fb23a0b..2019593 100644 --- a/main.c +++ b/main.c @@ -292,11 +292,9 @@ static void game_tasklet_func(unsigned long __data) if (finish && turn == 'O') { WRITE_ONCE(finish, 0); - smp_wmb(); queue_work(kxo_workqueue, &ai_one_work); } else if (finish && turn == 'X') { WRITE_ONCE(finish, 0); - smp_wmb(); queue_work(kxo_workqueue, &ai_two_work); } queue_work(kxo_workqueue, &drawboard_work);