1
0

static_key.c 4.8 KB

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