net: add req_prot_cleanup() & req_prot_init() helpers

Make proto_register() & proto_unregister() a bit nicer.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Eric Dumazet 2015-03-12 16:44:07 -07:00 committed by David S. Miller
parent 1e2e01172f
commit 0159dfd3d7
1 changed files with 41 additions and 28 deletions

View File

@ -2726,6 +2726,42 @@ static inline void release_proto_idx(struct proto *prot)
} }
#endif #endif
static void req_prot_cleanup(struct request_sock_ops *rsk_prot)
{
if (!rsk_prot)
return;
kfree(rsk_prot->slab_name);
rsk_prot->slab_name = NULL;
if (rsk_prot->slab) {
kmem_cache_destroy(rsk_prot->slab);
rsk_prot->slab = NULL;
}
}
static int req_prot_init(const struct proto *prot)
{
struct request_sock_ops *rsk_prot = prot->rsk_prot;
if (!rsk_prot)
return 0;
rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s",
prot->name);
if (!rsk_prot->slab_name)
return -ENOMEM;
rsk_prot->slab = kmem_cache_create(rsk_prot->slab_name,
rsk_prot->obj_size, 0,
SLAB_HWCACHE_ALIGN, NULL);
if (!rsk_prot->slab) {
pr_crit("%s: Can't create request sock SLAB cache!\n",
prot->name);
return -ENOMEM;
}
return 0;
}
int proto_register(struct proto *prot, int alloc_slab) int proto_register(struct proto *prot, int alloc_slab)
{ {
if (alloc_slab) { if (alloc_slab) {
@ -2739,21 +2775,8 @@ int proto_register(struct proto *prot, int alloc_slab)
goto out; goto out;
} }
if (prot->rsk_prot != NULL) { if (req_prot_init(prot))
prot->rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s", prot->name); goto out_free_request_sock_slab;
if (prot->rsk_prot->slab_name == NULL)
goto out_free_sock_slab;
prot->rsk_prot->slab = kmem_cache_create(prot->rsk_prot->slab_name,
prot->rsk_prot->obj_size, 0,
SLAB_HWCACHE_ALIGN, NULL);
if (prot->rsk_prot->slab == NULL) {
pr_crit("%s: Can't create request sock SLAB cache!\n",
prot->name);
goto out_free_request_sock_slab_name;
}
}
if (prot->twsk_prot != NULL) { if (prot->twsk_prot != NULL) {
prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name); prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name);
@ -2782,14 +2805,8 @@ int proto_register(struct proto *prot, int alloc_slab)
out_free_timewait_sock_slab_name: out_free_timewait_sock_slab_name:
kfree(prot->twsk_prot->twsk_slab_name); kfree(prot->twsk_prot->twsk_slab_name);
out_free_request_sock_slab: out_free_request_sock_slab:
if (prot->rsk_prot && prot->rsk_prot->slab) { req_prot_cleanup(prot->rsk_prot);
kmem_cache_destroy(prot->rsk_prot->slab);
prot->rsk_prot->slab = NULL;
}
out_free_request_sock_slab_name:
if (prot->rsk_prot)
kfree(prot->rsk_prot->slab_name);
out_free_sock_slab:
kmem_cache_destroy(prot->slab); kmem_cache_destroy(prot->slab);
prot->slab = NULL; prot->slab = NULL;
out: out:
@ -2809,11 +2826,7 @@ void proto_unregister(struct proto *prot)
prot->slab = NULL; prot->slab = NULL;
} }
if (prot->rsk_prot != NULL && prot->rsk_prot->slab != NULL) { req_prot_cleanup(prot->rsk_prot);
kmem_cache_destroy(prot->rsk_prot->slab);
kfree(prot->rsk_prot->slab_name);
prot->rsk_prot->slab = NULL;
}
if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) { if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
kmem_cache_destroy(prot->twsk_prot->twsk_slab); kmem_cache_destroy(prot->twsk_prot->twsk_slab);