/* DF-2987: refcount_release has no underflow floor: releasing an
 * already-zero count silently wraps to 0xFFFFFFFF and returns FALSE. */
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/refcount.h>

static volatile u_int rc;

static int ref2987_modevent(module_t mod, int type, void *data)
{
    switch (type) {
    case MOD_LOAD:
        rc = 0;
        kprintf("DF2987: rc=0; refcount_release(&rc)...\n");
        int last = refcount_release(&rc);
        kprintf("DF2987: returned %d, rc now 0x%08x (silent wrap = bug)\n",
                last, rc);
        if (rc == 0xFFFFFFFF && last == 0)
            kprintf("DF2987: BUG REPRODUCED - underflowed silently\n");
        break;
    case MOD_UNLOAD:
        break;
    default:
        return EOPNOTSUPP;
    }
    return 0;
}
static moduledata_t ref2987_mod = { "ref2987", ref2987_modevent, NULL };
DECLARE_MODULE(ref2987, ref2987_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
