[NET]: Clean proto_(un)register from in-code ifdefs
The struct proto has the per-cpu "inuse" counter, which is handled with a special care. All the handling code hides under the ifdef CONFIG_SMP and it introduces some code duplication and makes it look worse than it could. Clean this. Signed-off-by: Pavel Emelyanov <xemul@openvz.org> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
parent
4999f3621f
commit
b733c007ed
1 changed files with 41 additions and 30 deletions
|
@ -1819,23 +1819,48 @@ static int inuse_get(const struct proto *prot)
|
||||||
res += per_cpu_ptr(prot->inuse_ptr, cpu)[0];
|
res += per_cpu_ptr(prot->inuse_ptr, cpu)[0];
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int inuse_init(struct proto *prot)
|
||||||
|
{
|
||||||
|
if (!prot->inuse_getval || !prot->inuse_add) {
|
||||||
|
prot->inuse_ptr = alloc_percpu(int);
|
||||||
|
if (prot->inuse_ptr == NULL)
|
||||||
|
return -ENOBUFS;
|
||||||
|
|
||||||
|
prot->inuse_getval = inuse_get;
|
||||||
|
prot->inuse_add = inuse_add;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void inuse_fini(struct proto *prot)
|
||||||
|
{
|
||||||
|
if (prot->inuse_ptr != NULL) {
|
||||||
|
free_percpu(prot->inuse_ptr);
|
||||||
|
prot->inuse_ptr = NULL;
|
||||||
|
prot->inuse_getval = NULL;
|
||||||
|
prot->inuse_add = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
static inline int inuse_init(struct proto *prot)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void inuse_fini(struct proto *prot)
|
||||||
|
{
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int proto_register(struct proto *prot, int alloc_slab)
|
int proto_register(struct proto *prot, int alloc_slab)
|
||||||
{
|
{
|
||||||
char *request_sock_slab_name = NULL;
|
char *request_sock_slab_name = NULL;
|
||||||
char *timewait_sock_slab_name;
|
char *timewait_sock_slab_name;
|
||||||
int rc = -ENOBUFS;
|
|
||||||
|
|
||||||
#ifdef CONFIG_SMP
|
if (inuse_init(prot))
|
||||||
if (!prot->inuse_getval || !prot->inuse_add) {
|
goto out;
|
||||||
prot->inuse_ptr = alloc_percpu(int);
|
|
||||||
if (prot->inuse_ptr == NULL)
|
|
||||||
goto out;
|
|
||||||
prot->inuse_getval = inuse_get;
|
|
||||||
prot->inuse_add = inuse_add;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (alloc_slab) {
|
if (alloc_slab) {
|
||||||
prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
|
prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
|
||||||
SLAB_HWCACHE_ALIGN, NULL);
|
SLAB_HWCACHE_ALIGN, NULL);
|
||||||
|
@ -1887,9 +1912,8 @@ int proto_register(struct proto *prot, int alloc_slab)
|
||||||
write_lock(&proto_list_lock);
|
write_lock(&proto_list_lock);
|
||||||
list_add(&prot->node, &proto_list);
|
list_add(&prot->node, &proto_list);
|
||||||
write_unlock(&proto_list_lock);
|
write_unlock(&proto_list_lock);
|
||||||
rc = 0;
|
return 0;
|
||||||
out:
|
|
||||||
return rc;
|
|
||||||
out_free_timewait_sock_slab_name:
|
out_free_timewait_sock_slab_name:
|
||||||
kfree(timewait_sock_slab_name);
|
kfree(timewait_sock_slab_name);
|
||||||
out_free_request_sock_slab:
|
out_free_request_sock_slab:
|
||||||
|
@ -1903,15 +1927,9 @@ int proto_register(struct proto *prot, int alloc_slab)
|
||||||
kmem_cache_destroy(prot->slab);
|
kmem_cache_destroy(prot->slab);
|
||||||
prot->slab = NULL;
|
prot->slab = NULL;
|
||||||
out_free_inuse:
|
out_free_inuse:
|
||||||
#ifdef CONFIG_SMP
|
inuse_fini(prot);
|
||||||
if (prot->inuse_ptr != NULL) {
|
out:
|
||||||
free_percpu(prot->inuse_ptr);
|
return -ENOBUFS;
|
||||||
prot->inuse_ptr = NULL;
|
|
||||||
prot->inuse_getval = NULL;
|
|
||||||
prot->inuse_add = NULL;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
goto out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EXPORT_SYMBOL(proto_register);
|
EXPORT_SYMBOL(proto_register);
|
||||||
|
@ -1922,14 +1940,7 @@ void proto_unregister(struct proto *prot)
|
||||||
list_del(&prot->node);
|
list_del(&prot->node);
|
||||||
write_unlock(&proto_list_lock);
|
write_unlock(&proto_list_lock);
|
||||||
|
|
||||||
#ifdef CONFIG_SMP
|
inuse_fini(prot);
|
||||||
if (prot->inuse_ptr != NULL) {
|
|
||||||
free_percpu(prot->inuse_ptr);
|
|
||||||
prot->inuse_ptr = NULL;
|
|
||||||
prot->inuse_getval = NULL;
|
|
||||||
prot->inuse_add = NULL;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (prot->slab != NULL) {
|
if (prot->slab != NULL) {
|
||||||
kmem_cache_destroy(prot->slab);
|
kmem_cache_destroy(prot->slab);
|
||||||
prot->slab = NULL;
|
prot->slab = NULL;
|
||||||
|
|
Loading…
Add table
Reference in a new issue