linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] staging: rtl8192u: Fix sleep in atomic context bug in dm_fsync_timer_callback
@ 2022-05-20  6:15 Duoming Zhou
  2022-06-06  5:43 ` Greg KH
  0 siblings, 1 reply; 4+ messages in thread
From: Duoming Zhou @ 2022-05-20  6:15 UTC (permalink / raw)
  To: linux-staging, gregkh
  Cc: davem, kuba, alexander.deucher, broonie, linux-kernel, Duoming Zhou

There are sleep in atomic context bugs when dm_fsync_timer_callback is
executing. The root cause is that the memory allocation functions with
GFP_KERNEL parameter are called in dm_fsync_timer_callback which is a
timer handler. The call paths that could trigger bugs are shown below:

    (interrupt context)
dm_fsync_timer_callback
  write_nic_byte
    kzalloc(sizeof(data), GFP_KERNEL); //may sleep
  write_nic_dword
    kzalloc(sizeof(data), GFP_KERNEL); //may sleep

This patch changes allocation mode from GFP_KERNEL to GFP_ATOMIC
in order to prevent atomic context sleeping. The GFP_ATOMIC flag
makes memory allocation operation could be used in atomic context.

Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
---
 drivers/staging/rtl8192u/r8192U_core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/staging/rtl8192u/r8192U_core.c b/drivers/staging/rtl8192u/r8192U_core.c
index ce807c9d421..679c362baad 100644
--- a/drivers/staging/rtl8192u/r8192U_core.c
+++ b/drivers/staging/rtl8192u/r8192U_core.c
@@ -267,7 +267,7 @@ int write_nic_byte(struct net_device *dev, int indx, u8 data)
 
 	struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
 	struct usb_device *udev = priv->udev;
-	u8 *usbdata = kzalloc(sizeof(data), GFP_KERNEL);
+	u8 *usbdata = kzalloc(sizeof(data), GFP_ATOMIC);
 
 	if (!usbdata)
 		return -ENOMEM;
@@ -319,7 +319,7 @@ int write_nic_dword(struct net_device *dev, int indx, u32 data)
 
 	struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
 	struct usb_device *udev = priv->udev;
-	u32 *usbdata = kzalloc(sizeof(data), GFP_KERNEL);
+	u32 *usbdata = kzalloc(sizeof(data), GFP_ATOMIC);
 
 	if (!usbdata)
 		return -ENOMEM;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] staging: rtl8192u: Fix sleep in atomic context bug in dm_fsync_timer_callback
  2022-05-20  6:15 [PATCH] staging: rtl8192u: Fix sleep in atomic context bug in dm_fsync_timer_callback Duoming Zhou
@ 2022-06-06  5:43 ` Greg KH
  2022-06-06  7:11   ` duoming
  0 siblings, 1 reply; 4+ messages in thread
From: Greg KH @ 2022-06-06  5:43 UTC (permalink / raw)
  To: Duoming Zhou
  Cc: linux-staging, davem, kuba, alexander.deucher, broonie, linux-kernel

On Fri, May 20, 2022 at 02:15:41PM +0800, Duoming Zhou wrote:
> There are sleep in atomic context bugs when dm_fsync_timer_callback is
> executing. The root cause is that the memory allocation functions with
> GFP_KERNEL parameter are called in dm_fsync_timer_callback which is a
> timer handler. The call paths that could trigger bugs are shown below:
> 
>     (interrupt context)
> dm_fsync_timer_callback
>   write_nic_byte
>     kzalloc(sizeof(data), GFP_KERNEL); //may sleep
>   write_nic_dword
>     kzalloc(sizeof(data), GFP_KERNEL); //may sleep
> 
> This patch changes allocation mode from GFP_KERNEL to GFP_ATOMIC
> in order to prevent atomic context sleeping. The GFP_ATOMIC flag
> makes memory allocation operation could be used in atomic context.
> 
> Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>

What commit id does this fix?

And how did you find this issue?  Did you run the code to verify it
still works properly?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] staging: rtl8192u: Fix sleep in atomic context bug in dm_fsync_timer_callback
  2022-06-06  5:43 ` Greg KH
@ 2022-06-06  7:11   ` duoming
  2022-06-07 11:01     ` Greg KH
  0 siblings, 1 reply; 4+ messages in thread
From: duoming @ 2022-06-06  7:11 UTC (permalink / raw)
  To: Greg KH
  Cc: linux-staging, davem, kuba, alexander.deucher, broonie, linux-kernel

Hello,

On Mon, 6 Jun 2022 07:43:11 +0200 greg k-h wrote:

> On Fri, May 20, 2022 at 02:15:41PM +0800, Duoming Zhou wrote:
> > There are sleep in atomic context bugs when dm_fsync_timer_callback is
> > executing. The root cause is that the memory allocation functions with
> > GFP_KERNEL parameter are called in dm_fsync_timer_callback which is a
> > timer handler. The call paths that could trigger bugs are shown below:
> > 
> >     (interrupt context)
> > dm_fsync_timer_callback
> >   write_nic_byte
> >     kzalloc(sizeof(data), GFP_KERNEL); //may sleep
> >   write_nic_dword
> >     kzalloc(sizeof(data), GFP_KERNEL); //may sleep
> > 
> > This patch changes allocation mode from GFP_KERNEL to GFP_ATOMIC
> > in order to prevent atomic context sleeping. The GFP_ATOMIC flag
> > makes memory allocation operation could be used in atomic context.
> > 
> > Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> 
> What commit id does this fix?

Thanks for your time and reply! 
The commit id this patch fix is 8fc8598e61f6 ("Staging: Added Realtek rtl8192u driver to staging").
 
> And how did you find this issue?  Did you run the code to verify it
> still works properly?

I find this issue by writing codeql query. I am trying to use usb raw-gadget to simulate
rtl8192u card in order to test this code.

What`s more, I found the usb_control_msg() with GFP_NOIO parameter in write_nic_byte() 
and write_nic_dword() may also sleep. So I think use the delayed queue to replace timer
is better. 

Best regards,
Duoming Zhou



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] staging: rtl8192u: Fix sleep in atomic context bug in dm_fsync_timer_callback
  2022-06-06  7:11   ` duoming
@ 2022-06-07 11:01     ` Greg KH
  0 siblings, 0 replies; 4+ messages in thread
From: Greg KH @ 2022-06-07 11:01 UTC (permalink / raw)
  To: duoming
  Cc: linux-staging, davem, kuba, alexander.deucher, broonie, linux-kernel

On Mon, Jun 06, 2022 at 03:11:39PM +0800, duoming@zju.edu.cn wrote:
> Hello,
> 
> On Mon, 6 Jun 2022 07:43:11 +0200 greg k-h wrote:
> 
> > On Fri, May 20, 2022 at 02:15:41PM +0800, Duoming Zhou wrote:
> > > There are sleep in atomic context bugs when dm_fsync_timer_callback is
> > > executing. The root cause is that the memory allocation functions with
> > > GFP_KERNEL parameter are called in dm_fsync_timer_callback which is a
> > > timer handler. The call paths that could trigger bugs are shown below:
> > > 
> > >     (interrupt context)
> > > dm_fsync_timer_callback
> > >   write_nic_byte
> > >     kzalloc(sizeof(data), GFP_KERNEL); //may sleep
> > >   write_nic_dword
> > >     kzalloc(sizeof(data), GFP_KERNEL); //may sleep
> > > 
> > > This patch changes allocation mode from GFP_KERNEL to GFP_ATOMIC
> > > in order to prevent atomic context sleeping. The GFP_ATOMIC flag
> > > makes memory allocation operation could be used in atomic context.
> > > 
> > > Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> > 
> > What commit id does this fix?
> 
> Thanks for your time and reply! 
> The commit id this patch fix is 8fc8598e61f6 ("Staging: Added Realtek rtl8192u driver to staging").
>  
> > And how did you find this issue?  Did you run the code to verify it
> > still works properly?
> 
> I find this issue by writing codeql query. I am trying to use usb raw-gadget to simulate
> rtl8192u card in order to test this code.
> 
> What`s more, I found the usb_control_msg() with GFP_NOIO parameter in write_nic_byte() 
> and write_nic_dword() may also sleep. So I think use the delayed queue to replace timer
> is better. 

Please explain all of this in the changelog text when you resend this.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-06-07 11:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-20  6:15 [PATCH] staging: rtl8192u: Fix sleep in atomic context bug in dm_fsync_timer_callback Duoming Zhou
2022-06-06  5:43 ` Greg KH
2022-06-06  7:11   ` duoming
2022-06-07 11:01     ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).