From owner-freebsd-hackers@FreeBSD.ORG Wed Oct 8 08:36:28 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 538B416A4B3; Wed, 8 Oct 2003 08:36:28 -0700 (PDT) Received: from tinker.exit.com (tinker.exit.com [206.223.0.1]) by mx1.FreeBSD.org (Postfix) with ESMTP id 5A96343FEC; Wed, 8 Oct 2003 08:36:19 -0700 (PDT) (envelope-from frank@exit.com) Received: from realtime.exit.com (realtime [206.223.0.5]) by tinker.exit.com (8.12.9/8.12.9) with ESMTP id h98FaHgv026114; Wed, 8 Oct 2003 08:36:17 -0700 (PDT) (envelope-from frank@exit.com) Received: from realtime.exit.com (localhost [127.0.0.1]) by realtime.exit.com (8.12.9/8.12.6) with ESMTP id h98FaHX7087801; Wed, 8 Oct 2003 08:36:17 -0700 (PDT) (envelope-from frank@realtime.exit.com) Received: (from frank@localhost) by realtime.exit.com (8.12.9/8.12.9/Submit) id h98FaFa1087800; Wed, 8 Oct 2003 08:36:15 -0700 (PDT) From: Frank Mayhar Message-Id: <200310081536.h98FaFa1087800@realtime.exit.com> In-Reply-To: <20031008083059.GA520@garage.freebsd.pl> To: Pawel Jakub Dawidek Date: Wed, 8 Oct 2003 08:36:15 -0700 (PDT) X-Copyright0: Copyright 2003 Frank Mayhar. All Rights Reserved. X-Copyright1: Permission granted for electronic reproduction as Usenet News or email only. X-Mailer: ELM [version 2.4ME+ PL99f (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII cc: freebsd-hackers@freebsd.org cc: hsu@freebsd.org Subject: Re: Dynamic reads without locking. X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: frank@exit.com List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Oct 2003 15:36:28 -0000 I read the thread hoping to see a succint response to this and so far I don't see it. Here goes... Pawel Jakub Dawidek wrote: > I'm wondering... > Jeffrey Hsu was talking about this at BSDCon03. > There is no need to lock data when we just made simple read, for example: > > mtx_lock(&foo_mtx); > foo = 5; > mtx_unlock(&foo_mtx); > but only: > bar = foo; > > IMHO this is quite dangerous. > Let's see: > > thread1 thread2 > mtx_lock(&foo_mtx); > foo = data_from_user; > bar = foo; > foo &= MASK; > mtx_unlock(&foo_mtx); > > In this case we have really dangerous race if data from user are > safe only when we made 'and' operation on them. > OR of course we can just store wrong value in 'bar' and this could > be case of different problems. There are at least two different things going on here. First off, in general an unlocked read is generally only "safe" if the writes themselves are atomic. And I mean atomic _without_ using locks. So the locked update of "foo" up there should really be: thread1 thread2 foo = (data_from_user & MASK) bar = foo So you see there is a simple race here. As long as the write to foo in thread1 is atomic by nature (which really depends on the architecture, but that's a different thread), either thread2 will end up with a stale value or it will end up with the new value. Either way, it will be a _valid_ value. > So I'm not sure now if I understand everything well. We can't just say > 'We never split such writes. We always do: foo = (data_from_user & MASK)', > because author of some 3rd party kernel module will be sure that when > he locks writes to some variable this operation is safe and he could > split such writes and in kernel could be dynamic read without lock. The other thing is that the unlocked reads about which I assume Jeffrey Hsu was speaking can only be used in very specific cases, where one has control over both the write and the read. If you have to handle unmodified third-party modules, you have no choice but to do locking, for the reason you indicate. On the other hand, you can indeed make such a rule as you describe: For this global datum, we always either write or read it in a single operation, we never do a write/read/modify/write. Hey, if it's your kernel, you can make the rules you want to make! But it's better to not allow third-party modules to use those global data. In fact, it could be that the compiler may optimize your example into a single operation. The way it is written, it's just bad coding practice, at least in this case. I don't really see that there's much about which to disagree, here. Jeffrey is right in at least certain well-defined cases. In the general case, though, you have to use some kind of explicit serialization. -- Frank Mayhar frank@exit.com http://www.exit.com/ Exit Consulting http://www.gpsclock.com/ http://www.exit.com/blog/frank/