tpm: Provide strong locking for device removal
[deliverable/linux.git] / drivers / char / tpm / tpm-chip.c
1 /*
2 * Copyright (C) 2004 IBM Corporation
3 * Copyright (C) 2014 Intel Corporation
4 *
5 * Authors:
6 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
7 * Leendert van Doorn <leendert@watson.ibm.com>
8 * Dave Safford <safford@watson.ibm.com>
9 * Reiner Sailer <sailer@watson.ibm.com>
10 * Kylene Hall <kjhall@us.ibm.com>
11 *
12 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
13 *
14 * TPM chip management routines.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation, version 2 of the
19 * License.
20 *
21 */
22
23 #include <linux/poll.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include <linux/spinlock.h>
27 #include <linux/freezer.h>
28 #include <linux/major.h>
29 #include "tpm.h"
30 #include "tpm_eventlog.h"
31
32 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
33 static LIST_HEAD(tpm_chip_list);
34 static DEFINE_SPINLOCK(driver_lock);
35
36 struct class *tpm_class;
37 dev_t tpm_devt;
38
39 /**
40 * tpm_try_get_ops() - Get a ref to the tpm_chip
41 * @chip: Chip to ref
42 *
43 * The caller must already have some kind of locking to ensure that chip is
44 * valid. This function will lock the chip so that the ops member can be
45 * accessed safely. The locking prevents tpm_chip_unregister from
46 * completing, so it should not be held for long periods.
47 *
48 * Returns -ERRNO if the chip could not be got.
49 */
50 int tpm_try_get_ops(struct tpm_chip *chip)
51 {
52 int rc = -EIO;
53
54 get_device(&chip->dev);
55
56 down_read(&chip->ops_sem);
57 if (!chip->ops)
58 goto out_lock;
59
60 if (!try_module_get(chip->dev.parent->driver->owner))
61 goto out_lock;
62
63 return 0;
64 out_lock:
65 up_read(&chip->ops_sem);
66 put_device(&chip->dev);
67 return rc;
68 }
69 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
70
71 /**
72 * tpm_put_ops() - Release a ref to the tpm_chip
73 * @chip: Chip to put
74 *
75 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
76 * be kfree'd.
77 */
78 void tpm_put_ops(struct tpm_chip *chip)
79 {
80 module_put(chip->dev.parent->driver->owner);
81 up_read(&chip->ops_sem);
82 put_device(&chip->dev);
83 }
84 EXPORT_SYMBOL_GPL(tpm_put_ops);
85
86 /**
87 * tpm_chip_find_get() - return tpm_chip for a given chip number
88 * @chip_num: id to find
89 *
90 * The return'd chip has been tpm_try_get_ops'd and must be released via
91 * tpm_put_ops
92 */
93 struct tpm_chip *tpm_chip_find_get(int chip_num)
94 {
95 struct tpm_chip *pos, *chip = NULL;
96
97 rcu_read_lock();
98 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
99 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
100 continue;
101
102 /* rcu prevents chip from being free'd */
103 if (!tpm_try_get_ops(pos))
104 chip = pos;
105 break;
106 }
107 rcu_read_unlock();
108 return chip;
109 }
110
111 /**
112 * tpm_dev_release() - free chip memory and the device number
113 * @dev: the character device for the TPM chip
114 *
115 * This is used as the release function for the character device.
116 */
117 static void tpm_dev_release(struct device *dev)
118 {
119 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
120
121 spin_lock(&driver_lock);
122 clear_bit(chip->dev_num, dev_mask);
123 spin_unlock(&driver_lock);
124 kfree(chip);
125 }
126
127 /**
128 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
129 * @dev: device to which the chip is associated
130 * @ops: struct tpm_class_ops instance
131 *
132 * Allocates a new struct tpm_chip instance and assigns a free
133 * device number for it. Caller does not have to worry about
134 * freeing the allocated resources. When the devices is removed
135 * devres calls tpmm_chip_remove() to do the job.
136 */
137 struct tpm_chip *tpmm_chip_alloc(struct device *dev,
138 const struct tpm_class_ops *ops)
139 {
140 struct tpm_chip *chip;
141 int rc;
142
143 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
144 if (chip == NULL)
145 return ERR_PTR(-ENOMEM);
146
147 mutex_init(&chip->tpm_mutex);
148 init_rwsem(&chip->ops_sem);
149 INIT_LIST_HEAD(&chip->list);
150
151 chip->ops = ops;
152
153 spin_lock(&driver_lock);
154 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
155 spin_unlock(&driver_lock);
156
157 if (chip->dev_num >= TPM_NUM_DEVICES) {
158 dev_err(dev, "No available tpm device numbers\n");
159 kfree(chip);
160 return ERR_PTR(-ENOMEM);
161 }
162
163 set_bit(chip->dev_num, dev_mask);
164
165 device_initialize(&chip->dev);
166
167 dev_set_drvdata(dev, chip);
168
169 chip->dev.class = tpm_class;
170 chip->dev.release = tpm_dev_release;
171 chip->dev.parent = dev;
172 #ifdef CONFIG_ACPI
173 chip->dev.groups = chip->groups;
174 #endif
175
176 if (chip->dev_num == 0)
177 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
178 else
179 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
180
181 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
182 if (rc)
183 goto out;
184
185 cdev_init(&chip->cdev, &tpm_fops);
186 chip->cdev.owner = dev->driver->owner;
187 chip->cdev.kobj.parent = &chip->dev.kobj;
188
189 rc = devm_add_action(dev, (void (*)(void *)) put_device, &chip->dev);
190 if (rc) {
191 put_device(&chip->dev);
192 return ERR_PTR(rc);
193 }
194
195 return chip;
196
197 out:
198 put_device(&chip->dev);
199 return ERR_PTR(rc);
200 }
201 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
202
203 static int tpm_add_char_device(struct tpm_chip *chip)
204 {
205 int rc;
206
207 rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
208 if (rc) {
209 dev_err(&chip->dev,
210 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
211 dev_name(&chip->dev), MAJOR(chip->dev.devt),
212 MINOR(chip->dev.devt), rc);
213
214 return rc;
215 }
216
217 rc = device_add(&chip->dev);
218 if (rc) {
219 dev_err(&chip->dev,
220 "unable to device_register() %s, major %d, minor %d, err=%d\n",
221 dev_name(&chip->dev), MAJOR(chip->dev.devt),
222 MINOR(chip->dev.devt), rc);
223
224 cdev_del(&chip->cdev);
225 return rc;
226 }
227
228 return rc;
229 }
230
231 static void tpm_del_char_device(struct tpm_chip *chip)
232 {
233 cdev_del(&chip->cdev);
234
235 /* Make the driver uncallable. */
236 down_write(&chip->ops_sem);
237 chip->ops = NULL;
238 up_write(&chip->ops_sem);
239
240 device_del(&chip->dev);
241 }
242
243 static int tpm1_chip_register(struct tpm_chip *chip)
244 {
245 int rc;
246
247 if (chip->flags & TPM_CHIP_FLAG_TPM2)
248 return 0;
249
250 rc = tpm_sysfs_add_device(chip);
251 if (rc)
252 return rc;
253
254 chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
255
256 return 0;
257 }
258
259 static void tpm1_chip_unregister(struct tpm_chip *chip)
260 {
261 if (chip->flags & TPM_CHIP_FLAG_TPM2)
262 return;
263
264 if (chip->bios_dir)
265 tpm_bios_log_teardown(chip->bios_dir);
266
267 tpm_sysfs_del_device(chip);
268 }
269
270 /*
271 * tpm_chip_register() - create a character device for the TPM chip
272 * @chip: TPM chip to use.
273 *
274 * Creates a character device for the TPM chip and adds sysfs attributes for
275 * the device. As the last step this function adds the chip to the list of TPM
276 * chips available for in-kernel use.
277 *
278 * This function should be only called after the chip initialization is
279 * complete.
280 */
281 int tpm_chip_register(struct tpm_chip *chip)
282 {
283 int rc;
284
285 rc = tpm1_chip_register(chip);
286 if (rc)
287 return rc;
288
289 tpm_add_ppi(chip);
290
291 rc = tpm_add_char_device(chip);
292 if (rc)
293 goto out_err;
294
295 /* Make the chip available. */
296 spin_lock(&driver_lock);
297 list_add_tail_rcu(&chip->list, &tpm_chip_list);
298 spin_unlock(&driver_lock);
299
300 chip->flags |= TPM_CHIP_FLAG_REGISTERED;
301
302 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
303 rc = __compat_only_sysfs_link_entry_to_kobj(
304 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
305 if (rc && rc != -ENOENT) {
306 tpm_chip_unregister(chip);
307 return rc;
308 }
309 }
310
311 return 0;
312 out_err:
313 tpm1_chip_unregister(chip);
314 return rc;
315 }
316 EXPORT_SYMBOL_GPL(tpm_chip_register);
317
318 /*
319 * tpm_chip_unregister() - release the TPM driver
320 * @chip: TPM chip to use.
321 *
322 * Takes the chip first away from the list of available TPM chips and then
323 * cleans up all the resources reserved by tpm_chip_register().
324 *
325 * Once this function returns the driver call backs in 'op's will not be
326 * running and will no longer start.
327 *
328 * NOTE: This function should be only called before deinitializing chip
329 * resources.
330 */
331 void tpm_chip_unregister(struct tpm_chip *chip)
332 {
333 if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
334 return;
335
336 spin_lock(&driver_lock);
337 list_del_rcu(&chip->list);
338 spin_unlock(&driver_lock);
339 synchronize_rcu();
340
341 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
342 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
343
344 tpm1_chip_unregister(chip);
345 tpm_del_char_device(chip);
346 }
347 EXPORT_SYMBOL_GPL(tpm_chip_unregister);
This page took 0.038437 seconds and 5 git commands to generate.