net: core: annotate socks of struct sock_reuseport with __counted_by
authorDmitry Antipov <dmantipov@yandex.ru>
Thu, 1 Aug 2024 14:23:11 +0000 (17:23 +0300)
committerJakub Kicinski <kuba@kernel.org>
Sat, 3 Aug 2024 00:16:59 +0000 (17:16 -0700)
According to '__reuseport_alloc()', annotate flexible array member
'sock' of 'struct sock_reuseport' with '__counted_by()' and use
convenient 'struct_size()' to simplify the math used in 'kzalloc()'.

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Link: https://patch.msgid.link/20240801142311.42837-1-dmantipov@yandex.ru
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/sock_reuseport.h
net/core/sock_reuseport.c

index 6ec140b0a61bf157431aac2068e044e04ab03c8c..6e4faf3ee76fbd86e2c4ac6ff13ead5c9a9187d6 100644 (file)
@@ -26,7 +26,7 @@ struct sock_reuseport {
        unsigned int            bind_inany:1;
        unsigned int            has_conns:1;
        struct bpf_prog __rcu   *prog;          /* optional BPF sock selector */
-       struct sock             *socks[];       /* array of sock pointers */
+       struct sock             *socks[] __counted_by(max_socks);
 };
 
 extern int reuseport_alloc(struct sock *sk, bool bind_inany);
index 5a165286e4d8e8d2702cfe4b1d50f44713baa3d7..4211710393a8ac778359d22116e5a3b2aa53f123 100644 (file)
@@ -173,10 +173,9 @@ static bool __reuseport_detach_closed_sock(struct sock *sk,
 
 static struct sock_reuseport *__reuseport_alloc(unsigned int max_socks)
 {
-       unsigned int size = sizeof(struct sock_reuseport) +
-                     sizeof(struct sock *) * max_socks;
-       struct sock_reuseport *reuse = kzalloc(size, GFP_ATOMIC);
+       struct sock_reuseport *reuse;
 
+       reuse = kzalloc(struct_size(reuse, socks, max_socks), GFP_ATOMIC);
        if (!reuse)
                return NULL;