Page MenuHomeFreeBSD

pf: Avoid taking the pf rules write lock in a couple of ioctls
ClosedPublic

Authored by markj on Thu, Dec 18, 6:13 PM.
Tags
None
Referenced Files
F140156552: D54292.id.diff
Sat, Dec 20, 11:13 PM
F140106283: D54292.diff
Sat, Dec 20, 7:41 AM
Unknown Object (File)
Fri, Dec 19, 1:31 AM
Unknown Object (File)
Fri, Dec 19, 12:11 AM

Details

Summary

The DIOCGETRULES ioctl handlers has taken the write lock ever since
fine-grained locking was merged to pf, but I believe it's unneeded. Use
the read lock instead.

DIOCGETRULENV takes the write lock as well but I believe this is only
required when clearing rule counters. (It might not be required even
then, on platforms where counter increment is done atomically.) Acquire
the read lock if that is not the case.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 69378
Build 66261: arc lint + arc unit

Event Timeline

markj requested review of this revision.Thu, Dec 18, 6:13 PM
This revision is now accepted and ready to land.Thu, Dec 18, 8:04 PM

DIOCGETRULENV takes the write lock as well but I believe this is only
required when clearing rule counters. (It might not be required even
then, on platforms where counter increment is done atomically.) Acquire
the read lock if that is not the case.

I believe the intent behind the write lock is to enable a userspace pattern of repeated fetch+clear without missing counts. If we'd take the read lock to retrieve the counters we could be incrementing them while this is going on, before we clear them again. That'd mean we'd miss packets/bytes/evaluations.

In D54292#1241041, @kp wrote:

DIOCGETRULENV takes the write lock as well but I believe this is only
required when clearing rule counters. (It might not be required even
then, on platforms where counter increment is done atomically.) Acquire
the read lock if that is not the case.

I believe the intent behind the write lock is to enable a userspace pattern of repeated fetch+clear without missing counts. If we'd take the read lock to retrieve the counters we could be incrementing them while this is going on, before we clear them again. That'd mean we'd miss packets/bytes/evaluations.

Ah, that makes sense.