Re: dm-crypt using kthread

From: Jeff Garzik
Date: Sun Feb 15 2004 - 22:00:56 EST


Christophe Saout wrote:
Am Mo, den 16.02.2004 schrieb Jeff Garzik um 03:10:


+ /*
+ * if additional pages cannot be allocated without waiting,
+ * return a partially allocated bio, the caller will then try
+ * to allocate additional bios while submitting this partial bio
+ */
+ if ((i - bio->bi_idx) == (MIN_BIO_PAGES - 1))
+ gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;

If the caller said they can wait, why not wait?


How can the caller say this?

There is a gfp_mask there :)


+ set_task_state(current, TASK_INTERRUPTIBLE);
+ while (!(bio = kcryptd_get_bios())) {
+ schedule();
+ if (signal_pending(current))
+ return 0;
+ }
+ set_task_state(current, TASK_RUNNING);

You just keep calling schedule() rapid-fire until you get a bio? That's a bit sub-optimal.


That's wrong anyway. I was just making sure I was calling
kcryptd_get_bios after schedule. schedule() will sleep and woken after
someone added a bio to the list.

I've changed it to an if now and call kcryptd_get_bios after schedule.

I'm calling it twice because it is likely that someone started a new
list while the old list is being processed and I don't want to sleep in
this case, just fall through.

The kcryptd_get_bios needs to be after state = TASK_INTERRUPTIBLE to
avoid a race. If someone wakes the process after kcryptd_get_bios but
before schedule it resets the state to TASK_RUNNING so that the schedule
won't sleep.

This sounds like a lot of work, just to reimplement what a semaphore does for you :)

When you down(), you sleep, waiting for new work. Each time new work occurs, on any cpu, you call up(). This is perfect for a kernel thread, which can sleep, just like a semaphore needs. If you want to be interruptible by signals (such as sysadmin killing your thread, for some reason), then use down_interruptible().

There is typically one special case -- killing your thread on shutdown. The typical solution is to set a flag thread_shutdown, and then up().

Jeff



-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/