-1

I'm kind of confused about what sort of functions are not allowed in a spin lock's critical section.

In particular I'm confused about reentrant functions. I thought that all reentrant functions are unsafe to use in a spin lock's critical section, but it appears that functions like kfree and memcpy are ok to use.

So how do we know what functions are ok or not ok? I generally think anything that might block is unsafe, but don't all reentrant functions have the capacity/potential to block?

Also what is the role and relationship of the interrupt handler to spin locks?

Shisui
  • 1,051
  • 1
  • 8
  • 23

1 Answers1

0

You have reentrant backward: Re-entrant functions are the functions that are safe to use while a spinlock is held.

Many non-reentrant functions can be ok as well.

The primary concern is calling a function that directly or indirectly tries to acquire the same lock you're currently holding which leads to deadlock.


Specific to kmalloc, though you did not specify this in your question:

The general answer above is the reason why you've heard kmalloc may not be safe to use within a spinlock: It also acquires a spinlock by default. Use the flag GFP_ATOMIC to control whether kmalloc attempts to acquire a spinlock:

GFP_ATOMIC The allocation is high priority and must not sleep. This is the flag to use in interrupt handlers, in bottom halves, while holding a spinlock, and in other situations where you cannot sleep.

http://books.gigatux.nl/mirror/kerneldevelopment/0672327201/ch11lev1sec4.html

qz-
  • 674
  • 1
  • 4
  • 14
  • Malloc is reentrant and it's not safe to use while a spin lock is being held, so I don't think this is true – Shisui Oct 29 '21 at 00:39
  • @Shisui Where did you head that from? – qz- Oct 29 '21 at 00:51
  • Do you mean `kmalloc`? Is your question focused on the Linux kernel or not? `kmalloc` has a `get_free_pages`-style bitmask argument which determines what sort of reentrancy requirements it meets. E.g. it is more reentrant with `GFP_ATOMIC`. – Kaz Oct 29 '21 at 00:52
  • @Kaz yes, kmalloc. – Shisui Oct 29 '21 at 02:31
  • @Shisui I've updated the answer with information relevant to your `kmalloc` concerns. – qz- Oct 29 '21 at 03:29