diff mbox series

[RFC,net-next,v6,04/14] af_vsock: generalize bind table functions

Message ID 20240710212555.1617795-5-amery.hung@bytedance.com (mailing list archive)
State RFC
Delegated to: Netdev Maintainers
Headers show
Series virtio/vsock: support datagrams | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 816 this patch: 816
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 1 maintainers not CCed: virtualization@lists.linux.dev
netdev/build_clang success Errors and warnings before: 821 this patch: 821
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 821 this patch: 821
netdev/checkpatch warning WARNING: line length of 82 exceeds 80 columns WARNING: line length of 83 exceeds 80 columns WARNING: line length of 93 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Amery Hung July 10, 2024, 9:25 p.m. UTC
From: Bobby Eshleman <bobby.eshleman@bytedance.com>

This commit makes the bind table management functions in vsock usable
for different bind tables. Future work will introduce a new table for
datagrams to avoid address collisions, and these functions will be used
there.

Signed-off-by: Bobby Eshleman <bobby.eshleman@bytedance.com>
---
 net/vmw_vsock/af_vsock.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

Comments

Stefano Garzarella July 23, 2024, 2:39 p.m. UTC | #1
On Wed, Jul 10, 2024 at 09:25:45PM GMT, Amery Hung wrote:
>From: Bobby Eshleman <bobby.eshleman@bytedance.com>
>
>This commit makes the bind table management functions in vsock usable
>for different bind tables. Future work will introduce a new table for
>datagrams to avoid address collisions, and these functions will be used
>there.
>
>Signed-off-by: Bobby Eshleman <bobby.eshleman@bytedance.com>
>---
> net/vmw_vsock/af_vsock.c | 34 +++++++++++++++++++++++++++-------
> 1 file changed, 27 insertions(+), 7 deletions(-)
>
>diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
>index acc15e11700c..d571be9cdbf0 100644
>--- a/net/vmw_vsock/af_vsock.c
>+++ b/net/vmw_vsock/af_vsock.c
>@@ -232,11 +232,12 @@ static void __vsock_remove_connected(struct vsock_sock *vsk)
>       sock_put(&vsk->sk);
> }
>
>-static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
>+static struct sock *vsock_find_bound_socket_common(struct sockaddr_vm *addr,
>+                                                 struct list_head *bind_table)
> {
>       struct vsock_sock *vsk;
>
>-      list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) {
>+      list_for_each_entry(vsk, bind_table, bound_table) {
>               if (vsock_addr_equals_addr(addr, &vsk->local_addr))
>                       return sk_vsock(vsk);
>
>@@ -249,6 +250,11 @@ static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
>       return NULL;
> }
>
>+static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
>+{
>+      return vsock_find_bound_socket_common(addr, vsock_bound_sockets(addr));
>+}
>+
> static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,
>                                                 struct sockaddr_vm *dst)
> {
>@@ -671,12 +677,18 @@ static void vsock_pending_work(struct work_struct *work)
>
> /**** SOCKET OPERATIONS ****/
>
>-static int __vsock_bind_connectible(struct vsock_sock *vsk,
>-                                  struct sockaddr_vm *addr)
>+static int vsock_bind_common(struct vsock_sock *vsk,
>+                           struct sockaddr_vm *addr,
>+                           struct list_head *bind_table,
>+                           size_t table_size)
> {
>       static u32 port;
>       struct sockaddr_vm new_addr;
>
>+      if (WARN_ONCE(table_size < VSOCK_HASH_SIZE,
>+                    "table size too small, may cause overflow"))
>+              return -EINVAL;
>+

I'd add this in another commit.

>       if (!port)
>               port = get_random_u32_above(LAST_RESERVED_PORT);
>
>@@ -692,7 +704,8 @@ static int __vsock_bind_connectible(struct
>vsock_sock *vsk,
>
>                       new_addr.svm_port = port++;
>
>-                      if (!__vsock_find_bound_socket(&new_addr)) {
>+                      if (!vsock_find_bound_socket_common(&new_addr,
>+                                                          &bind_table[VSOCK_HASH(addr)])) {

Can we add a macro for `&bind_table[VSOCK_HASH(addr)])` ?

>                               found = true;
>                               break;
>                       }
>@@ -709,7 +722,8 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk,
>                       return -EACCES;
>               }
>
>-              if (__vsock_find_bound_socket(&new_addr))
>+              if (vsock_find_bound_socket_common(&new_addr,
>+                                                 &bind_table[VSOCK_HASH(addr)]))
>                       return -EADDRINUSE;
>       }
>
>@@ -721,11 +735,17 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk,
>        * by AF_UNIX.
>        */
>       __vsock_remove_bound(vsk);
>-      __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
>+      __vsock_insert_bound(&bind_table[VSOCK_HASH(&vsk->local_addr)], vsk);
>
>       return 0;
> }
>
>+static int __vsock_bind_connectible(struct vsock_sock *vsk,
>+                                  struct sockaddr_vm *addr)
>+{
>+      return vsock_bind_common(vsk, addr, vsock_bind_table, VSOCK_HASH_SIZE + 1);

What about using ARRAY_SIZE(x) ?

BTW we are using that size just to check it, but all the arrays we use
are statically allocated, so what about a compile time check like
BUILD_BUG_ON()?

Thanks,
Stefano


>+}
>+
> static int __vsock_bind_dgram(struct vsock_sock *vsk,
>                             struct sockaddr_vm *addr)
> {
>--
>2.20.1
>
Amery Hung July 28, 2024, 6:52 p.m. UTC | #2
On Tue, Jul 23, 2024 at 7:40 AM Stefano Garzarella <sgarzare@redhat.com> wrote:
>
> On Wed, Jul 10, 2024 at 09:25:45PM GMT, Amery Hung wrote:
> >From: Bobby Eshleman <bobby.eshleman@bytedance.com>
> >
> >This commit makes the bind table management functions in vsock usable
> >for different bind tables. Future work will introduce a new table for
> >datagrams to avoid address collisions, and these functions will be used
> >there.
> >
> >Signed-off-by: Bobby Eshleman <bobby.eshleman@bytedance.com>
> >---
> > net/vmw_vsock/af_vsock.c | 34 +++++++++++++++++++++++++++-------
> > 1 file changed, 27 insertions(+), 7 deletions(-)
> >
> >diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
> >index acc15e11700c..d571be9cdbf0 100644
> >--- a/net/vmw_vsock/af_vsock.c
> >+++ b/net/vmw_vsock/af_vsock.c
> >@@ -232,11 +232,12 @@ static void __vsock_remove_connected(struct vsock_sock *vsk)
> >       sock_put(&vsk->sk);
> > }
> >
> >-static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
> >+static struct sock *vsock_find_bound_socket_common(struct sockaddr_vm *addr,
> >+                                                 struct list_head *bind_table)
> > {
> >       struct vsock_sock *vsk;
> >
> >-      list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) {
> >+      list_for_each_entry(vsk, bind_table, bound_table) {
> >               if (vsock_addr_equals_addr(addr, &vsk->local_addr))
> >                       return sk_vsock(vsk);
> >
> >@@ -249,6 +250,11 @@ static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
> >       return NULL;
> > }
> >
> >+static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
> >+{
> >+      return vsock_find_bound_socket_common(addr, vsock_bound_sockets(addr));
> >+}
> >+
> > static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,
> >                                                 struct sockaddr_vm *dst)
> > {
> >@@ -671,12 +677,18 @@ static void vsock_pending_work(struct work_struct *work)
> >
> > /**** SOCKET OPERATIONS ****/
> >
> >-static int __vsock_bind_connectible(struct vsock_sock *vsk,
> >-                                  struct sockaddr_vm *addr)
> >+static int vsock_bind_common(struct vsock_sock *vsk,
> >+                           struct sockaddr_vm *addr,
> >+                           struct list_head *bind_table,
> >+                           size_t table_size)
> > {
> >       static u32 port;
> >       struct sockaddr_vm new_addr;
> >
> >+      if (WARN_ONCE(table_size < VSOCK_HASH_SIZE,
> >+                    "table size too small, may cause overflow"))
> >+              return -EINVAL;
> >+
>
> I'd add this in another commit.
>
> >       if (!port)
> >               port = get_random_u32_above(LAST_RESERVED_PORT);
> >
> >@@ -692,7 +704,8 @@ static int __vsock_bind_connectible(struct
> >vsock_sock *vsk,
> >
> >                       new_addr.svm_port = port++;
> >
> >-                      if (!__vsock_find_bound_socket(&new_addr)) {
> >+                      if (!vsock_find_bound_socket_common(&new_addr,
> >+                                                          &bind_table[VSOCK_HASH(addr)])) {
>
> Can we add a macro for `&bind_table[VSOCK_HASH(addr)])` ?
>

Definitely. I will add the following macro:

#define vsock_bound_sockets_in_table(bind_table, addr) \
        (&bind_table[VSOCK_HASH(addr)])

> >                               found = true;
> >                               break;
> >                       }
> >@@ -709,7 +722,8 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk,
> >                       return -EACCES;
> >               }
> >
> >-              if (__vsock_find_bound_socket(&new_addr))
> >+              if (vsock_find_bound_socket_common(&new_addr,
> >+                                                 &bind_table[VSOCK_HASH(addr)]))
> >                       return -EADDRINUSE;
> >       }
> >
> >@@ -721,11 +735,17 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk,
> >        * by AF_UNIX.
> >        */
> >       __vsock_remove_bound(vsk);
> >-      __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
> >+      __vsock_insert_bound(&bind_table[VSOCK_HASH(&vsk->local_addr)], vsk);
> >
> >       return 0;
> > }
> >
> >+static int __vsock_bind_connectible(struct vsock_sock *vsk,
> >+                                  struct sockaddr_vm *addr)
> >+{
> >+      return vsock_bind_common(vsk, addr, vsock_bind_table, VSOCK_HASH_SIZE + 1);
>
> What about using ARRAY_SIZE(x) ?
>
> BTW we are using that size just to check it, but all the arrays we use
> are statically allocated, so what about a compile time check like
> BUILD_BUG_ON()?
>

I will remove the table_size check you mentioned earlier and the
argument here as the arrays are allocated statically like you
mentioned.

If you think this check may be a good addition, I can add a
BUILD_BUG_ON() in the new vsock_bound_sockets_in_table() macro.

Thanks,
Amery

> Thanks,
> Stefano
>
>
> >+}
> >+
> > static int __vsock_bind_dgram(struct vsock_sock *vsk,
> >                             struct sockaddr_vm *addr)
> > {
> >--
> >2.20.1
> >
>
Stefano Garzarella July 30, 2024, 8 a.m. UTC | #3
On Sun, Jul 28, 2024 at 11:52:54AM GMT, Amery Hung wrote:
>On Tue, Jul 23, 2024 at 7:40 AM Stefano Garzarella <sgarzare@redhat.com> wrote:
>>
>> On Wed, Jul 10, 2024 at 09:25:45PM GMT, Amery Hung wrote:
>> >From: Bobby Eshleman <bobby.eshleman@bytedance.com>
>> >
>> >This commit makes the bind table management functions in vsock usable
>> >for different bind tables. Future work will introduce a new table for
>> >datagrams to avoid address collisions, and these functions will be used
>> >there.
>> >
>> >Signed-off-by: Bobby Eshleman <bobby.eshleman@bytedance.com>
>> >---
>> > net/vmw_vsock/af_vsock.c | 34 +++++++++++++++++++++++++++-------
>> > 1 file changed, 27 insertions(+), 7 deletions(-)
>> >
>> >diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
>> >index acc15e11700c..d571be9cdbf0 100644
>> >--- a/net/vmw_vsock/af_vsock.c
>> >+++ b/net/vmw_vsock/af_vsock.c
>> >@@ -232,11 +232,12 @@ static void __vsock_remove_connected(struct vsock_sock *vsk)
>> >       sock_put(&vsk->sk);
>> > }
>> >
>> >-static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
>> >+static struct sock *vsock_find_bound_socket_common(struct sockaddr_vm *addr,
>> >+                                                 struct list_head *bind_table)
>> > {
>> >       struct vsock_sock *vsk;
>> >
>> >-      list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) {
>> >+      list_for_each_entry(vsk, bind_table, bound_table) {
>> >               if (vsock_addr_equals_addr(addr, &vsk->local_addr))
>> >                       return sk_vsock(vsk);
>> >
>> >@@ -249,6 +250,11 @@ static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
>> >       return NULL;
>> > }
>> >
>> >+static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
>> >+{
>> >+      return vsock_find_bound_socket_common(addr, vsock_bound_sockets(addr));
>> >+}
>> >+
>> > static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,
>> >                                                 struct sockaddr_vm *dst)
>> > {
>> >@@ -671,12 +677,18 @@ static void vsock_pending_work(struct work_struct *work)
>> >
>> > /**** SOCKET OPERATIONS ****/
>> >
>> >-static int __vsock_bind_connectible(struct vsock_sock *vsk,
>> >-                                  struct sockaddr_vm *addr)
>> >+static int vsock_bind_common(struct vsock_sock *vsk,
>> >+                           struct sockaddr_vm *addr,
>> >+                           struct list_head *bind_table,
>> >+                           size_t table_size)
>> > {
>> >       static u32 port;
>> >       struct sockaddr_vm new_addr;
>> >
>> >+      if (WARN_ONCE(table_size < VSOCK_HASH_SIZE,
>> >+                    "table size too small, may cause overflow"))
>> >+              return -EINVAL;
>> >+
>>
>> I'd add this in another commit.
>>
>> >       if (!port)
>> >               port = get_random_u32_above(LAST_RESERVED_PORT);
>> >
>> >@@ -692,7 +704,8 @@ static int __vsock_bind_connectible(struct
>> >vsock_sock *vsk,
>> >
>> >                       new_addr.svm_port = port++;
>> >
>> >-                      if (!__vsock_find_bound_socket(&new_addr)) {
>> >+                      if (!vsock_find_bound_socket_common(&new_addr,
>> >+                                                          &bind_table[VSOCK_HASH(addr)])) {
>>
>> Can we add a macro for `&bind_table[VSOCK_HASH(addr)])` ?
>>
>
>Definitely. I will add the following macro:
>
>#define vsock_bound_sockets_in_table(bind_table, addr) \
>        (&bind_table[VSOCK_HASH(addr)])

yeah.

>
>> >                               found = true;
>> >                               break;
>> >                       }
>> >@@ -709,7 +722,8 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk,
>> >                       return -EACCES;
>> >               }
>> >
>> >-              if (__vsock_find_bound_socket(&new_addr))
>> >+              if (vsock_find_bound_socket_common(&new_addr,
>> >+                                                 &bind_table[VSOCK_HASH(addr)]))
>> >                       return -EADDRINUSE;
>> >       }
>> >
>> >@@ -721,11 +735,17 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk,
>> >        * by AF_UNIX.
>> >        */
>> >       __vsock_remove_bound(vsk);
>> >-      __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
>> >+      __vsock_insert_bound(&bind_table[VSOCK_HASH(&vsk->local_addr)], vsk);
>> >
>> >       return 0;
>> > }
>> >
>> >+static int __vsock_bind_connectible(struct vsock_sock *vsk,
>> >+                                  struct sockaddr_vm *addr)
>> >+{
>> >+      return vsock_bind_common(vsk, addr, vsock_bind_table, VSOCK_HASH_SIZE + 1);
>>
>> What about using ARRAY_SIZE(x) ?
>>
>> BTW we are using that size just to check it, but all the arrays we use
>> are statically allocated, so what about a compile time check like
>> BUILD_BUG_ON()?
>>
>
>I will remove the table_size check you mentioned earlier and the
>argument here as the arrays are allocated statically like you
>mentioned.
>
>If you think this check may be a good addition, I can add a
>BUILD_BUG_ON() in the new vsock_bound_sockets_in_table() macro.

If you want to add it, we need to do it in a separate commit. But since 
we already have so many changes and both arrays are statically allocated 
in the same file, IMHO we can avoid the check.

Stefano
Amery Hung July 30, 2024, 5:56 p.m. UTC | #4
On Tue, Jul 30, 2024 at 1:00 AM Stefano Garzarella <sgarzare@redhat.com> wrote:
>
> On Sun, Jul 28, 2024 at 11:52:54AM GMT, Amery Hung wrote:
> >On Tue, Jul 23, 2024 at 7:40 AM Stefano Garzarella <sgarzare@redhat.com> wrote:
> >>
> >> On Wed, Jul 10, 2024 at 09:25:45PM GMT, Amery Hung wrote:
> >> >From: Bobby Eshleman <bobby.eshleman@bytedance.com>
> >> >
> >> >This commit makes the bind table management functions in vsock usable
> >> >for different bind tables. Future work will introduce a new table for
> >> >datagrams to avoid address collisions, and these functions will be used
> >> >there.
> >> >
> >> >Signed-off-by: Bobby Eshleman <bobby.eshleman@bytedance.com>
> >> >---
> >> > net/vmw_vsock/af_vsock.c | 34 +++++++++++++++++++++++++++-------
> >> > 1 file changed, 27 insertions(+), 7 deletions(-)
> >> >
> >> >diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
> >> >index acc15e11700c..d571be9cdbf0 100644
> >> >--- a/net/vmw_vsock/af_vsock.c
> >> >+++ b/net/vmw_vsock/af_vsock.c
> >> >@@ -232,11 +232,12 @@ static void __vsock_remove_connected(struct vsock_sock *vsk)
> >> >       sock_put(&vsk->sk);
> >> > }
> >> >
> >> >-static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
> >> >+static struct sock *vsock_find_bound_socket_common(struct sockaddr_vm *addr,
> >> >+                                                 struct list_head *bind_table)
> >> > {
> >> >       struct vsock_sock *vsk;
> >> >
> >> >-      list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) {
> >> >+      list_for_each_entry(vsk, bind_table, bound_table) {
> >> >               if (vsock_addr_equals_addr(addr, &vsk->local_addr))
> >> >                       return sk_vsock(vsk);
> >> >
> >> >@@ -249,6 +250,11 @@ static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
> >> >       return NULL;
> >> > }
> >> >
> >> >+static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
> >> >+{
> >> >+      return vsock_find_bound_socket_common(addr, vsock_bound_sockets(addr));
> >> >+}
> >> >+
> >> > static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,
> >> >                                                 struct sockaddr_vm *dst)
> >> > {
> >> >@@ -671,12 +677,18 @@ static void vsock_pending_work(struct work_struct *work)
> >> >
> >> > /**** SOCKET OPERATIONS ****/
> >> >
> >> >-static int __vsock_bind_connectible(struct vsock_sock *vsk,
> >> >-                                  struct sockaddr_vm *addr)
> >> >+static int vsock_bind_common(struct vsock_sock *vsk,
> >> >+                           struct sockaddr_vm *addr,
> >> >+                           struct list_head *bind_table,
> >> >+                           size_t table_size)
> >> > {
> >> >       static u32 port;
> >> >       struct sockaddr_vm new_addr;
> >> >
> >> >+      if (WARN_ONCE(table_size < VSOCK_HASH_SIZE,
> >> >+                    "table size too small, may cause overflow"))
> >> >+              return -EINVAL;
> >> >+
> >>
> >> I'd add this in another commit.
> >>
> >> >       if (!port)
> >> >               port = get_random_u32_above(LAST_RESERVED_PORT);
> >> >
> >> >@@ -692,7 +704,8 @@ static int __vsock_bind_connectible(struct
> >> >vsock_sock *vsk,
> >> >
> >> >                       new_addr.svm_port = port++;
> >> >
> >> >-                      if (!__vsock_find_bound_socket(&new_addr)) {
> >> >+                      if (!vsock_find_bound_socket_common(&new_addr,
> >> >+                                                          &bind_table[VSOCK_HASH(addr)])) {
> >>
> >> Can we add a macro for `&bind_table[VSOCK_HASH(addr)])` ?
> >>
> >
> >Definitely. I will add the following macro:
> >
> >#define vsock_bound_sockets_in_table(bind_table, addr) \
> >        (&bind_table[VSOCK_HASH(addr)])
>
> yeah.
>
> >
> >> >                               found = true;
> >> >                               break;
> >> >                       }
> >> >@@ -709,7 +722,8 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk,
> >> >                       return -EACCES;
> >> >               }
> >> >
> >> >-              if (__vsock_find_bound_socket(&new_addr))
> >> >+              if (vsock_find_bound_socket_common(&new_addr,
> >> >+                                                 &bind_table[VSOCK_HASH(addr)]))
> >> >                       return -EADDRINUSE;
> >> >       }
> >> >
> >> >@@ -721,11 +735,17 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk,
> >> >        * by AF_UNIX.
> >> >        */
> >> >       __vsock_remove_bound(vsk);
> >> >-      __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
> >> >+      __vsock_insert_bound(&bind_table[VSOCK_HASH(&vsk->local_addr)], vsk);
> >> >
> >> >       return 0;
> >> > }
> >> >
> >> >+static int __vsock_bind_connectible(struct vsock_sock *vsk,
> >> >+                                  struct sockaddr_vm *addr)
> >> >+{
> >> >+      return vsock_bind_common(vsk, addr, vsock_bind_table, VSOCK_HASH_SIZE + 1);
> >>
> >> What about using ARRAY_SIZE(x) ?
> >>
> >> BTW we are using that size just to check it, but all the arrays we use
> >> are statically allocated, so what about a compile time check like
> >> BUILD_BUG_ON()?
> >>
> >
> >I will remove the table_size check you mentioned earlier and the
> >argument here as the arrays are allocated statically like you
> >mentioned.
> >
> >If you think this check may be a good addition, I can add a
> >BUILD_BUG_ON() in the new vsock_bound_sockets_in_table() macro.
>
> If you want to add it, we need to do it in a separate commit. But since
> we already have so many changes and both arrays are statically allocated
> in the same file, IMHO we can avoid the check.
>
> Stefano
>

Okay. I will not add the check.

Thanks,
Amery
diff mbox series

Patch

diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index acc15e11700c..d571be9cdbf0 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -232,11 +232,12 @@  static void __vsock_remove_connected(struct vsock_sock *vsk)
 	sock_put(&vsk->sk);
 }
 
-static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
+static struct sock *vsock_find_bound_socket_common(struct sockaddr_vm *addr,
+						   struct list_head *bind_table)
 {
 	struct vsock_sock *vsk;
 
-	list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) {
+	list_for_each_entry(vsk, bind_table, bound_table) {
 		if (vsock_addr_equals_addr(addr, &vsk->local_addr))
 			return sk_vsock(vsk);
 
@@ -249,6 +250,11 @@  static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
 	return NULL;
 }
 
+static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
+{
+	return vsock_find_bound_socket_common(addr, vsock_bound_sockets(addr));
+}
+
 static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,
 						  struct sockaddr_vm *dst)
 {
@@ -671,12 +677,18 @@  static void vsock_pending_work(struct work_struct *work)
 
 /**** SOCKET OPERATIONS ****/
 
-static int __vsock_bind_connectible(struct vsock_sock *vsk,
-				    struct sockaddr_vm *addr)
+static int vsock_bind_common(struct vsock_sock *vsk,
+			     struct sockaddr_vm *addr,
+			     struct list_head *bind_table,
+			     size_t table_size)
 {
 	static u32 port;
 	struct sockaddr_vm new_addr;
 
+	if (WARN_ONCE(table_size < VSOCK_HASH_SIZE,
+		      "table size too small, may cause overflow"))
+		return -EINVAL;
+
 	if (!port)
 		port = get_random_u32_above(LAST_RESERVED_PORT);
 
@@ -692,7 +704,8 @@  static int __vsock_bind_connectible(struct vsock_sock *vsk,
 
 			new_addr.svm_port = port++;
 
-			if (!__vsock_find_bound_socket(&new_addr)) {
+			if (!vsock_find_bound_socket_common(&new_addr,
+							    &bind_table[VSOCK_HASH(addr)])) {
 				found = true;
 				break;
 			}
@@ -709,7 +722,8 @@  static int __vsock_bind_connectible(struct vsock_sock *vsk,
 			return -EACCES;
 		}
 
-		if (__vsock_find_bound_socket(&new_addr))
+		if (vsock_find_bound_socket_common(&new_addr,
+						   &bind_table[VSOCK_HASH(addr)]))
 			return -EADDRINUSE;
 	}
 
@@ -721,11 +735,17 @@  static int __vsock_bind_connectible(struct vsock_sock *vsk,
 	 * by AF_UNIX.
 	 */
 	__vsock_remove_bound(vsk);
-	__vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
+	__vsock_insert_bound(&bind_table[VSOCK_HASH(&vsk->local_addr)], vsk);
 
 	return 0;
 }
 
+static int __vsock_bind_connectible(struct vsock_sock *vsk,
+				    struct sockaddr_vm *addr)
+{
+	return vsock_bind_common(vsk, addr, vsock_bind_table, VSOCK_HASH_SIZE + 1);
+}
+
 static int __vsock_bind_dgram(struct vsock_sock *vsk,
 			      struct sockaddr_vm *addr)
 {