Skip to content

Commit c92c81d

Browse files
posk-iodavem330
authored andcommitted
net: dccp: fix kernel crash on module load
Patch eedbbb0 "net: dccp: initialize (addr,port) ..." added calling to inet_hashinfo2_init() from dccp_init(). However, inet_hashinfo2_init() is marked as __init(), and thus the kernel panics when dccp is loaded as module. Removing __init() tag from inet_hashinfo2_init() is not feasible because it calls into __init functions in mm. This patch adds inet_hashinfo2_init_mod() function that can be called after the init phase is done; changes dccp_init() to call the new function; un-marks inet_hashinfo2_init() as exported. Fixes: eedbbb0 ("net: dccp: initialize (addr,port) ...") Reported-by: kernel test robot <lkp@intel.com> Signed-off-by: Peter Oskolkov <posk@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent bd437c9 commit c92c81d

3 files changed

Lines changed: 31 additions & 12 deletions

File tree

‎include/net/inet_hashtables.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ void inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
237237
unsigned long numentries, int scale,
238238
unsigned long low_limit,
239239
unsigned long high_limit);
240+
int inet_hashinfo2_init_mod(struct inet_hashinfo *h);
240241

241242
bool inet_ehash_insert(struct sock *sk, struct sock *osk);
242243
bool inet_ehash_nolisten(struct sock *sk, struct sock *osk);

‎net/dccp/proto.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,11 +1139,11 @@ static int __init dccp_init(void)
11391139
rc = percpu_counter_init(&dccp_orphan_count, 0, GFP_KERNEL);
11401140
if (rc)
11411141
goto out_fail;
1142-
rc = -ENOBUFS;
11431142
inet_hashinfo_init(&dccp_hashinfo);
1144-
inet_hashinfo2_init(&dccp_hashinfo, "dccp_listen_portaddr_hash",
1145-
INET_LHTABLE_SIZE, 21, /* one slot per 2 MB*/
1146-
0, 64 * 1024);
1143+
rc = inet_hashinfo2_init_mod(&dccp_hashinfo);
1144+
if (rc)
1145+
goto out_fail;
1146+
rc = -ENOBUFS;
11471147
dccp_hashinfo.bind_bucket_cachep =
11481148
kmem_cache_create("dccp_bind_bucket",
11491149
sizeof(struct inet_bind_bucket), 0,

‎net/ipv4/inet_hashtables.c‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -762,13 +762,22 @@ void inet_hashinfo_init(struct inet_hashinfo *h)
762762
}
763763
EXPORT_SYMBOL_GPL(inet_hashinfo_init);
764764

765+
static void init_hashinfo_lhash2(struct inet_hashinfo *h)
766+
{
767+
int i;
768+
769+
for (i = 0; i <= h->lhash2_mask; i++) {
770+
spin_lock_init(&h->lhash2[i].lock);
771+
INIT_HLIST_HEAD(&h->lhash2[i].head);
772+
h->lhash2[i].count = 0;
773+
}
774+
}
775+
765776
void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
766777
unsigned long numentries, int scale,
767778
unsigned long low_limit,
768779
unsigned long high_limit)
769780
{
770-
unsigned int i;
771-
772781
h->lhash2 = alloc_large_system_hash(name,
773782
sizeof(*h->lhash2),
774783
numentries,
@@ -778,14 +787,23 @@ void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
778787
&h->lhash2_mask,
779788
low_limit,
780789
high_limit);
790+
init_hashinfo_lhash2(h);
791+
}
781792

782-
for (i = 0; i <= h->lhash2_mask; i++) {
783-
spin_lock_init(&h->lhash2[i].lock);
784-
INIT_HLIST_HEAD(&h->lhash2[i].head);
785-
h->lhash2[i].count = 0;
786-
}
793+
int inet_hashinfo2_init_mod(struct inet_hashinfo *h)
794+
{
795+
h->lhash2 = kmalloc_array(INET_LHTABLE_SIZE, sizeof(*h->lhash2), GFP_KERNEL);
796+
if (!h->lhash2)
797+
return -ENOMEM;
798+
799+
h->lhash2_mask = INET_LHTABLE_SIZE - 1;
800+
/* INET_LHTABLE_SIZE must be a power of 2 */
801+
BUG_ON(INET_LHTABLE_SIZE & h->lhash2_mask);
802+
803+
init_hashinfo_lhash2(h);
804+
return 0;
787805
}
788-
EXPORT_SYMBOL_GPL(inet_hashinfo2_init);
806+
EXPORT_SYMBOL_GPL(inet_hashinfo2_init_mod);
789807

790808
int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
791809
{

0 commit comments

Comments
 (0)