From: Jean Delvare <jdelvare@suse.de>
To: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: Wolfram Sang <wsa@the-dreams.de>,
linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] i2c: make sure i2c_master_send/recv return negative error codes
Date: Thu, 20 Apr 2017 12:41:43 +0200 [thread overview]
Message-ID: <20170420124143.606ae032@endymion> (raw)
In-Reply-To: <20170401175435.GA12354@dtor-ws>
Hi Dmirty,
On Sat, 1 Apr 2017 10:54:35 -0700, Dmitry Torokhov wrote:
> There is theoretical possibility that i2c_master_send() and
> i2c_master_recv() may return non-negative result on error: we pass
> return values from i2c_xfer() unmodified to the caller, unless we
> transferred exactly 1 message. Let's ensure we always return negative on
> error.
>
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
> drivers/i2c/i2c-core.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
> index 6efeba42d10b..34b0482333f4 100644
> --- a/drivers/i2c/i2c-core.c
> +++ b/drivers/i2c/i2c-core.c
> @@ -2835,7 +2835,10 @@ int i2c_master_send(const struct i2c_client *client, const void *buf, int count)
> * If everything went ok (i.e. 1 msg transmitted), return #bytes
> * transmitted, else error code.
> */
> - return (ret == 1) ? count : ret;
> + if (likely(ret == 1))
> + return count;
> +
> + return ret < 0 ? ret : -EIO;
> }
> EXPORT_SYMBOL(i2c_master_send);
>
> @@ -2865,7 +2868,10 @@ int i2c_master_recv(const struct i2c_client *client, void *buf, int count)
> * If everything went ok (i.e. 1 msg received), return #bytes received,
> * else error code.
> */
> - return (ret == 1) ? count : ret;
> + if (likely(ret == 1))
> + return count;
> +
> + return ret < 0 ? ret : -EIO;
> }
> EXPORT_SYMBOL(i2c_master_recv);
>
I'm not convinced.
Firstly, that would be a device driver bug, and I can't see how
silently working around it here helps. If a driver is broken, it should be
fixed. So I would expect a log message.
Secondly, I believe i2c_master_send() and i2c_master_recv() should be
able to trust the return value of i2c_transfer(), which in turn should
be able to trust the return value of __i2c_transfer(). If you really
want to check the value returned by i2c_algo->master_xfer() for
validity, this should be done in __i2c_transfer(). But then again, I
find it hard to justify the run-time overhead for working drivers, so
maybe it should only be done if CONFIG_I2C_DEBUG_BUS is enabled.
--
Jean Delvare
SUSE L3 Support
--
Jean Delvare
SUSE L3 Support
--
Jean Delvare
SUSE L3 Support
prev parent reply other threads:[~2017-04-20 10:41 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-01 17:54 [PATCH] i2c: make sure i2c_master_send/recv return negative error codes Dmitry Torokhov
2017-04-20 10:41 ` Jean Delvare [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=20170420124143.606ae032@endymion \
--to=jdelvare@suse.de \
--cc=dmitry.torokhov@gmail.com \
--cc=linux-i2c@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=wsa@the-dreams.de \
/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).