static_key.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * static_key.c
  3. */
  4. #include <linux/atomic.h>
  5. #include <linux/device.h>
  6. #include <linux/fs.h>
  7. #include <linux/kernel.h> /* for sprintf() */
  8. #include <linux/module.h>
  9. #include <linux/printk.h>
  10. #include <linux/types.h>
  11. #include <linux/uaccess.h> /* for get_user and put_user */
  12. #include <asm/errno.h>
  13. static int device_open(struct inode *inode, struct file *file);
  14. static int device_release(struct inode *inode, struct file *file);
  15. static ssize_t device_read(struct file *file, char __user *buf, size_t count,
  16. loff_t *ppos);
  17. static ssize_t device_write(struct file *file, const char __user *buf,
  18. size_t count, loff_t *ppos);
  19. #define SUCCESS 0
  20. #define DEVICE_NAME "key_state"
  21. #define BUF_LEN 10
  22. static int major;
  23. enum {
  24. CDEV_NOT_USED = 0,
  25. CDEV_EXCLUSIVE_OPEN = 1,
  26. };
  27. static atomic_t already_open = ATOMIC_INIT(CDEV_NOT_USED);
  28. static char msg[BUF_LEN + 1];
  29. static struct class *cls;
  30. static DEFINE_STATIC_KEY_FALSE(fkey);
  31. static struct file_operations chardev_fops = {
  32. .owner = THIS_MODULE,
  33. .open = device_open,
  34. .release = device_release,
  35. .read = device_read,
  36. .write = device_write,
  37. };
  38. static int __init chardev_init(void)
  39. {
  40. major = register_chrdev(0, DEVICE_NAME, &chardev_fops);
  41. if (major < 0) {
  42. pr_alert("Registering char device failed with %d\n", major);
  43. return major;
  44. }
  45. pr_info("I was assigned major number %d\n", major);
  46. cls = class_create(THIS_MODULE, DEVICE_NAME);
  47. device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
  48. pr_info("Device created on /dev/%s\n", DEVICE_NAME);
  49. return SUCCESS;
  50. }
  51. static void __exit chardev_exit(void)
  52. {
  53. device_destroy(cls, MKDEV(major, 0));
  54. class_destroy(cls);
  55. /* Unregister the device */
  56. unregister_chrdev(major, DEVICE_NAME);
  57. }
  58. /* Methods */
  59. /**
  60. * Called when a process tried to open the device file, like
  61. * cat /dev/key_state
  62. */
  63. static int device_open(struct inode *inode, struct file *file)
  64. {
  65. if (atomic_cmpxchg(&already_open, CDEV_NOT_USED, CDEV_EXCLUSIVE_OPEN))
  66. return -EBUSY;
  67. sprintf(msg, static_key_enabled(&fkey) ? "enabled\n" : "disabled\n");
  68. pr_info("fastpath 1\n");
  69. if (static_branch_unlikely(&fkey))
  70. pr_alert("do unlikely thing\n");
  71. pr_info("fastpath 2\n");
  72. try_module_get(THIS_MODULE);
  73. return SUCCESS;
  74. }
  75. /**
  76. * Called when a process closes the device file
  77. */
  78. static int device_release(struct inode *inode, struct file *file)
  79. {
  80. /* We are now ready for our next caller. */
  81. atomic_set(&already_open, CDEV_NOT_USED);
  82. /**
  83. * Decrement the usage count, or else once you opened the file, you will
  84. * never get rid of the module.
  85. */
  86. module_put(THIS_MODULE);
  87. return SUCCESS;
  88. }
  89. /**
  90. * Called when a process, which already opened the dev file, attempts to
  91. * read from it.
  92. */
  93. static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
  94. char __user *buffer, /* buffer to fill with data */
  95. size_t length, /* length of the buffer */
  96. loff_t *offset)
  97. {
  98. /* Number of the bytes actually written to the buffer */
  99. int bytes_read = 0;
  100. const char *msg_ptr = msg;
  101. if (!*(msg_ptr + *offset)) { /* We are at the end of the message */
  102. *offset = 0; /* reset the offset */
  103. return 0; /* signify end of file */
  104. }
  105. msg_ptr += *offset;
  106. /* Actually put the date into the buffer */
  107. while (length && *msg_ptr) {
  108. /**
  109. * The buffer is in the user data segment, not the kernel
  110. * segment so "*" assignment won't work. We have to use
  111. * put_user which copies data from the kernel data segment to
  112. * the user data segment.
  113. */
  114. put_user(*(msg_ptr++), buffer++);
  115. length--;
  116. bytes_read++;
  117. }
  118. *offset += bytes_read;
  119. /* Most read functions return the number of bytes put into the buffer. */
  120. return bytes_read;
  121. }
  122. /* Called when a process writes to dev file; echo "enable" > /dev/key_state */
  123. static ssize_t device_write(struct file *filp, const char __user *buffer,
  124. size_t length, loff_t *offset)
  125. {
  126. char command[10];
  127. if (length > 10) {
  128. pr_err("command exceeded 10 char\n");
  129. return -EINVAL;
  130. }
  131. if (copy_from_user(command, buffer, length))
  132. return -EFAULT;
  133. if (strncmp(command, "enable", strlen("enable")) == 0)
  134. static_branch_enable(&fkey);
  135. else if (strncmp(command, "disable", strlen("disable")) == 0)
  136. static_branch_disable(&fkey);
  137. else {
  138. pr_err("Invalid command: %s\n", command);
  139. return -EINVAL;
  140. }
  141. /* Again, return the number of input characters used. */
  142. return length;
  143. }
  144. module_init(chardev_init);
  145. module_exit(chardev_exit);
  146. MODULE_LICENSE("GPL");