10.2.1.3 Atomic Counting With Release Memory Barrier

This style of reference is used in the Linux kernel's networking layer to track the destination caches that are used in packet routing. The actual implementation is quite a bit more involved; this section focuses on the aspects of struct dst_entry reference-count handling that matches this use case, shown in Figure [*].

Figure: Linux Kernel dst_clone API
\begin{figure}{ \scriptsize
\begin{verbatim}1 static inline
2 struct dst_ent...
...dec();
15 atomic_dec(&dst->__refcnt);
16 }
17 }\end{verbatim}
}\end{figure}

The dst_clone() primitive may be used if the caller already has a reference to the specified dst_entry, in which case it obtains another reference that may be handed off to some other entity within the kernel. Because a reference is already held by the caller, dst_clone() need not execute any memory barriers. The act of handing the dst_entry to some other entity might or might not require a memory barrier, but if such a memory barrier is required, it will be embedded in the mechanism used to hand the dst_entry off.

The dst_release() primitive may be invoked from any environment, and the caller might well reference elements of the dst_entry structure immediately prior to the call to dst_release(). The dst_release() primitive therefore contains a memory barrier on line 14 preventing both the compiler and the CPU from misordering accesses.

Please note that the programmer making use of dst_clone() and dst_release() need not be aware of the memory barriers, only of the rules for using these two primitives.

Paul E. McKenney 2011-12-16