public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Re: use mutex instead of semaphore in RocketPort driver
       [not found] <200705081903.l48J3lr1012622@hera.kernel.org>
@ 2007-05-22 16:59 ` Arjan van de Ven
  2007-05-22 20:06   ` Matthias Kaehlcke
  0 siblings, 1 reply; 4+ messages in thread
From: Arjan van de Ven @ 2007-05-22 16:59 UTC (permalink / raw)
  To: Linux Kernel Mailing List; +Cc: matthias.kaehlcke

>  
> -	down_interruptible(&info->write_sem);
> +	mutex_lock_interruptible(&info->write_mtx);
>  
>  #ifdef ROCKET_DEBUG_WRITE
>  	printk(KERN_INFO "rp_write %d chars...", count);
> @@ -1773,7 +1776,7 @@ end:
>  		wake_up_interruptible(&tty->poll_wait);
>  #endif
>  	}
> -	up(&info->write_sem);
> +	mutex_unlock(&info->write_mtx);
>  	return retval

this code is very very buggy.

mutex_lock_interruptible() may not get the mutex in case a signal
happens... and yet you unlox the mutex unconditionally!!!



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: use mutex instead of semaphore in RocketPort driver
  2007-05-22 16:59 ` use mutex instead of semaphore in RocketPort driver Arjan van de Ven
@ 2007-05-22 20:06   ` Matthias Kaehlcke
  2007-05-22 21:04     ` Simon Arlott
  0 siblings, 1 reply; 4+ messages in thread
From: Matthias Kaehlcke @ 2007-05-22 20:06 UTC (permalink / raw)
  To: Arjan van de Ven; +Cc: Linux Kernel Mailing List

El Tue, May 22, 2007 at 09:59:01AM -0700 Arjan van de Ven ha dit:

> >  
> > -	down_interruptible(&info->write_sem);
> > +	mutex_lock_interruptible(&info->write_mtx);
> >  
> >  #ifdef ROCKET_DEBUG_WRITE
> >  	printk(KERN_INFO "rp_write %d chars...", count);
> > @@ -1773,7 +1776,7 @@ end:
> >  		wake_up_interruptible(&tty->poll_wait);
> >  #endif
> >  	}
> > -	up(&info->write_sem);
> > +	mutex_unlock(&info->write_mtx);
> >  	return retval
> 
> this code is very very buggy.

more buggy than with the use of a semaphore?
 
> mutex_lock_interruptible() may not get the mutex in case a signal
> happens... and yet you unlox the mutex unconditionally!!!

as far as i understand only the thread that locked the mutex can
unlock it (as opposed to semaphores, which can be released by any
thread/process). obviously this doesn't make the code be more
correct. what i don't know is how the kernel behaves when
trying to unlock a mutex the thread doesn't own. another and possibly
more important problem of the code is that in case of being
interrupted by a signal the data that should be protected by the
mutex/semaphore can be accessed/changed by two threads at the same
time.

would the following resolve the problem?

if(mutex_lock_interruptible(&info->write_mtx)) 
        return -ERESTARTSYS

thanks for your comments

-- 
Matthias Kaehlcke
Linux Application Developer
Barcelona

            Nothing is more despicable than respect based on fear
                              (Albert Camus)
                                                                 .''`.
    using free software / Debian GNU/Linux | http://debian.org  : :'  :
                                                                `. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4                  `-

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: use mutex instead of semaphore in RocketPort driver
  2007-05-22 20:06   ` Matthias Kaehlcke
@ 2007-05-22 21:04     ` Simon Arlott
  2007-05-22 21:23       ` Jiri Slaby
  0 siblings, 1 reply; 4+ messages in thread
From: Simon Arlott @ 2007-05-22 21:04 UTC (permalink / raw)
  To: Matthias Kaehlcke, Arjan van de Ven, Linux Kernel Mailing List

On 22/05/07 21:06, Matthias Kaehlcke wrote:
> El Tue, May 22, 2007 at 09:59:01AM -0700 Arjan van de Ven ha dit:
> 
>>>  

Please provide context when quoting a patch, git grep takes a while...

>>> -	down_interruptible(&info->write_sem);
>>> +	mutex_lock_interruptible(&info->write_mtx);
>>>  
>>>  #ifdef ROCKET_DEBUG_WRITE
>>>  	printk(KERN_INFO "rp_write %d chars...", count);
>>> @@ -1773,7 +1776,7 @@ end:
>>>  		wake_up_interruptible(&tty->poll_wait);
>>>  #endif
>>>  	}
>>> -	up(&info->write_sem);
>>> +	mutex_unlock(&info->write_mtx);
>>>  	return retval
>> this code is very very buggy.
> 
> more buggy than with the use of a semaphore?
>  
>> mutex_lock_interruptible() may not get the mutex in case a signal
>> happens... and yet you unlox the mutex unconditionally!!!
> 
> as far as i understand only the thread that locked the mutex can
> unlock it (as opposed to semaphores, which can be released by any
> thread/process). obviously this doesn't make the code be more
> correct. what i don't know is how the kernel behaves when
> trying to unlock a mutex the thread doesn't own. another and possibly
> more important problem of the code is that in case of being
> interrupted by a signal the data that should be protected by the
> mutex/semaphore can be accessed/changed by two threads at the same
> time.
> 
> would the following resolve the problem?
> 
> if(mutex_lock_interruptible(&info->write_mtx)) 
>         return -ERESTARTSYS
> 
> thanks for your comments
> 

No. At least one user of tty_operations/tty_driver's write function 
doesn't check the return value so it would never be retried, mutex_lock 
should be used instead.

All of the _interruptible and functions that return -ERESTARTSYS should 
probably use __must_check...

-- 
Simon Arlott

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: use mutex instead of semaphore in RocketPort driver
  2007-05-22 21:04     ` Simon Arlott
@ 2007-05-22 21:23       ` Jiri Slaby
  0 siblings, 0 replies; 4+ messages in thread
From: Jiri Slaby @ 2007-05-22 21:23 UTC (permalink / raw)
  To: Simon Arlott
  Cc: Matthias Kaehlcke, Arjan van de Ven, Linux Kernel Mailing List

Simon Arlott napsal(a):
> On 22/05/07 21:06, Matthias Kaehlcke wrote:
>> would the following resolve the problem?
>>
>> if(mutex_lock_interruptible(&info->write_mtx))         return
>> -ERESTARTSYS
>>
>> thanks for your comments
>>
> 
> No. At least one user of tty_operations/tty_driver's write function
> doesn't check the return value so it would never be retried, mutex_lock
> should be used instead.

Who? There are some drivers that returns ERESTARTSYS from write function.

regards,
-- 
http://www.fi.muni.cz/~xslaby/            Jiri Slaby
faculty of informatics, masaryk university, brno, cz
e-mail: jirislaby gmail com, gpg pubkey fingerprint:
 B674 9967 0407 CE62 ACC8  22A0 32CC 55C3 39D4 7A7E

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-05-22 21:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <200705081903.l48J3lr1012622@hera.kernel.org>
2007-05-22 16:59 ` use mutex instead of semaphore in RocketPort driver Arjan van de Ven
2007-05-22 20:06   ` Matthias Kaehlcke
2007-05-22 21:04     ` Simon Arlott
2007-05-22 21:23       ` Jiri Slaby

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox