Skip to content

Commit

Permalink
ZFS: add mutex protection to metaslab_class_t.mc_historgram
Browse files Browse the repository at this point in the history
The mc_histogram fields were unprotected when that code was first
written in "Illumos 4976-4984 - metaslab improvements" (OpenZFS
f3a7f66).  The lock wasn't added until
3dfb57a, though it's unclear exactly
which fields it's supposed to protect.  In any case, it wasn't until
vdev_load was parallelized that any code attempted concurrent access to
those fields.
  • Loading branch information
asomers committed Jan 22, 2021
1 parent a8041d9 commit b00c309
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions module/zfs/metaslab.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ metaslab_class_histogram_verify(metaslab_class_t *mc)
mc_hist = kmem_zalloc(sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE,
KM_SLEEP);

mutex_enter(&mc->mc_lock);
for (int c = 0; c < rvd->vdev_children; c++) {
vdev_t *tvd = rvd->vdev_child[c];
metaslab_group_t *mg = tvd->vdev_mg;
Expand All @@ -542,6 +543,7 @@ metaslab_class_histogram_verify(metaslab_class_t *mc)
for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
VERIFY3U(mc_hist[i], ==, mc->mc_histogram[i]);

mutex_exit(&mc->mc_lock);
kmem_free(mc_hist, sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE);
}

Expand Down Expand Up @@ -1053,12 +1055,14 @@ metaslab_group_histogram_add(metaslab_group_t *mg, metaslab_t *msp)
return;

mutex_enter(&mg->mg_lock);
mutex_enter(&mc->mc_lock);
for (int i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++) {
mg->mg_histogram[i + ashift] +=
msp->ms_sm->sm_phys->smp_histogram[i];
mc->mc_histogram[i + ashift] +=
msp->ms_sm->sm_phys->smp_histogram[i];
}
mutex_exit(&mc->mc_lock);
mutex_exit(&mg->mg_lock);
}

Expand All @@ -1073,6 +1077,7 @@ metaslab_group_histogram_remove(metaslab_group_t *mg, metaslab_t *msp)
return;

mutex_enter(&mg->mg_lock);
mutex_enter(&mc->mc_lock);
for (int i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++) {
ASSERT3U(mg->mg_histogram[i + ashift], >=,
msp->ms_sm->sm_phys->smp_histogram[i]);
Expand All @@ -1084,6 +1089,7 @@ metaslab_group_histogram_remove(metaslab_group_t *mg, metaslab_t *msp)
mc->mc_histogram[i + ashift] -=
msp->ms_sm->sm_phys->smp_histogram[i];
}
mutex_exit(&mc->mc_lock);
mutex_exit(&mg->mg_lock);
}

Expand Down

0 comments on commit b00c309

Please sign in to comment.