Question Details

No question body available.

Tags

c linux linux-kernel atomic kernel-module

Answers (1)

Accepted Answer Available
Accepted Answer
February 3, 2026 Score: 5 Rep: 379,192 Quality: Expert Completeness: 80%

atomicinc looks useless to me in this case. The only way for this module's code to run is directly from its moduleinit and moduleexit functions, as far as I can tell. Its init function uses scheduletimeout(5 * HZ); to pause before returning for some reason, but doesn't start any kernel tasks to run any other functions. And doesn't register any callback functions which could get called by user-space from read/write/ioctl on a /proc or /sys or device file.

If it did, then concurrent access to static struct listhead head; at global scope in the file would be possible.

And then you'd probably want to be doing atomic ops on static head and the list (aka next) member of the struct as you build the list, if that can still be happening while other cores are running other functions which access it. (Concurrently adding nodes to a lock-free list is relatively easy, with exchange or CAS on the head, or the next pointer of an element. Removal is hard because you can't deallocate while another thread/core might be about to read that memory. I guess in the kernel it's less hard with kmalloced memory since the pointer will always be valid, except maybe with memory hotplug depending on how that works. As long as reading garbage data which doesn't get used is fine, e.g. because this case will lead to CAS failure. In user-space the deallocation problem is hard except in garbage-collected languages; any thread could have just read a pointer to this object, but delay indefinitely before deref.)


Is that one step in a series of example which builds up to something more complicated, like registering callbacks for /proc or /sys or device-file read/write/ioctl functions? If so, maybe the author wrote the final version and then just removed parts to make the earlier examples leading up to it, and missed this simplification that atomict / atomicinc wasn't needed yet.

Related: https://www.kernel.org/doc/Documentation/atomict.txt - you only want atomict for functions like atomicinc, which this code does use. Unlike C11 atomicint and so on, simple assignment or read of a Linux atomic_t variable doesn't have special semantics. It's presumably just a typedef for int or long or something.