Skip to content

Commit

Permalink
FreeBSD: Fix memory leaks in kstats
Browse files Browse the repository at this point in the history
Don't handle (incorrectly) kmem_zalloc() failure.  With KM_SLEEP,
will never return NULL.

Free the data allocated for non-virtual kstats when deleting the object.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Alexander Motin <mav@FreeBSD.org>
Signed-off-by: Ryan Moeller <ryan@iXsystems.com>
Closes openzfs#11767
  • Loading branch information
Ryan Moeller authored and RageLtMan committed May 31, 2021
1 parent 6603d90 commit 9dafa3d
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions module/os/freebsd/spl/spl_kstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,10 @@ __kstat_create(const char *module, int instance, const char *name,
panic("Undefined kstat type %d\n", ksp->ks_type);
}

if (ksp->ks_flags & KSTAT_FLAG_VIRTUAL) {
if (ksp->ks_flags & KSTAT_FLAG_VIRTUAL)
ksp->ks_data = NULL;
} else {
else
ksp->ks_data = kmem_zalloc(ksp->ks_data_size, KM_SLEEP);
if (ksp->ks_data == NULL) {
kmem_free(ksp, sizeof (*ksp));
ksp = NULL;
}
}

/*
* Some kstats use a module name like "zfs/poolname" to distinguish a
Expand Down Expand Up @@ -509,6 +504,8 @@ kstat_delete(kstat_t *ksp)
sysctl_ctx_free(&ksp->ks_sysctl_ctx);
ksp->ks_lock = NULL;
mutex_destroy(&ksp->ks_private_lock);
if (!(ksp->ks_flags & KSTAT_FLAG_VIRTUAL))
kmem_free(ksp->ks_data, ksp->ks_data_size);
free(ksp, M_KSTAT);
}

Expand Down

0 comments on commit 9dafa3d

Please sign in to comment.