From: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
To: Ming Yu <a0282524688@gmail.com>
Cc: tmyu0@nuvoton.com, lee@kernel.org, linus.walleij@linaro.org,
brgl@bgdev.pl, andi.shyti@kernel.org, mkl@pengutronix.de,
andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com,
kuba@kernel.org, pabeni@redhat.com, wim@linux-watchdog.org,
linux@roeck-us.net, jdelvare@suse.com,
alexandre.belloni@bootlin.com, linux-kernel@vger.kernel.org,
linux-gpio@vger.kernel.org, linux-i2c@vger.kernel.org,
linux-can@vger.kernel.org, netdev@vger.kernel.org,
linux-watchdog@vger.kernel.org, linux-hwmon@vger.kernel.org,
linux-rtc@vger.kernel.org, linux-usb@vger.kernel.org
Subject: Re: [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support
Date: Wed, 15 Jan 2025 12:36:13 +0900 [thread overview]
Message-ID: <0ccedc84-d429-4d6f-bd21-7487e6a0fbf9@wanadoo.fr> (raw)
In-Reply-To: <CAOoeyxVST6rEqp65rU6ZgmM-rSkAdeUVM=0nTLZYrqiO4DbQOA@mail.gmail.com>
On 15/01/2025 at 11:11, Ming Yu wrote:
> Vincent Mailhol <mailhol.vincent@wanadoo.fr> 於 2025年1月14日 週二 下午11:12寫道:
>>
> ...
>>>>> +static int nct6694_can_get_berr_counter(const struct net_device *ndev,
>>>>> + struct can_berr_counter *bec)
>>>>> +{
>>>>> + struct nct6694_can_priv *priv = netdev_priv(ndev);
>>>>> + struct nct6694_can_event *evt = priv->rx->event;
>>>>> + struct nct6694_cmd_header cmd_hd;
>>>>> + u8 mask = NCT6694_CAN_EVENT_REC | NCT6694_CAN_EVENT_TEC;
>>>>> + int ret;
>>>>> +
>>>>> + guard(mutex)(&priv->lock);
>>>>> +
>>>>> + cmd_hd = (struct nct6694_cmd_header) {
>>>>> + .mod = NCT6694_CAN_MOD,
>>>>> + .cmd = NCT6694_CAN_EVENT,
>>>>> + .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, mask),
>>>>> + .len = cpu_to_le16(sizeof(priv->rx->event))
>>>>> + };
>>>>> +
>>>>> + ret = nct6694_read_msg(priv->nct6694, &cmd_hd, evt);
>>>>> + if (ret < 0)
>>>>> + return ret;
>>>>
>>>> You are holding the priv->lock mutex before calling
>>>> nct6694_read_msg(). But nct6694_read_msg() then holds the
>>>> nct6694->access_lock mutex. Why do you need a double mutex here? What
>>>> kind of race scenario are you trying to prevent here?
>>>>
>>>
>>> I think priv->lock need to be placed here to prevent priv->rx from
>>> being assigned by other functions, and nct6694->access_lock ensures
>>> that the nct6694_read_msg() transaction is completed.
>>> But in this case, cmd_hd does not need to be in priv->lock's scope.
>>
>> So, the only reason for holding priv->lock is because priv->rx is shared
>> between functions.
>>
>> struct nct6694_can_event is only 8 bytes. And you only need it for the
>> life time of the function so it can simply be declared on the stack:
>>
>> struct nct6694_can_event evt;
>>
>> and with this, no more need to hold the lock. And the same thing also
>> applies to the other functions.
>>
>> Here, by trying to optimize the memory for only a few bytes, you are
>> getting a huge penalty on the performance by putting locks on all the
>> functions. This is not a good tradeoff.
>>
>
> Since nct6694_read_msg()/nct6694_write_msg() process URBs via
> usb_bulk_msg(), the transferred data must not be located on the stack.
> For more details about allocating buffers for transmitting data,
> please refer to the link:
> https://lore.kernel.org/linux-can/20241028-observant-gentle-doberman-0a2baa-mkl@pengutronix.de/
Ack, I forgot that you can not use stack memory in usb_bulk_msg().
Then, instead, you can either:
- do a dynamic memory allocation directly in the function (good for
when you are outside of the hot path, for example struct
nct6694_can_setting)
- and for the other structures which are part of the hot path
(typically struct nct6694_can_frame) continue to use a dynamically
allocated buffer stored in your priv but change the type of
nct6694_can_tx and nct6694_can_rx from union to structures.
And no more overlaps, thus no more need for the mutex.
Yours sincerely,
Vincent Mailhol
next prev parent reply other threads:[~2025-01-15 3:36 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-14 3:30 [PATCH v5 0/7] Add Nuvoton NCT6694 MFD drivers Ming Yu
2025-01-14 3:30 ` [PATCH v5 1/7] mfd: Add core driver for Nuvoton NCT6694 Ming Yu
2025-01-15 16:04 ` Lee Jones
2025-01-16 2:28 ` Ming Yu
2025-01-14 3:30 ` [PATCH v5 2/7] gpio: Add Nuvoton NCT6694 GPIO support Ming Yu
2025-01-14 12:12 ` Bartosz Golaszewski
2025-01-15 7:05 ` Ming Yu
2025-01-14 3:30 ` [PATCH v5 3/7] i2c: Add Nuvoton NCT6694 I2C support Ming Yu
2025-01-14 3:30 ` [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support Ming Yu
2025-01-14 8:05 ` Vincent Mailhol
2025-01-14 10:46 ` Ming Yu
2025-01-14 15:11 ` Vincent Mailhol
2025-01-15 2:11 ` Ming Yu
2025-01-15 3:36 ` Vincent Mailhol [this message]
2025-01-15 5:35 ` Ming Yu
2025-01-15 6:43 ` Vincent Mailhol
2025-01-15 7:03 ` Ming Yu
2025-01-15 16:45 ` Vincent Mailhol
2025-01-16 6:34 ` Ming Yu
2025-01-16 11:23 ` Vincent Mailhol
2025-01-14 3:30 ` [PATCH v5 5/7] watchdog: Add Nuvoton NCT6694 WDT support Ming Yu
2025-01-14 3:30 ` [PATCH v5 6/7] hwmon: Add Nuvoton NCT6694 HWMON support Ming Yu
2025-01-14 3:30 ` [PATCH v5 7/7] rtc: Add Nuvoton NCT6694 RTC support Ming Yu
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=0ccedc84-d429-4d6f-bd21-7487e6a0fbf9@wanadoo.fr \
--to=mailhol.vincent@wanadoo.fr \
--cc=a0282524688@gmail.com \
--cc=alexandre.belloni@bootlin.com \
--cc=andi.shyti@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=brgl@bgdev.pl \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=jdelvare@suse.com \
--cc=kuba@kernel.org \
--cc=lee@kernel.org \
--cc=linus.walleij@linaro.org \
--cc=linux-can@vger.kernel.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-i2c@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rtc@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=linux-watchdog@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=mkl@pengutronix.de \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=tmyu0@nuvoton.com \
--cc=wim@linux-watchdog.org \
/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
all inboxes | Powered by JetHome®