In the module, you try to implement a queue by yourself, and there will be problems when performing multi-process (the kernel should be called multithreading) operations.
I tried to solve the problem, but failed because I had to sign the kernel development effort ....
Later, we adopted the kernel implementation kfifo (include/Linux/kfifo. h), kfifo is added in the Linux standard kernel> = 2.6.10. The standard kernel here is because the rhel4.8 kernel version is 2.6.9 (less than 2.6.10) but also has this data structure, and then the standard kernel (www.kernel.org) <2.6.10 does not have this data structure.
Write a simple implementation as follows:
# Include <Linux/init. h> # include <Linux/module. h> # include <Linux/kfifo. h> # include <Linux/err. h> # define str_len 64 # define kff_len 512*256 struct kfifo * my_kfifo = NULL; spinlock_t my_1_o_lock; static int my_put (INT num) {int I; char buff [str_len]; for (I = 0; I <num; I ++) {sprintf (buff, "my data: % d... ", I); If (k1_o_put (my_kfifo, buff, str_len) <= 0) {printk (" <2> "" % s failed \ n ", buff );}} return num;} St Atic int my_get (void) {char buff [str_len]; while (k1_o_len (my_kfifo)> 0) // The number of bytes available in the FIFO? It seems wrong. {If (k1_o_get (my_kfifo, buff, str_len) <= 0) {printk ("<2>" k1_o_get failed \ n ");} printk ("<2>" "% s \ n", buff);} return 0;} static int _ init my_init (void) {printk ("<2>" "init kfifo \ n"); spin_lock_init (& my_1_o_lock); my_kfifo = k1_o_alloc (kff_len, gfp_kernel, & my_1_o_lock ); // 128 K if (is_err (my_kfifo) {printk ("<2>" "k1_o_alloc failed \ n"); Return-1;} my_put (10 ); my_get (); Return 0;} static void _ exit my_exit (void) {k1_o_free (my_kfifo); printk ("<2>" Exit kfifo \ n ");} module_init (my_init); module_exit (my_exit); module_license ("GPL ");
Makefile:
ifneq ($(KERNELRELEASE),) obj-m := kfifo.oelsePWD ?= $(shell pwd)KVERSION := $(shell uname -r)all: make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules clean: make -C /lib/modules/$(KVERSION)/build M=$(PWD) cleanendif
The first parameter of kmeano_alloc (...) limits the maximum queue size to 512*256 (128 K)
Because kw.o_alloc uses kmalloc to allocate memory, kfree cannot allocate more than 128 kb of memory.
The following uses another method to allocate kfifo with> 128 kb of memory
Static int _ init my_init (void) {printk ("<2>" init kfifo \ n "); spin_lock_init (& my_fifo_lock); PTR = (char *) vmalloc (kff_len * 128); // 16 MB of memory allocated if (is_err (PTR) {printk ("<2>" "vmalloc failed \ n "); return-1;} my_kfifo = kw.o_init (PTR, kff_len * 128, gfp_kernel, & my_1_o_lock); // initialize the 16 m queue if (is_err (my_kfifo )) {printk ("<2>" "k1_o_alloc failed \ n"); Return-1;} my_put (32768); my_get (); Return 0 ;} static void _ exit my_exit (void) {kfree (my_kfifo); vfree (PTR); printk ("<2>" Exit kfifo \ n ");}
About k1_o_len
/** * __kfifo_len - returns the number of bytes available in the FIFO, no locking version * @fifo: the fifo to be used. */static inline unsigned int __kfifo_len(struct kfifo *fifo){ return fifo->in - fifo->out;}/** * kfifo_len - returns the number of bytes available in the FIFO * @fifo: the fifo to be used. */static inline unsigned int kfifo_len(struct kfifo *fifo){ unsigned long flags; unsigned int ret; spin_lock_irqsave(fifo->lock, flags); ret = __kfifo_len(fifo); spin_unlock_irqrestore(fifo->lock, flags); return ret;}
The description of kmeano_len () in the source code is:
Kmeano_len-returns the number of bytes available in the FIFO
It seems that the number of available bytes in the first-in-first-out (FIFO) is returned.
This value keeps decreasing as the team leaves, until 0
The available here should be available data.