1
votes

I'm pretty new to Linux device drivers and kernel. I basically want to synchronize a workque thread (Lets call it A()) with another function (Lets call it B()). My purpose here is to fail B when A is running.

Currently, what I have done is as follows.

A(){
  active = true; // a variable shared b/w both A and B
  ...
  ...
  ...
  active = false;
}

B(){
   if(active){
      return -EBUSY
    }
 }

Is this the right way to synchronize these 2 functions? Is there any other strategy I should follow?

3
It depends on where B() is called from (in which context, process or atomic) and how much code each function has. Please share the actual code with us. - Sam Protsenko
To be exact, A is mmc_recan which is run using workqueue (an hence process context). And B is mmc_suspend_host. Basically I need to avoid B from happening when A is running! - Django
What is exactly the issue that you have? - Sam Protsenko
Are you sure you don't want B to wait for availability instead? That is the more usual kernel approach unless the user-mode app has said it doesn't want to block. - wallyk
B already has mechanism to block. But the block is active indefinitely and the only way out I see is to return from B. - Django

3 Answers

1
votes

Why do you want to do so?

A: mmc_rescan() is defined as INIT_DELAYED_WORK(&host->detect, mmc_rescan);

B: First line of code in mmc_suspend_host() is cancel_delayed_work(&host->detect);

So your A is canceled in B. It was done for a reason, brought by this commit.

So what's the reason to replace this cancellation with another synchronization? If you don't have this commit in your kernel, just pull it (cherry-pick), it probably fixes your problem.

UPDATE

Please see next commits, they change MMC suspend behavior:

  1. mmc: core: Push common suspend|resume code into each bus_ops

  2. mmc: core: Initiate suspend|resume from mmc bus instead of mmc host

  3. mmc: core: Remove deprecated mmc_suspend|resume_host APIs

Maybe you just need to back-port those patches to fix your problem. Or at least they will make it a bit simpler to fix it.

0
votes

You should define a mutex and use it whenever you have shared variable in your code, here is an implementation of mutex and conditions functions in Linux kernel Linux Mutexes and Conditions