static_key.c 5.0 KB

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