When and when not to use CTLFLAG_MPSAFE with the SYSCTL macros..?

Oliver Pinter oliver.pinter at hardenedbsd.org
Fri Aug 7 20:53:52 UTC 2015


On 8/7/15, John Baldwin <jhb at freebsd.org> wrote:
> On Wednesday, August 05, 2015 08:38:18 AM Hans Petter Selasky wrote:
>> On 08/05/15 00:47, Garrett Cooper wrote:
>> > Hi,
>> > 	I’ve been trying to figure out (because sysctl(9) is lacking) when to
>> > use CTLFLAG_MPSAFE. Is it strictly when dealing with SYSCTL_PROC
>> > handlers that do proper locking of shared resources, or are there other
>> > nuances that need to be handled?
>> > 	I’m also asking because SYSCTL_UQUAD, for instance, explicitly uses
>> > CTLFLAG_MPSAFE in the handler, which is a bit confusing.
>> > Thanks!
>> > -NGie
>>
>> Hi,
>>
>> This flag decides if you have Giant automatically locked or not around
>> the sysctl proc. Your functions should have their own locks basically. I
>> believe it is a leftover from many years ago, when the FreeBSD kernel
>> was going multi threaded.
>
> It's only about 2-3 years old actually.
>
> To answer your question Garrett: yes it is really only for SYSCTL_PROC
> handlers.  The existing "simple" handlers like sysctl_handle_int are
> as atomic as they can be regardless of Giant, so they don't need Giant.

For the basic types the CTLFLAG_MPSAFE flag are always added:
https://github.com/freebsd/freebsd/blob/master/sys/sys/sysctl.h#L322 .

>
> If you have a variable that you want to control access to via locking you
> need to use a custom handler, even if it is a simple int.
>
> --
> John Baldwin
> _______________________________________________
> freebsd-hackers at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe at freebsd.org"


More information about the freebsd-hackers mailing list