1
0

static_key.c 5.0 KB

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