Skip to contentSkip to conversation

staging: r8188eu: how to handle nested mutex under spinlock

DISCUSSION Michael Straube 5 participants 21 messages Apr 2, 2022 – Apr 4, 2022 9 messages not in the archive Staging

lore
Share & tools
lore
#0 lore raw

Hi all,

smatch reported a sleeping in atomic context.

rtw_set_802_11_disassociate() <- disables preempt
-> _rtw_pwr_wakeup()
-> ips_leave()

rtw_set_802_11_disassociate() takes a spinlock and ips_leave() uses a mutex.

I'm fairly new to the locking stuff, but as far as I know this is not a false positive since mutex can sleep, but that's not allowed under a spinlock.

What is the best way to handle this?
I'm not sure if converting the mutex to a spinlock (including all the other places where the mutex is used) is the right thing to do?

thanks,
Michael

#1 lore raw

Hi Michael,

Quoted 11 lines · Michael Straube

Hi all,

smatch reported a sleeping in atomic context.

rtw_set_802_11_disassociate() <- disables preempt
-> _rtw_pwr_wakeup()

     -> ips_leave()

rtw_set_802_11_disassociate() takes a spinlock and ips_leave() uses a mutex.

Quoted 8 lines

I'm fairly new to the locking stuff, but as far as I know this is not a false positive since mutex can sleep, but that's not allowed under a spinlock.

What is the best way to handle this?
I'm not sure if converting the mutex to a spinlock (including all the other places where the mutex is used) is the right thing to do?

I've looked into this like a month ago.

IMO, this code just need to be redesigned, since locking scheme is very complicated there and, as smatch says, not correct.

Simple s/mutex_lock/spin_lock/ may work in that case, but one day locking scheme should be reworked... Or just some code parts should be dropped :))

With regards,
Pavel Skripkin

#2 lore raw
In reply to Michael Straube
Quoted 11 lines · Michael Straube

Hi all,

smatch reported a sleeping in atomic context.

rtw_set_802_11_disassociate() <- disables preempt
-> _rtw_pwr_wakeup()
   -> ips_leave()

rtw_set_802_11_disassociate() takes a spinlock and ips_leave() uses a mutex.

Quoted 7 lines

I'm fairly new to the locking stuff, but as far as I know this is not a false positive since mutex can sleep, but that's not allowed under a spinlock.

What is the best way to handle this?
I'm not sure if converting the mutex to a spinlock (including all the other places where the mutex is used) is the right thing to do?

In drivers/net/wireless/realtek/rtlwifi, we had a similar problem. There it was handled by putting the lps_enter() and lps_leave() operations in a separate workqueue. In this case, the routines were rtl_lps_enter() and rtl_lps_leave(). Each of them sets a variable to indicate whether enter_ps is true or false, and schedules the workqueue. In the workqueue's callback routine, the routines to start/stop ps mode are called. The code is in drivers/net/wireless/realtek/rtlwifi/ps.c.

This solution is only one of many, and there may be a better one.

Larry

#3 lore raw
Quoted 6 lines · Larry Finger

In drivers/net/wireless/realtek/rtlwifi, we had a similar problem. There it was handled by putting the lps_enter() and lps_leave() operations in a separate workqueue. In this case, the routines were rtl_lps_enter() and rtl_lps_leave(). Each of them sets a variable to indicate whether enter_ps is true or false, and schedules the workqueue. In the workqueue's callback routine, the routines to start/stop ps mode are called. The code is in drivers/net/wireless/realtek/rtlwifi/ps.c.

This solution is only one of many, and there may be a better one.

Larry

Thank you for the explanation Larry.

Michael

#6 lore raw
In reply to a message not in the archive
Quoted 38 lines · Fabio M. De Francesco
Quoted 37 lines · On domenica 3 aprile 2022 12:43:04 CEST Fabio M. De Francesco wrote:
Quoted 15 lines · On sabato 2 aprile 2022 22:47:27 CEST Michael Straube wrote:

Hi all,

smatch reported a sleeping in atomic context.

rtw_set_802_11_disassociate() <- disables preempt
-> _rtw_pwr_wakeup()

     -> ips_leave()

rtw_set_802_11_disassociate() takes a spinlock and ips_leave() uses a mutex.

I'm fairly new to the locking stuff, but as far as I know this is not a false positive since mutex can sleep, but that's not allowed under a spinlock.

Quoted 7 lines

What is the best way to handle this?
I'm not sure if converting the mutex to a spinlock (including all the other places where the mutex is used) is the right thing to do?

thanks,
Michael

Hi Michael,

No, this is a false positive: ips_leave is never called under spinlocks. Some time ago I blindly trusted Smatch and submitted a patch for what you are reporting just now again. Soon after submission I realized it and then I had to ask Greg to discard my patch.

Please read the related thread:

[PATCH] staging: r8188eu: Use kzalloc() with GFP_ATOMIC in atomic context
https://lore.kernel.org/lkml/[email protected]/

Thanks,

Quoted 8 lines
Quoted 1 line

Fabio

I'm sorry, the correct link is the following:
[PATCH v2 2/2] staging: r8188eu: Use kzalloc() with GFP_ATOMIC in atomic context
https://lore.kernel.org/lkml/[email protected]/

Fabio

Hi Fabio,

Ah I see now, thanks. Well, I think the code is not very clear and easy to follow here. Perhaps we should refactor this area someday to avoid future confusions.

regards,
Michael

#8 lore raw
In reply to a message not in the archive
Quoted 70 lines · Fabio M. De Francesco
Quoted 48 lines · On domenica 3 aprile 2022 13:08:35 CEST Michael Straube wrote:
Quoted 38 lines · Fabio M. De Francesco
Quoted 37 lines · On domenica 3 aprile 2022 12:43:04 CEST Fabio M. De Francesco wrote:
Quoted 15 lines · On sabato 2 aprile 2022 22:47:27 CEST Michael Straube wrote:

Hi all,

smatch reported a sleeping in atomic context.

rtw_set_802_11_disassociate() <- disables preempt
-> _rtw_pwr_wakeup()

      -> ips_leave()

rtw_set_802_11_disassociate() takes a spinlock and ips_leave() uses a mutex.

I'm fairly new to the locking stuff, but as far as I know this is not a false positive since mutex can sleep, but that's not allowed under a spinlock.

Quoted 7 lines

What is the best way to handle this?
I'm not sure if converting the mutex to a spinlock (including all the other places where the mutex is used) is the right thing to do?

thanks,
Michael

Hi Michael,

No, this is a false positive: ips_leave is never called under spinlocks. Some time ago I blindly trusted Smatch and submitted a patch for what you are reporting just now again. Soon after submission I realized it and then I had to ask Greg to discard my patch.

Please read the related thread:

[PATCH] staging: r8188eu: Use kzalloc() with GFP_ATOMIC in atomic context
https://lore.kernel.org/lkml/[email protected]/

Thanks,

Quoted 8 lines
Quoted 1 line

Fabio

I'm sorry, the correct link is the following:
[PATCH v2 2/2] staging: r8188eu: Use kzalloc() with GFP_ATOMIC in atomic context
https://lore.kernel.org/lkml/[email protected]/

Fabio

Quoted 9 lines

Hi Fabio,

Ah I see now, thanks. Well, I think the code is not very clear and easy to follow here. Perhaps we should refactor this area someday to avoid future confusions.

regards,
Michael

Soon after I sent the email above, I read yours anew. The issue I were trying to address were different than what you noticed today. I didn't even see that we were in nested mutexes under spinlocks and bottom halves disabled. I just saw those kmalloc() with GFP_KERNEL.

You are noticing something one layer up. And you are right, this is a real issue. Larry's suggestion is the only correct one for fixing this.

I've analyzed and reviewed some code in the tty layer that implements the same solution that Larry is talking about. Let me find the link and I'll soon send it to you, so that you might be inspired from that implementation.

Quoted 8 lines

Sorry for the confusion.

Thanks,

Fabio

Hi Fabio,

wait..

rtw_set_802_11_disassociate() calls rtw_pwr_wakeup() only if check_fwstate(pmlmepriv, _FW_LINKED) is true.

	if (check_fwstate(pmlmepriv, _FW_LINKED)) {
		rtw_disassoc_cmd(padapter, 0, true);
		rtw_indicate_disconnect(padapter);
		rtw_free_assoc_resources(padapter, 1);
		rtw_pwr_wakeup(padapter);
	}

in rtw_pwr_wakeup() there is the same check and if it is true the function returns before calling ips_leave().

	if (check_fwstate(pmlmepriv, _FW_LINKED)) {
		ret = _SUCCESS;
		goto exit;
	}
	if (rf_off == pwrpriv->rf_pwrstate) {
		if (_FAIL ==  ips_leave(padapter)) {
			ret = _FAIL;
			goto exit;
		}
	}

So ips_leave() is not called in atomic context in this case and smatch reports indeed a false positive, or am I wrong?

I have not checked the other calls to rtw_pwr_wakeup().

regards,
Michael

#9 lore raw
In reply to Michael Straube

Hi Michael,

Quoted 9 lines · Michael Straube

Hi Fabio,

wait..

rtw_set_802_11_disassociate() calls rtw_pwr_wakeup() only if check_fwstate(pmlmepriv, _FW_LINKED) is true.

Quoted 7 lines
	if (check_fwstate(pmlmepriv, _FW_LINKED)) {
		rtw_disassoc_cmd(padapter, 0, true);
		rtw_indicate_disconnect(padapter);
		rtw_free_assoc_resources(padapter, 1);
		rtw_pwr_wakeup(padapter);
	}

msleep() cannot be called in atomic context:

drivers/staging/r8188eu/core/rtw_pwrctrl.c:379

if (pwrpriv->ps_processing) {
while (pwrpriv->ps_processing && rtw_get_passing_time_ms(start) <= 3000)
msleep(10);
}

With regards,
Pavel Skripkin

#11 lore raw
In reply to a message not in the archive
Quoted 11 lines · Fabio M. De Francesco
Quoted 10 lines · On domenica 3 aprile 2022 13:48:31 CEST Pavel Skripkin wrote:

Hi Michael,

msleep() cannot be called in atomic context:

drivers/staging/r8188eu/core/rtw_pwrctrl.c:379

   	if (pwrpriv->ps_processing) {
   		while (pwrpriv->ps_processing && rtw_get_passing_time_ms(start) <= 3000)
   			msleep(10);
   	}

Ah, just another issue...

Quoted 6 lines

I wanted to use mdelay() (allowed under spinlocks because it does not sleep) but Dan said to leave it as it is. You might easily find this discussion on this list. I cannot remember why Dan was against replacing msleep() with mdelay(). Please try to find that thread.

I'll take a breath and look further into it later. Seems a solution like the one Larry mentioned is reasonable here.

Thank you both,
Michael

#25 lore raw
In reply to a message not in the archive
Quoted 9 lines · Fabio M. De Francesco

For a list of all the paths to a given function you may use Smatch:

./smatch/smatch_data/db/smdb.py ips_leave

or

./smatch/smatch_data/db/smdb.py call_tree ips_leave

But perhaps you already know how to do it.

Yes, but thank you anyway. :)

Michael

#12 lore raw
In reply to Pavel Skripkin
Quoted 18 lines · Pavel Skripkin
Quoted 9 lines · Michael Straube

Hi Fabio,

wait..

rtw_set_802_11_disassociate() calls rtw_pwr_wakeup() only if check_fwstate(pmlmepriv, _FW_LINKED) is true.

Quoted 7 lines
	if (check_fwstate(pmlmepriv, _FW_LINKED)) {
		rtw_disassoc_cmd(padapter, 0, true);
		rtw_indicate_disconnect(padapter);
		rtw_free_assoc_resources(padapter, 1);
		rtw_pwr_wakeup(padapter);
	}
Quoted 9 lines

msleep() cannot be called in atomic context:

drivers/staging/r8188eu/core/rtw_pwrctrl.c:379

   	if (pwrpriv->ps_processing) {
   		while (pwrpriv->ps_processing && rtw_get_passing_time_ms(start) <= 3000)
   			msleep(10);
   	}

Hm, just wondering, shouldn't we annotate load from pwrpriv->ps_processing with READ_ONCE() inside while loop?

IIUC compiler might want to cache first load into register and we will stuck here forever.

Am I missing something?

With regards,
Pavel Skripkin

#14 lore raw
In reply to a message not in the archive

Hi Fabio,

Quoted 9 lines · Fabio M. De Francesco
Quoted 9 lines
Quoted 8 lines

drivers/staging/r8188eu/core/rtw_pwrctrl.c:379

   	if (pwrpriv->ps_processing) {
   		while (pwrpriv->ps_processing && rtw_get_passing_time_ms(start) <= 3000)
   			msleep(10);
   	}
Quoted 6 lines
Quoted 2 lines

Hm, just wondering, shouldn't we annotate load from pwrpriv->ps_processing with READ_ONCE() inside while loop? IIUC compiler might want to cache first load into register and we will stuck here forever.

You're right. This can be cached. In situations like these one should use barriers or other API that use barriers implicitly (completions, for example).

Not sure about completions, since they may sleep.

Also, don't think that barriers are needed here, since this code just waiting for observing value 1. Might be barrier will slightly speed up waiting thread, but will also slow down other thread

Quoted 11 lines

In this list, a couple of weeks ago, I suggested someone to use them. He/she re-submitted his/her patches with wait_for_completion() / complete(), I placed my Reviewed-by tag and Greg was happy to accept it and merge.

Not sure about READ_ONCE(), because, as far as I know, it is used only to prevent load tearing. But I might be wrong here.

Fabio

P.S.: Is Paul's book still un-read? Unfortunately, I am not yet done :(

If I could read it 24/7 I will be the happiest person ever :) As you might expect I am not yet done as well

With regards,
Pavel Skripkin

#16 lore raw
In reply to a message not in the archive

Hi Fabio,

Quoted 22 lines · Fabio M. De Francesco
Quoted 12 lines · On domenica 3 aprile 2022 14:45:49 CEST Pavel Skripkin wrote:

Hi Fabio,

Quoted 9 lines · Fabio M. De Francesco
Quoted 9 lines
Quoted 8 lines

drivers/staging/r8188eu/core/rtw_pwrctrl.c:379

   	if (pwrpriv->ps_processing) {
   		while (pwrpriv->ps_processing && rtw_get_passing_time_ms(start) <= 3000)
   			msleep(10);
   	}
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Hm, just wondering, shouldn't we annotate load from pwrpriv->ps_processing with READ_ONCE() inside while loop? IIUC compiler might want to cache first load into register and we will stuck here forever.

You're right. This can be cached. In situations like these one should use barriers or other API that use barriers implicitly (completions, for example).

Not sure about completions, since they may sleep.

Quoted 9 lines

No completions in this special context. They for _sure_ might sleep. I was talking about general cases when you are in a loop and wait for status change.

Quoted 2 lines

Also, don't think that barriers are needed here, since this code just waiting for observing value 1. Might be barrier will slightly speed up waiting thread, but will also slow down other thread

Here, I cannot help with a 100% good answer. Maybe Greg wants to say something about it?

IMO, the best answer is just remove this loop, since it does nothing. Or redesign it to be more sane

It waits for ps_processing to become 0 for 3000 ms, but if 3000 ms expires... execution goes forward like as ps_processing was 0 from the beginning

Maybe it's something hw related, like wait for 3000 ms and all will be ok. Can't say...

With regards,
Pavel Skripkin

#17 lore raw
Quoted 34 lines · Pavel Skripkin

Hi Fabio,

Quoted 21 lines · Fabio M. De Francesco
Quoted 11 lines · On domenica 3 aprile 2022 14:45:49 CEST Pavel Skripkin wrote:

Hi Fabio,

Quoted 3 lines · Fabio M. De Francesco
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/staging/r8188eu/core/rtw_pwrctrl.c:379
        if (pwrpriv->ps_processing) {

            while (pwrpriv->ps_processing &&

rtw_get_passing_time_ms(start) <= 3000)

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

                msleep(10);
        }

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Hm, just wondering, shouldn't we annotate load from >>

pwrpriv->ps_processing with READ_ONCE() inside while loop?

Quoted 8 lines
Quoted 1 line
Quoted 1 line

IIUC compiler might want to cache first load into register and we

will >> stuck here forever.

Quoted 1 line
Quoted 1 line

You're right. This can be cached. In situations like these one

should use

Quoted 1 line

barriers or other API that use barriers implicitly (completions,

for example).

Quoted 1 line

Not sure about completions, since they may sleep.

Quoted 9 lines

No completions in this special context. They for _sure_ might sleep. I was talking about general cases when you are in a loop and wait for status change.

Quoted 2 lines

Also, don't think that barriers are needed here, since this code just waiting for observing value 1. Might be barrier will slightly speed up waiting thread, but will also slow down other thread

Here, I cannot help with a 100% good answer. Maybe Greg wants to say something about it?

Quoted 6 lines

IMO, the best answer is just remove this loop, since it does nothing. Or redesign it to be more sane

It waits for ps_processing to become 0 for 3000 ms, but if 3000 ms expires... execution goes forward like as ps_processing was 0 from the beginning

Maybe it's something hw related, like wait for 3000 ms and all will be ok. Can't say...

Hi Pavel,

same with the loop that follows:

	/* System suspend is not allowed to wakeup */
	if (pwrpriv->bInSuspend) {
		while (pwrpriv->bInSuspend &&
		       (rtw_get_passing_time_ms(start) <= 3000 ||
		       (rtw_get_passing_time_ms(start) <= 500)))
				msleep(10);
	}

I just waits 500ms if pwrpriv->bInSuspend is true. Additionaly the <= 3000 has no effect here because of the ored <= 500.

Even worse the comment seems misleading because pwrpriv->bInSuspend indicates usb autosuspend but not system suspend.

regards,
Michael

#18 lore raw

Hi Michael,

Quoted 8 lines · Michael Straube
Quoted 7 lines

IMO, the best answer is just remove this loop, since it does nothing. Or redesign it to be more sane

It waits for ps_processing to become 0 for 3000 ms, but if 3000 ms expires... execution goes forward like as ps_processing was 0 from the beginning

Maybe it's something hw related, like wait for 3000 ms and all will be ok. Can't say...

Quoted 6 lines

Hi Pavel,

same with the loop that follows:

	/* System suspend is not allowed to wakeup */
	if (pwrpriv->bInSuspend) {
	   ^^^^

btw, this part is useless to

Quoted 1 line
		while (pwrpriv->bInSuspend &&

I've looked into what gcc11 produced from this function and looks like my compiler is smart enough to not cache that value, but I am afraid not all compilers are that smart.

And looks like it will be better to wait on mutex_lock(&pwrpriv->lock); rather than odd loops. Ah, we can't wait here...

In first place, why this function cares about usb suspend callback?

I've got too many questions to that code... I'd better stop

Quoted 8 lines
		       (rtw_get_passing_time_ms(start) <= 3000 ||
		       (rtw_get_passing_time_ms(start) <= 500)))
				msleep(10);
	}

I just waits 500ms if pwrpriv->bInSuspend is true. Additionaly the <= 3000 has no effect here because of the ored <= 500.

Yeah, and unfortunately it won't be optimized out :(

Quoted 3 lines

Even worse the comment seems misleading because pwrpriv->bInSuspend indicates usb autosuspend but not system suspend.

With regards,
Pavel Skripkin

#19 lore raw

From: Pavel Skripkin

Quoted 26 lines

Sent: 03 April 2022 22:15

Hi Michael,

Quoted 12 lines · Michael Straube
Quoted 11 lines

IMO, the best answer is just remove this loop, since it does nothing. Or redesign it to be more sane

It waits for ps_processing to become 0 for 3000 ms, but if 3000 ms expires... execution goes forward like as ps_processing was 0 from the beginning

Maybe it's something hw related, like wait for 3000 ms and all will be ok. Can't say...

Quoted 6 lines

Hi Pavel,

same with the loop that follows:

	/* System suspend is not allowed to wakeup */
	if (pwrpriv->bInSuspend) {
	   ^^^^
Quoted 8 lines

btw, this part is useless to

Quoted 1 line
		while (pwrpriv->bInSuspend &&

I've looked into what gcc11 produced from this function and looks like my compiler is smart enough to not cache that value, but I am afraid not all compilers are that smart.

The compiler can't cache the value because of the function call.

Quite whether the code is in any way sane in another matter.

You definitely cannot sleep with a spinlock held. Imagine what happens if another process tries to acquire the spinlock while you are sleeping.
It will spin forever.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#26 lore raw
In reply to Michael Straube
Quoted 5 lines · Michael Straube
Quoted 4 lines · Fabio M. De Francesco

For a list of all the paths to a given function you may use Smatch:

./smatch/smatch_data/db/smdb.py ips_leave

Quoted 8 lines
Quoted 5 lines

or

./smatch/smatch_data/db/smdb.py call_tree ips_leave

But perhaps you already know how to do it.

Yes, but thank you anyway. :)

My email (gmail account) has been so weird recently. I don't know why I'm not getting Fabio's emails... Presumably they will show up in a few days.

The other command to use is:

$ ./smatch/smatch_data/db/smdb.py preempt ips_leave
rtw_set_802_11_disassociate() <- disables preempt
-> _rtw_pwr_wakeup()
   -> LeaveAllPowerSaveMode()
      -> ips_leave()

I save that to a file, open it with with vim and run a vim function hall (for highlight all) from my .vimrc file.

" Use :hall to highlight all the words in a file (for debugging sleeping bugs) function HLall()
let a=[]
%s/\w\+/\=add(a, submatch(0))/gn
let @/ = join(a, "\\|")
endfunction
cnoreabbrev hall call HLall() <CR>:set hls<CR>

That highlights all the functions is the call tree, then I use cscope to jump to rtw_set_802_11_disassociate and follow the highlighted functions to ips_leave().

regards,
dan carpenter

#27 lore raw
Quoted 38 lines · Dan Carpenter
Quoted 5 lines · Michael Straube
Quoted 4 lines · Fabio M. De Francesco

For a list of all the paths to a given function you may use Smatch:

./smatch/smatch_data/db/smdb.py ips_leave

Quoted 8 lines
Quoted 5 lines

or

./smatch/smatch_data/db/smdb.py call_tree ips_leave

But perhaps you already know how to do it.

Yes, but thank you anyway. :)

My email (gmail account) has been so weird recently. I don't know why I'm not getting Fabio's emails... Presumably they will show up in a few days.

The other command to use is:

$ ./smatch/smatch_data/db/smdb.py preempt ips_leave
rtw_set_802_11_disassociate() <- disables preempt
-> _rtw_pwr_wakeup()
    -> LeaveAllPowerSaveMode()
       -> ips_leave()

I save that to a file, open it with with vim and run a vim function hall (for highlight all) from my .vimrc file.

" Use :hall to highlight all the words in a file (for debugging sleeping bugs) function HLall()
let a=[]
%s/\w\+/\=add(a, submatch(0))/gn
let @/ = join(a, "\\|")
endfunction
cnoreabbrev hall call HLall() <CR>:set hls<CR>

Quoted 3 lines

That highlights all the functions is the call tree, then I use cscope to jump to rtw_set_802_11_disassociate and follow the highlighted functions to ips_leave().

Thank you for this hint Dan. :)

thanks,
Michael

#20 lore raw
In reply to David Laight

Hi David,

Quoted 9 lines · David Laight
Quoted 6 lines
Quoted 2 lines
		while (pwrpriv->bInSuspend &&

I've looked into what gcc11 produced from this function and looks like my compiler is smart enough to not cache that value, but I am afraid not all compilers are that smart.

The compiler can't cache the value because of the function call.

Hm, I am a newbie in compilers, so can you, please, explain (or give a link to any resource where I can read about it) how function call here prevent caching.

IIUC compiler generates code that works well in scope of single-threaded application, so why can't compiler cache that value instead of accessing memory on each iteration... Isn't register access a way faster than even cache hit?

Thanks!

With regards,
Pavel Skripkin

#21 lore raw

From: Pavel Skripkin

Quoted 15 lines

Sent: 04 April 2022 17:39

Hi David,

Quoted 9 lines · David Laight
Quoted 6 lines
Quoted 2 lines
		while (pwrpriv->bInSuspend &&

I've looked into what gcc11 produced from this function and looks like my compiler is smart enough to not cache that value, but I am afraid not all compilers are that smart.

The compiler can't cache the value because of the function call.

Quoted 8 lines

Hm, I am a newbie in compilers, so can you, please, explain (or give a link to any resource where I can read about it) how function call here prevent caching.

IIUC compiler generates code that works well in scope of single-threaded application, so why can't compiler cache that value instead of accessing memory on each iteration... Isn't register access a way faster than even cache hit?

Because calls to external functions are allowed to change any data via 'other' references.
For instance the structure pointer the function has could also be in global data somewhere.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#22 lore raw

Hi David,

Quoted 25 lines · David Laight

From: Pavel Skripkin

Quoted 15 lines

Sent: 04 April 2022 17:39

Hi David,

Quoted 9 lines · David Laight
Quoted 6 lines
Quoted 2 lines
		while (pwrpriv->bInSuspend &&

I've looked into what gcc11 produced from this function and looks like my compiler is smart enough to not cache that value, but I am afraid not all compilers are that smart.

The compiler can't cache the value because of the function call.

Quoted 8 lines

Hm, I am a newbie in compilers, so can you, please, explain (or give a link to any resource where I can read about it) how function call here prevent caching.

IIUC compiler generates code that works well in scope of single-threaded application, so why can't compiler cache that value instead of accessing memory on each iteration... Isn't register access a way faster than even cache hit?

Quoted 5 lines

Because calls to external functions are allowed to change any data via 'other' references.
For instance the structure pointer the function has could also be in global data somewhere.

Make sense, thank you for explanation!

With regards,
Pavel Skripkin