LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 01-11-2024, 11:13 AM   #1
pappasbrent
LQ Newbie
 
Registered: Jan 2024
Distribution: Ubuntu
Posts: 3

Rep: Reputation: 0
Question Is it safe to invoke rcu_dereference() outside RCU critical sections in the kernel?


Hi all,

I have been reading the Linux kernel's RCU API documentation, and the docs seem to indicate that it is incorrect to call rcu_dereference() outside of an RCU read-side critical section (i.e., a section of code that begins with a call to rcu_read_lock() and ends with a call to rcu_read_unlock()).
However, I found several instances in the Linux kernel where this macro is invoked outside of a critical section.
Here is an example taken from line 5004 of net/mac80211/tx.c:

Code:
resp = rcu_dereference(link->u.ap.probe_resp);
Since this call to rcu_dereference() is outside a critical section, is it therefore erroneous?
Or am I misunderstanding how the RCU API works?
 
Old 01-12-2024, 12:43 PM   #2
pappasbrent
LQ Newbie
 
Registered: Jan 2024
Distribution: Ubuntu
Posts: 3

Original Poster
Rep: Reputation: 0
So I took a deeper look at the invocation I linked to in my previous post in this thread, and I made an interesting discovery.
The invocation of rcu_dereference() on line 5004 of net/mac80211/tx.c is inside the definition of the function ieee80211_set_beacon_cntdwn().
This function is only invoked inside RCU read-side critical sections, so I suppose that transitively that means that all calls to rcu_dereference() inside its definition are called within critical sections as well.
This is fine, but leads me to another question: Should the definition of ieee80211_set_beacon_cntdwn() be annotated with one of the RCU annotation macros (e.g., __rcu, __user, or __kernel; defined on line 28 of include/linux/compiler_types.h) to signify that it is only to be called within RCU critical sections (I'm assuming the __rcu annotation)?

Last edited by pappasbrent; 01-12-2024 at 12:45 PM.
 
Old 01-12-2024, 01:14 PM   #3
pappasbrent
LQ Newbie
 
Registered: Jan 2024
Distribution: Ubuntu
Posts: 3

Original Poster
Rep: Reputation: 0
Ok, I think I figured it out!
The __rcu macro I mentioned in my last post expands to the noderef and address_space() macro annotations, which according to this page, should only be used to data structures.
Therefore my previous idea to use this macro to annotate the definition of the function ieee80211_set_beacon_cntdwn() was wrong.
Instead, I think ieee80211_set_beacon_cntdwn() should be annotated with the __must_hold() macro, which according to the sparse documentation should be passed the name of the struct that the function is expected to have a lock on when it is called.
The call to rcu_dereference() in the body of ieee80211_set_beacon_cntdwn() is passed the struct named "link", like so:

Code:
rcu_dereference(link->u.ap.probe_resp)
So I think the annotation should ultimately be:

Code:
__must_hold(link)
Can anyone with more kernel development experience confirm if my reasoning is correct here?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Current64 - Fri Apr 20 updates - RCU kernel options burdi01 Slackware 11 04-26-2018 11:09 PM
how rcu list entry be protected in ip_vs_add_dest() & ip_vs_del_dest() [centos 7.2 kernel 3.10.327] newcent Linux - Kernel 0 10-21-2017 10:46 AM
LXer: Linux 2.6.38 could improve kernel performance by 50 percent, thanks to new RCU lookup. LXer Syndicated Linux News 0 01-19-2011 10:20 PM
fundamental question on threads and critical sections yanivwehtam Programming 9 04-17-2009 08:30 AM
LXer: Using flock to protect critical sections in shell scripts LXer Syndicated Linux News 0 01-12-2009 02:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 01:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration