Skip to content

Commit

Permalink
use mutex instead of semaphore in TPM driver
Browse files Browse the repository at this point in the history
The TPM driver uses two semaphores as mutexes.  Use the mutex API instead of
the (binary) semaphores.

Signed-off-by: Matthias Kaehlcke <matthias.kaehlcke@gmail.com>
Cc: Kylene Hall <kjhall@us.ibm.com>
Cc: Marcel Selhorst <tpm@selhorst.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Matthias Kaehlcke authored and Linus Torvalds committed May 8, 2007
1 parent 69f545e commit d081d47
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
24 changes: 13 additions & 11 deletions drivers/char/tpm/tpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
*/

#include <linux/poll.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>

#include "tpm.h"

enum tpm_const {
Expand Down Expand Up @@ -328,10 +330,10 @@ static void timeout_work(struct work_struct *work)
{
struct tpm_chip *chip = container_of(work, struct tpm_chip, work);

down(&chip->buffer_mutex);
mutex_lock(&chip->buffer_mutex);
atomic_set(&chip->data_pending, 0);
memset(chip->data_buffer, 0, TPM_BUFSIZE);
up(&chip->buffer_mutex);
mutex_unlock(&chip->buffer_mutex);
}

/*
Expand Down Expand Up @@ -380,7 +382,7 @@ static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
return -E2BIG;
}

down(&chip->tpm_mutex);
mutex_lock(&chip->tpm_mutex);

if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
dev_err(chip->dev,
Expand Down Expand Up @@ -419,7 +421,7 @@ static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
dev_err(chip->dev,
"tpm_transmit: tpm_recv: error %zd\n", rc);
out:
up(&chip->tpm_mutex);
mutex_unlock(&chip->tpm_mutex);
return rc;
}

Expand Down Expand Up @@ -966,22 +968,22 @@ ssize_t tpm_write(struct file *file, const char __user *buf,
while (atomic_read(&chip->data_pending) != 0)
msleep(TPM_TIMEOUT);

down(&chip->buffer_mutex);
mutex_lock(&chip->buffer_mutex);

if (in_size > TPM_BUFSIZE)
in_size = TPM_BUFSIZE;

if (copy_from_user
(chip->data_buffer, (void __user *) buf, in_size)) {
up(&chip->buffer_mutex);
mutex_unlock(&chip->buffer_mutex);
return -EFAULT;
}

/* atomic tpm command send and result receive */
out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);

atomic_set(&chip->data_pending, out_size);
up(&chip->buffer_mutex);
mutex_unlock(&chip->buffer_mutex);

/* Set a timeout by which the reader must come claim the result */
mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
Expand All @@ -1004,10 +1006,10 @@ ssize_t tpm_read(struct file *file, char __user *buf,
if (size < ret_size)
ret_size = size;

down(&chip->buffer_mutex);
mutex_lock(&chip->buffer_mutex);
if (copy_to_user(buf, chip->data_buffer, ret_size))
ret_size = -EFAULT;
up(&chip->buffer_mutex);
mutex_unlock(&chip->buffer_mutex);
}

return ret_size;
Expand Down Expand Up @@ -1105,8 +1107,8 @@ struct tpm_chip *tpm_register_hardware(struct device *dev, const struct tpm_vend
return NULL;
}

init_MUTEX(&chip->buffer_mutex);
init_MUTEX(&chip->tpm_mutex);
mutex_init(&chip->buffer_mutex);
mutex_init(&chip->tpm_mutex);
INIT_LIST_HEAD(&chip->list);

INIT_WORK(&chip->work, timeout_work);
Expand Down
5 changes: 3 additions & 2 deletions drivers/char/tpm/tpm.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
Expand Down Expand Up @@ -94,11 +95,11 @@ struct tpm_chip {
/* Data passed to and from the tpm via the read/write calls */
u8 *data_buffer;
atomic_t data_pending;
struct semaphore buffer_mutex;
struct mutex buffer_mutex;

struct timer_list user_read_timer; /* user needs to claim result */
struct work_struct work;
struct semaphore tpm_mutex; /* tpm is processing */
struct mutex tpm_mutex; /* tpm is processing */

struct tpm_vendor_specific vendor;

Expand Down

0 comments on commit d081d47

Please sign in to comment.