eal/linux: truncate thread name

Message ID 20200710094551.3716-1-david.marchand@redhat.com (mailing list archive)
State Accepted, archived
Delegated to: David Marchand
Headers
Series eal/linux: truncate thread name |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/iol-broadcom-Performance success Performance Testing PASS
ci/Intel-compilation success Compilation OK
ci/iol-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS

Commit Message

David Marchand July 10, 2020, 9:45 a.m. UTC
  pthread_setname_np refuses names larger than 16 bytes (\0 included).
Rather than return an error, truncate the name to this limit in the
rte_thread_setname helper.

Caught with ixgbe which creates control thread with name
"ixgbe-link-handler":

Configuring Port 0 (socket 0)
EAL: Cannot set name for ctrl thread
...
EAL: Cannot set name for ctrl thread

Port 0: link state change event
...
EAL: Cannot set name for ctrl thread

Port 0: link state change event

Note: before this change, the thread would keep its original name, which
meant in my test for the ixgbe handler either "dpdk-testpmd" or
"eal-intr-thread".

Signed-off-by: David Marchand <david.marchand@redhat.com>
---
 lib/librte_eal/linux/eal_thread.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
  

Comments

Thomas Monjalon July 10, 2020, 12:41 p.m. UTC | #1
10/07/2020 11:45, David Marchand:
> pthread_setname_np refuses names larger than 16 bytes (\0 included).
> Rather than return an error, truncate the name to this limit in the
> rte_thread_setname helper.
[...]
> --- a/lib/librte_eal/linux/eal_thread.c
> +++ b/lib/librte_eal/linux/eal_thread.c
> @@ -153,7 +153,10 @@ int rte_thread_setname(pthread_t id, const char *name)
>  	int ret = ENOSYS;
>  #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
>  #if __GLIBC_PREREQ(2, 12)
> -	ret = pthread_setname_np(id, name);
> +	char truncated[16];

That's a pity POSIX is not defining a constant for this limit.

> +
> +	strlcpy(truncated, name, sizeof(truncated));
> +	ret = pthread_setname_np(id, truncated);
>  #endif
>  #endif

Acked-by: Thomas Monjalon <thomas@monjalon.net>
  
David Marchand July 10, 2020, 12:44 p.m. UTC | #2
On Fri, Jul 10, 2020 at 2:41 PM Thomas Monjalon <thomas@monjalon.net> wrote:
>
> 10/07/2020 11:45, David Marchand:
> > pthread_setname_np refuses names larger than 16 bytes (\0 included).
> > Rather than return an error, truncate the name to this limit in the
> > rte_thread_setname helper.
> [...]
> > --- a/lib/librte_eal/linux/eal_thread.c
> > +++ b/lib/librte_eal/linux/eal_thread.c
> > @@ -153,7 +153,10 @@ int rte_thread_setname(pthread_t id, const char *name)
> >       int ret = ENOSYS;
> >  #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
> >  #if __GLIBC_PREREQ(2, 12)
> > -     ret = pthread_setname_np(id, name);
> > +     char truncated[16];
>
> That's a pity POSIX is not defining a constant for this limit.

pthread_setname "_np" :-)
  
David Marchand July 11, 2020, 1:47 p.m. UTC | #3
On Fri, Jul 10, 2020 at 2:41 PM Thomas Monjalon <thomas@monjalon.net> wrote:
> 10/07/2020 11:45, David Marchand:
> > pthread_setname_np refuses names larger than 16 bytes (\0 included).
> > Rather than return an error, truncate the name to this limit in the
> > rte_thread_setname helper.
> Acked-by: Thomas Monjalon <thomas@monjalon.net>

Applied.
  

Patch

diff --git a/lib/librte_eal/linux/eal_thread.c b/lib/librte_eal/linux/eal_thread.c
index 48a2c1124b..068de25595 100644
--- a/lib/librte_eal/linux/eal_thread.c
+++ b/lib/librte_eal/linux/eal_thread.c
@@ -153,7 +153,10 @@  int rte_thread_setname(pthread_t id, const char *name)
 	int ret = ENOSYS;
 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
 #if __GLIBC_PREREQ(2, 12)
-	ret = pthread_setname_np(id, name);
+	char truncated[16];
+
+	strlcpy(truncated, name, sizeof(truncated));
+	ret = pthread_setname_np(id, truncated);
 #endif
 #endif
 	RTE_SET_USED(id);