Skip to content

Commit 33940d0

Browse files
Joern EngelNicholas Bellinger
authored andcommitted
target: encapsulate smp_mb__after_atomic()
The target code has a rather generous helping of smp_mb__after_atomic() throughout the code base. Most atomic operations were followed by one and none were preceded by smp_mb__before_atomic(), nor accompanied by a comment explaining the need for a barrier. Instead of trying to prove for every case whether or not it is needed, this patch introduces atomic_inc_mb() and atomic_dec_mb(), which explicitly include the memory barriers before and after the atomic operation. For now they are defined in a target header, although they could be of general use. Most of the existing atomic/mb combinations were replaced by the new helpers. In a few cases the atomic was sandwiched in spin_lock/spin_unlock and I simply removed the barrier. I suspect that in most cases the correct conversion would have been to drop the barrier. I also suspect that a few cases exist where a) the barrier was necessary and b) a second barrier before the atomic would have been necessary and got added by this patch. Signed-off-by: Joern Engel <joern@logfs.org> Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
1 parent 74ed7e6 commit 33940d0

7 files changed

Lines changed: 70 additions & 115 deletions

File tree

‎drivers/target/loopback/tcm_loop.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -960,8 +960,7 @@ static int tcm_loop_port_link(
960960
struct tcm_loop_tpg, tl_se_tpg);
961961
struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
962962

963-
atomic_inc(&tl_tpg->tl_tpg_port_count);
964-
smp_mb__after_atomic();
963+
atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
965964
/*
966965
* Add Linux/SCSI struct scsi_device by HCTL
967966
*/
@@ -995,8 +994,7 @@ static void tcm_loop_port_unlink(
995994
scsi_remove_device(sd);
996995
scsi_device_put(sd);
997996

998-
atomic_dec(&tl_tpg->tl_tpg_port_count);
999-
smp_mb__after_atomic();
997+
atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
1000998

1001999
pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
10021000
}

‎drivers/target/target_core_alua.c‎

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,7 @@ target_emulate_set_target_port_groups(struct se_cmd *cmd)
392392
if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
393393
continue;
394394

395-
atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
396-
smp_mb__after_atomic();
395+
atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
397396

398397
spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
399398

@@ -403,8 +402,7 @@ target_emulate_set_target_port_groups(struct se_cmd *cmd)
403402
found = true;
404403

405404
spin_lock(&dev->t10_alua.tg_pt_gps_lock);
406-
atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
407-
smp_mb__after_atomic();
405+
atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
408406
break;
409407
}
410408
spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
@@ -998,8 +996,7 @@ static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
998996
* every I_T nexus other than the I_T nexus on which the SET
999997
* TARGET PORT GROUPS command
1000998
*/
1001-
atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
1002-
smp_mb__after_atomic();
999+
atomic_inc_mb(&mem->tg_pt_gp_mem_ref_cnt);
10031000
spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
10041001

10051002
spin_lock_bh(&port->sep_alua_lock);
@@ -1028,8 +1025,7 @@ static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
10281025
spin_unlock_bh(&port->sep_alua_lock);
10291026

10301027
spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1031-
atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
1032-
smp_mb__after_atomic();
1028+
atomic_dec_mb(&mem->tg_pt_gp_mem_ref_cnt);
10331029
}
10341030
spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
10351031
/*
@@ -1063,7 +1059,6 @@ static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
10631059
core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_pending_state));
10641060
spin_lock(&dev->t10_alua.tg_pt_gps_lock);
10651061
atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1066-
smp_mb__after_atomic();
10671062
spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
10681063

10691064
if (tg_pt_gp->tg_pt_gp_transition_complete)
@@ -1125,7 +1120,6 @@ static int core_alua_do_transition_tg_pt(
11251120
*/
11261121
spin_lock(&dev->t10_alua.tg_pt_gps_lock);
11271122
atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1128-
smp_mb__after_atomic();
11291123
spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
11301124

11311125
if (!explicit && tg_pt_gp->tg_pt_gp_implicit_trans_secs) {
@@ -1168,7 +1162,6 @@ int core_alua_do_port_transition(
11681162
spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
11691163
lu_gp = local_lu_gp_mem->lu_gp;
11701164
atomic_inc(&lu_gp->lu_gp_ref_cnt);
1171-
smp_mb__after_atomic();
11721165
spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
11731166
/*
11741167
* For storage objects that are members of the 'default_lu_gp',
@@ -1184,8 +1177,7 @@ int core_alua_do_port_transition(
11841177
l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
11851178
rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
11861179
new_state, explicit);
1187-
atomic_dec(&lu_gp->lu_gp_ref_cnt);
1188-
smp_mb__after_atomic();
1180+
atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
11891181
return rc;
11901182
}
11911183
/*
@@ -1198,8 +1190,7 @@ int core_alua_do_port_transition(
11981190
lu_gp_mem_list) {
11991191

12001192
dev = lu_gp_mem->lu_gp_mem_dev;
1201-
atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
1202-
smp_mb__after_atomic();
1193+
atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
12031194
spin_unlock(&lu_gp->lu_gp_lock);
12041195

12051196
spin_lock(&dev->t10_alua.tg_pt_gps_lock);
@@ -1227,8 +1218,7 @@ int core_alua_do_port_transition(
12271218
tg_pt_gp->tg_pt_gp_alua_port = NULL;
12281219
tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
12291220
}
1230-
atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1231-
smp_mb__after_atomic();
1221+
atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
12321222
spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
12331223
/*
12341224
* core_alua_do_transition_tg_pt() will always return
@@ -1238,16 +1228,14 @@ int core_alua_do_port_transition(
12381228
new_state, explicit);
12391229

12401230
spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1241-
atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1242-
smp_mb__after_atomic();
1231+
atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
12431232
if (rc)
12441233
break;
12451234
}
12461235
spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
12471236

12481237
spin_lock(&lu_gp->lu_gp_lock);
1249-
atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
1250-
smp_mb__after_atomic();
1238+
atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
12511239
}
12521240
spin_unlock(&lu_gp->lu_gp_lock);
12531241

@@ -1260,8 +1248,7 @@ int core_alua_do_port_transition(
12601248
core_alua_dump_state(new_state));
12611249
}
12621250

1263-
atomic_dec(&lu_gp->lu_gp_ref_cnt);
1264-
smp_mb__after_atomic();
1251+
atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
12651252
return rc;
12661253
}
12671254

‎drivers/target/target_core_device.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ struct se_dev_entry *core_get_se_deve_from_rtpi(
224224
if (port->sep_rtpi != rtpi)
225225
continue;
226226

227-
atomic_inc(&deve->pr_ref_count);
228-
smp_mb__after_atomic();
227+
atomic_inc_mb(&deve->pr_ref_count);
229228
spin_unlock_irq(&nacl->device_list_lock);
230229

231230
return deve;
@@ -1388,8 +1387,7 @@ int core_dev_add_initiator_node_lun_acl(
13881387

13891388
spin_lock(&lun->lun_acl_lock);
13901389
list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
1391-
atomic_inc(&lun->lun_acl_count);
1392-
smp_mb__after_atomic();
1390+
atomic_inc_mb(&lun->lun_acl_count);
13931391
spin_unlock(&lun->lun_acl_lock);
13941392

13951393
pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
@@ -1422,8 +1420,7 @@ int core_dev_del_initiator_node_lun_acl(
14221420

14231421
spin_lock(&lun->lun_acl_lock);
14241422
list_del(&lacl->lacl_list);
1425-
atomic_dec(&lun->lun_acl_count);
1426-
smp_mb__after_atomic();
1423+
atomic_dec_mb(&lun->lun_acl_count);
14271424
spin_unlock(&lun->lun_acl_lock);
14281425

14291426
core_disable_device_list_for_node(lun, NULL, lacl->mapped_lun,

0 commit comments

Comments
 (0)