Commit e3302e0d authored by Jason Gunthorpe's avatar Jason Gunthorpe Committed by Peter Huewe

tpm: Make tpm-dev allocate a per-file structure

This consolidates everything that is only used within tpm-dev.c
into tpm-dev.c and out of the publicly visible struct tpm_chip.

The per-file allocation lays the ground work for someday fixing the
strange forced O_EXCL behaviour of the current code.
Signed-off-by: default avatarJason Gunthorpe <jgunthorpe@obsidianresearch.com>
Reviewed-by: default avatarJoel Schopp <jschopp@linux.vnet.ibm.com>
Reviewed-by: default avatarAshley Lai <adlai@linux.vnet.ibm.com>
Signed-off-by: default avatarPeter Huewe <peterhuewe@gmx.de>
parent 5f82e9f0
...@@ -22,21 +22,34 @@ ...@@ -22,21 +22,34 @@
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include "tpm.h" #include "tpm.h"
struct file_priv {
struct tpm_chip *chip;
/* Data passed to and from the tpm via the read/write calls */
atomic_t data_pending;
struct mutex buffer_mutex;
struct timer_list user_read_timer; /* user needs to claim result */
struct work_struct work;
u8 data_buffer[TPM_BUFSIZE];
};
static void user_reader_timeout(unsigned long ptr) static void user_reader_timeout(unsigned long ptr)
{ {
struct tpm_chip *chip = (struct tpm_chip *) ptr; struct file_priv *priv = (struct file_priv *)ptr;
schedule_work(&chip->work); schedule_work(&priv->work);
} }
static void timeout_work(struct work_struct *work) static void timeout_work(struct work_struct *work)
{ {
struct tpm_chip *chip = container_of(work, struct tpm_chip, work); struct file_priv *priv = container_of(work, struct file_priv, work);
mutex_lock(&chip->buffer_mutex); mutex_lock(&priv->buffer_mutex);
atomic_set(&chip->data_pending, 0); atomic_set(&priv->data_pending, 0);
memset(chip->data_buffer, 0, TPM_BUFSIZE); memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
mutex_unlock(&chip->buffer_mutex); mutex_unlock(&priv->buffer_mutex);
} }
static int tpm_open(struct inode *inode, struct file *file) static int tpm_open(struct inode *inode, struct file *file)
...@@ -44,6 +57,7 @@ static int tpm_open(struct inode *inode, struct file *file) ...@@ -44,6 +57,7 @@ static int tpm_open(struct inode *inode, struct file *file)
struct miscdevice *misc = file->private_data; struct miscdevice *misc = file->private_data;
struct tpm_chip *chip = container_of(misc, struct tpm_chip, struct tpm_chip *chip = container_of(misc, struct tpm_chip,
vendor.miscdev); vendor.miscdev);
struct file_priv *priv;
/* It's assured that the chip will be opened just once, /* It's assured that the chip will be opened just once,
* by the check of is_open variable, which is protected * by the check of is_open variable, which is protected
...@@ -53,15 +67,20 @@ static int tpm_open(struct inode *inode, struct file *file) ...@@ -53,15 +67,20 @@ static int tpm_open(struct inode *inode, struct file *file)
return -EBUSY; return -EBUSY;
} }
chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL); priv = kzalloc(sizeof(*priv), GFP_KERNEL);
if (chip->data_buffer == NULL) { if (priv == NULL) {
clear_bit(0, &chip->is_open); clear_bit(0, &chip->is_open);
return -ENOMEM; return -ENOMEM;
} }
atomic_set(&chip->data_pending, 0); priv->chip = chip;
atomic_set(&priv->data_pending, 0);
mutex_init(&priv->buffer_mutex);
setup_timer(&priv->user_read_timer, user_reader_timeout,
(unsigned long)priv);
INIT_WORK(&priv->work, timeout_work);
file->private_data = chip; file->private_data = priv;
get_device(chip->dev); get_device(chip->dev);
return 0; return 0;
} }
...@@ -69,28 +88,28 @@ static int tpm_open(struct inode *inode, struct file *file) ...@@ -69,28 +88,28 @@ static int tpm_open(struct inode *inode, struct file *file)
static ssize_t tpm_read(struct file *file, char __user *buf, static ssize_t tpm_read(struct file *file, char __user *buf,
size_t size, loff_t *off) size_t size, loff_t *off)
{ {
struct tpm_chip *chip = file->private_data; struct file_priv *priv = file->private_data;
ssize_t ret_size; ssize_t ret_size;
int rc; int rc;
del_singleshot_timer_sync(&chip->user_read_timer); del_singleshot_timer_sync(&priv->user_read_timer);
flush_work(&chip->work); flush_work(&priv->work);
ret_size = atomic_read(&chip->data_pending); ret_size = atomic_read(&priv->data_pending);
if (ret_size > 0) { /* relay data */ if (ret_size > 0) { /* relay data */
ssize_t orig_ret_size = ret_size; ssize_t orig_ret_size = ret_size;
if (size < ret_size) if (size < ret_size)
ret_size = size; ret_size = size;
mutex_lock(&chip->buffer_mutex); mutex_lock(&priv->buffer_mutex);
rc = copy_to_user(buf, chip->data_buffer, ret_size); rc = copy_to_user(buf, priv->data_buffer, ret_size);
memset(chip->data_buffer, 0, orig_ret_size); memset(priv->data_buffer, 0, orig_ret_size);
if (rc) if (rc)
ret_size = -EFAULT; ret_size = -EFAULT;
mutex_unlock(&chip->buffer_mutex); mutex_unlock(&priv->buffer_mutex);
} }
atomic_set(&chip->data_pending, 0); atomic_set(&priv->data_pending, 0);
return ret_size; return ret_size;
} }
...@@ -98,7 +117,7 @@ static ssize_t tpm_read(struct file *file, char __user *buf, ...@@ -98,7 +117,7 @@ static ssize_t tpm_read(struct file *file, char __user *buf,
static ssize_t tpm_write(struct file *file, const char __user *buf, static ssize_t tpm_write(struct file *file, const char __user *buf,
size_t size, loff_t *off) size_t size, loff_t *off)
{ {
struct tpm_chip *chip = file->private_data; struct file_priv *priv = file->private_data;
size_t in_size = size; size_t in_size = size;
ssize_t out_size; ssize_t out_size;
...@@ -106,32 +125,33 @@ static ssize_t tpm_write(struct file *file, const char __user *buf, ...@@ -106,32 +125,33 @@ static ssize_t tpm_write(struct file *file, const char __user *buf,
either via tpm_read or a user_read_timer timeout. either via tpm_read or a user_read_timer timeout.
This also prevents splitted buffered writes from blocking here. This also prevents splitted buffered writes from blocking here.
*/ */
if (atomic_read(&chip->data_pending) != 0) if (atomic_read(&priv->data_pending) != 0)
return -EBUSY; return -EBUSY;
if (in_size > TPM_BUFSIZE) if (in_size > TPM_BUFSIZE)
return -E2BIG; return -E2BIG;
mutex_lock(&chip->buffer_mutex); mutex_lock(&priv->buffer_mutex);
if (copy_from_user if (copy_from_user
(chip->data_buffer, (void __user *) buf, in_size)) { (priv->data_buffer, (void __user *) buf, in_size)) {
mutex_unlock(&chip->buffer_mutex); mutex_unlock(&priv->buffer_mutex);
return -EFAULT; return -EFAULT;
} }
/* atomic tpm command send and result receive */ /* atomic tpm command send and result receive */
out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE); out_size = tpm_transmit(priv->chip, priv->data_buffer,
sizeof(priv->data_buffer));
if (out_size < 0) { if (out_size < 0) {
mutex_unlock(&chip->buffer_mutex); mutex_unlock(&priv->buffer_mutex);
return out_size; return out_size;
} }
atomic_set(&chip->data_pending, out_size); atomic_set(&priv->data_pending, out_size);
mutex_unlock(&chip->buffer_mutex); mutex_unlock(&priv->buffer_mutex);
/* Set a timeout by which the reader must come claim the result */ /* Set a timeout by which the reader must come claim the result */
mod_timer(&chip->user_read_timer, jiffies + (60 * HZ)); mod_timer(&priv->user_read_timer, jiffies + (60 * HZ));
return in_size; return in_size;
} }
...@@ -141,15 +161,15 @@ static ssize_t tpm_write(struct file *file, const char __user *buf, ...@@ -141,15 +161,15 @@ static ssize_t tpm_write(struct file *file, const char __user *buf,
*/ */
static int tpm_release(struct inode *inode, struct file *file) static int tpm_release(struct inode *inode, struct file *file)
{ {
struct tpm_chip *chip = file->private_data; struct file_priv *priv = file->private_data;
del_singleshot_timer_sync(&chip->user_read_timer); del_singleshot_timer_sync(&priv->user_read_timer);
flush_work(&chip->work); flush_work(&priv->work);
file->private_data = NULL; file->private_data = NULL;
atomic_set(&chip->data_pending, 0); atomic_set(&priv->data_pending, 0);
kzfree(chip->data_buffer); clear_bit(0, &priv->chip->is_open);
clear_bit(0, &chip->is_open); put_device(priv->chip->dev);
put_device(chip->dev); kfree(priv);
return 0; return 0;
} }
...@@ -166,12 +186,6 @@ int tpm_dev_add_device(struct tpm_chip *chip) ...@@ -166,12 +186,6 @@ int tpm_dev_add_device(struct tpm_chip *chip)
{ {
int rc; int rc;
mutex_init(&chip->buffer_mutex);
INIT_WORK(&chip->work, timeout_work);
setup_timer(&chip->user_read_timer, user_reader_timeout,
(unsigned long)chip);
chip->vendor.miscdev.fops = &tpm_fops; chip->vendor.miscdev.fops = &tpm_fops;
if (chip->dev_num == 0) if (chip->dev_num == 0)
chip->vendor.miscdev.minor = TPM_MINOR; chip->vendor.miscdev.minor = TPM_MINOR;
......
...@@ -103,13 +103,6 @@ struct tpm_chip { ...@@ -103,13 +103,6 @@ struct tpm_chip {
unsigned long is_open; /* only one allowed */ unsigned long is_open; /* only one allowed */
int time_expired; int time_expired;
/* Data passed to and from the tpm via the read/write calls */
u8 *data_buffer;
atomic_t data_pending;
struct mutex buffer_mutex;
struct timer_list user_read_timer; /* user needs to claim result */
struct work_struct work;
struct mutex tpm_mutex; /* tpm is processing */ struct mutex tpm_mutex; /* tpm is processing */
struct tpm_vendor_specific vendor; struct tpm_vendor_specific vendor;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment