inet_hashtables.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Generic INET transport hashtables
  7. *
  8. * Authors: Lotsa people, from code originally in tcp
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/random.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/wait.h>
  20. #include <linux/vmalloc.h>
  21. #include <net/inet_connection_sock.h>
  22. #include <net/inet_hashtables.h>
  23. #include <net/secure_seq.h>
  24. #include <net/ip.h>
  25. static u32 inet_ehashfn(const struct net *net, const __be32 laddr,
  26. const __u16 lport, const __be32 faddr,
  27. const __be16 fport)
  28. {
  29. static u32 inet_ehash_secret __read_mostly;
  30. net_get_random_once(&inet_ehash_secret, sizeof(inet_ehash_secret));
  31. return __inet_ehashfn(laddr, lport, faddr, fport,
  32. inet_ehash_secret + net_hash_mix(net));
  33. }
  34. /* This function handles inet_sock, but also timewait and request sockets
  35. * for IPv4/IPv6.
  36. */
  37. u32 sk_ehashfn(const struct sock *sk)
  38. {
  39. #if IS_ENABLED(CONFIG_IPV6)
  40. if (sk->sk_family == AF_INET6 &&
  41. !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
  42. return inet6_ehashfn(sock_net(sk),
  43. &sk->sk_v6_rcv_saddr, sk->sk_num,
  44. &sk->sk_v6_daddr, sk->sk_dport);
  45. #endif
  46. return inet_ehashfn(sock_net(sk),
  47. sk->sk_rcv_saddr, sk->sk_num,
  48. sk->sk_daddr, sk->sk_dport);
  49. }
  50. /*
  51. * Allocate and initialize a new local port bind bucket.
  52. * The bindhash mutex for snum's hash chain must be held here.
  53. */
  54. struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
  55. struct net *net,
  56. struct inet_bind_hashbucket *head,
  57. const unsigned short snum)
  58. {
  59. struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
  60. if (tb) {
  61. write_pnet(&tb->ib_net, net);
  62. tb->port = snum;
  63. tb->fastreuse = 0;
  64. tb->fastreuseport = 0;
  65. tb->num_owners = 0;
  66. INIT_HLIST_HEAD(&tb->owners);
  67. hlist_add_head(&tb->node, &head->chain);
  68. }
  69. return tb;
  70. }
  71. /*
  72. * Caller must hold hashbucket lock for this tb with local BH disabled
  73. */
  74. void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
  75. {
  76. if (hlist_empty(&tb->owners)) {
  77. __hlist_del(&tb->node);
  78. kmem_cache_free(cachep, tb);
  79. }
  80. }
  81. void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  82. const unsigned short snum)
  83. {
  84. inet_sk(sk)->inet_num = snum;
  85. sk_add_bind_node(sk, &tb->owners);
  86. tb->num_owners++;
  87. inet_csk(sk)->icsk_bind_hash = tb;
  88. }
  89. /*
  90. * Get rid of any references to a local port held by the given sock.
  91. */
  92. static void __inet_put_port(struct sock *sk)
  93. {
  94. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  95. const int bhash = inet_bhashfn(sock_net(sk), inet_sk(sk)->inet_num,
  96. hashinfo->bhash_size);
  97. struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
  98. struct inet_bind_bucket *tb;
  99. spin_lock(&head->lock);
  100. tb = inet_csk(sk)->icsk_bind_hash;
  101. __sk_del_bind_node(sk);
  102. tb->num_owners--;
  103. inet_csk(sk)->icsk_bind_hash = NULL;
  104. inet_sk(sk)->inet_num = 0;
  105. inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
  106. spin_unlock(&head->lock);
  107. }
  108. void inet_put_port(struct sock *sk)
  109. {
  110. local_bh_disable();
  111. __inet_put_port(sk);
  112. local_bh_enable();
  113. }
  114. EXPORT_SYMBOL(inet_put_port);
  115. int __inet_inherit_port(struct sock *sk, struct sock *child)
  116. {
  117. struct inet_hashinfo *table = sk->sk_prot->h.hashinfo;
  118. unsigned short port = inet_sk(child)->inet_num;
  119. const int bhash = inet_bhashfn(sock_net(sk), port,
  120. table->bhash_size);
  121. struct inet_bind_hashbucket *head = &table->bhash[bhash];
  122. struct inet_bind_bucket *tb;
  123. spin_lock(&head->lock);
  124. tb = inet_csk(sk)->icsk_bind_hash;
  125. if (tb->port != port) {
  126. /* NOTE: using tproxy and redirecting skbs to a proxy
  127. * on a different listener port breaks the assumption
  128. * that the listener socket's icsk_bind_hash is the same
  129. * as that of the child socket. We have to look up or
  130. * create a new bind bucket for the child here. */
  131. inet_bind_bucket_for_each(tb, &head->chain) {
  132. if (net_eq(ib_net(tb), sock_net(sk)) &&
  133. tb->port == port)
  134. break;
  135. }
  136. if (!tb) {
  137. tb = inet_bind_bucket_create(table->bind_bucket_cachep,
  138. sock_net(sk), head, port);
  139. if (!tb) {
  140. spin_unlock(&head->lock);
  141. return -ENOMEM;
  142. }
  143. }
  144. }
  145. inet_bind_hash(child, tb, port);
  146. spin_unlock(&head->lock);
  147. return 0;
  148. }
  149. EXPORT_SYMBOL_GPL(__inet_inherit_port);
  150. static inline int compute_score(struct sock *sk, struct net *net,
  151. const unsigned short hnum, const __be32 daddr,
  152. const int dif)
  153. {
  154. int score = -1;
  155. struct inet_sock *inet = inet_sk(sk);
  156. if (net_eq(sock_net(sk), net) && inet->inet_num == hnum &&
  157. !ipv6_only_sock(sk)) {
  158. __be32 rcv_saddr = inet->inet_rcv_saddr;
  159. score = sk->sk_family == PF_INET ? 2 : 1;
  160. if (rcv_saddr) {
  161. if (rcv_saddr != daddr)
  162. return -1;
  163. score += 4;
  164. }
  165. if (sk->sk_bound_dev_if) {
  166. if (sk->sk_bound_dev_if != dif)
  167. return -1;
  168. score += 4;
  169. }
  170. }
  171. return score;
  172. }
  173. /*
  174. * Don't inline this cruft. Here are some nice properties to exploit here. The
  175. * BSD API does not allow a listening sock to specify the remote port nor the
  176. * remote address for the connection. So always assume those are both
  177. * wildcarded during the search since they can never be otherwise.
  178. */
  179. struct sock *__inet_lookup_listener(struct net *net,
  180. struct inet_hashinfo *hashinfo,
  181. const __be32 saddr, __be16 sport,
  182. const __be32 daddr, const unsigned short hnum,
  183. const int dif)
  184. {
  185. struct sock *sk, *result;
  186. struct hlist_nulls_node *node;
  187. unsigned int hash = inet_lhashfn(net, hnum);
  188. struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash];
  189. int score, hiscore, matches = 0, reuseport = 0;
  190. u32 phash = 0;
  191. rcu_read_lock();
  192. begin:
  193. result = NULL;
  194. hiscore = 0;
  195. sk_nulls_for_each_rcu(sk, node, &ilb->head) {
  196. score = compute_score(sk, net, hnum, daddr, dif);
  197. if (score > hiscore) {
  198. result = sk;
  199. hiscore = score;
  200. reuseport = sk->sk_reuseport;
  201. if (reuseport) {
  202. phash = inet_ehashfn(net, daddr, hnum,
  203. saddr, sport);
  204. matches = 1;
  205. }
  206. } else if (score == hiscore && reuseport) {
  207. matches++;
  208. if (reciprocal_scale(phash, matches) == 0)
  209. result = sk;
  210. phash = next_pseudo_random32(phash);
  211. }
  212. }
  213. /*
  214. * if the nulls value we got at the end of this lookup is
  215. * not the expected one, we must restart lookup.
  216. * We probably met an item that was moved to another chain.
  217. */
  218. if (get_nulls_value(node) != hash + LISTENING_NULLS_BASE)
  219. goto begin;
  220. if (result) {
  221. if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
  222. result = NULL;
  223. else if (unlikely(compute_score(result, net, hnum, daddr,
  224. dif) < hiscore)) {
  225. sock_put(result);
  226. goto begin;
  227. }
  228. }
  229. rcu_read_unlock();
  230. return result;
  231. }
  232. EXPORT_SYMBOL_GPL(__inet_lookup_listener);
  233. /* All sockets share common refcount, but have different destructors */
  234. void sock_gen_put(struct sock *sk)
  235. {
  236. if (!atomic_dec_and_test(&sk->sk_refcnt))
  237. return;
  238. if (sk->sk_state == TCP_TIME_WAIT)
  239. inet_twsk_free(inet_twsk(sk));
  240. else if (sk->sk_state == TCP_NEW_SYN_RECV)
  241. reqsk_free(inet_reqsk(sk));
  242. else
  243. sk_free(sk);
  244. }
  245. EXPORT_SYMBOL_GPL(sock_gen_put);
  246. void sock_edemux(struct sk_buff *skb)
  247. {
  248. sock_gen_put(skb->sk);
  249. }
  250. EXPORT_SYMBOL(sock_edemux);
  251. struct sock *__inet_lookup_established(struct net *net,
  252. struct inet_hashinfo *hashinfo,
  253. const __be32 saddr, const __be16 sport,
  254. const __be32 daddr, const u16 hnum,
  255. const int dif)
  256. {
  257. INET_ADDR_COOKIE(acookie, saddr, daddr);
  258. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  259. struct sock *sk;
  260. const struct hlist_nulls_node *node;
  261. /* Optimize here for direct hit, only listening connections can
  262. * have wildcards anyways.
  263. */
  264. unsigned int hash = inet_ehashfn(net, daddr, hnum, saddr, sport);
  265. unsigned int slot = hash & hashinfo->ehash_mask;
  266. struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
  267. rcu_read_lock();
  268. begin:
  269. sk_nulls_for_each_rcu(sk, node, &head->chain) {
  270. if (sk->sk_hash != hash)
  271. continue;
  272. if (likely(INET_MATCH(sk, net, acookie,
  273. saddr, daddr, ports, dif))) {
  274. if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt)))
  275. goto out;
  276. if (unlikely(!INET_MATCH(sk, net, acookie,
  277. saddr, daddr, ports, dif))) {
  278. sock_gen_put(sk);
  279. goto begin;
  280. }
  281. goto found;
  282. }
  283. }
  284. /*
  285. * if the nulls value we got at the end of this lookup is
  286. * not the expected one, we must restart lookup.
  287. * We probably met an item that was moved to another chain.
  288. */
  289. if (get_nulls_value(node) != slot)
  290. goto begin;
  291. out:
  292. sk = NULL;
  293. found:
  294. rcu_read_unlock();
  295. return sk;
  296. }
  297. EXPORT_SYMBOL_GPL(__inet_lookup_established);
  298. /* called with local bh disabled */
  299. static int __inet_check_established(struct inet_timewait_death_row *death_row,
  300. struct sock *sk, __u16 lport,
  301. struct inet_timewait_sock **twp)
  302. {
  303. struct inet_hashinfo *hinfo = death_row->hashinfo;
  304. struct inet_sock *inet = inet_sk(sk);
  305. __be32 daddr = inet->inet_rcv_saddr;
  306. __be32 saddr = inet->inet_daddr;
  307. int dif = sk->sk_bound_dev_if;
  308. INET_ADDR_COOKIE(acookie, saddr, daddr);
  309. const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
  310. struct net *net = sock_net(sk);
  311. unsigned int hash = inet_ehashfn(net, daddr, lport,
  312. saddr, inet->inet_dport);
  313. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  314. spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
  315. struct sock *sk2;
  316. const struct hlist_nulls_node *node;
  317. struct inet_timewait_sock *tw = NULL;
  318. int twrefcnt = 0;
  319. spin_lock(lock);
  320. sk_nulls_for_each(sk2, node, &head->chain) {
  321. if (sk2->sk_hash != hash)
  322. continue;
  323. if (likely(INET_MATCH(sk2, net, acookie,
  324. saddr, daddr, ports, dif))) {
  325. if (sk2->sk_state == TCP_TIME_WAIT) {
  326. tw = inet_twsk(sk2);
  327. if (twsk_unique(sk, sk2, twp))
  328. break;
  329. }
  330. goto not_unique;
  331. }
  332. }
  333. /* Must record num and sport now. Otherwise we will see
  334. * in hash table socket with a funny identity.
  335. */
  336. inet->inet_num = lport;
  337. inet->inet_sport = htons(lport);
  338. sk->sk_hash = hash;
  339. WARN_ON(!sk_unhashed(sk));
  340. __sk_nulls_add_node_rcu(sk, &head->chain);
  341. if (tw) {
  342. twrefcnt = inet_twsk_unhash(tw);
  343. NET_INC_STATS_BH(net, LINUX_MIB_TIMEWAITRECYCLED);
  344. }
  345. spin_unlock(lock);
  346. if (twrefcnt)
  347. inet_twsk_put(tw);
  348. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  349. if (twp) {
  350. *twp = tw;
  351. } else if (tw) {
  352. /* Silly. Should hash-dance instead... */
  353. inet_twsk_deschedule(tw);
  354. inet_twsk_put(tw);
  355. }
  356. return 0;
  357. not_unique:
  358. spin_unlock(lock);
  359. return -EADDRNOTAVAIL;
  360. }
  361. static u32 inet_sk_port_offset(const struct sock *sk)
  362. {
  363. const struct inet_sock *inet = inet_sk(sk);
  364. return secure_ipv4_port_ephemeral(inet->inet_rcv_saddr,
  365. inet->inet_daddr,
  366. inet->inet_dport);
  367. }
  368. int __inet_hash_nolisten(struct sock *sk, struct inet_timewait_sock *tw)
  369. {
  370. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  371. struct hlist_nulls_head *list;
  372. struct inet_ehash_bucket *head;
  373. spinlock_t *lock;
  374. int twrefcnt = 0;
  375. WARN_ON(!sk_unhashed(sk));
  376. sk->sk_hash = sk_ehashfn(sk);
  377. head = inet_ehash_bucket(hashinfo, sk->sk_hash);
  378. list = &head->chain;
  379. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  380. spin_lock(lock);
  381. __sk_nulls_add_node_rcu(sk, list);
  382. if (tw) {
  383. WARN_ON(sk->sk_hash != tw->tw_hash);
  384. twrefcnt = inet_twsk_unhash(tw);
  385. }
  386. spin_unlock(lock);
  387. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  388. return twrefcnt;
  389. }
  390. EXPORT_SYMBOL_GPL(__inet_hash_nolisten);
  391. int __inet_hash(struct sock *sk, struct inet_timewait_sock *tw)
  392. {
  393. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  394. struct inet_listen_hashbucket *ilb;
  395. if (sk->sk_state != TCP_LISTEN)
  396. return __inet_hash_nolisten(sk, tw);
  397. WARN_ON(!sk_unhashed(sk));
  398. ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  399. spin_lock(&ilb->lock);
  400. __sk_nulls_add_node_rcu(sk, &ilb->head);
  401. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  402. spin_unlock(&ilb->lock);
  403. return 0;
  404. }
  405. EXPORT_SYMBOL(__inet_hash);
  406. void inet_hash(struct sock *sk)
  407. {
  408. if (sk->sk_state != TCP_CLOSE) {
  409. local_bh_disable();
  410. __inet_hash(sk, NULL);
  411. local_bh_enable();
  412. }
  413. }
  414. EXPORT_SYMBOL_GPL(inet_hash);
  415. void inet_unhash(struct sock *sk)
  416. {
  417. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  418. spinlock_t *lock;
  419. int done;
  420. if (sk_unhashed(sk))
  421. return;
  422. if (sk->sk_state == TCP_LISTEN)
  423. lock = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)].lock;
  424. else
  425. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  426. spin_lock_bh(lock);
  427. done = __sk_nulls_del_node_init_rcu(sk);
  428. if (done)
  429. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  430. spin_unlock_bh(lock);
  431. }
  432. EXPORT_SYMBOL_GPL(inet_unhash);
  433. int __inet_hash_connect(struct inet_timewait_death_row *death_row,
  434. struct sock *sk, u32 port_offset,
  435. int (*check_established)(struct inet_timewait_death_row *,
  436. struct sock *, __u16, struct inet_timewait_sock **))
  437. {
  438. struct inet_hashinfo *hinfo = death_row->hashinfo;
  439. const unsigned short snum = inet_sk(sk)->inet_num;
  440. struct inet_bind_hashbucket *head;
  441. struct inet_bind_bucket *tb;
  442. int ret;
  443. struct net *net = sock_net(sk);
  444. int twrefcnt = 1;
  445. if (!snum) {
  446. int i, remaining, low, high, port;
  447. static u32 hint;
  448. u32 offset = hint + port_offset;
  449. struct inet_timewait_sock *tw = NULL;
  450. inet_get_local_port_range(net, &low, &high);
  451. remaining = (high - low) + 1;
  452. /* By starting with offset being an even number,
  453. * we tend to leave about 50% of ports for other uses,
  454. * like bind(0).
  455. */
  456. offset &= ~1;
  457. local_bh_disable();
  458. for (i = 0; i < remaining; i++) {
  459. port = low + (i + offset) % remaining;
  460. if (inet_is_local_reserved_port(net, port))
  461. continue;
  462. head = &hinfo->bhash[inet_bhashfn(net, port,
  463. hinfo->bhash_size)];
  464. spin_lock(&head->lock);
  465. /* Does not bother with rcv_saddr checks,
  466. * because the established check is already
  467. * unique enough.
  468. */
  469. inet_bind_bucket_for_each(tb, &head->chain) {
  470. if (net_eq(ib_net(tb), net) &&
  471. tb->port == port) {
  472. if (tb->fastreuse >= 0 ||
  473. tb->fastreuseport >= 0)
  474. goto next_port;
  475. WARN_ON(hlist_empty(&tb->owners));
  476. if (!check_established(death_row, sk,
  477. port, &tw))
  478. goto ok;
  479. goto next_port;
  480. }
  481. }
  482. tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
  483. net, head, port);
  484. if (!tb) {
  485. spin_unlock(&head->lock);
  486. break;
  487. }
  488. tb->fastreuse = -1;
  489. tb->fastreuseport = -1;
  490. goto ok;
  491. next_port:
  492. spin_unlock(&head->lock);
  493. }
  494. local_bh_enable();
  495. return -EADDRNOTAVAIL;
  496. ok:
  497. hint += (i + 2) & ~1;
  498. /* Head lock still held and bh's disabled */
  499. inet_bind_hash(sk, tb, port);
  500. if (sk_unhashed(sk)) {
  501. inet_sk(sk)->inet_sport = htons(port);
  502. twrefcnt += __inet_hash_nolisten(sk, tw);
  503. }
  504. if (tw)
  505. twrefcnt += inet_twsk_bind_unhash(tw, hinfo);
  506. spin_unlock(&head->lock);
  507. if (tw) {
  508. inet_twsk_deschedule(tw);
  509. while (twrefcnt) {
  510. twrefcnt--;
  511. inet_twsk_put(tw);
  512. }
  513. }
  514. ret = 0;
  515. goto out;
  516. }
  517. head = &hinfo->bhash[inet_bhashfn(net, snum, hinfo->bhash_size)];
  518. tb = inet_csk(sk)->icsk_bind_hash;
  519. spin_lock_bh(&head->lock);
  520. if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
  521. __inet_hash_nolisten(sk, NULL);
  522. spin_unlock_bh(&head->lock);
  523. return 0;
  524. } else {
  525. spin_unlock(&head->lock);
  526. /* No definite answer... Walk to established hash table */
  527. ret = check_established(death_row, sk, snum, NULL);
  528. out:
  529. local_bh_enable();
  530. return ret;
  531. }
  532. }
  533. /*
  534. * Bind a port for a connect operation and hash it.
  535. */
  536. int inet_hash_connect(struct inet_timewait_death_row *death_row,
  537. struct sock *sk)
  538. {
  539. u32 port_offset = 0;
  540. if (!inet_sk(sk)->inet_num)
  541. port_offset = inet_sk_port_offset(sk);
  542. return __inet_hash_connect(death_row, sk, port_offset,
  543. __inet_check_established);
  544. }
  545. EXPORT_SYMBOL_GPL(inet_hash_connect);
  546. void inet_hashinfo_init(struct inet_hashinfo *h)
  547. {
  548. int i;
  549. for (i = 0; i < INET_LHTABLE_SIZE; i++) {
  550. spin_lock_init(&h->listening_hash[i].lock);
  551. INIT_HLIST_NULLS_HEAD(&h->listening_hash[i].head,
  552. i + LISTENING_NULLS_BASE);
  553. }
  554. }
  555. EXPORT_SYMBOL_GPL(inet_hashinfo_init);
  556. int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
  557. {
  558. unsigned int i, nblocks = 1;
  559. if (sizeof(spinlock_t) != 0) {
  560. /* allocate 2 cache lines or at least one spinlock per cpu */
  561. nblocks = max_t(unsigned int,
  562. 2 * L1_CACHE_BYTES / sizeof(spinlock_t),
  563. 1);
  564. nblocks = roundup_pow_of_two(nblocks * num_possible_cpus());
  565. /* no more locks than number of hash buckets */
  566. nblocks = min(nblocks, hashinfo->ehash_mask + 1);
  567. hashinfo->ehash_locks = kmalloc_array(nblocks, sizeof(spinlock_t),
  568. GFP_KERNEL | __GFP_NOWARN);
  569. if (!hashinfo->ehash_locks)
  570. hashinfo->ehash_locks = vmalloc(nblocks * sizeof(spinlock_t));
  571. if (!hashinfo->ehash_locks)
  572. return -ENOMEM;
  573. for (i = 0; i < nblocks; i++)
  574. spin_lock_init(&hashinfo->ehash_locks[i]);
  575. }
  576. hashinfo->ehash_locks_mask = nblocks - 1;
  577. return 0;
  578. }
  579. EXPORT_SYMBOL_GPL(inet_ehash_locks_alloc);