Skip to content

Commit e3302e0

Browse files
jgunthorpePeterHuewe
authored andcommitted
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: Jason Gunthorpe <jgunthorpe@obsidianresearch.com> Reviewed-by: Joel Schopp <jschopp@linux.vnet.ibm.com> Reviewed-by: Ashley Lai <adlai@linux.vnet.ibm.com> Signed-off-by: Peter Huewe <peterhuewe@gmx.de>
1 parent 5f82e9f commit e3302e0

2 files changed

Lines changed: 57 additions & 50 deletions

File tree

‎drivers/char/tpm/tpm-dev.c‎

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,42 @@
2222
#include <linux/uaccess.h>
2323
#include "tpm.h"
2424

25+
struct file_priv {
26+
struct tpm_chip *chip;
27+
28+
/* Data passed to and from the tpm via the read/write calls */
29+
atomic_t data_pending;
30+
struct mutex buffer_mutex;
31+
32+
struct timer_list user_read_timer; /* user needs to claim result */
33+
struct work_struct work;
34+
35+
u8 data_buffer[TPM_BUFSIZE];
36+
};
37+
2538
static void user_reader_timeout(unsigned long ptr)
2639
{
27-
struct tpm_chip *chip = (struct tpm_chip *) ptr;
40+
struct file_priv *priv = (struct file_priv *)ptr;
2841

29-
schedule_work(&chip->work);
42+
schedule_work(&priv->work);
3043
}
3144

3245
static void timeout_work(struct work_struct *work)
3346
{
34-
struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
47+
struct file_priv *priv = container_of(work, struct file_priv, work);
3548

36-
mutex_lock(&chip->buffer_mutex);
37-
atomic_set(&chip->data_pending, 0);
38-
memset(chip->data_buffer, 0, TPM_BUFSIZE);
39-
mutex_unlock(&chip->buffer_mutex);
49+
mutex_lock(&priv->buffer_mutex);
50+
atomic_set(&priv->data_pending, 0);
51+
memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
52+
mutex_unlock(&priv->buffer_mutex);
4053
}
4154

4255
static int tpm_open(struct inode *inode, struct file *file)
4356
{
4457
struct miscdevice *misc = file->private_data;
4558
struct tpm_chip *chip = container_of(misc, struct tpm_chip,
4659
vendor.miscdev);
60+
struct file_priv *priv;
4761

4862
/* It's assured that the chip will be opened just once,
4963
* by the check of is_open variable, which is protected
@@ -53,85 +67,91 @@ static int tpm_open(struct inode *inode, struct file *file)
5367
return -EBUSY;
5468
}
5569

56-
chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
57-
if (chip->data_buffer == NULL) {
70+
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
71+
if (priv == NULL) {
5872
clear_bit(0, &chip->is_open);
5973
return -ENOMEM;
6074
}
6175

62-
atomic_set(&chip->data_pending, 0);
76+
priv->chip = chip;
77+
atomic_set(&priv->data_pending, 0);
78+
mutex_init(&priv->buffer_mutex);
79+
setup_timer(&priv->user_read_timer, user_reader_timeout,
80+
(unsigned long)priv);
81+
INIT_WORK(&priv->work, timeout_work);
6382

64-
file->private_data = chip;
83+
file->private_data = priv;
6584
get_device(chip->dev);
6685
return 0;
6786
}
6887

6988
static ssize_t tpm_read(struct file *file, char __user *buf,
7089
size_t size, loff_t *off)
7190
{
72-
struct tpm_chip *chip = file->private_data;
91+
struct file_priv *priv = file->private_data;
7392
ssize_t ret_size;
7493
int rc;
7594

76-
del_singleshot_timer_sync(&chip->user_read_timer);
77-
flush_work(&chip->work);
78-
ret_size = atomic_read(&chip->data_pending);
95+
del_singleshot_timer_sync(&priv->user_read_timer);
96+
flush_work(&priv->work);
97+
ret_size = atomic_read(&priv->data_pending);
7998
if (ret_size > 0) { /* relay data */
8099
ssize_t orig_ret_size = ret_size;
81100
if (size < ret_size)
82101
ret_size = size;
83102

84-
mutex_lock(&chip->buffer_mutex);
85-
rc = copy_to_user(buf, chip->data_buffer, ret_size);
86-
memset(chip->data_buffer, 0, orig_ret_size);
103+
mutex_lock(&priv->buffer_mutex);
104+
rc = copy_to_user(buf, priv->data_buffer, ret_size);
105+
memset(priv->data_buffer, 0, orig_ret_size);
87106
if (rc)
88107
ret_size = -EFAULT;
89108

90-
mutex_unlock(&chip->buffer_mutex);
109+
mutex_unlock(&priv->buffer_mutex);
91110
}
92111

93-
atomic_set(&chip->data_pending, 0);
112+
atomic_set(&priv->data_pending, 0);
94113

95114
return ret_size;
96115
}
97116

98117
static ssize_t tpm_write(struct file *file, const char __user *buf,
99118
size_t size, loff_t *off)
100119
{
101-
struct tpm_chip *chip = file->private_data;
120+
struct file_priv *priv = file->private_data;
102121
size_t in_size = size;
103122
ssize_t out_size;
104123

105124
/* cannot perform a write until the read has cleared
106125
either via tpm_read or a user_read_timer timeout.
107126
This also prevents splitted buffered writes from blocking here.
108127
*/
109-
if (atomic_read(&chip->data_pending) != 0)
128+
if (atomic_read(&priv->data_pending) != 0)
110129
return -EBUSY;
111130

112131
if (in_size > TPM_BUFSIZE)
113132
return -E2BIG;
114133

115-
mutex_lock(&chip->buffer_mutex);
134+
mutex_lock(&priv->buffer_mutex);
116135

117136
if (copy_from_user
118-
(chip->data_buffer, (void __user *) buf, in_size)) {
119-
mutex_unlock(&chip->buffer_mutex);
137+
(priv->data_buffer, (void __user *) buf, in_size)) {
138+
mutex_unlock(&priv->buffer_mutex);
120139
return -EFAULT;
121140
}
122141

123142
/* atomic tpm command send and result receive */
124-
out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
143+
out_size = tpm_transmit(priv->chip, priv->data_buffer,
144+
sizeof(priv->data_buffer));
125145
if (out_size < 0) {
126-
mutex_unlock(&chip->buffer_mutex);
146+
mutex_unlock(&priv->buffer_mutex);
127147
return out_size;
128148
}
129149

130-
atomic_set(&chip->data_pending, out_size);
131-
mutex_unlock(&chip->buffer_mutex);
150+
atomic_set(&priv->data_pending, out_size);
151+
mutex_unlock(&priv->buffer_mutex);
132152

133153
/* Set a timeout by which the reader must come claim the result */
134-
mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
154+
mod_timer(&priv->user_read_timer, jiffies + (60 * HZ));
135155

136156
return in_size;
137157
}
@@ -141,15 +161,15 @@ static ssize_t tpm_write(struct file *file, const char __user *buf,
141161
*/
142162
static int tpm_release(struct inode *inode, struct file *file)
143163
{
144-
struct tpm_chip *chip = file->private_data;
164+
struct file_priv *priv = file->private_data;
145165

146-
del_singleshot_timer_sync(&chip->user_read_timer);
147-
flush_work(&chip->work);
166+
del_singleshot_timer_sync(&priv->user_read_timer);
167+
flush_work(&priv->work);
148168
file->private_data = NULL;
149-
atomic_set(&chip->data_pending, 0);
150-
kzfree(chip->data_buffer);
151-
clear_bit(0, &chip->is_open);
152-
put_device(chip->dev);
169+
atomic_set(&priv->data_pending, 0);
170+
clear_bit(0, &priv->chip->is_open);
171+
put_device(priv->chip->dev);
172+
kfree(priv);
153173
return 0;
154174
}
155175

@@ -166,12 +186,6 @@ int tpm_dev_add_device(struct tpm_chip *chip)
166186
{
167187
int rc;
168188

169-
mutex_init(&chip->buffer_mutex);
170-
INIT_WORK(&chip->work, timeout_work);
171-
172-
setup_timer(&chip->user_read_timer, user_reader_timeout,
173-
(unsigned long)chip);
174-
175189
chip->vendor.miscdev.fops = &tpm_fops;
176190
if (chip->dev_num == 0)
177191
chip->vendor.miscdev.minor = TPM_MINOR;

‎drivers/char/tpm/tpm.h‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@ struct tpm_chip {
103103
unsigned long is_open; /* only one allowed */
104104
int time_expired;
105105

106-
/* Data passed to and from the tpm via the read/write calls */
107-
u8 *data_buffer;
108-
atomic_t data_pending;
109-
struct mutex buffer_mutex;
110-
111-
struct timer_list user_read_timer; /* user needs to claim result */
112-
struct work_struct work;
113106
struct mutex tpm_mutex; /* tpm is processing */
114107

115108
struct tpm_vendor_specific vendor;

0 commit comments

Comments
 (0)