From: "Wang, Haiyue" <haiyue.wang@linux.intel.com>
To: minyard@acm.org, joel@jms.id.au, avifishman70@gmail.com,
openbmc@lists.ozlabs.org,
openipmi-developer@lists.sourceforge.net,
linux-kernel@vger.kernel.org
Cc: andriy.shevchenko@intel.com
Subject: Re: [PATCH arm/aspeed/ast2500 v2] ipmi: add an Aspeed KCS IPMI BMC driver
Date: Fri, 26 Jan 2018 14:26:04 +0800 [thread overview]
Message-ID: <9b194ad6-d277-44e5-54b0-c7e453a6fb0c@linux.intel.com> (raw)
In-Reply-To: <f20da565-6644-3ea4-823b-a808cf613958@gmail.com>
On 2018-01-25 01:48, Corey Minyard wrote:
> On 01/24/2018 10:06 AM, Haiyue Wang wrote:
>> The KCS (Keyboard Controller Style) interface is used to perform in-band
>> IPMI communication between a server host and its BMC (BaseBoard
>> Management
>> Controllers).
>>
>> This driver exposes the KCS interface on ASpeed SOCs (AST2400 and
>> AST2500)
>> as a character device. Such SOCs are commonly used as BMCs and this
>> driver
>> implements the BMC side of the KCS interface.
>>
>> Signed-off-by: Haiyue Wang <haiyue.wang@linux.intel.com>
>>
>> ---
>
>> +
>> +static ssize_t kcs_bmc_read(struct file *filp, char *buf,
>> + size_t count, loff_t *offset)
>> +{
>> + struct kcs_bmc *kcs_bmc = file_kcs_bmc(filp);
>> + ssize_t ret = -EAGAIN;
>> +
>
> This function still has some issues.
>
> You can't call copy_to_user() with a spinlock held or interrupts
> disabled.
> To handle readers, you probably need a separate mutex.
>
> Also, this function can return -EAGAIN even if O_NONBLOCK is not set if
> kcs_bmc->data_in_avail changes between when you wait on the event
> and when you check it under the lock.
>
> You also clear data_in_avail even if the copy_to_user() fails, which is
> wrong.
>
> I believe the best way to handle this would be to have the spinlock
> protect the inner workings of the state machine and a mutex handle
> copying data out, setting/clearing the running flag (thus a mutex
> instead of spinlock in open and release) and the ioctl settings (except
> for abort where you will need to grab the spinlock).
>
> After the wait event below, grab the mutex. If data is not available
> and O_NONBLOCK is not set, drop the mutex and retry. Otherwise
> this is the only place (besides release) that sets data_in_avail to
> false.
> Do the copy_to_user(), grab the spinlock, clear data_in_avail and
> data_in_idx, then release the lock and mutex. If you are really
> adventurous you can do this without grabbing the lock using
> barriers, but it's probably not necessary here.
>
The main race is data_in and data_out memory copy from & to between one
user-land (ipmid) and
the irq handler. If separates the copy_to_user into two parts: check the
'access_ok(VERIFY_WRITE, to, n)',
if no errors, then grap the spinlock and irq disabled, then
'memcpy((void __force *)to, from, n);' It it right
calling ?
I will add a mutex to avoid spinlcok using as possible.
>> + if (!(filp->f_flags & O_NONBLOCK))
>> + wait_event_interruptible(kcs_bmc->queue,
>> + kcs_bmc->data_in_avail);
>> +
>> + spin_lock_irq(&kcs_bmc->lock);
>> +
>> + if (kcs_bmc->data_in_avail) {
>> + kcs_bmc->data_in_avail = false;
>> +
>> + if (count > kcs_bmc->data_in_idx)
>> + count = kcs_bmc->data_in_idx;
>> +
>> + if (!copy_to_user(buf, kcs_bmc->data_in, count))
>> + ret = count;
>> + else
>> + ret = -EFAULT;
>> + }
>> +
>> + spin_unlock_irq(&kcs_bmc->lock);
>> +
>> + return ret;
>> +}
>> +
>
>> + }
>> +
>> + spin_unlock_irq(&kcs_bmc->lock);
>> +
>> + return ret;
>> +}
>
prev parent reply other threads:[~2018-01-26 6:26 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-24 16:06 [PATCH arm/aspeed/ast2500 v2] ipmi: add an Aspeed KCS IPMI BMC driver Haiyue Wang
2018-01-24 17:05 ` Andy Shevchenko
2018-01-26 5:33 ` Wang, Haiyue
2018-01-24 17:48 ` Corey Minyard
2018-01-26 6:08 ` Wang, Haiyue
2018-01-26 14:48 ` Corey Minyard
2018-01-29 13:57 ` Wang, Haiyue
2018-01-30 13:49 ` Corey Minyard
2018-01-31 0:02 ` Wang, Haiyue
2018-01-31 0:52 ` Corey Minyard
2018-01-31 1:02 ` Wang, Haiyue
2018-01-31 1:25 ` Corey Minyard
2018-01-31 1:37 ` Wang, Haiyue
2018-01-31 1:52 ` Corey Minyard
2018-01-31 2:01 ` Wang, Haiyue
2018-01-26 6:26 ` Wang, Haiyue [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=9b194ad6-d277-44e5-54b0-c7e453a6fb0c@linux.intel.com \
--to=haiyue.wang@linux.intel.com \
--cc=andriy.shevchenko@intel.com \
--cc=avifishman70@gmail.com \
--cc=joel@jms.id.au \
--cc=linux-kernel@vger.kernel.org \
--cc=minyard@acm.org \
--cc=openbmc@lists.ozlabs.org \
--cc=openipmi-developer@lists.sourceforge.net \
/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;
as well as URLs for NNTP newsgroup(s).