tpm-dev-common.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. * Authors:
  4. * Leendert van Doorn <leendert@watson.ibm.com>
  5. * Dave Safford <safford@watson.ibm.com>
  6. * Reiner Sailer <sailer@watson.ibm.com>
  7. * Kylene Hall <kjhall@us.ibm.com>
  8. *
  9. * Copyright (C) 2013 Obsidian Research Corp
  10. * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
  11. *
  12. * Device file system interface to the TPM
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation, version 2 of the
  17. * License.
  18. *
  19. */
  20. #include <linux/poll.h>
  21. #include <linux/slab.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/workqueue.h>
  24. #include "tpm.h"
  25. #include "tpm-dev.h"
  26. static struct workqueue_struct *tpm_dev_wq;
  27. static DEFINE_MUTEX(tpm_dev_wq_lock);
  28. static void tpm_async_work(struct work_struct *work)
  29. {
  30. struct file_priv *priv =
  31. container_of(work, struct file_priv, async_work);
  32. ssize_t ret;
  33. mutex_lock(&priv->buffer_mutex);
  34. priv->command_enqueued = false;
  35. ret = tpm_transmit(priv->chip, priv->space, priv->data_buffer,
  36. sizeof(priv->data_buffer), 0);
  37. tpm_put_ops(priv->chip);
  38. if (ret > 0) {
  39. priv->data_pending = ret;
  40. mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
  41. }
  42. mutex_unlock(&priv->buffer_mutex);
  43. wake_up_interruptible(&priv->async_wait);
  44. }
  45. static void user_reader_timeout(struct timer_list *t)
  46. {
  47. struct file_priv *priv = from_timer(priv, t, user_read_timer);
  48. pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
  49. task_tgid_nr(current));
  50. schedule_work(&priv->timeout_work);
  51. }
  52. static void tpm_timeout_work(struct work_struct *work)
  53. {
  54. struct file_priv *priv = container_of(work, struct file_priv,
  55. timeout_work);
  56. mutex_lock(&priv->buffer_mutex);
  57. priv->data_pending = 0;
  58. memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
  59. mutex_unlock(&priv->buffer_mutex);
  60. wake_up_interruptible(&priv->async_wait);
  61. }
  62. void tpm_common_open(struct file *file, struct tpm_chip *chip,
  63. struct file_priv *priv, struct tpm_space *space)
  64. {
  65. priv->chip = chip;
  66. priv->space = space;
  67. mutex_init(&priv->buffer_mutex);
  68. timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
  69. INIT_WORK(&priv->timeout_work, tpm_timeout_work);
  70. INIT_WORK(&priv->async_work, tpm_async_work);
  71. init_waitqueue_head(&priv->async_wait);
  72. file->private_data = priv;
  73. }
  74. ssize_t tpm_common_read(struct file *file, char __user *buf,
  75. size_t size, loff_t *off)
  76. {
  77. struct file_priv *priv = file->private_data;
  78. ssize_t ret_size = 0;
  79. int rc;
  80. del_singleshot_timer_sync(&priv->user_read_timer);
  81. flush_work(&priv->timeout_work);
  82. mutex_lock(&priv->buffer_mutex);
  83. if (priv->data_pending) {
  84. ret_size = min_t(ssize_t, size, priv->data_pending);
  85. if (ret_size > 0) {
  86. rc = copy_to_user(buf, priv->data_buffer, ret_size);
  87. memset(priv->data_buffer, 0, priv->data_pending);
  88. if (rc)
  89. ret_size = -EFAULT;
  90. }
  91. priv->data_pending = 0;
  92. }
  93. mutex_unlock(&priv->buffer_mutex);
  94. return ret_size;
  95. }
  96. ssize_t tpm_common_write(struct file *file, const char __user *buf,
  97. size_t size, loff_t *off)
  98. {
  99. struct file_priv *priv = file->private_data;
  100. int ret = 0;
  101. if (size > TPM_BUFSIZE)
  102. return -E2BIG;
  103. mutex_lock(&priv->buffer_mutex);
  104. /* Cannot perform a write until the read has cleared either via
  105. * tpm_read or a user_read_timer timeout. This also prevents split
  106. * buffered writes from blocking here.
  107. */
  108. if (priv->data_pending != 0 || priv->command_enqueued) {
  109. ret = -EBUSY;
  110. goto out;
  111. }
  112. if (copy_from_user(priv->data_buffer, buf, size)) {
  113. ret = -EFAULT;
  114. goto out;
  115. }
  116. if (size < 6 ||
  117. size < be32_to_cpu(*((__be32 *)(priv->data_buffer + 2)))) {
  118. ret = -EINVAL;
  119. goto out;
  120. }
  121. /* atomic tpm command send and result receive. We only hold the ops
  122. * lock during this period so that the tpm can be unregistered even if
  123. * the char dev is held open.
  124. */
  125. if (tpm_try_get_ops(priv->chip)) {
  126. ret = -EPIPE;
  127. goto out;
  128. }
  129. /*
  130. * If in nonblocking mode schedule an async job to send
  131. * the command return the size.
  132. * In case of error the err code will be returned in
  133. * the subsequent read call.
  134. */
  135. if (file->f_flags & O_NONBLOCK) {
  136. priv->command_enqueued = true;
  137. queue_work(tpm_dev_wq, &priv->async_work);
  138. mutex_unlock(&priv->buffer_mutex);
  139. return size;
  140. }
  141. ret = tpm_transmit(priv->chip, priv->space, priv->data_buffer,
  142. sizeof(priv->data_buffer), 0);
  143. tpm_put_ops(priv->chip);
  144. if (ret > 0) {
  145. priv->data_pending = ret;
  146. mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
  147. ret = size;
  148. }
  149. out:
  150. mutex_unlock(&priv->buffer_mutex);
  151. return ret;
  152. }
  153. __poll_t tpm_common_poll(struct file *file, poll_table *wait)
  154. {
  155. struct file_priv *priv = file->private_data;
  156. __poll_t mask = 0;
  157. poll_wait(file, &priv->async_wait, wait);
  158. if (priv->data_pending)
  159. mask = EPOLLIN | EPOLLRDNORM;
  160. else
  161. mask = EPOLLOUT | EPOLLWRNORM;
  162. return mask;
  163. }
  164. /*
  165. * Called on file close
  166. */
  167. void tpm_common_release(struct file *file, struct file_priv *priv)
  168. {
  169. flush_work(&priv->async_work);
  170. del_singleshot_timer_sync(&priv->user_read_timer);
  171. flush_work(&priv->timeout_work);
  172. file->private_data = NULL;
  173. priv->data_pending = 0;
  174. }
  175. int __init tpm_dev_common_init(void)
  176. {
  177. tpm_dev_wq = alloc_workqueue("tpm_dev_wq", WQ_MEM_RECLAIM, 0);
  178. return !tpm_dev_wq ? -ENOMEM : 0;
  179. }
  180. void __exit tpm_dev_common_exit(void)
  181. {
  182. if (tpm_dev_wq) {
  183. destroy_workqueue(tpm_dev_wq);
  184. tpm_dev_wq = NULL;
  185. }
  186. }