smp_processor_id used in preemptable context ?
Vincent Ray
vray at kalrayinc.com
Fri Apr 29 22:27:45 EDT 2022
Thanks for your answer Valdis.
So, if I understand well :
1) the comment in net/core/dev.c:__dev_queue_xmit is indeed misleading :
int cpu = smp_processor_id(); /* ok because BHs are off */
as there is a small chance that we get the wrong info. Disabled BHs are not enough.
Disabling preemption would be needed to be 100% correct.
You agree ?
2) You're saying that, most of the time it's ok, as, at worst, we will be a little wrong on some per-cpu stats.
I'm sure it does not matter if cpu0 gets 45 'events' instead of 46, and cpu1 gets 1 extra for free, but that's only if atomic_inc
or something similar is used. If not, can't we end up with completely broken values, with e.g. cpu0 more or less writing its own count,
totally different from that of cpu1, to cpu1's area ?
3) Finally it looks to me that this particular usage of smp_processor_id() in __dev_queue_xmit()
is more about preventing a dead loop than just gathering statistics.
Although I don't understand everything going on here, it looks like cpu id is directly used to detect
a bad scenario, leading to a critical message :
if (dev->flags & IFF_UP) {
int cpu = smp_processor_id(); /* ok because BHs are off */
/* Other cpus might concurrently change txq->xmit_lock_owner
* to -1 or to their cpu id, but not to our id.
*/
if (READ_ONCE(txq->xmit_lock_owner) != cpu) {
[...]
} else {
/* Recursion is detected! It is possible,
* unfortunately
*/
recursion_alert:
net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
dev->name);
So if our thread is migrated just after smp_processor_id() and lands on the cpu occupied by the thread that is the txq->xmit_lock_owner,
itself just scheduled out, can't we go to the net_crit_ratelimited() ?
Thanks,
V
More information about the Kernelnewbies
mailing list