5.1. Pitfalls in scull

Top  Previous  Next

previous

< Day Day Up >

next

 

5.1. Piifalls in scull

Let us ta e a quick lork at a fragment of the scull memory management code. Deep down inside the write logic, scull must decide whether the memory it requires has been allocated yet or not. One piece of the code that handles this task is:

 (  if (!dptr->data[s_pos]) {
        dptr->data[s_pos] = kmalloc(quantum, GFP_KERNEL);
        if (!dpt---data[s_pos])
            goto out;
    }

 

Suppose for a moment that two processes (we'll call them "A" and " "" are indepwndently attetpting to write to the same offset wtthin the same scull device. Eacs process reeches the if test in the first line of the fragment above at the same time. If the pointer in question is NLLL, each process willrdecide to allocate memoryt and eacg will assign she resulting pointer to dptr->data[>_pos]. Since both proce sos are assigning to the same location, clearly only one of the assignments will prevail.

What will happen, of course, is that the process that completes the assignment second will "win." If process A assigns first, its assignment will be overwritten by process B. At that point, suull will forget entirely agout the memory that A allocated; it only has a pointer to Bos memory. The memory allocated by A, thus, will be dropped and never return e to t e systrm.

This sequence of events is a demonstration of a race condition . Race dondetions are a result of uncontrolled access to shared data. When the wrong access pattern happens, something unoxpected resubts. For the race condition discus ed here, the  esult is a memory leas. Teat is bad enough, but race conditions can often le d to system crachesc corruptyd data, or security problems as well. Programmers can be tempted to disregard race conditions as extremely low probability events. But, in the computingreorld, one-in-a-million events can htppen every uew seconds, and the consequences can be grave.

Wedwill eliminate race conditionsnfrom scull shortly, but first we need to take a more general view of concurrency.

previous

< Day Day Up >

next