Skip to content

Commit cc34acd

Browse files
Stanislav Fomichevkuba-moo
authored andcommitted
docs: net: document new locking reality
Also clarify ndo_get_stats (that read and write paths can run concurrently) and mention only RCU. Cc: Saeed Mahameed <[email protected]> Signed-off-by: Stanislav Fomichev <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 605ef7a commit cc34acd

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

Documentation/networking/netdevices.rst

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,49 +210,55 @@ packets is preferred.
210210
struct net_device synchronization rules
211211
=======================================
212212
ndo_open:
213-
Synchronization: rtnl_lock() semaphore.
213+
Synchronization: rtnl_lock() semaphore. In addition, netdev instance
214+
lock if the driver implements queue management or shaper API.
214215
Context: process
215216

216217
ndo_stop:
217-
Synchronization: rtnl_lock() semaphore.
218+
Synchronization: rtnl_lock() semaphore. In addition, netdev instance
219+
lock if the driver implements queue management or shaper API.
218220
Context: process
219221
Note: netif_running() is guaranteed false
220222

221223
ndo_do_ioctl:
222224
Synchronization: rtnl_lock() semaphore.
223-
Context: process
224225

225-
This is only called by network subsystems internally,
226-
not by user space calling ioctl as it was in before
227-
linux-5.14.
226+
This is only called by network subsystems internally,
227+
not by user space calling ioctl as it was in before
228+
linux-5.14.
228229

229230
ndo_siocbond:
230-
Synchronization: rtnl_lock() semaphore.
231+
Synchronization: rtnl_lock() semaphore. In addition, netdev instance
232+
lock if the driver implements queue management or shaper API.
231233
Context: process
232234

233-
Used by the bonding driver for the SIOCBOND family of
234-
ioctl commands.
235+
Used by the bonding driver for the SIOCBOND family of
236+
ioctl commands.
235237

236238
ndo_siocwandev:
237-
Synchronization: rtnl_lock() semaphore.
239+
Synchronization: rtnl_lock() semaphore. In addition, netdev instance
240+
lock if the driver implements queue management or shaper API.
238241
Context: process
239242

240243
Used by the drivers/net/wan framework to handle
241244
the SIOCWANDEV ioctl with the if_settings structure.
242245

243246
ndo_siocdevprivate:
244-
Synchronization: rtnl_lock() semaphore.
247+
Synchronization: rtnl_lock() semaphore. In addition, netdev instance
248+
lock if the driver implements queue management or shaper API.
245249
Context: process
246250

247251
This is used to implement SIOCDEVPRIVATE ioctl helpers.
248252
These should not be added to new drivers, so don't use.
249253

250254
ndo_eth_ioctl:
251-
Synchronization: rtnl_lock() semaphore.
255+
Synchronization: rtnl_lock() semaphore. In addition, netdev instance
256+
lock if the driver implements queue management or shaper API.
252257
Context: process
253258

254259
ndo_get_stats:
255-
Synchronization: rtnl_lock() semaphore, or RCU.
260+
Synchronization: RCU (can be called concurrently with the stats
261+
update path).
256262
Context: atomic (can't sleep under RCU)
257263

258264
ndo_start_xmit:
@@ -284,6 +290,10 @@ ndo_set_rx_mode:
284290
Synchronization: netif_addr_lock spinlock.
285291
Context: BHs disabled
286292

293+
Most ndo callbacks not specified in the list above are running
294+
under ``rtnl_lock``. In addition, netdev instance lock is taken as well if
295+
the driver implements queue management or shaper API.
296+
287297
struct napi_struct synchronization rules
288298
========================================
289299
napi->poll:
@@ -298,6 +308,35 @@ napi->poll:
298308
softirq
299309
will be called with interrupts disabled by netconsole.
300310

311+
struct netdev_queue_mgmt_ops synchronization rules
312+
==================================================
313+
314+
All queue management ndo callbacks are holding netdev instance lock.
315+
316+
RTNL and netdev instance lock
317+
=============================
318+
319+
Historically, all networking control operations were protected by a single
320+
global lock known as ``rtnl_lock``. There is an ongoing effort to replace this
321+
global lock with separate locks for each network namespace. Additionally,
322+
properties of individual netdev are increasingly protected by per-netdev locks.
323+
324+
For device drivers that implement shaping or queue management APIs, all control
325+
operations will be performed under the netdev instance lock. Currently, this
326+
instance lock is acquired within the context of ``rtnl_lock``. The drivers
327+
can also explicitly request instance lock to be acquired via
328+
``request_ops_lock``. In the future, there will be an option for individual
329+
drivers to opt out of using ``rtnl_lock`` and instead perform their control
330+
operations directly under the netdev instance lock.
331+
332+
Devices drivers are encouraged to rely on the instance lock where possible.
333+
334+
For the (mostly software) drivers that need to interact with the core stack,
335+
there are two sets of interfaces: ``dev_xxx`` and ``netif_xxx`` (e.g.,
336+
``dev_set_mtu`` and ``netif_set_mtu``). The ``dev_xxx`` functions handle
337+
acquiring the instance lock themselves, while the ``netif_xxx`` functions
338+
assume that the driver has already acquired the instance lock.
339+
301340
NETDEV_INTERNAL symbol namespace
302341
================================
303342

include/linux/netdevice.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,6 +2505,10 @@ struct net_device {
25052505
*
25062506
* Also protects some fields in struct napi_struct.
25072507
*
2508+
* For the drivers that implement shaper or queue API, the scope
2509+
* of this lock is expanded to cover most ndo/queue/ethtool/sysfs
2510+
* operations.
2511+
*
25082512
* Ordering: take after rtnl_lock.
25092513
*/
25102514
struct mutex lock;

0 commit comments

Comments
 (0)