tpm-dev-common.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/slab.h>
  21. #include <linux/uaccess.h>
  22. #include "tpm.h"
  23. #include "tpm-dev.h"
  24. static void user_reader_timeout(unsigned long ptr)
  25. {
  26. struct file_priv *priv = (struct file_priv *)ptr;
  27. pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
  28. task_tgid_nr(current));
  29. schedule_work(&priv->work);
  30. }
  31. static void timeout_work(struct work_struct *work)
  32. {
  33. struct file_priv *priv = container_of(work, struct file_priv, work);
  34. mutex_lock(&priv->buffer_mutex);
  35. atomic_set(&priv->data_pending, 0);
  36. memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
  37. mutex_unlock(&priv->buffer_mutex);
  38. }
  39. void tpm_common_open(struct file *file, struct tpm_chip *chip,
  40. struct file_priv *priv)
  41. {
  42. priv->chip = chip;
  43. atomic_set(&priv->data_pending, 0);
  44. mutex_init(&priv->buffer_mutex);
  45. setup_timer(&priv->user_read_timer, user_reader_timeout,
  46. (unsigned long)priv);
  47. INIT_WORK(&priv->work, timeout_work);
  48. file->private_data = priv;
  49. }
  50. ssize_t tpm_common_read(struct file *file, char __user *buf,
  51. size_t size, loff_t *off)
  52. {
  53. struct file_priv *priv = file->private_data;
  54. ssize_t ret_size;
  55. ssize_t orig_ret_size;
  56. int rc;
  57. del_singleshot_timer_sync(&priv->user_read_timer);
  58. flush_work(&priv->work);
  59. ret_size = atomic_read(&priv->data_pending);
  60. if (ret_size > 0) { /* relay data */
  61. orig_ret_size = ret_size;
  62. if (size < ret_size)
  63. ret_size = size;
  64. mutex_lock(&priv->buffer_mutex);
  65. rc = copy_to_user(buf, priv->data_buffer, ret_size);
  66. memset(priv->data_buffer, 0, orig_ret_size);
  67. if (rc)
  68. ret_size = -EFAULT;
  69. mutex_unlock(&priv->buffer_mutex);
  70. }
  71. atomic_set(&priv->data_pending, 0);
  72. return ret_size;
  73. }
  74. ssize_t tpm_common_write(struct file *file, const char __user *buf,
  75. size_t size, loff_t *off, struct tpm_space *space)
  76. {
  77. struct file_priv *priv = file->private_data;
  78. size_t in_size = size;
  79. ssize_t out_size;
  80. /* Cannot perform a write until the read has cleared either via
  81. * tpm_read or a user_read_timer timeout. This also prevents split
  82. * buffered writes from blocking here.
  83. */
  84. if (atomic_read(&priv->data_pending) != 0)
  85. return -EBUSY;
  86. if (in_size > TPM_BUFSIZE)
  87. return -E2BIG;
  88. mutex_lock(&priv->buffer_mutex);
  89. if (copy_from_user
  90. (priv->data_buffer, (void __user *) buf, in_size)) {
  91. mutex_unlock(&priv->buffer_mutex);
  92. return -EFAULT;
  93. }
  94. /* atomic tpm command send and result receive. We only hold the ops
  95. * lock during this period so that the tpm can be unregistered even if
  96. * the char dev is held open.
  97. */
  98. if (tpm_try_get_ops(priv->chip)) {
  99. mutex_unlock(&priv->buffer_mutex);
  100. return -EPIPE;
  101. }
  102. out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
  103. sizeof(priv->data_buffer), 0);
  104. tpm_put_ops(priv->chip);
  105. if (out_size < 0) {
  106. mutex_unlock(&priv->buffer_mutex);
  107. return out_size;
  108. }
  109. atomic_set(&priv->data_pending, out_size);
  110. mutex_unlock(&priv->buffer_mutex);
  111. /* Set a timeout by which the reader must come claim the result */
  112. mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
  113. return in_size;
  114. }
  115. /*
  116. * Called on file close
  117. */
  118. void tpm_common_release(struct file *file, struct file_priv *priv)
  119. {
  120. del_singleshot_timer_sync(&priv->user_read_timer);
  121. flush_work(&priv->work);
  122. file->private_data = NULL;
  123. atomic_set(&priv->data_pending, 0);
  124. }