diff mbox series

[v3,1/2] telemetry: fix missing check for thread creation

Message ID 1618487412-26678-2-git-send-email-humin29@huawei.com (mailing list archive)
State Superseded
Delegated to: David Marchand
Headers show
Series fix missing check for thread creation | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Min Hu (Connor) April 15, 2021, 11:50 a.m. UTC
From: Chengwen Feng <fengchengwen@huawei.com>

Add result check and message print out for thread creation after
failure.

Fixes: b80fe1805eee ("telemetry: introduce backward compatibility")
Cc: stable@dpdk.org

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
---
 lib/librte_telemetry/telemetry.c        | 30 +++++++++++++++++++++++++++---
 lib/librte_telemetry/telemetry_legacy.c | 10 ++++++++--
 2 files changed, 35 insertions(+), 5 deletions(-)

Comments

David Marchand April 15, 2021, 2:55 p.m. UTC | #1
On Thu, Apr 15, 2021 at 1:50 PM Min Hu (Connor) <humin29@huawei.com> wrote:
> diff --git a/lib/librte_telemetry/telemetry.c b/lib/librte_telemetry/telemetry.c
> index 7e08afd..e6a99f3 100644
> --- a/lib/librte_telemetry/telemetry.c
> +++ b/lib/librte_telemetry/telemetry.c
> @@ -350,6 +350,7 @@ socket_listener(void *socket)
>  {
>         while (1) {
>                 pthread_t th;
> +               int rc;
>                 struct socket *s = (struct socket *)socket;
>                 int s_accepted = accept(s->sock, NULL, NULL);
>                 if (s_accepted < 0) {
> @@ -366,7 +367,12 @@ socket_listener(void *socket)
>                         __atomic_add_fetch(s->num_clients, 1,
>                                         __ATOMIC_RELAXED);
>                 }
> -               pthread_create(&th, NULL, s->fn, (void *)(uintptr_t)s_accepted);
> +               rc = pthread_create(&th, NULL, s->fn, (void *)(uintptr_t)s_accepted);
> +               if (rc != 0) {
> +                       TMTY_LOG(ERR, "Error with create client thread\n");
> +                       close(s_accepted);
> +                       return NULL;
> +               }

Repeating my comment:

Either you use this rc variable (adding strerror(rc) in the log
message in TMTY_LOG()) or you remove the variable and simply test if
(pthread_create(...) != 0).

This comment applies to other changes in this patch.
And this applies to patch 2 too.

>                 pthread_detach(th);
>         }
>         return NULL;

Thanks.


--
David Marchand
Power, Ciara April 15, 2021, 4:11 p.m. UTC | #2
Hi Connor, 

Thanks for looking at this. Left some comments inline.

Ciara

>-----Original Message-----
>From: Min Hu (Connor) <humin29@huawei.com>
>Sent: Thursday 15 April 2021 12:50
>To: dev@dpdk.org
>Cc: Yigit, Ferruh <ferruh.yigit@intel.com>; Power, Ciara
><ciara.power@intel.com>; Pattan, Reshma <reshma.pattan@intel.com>;
>david.marchand@redhat.com
>Subject: [PATCH v3 1/2] telemetry: fix missing check for thread creation
>
>From: Chengwen Feng <fengchengwen@huawei.com>
>
>Add result check and message print out for thread creation after failure.
>
>Fixes: b80fe1805eee ("telemetry: introduce backward compatibility")
>Cc: stable@dpdk.org
>
>Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
>Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
>---
> lib/librte_telemetry/telemetry.c        | 30 +++++++++++++++++++++++++++-
>--
> lib/librte_telemetry/telemetry_legacy.c | 10 ++++++++--
> 2 files changed, 35 insertions(+), 5 deletions(-)
>
>diff --git a/lib/librte_telemetry/telemetry.c
>b/lib/librte_telemetry/telemetry.c
>index 7e08afd..e6a99f3 100644
>--- a/lib/librte_telemetry/telemetry.c
>+++ b/lib/librte_telemetry/telemetry.c
>@@ -350,6 +350,7 @@ socket_listener(void *socket)  {
> 	while (1) {
> 		pthread_t th;
>+		int rc;
> 		struct socket *s = (struct socket *)socket;
> 		int s_accepted = accept(s->sock, NULL, NULL);
> 		if (s_accepted < 0) {
>@@ -366,7 +367,12 @@ socket_listener(void *socket)
> 			__atomic_add_fetch(s->num_clients, 1,
> 					__ATOMIC_RELAXED);
> 		}
>-		pthread_create(&th, NULL, s->fn, (void
>*)(uintptr_t)s_accepted);
>+		rc = pthread_create(&th, NULL, s->fn, (void
>*)(uintptr_t)s_accepted);
>+		if (rc != 0) {
>+			TMTY_LOG(ERR, "Error with create client thread\n");
>+			close(s_accepted);
>+			return NULL;


I think this should be "continue" instead of "return NULL" - if one client connection thread fails,
we probably want to keep listening for more clients rather than stop completely.

The "s->num_clients" counter should be decremented here too,
it gets incremented above when the connection is accepted, but if we are closing the connection,
the counter should reflect that.


>+		}
> 		pthread_detach(th);
> 	}
> 	return NULL;
>@@ -425,6 +431,7 @@ static int
> telemetry_legacy_init(void)
> {
> 	pthread_t t_old;
>+	int rc;
>
> 	if (num_legacy_callbacks == 1) {
> 		TMTY_LOG(WARNING, "No legacy callbacks, legacy socket not
>created\n"); @@ -440,7 +447,15 @@ telemetry_legacy_init(void)
> 	v1_socket.sock = create_socket(v1_socket.path);
> 	if (v1_socket.sock < 0)
> 		return -1;
>-	pthread_create(&t_old, NULL, socket_listener, &v1_socket);
>+	rc = pthread_create(&t_old, NULL, socket_listener, &v1_socket);
>+	if (rc != 0) {
>+		TMTY_LOG(ERR, "Error with create thread for legacy
>socket\n");
>+		close(v1_socket.sock);
>+		v1_socket.sock = -1;
>+		unlink(v1_socket.path);
>+		v1_socket.path[0] = '\0';
>+		return -1;
>+	}
> 	pthread_setaffinity_np(t_old, sizeof(*thread_cpuset),
>thread_cpuset);
>
> 	TMTY_LOG(DEBUG, "Legacy telemetry socket initialized ok\n"); @@ -
>451,6 +466,7 @@ static int
> telemetry_v2_init(void)
> {
> 	pthread_t t_new;
>+	int rc;
>
> 	v2_socket.num_clients = &v2_clients;
> 	rte_telemetry_register_cmd("/", list_commands, @@ -469,7 +485,15
>@@ telemetry_v2_init(void)
> 	v2_socket.sock = create_socket(v2_socket.path);
> 	if (v2_socket.sock < 0)
> 		return -1;
>-	pthread_create(&t_new, NULL, socket_listener, &v2_socket);
>+	rc = pthread_create(&t_new, NULL, socket_listener, &v2_socket);
>+	if (rc != 0) {
>+		TMTY_LOG(ERR, "Error with create thread for socket");
>+		close(v2_socket.sock);
>+		v2_socket.sock = -1;
>+		unlink(v2_socket.path);
>+		v2_socket.path[0] = '\0';
>+		return -1;
>+	}
> 	pthread_setaffinity_np(t_new, sizeof(*thread_cpuset),
>thread_cpuset);
> 	atexit(unlink_sockets);
>
>diff --git a/lib/librte_telemetry/telemetry_legacy.c
>b/lib/librte_telemetry/telemetry_legacy.c
>index 5e9af37..fd242bf 100644
>--- a/lib/librte_telemetry/telemetry_legacy.c
>+++ b/lib/librte_telemetry/telemetry_legacy.c
>@@ -83,6 +83,7 @@ register_client(const char *cmd __rte_unused, const
>char *params,
> 	pthread_t th;
> 	char data[BUF_SIZE];
> 	int fd;
>+	int rc;
> 	struct sockaddr_un addrs;
> #endif /* !RTE_EXEC_ENV_WINDOWS */
>
>@@ -112,8 +113,13 @@ register_client(const char *cmd __rte_unused, const
>char *params,
> 		close(fd);
> 		return -1;
> 	}
>-	pthread_create(&th, NULL, &legacy_client_handler,
>-			(void *)(uintptr_t)fd);
>+	rc = pthread_create(&th, NULL, &legacy_client_handler,
>+				(void *)(uintptr_t)fd);
>+	if (rc != 0) {
>+		perror("Failed to create legacy client thread\n");
>+		close(fd);
>+		return -1;
>+	}
> #endif /* !RTE_EXEC_ENV_WINDOWS */
> 	return 0;
> }
>--
>2.7.4
Min Hu (Connor) April 16, 2021, 8:19 a.m. UTC | #3
Thanks Power, v4 has been sent, please check it out.

在 2021/4/16 0:11, Power, Ciara 写道:
> Hi Connor,
> 
> Thanks for looking at this. Left some comments inline.
> 
> Ciara
> 
>> -----Original Message-----
>> From: Min Hu (Connor) <humin29@huawei.com>
>> Sent: Thursday 15 April 2021 12:50
>> To: dev@dpdk.org
>> Cc: Yigit, Ferruh <ferruh.yigit@intel.com>; Power, Ciara
>> <ciara.power@intel.com>; Pattan, Reshma <reshma.pattan@intel.com>;
>> david.marchand@redhat.com
>> Subject: [PATCH v3 1/2] telemetry: fix missing check for thread creation
>>
>> From: Chengwen Feng <fengchengwen@huawei.com>
>>
>> Add result check and message print out for thread creation after failure.
>>
>> Fixes: b80fe1805eee ("telemetry: introduce backward compatibility")
>> Cc: stable@dpdk.org
>>
>> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
>> Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
>> ---
>> lib/librte_telemetry/telemetry.c        | 30 +++++++++++++++++++++++++++-
>> --
>> lib/librte_telemetry/telemetry_legacy.c | 10 ++++++++--
>> 2 files changed, 35 insertions(+), 5 deletions(-)
>>
>> diff --git a/lib/librte_telemetry/telemetry.c
>> b/lib/librte_telemetry/telemetry.c
>> index 7e08afd..e6a99f3 100644
>> --- a/lib/librte_telemetry/telemetry.c
>> +++ b/lib/librte_telemetry/telemetry.c
>> @@ -350,6 +350,7 @@ socket_listener(void *socket)  {
>> 	while (1) {
>> 		pthread_t th;
>> +		int rc;
>> 		struct socket *s = (struct socket *)socket;
>> 		int s_accepted = accept(s->sock, NULL, NULL);
>> 		if (s_accepted < 0) {
>> @@ -366,7 +367,12 @@ socket_listener(void *socket)
>> 			__atomic_add_fetch(s->num_clients, 1,
>> 					__ATOMIC_RELAXED);
>> 		}
>> -		pthread_create(&th, NULL, s->fn, (void
>> *)(uintptr_t)s_accepted);
>> +		rc = pthread_create(&th, NULL, s->fn, (void
>> *)(uintptr_t)s_accepted);
>> +		if (rc != 0) {
>> +			TMTY_LOG(ERR, "Error with create client thread\n");
>> +			close(s_accepted);
>> +			return NULL;
> 
> 
> I think this should be "continue" instead of "return NULL" - if one client connection thread fails,
> we probably want to keep listening for more clients rather than stop completely.
> 
> The "s->num_clients" counter should be decremented here too,
> it gets incremented above when the connection is accepted, but if we are closing the connection,
> the counter should reflect that.
> 
> 
>> +		}
>> 		pthread_detach(th);
>> 	}
>> 	return NULL;
>> @@ -425,6 +431,7 @@ static int
>> telemetry_legacy_init(void)
>> {
>> 	pthread_t t_old;
>> +	int rc;
>>
>> 	if (num_legacy_callbacks == 1) {
>> 		TMTY_LOG(WARNING, "No legacy callbacks, legacy socket not
>> created\n"); @@ -440,7 +447,15 @@ telemetry_legacy_init(void)
>> 	v1_socket.sock = create_socket(v1_socket.path);
>> 	if (v1_socket.sock < 0)
>> 		return -1;
>> -	pthread_create(&t_old, NULL, socket_listener, &v1_socket);
>> +	rc = pthread_create(&t_old, NULL, socket_listener, &v1_socket);
>> +	if (rc != 0) {
>> +		TMTY_LOG(ERR, "Error with create thread for legacy
>> socket\n");
>> +		close(v1_socket.sock);
>> +		v1_socket.sock = -1;
>> +		unlink(v1_socket.path);
>> +		v1_socket.path[0] = '\0';
>> +		return -1;
>> +	}
>> 	pthread_setaffinity_np(t_old, sizeof(*thread_cpuset),
>> thread_cpuset);
>>
>> 	TMTY_LOG(DEBUG, "Legacy telemetry socket initialized ok\n"); @@ -
>> 451,6 +466,7 @@ static int
>> telemetry_v2_init(void)
>> {
>> 	pthread_t t_new;
>> +	int rc;
>>
>> 	v2_socket.num_clients = &v2_clients;
>> 	rte_telemetry_register_cmd("/", list_commands, @@ -469,7 +485,15
>> @@ telemetry_v2_init(void)
>> 	v2_socket.sock = create_socket(v2_socket.path);
>> 	if (v2_socket.sock < 0)
>> 		return -1;
>> -	pthread_create(&t_new, NULL, socket_listener, &v2_socket);
>> +	rc = pthread_create(&t_new, NULL, socket_listener, &v2_socket);
>> +	if (rc != 0) {
>> +		TMTY_LOG(ERR, "Error with create thread for socket");
>> +		close(v2_socket.sock);
>> +		v2_socket.sock = -1;
>> +		unlink(v2_socket.path);
>> +		v2_socket.path[0] = '\0';
>> +		return -1;
>> +	}
>> 	pthread_setaffinity_np(t_new, sizeof(*thread_cpuset),
>> thread_cpuset);
>> 	atexit(unlink_sockets);
>>
>> diff --git a/lib/librte_telemetry/telemetry_legacy.c
>> b/lib/librte_telemetry/telemetry_legacy.c
>> index 5e9af37..fd242bf 100644
>> --- a/lib/librte_telemetry/telemetry_legacy.c
>> +++ b/lib/librte_telemetry/telemetry_legacy.c
>> @@ -83,6 +83,7 @@ register_client(const char *cmd __rte_unused, const
>> char *params,
>> 	pthread_t th;
>> 	char data[BUF_SIZE];
>> 	int fd;
>> +	int rc;
>> 	struct sockaddr_un addrs;
>> #endif /* !RTE_EXEC_ENV_WINDOWS */
>>
>> @@ -112,8 +113,13 @@ register_client(const char *cmd __rte_unused, const
>> char *params,
>> 		close(fd);
>> 		return -1;
>> 	}
>> -	pthread_create(&th, NULL, &legacy_client_handler,
>> -			(void *)(uintptr_t)fd);
>> +	rc = pthread_create(&th, NULL, &legacy_client_handler,
>> +				(void *)(uintptr_t)fd);
>> +	if (rc != 0) {
>> +		perror("Failed to create legacy client thread\n");
>> +		close(fd);
>> +		return -1;
>> +	}
>> #endif /* !RTE_EXEC_ENV_WINDOWS */
>> 	return 0;
>> }
>> --
>> 2.7.4
> 
> .
>
Min Hu (Connor) April 16, 2021, 8:21 a.m. UTC | #4
Hi, David, fixed in v4. Thanks.

在 2021/4/15 22:55, David Marchand 写道:
> On Thu, Apr 15, 2021 at 1:50 PM Min Hu (Connor) <humin29@huawei.com> wrote:
>> diff --git a/lib/librte_telemetry/telemetry.c b/lib/librte_telemetry/telemetry.c
>> index 7e08afd..e6a99f3 100644
>> --- a/lib/librte_telemetry/telemetry.c
>> +++ b/lib/librte_telemetry/telemetry.c
>> @@ -350,6 +350,7 @@ socket_listener(void *socket)
>>   {
>>          while (1) {
>>                  pthread_t th;
>> +               int rc;
>>                  struct socket *s = (struct socket *)socket;
>>                  int s_accepted = accept(s->sock, NULL, NULL);
>>                  if (s_accepted < 0) {
>> @@ -366,7 +367,12 @@ socket_listener(void *socket)
>>                          __atomic_add_fetch(s->num_clients, 1,
>>                                          __ATOMIC_RELAXED);
>>                  }
>> -               pthread_create(&th, NULL, s->fn, (void *)(uintptr_t)s_accepted);
>> +               rc = pthread_create(&th, NULL, s->fn, (void *)(uintptr_t)s_accepted);
>> +               if (rc != 0) {
>> +                       TMTY_LOG(ERR, "Error with create client thread\n");
>> +                       close(s_accepted);
>> +                       return NULL;
>> +               }
> 
> Repeating my comment:
> 
> Either you use this rc variable (adding strerror(rc) in the log
> message in TMTY_LOG()) or you remove the variable and simply test if
> (pthread_create(...) != 0).
> 
> This comment applies to other changes in this patch.
> And this applies to patch 2 too.
> 
>>                  pthread_detach(th);
>>          }
>>          return NULL;
> 
> Thanks.
> 
> 
> --
> David Marchand
> 
> .
>
diff mbox series

Patch

diff --git a/lib/librte_telemetry/telemetry.c b/lib/librte_telemetry/telemetry.c
index 7e08afd..e6a99f3 100644
--- a/lib/librte_telemetry/telemetry.c
+++ b/lib/librte_telemetry/telemetry.c
@@ -350,6 +350,7 @@  socket_listener(void *socket)
 {
 	while (1) {
 		pthread_t th;
+		int rc;
 		struct socket *s = (struct socket *)socket;
 		int s_accepted = accept(s->sock, NULL, NULL);
 		if (s_accepted < 0) {
@@ -366,7 +367,12 @@  socket_listener(void *socket)
 			__atomic_add_fetch(s->num_clients, 1,
 					__ATOMIC_RELAXED);
 		}
-		pthread_create(&th, NULL, s->fn, (void *)(uintptr_t)s_accepted);
+		rc = pthread_create(&th, NULL, s->fn, (void *)(uintptr_t)s_accepted);
+		if (rc != 0) {
+			TMTY_LOG(ERR, "Error with create client thread\n");
+			close(s_accepted);
+			return NULL;
+		}
 		pthread_detach(th);
 	}
 	return NULL;
@@ -425,6 +431,7 @@  static int
 telemetry_legacy_init(void)
 {
 	pthread_t t_old;
+	int rc;
 
 	if (num_legacy_callbacks == 1) {
 		TMTY_LOG(WARNING, "No legacy callbacks, legacy socket not created\n");
@@ -440,7 +447,15 @@  telemetry_legacy_init(void)
 	v1_socket.sock = create_socket(v1_socket.path);
 	if (v1_socket.sock < 0)
 		return -1;
-	pthread_create(&t_old, NULL, socket_listener, &v1_socket);
+	rc = pthread_create(&t_old, NULL, socket_listener, &v1_socket);
+	if (rc != 0) {
+		TMTY_LOG(ERR, "Error with create thread for legacy socket\n");
+		close(v1_socket.sock);
+		v1_socket.sock = -1;
+		unlink(v1_socket.path);
+		v1_socket.path[0] = '\0';
+		return -1;
+	}
 	pthread_setaffinity_np(t_old, sizeof(*thread_cpuset), thread_cpuset);
 
 	TMTY_LOG(DEBUG, "Legacy telemetry socket initialized ok\n");
@@ -451,6 +466,7 @@  static int
 telemetry_v2_init(void)
 {
 	pthread_t t_new;
+	int rc;
 
 	v2_socket.num_clients = &v2_clients;
 	rte_telemetry_register_cmd("/", list_commands,
@@ -469,7 +485,15 @@  telemetry_v2_init(void)
 	v2_socket.sock = create_socket(v2_socket.path);
 	if (v2_socket.sock < 0)
 		return -1;
-	pthread_create(&t_new, NULL, socket_listener, &v2_socket);
+	rc = pthread_create(&t_new, NULL, socket_listener, &v2_socket);
+	if (rc != 0) {
+		TMTY_LOG(ERR, "Error with create thread for socket");
+		close(v2_socket.sock);
+		v2_socket.sock = -1;
+		unlink(v2_socket.path);
+		v2_socket.path[0] = '\0';
+		return -1;
+	}
 	pthread_setaffinity_np(t_new, sizeof(*thread_cpuset), thread_cpuset);
 	atexit(unlink_sockets);
 
diff --git a/lib/librte_telemetry/telemetry_legacy.c b/lib/librte_telemetry/telemetry_legacy.c
index 5e9af37..fd242bf 100644
--- a/lib/librte_telemetry/telemetry_legacy.c
+++ b/lib/librte_telemetry/telemetry_legacy.c
@@ -83,6 +83,7 @@  register_client(const char *cmd __rte_unused, const char *params,
 	pthread_t th;
 	char data[BUF_SIZE];
 	int fd;
+	int rc;
 	struct sockaddr_un addrs;
 #endif /* !RTE_EXEC_ENV_WINDOWS */
 
@@ -112,8 +113,13 @@  register_client(const char *cmd __rte_unused, const char *params,
 		close(fd);
 		return -1;
 	}
-	pthread_create(&th, NULL, &legacy_client_handler,
-			(void *)(uintptr_t)fd);
+	rc = pthread_create(&th, NULL, &legacy_client_handler,
+				(void *)(uintptr_t)fd);
+	if (rc != 0) {
+		perror("Failed to create legacy client thread\n");
+		close(fd);
+		return -1;
+	}
 #endif /* !RTE_EXEC_ENV_WINDOWS */
 	return 0;
 }