Friday, August 9, 2013

Thoughts on why that '(un)cacheable' bit matters...

If you have had the chance of configuring page-tables of the kernel by hand, then you would probably be aware that there is a seemingly unimportant 'cacheable' bit associated with each Page-Table entry (PTE). 99 out of 100 times, you would just tend to ignore this fiddly detail of the PTE and use the default bit for it ie. cacheable. As the name suggests, this allows the Memory controller, the liberty to cache that particular virtual page (maps to an actual physical page) in L1/L2 cache. This improves performance by obviating memory access. However, following are at least two scenarios where it's worth spending some time to think about the 'cacheable' bit:


  1. Sharing Page/Memory in a multi-kernel system:
    Imagine a multiprocessor system, in which each core is managed by a different kernel image. Note that this is in contrast how most commercial systems like Linux work these days, which involve a single kernel image running on all the cores. Anyways, Barrelfish is one such kernel that boots up each core with a separate kernel image loaded in RAM. Now, let's suppose that kernel running on Bootstrap processor(BSP) wants to share some information with another application processor (AP). Simplest way to do this is by mapping a particular physical page in the virtual address space of both the cores/kernels. When either of the kernels access the corresponding virtual address in their own virtual address space,  it maps to the shared physical page, and can therefore act as a shared memory between the cores.
    However, the big catch here is that the PTE for this particular virtual address (that maps to the shared physical page) must be marked as "non-cacheable". The point is that any writes by any of the cores must go through to the actual physical memory. Application processor may remain oblivious to any writes by the BSP, if those writes only occur in the cache. Remember that both the kernels are running in a different virtual address space, so cache coherency protocol won't update** the cache of the Application processor.  By marking the PTE for the virtual page as non-cacheable, it is ensured* that writes will actually occur in the physical memory, which can then be read by the other core.

  2. Memory-mapped IO: This is similar to the previous example in terms of the basic idea. That writes must pass through to the actual physical memory. A lot of IO devices have their registers mapped somewhere in the physical memory map of the chipset (Ex: The 'hole' at 640K - 1 MB in the PC, that maps to BIOS in ROM, or may be IO devices). Consider a UART device, whose Transmit FIFO is mapped at Virtual address 0xFFFFE000H. If the PTE for this particular page is not marked uncacheable, then no writes may occur to the IO device, and you may see no output on your terminal connected to the RS-232 port.


    * (Long Footnote): It is generally a good idea to mark the pointers holding the address of the shared page between two cores or the address of the IO device register with the volatile keyword. This is because the access to these addresses occurs outside the current C program (by some another kernel running on a different core OR the IO controller accessing device registers). The compiler has no way of knowing about such extraneous access, and may act all wise and entirely remove the write in the name of optimization. Nobody wants that. So, either just use the volatile keyword to avoid such compiler optimizations or take the drastic step of turning off compiler optimization (using -O flag) entirely.

Saturday, May 18, 2013

Expression Templates: A Performance Analysis

Here is a link(PPT Link PDF Link) to a short presentation given by me recently on Expression Templates. The presentation has been adapted from the paper: "Expression Templates Revisited: A performance analysis of current methodologies" by K.Iglberger et. al.


Saturday, May 4, 2013

How to make your Linux System unbootable (and remedy it)...

This happened almost a week back, when I turned on my laptop and was greeted with a black screen instead of the usual tri-colour desktop. Scared because I had already invested too much time in preparing the machine and just the thought of having to do all that again gave me jitters. Fortunately, this time I was able to figure out the problem without having to spend too much time.

In a nutshell, if you want to make your system unbootable (or just want to scare off someone ;)

  1. Select a partition (or create a new one)
  2. Mark it such that it is mounted at boot-time
  3. Assign some random options to be used with the mount command at boot-time 
That's it! The interesting bit that makes the system unbootable is that instead of proceeding with the normal boot-up process in case a partition can't be mounted, the system just gives up. 

In my case, I had just created a partition using Applications->System Tools->Preferences->Disks utility (Ubuntu 12.10) to be auto-mounted at boot-time at /opt. However, as it turned out, the default options that 'Disks' utility sets for a partition do _not_ work. Following is a screenshot showing the default options that 'Disks' uses for a partition:


As can be seen in the section 'Mount Options', Disks utility uses the options: 

nosuid,nodev,nofail,x-gvfs-show 

for the partition. The major culprit here is the 'x-gvfs-show'  option which as of now gives an error when used with the mount command. Here is the relevant bug-report.

What really this 'Disks' utility(?) does is that it creates an entry in the /etc/fstab file for the partition in question. Following is an example entry from my /etc/fstab:

# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda9 during installation
UUID=5b13003d-2fa9-4080-956a-0e990542dd39 / ext4 errors=remount-ro 0 1
# /home was on /dev/sda1 during installation
UUID=9ec2dc33-245d-4590-a68e-0cc51e6faef4 /home ext4 defaults 0 2
/dev/disk/by-uuid/6C5F831B0F4B8077 /mnt/6C5F831B0F4B8077 auto nosuid,nodev,nofail,x-gvfs-show 0 0


The first column identifies the partition, second column gives the mount point and third column defines the options to be used together with the mount.  The third entry corresponds to the partition shown in the screenshot above. As can be seen, the option x-gvfs-show is supposed to be used which prevents the partition from being successfully mounted. To remedy this problem, we just need to change the mount options to 'defaults', as shown for the other 2 ext4 partitions in the above /etc/fstab file. And the system will start booting up like a sunflower on a pleasant Zurich afternoon.  

Another point worth noting here is that unless explicitly specified, a partition is mounted at boot-time. To ensure that the partition is not automatically mounted at boot-time, we need to specify the 'noauto' option in the corresponding /etc/fstab entry. Just imagine setting this option for your root partition!! 



Sunday, March 31, 2013

Why Future can't be predicted OR Why Crystal-balls won't work?

Consider a machine M with vast computational power/storage that could take as input the current state of the universe (at time t) and predict the state of universe at time (t + dt). The point is that even if we had enough resources to maintain and manipulate the state of the universe (just imagine representing that), we would still always fall short of creating a correct future predictor. This can be proved through the following program which outputs a number '0' or '1' depending on machine M's prediction.

Program: Counter-Crystal Ball
Input: machine M's prediction P = (0 or 1)  at time t about this program's output at time t + dt.
Output: ~P at time (t + dt). ~P denotes negation of P, where P is machine M's prediction at time t. 

Thus, the output of the Program Counter-Crystal Ball at time t + dt is always opposite of the prediction P that machine M made at time t about Program's behavior at time t + dt. Hence, the machine M is always wrong about the behavior of the Program Counter-Crystal Ball, and such an "always-correct" machine is theoretically impossible. 

However, that being said, it is plausible that if objects in the universe (including the Program: Counter-Crystal Ball) were forbidden from interacting with the Crystal Ball or machine M, M might still predict the future events correctly. After all, it was Counter-Crystal Ball's knowledge of M's output that allowed it to render M wrong. However, if no object is allowed to interact with such a crystal ball (including humans), then why would anyone even bother to make one! In short, any "tell-all" crystal-ball making plans are just not worth it. Even if somehow made one, we won't be able to use it if it is to make correct predictions. 

P.S: This line of reasoning is similar to Fred Cohen's reasoning as to how there can never be a perfect virus scanner. Still, it's good to know that crystal-balls are just a thing of fantasy.