Kernel sysctl and missing linker sets.

Not really a question. As part of porting other platform code, FreeBSD and Linux, there is a #define macro used to specify module parameters. It is desirable for these new sysctl to show automatically when "upstream" adds them. (without having to manually maintain a list)

This is usually done with "Linker Sets" but they are not available in kexts, mostly due to __mh_execute_header.

I took a different approach with:

#define ZFS_MODULE_PARAM(scope_prefix, name_prefix, name, type, perm, desc) \
    SYSCTL_DECL( _kstat_zfs_darwin_tunable_ ## scope_prefix); \
    SYSCTL_##type( _kstat_zfs_darwin_tunable_ ## scope_prefix, OID_AUTO, name, perm, \
        &name_prefix ## name, 0, desc) ; \
    __attribute__((constructor)) void \
        _zcnst_sysctl__kstat_zfs_darwin_tunable_ ## scope_prefix ## _ ## name (void) \
    { \
        sysctl_register_oid(&sysctl__kstat_zfs_darwin_tunable_ ## scope_prefix ## _ ## name ); \
    } \
    __attribute__((destructor)) void \
        _zdest_sysctl__kstat_zfs_darwin_tunable_ ## scope_prefix ## _ ## name (void) \
    { \
        sysctl_unregister_oid(&sysctl__kstat_zfs_darwin_tunable_ ## scope_prefix ## _ ## name ); \
    }

Ie, when macro is used, I use __attribute__((constructor)) on a function named after the sysctl, which is then called automatically on kext load, and each one of those functions, call sysctl_register_oid().

And likewise for destructor / unregister.

So far it works quite well. Any known drawbacks? I've not tested it on M1.

Kernel sysctl and missing linker sets.
 
 
Q