[Understanding] Why does my spin_lock_irqsave () Fail to lock the clock?

Source: Internet
Author: User

LZ:

 

I tried to use spin_lock_irqsave () and found that no clock interruption was disabled. I don't know where I got it wrong?
Kernel version:

  1. Uname-a result:
  2. Linux localhost. localdomain 2.6.18 #1 sat Jul 19 13:06:00 EDT 2008 i686 i686 i386 GNU/Linux

Copy code

My code:

  1. # Include <Linux/kernel. h>
  2. # Include <Linux/init. h>
  3. # Include <Linux/module. h>
  4. # Include <Linux/spinlock. h>
  5. Static spinlock_t my_spinlock;
  6. Static int _ init init_test ()
  7. {
  8. Unsigned long flags;
  9. Spin_lock_init (& my_spinlock );
  10. Spin_lock_irqsave (& my_spinlock, flags );
  11. Return 0;
  12. }
  13. Static void _ exit init_exit ()
  14. {
  15. Return;
  16. }
  17. Module_init (init_test );
  18. Module_exit (init_exit );

Copy code

After the module is loaded, run the following command to view the Wall Clock:

  1. # Date; sleep 2; Date
  2. Output result:
  3. Sun Apr 19 12:59:42 EDT 2009
  4. Sun Apr 19 12:59:44 EDT 2009

Copy code

The clock on the wall is still changing, indicating that the clock interruption is not disabled.
But I understand that spin_lock_irqsave () will turn off all IRQ through CLI, and I do not have the spin_unlock_irqrestore () in the module, so after loading this module, the system will not respond to any external blocked interruptions. However, this is not the actual result.

Where did I get it wrong? Thank you for your advice!

 

 

Answer:

Write a section of the kernel module and userspace program to verify:

Kernel module: my_spin_lock.c

  1. # Include <Linux/kernel. h>
  2. # Include <Linux/init. h>
  3. # Include <Linux/module. h>
  4. # Include <Linux/spinlock. h>
  5. # Define if_mask 0x00000200
  6. Static spinlock_t my_spinlock;
  7. Static unsigned int is_interrupt_enable ()
  8. {
  9. Unsigned long my_eflags;
  10. ASM volatile ("pushfl/n/t"
  11. "Popl % 0"
  12. : "= A" (my_eflags ));
  13. Return (my_eflags & if_mask) = 0 )? 0: 1 );
  14. }
  15. Static int _ init init_test ()
  16. {
  17. Unsigned long flags;
  18. Spin_lock_init (& my_spinlock );
  19. Spin_lock_irqsave (& my_spinlock, flags );
  20. Printk ("from kernelspace init: interrupt was enable: % d/N", is_interrupt_enable ());
  21. Return 0;
  22. }
  23. Static void _ exit init_exit ()
  24. {
  25. Return;
  26. }
  27. Module_license ("GPL ");
  28. Module_init (init_test );
  29. Module_exit (init_exit );

Copy code

Generate my_spin_lock.ko after compilation

Then write a userspace program: Test. C.

  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Define if_mask 0x00000200
  4. # Define pai_len 100
  5. Unsigned int is_interrupt_enable ()
  6. {
  7. Unsigned long my_eflags;
  8. ASM volatile ("pushfl/n/t"
  9. "Popl % 0/n/t"
  10. : "= A" (my_eflags ));
  11. Return (my_eflags & if_mask) = 0 )? 0: 1 );
  12. }
  13. Int main (INT argc, char * argv [])
  14. {
  15. If (argc! = 2)
  16. {
  17. Printf ("usersage: insmod your_kernel_module/N ");
  18. Return 0;
  19. }
  20. Char cmd [cmd_len];
  21. Memset (CMD, 0, sizeof (CMD ));
  22. Sprintf (CMD, "insmod % s", argv [1]);
  23. Printf ("from userspace, before insmod, inerrput is enable: % d/N", is_interrupt_enable ());
  24. System (CMD);/* insmod kernel module */
  25. Printf ("from userspace, after insmod, interrupt is enable: % d/N", is_interrupt_enable ());
  26. Memset (CMD, 0, sizeof (CMD ));
  27. Sprintf (CMD, "rmmod % s", argv [1]);
  28. System (CMD);/* rmmod kernel module */
  29. Return 0;
  30. }

Copy code

Generate the test binary file after compilation

Run the following command:

  1. ./Test my_spin_lock.ko

Copy code

Running result:

Quote: From userspace, before insmod, interrupt is enable: 1
From kernelspace init: interrupt was enable: 0
From userspace, after insmod, interrupt is enable: 1

It can be seen that interrupt is indeed disabled after the init function of the kernel module is run. Then return to the user State. Interrupt is enabled.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.