Skip to content

Commit 4be2700

Browse files
pcmooredavem330
authored andcommitted
[NetLabel]: correct usage of RCU locking
This fixes some awkward, and perhaps even problematic, RCU lock usage in the NetLabel code as well as some other related trivial cleanups found when looking through the RCU locking. Most of the changes involve removing the redundant RCU read locks wrapping spinlocks in the case of a RCU writer. Signed-off-by: Paul Moore <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 94d3b1e commit 4be2700

File tree

4 files changed

+22
-62
lines changed

4 files changed

+22
-62
lines changed

net/ipv4/cipso_ipv4.c

+10-29
Original file line numberDiff line numberDiff line change
@@ -504,22 +504,16 @@ int cipso_v4_doi_add(struct cipso_v4_doi *doi_def)
504504
INIT_RCU_HEAD(&doi_def->rcu);
505505
INIT_LIST_HEAD(&doi_def->dom_list);
506506

507-
rcu_read_lock();
508-
if (cipso_v4_doi_search(doi_def->doi) != NULL)
509-
goto doi_add_failure_rlock;
510507
spin_lock(&cipso_v4_doi_list_lock);
511508
if (cipso_v4_doi_search(doi_def->doi) != NULL)
512-
goto doi_add_failure_slock;
509+
goto doi_add_failure;
513510
list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
514511
spin_unlock(&cipso_v4_doi_list_lock);
515-
rcu_read_unlock();
516512

517513
return 0;
518514

519-
doi_add_failure_slock:
515+
doi_add_failure:
520516
spin_unlock(&cipso_v4_doi_list_lock);
521-
doi_add_failure_rlock:
522-
rcu_read_unlock();
523517
return -EEXIST;
524518
}
525519

@@ -543,29 +537,23 @@ int cipso_v4_doi_remove(u32 doi,
543537
struct cipso_v4_doi *doi_def;
544538
struct cipso_v4_domhsh_entry *dom_iter;
545539

546-
rcu_read_lock();
547-
if (cipso_v4_doi_search(doi) != NULL) {
548-
spin_lock(&cipso_v4_doi_list_lock);
549-
doi_def = cipso_v4_doi_search(doi);
550-
if (doi_def == NULL) {
551-
spin_unlock(&cipso_v4_doi_list_lock);
552-
rcu_read_unlock();
553-
return -ENOENT;
554-
}
540+
spin_lock(&cipso_v4_doi_list_lock);
541+
doi_def = cipso_v4_doi_search(doi);
542+
if (doi_def != NULL) {
555543
doi_def->valid = 0;
556544
list_del_rcu(&doi_def->list);
557545
spin_unlock(&cipso_v4_doi_list_lock);
546+
rcu_read_lock();
558547
list_for_each_entry_rcu(dom_iter, &doi_def->dom_list, list)
559548
if (dom_iter->valid)
560549
netlbl_domhsh_remove(dom_iter->domain,
561550
audit_info);
562-
cipso_v4_cache_invalidate();
563551
rcu_read_unlock();
564-
552+
cipso_v4_cache_invalidate();
565553
call_rcu(&doi_def->rcu, callback);
566554
return 0;
567555
}
568-
rcu_read_unlock();
556+
spin_unlock(&cipso_v4_doi_list_lock);
569557

570558
return -ENOENT;
571559
}
@@ -653,22 +641,19 @@ int cipso_v4_doi_domhsh_add(struct cipso_v4_doi *doi_def, const char *domain)
653641
new_dom->valid = 1;
654642
INIT_RCU_HEAD(&new_dom->rcu);
655643

656-
rcu_read_lock();
657644
spin_lock(&cipso_v4_doi_list_lock);
658-
list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
645+
list_for_each_entry(iter, &doi_def->dom_list, list)
659646
if (iter->valid &&
660647
((domain != NULL && iter->domain != NULL &&
661648
strcmp(iter->domain, domain) == 0) ||
662649
(domain == NULL && iter->domain == NULL))) {
663650
spin_unlock(&cipso_v4_doi_list_lock);
664-
rcu_read_unlock();
665651
kfree(new_dom->domain);
666652
kfree(new_dom);
667653
return -EEXIST;
668654
}
669655
list_add_tail_rcu(&new_dom->list, &doi_def->dom_list);
670656
spin_unlock(&cipso_v4_doi_list_lock);
671-
rcu_read_unlock();
672657

673658
return 0;
674659
}
@@ -689,23 +674,19 @@ int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi *doi_def,
689674
{
690675
struct cipso_v4_domhsh_entry *iter;
691676

692-
rcu_read_lock();
693677
spin_lock(&cipso_v4_doi_list_lock);
694-
list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
678+
list_for_each_entry(iter, &doi_def->dom_list, list)
695679
if (iter->valid &&
696680
((domain != NULL && iter->domain != NULL &&
697681
strcmp(iter->domain, domain) == 0) ||
698682
(domain == NULL && iter->domain == NULL))) {
699683
iter->valid = 0;
700684
list_del_rcu(&iter->list);
701685
spin_unlock(&cipso_v4_doi_list_lock);
702-
rcu_read_unlock();
703686
call_rcu(&iter->rcu, cipso_v4_doi_domhsh_free);
704-
705687
return 0;
706688
}
707689
spin_unlock(&cipso_v4_doi_list_lock);
708-
rcu_read_unlock();
709690

710691
return -ENOENT;
711692
}

net/netlabel/netlabel_domainhash.c

+11-26
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,9 @@ int netlbl_domhsh_init(u32 size)
178178
for (iter = 0; iter < hsh_tbl->size; iter++)
179179
INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
180180

181-
rcu_read_lock();
182181
spin_lock(&netlbl_domhsh_lock);
183182
rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
184183
spin_unlock(&netlbl_domhsh_lock);
185-
rcu_read_unlock();
186184

187185
return 0;
188186
}
@@ -222,7 +220,6 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry,
222220
entry->valid = 1;
223221
INIT_RCU_HEAD(&entry->rcu);
224222

225-
ret_val = 0;
226223
rcu_read_lock();
227224
if (entry->domain != NULL) {
228225
bkt = netlbl_domhsh_hash(entry->domain);
@@ -233,17 +230,15 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry,
233230
else
234231
ret_val = -EEXIST;
235232
spin_unlock(&netlbl_domhsh_lock);
236-
} else if (entry->domain == NULL) {
233+
} else {
237234
INIT_LIST_HEAD(&entry->list);
238235
spin_lock(&netlbl_domhsh_def_lock);
239236
if (rcu_dereference(netlbl_domhsh_def) == NULL)
240237
rcu_assign_pointer(netlbl_domhsh_def, entry);
241238
else
242239
ret_val = -EEXIST;
243240
spin_unlock(&netlbl_domhsh_def_lock);
244-
} else
245-
ret_val = -EINVAL;
246-
241+
}
247242
audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
248243
if (audit_buf != NULL) {
249244
audit_log_format(audit_buf,
@@ -262,7 +257,6 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry,
262257
audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
263258
audit_log_end(audit_buf);
264259
}
265-
266260
rcu_read_unlock();
267261

268262
if (ret_val != 0) {
@@ -313,38 +307,30 @@ int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
313307
struct audit_buffer *audit_buf;
314308

315309
rcu_read_lock();
316-
if (domain != NULL)
317-
entry = netlbl_domhsh_search(domain, 0);
318-
else
319-
entry = netlbl_domhsh_search(domain, 1);
310+
entry = netlbl_domhsh_search(domain, (domain != NULL ? 0 : 1));
320311
if (entry == NULL)
321312
goto remove_return;
322313
switch (entry->type) {
323-
case NETLBL_NLTYPE_UNLABELED:
324-
break;
325314
case NETLBL_NLTYPE_CIPSOV4:
326-
ret_val = cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
327-
entry->domain);
328-
if (ret_val != 0)
329-
goto remove_return;
315+
cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
316+
entry->domain);
330317
break;
331318
}
332-
ret_val = 0;
333319
if (entry != rcu_dereference(netlbl_domhsh_def)) {
334320
spin_lock(&netlbl_domhsh_lock);
335321
if (entry->valid) {
336322
entry->valid = 0;
337323
list_del_rcu(&entry->list);
338-
} else
339-
ret_val = -ENOENT;
324+
ret_val = 0;
325+
}
340326
spin_unlock(&netlbl_domhsh_lock);
341327
} else {
342328
spin_lock(&netlbl_domhsh_def_lock);
343329
if (entry->valid) {
344330
entry->valid = 0;
345331
rcu_assign_pointer(netlbl_domhsh_def, NULL);
346-
} else
347-
ret_val = -ENOENT;
332+
ret_val = 0;
333+
}
348334
spin_unlock(&netlbl_domhsh_def_lock);
349335
}
350336

@@ -357,11 +343,10 @@ int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
357343
audit_log_end(audit_buf);
358344
}
359345

360-
if (ret_val == 0)
361-
call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
362-
363346
remove_return:
364347
rcu_read_unlock();
348+
if (ret_val == 0)
349+
call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
365350
return ret_val;
366351
}
367352

net/netlabel/netlabel_mgmt.c

-4
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,9 @@ static const struct nla_policy netlbl_mgmt_genl_policy[NLBL_MGMT_A_MAX + 1] = {
8585
*/
8686
void netlbl_mgmt_protocount_inc(void)
8787
{
88-
rcu_read_lock();
8988
spin_lock(&netlabel_mgmt_protocount_lock);
9089
netlabel_mgmt_protocount++;
9190
spin_unlock(&netlabel_mgmt_protocount_lock);
92-
rcu_read_unlock();
9391
}
9492

9593
/**
@@ -103,12 +101,10 @@ void netlbl_mgmt_protocount_inc(void)
103101
*/
104102
void netlbl_mgmt_protocount_dec(void)
105103
{
106-
rcu_read_lock();
107104
spin_lock(&netlabel_mgmt_protocount_lock);
108105
if (netlabel_mgmt_protocount > 0)
109106
netlabel_mgmt_protocount--;
110107
spin_unlock(&netlabel_mgmt_protocount_lock);
111-
rcu_read_unlock();
112108
}
113109

114110
/**

net/netlabel/netlabel_unlabeled.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,10 @@ static void netlbl_unlabel_acceptflg_set(u8 value,
8484
struct audit_buffer *audit_buf;
8585
u8 old_val;
8686

87-
rcu_read_lock();
88-
old_val = netlabel_unlabel_acceptflg;
8987
spin_lock(&netlabel_unlabel_acceptflg_lock);
88+
old_val = netlabel_unlabel_acceptflg;
9089
netlabel_unlabel_acceptflg = value;
9190
spin_unlock(&netlabel_unlabel_acceptflg_lock);
92-
rcu_read_unlock();
9391

9492
audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
9593
audit_info);

0 commit comments

Comments
 (0)