Skip to content

Commit 13854e5

Browse files
edumazetdavem330
authored andcommitted
inet: add proper refcounting to request sock
reqsk_put() is the generic function that should be used to release a refcount (and automatically call reqsk_free()) reqsk_free() might be called if refcount is known to be 0 or undefined. refcnt is set to one in inet_csk_reqsk_queue_add() As request socks are not yet in global ehash table, I added temporary debugging checks in reqsk_put() and reqsk_free() Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 2c13270 commit 13854e5

7 files changed

Lines changed: 29 additions & 18 deletions

File tree

‎include/net/inet_connection_sock.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ static inline void inet_csk_reqsk_queue_add(struct sock *sk,
275275
struct sock *child)
276276
{
277277
reqsk_queue_add(&inet_csk(sk)->icsk_accept_queue, req, sk, child);
278+
/* before letting lookups find us, make sure all req fields
279+
* are committed to memory.
280+
*/
281+
smp_wmb();
282+
atomic_set(&req->rsk_refcnt, 1);
278283
}
279284

280285
void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,

‎include/net/inet_sock.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ static inline struct request_sock *inet_reqsk_alloc(struct request_sock_ops *ops
255255
ireq->opt = NULL;
256256
atomic64_set(&ireq->ir_cookie, 0);
257257
ireq->ireq_state = TCP_NEW_SYN_RECV;
258+
259+
/* Following is temporary. It is coupled with debugging
260+
* helpers in reqsk_put() & reqsk_free()
261+
*/
262+
atomic_set(&ireq->ireq_refcnt, 0);
258263
}
259264

260265
return req;

‎include/net/request_sock.h‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,20 @@ static inline struct request_sock *inet_reqsk(struct sock *sk)
8282
return (struct request_sock *)sk;
8383
}
8484

85-
static inline void __reqsk_free(struct request_sock *req)
86-
{
87-
kmem_cache_free(req->rsk_ops->slab, req);
88-
}
89-
9085
static inline void reqsk_free(struct request_sock *req)
9186
{
87+
/* temporary debugging */
88+
WARN_ON_ONCE(atomic_read(&req->rsk_refcnt) != 0);
89+
9290
req->rsk_ops->destructor(req);
93-
__reqsk_free(req);
91+
kmem_cache_free(req->rsk_ops->slab, req);
9492
}
9593

9694
static inline void reqsk_put(struct request_sock *req)
9795
{
96+
/* temporary debugging, until req sock are put into ehash table */
97+
WARN_ON_ONCE(atomic_read(&req->rsk_refcnt) != 1);
98+
9899
if (atomic_dec_and_test(&req->rsk_refcnt))
99100
reqsk_free(req);
100101
}

‎net/core/request_sock.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void reqsk_queue_destroy(struct request_sock_queue *queue)
103103
while ((req = lopt->syn_table[i]) != NULL) {
104104
lopt->syn_table[i] = req->dl_next;
105105
lopt->qlen--;
106-
reqsk_free(req);
106+
reqsk_put(req);
107107
}
108108
}
109109
}
@@ -180,7 +180,7 @@ void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
180180
*/
181181
spin_unlock_bh(&fastopenq->lock);
182182
sock_put(lsk);
183-
reqsk_free(req);
183+
reqsk_put(req);
184184
return;
185185
}
186186
/* Wait for 60secs before removing a req that has triggered RST.

‎net/ipv4/inet_connection_sock.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
340340
out:
341341
release_sock(sk);
342342
if (req)
343-
__reqsk_free(req);
343+
reqsk_put(req);
344344
return newsk;
345345
out_err:
346346
newsk = NULL;
@@ -635,7 +635,7 @@ void inet_csk_reqsk_queue_prune(struct sock *parent,
635635
/* Drop this request */
636636
inet_csk_reqsk_queue_unlink(parent, req, reqp);
637637
reqsk_queue_removed(queue, req);
638-
reqsk_free(req);
638+
reqsk_put(req);
639639
continue;
640640
}
641641
reqp = &req->dl_next;
@@ -837,7 +837,7 @@ void inet_csk_listen_stop(struct sock *sk)
837837
sock_put(child);
838838

839839
sk_acceptq_removed(sk);
840-
__reqsk_free(req);
840+
reqsk_put(req);
841841
}
842842
if (queue->fastopenq != NULL) {
843843
/* Free all the reqs queued in rskq_rst_head. */
@@ -847,7 +847,7 @@ void inet_csk_listen_stop(struct sock *sk)
847847
spin_unlock_bh(&queue->fastopenq->lock);
848848
while ((req = acc_req) != NULL) {
849849
acc_req = req->dl_next;
850-
__reqsk_free(req);
850+
reqsk_put(req);
851851
}
852852
}
853853
WARN_ON(sk->sk_ack_backlog);

‎net/ipv4/syncookies.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
219219
}
220220
EXPORT_SYMBOL_GPL(__cookie_v4_check);
221221

222-
static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
223-
struct request_sock *req,
224-
struct dst_entry *dst)
222+
static struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
223+
struct request_sock *req,
224+
struct dst_entry *dst)
225225
{
226226
struct inet_connection_sock *icsk = inet_csk(sk);
227227
struct sock *child;
@@ -357,7 +357,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
357357
ireq->opt = tcp_v4_save_options(skb);
358358

359359
if (security_inet_conn_request(sk, skb, req)) {
360-
reqsk_free(req);
360+
reqsk_put(req);
361361
goto out;
362362
}
363363

@@ -378,7 +378,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
378378
security_req_classify_flow(req, flowi4_to_flowi(&fl4));
379379
rt = ip_route_output_key(sock_net(sk), &fl4);
380380
if (IS_ERR(rt)) {
381-
reqsk_free(req);
381+
reqsk_put(req);
382382
goto out;
383383
}
384384

‎net/ipv4/tcp_fastopen.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ static bool tcp_fastopen_queue_check(struct sock *sk)
253253
fastopenq->rskq_rst_head = req1->dl_next;
254254
fastopenq->qlen--;
255255
spin_unlock(&fastopenq->lock);
256-
reqsk_free(req1);
256+
reqsk_put(req1);
257257
}
258258
return true;
259259
}

0 commit comments

Comments
 (0)