Skip to contentSkip to conversation

WARNING: mutexes are preferred for single holder semaphores

DISCUSSION Sean MacLennan 8 participants 12 messages May 5, 2008 – May 8, 2008

lore
Share & tools
lore
#0 lore raw

This is a bit OT, but I got the warning in the subject from checkpatch.pl for a piece of code. The code *is* using a mutex. Does it actually mean I shouldn't use a mutex?

The code declares a global mutex:

	static DECLARE_MUTEX(list_lock);

The odds of two accesses to the list_lock at the same time are zero. But it would be Very Bad(tm) if it did happen. Since the odds of contention are near zero, the cost of the mutex is near zero, so I put it in.

I think I can safely ignore the warning, but I want to make sure....

Cheers,
Sean

#1 lore raw
Quoted 7 lines · Sean MacLennan

This is a bit OT, but I got the warning in the subject from checkpatch.pl for a piece of code. The code *is* using a mutex. Does it actually mean I shouldn't use a mutex?

The code declares a global mutex:

	static DECLARE_MUTEX(list_lock);

.. which is a semaphore :( [see include/linux/semaphore.h]

I think you want DEFINE_MUTEX().

Yes, this is completely ridiculous.

cheers

Signature · 8 lines
Michael Ellerman
OzLabs, IBM Australia Development Lab

wwweb: http://michael.ellerman.id.au
phone: +61 2 6212 1183 (tie line 70 21183)

We do not inherit the earth from our ancestors,
we borrow it from our children. - S.M.A.R.T Person
#2 lore raw
Quoted 9 lines · On Mon, 05 May 2008 11:06:55 +1000 "Michael Ellerman" <[email protected]>…
Quoted 7 lines · Sean MacLennan

This is a bit OT, but I got the warning in the subject from checkpatch.pl for a piece of code. The code *is* using a mutex. Does it actually mean I shouldn't use a mutex?

The code declares a global mutex:

	static DECLARE_MUTEX(list_lock);
Quoted 8 lines

.. which is a semaphore :( [see include/linux/semaphore.h]

I think you want DEFINE_MUTEX().

Yes, this is completely ridiculous.

cheers

Ok, that fixed it, once I changed all the up and down calls :p

Thanks.

Cheers,
Sean

#3 lore raw
In reply to Sean MacLennan
Quoted 3 lines · Sean MacLennan

This is a bit OT, but I got the warning in the subject from checkpatch.pl for a piece of code. The code *is* using a mutex. Does it actually mean I shouldn't use a mutex?

I don't require zero checkpatch warnings or errors on patches before I accept them. If what checkpatch is saying is rubbish, just ignore it.

Paul.

#4 lore raw
Quoted 8 lines · Paul Mackerras
Quoted 3 lines · Sean MacLennan

This is a bit OT, but I got the warning in the subject from checkpatch.pl for a piece of code. The code *is* using a mutex. Does it actually mean I shouldn't use a mutex?

I don't require zero checkpatch warnings or errors on patches before I accept them. If what checkpatch is saying is rubbish, just ignore it.

Current versions don't spew much rubbish anymore, and this one certainly isn't.

#5 lore raw
In reply to Sean MacLennan
Quoted 6 lines · Sean MacLennan

This is a bit OT, but I got the warning in the subject from checkpatch.pl for a piece of code. The code *is* using a mutex. Does it actually mean I shouldn't use a mutex?

The code declares a global mutex:

Quoted 8 lines
	static DECLARE_MUTEX(list_lock);

The odds of two accesses to the list_lock at the same time are zero. But it would be Very Bad(tm) if it did happen. Since the odds of contention are near zero, the cost of the mutex is near zero, so I put it in.

I think I can safely ignore the warning, but I want to make sure....

Show us the code... It could be a bug in checkpatch or you using the wrong functions somewhere ...

Ben.

#6 lore raw
Quoted 2 lines · On Mon, 05 May 2008 13:38:39 +1000 "Benjamin Herrenschmidt"…

Show us the code... It could be a bug in checkpatch or you using the wrong functions somewhere ...

The change to DEFINE_MUTEX and changing up/down to mutex_lock/mutex_unlock solved the problem.

The code is GPLed but not currently available on the net. It is basically a little driver that registers a character device and then passes out the minor numbers to the PIKA board drivers.

It was written to isolate all the character/sysfs code to one place since we have five drivers, and many debug drivers, that use it.

If anybody is really interested, I can post it here. I doubt it will ever be submitted to the mainline kernel however.

Cheers,
Sean

#7 lore raw

All your drivers are belong to us [was WARNING: mutexes are preferred for single holder semaphores]

Hi Sean,

Quoted 9 lines

The code is GPLed but not currently available on the net. It is basically a little driver that registers a character device and then passes out the minor numbers to the PIKA board drivers.

It was written to isolate all the character/sysfs code to one place since we have five drivers, and many debug drivers, that use it.

If anybody is really interested, I can post it here. I doubt it will ever be submitted to the mainline kernel however.

Just for the record and because it may be interesting to others, during the Hannover Messe end of April, in the context of the OSADL Congress[1] I attended a speech of Greg KH about driver development and citing from memory he said:

"I am serious, please post all drivers that there are, even if you think they are useless for other people. We have a driver in mainline for a device that I know only exists once in the whole world.

Having the drivers in the kernel gives the developers the chance to see how the infrastructure is being used and to isolate good opportunities for modularization benefiting the linux kernel.

[...]

It also happened that a driver once posted for what the customer thought was a completely specific device of his own today supports lots of different boards from at least four different manufacturers."

The wording is of course not exact but I hope I caught the spirit of what Greg wanted to say. So yes, please post the driver - maybe Greg KH will tunnel it into mainline...

Cheers
Detlev

[1] http://www.osadl.org/International-Congress-Open-Source-mee.hannover-2008-congress.0.html

PS: The OSADL pages should be updated in a few days to link to slides of
the talks.

Signature · 8 lines
Ftpd never switches uid and euid, it uses setfsuid(2) instead. The
main reason is that uid switching has been exploited in several
breakins, but the sheer ugliness of uid switching counts too.
                                     -- pure-ftpd(8)
--
DENX Software Engineering GmbH,      MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich,  Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-40 Fax: (+49)-8142-66989-80 Email: [email protected]
#8 lore raw

Re: All your drivers are belong to us [was WARNING: mutexes are preferredfor single holder semaphores]

Quoted 7 lines · On Wed, 07 May 2008 16:36:30 +0200 "Detlev Zundel" <[email protected]> wrote:

It also happened that a driver once posted for what the customer thought was a completely specific device of his own today supports lots of different boards from at least four different manufacturers."

The wording is of course not exact but I hope I caught the spirit of what Greg wanted to say. So yes, please post the driver - maybe Greg KH will tunnel it into mainline...

I agree with the sentiment. However, it is hard enough to get legitimate drivers into the kernel. The drivers I mentioned do not come close to following the Linux kernel coding spec.

Just to start, they use 2 character spaces for indentation :( They are full of ifdefs since we need to support the whole gamut of 2.6 kernels plus windows. We also must support multiple board versions in the same files.

Cheers,
Sean

#9 lore raw

Re: All your drivers are belong to us [was WARNING: mutexes are preferredfor single holder semaphores]

Quoted 7 lines · On Wed, 7 May 2008 17:42:51 -0400 Sean MacLennan <[email protected]>…
Quoted 4 lines · On Wed, 07 May 2008 16:36:30 +0200 "Detlev Zundel" <[email protected]> wrote:

It also happened that a driver once posted for what the customer thought was a completely specific device of his own today supports lots of different boards from at least four different manufacturers."

Quoted 7 lines
Quoted 3 lines

The wording is of course not exact but I hope I caught the spirit of what Greg wanted to say. So yes, please post the driver - maybe Greg KH will tunnel it into mainline...

I agree with the sentiment. However, it is hard enough to get legitimate drivers into the kernel. The drivers I mentioned do not come close to following the Linux kernel coding spec.

Here's the cool part. There's this Linux driver project:

http://www.linuxdriverproject.org/twiki/bin/view

where they can take drivers like that and clean them up. At least to a reasonable degree. The benefit for you is that once it's cleaned up, it can go into the mainline kernel and it will just be there in future releases. Helpful when you update.

Now, I can't say I've personally ever used that project but its mission does seem pretty targeted towards groups that want to do the right thing with respect to drivers. Maybe you could give it a shot and tell others about the experience.

josh

#10 lore raw

Re: All your drivers are belong to us [was WARNING: mutexes are preferredfor single holder semaphores]

I have attached the driver in question below. This is *not* representative of PIKA board drivers in general. This driver was rewritten from scratch to work in 2.6.26, but still work on the 2.6.9 kernel in RedHatES 4.

Since it was rewritten, it matches the kernel coding specs rather than PIKA's. Emacs users will recognize the comment at the bottom ;)

This is also a generally useful driver that does one specific thing. It is also very small. The next smallest PIKA driver is close to 9,000 lines of code. Our largest is about 130,000 lines of code and lsmod reports 1.6M in size.

The other board drivers are *very* specific to PIKA hardware, and are not "chip" drivers as such. I suspect Greg was mainly talking about chip drivers.

I use this driver all the time to write quick drivers. It handles all the dirty work for you so you can quickly prototype character drivers.

And while the Linux driver project looks interesting, it doesn't really help me. We still have to support all our customers, which includes windows, and old Linux kernels. This means I would still have to maintain the PIKA version of the driver separate from the driver projects version.

Cheers,
Sean

pikacore.h new file +39 -0
@@ -0,0 +1,39 @@
+/*
+ * PIKA Core Driver
+ *
+ * Copyright (c) 2008 PIKA Technologies
+ *   Sean MacLennan <[email protected]>
+ */
+
+#ifndef PIKACORE_H
+#define PIKACORE_H
+
+#include <linux/module.h>
+#include <linux/fs.h>
+#include <linux/device.h>
+
+
+/* pikacore_register* returns a negative number on error.
+ * On success, it returns the minor number.
+ */
+int pikacore_register_board(struct file_operations *fops);
+int pikacore_register(int minor, char *name, struct file_operations *fops);
+int pikacore_unregister(int minor);
+int pikacore_device_attr(int minor, struct device_attribute *attr, void *data);
+
+
+/* Some compatibility changes. */
+
+#ifndef DEFINE_MUTEX
+#define DEFINE_MUTEX DECLARE_MUTEX
+#define mutex_lock down
+#define mutex_unlock up
+#endif
+
+#include <linux/interrupt.h>
+#ifndef IRQF_SHARED
+#define IRQF_SHARED (SA_SHIRQ | SA_INTERRUPT)
+#define IRQF_DISABLED 0
+#endif
+
+#endif
pikacore.c new file +261 -0

261 lines not on this page · raw message

#11 lore raw

Re: All your drivers are belong to us

In reply to Josh Boyer

Hi,

Quoted 18 lines
Quoted 7 lines · On Wed, 7 May 2008 17:42:51 -0400 Sean MacLennan <[email protected]>…
Quoted 4 lines · On Wed, 07 May 2008 16:36:30 +0200 "Detlev Zundel" <[email protected]> wrote:

It also happened that a driver once posted for what the customer thought was a completely specific device of his own today supports lots of different boards from at least four different manufacturers."

Quoted 7 lines
Quoted 3 lines

The wording is of course not exact but I hope I caught the spirit of what Greg wanted to say. So yes, please post the driver - maybe Greg KH will tunnel it into mainline...

I agree with the sentiment. However, it is hard enough to get legitimate drivers into the kernel. The drivers I mentioned do not come close to following the Linux kernel coding spec.

Quoted 8 lines

Here's the cool part. There's this Linux driver project:

http://www.linuxdriverproject.org/twiki/bin/view

where they can take drivers like that and clean them up. At least to a reasonable degree. The benefit for you is that once it's cleaned up, it can go into the mainline kernel and it will just be there in future releases. Helpful when you update.

Yes, I should have mentioned this also in my original mail. If I remember correctly, Greg mentioned that in this project, he has *300* developers waiting to do something. None of the companies previously claiming Linux had a driver problem showed up even when he contacted them directly....

Quoted 4 lines

Now, I can't say I've personally ever used that project but its mission does seem pretty targeted towards groups that want to do the right thing with respect to drivers. Maybe you could give it a shot and tell others about the experience.

This would definitely be interesting.

Cheers
Detlev

Signature · 6 lines
-- Question authority!
-- Yeah, says who?
--
DENX Software Engineering GmbH,      MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich,  Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-40 Fax: (+49)-8142-66989-80 Email: [email protected]