9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: presotto@plan9.bell-labs.com
To: 9fans@cse.psu.edu
Subject: Re: [9fans] Kernel question: i386 test-and-set problem
Date: Wed,  2 Aug 2000 12:24:41 -0400	[thread overview]
Message-ID: <200008021624.MAA28432@cse.psu.edu> (raw)

[-- Attachment #1: Type: text/plain, Size: 778 bytes --]

As I said in the last message, there has to be synchronization
external to the sleep code to make such a situation work.  For
example

	lock list
	add to list
	unlock list
	sleep
	lock list
	free
	unlock list

and another process will do

	lock list
	if(something to wakeup)
		wakeup
	unlock list

Things calling sleep/wakeup can and do perform such
synchronization.  It's part of writing a multithreaded
kernel, i.e., snchronizing accesses to structures.
You don't haphazardly free things when there are
possible accesses to them outstanding in other threads,
or at least I try not to.

Unfortunately, postnote is an asyncronous event often
initiated by someone that's trying to stop something.
It doesn't know about this external synchronization.

[-- Attachment #2: Type: message/rfc822, Size: 4982 bytes --]

[-- Attachment #2.1.1: Type: text/plain, Size: 178 bytes --]

Doesn't
	/*
	 * Expects that only one process can call wakeup for any given Rendez
	 */
	int
	wakeup(Rendez *r)
	{

mean that process p cannot continue after the sleep?

[-- Attachment #2.1.2: Type: message/rfc822, Size: 3269 bytes --]

From: miller@hamnavoe.demon.co.uk
To: 9fans@cse.psu.edu
Subject: Re: [9fans] Kernel question: i386 test-and-set problem
Date: Wed, 2 Aug 2000 15:51:33 0100
Message-ID: <E13Jzoi-000AkU-0A@finch-post-10.mail.demon.net>

> If it were at all possible for wakeup to be called with
> r already freed, the code would be wrong to begin with
> since r is an argument to wakeup.

For things to go wrong it's not necessary for wakeup to be
called after r is freed; what I said was "the free(r) and
malloc() could happen *while* or even before wakeup(r) runs".
It's sufficient to have sleep(r) and wakeup(r) racing on
two processors.  We could have this interleaving of events:

sleep(r) is called
  wakeup(r) is called
sleep tests wakeup condition, returns
  wakeup is delayed by an interrupt on its processor
caller of sleep deallocates structure containing r
some other process reallocates r and clobbers r->p
  wakeup resumes, dereferences r->p, ka-boom!

If you think sleep and wakeup are sufficiently interlocked
by higher level considerations that this can't happen,
then let's use your scenario with postnote, mutatis mutandis
to apply to the existing kernel algorithm:

process x calls postnote:
	postnote(p):
		p->notepending = 1
		lock(p->rlock)
		r = p->r
		if r != 0
			if(r->p == p && p->r == r)
				r->p = 0
				p->r = 0
				ready(p)
		unlock(p->rlock)

Immediately after the unlock(p->rlock) is executed,
process q calls wakeup

	process q:
		wakeup(r):    {wakeup condition is satisfied}
			coherence()
			p = r->p
			if p != 0
				lock(p->rlock)
				if (r->p == p && p->r == r)
				    r->p = 0
				    p->r = 0	
				    ready(p)
			    unlock(p->rlock)

During the call to coherence() process q is interrupted.
Process p now continues after the sleep:

	process p:
		sleep(r);
		free(r)

Process y now does

		xxx = malloc(234);
		xxx->a = 12;

And finally process q resumes, and loads and dereferences
r->p which is no longer vald.  ka-boom again.

-- Richard

             reply	other threads:[~2000-08-02 16:24 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-08-02 16:24 presotto [this message]
  -- strict thread matches above, loose matches on Subject: below --
2000-08-03  9:56 miller
2000-08-02 15:43 jmk
2000-08-02 14:51 miller
2000-08-02 13:20 presotto
2000-08-02  8:32 miller
2000-07-31 17:26 presotto
2000-07-23 14:41 miller
2000-07-21 13:15 presotto
2000-07-21  9:10 miller
2000-07-20 17:09 presotto
2000-07-20 13:54 miller
2000-07-20  2:03 jmk
2000-07-10 16:21 miller
2000-07-10 12:40 Russ Cox
2000-07-11  8:51 ` Jakub Jermar
2000-07-10  9:57 Jakub Jermar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200008021624.MAA28432@cse.psu.edu \
    --to=presotto@plan9.bell-labs.com \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).