SPIN_LOCK 小註解

最近看到一個 Kernel config 叫做 CONFIG_UNINLINE_SPIN_UNLOCK, 這 un 又 un, 真的是讓人看得眼花. 雖然說現在人強調 “減法" 的思維, 看這個名詞還是得用 “加法".

STEP 1: SPIN_LOCK [1], 多處理器共用一個資源時, 透過 SPIN_LOCK 去鎖住不讓其他人用. 自己用完後, 設為 unlock, 別人就可以用. 怎麼知道可不可以用呢? 要透過 trylock() 來判斷是否拿到資源. 如果拿不到回直接回傳 busy, 除非一直 try 否則也不會浪費資源. 真的 try 不停就要看演算了 [2].

STEP 2: INLINE_SPIN_LOCK. inline 是指會將 spin_lock() 插入程式之中. 但它也不只是當作巨集處理而已, 參考 [3] 這段話, 它會把 spin_lock() 這個函式的內容優化之後才代入. 

The point of making a function inline is to hint to the compiler that it is worth making some form of extra effort to call the function faster than it would otherwise – generally by substituting the code of the function into its caller. As well as eliminating the need for a call and return sequence, it might allow the compiler to perform certain optimizations between the bodies of both functions.

STEP 3:  INLINE_SPIN_UNLOCK. 有鎖的動作就有解鎖. 使用之前, kernel config 要先打開. 追溯到 2012 年, 當初只有 CONFIG_INLINE_SPIN_UNLOCK [4].

STEP 4:  Linus Torvalds 自己建議把原本預設開 CONFIG_INLINE_SPIN_UNLOCK, 改為預設 CONFIG_UNINLINE_SPIN_UNLOCK 未定義 [4].

-CONFIG_INLINE_SPIN_UNLOCK=y
+# CONFIG_UNINLINE_SPIN_UNLOCK is not set

想要 debug inline spin unlock 的人如果想要加 debug code, 開這個 config 就不會因為 inline 的作用, 使自己的 code 和原本 inline 的函式融為一體, 以致於無法正確地 debug.

預期的路人心得:  不能因為負負得正, 就把 CONFIG_UNINLINE_SPIN_UNLOCK 想成 CONFIG_INLINE_SPIN_LOCK. 哈!

[Ref]

  1. https://blog.csdn.net/u010144805/article/details/80354679
  2. https://locklessinc.com/articles/locks/
  3. http://www.greenend.org.uk/rjk/tech/inline.html
  4. http://lkml.iu.edu/hypermail/linux/kernel/1203.2/03015.html

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

%d 位部落客按了讚: