9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: erik quanstrom <quanstro@quanstro.net>
To: 9fans@9fans.net
Subject: [9fans] vunmap() bug
Date: Thu, 23 Jun 2011 21:04:28 -0400	[thread overview]
Message-ID: <38d47085b6f6ddb3af96fb1cc0670ee4@ladd.quanstro.net> (raw)

today i spent a little while chasing a crash that happened after
a driver i'm working on gave up during initialization and decided
to free its vmaps.  it crashed in vunmap.  since this hardware does
some crazy things, it took a while to rule out the hardware.
after all, vunmap can't possibly be broken, right?

as it turns out, there was an off by one error in pdbunmap().
in the case that (va&(4*MB-1)) == 0 and a 4mb pde were used,
the code would stick on the same address and attempt to double-free
the same va.  the code was
	va = (va+4*MB-1) & ~(4*MB-1);
this lead to a panic, since the second time through the va was
not mapped.  the reason for this is that a round-up algorithm
was used, and this is off by one.  i changed the code slightly
to eliminate the rounding in the case of a 4mb mapping.
we're assurred that (va&(4*MB-1)) == 0 in this case. (cf. pdbmap())

- erik

----

static void
pdbunmap(ulong *pdb, ulong va, int size)
{
	ulong vae;
	ulong *table;

	vae = va+size;
	while(va < vae){
		table = &pdb[PDX(va)];
		if(!(*table & PTEVALID)){
			panic("vunmap: not mapped pde");
			/*
			va = (va+4*MB) & ~(4*MB-1);
			continue;
			*/
		}
		if(*table & PTESIZE){
			if(va & 4*MB-1)
				panic("vunmap: misaligned: %#p\n", va);
			*table = 0;
			va += 4*MB;
			continue;
		}
		table = KADDR(PPN(*table));
		if(!(table[PTX(va)] & PTEVALID))
			panic("vunmap: not mapped");
		table[PTX(va)] = 0;
		va += BY2PG;
	}
}



                 reply	other threads:[~2011-06-24  1:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=38d47085b6f6ddb3af96fb1cc0670ee4@ladd.quanstro.net \
    --to=quanstro@quanstro.net \
    --cc=9fans@9fans.net \
    /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).