Bug 277065 - [libthr] reachable memory
Summary: [libthr] reachable memory
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: threads (show other bugs)
Version: 14.0-RELEASE
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-threads (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-02-15 12:31 UTC by Paul Floyd
Modified: 2024-02-15 13:22 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Floyd 2024-02-15 12:31:35 UTC
This is a follow on from https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=276818
Using the same testcase

I see two reachable allocations in Valgrind

==3215== 64 bytes in 1 blocks are still reachable in loss record 2 of 3
==3215==    at 0x48518C5: calloc (vg_replace_malloc.c:1601)
==3215==    by 0x4C7C772: ??? (in /lib/libthr.so.3)
==3215==    by 0x4C75385: ??? (in /lib/libthr.so.3)
==3215==    by 0x4C743AE: ??? (in /lib/libthr.so.3)
==3215==    by 0x400ABFC: ??? (in /libexec/ld-elf.so.1)
==3215==    by 0x40098C8: ??? (in /libexec/ld-elf.so.1)
==3215==    by 0x4006B88: ??? (in /libexec/ld-elf.so.1)
==3215== 
==3215== 1,664 bytes in 1 blocks are still reachable in loss record 3 of 3
==3215==    at 0x48518C5: calloc (vg_replace_malloc.c:1601)
==3215==    by 0x4C75374: ??? (in /lib/libthr.so.3)
==3215==    by 0x4C743AE: ??? (in /lib/libthr.so.3)
==3215==    by 0x400ABFC: ??? (in /libexec/ld-elf.so.1)
==3215==    by 0x40098C8: ??? (in /libexec/ld-elf.so.1)
==3215==    by 0x4006B88: ??? (in /libexec/ld-elf.so.1)

First problem (for me) is that Valgrind isn't reading debuginfo so I'm not seeing file and line number.

In gdb if I put breakpoints on those addresses I see that they are

4       breakpoint     keep y   0x0000000004c75374 in _thr_alloc at /usr/src/lib/libthr/thread/thr_list.c:154
5       breakpoint     keep y   0x0000000004c7c772 in _sleepq_alloc at /usr/src/lib/libthr/thread/thr_sleepq.c:66


For some reason gdb doesn't stop on the breakpoints. And if gdb can get file and line number then so should Valgrind.

The allocations are

        if (thread == NULL) {
                if (total_threads > MAX_THREADS)
                        return (NULL);
                atomic_fetchadd_int(&total_threads, 1);
                thread = calloc(1, sizeof(struct pthread));
                if (thread == NULL) {
                        atomic_fetchadd_int(&total_threads, -1);
                        return (NULL);
                }
                if ((thread->sleepqueue = _sleepq_alloc()) == NULL ||
                    (thread->wake_addr = _thr_alloc_wake_addr()) == NULL) {
                        thr_destroy(curthread, thread);
                        atomic_fetchadd_int(&total_threads, -1);
                        return (NULL);
                }


and

struct sleepqueue *
_sleepq_alloc(void)
{
        struct sleepqueue *sq;
  
        sq = calloc(1, sizeof(struct sleepqueue));
        TAILQ_INIT(&sq->sq_blocked);
        SLIST_INIT(&sq->sq_freeq);
        return (sq);
}
Comment 1 Konstantin Belousov freebsd_committer freebsd_triage 2024-02-15 13:00:23 UTC
This is the userspace struct thread allocation (curthread) for main thread during
initialization of the threading library. The memory is not supposed to be freed.
Comment 2 Paul Floyd 2024-02-15 13:20:39 UTC
(In reply to Konstantin Belousov from comment #1)

And the sleepqueue is the same?

It would be nice to have a freeres function https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=259294

IKn the meantime I'll probably add a suppression.