Linux wireless drivers development
 help / color / mirror / Atom feed
From: duoming@zju.edu.cn
To: "Kalle Valo" <kvalo@kernel.org>
Cc: "Jeff Johnson" <quic_jjohnson@quicinc.com>,
	linux-kernel@vger.kernel.org, amitkarwar@gmail.com,
	ganapathi017@gmail.com, sharvari.harisangam@nxp.com,
	huxinming820@gmail.com, davem@davemloft.net, edumazet@google.com,
	kuba@kernel.org, pabeni@redhat.com,
	linux-wireless@vger.kernel.org, netdev@vger.kernel.org
Subject: Re: [PATCH net v2] net: wireless: marvell: mwifiex: fix sleep in atomic context bugs
Date: Sat, 21 May 2022 21:59:46 +0800 (GMT+08:00)	[thread overview]
Message-ID: <4cd03c34.25fad.180e6eac2d7.Coremail.duoming@zju.edu.cn> (raw)
In-Reply-To: <87ilpzwg3e.fsf@kernel.org>

Hello,

On Sat, 21 May 2022 09:32:37 +0300 Kalle Valo wrote:

> >> >>>>> There are sleep in atomic context bugs when uploading device dump
> >> >>>>> data on usb interface. The root cause is that the operations that
> >> >>>>> may sleep are called in fw_dump_timer_fn which is a timer handler.
> >> >>>>> The call tree shows the execution paths that could lead to bugs:
> >> >>>>>
> >> >>>>>      (Interrupt context)
> >> >>>>> fw_dump_timer_fn
> >> >>>>>     mwifiex_upload_device_dump
> >> >>>>>       dev_coredumpv(..., GFP_KERNEL)
> >> >>
> >> >> just looking at this description, why isn't the simple fix just to
> >> >> change this call to use GFP_ATOMIC?
> >> > 
> >> > Because change the parameter of dev_coredumpv() to GFP_ATOMIC could only solve
> >> > partial problem. The following GFP_KERNEL parameters are in /lib/kobject.c
> >> > which is not influenced by dev_coredumpv().
> >> > 
> >> >   kobject_set_name_vargs
> >> >     kvasprintf_const(GFP_KERNEL, ...); //may sleep
> >> >     kstrdup(s, GFP_KERNEL); //may sleep
> >> 
> >> Then it seems there is a problem with dev_coredumpm().
> >> 
> >> dev_coredumpm() takes a gfp param which means it expects to be called in 
> >> any context, but it then calls dev_set_name() which, as you point out, 
> >> cannot be called from an atomic context.
> >> 
> >> So if we cannot change the fact that dev_set_name() cannot be called 
> >> from an atomic context, then it would seem to follow that 
> >> dev_coredumpv also cannot be called from an atomic 
> >> context and hence their gfp param is pointless and should presumably be 
> >> removed.
> >
> > Thanks for your time and suggestions! I think the gfp_t parameter of dev_coredumpv and
> > dev_coredumpm may not be removed, because it could be used to pass value to gfp_t
> > parameter of kzalloc in dev_coredumpm. What's more, there are also many other places
> > use dev_coredumpv and dev_coredumpm, if we remove the gfp_t parameter, there are too many
> > places that need to modify and these places are not in interrupt
> > context.
> 
> "Too many users" is not a valid reason to leave a bug in place, either
> dev_coredumpv() should support GFP_ATOMIC or the gfp_t parameter should
> be removed.

The following is one method that letting dev_coredump() support GFP_ATOMIC:

I think dev_set_name() is used to allocate memory to set a device name, 
which need only several bytes and there is little chance to sleep in the
real world. However the dev_coredumpv() is used to allocate memory to store
device coredump, which need lots of memory space and have more chances
to sleep. So I think only change the gfp_t parameter of dev_coredumpv()
from GFP_KERNEL to GFP_ATOMIC is ok.

diff --git a/drivers/net/wireless/marvell/mwifiex/main.c b/drivers/net/wireless/marvell/mwifiex/main.c
index ace7371c477..258906920a2 100644
--- a/drivers/net/wireless/marvell/mwifiex/main.c
+++ b/drivers/net/wireless/marvell/mwifiex/main.c
@@ -1116,7 +1116,7 @@ void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter)
        mwifiex_dbg(adapter, MSG,
                    "== mwifiex dump information to /sys/class/devcoredump start\n");
        dev_coredumpv(adapter->dev, adapter->devdump_data, adapter->devdump_len,
-                     GFP_KERNEL);
+                     GFP_ATOMIC);
        mwifiex_dbg(adapter, MSG,
                    "== mwifiex dump information to /sys/class/devcoredump end\n");

> > There are two solutions now: one is to moves the operations that may
> > sleep into a work item.
> 
> That does not fix the root cause that dev_coredumpv() claims it can be
> called in atomic contexts.

I agree with you. There is not GFP_ATOMIC in lib/kobject.c. Should we modify
the gfp_t parameter in kobject.c in order to support atomic contexts? Do you have
any other good methods?

> > Another is to change the gfp_t parameter of dev_coredumpv from GFP_KERNEL to GFP_ATOMIC, and
> > change the gfp_t parameter of kvasprintf_const and kstrdup from GFP_KERNEL to 
> > "in_interrupt() ? GFP_ATOMIC : GFP_KERNEL".
> 
> in_interrupt() is deprecated and should not be used. And I don't think
> it detects all atomic contexts like spinlocks.

I agree with you, the in_interrupt() is not proper.

Thanks for your time and suggestions!

Best regards,
Duoming Zhou

      reply	other threads:[~2022-05-21 14:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-19 13:53 [PATCH net v2] net: wireless: marvell: mwifiex: fix sleep in atomic context bugs Duoming Zhou
2022-05-19 14:58 ` Kalle Valo
2022-05-19 15:14   ` duoming
2022-05-19 15:48     ` Jeff Johnson
2022-05-20  0:08       ` duoming
2022-05-20 16:08         ` Jeff Johnson
2022-05-21  3:21           ` duoming
2022-05-21  6:32             ` Kalle Valo
2022-05-21 13:59               ` duoming [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4cd03c34.25fad.180e6eac2d7.Coremail.duoming@zju.edu.cn \
    --to=duoming@zju.edu.cn \
    --cc=amitkarwar@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=ganapathi017@gmail.com \
    --cc=huxinming820@gmail.com \
    --cc=kuba@kernel.org \
    --cc=kvalo@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=quic_jjohnson@quicinc.com \
    --cc=sharvari.harisangam@nxp.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox