Navigation

Linux kernel 2.6.39 removed the final part of the BKL, the whole BKL locking mechanism. BKL is now finally totally gone! Hooray!

The big kernel lock (BKL) is an old serialization method that we are trying to get rid of, replacing it with more fine-grained locking, in particular mutex, spinlock and RCU, where appropriate.

The BKL is a recursive lock, meaning that you can take it from a thread that already holds it. This may sound convenient, but easily introduces all sorts of bugs. Another problem is that the BKL is automatically released when a thread sleeps. This avoids lock order problems with mutexes in some circumstances, but also creates more problems because it makes it really hard to track what code is executed under the lock.

Many changes have been made and gone upstream, but some areas remain that we need to take care of independently:

llseek

The problem is in the llseek callback of the struct file_operations. Drivers and filesystems implement it to move the file pointer.

The problem arises when it's not implemented by a driver or a filesystem. In this case, the VFS layer calls a default one called default_llseek() that just changes the file pointer and does nothing else. But to protect against concurrent calls to llseek on a same file, default_llseek() protects this file pointer change using the BKL.

The thing is rather evil because not only do we have a lot of existing drivers that don't implement llseek, but also every new driver/filesystem that gets merged and that don't implement llseek falls back to the default_llseek() implementation.

It means two things: It can't stop bleeding and it does more and more. And we can't remove the BKL (or at least making it modular) until we get rid of default_llseek() (or at least making it modular ).

So the strategy is to give a sane llseek implementation to these drivers, once there is no llseek stub, we can start punching default_llseek() (ie: making it modular; build it only if drivers depend on the BKL, or may be an even more granular dependency).

The sane existing implementations are the following:

generic_file_llseek(), does the same thing than default_llseek(): move the file pointer accordingly to the offset given by the user. The difference is that it protects the operation using the inode mutex instead of the BKL. If you see that the driver/filesystem uses file->f_pos, or the offset parameter in one of its file operations callbacks, then choose this. Because if the driver uses the file pointer for its work, it will expect that llseek with behave like before with default_llseek(). It's just that the protection will change.

...and about this protection

default_llseek(), if you see that the driver uses the file offset as described above, plus it uses the BKL somewhere. Well that requires a bit of review. Try to see if the offset is ever read or written under the BKL, if it clearly doesn't then you can pick generic_file_llseek() but take care about describing why it's safe in your changelog. Actually take care of describing why it's safe in your changelog whatever callback you choose. Often, a simple "this driver doesn't use the BKL" suffice.

noop_llseek(), if you see the driver never use the file pointer, then choose this. This callback won't update the file pointer, it won't do anything in fact, which is exactly what we want if we don't use the file pointer. Note if you use this, you need to base your work on the -mmotm tree (in fact for all that, I would suggest you to base your work on linux-next: http://git.kernel.org/?p=linux/kernel/git/sfr/linux-next.git;a=summary).

no_llseek(), well it could be the good solution sometimes but preferably don't use it. It might make us lose our time. For the background: a driver that doesn't implement llseek is actually seekable because it falls back to default_llseek(). So a userspace program can seek on its files, and there may be some that do it, even if that has no effect for the driver. In this case it is tempting to use no_llseek(): it tells the file is non-seekable and any userspace program that try to seek on such file will get a -ENOTTY error. So these userspace program that seek even if it is useless may be broken because of that. Most maintainers refuse such change, this is why it's better to use noop_llseek() as it does nothing but doesn't break things either.

TTY layer

Probably the most central part of the BKL removal is TTY layer, which used to heavily rely on the BKL. Alan Cox has been working for years on introducing sane locking to the TTY code that will eventually let us remove the BKL, but this may still take some time to get there.

Also, a patch series from Arnd Bergmann was merged to take a shortcut, by separating the BKL usage in the TTY layer from the usage outside of it.

This series introduces a new Big TTY Mutex that takes the place of the BKL, but follows the normal rules for mutexes:

no autorelease on sleep, which is what most of the series is about.

no recursive locking

need to follow strict lock order to avoid ABBA deadlocks

limited to one subsystem only.

The first patches convert all the code using the BKL in the TTY layer and related drivers to the new interface, while the final patch adds the real mutex implementation. The new locking rules are shown in this picture:

This new lock should get removed in the future as well, but it does not lock against the BKL any more.

Block layer

A patch was merged to push down the BKL from the common block layer into the individual open/release/ioctl functions of all block device driver. It now needs to be removed from those. The blktrace code also still uses the BKL.

File locking (fs/locks.c)

One of the oldest patches to remove the BKL, which still has not been merged is for the file locking. The patch itself should be fairly stable at this point, but there is still interaction with how the BKL is used in the NFS file system, in particular lockd, which runs for its entire life time with the BKL held.

The hard part here is to find out what data structures in NFS actually need to be protected by lock_flock instead of lock_kernel.

Video4Linux

The BKL gets pushed into V4L device drivers by an existing patch, but most of the individual drivers now depend on the BKL. In general, this should be easy to replace with a private mutex per driver or to remove, but there are a lot of them.

Remaining drivers

When all the above changes have been done the base kernel no longer needs the BKL, but there are still a number of modules that need it. We need to mark these as 'depends on BKL' in Kconfig or remove the BKL from them, one at a time. Most of them are simple enough that they can be automatically converted to a private mutex using a script from Arnd.