History

kern: don't touch proc in wakeup() after procwakeup()

In case the Rendez structure is backed by dynamic memory that might be freed after we ready the sleeping process, we must not touch it (unlock(&r->lk)) after procwakeup(). (Also remove unused copy of rendez.c).

cinap_lenrek committed 2 years ago

Commit c0951f21 parent de6172c2 files · +6 −94Browse files

Unified · Split

2 files
  1. kern/rendez.c +0 −89
  2. kern/sleep.c +6 −5
kern/rendez.c+0−89
@@ -1,89 +0,0 @@
1−#include "u.h"
2−#include "lib.h"
3−#include "dat.h"
4−#include "fns.h"
5−#include "error.h"
6−
7−void
8−sleep(Rendez *r, int (*f)(void*), void *arg)
9−{
10− int s;
11−
12− s = splhi();
13−
14− lock(&r->lk);
15− lock(&up->rlock);
16− if(r->p){
17− print("double sleep %lud %lud\n", r->p->pid, up->pid);
18− }
19−
20− /*
21− * Wakeup only knows there may be something to do by testing
22− * r->p in order to get something to lock on.
23− * Flush that information out to memory in case the sleep is
24− * committed.
25− */
26− r->p = up;
27−
28− if((*f)(arg) || up->notepending){
29− /*
30− * if condition happened or a note is pending
31− * never mind
32− */
33− r->p = nil;
34− unlock(&up->rlock);
35− unlock(&r->lk);
36− } else {
37− /*
38− * now we are committed to
39− * change state and call scheduler
40− */
41− up->state = Wakeme;
42− up->r = r;
43−
44− /* statistics */
45− /* m->cs++; */
46−
47− unlock(&up->rlock);
48− unlock(&r->lk);
49−
50− procsleep();
51− }
52−
53− if(up->notepending) {
54− up->notepending = 0;
55− splx(s);
56− error(Eintr);
57− }
58−
59− splx(s);
60−}
61−
62−Proc*
63−wakeup(Rendez *r)
64−{
65− Proc *p;
66− int s;
67−
68− s = splhi();
69−
70− lock(&r->lk);
71− p = r->p;
72−
73− if(p != nil){
74− lock(&p->rlock);
75− if(p->state != Wakeme || p->r != r)
76− panic("wakeup: state");
77− r->p = nil;
78− p->r = nil;
79− p->state = Running;
80− procwakeup(p);
81− unlock(&p->rlock);
82− }
83− unlock(&r->lk);
84−
85− splx(s);
86−
87− return p;
88−}
89−
kern/sleep.c+6−5
@@ -70,18 +70,19 @@ wakeup(Rendez *r) · 69 unmodified lines
7070 lock(&r->lk);
7171 p = r->p;
7272
73− if(p != nil){
73+ if(p == nil)
74+ unlock(&r->lk);
75+ else {
7476 lock(&p->rlock);
7577 if(p->state != Wakeme || p->r != r)
7678 panic("wakeup: state");
79+ p->state = Running;
7780 r->p = nil;
7881 p->r = nil;
79− p->state = Running;
80− procwakeup(p);
8182 unlock(&p->rlock);
83+ unlock(&r->lk);
84+ procwakeup(p);
8285 }
83− unlock(&r->lk);
84−
8586 splx(s);
8687
8788 return p;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t