Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

There are or are not shared resources in this device driver example

6 views
Skip to first unread message

fl

unread,
May 13, 2013, 6:49:10 AM5/13/13
to
Hi,

I read Linux Device Drivers, 3rd edition. I do not understand an example on locking mechanism in Chapter 5. On page 107, it first gave the memory allocation to show the potential racing:

////////////////////////////
Let us take a quick look 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 (!dptr->data[s_pos])
goto out;
}
.......................

On page 112, it implemented separated semaphore for each virtual device driver.


//////////////////////////
The semaphore mechanism gives scull a tool that can be used to avoid race conditions while accessing the scull_dev data structure. But it is up to us to use that tool correctly. The keys to proper use of locking primitives are to specify exactly which resources are to be protected and to make sure that every access to those resources uses the proper locking. In our example driver, everything of interest is contained within the scull_dev structure, so that is the logical scope for our locking regime.

Let’s look again at that structure:

struct scull_dev {
struct scull_qset *data; /* Pointer to first quantum set */
int quantum; /* the current quantum size */
int qset; /* the current array size */
unsigned long size; /* amount of data stored here */
unsigned int access_key; /* used by sculluid and scullpriv */
struct semaphore sem; /* mutual exclusion semaphore */
struct cdev cdev; /* Char device structure */
};

Toward the bottom of the structure is a member called sem which is, of course, our semaphore. We have chosen to use a separate semaphore for each virtual scull
device. It would have been equally correct to use a single, global semaphore. The various scull devices share no resources in common, however, and there is no reason to make one process wait while another process is working with a different scull device. Using a separate semaphore for each device allows operations on different devices to proceed in parallel and, therefore, improves performance.
...........................

I am puzzled about the separate semaphore. I think semaphore is useful only for shared resources. Only different device drivers share one semaphore can protect the underlying resources. In the above example, where is the racing for the memory allocation operation concurrently by more device drivers?


Thanks in advance



fl

unread,
May 13, 2013, 7:04:55 AM5/13/13
to
I add the following paragraphs, which are below the first snippet (beginning with 'if'):
///////////////
Suppose for a moment that two processes (we’ll call them “A” and “B”) are independently attempting to write to the same offset within the same scull device. Each process reaches the if test in the first line of the fragment above at the same time. If the pointer in question is NULL, each process will decide to allocate memory, and each will assign the resulting pointer to
dptr->data[s_pos]. Since both processes 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, scull will forget entirely about the memory that A allocated; it only has a pointer to B’s memory. The memory allocated by A, thus, will be dropped and never returned to the system.
......................

fl

unread,
May 13, 2013, 8:09:45 AM5/13/13
to
On Monday, May 13, 2013 6:49:10 AM UTC-4, fl wrote:
Hi,

My questions are mainly at the last paragraph. Please correct me or explain it to me if you could.
How can "Using a separate semaphore for each device " to protect shared resources? Specifically, it first gave an example on memory allocation function kmalloc(). I do not understand whether there is connection between these device drivers if they have separate semaphores. Even though the author may mean to use semaphore in the kmalloc(), I don't understand how to implement the separate semaphores.

I may have misunderstand on device and process. The chapter mentions:

'The various scull devices', 'process A', 'process B' etc. Could you help me on last paragraph?

Thanks.


//////////////

Lew Pitcher

unread,
May 14, 2013, 10:12:52 PM5/14/13
to
Gee, you've answered your own question.

You say that you are "puzzled about the separate semaphore", and that
you "think semaphore is useful only for shared resources".

Two separate, simultaneous invocations of a single device driver will have a
lot of "shared resources" that should not be confused between invocations.

In your example, each invocation of driver kmalloc()s some memory, and, with
the semaphore to protect it, will kfree() the memory that it alone
kmalloc()ed.

Without the semaphore, each invocation would still kmalloc() memory, but
(because of race conditions on the memory holding the pointer to this
memory), would only free one block (multiple times). The other blocks would
be lost to the kernel memory, kmalloc()ed but never used or kfree()ed.

HTH
--
Lew Pitcher
"In Skills, We Trust"
0 new messages