From owner-freebsd-hackers Wed Jul 1 11:22:35 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id LAA26482 for freebsd-hackers-outgoing; Wed, 1 Jul 1998 11:22:35 -0700 (PDT) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from smtp04.primenet.com (daemon@smtp04.primenet.com [206.165.6.134]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id LAA26466 for ; Wed, 1 Jul 1998 11:22:27 -0700 (PDT) (envelope-from tlambert@usr01.primenet.com) Received: (from daemon@localhost) by smtp04.primenet.com (8.8.8/8.8.8) id LAA21392; Wed, 1 Jul 1998 11:22:27 -0700 (MST) Received: from usr01.primenet.com(206.165.6.201) via SMTP by smtp04.primenet.com, id smtpd021324; Wed Jul 1 11:22:19 1998 Received: (from tlambert@localhost) by usr01.primenet.com (8.8.5/8.8.5) id LAA29206; Wed, 1 Jul 1998 11:22:16 -0700 (MST) From: Terry Lambert Message-Id: <199807011822.LAA29206@usr01.primenet.com> Subject: Re: mlock()/mclear (was Re: Unsupport calls) To: dbj@iglou.com (David E. Brooks Jr) Date: Wed, 1 Jul 1998 18:22:15 +0000 (GMT) Cc: tlambert@primenet.com, hackers@FreeBSD.ORG In-Reply-To: from "David E. Brooks Jr" at Jun 30, 98 07:32:00 pm X-Mailer: ELM [version 2.4 PL25] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > > It is trivial to implement p/v semaphores using __asm__ to generate > > single instruction spinlocks; since the VM/buffer cache is unified, > > this will work on shared memory/mmap'ed files without needing a > > special system call to guaranteee lock coherency. > > Funny you should mention this. I was poking around the man page to > mmap() the other day looking for any hints on a "standard" way to > perform inter-process synchronization when sharing memory. This led > me to read the 'newvm' paper, specifically about > mlock()/mclear(). Well, to make a short story even shorter, I ended up > having a brief e-mail exchange with David Greenman where he said in my > mail batch this morning "...a bright beginner could tackle it." > > So, I figured I'd try and find out how bright I really am . Look at the SMP locking code; it handles all of this. Specifically, look at the big giant kernel reentrancy lock. > I haven't gotten very far (I'm re-reading relevant portions of _The > Design and Implementation of 4.4 BSD Operating System_ and lots of > stuff in section 9 right now), but I do have one question right off > the bat. Both mmap(2) and the newvm paper make mention of the > MAP_HASSEMAPHORE flag; Why is (or was) this necessary? All that comes > to mind is multi-processor systems and keeping any copies of that > page of memory synchronized. SMP synchornization is automatic, via IPI (Inter Processor Interrupt) as part of the APIC (Advanced Programmable Interrupt Controller) that is built into the CPU. Intel supports full MESI cache coherence. The place you need MAP_HASSEMAPHORE is when you need to take action to synchronize the memory. For example, the multiprocessor BeBox (and most SMP capable Apple PPC hadrware not running the PPC620) designs remove the L2 cache, and use the cache notification lines for coherency signalling. This is an MEI, not a MESI implementation. Because of this, memory that is shared between processors has to be updated like so in order to ensure cache invalidation: 1) Get a mapping for the memory 2) Mark it non-writeable 3) Take the write fault 4) Do the operation anyway (in the trap handler, using a lookaside) 5) Assert the "L2 cache changed" to invalidate the L1 cache on the other processor(s). For a non-unified VM and buffer cache, it means that you must echo the changes between the buffer cache and the VM immediately. In general, you use the same trick. For many System V shared memory (and for BSD's without a unified cache), you would use the MAP_HASSEMAPHORE flag to notify the kernel. This lets the kernel be lazy (ie: it late binds the updates from the VM to the buffer cache, and vice versa), unless it has no choice in the matter, which is much less expensive. You can actually look at the 386 support code for kernel write faults; the "copyout" function (called from uiomove) has to do similar juggling in order to cause a SEGV if the user space page it is copying out to is not present. This is because you can't get a write fault on a 386 in kernel (supervisor) mode for an unwriteable page. Terry Lambert terry@lambert.org --- Any opinions in this posting are my own and not those of my present or previous employers. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message