Skip to content

Commit 2a7ee69

Browse files
Tuong Liendavem330
authored andcommitted
tipc: add reference counter to bearer
As a need to support the crypto asynchronous operations in the later commits, apart from the current RCU mechanism for bearer pointer, we add a 'refcnt' to the bearer object as well. So, a bearer can be hold via 'tipc_bearer_hold()' without being freed even though the bearer or interface can be disabled in the meanwhile. If that happens, the bearer will be released then when the crypto operation is completed and 'tipc_bearer_put()' is called. Acked-by: Ying Xue <[email protected]> Acked-by: Jon Maloy <[email protected]> Signed-off-by: Tuong Lien <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f1ff4e8 commit 2a7ee69

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

net/tipc/bearer.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ static int tipc_enable_bearer(struct net *net, const char *name,
315315
b->net_plane = bearer_id + 'A';
316316
b->priority = prio;
317317
test_and_set_bit_lock(0, &b->up);
318+
refcount_set(&b->refcnt, 1);
318319

319320
res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
320321
if (res) {
@@ -351,6 +352,17 @@ static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b)
351352
return 0;
352353
}
353354

355+
bool tipc_bearer_hold(struct tipc_bearer *b)
356+
{
357+
return (b && refcount_inc_not_zero(&b->refcnt));
358+
}
359+
360+
void tipc_bearer_put(struct tipc_bearer *b)
361+
{
362+
if (b && refcount_dec_and_test(&b->refcnt))
363+
kfree_rcu(b, rcu);
364+
}
365+
354366
/**
355367
* bearer_disable
356368
*
@@ -369,7 +381,7 @@ static void bearer_disable(struct net *net, struct tipc_bearer *b)
369381
if (b->disc)
370382
tipc_disc_delete(b->disc);
371383
RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL);
372-
kfree_rcu(b, rcu);
384+
tipc_bearer_put(b);
373385
tipc_mon_delete(net, bearer_id);
374386
}
375387

net/tipc/bearer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ struct tipc_bearer {
165165
struct tipc_discoverer *disc;
166166
char net_plane;
167167
unsigned long up;
168+
refcount_t refcnt;
168169
};
169170

170171
struct tipc_bearer_names {
@@ -210,6 +211,8 @@ int tipc_media_set_window(const char *name, u32 new_value);
210211
int tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a);
211212
int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
212213
struct nlattr *attrs[]);
214+
bool tipc_bearer_hold(struct tipc_bearer *b);
215+
void tipc_bearer_put(struct tipc_bearer *b);
213216
void tipc_disable_l2_media(struct tipc_bearer *b);
214217
int tipc_l2_send_msg(struct net *net, struct sk_buff *buf,
215218
struct tipc_bearer *b, struct tipc_media_addr *dest);

0 commit comments

Comments
 (0)