static_key.c 4.9 KB

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