Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/posix/pthread/pthread.c: fix thread count limiting in pthread_create() #12056

Merged

Conversation

JulianHolzwarth
Copy link
Contributor

@JulianHolzwarth JulianHolzwarth commented Aug 21, 2019

Contribution description

While looking at posix threads I found a bug. When creating more threads than possible pthread_create does not check correctly for errors.

First change in insert because of :

 kernel_pid_t pthread_pid = insert(pt);
    if (pthread_pid == KERNEL_PID_UNDEF) {

second change because thread_create returns -EOVERFLOW not KERNEL_PID_UNDEF when returning an error (we dont need to check the other error because the priority is main thread prority)

Testing procedure

Try to create more than MAXTHREADS pthreads and join on every successfully created thread.
Before the fix it
Should I implement a test for it?

Issues/PRs references

@JulianHolzwarth
Copy link
Contributor Author

@PeterKietzmann Could you review this pr please? It looks like you are a maintainer for the POSIX Layer.

@JulianHolzwarth
Copy link
Contributor Author

@smlng Could you look at this? I was told you could be the right person.

Copy link
Member

@smlng smlng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regarding testing: maybe copy from tests/thread_flood i.e. provide a pthread variant of that in the same style?

@@ -160,7 +161,7 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr, void *(*sta
pthread_start_routine,
pt,
"pthread");
if (pt->thread_pid == KERNEL_PID_UNDEF) {
if (pt->thread_pid == -EOVERFLOW) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in error case thread_create can return -EAGAIN, -EOVERFLOW or KERNEL_PID_UNDEF, hence I would suggest to check for <= KERNEL_PID_UNDEF?

Copy link
Contributor Author

@JulianHolzwarth JulianHolzwarth Aug 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think it can return KERNEL_PID_UNDEF because it has 3 returns
Code from core/thread.c

if (pid == KERNEL_PID_UNDEF) {
        DEBUG("thread_create(): too many threads!\n");

        irq_restore(state);

        return -EOVERFLOW;
    }

return pid; cannot return KERNEL_PID_UNDEF because of previous code if (pid == KERNEL_PID_UNDEF) { ... return -EOVERFLOW

and this return that only returns when the priority is wrong. But the priority is always THREAD_PRIORITY_MAIN so this should never return -EINVAL

if (priority >= SCHED_PRIO_LEVELS) {
        return -EINVAL;
    }

That is why I only check for -EOVERFLOW
But the check <= KERNEL_PID_UNDEF could be easier to read.
Would you still suggest the change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mhm, maybe check for < KERNEL_PID_UNDEF then, bc I wouldn't rely on the priority to work either - e.g. someone might redefine THREAD_PRIORITY_MAIN making it invalid.

However, you're right it can never return KERNEL_PID_UNDEF.

Copy link
Contributor Author

@JulianHolzwarth JulianHolzwarth Aug 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will use the function pid_is_valid that should be the right way to do it.

@@ -87,7 +88,7 @@ static void *pthread_start_routine(void *pt_)

static int insert(pthread_thread_t *pt)
{
int result = -1;
int result = KERNEL_PID_UNDEF;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch!

@smlng smlng self-assigned this Aug 28, 2019
@smlng smlng added Area: core Area: RIOT kernel. Handle PRs marked with this with care! Area: POSIX Area: POSIX API wrapper Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) labels Aug 28, 2019
@JulianHolzwarth
Copy link
Contributor Author

Implementing a test is a good idea. Will do.
Should it be in the same pr? @smlng

@smlng
Copy link
Member

smlng commented Aug 28, 2019

Implementing a test is a good idea. Will do.
Should it be in the same pr? @smlng

sure, but extra commit

@kaspar030 kaspar030 changed the title sys/posix/pthread/pthread.c: pthread_create bug fix sys/posix/pthread/pthread.c: fix thread count limiting in pthread_create() Aug 29, 2019
@JulianHolzwarth
Copy link
Contributor Author

I added a small test. I can add more comments if needed.

@JulianHolzwarth
Copy link
Contributor Author

removed unnecessary includes

@smlng
Copy link
Member

smlng commented Sep 6, 2019

please fix CI issue and squash directly

The function insert returns KERNEL_PID_UNDEF now because pthread_create checks for it.
In pthread_create it checks now if thread_create returns a valid pid
@JulianHolzwarth JulianHolzwarth force-pushed the pr/posix/pthread/small_insert_fix branch from 28b604b to 2074f12 Compare September 6, 2019 13:55
@JulianHolzwarth
Copy link
Contributor Author

done

@smlng smlng added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Sep 9, 2019
Copy link
Member

@smlng smlng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested ACK

@smlng smlng added Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 3-testing The PR was tested according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines labels Sep 9, 2019
@smlng
Copy link
Member

smlng commented Sep 9, 2019

looks like you need to put the nucleo-f031k6 board into the list of insufficient memory boards.

@JulianHolzwarth
Copy link
Contributor Author

JulianHolzwarth commented Sep 11, 2019

Can I rebase? ( Fixup that added BOARD_INSUFFICIENT_MEMORY := nucleo-f031k6 )

@smlng
Copy link
Member

smlng commented Sep 11, 2019

rebase and (most importantly) squash, yes please

@JulianHolzwarth JulianHolzwarth force-pushed the pr/posix/pthread/small_insert_fix branch from b0215de to 984eb7c Compare September 11, 2019 13:08
@JulianHolzwarth
Copy link
Contributor Author

done

@jcarrano jcarrano merged commit 00e0a1c into RIOT-OS:master Sep 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: core Area: RIOT kernel. Handle PRs marked with this with care! Area: POSIX Area: POSIX API wrapper CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 3-testing The PR was tested according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants