[{"id":1772820,"web_url":"http://patchwork.ozlabs.org/comment/1772820/","msgid":"<20170921145922.000017b5@huawei.com>","list_archive_url":null,"date":"2017-09-21T13:59:22","subject":"Re: [RFC PATCH v5 2/6] i2c: add helpers to ease DMA handling","submitter":{"id":71988,"url":"http://patchwork.ozlabs.org/api/people/71988/","name":"Jonathan Cameron","email":"Jonathan.Cameron@huawei.com"},"content":"On Wed, 20 Sep 2017 20:59:52 +0200\nWolfram Sang <wsa+renesas@sang-engineering.com> wrote:\n\n> One helper checks if DMA is suitable and optionally creates a bounce\n> buffer, if not. The other function returns the bounce buffer and makes\n> sure the data is properly copied back to the message.\n> \n> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>\n\nOne minor suggestion for wording. Otherwise looks good to me.\n\nReviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>\n\n> ---\n>  drivers/i2c/i2c-core-base.c | 45 +++++++++++++++++++++++++++++++++++++++++++++\n>  include/linux/i2c.h         |  3 +++\n>  2 files changed, 48 insertions(+)\n> \n> diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c\n> index 56e46581b84bdb..925a22794d3ced 100644\n> --- a/drivers/i2c/i2c-core-base.c\n> +++ b/drivers/i2c/i2c-core-base.c\n> @@ -2241,6 +2241,51 @@ void i2c_put_adapter(struct i2c_adapter *adap)\n>  }\n>  EXPORT_SYMBOL(i2c_put_adapter);\n>  \n> +/**\n> + * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg\n> + * @msg: the message to be checked\n> + * @threshold: the amount of byte from which using DMA makes sense\n\nthe minimum number of bytes for which using DMA makes sense.\n\n> + *\n> + * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.\n> + *\n> + *\t   Or a valid pointer to be used with DMA. Note that it can either be\n> + *\t   msg->buf or a bounce buffer. After use, release it by calling\n> + *\t   i2c_release_dma_safe_msg_buf().\n> + *\n> + * This function must only be called from process context!\n> + */\n> +u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)\n> +{\n> +\tif (msg->len < threshold)\n> +\t\treturn NULL;\n> +\n> +\tif (msg->flags & I2C_M_DMA_SAFE)\n> +\t\treturn msg->buf;\n> +\n> +\tif (msg->flags & I2C_M_RD)\n> +\t\treturn kzalloc(msg->len, GFP_KERNEL);\n> +\telse\n> +\t\treturn kmemdup(msg->buf, msg->len, GFP_KERNEL);\n> +}\n> +EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);\n> +\n> +/**\n> + * i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg\n> + * @msg: the message to be synced with\n> + * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.\n> + */\n> +void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)\n> +{\n> +\tif (!buf || buf == msg->buf)\n> +\t\treturn;\n> +\n> +\tif (msg->flags & I2C_M_RD)\n> +\t\tmemcpy(msg->buf, buf, msg->len);\n> +\n> +\tkfree(buf);\n> +}\n> +EXPORT_SYMBOL_GPL(i2c_release_dma_safe_msg_buf);\n> +\n>  MODULE_AUTHOR(\"Simon G. Vogl <simon@tk.uni-linz.ac.at>\");\n>  MODULE_DESCRIPTION(\"I2C-Bus main module\");\n>  MODULE_LICENSE(\"GPL\");\n> diff --git a/include/linux/i2c.h b/include/linux/i2c.h\n> index d501d3956f13f0..1e99342f180f45 100644\n> --- a/include/linux/i2c.h\n> +++ b/include/linux/i2c.h\n> @@ -767,6 +767,9 @@ static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)\n>  \treturn (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0);\n>  }\n>  \n> +u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold);\n> +void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf);\n> +\n>  int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr);\n>  /**\n>   * module_i2c_driver() - Helper macro for registering a modular I2C driver","headers":{"Return-Path":"<linux-i2c-owner@vger.kernel.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=linux-i2c-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xydYS6K3rz9rxj\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 00:00:44 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1751962AbdIUOAL (ORCPT <rfc822;incoming@patchwork.ozlabs.org>);\n\tThu, 21 Sep 2017 10:00:11 -0400","from szxga04-in.huawei.com ([45.249.212.190]:6968 \"EHLO\n\tszxga04-in.huawei.com\" rhost-flags-OK-OK-OK-OK) by vger.kernel.org\n\twith ESMTP id S1751951AbdIUOAJ (ORCPT\n\t<rfc822; linux-i2c@vger.kernel.org>); Thu, 21 Sep 2017 10:00:09 -0400","from 172.30.72.59 (EHLO DGGEMS403-HUB.china.huawei.com)\n\t([172.30.72.59])\n\tby dggrg04-dlp.huawei.com (MOS 4.4.6-GA FastPath queued)\n\twith ESMTP id DHR61523; Thu, 21 Sep 2017 22:00:00 +0800 (CST)","from localhost (10.206.48.115) by DGGEMS403-HUB.china.huawei.com\n\t(10.3.19.203) with Microsoft SMTP Server (TLS) id 14.3.301.0;\n\tThu, 21 Sep 2017 21:59:55 +0800"],"Date":"Thu, 21 Sep 2017 14:59:22 +0100","From":"Jonathan Cameron <Jonathan.Cameron@huawei.com>","To":"Wolfram Sang <wsa+renesas@sang-engineering.com>","CC":"<linux-i2c@vger.kernel.org>, <linux-kernel@vger.kernel.org>,\n\t<linux-renesas-soc@vger.kernel.org>, <linux-iio@vger.kernel.org>,\n\t<linux-input@vger.kernel.org>, <linux-media@vger.kernel.org>,\n\t<dri-devel@lists.freedesktop.org>","Subject":"Re: [RFC PATCH v5 2/6] i2c: add helpers to ease DMA handling","Message-ID":"<20170921145922.000017b5@huawei.com>","In-Reply-To":"<20170920185956.13874-3-wsa+renesas@sang-engineering.com>","References":"<20170920185956.13874-1-wsa+renesas@sang-engineering.com>\n\t<20170920185956.13874-3-wsa+renesas@sang-engineering.com>","Organization":"Huawei","X-Mailer":"Claws Mail 3.15.0 (GTK+ 2.24.31; x86_64-w64-mingw32)","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"US-ASCII\"","Content-Transfer-Encoding":"7bit","X-Originating-IP":"[10.206.48.115]","X-CFilter-Loop":"Reflected","X-Mirapoint-Virus-RAPID-Raw":"score=unknown(0),\n\trefid=str=0001.0A020204.59C3C5E5.0178, ss=1, re=0.000, recu=0.000,\n\treip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0,\n\tso=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32","X-Mirapoint-Loop-Id":"dacf39b46947e48b69d27f3a42d7687d","Sender":"linux-i2c-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<linux-i2c.vger.kernel.org>","X-Mailing-List":"linux-i2c@vger.kernel.org"}},{"id":1772822,"web_url":"http://patchwork.ozlabs.org/comment/1772822/","msgid":"<20170921150554.0000273b@huawei.com>","list_archive_url":null,"date":"2017-09-21T14:05:54","subject":"Re: [RFC PATCH v5 2/6] i2c: add helpers to ease DMA handling","submitter":{"id":71988,"url":"http://patchwork.ozlabs.org/api/people/71988/","name":"Jonathan Cameron","email":"Jonathan.Cameron@huawei.com"},"content":"On Thu, 21 Sep 2017 14:59:22 +0100\nJonathan Cameron <Jonathan.Cameron@huawei.com> wrote:\n\n> On Wed, 20 Sep 2017 20:59:52 +0200\n> Wolfram Sang <wsa+renesas@sang-engineering.com> wrote:\n> \n> > One helper checks if DMA is suitable and optionally creates a bounce\n> > buffer, if not. The other function returns the bounce buffer and makes\n> > sure the data is properly copied back to the message.\n> > \n> > Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>  \n> \n> One minor suggestion for wording. Otherwise looks good to me.\n> \n> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>\n>\n\nNeed more coffee. See below.\n \n> > ---\n> >  drivers/i2c/i2c-core-base.c | 45 +++++++++++++++++++++++++++++++++++++++++++++\n> >  include/linux/i2c.h         |  3 +++\n> >  2 files changed, 48 insertions(+)\n> > \n> > diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c\n> > index 56e46581b84bdb..925a22794d3ced 100644\n> > --- a/drivers/i2c/i2c-core-base.c\n> > +++ b/drivers/i2c/i2c-core-base.c\n> > @@ -2241,6 +2241,51 @@ void i2c_put_adapter(struct i2c_adapter *adap)\n> >  }\n> >  EXPORT_SYMBOL(i2c_put_adapter);\n> >  \n> > +/**\n> > + * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg\n> > + * @msg: the message to be checked\n> > + * @threshold: the amount of byte from which using DMA makes sense  \n> \n> the minimum number of bytes for which using DMA makes sense.\n> \n> > + *\n> > + * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.\n> > + *\n> > + *\t   Or a valid pointer to be used with DMA. Note that it can either be\n> > + *\t   msg->buf or a bounce buffer. After use, release it by calling\n> > + *\t   i2c_release_dma_safe_msg_buf().\n> > + *\n> > + * This function must only be called from process context!\n> > + */\n> > +u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)\n> > +{\n> > +\tif (msg->len < threshold)\n> > +\t\treturn NULL;\n> > +\n> > +\tif (msg->flags & I2C_M_DMA_SAFE)\n> > +\t\treturn msg->buf;\n> > +\n> > +\tif (msg->flags & I2C_M_RD)\n> > +\t\treturn kzalloc(msg->len, GFP_KERNEL);\n> > +\telse\n> > +\t\treturn kmemdup(msg->buf, msg->len, GFP_KERNEL);\n> > +}\n> > +EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);\n> > +\n> > +/**\n> > + * i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg\n> > + * @msg: the message to be synced with\n> > + * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.\n> > + */\n> > +void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)\n> > +{\n> > +\tif (!buf || buf == msg->buf)\n> > +\t\treturn;\n> > +\n> > +\tif (msg->flags & I2C_M_RD)\n> > +\t\tmemcpy(msg->buf, buf, msg->len);\n> > +\n> > +\tkfree(buf);\n\nOnly free when you actually allocated it.  Seems to me like you need\nto check if (!(msg->flags & I2C_M_DMA_SAFE)) before kfree.\n\nOtherwise the logic to do this will be needed in every driver\nwhich will get irritating fast.\n\n\n> > +}\n> > +EXPORT_SYMBOL_GPL(i2c_release_dma_safe_msg_buf);\n> > +\n> >  MODULE_AUTHOR(\"Simon G. Vogl <simon@tk.uni-linz.ac.at>\");\n> >  MODULE_DESCRIPTION(\"I2C-Bus main module\");\n> >  MODULE_LICENSE(\"GPL\");\n> > diff --git a/include/linux/i2c.h b/include/linux/i2c.h\n> > index d501d3956f13f0..1e99342f180f45 100644\n> > --- a/include/linux/i2c.h\n> > +++ b/include/linux/i2c.h\n> > @@ -767,6 +767,9 @@ static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)\n> >  \treturn (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0);\n> >  }\n> >  \n> > +u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold);\n> > +void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf);\n> > +\n> >  int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr);\n> >  /**\n> >   * module_i2c_driver() - Helper macro for registering a modular I2C driver  \n> \n> --\n> To unsubscribe from this list: send the line \"unsubscribe linux-iio\" in\n> the body of a message to majordomo@vger.kernel.org\n> More majordomo info at  http://vger.kernel.org/majordomo-info.html","headers":{"Return-Path":"<linux-i2c-owner@vger.kernel.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=linux-i2c-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xydhf5YdCz9s7f\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 00:06:58 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1751596AbdIUOGL (ORCPT <rfc822;incoming@patchwork.ozlabs.org>);\n\tThu, 21 Sep 2017 10:06:11 -0400","from szxga04-in.huawei.com ([45.249.212.190]:6969 \"EHLO\n\tszxga04-in.huawei.com\" rhost-flags-OK-OK-OK-OK) by vger.kernel.org\n\twith ESMTP id S1751387AbdIUOGK (ORCPT\n\t<rfc822; linux-i2c@vger.kernel.org>); Thu, 21 Sep 2017 10:06:10 -0400","from 172.30.72.58 (EHLO DGGEMS408-HUB.china.huawei.com)\n\t([172.30.72.58])\n\tby dggrg04-dlp.huawei.com (MOS 4.4.6-GA FastPath queued)\n\twith ESMTP id DHR62198; Thu, 21 Sep 2017 22:06:07 +0800 (CST)","from localhost (10.206.48.115) by DGGEMS408-HUB.china.huawei.com\n\t(10.3.19.208) with Microsoft SMTP Server (TLS) id 14.3.301.0;\n\tThu, 21 Sep 2017 22:06:04 +0800"],"Date":"Thu, 21 Sep 2017 15:05:54 +0100","From":"Jonathan Cameron <Jonathan.Cameron@huawei.com>","To":"Wolfram Sang <wsa+renesas@sang-engineering.com>","CC":"<linux-i2c@vger.kernel.org>, <linux-kernel@vger.kernel.org>,\n\t<linux-renesas-soc@vger.kernel.org>, <linux-iio@vger.kernel.org>,\n\t<linux-input@vger.kernel.org>, <linux-media@vger.kernel.org>,\n\t<dri-devel@lists.freedesktop.org>","Subject":"Re: [RFC PATCH v5 2/6] i2c: add helpers to ease DMA handling","Message-ID":"<20170921150554.0000273b@huawei.com>","In-Reply-To":"<20170921145922.000017b5@huawei.com>","References":"<20170920185956.13874-1-wsa+renesas@sang-engineering.com>\n\t<20170920185956.13874-3-wsa+renesas@sang-engineering.com>\n\t<20170921145922.000017b5@huawei.com>","Organization":"Huawei","X-Mailer":"Claws Mail 3.15.0 (GTK+ 2.24.31; x86_64-w64-mingw32)","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"US-ASCII\"","Content-Transfer-Encoding":"7bit","X-Originating-IP":"[10.206.48.115]","X-CFilter-Loop":"Reflected","X-Mirapoint-Virus-RAPID-Raw":"score=unknown(0),\n\trefid=str=0001.0A020204.59C3C74F.00A6, ss=1, re=0.000, recu=0.000,\n\treip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0,\n\tso=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32","X-Mirapoint-Loop-Id":"dacf39b46947e48b69d27f3a42d7687d","Sender":"linux-i2c-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<linux-i2c.vger.kernel.org>","X-Mailing-List":"linux-i2c@vger.kernel.org"}},{"id":1772829,"web_url":"http://patchwork.ozlabs.org/comment/1772829/","msgid":"<20170921141528.xre53zpxwk355uih@ninjato>","list_archive_url":null,"date":"2017-09-21T14:15:28","subject":"Re: [RFC PATCH v5 2/6] i2c: add helpers to ease DMA handling","submitter":{"id":22495,"url":"http://patchwork.ozlabs.org/api/people/22495/","name":"Wolfram Sang","email":"wsa@the-dreams.de"},"content":"> > > +/**\n> > > + * i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg\n> > > + * @msg: the message to be synced with\n> > > + * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.\n> > > + */\n> > > +void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)\n> > > +{\n> > > +\tif (!buf || buf == msg->buf)\n> > > +\t\treturn;\n> > > +\n> > > +\tif (msg->flags & I2C_M_RD)\n> > > +\t\tmemcpy(msg->buf, buf, msg->len);\n> > > +\n> > > +\tkfree(buf);\n> \n> Only free when you actually allocated it.  Seems to me like you need\n> to check if (!(msg->flags & I2C_M_DMA_SAFE)) before kfree.\n> \n> Otherwise the logic to do this will be needed in every driver\n> which will get irritating fast.\n\nWell, I return early if (buf == msg->buf) which is only true for\nI2C_M_DMA_SAFE. If not, I allocated the buffer. Am I missing something?\nIt would be very strange to call this function if the caller allocated\nthe buffer manually.\n\nThanks for the review!","headers":{"Return-Path":"<linux-i2c-owner@vger.kernel.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=linux-i2c-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xydv26t91z9s7f\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 00:15:58 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1751697AbdIUOPb (ORCPT <rfc822;incoming@patchwork.ozlabs.org>);\n\tThu, 21 Sep 2017 10:15:31 -0400","from sauhun.de ([88.99.104.3]:41902 \"EHLO pokefinder.org\"\n\trhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP\n\tid S1751596AbdIUOPa (ORCPT <rfc822;linux-i2c@vger.kernel.org>);\n\tThu, 21 Sep 2017 10:15:30 -0400","from localhost (p54B33566.dip0.t-ipconnect.de [84.179.53.102])\n\tby pokefinder.org (Postfix) with ESMTPSA id 661412C48FF;\n\tThu, 21 Sep 2017 16:15:28 +0200 (CEST)"],"Date":"Thu, 21 Sep 2017 16:15:28 +0200","From":"Wolfram Sang <wsa@the-dreams.de>","To":"Jonathan Cameron <Jonathan.Cameron@huawei.com>","Cc":"Wolfram Sang <wsa+renesas@sang-engineering.com>,\n\tlinux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org,\n\tlinux-renesas-soc@vger.kernel.org, linux-iio@vger.kernel.org,\n\tlinux-input@vger.kernel.org, linux-media@vger.kernel.org,\n\tdri-devel@lists.freedesktop.org","Subject":"Re: [RFC PATCH v5 2/6] i2c: add helpers to ease DMA handling","Message-ID":"<20170921141528.xre53zpxwk355uih@ninjato>","References":"<20170920185956.13874-1-wsa+renesas@sang-engineering.com>\n\t<20170920185956.13874-3-wsa+renesas@sang-engineering.com>\n\t<20170921145922.000017b5@huawei.com>\n\t<20170921150554.0000273b@huawei.com>","MIME-Version":"1.0","Content-Type":"multipart/signed; micalg=pgp-sha256;\n\tprotocol=\"application/pgp-signature\"; boundary=\"w6ftkolbg5vznbjm\"","Content-Disposition":"inline","In-Reply-To":"<20170921150554.0000273b@huawei.com>","User-Agent":"NeoMutt/20170113 (1.7.2)","Sender":"linux-i2c-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<linux-i2c.vger.kernel.org>","X-Mailing-List":"linux-i2c@vger.kernel.org"}},{"id":1772867,"web_url":"http://patchwork.ozlabs.org/comment/1772867/","msgid":"<20170921153629.00001aae@huawei.com>","list_archive_url":null,"date":"2017-09-21T14:36:29","subject":"Re: [RFC PATCH v5 2/6] i2c: add helpers to ease DMA handling","submitter":{"id":71988,"url":"http://patchwork.ozlabs.org/api/people/71988/","name":"Jonathan Cameron","email":"Jonathan.Cameron@huawei.com"},"content":"On Thu, 21 Sep 2017 16:15:28 +0200\nWolfram Sang <wsa@the-dreams.de> wrote:\n\n> > > > +/**\n> > > > + * i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg\n> > > > + * @msg: the message to be synced with\n> > > > + * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.\n> > > > + */\n> > > > +void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)\n> > > > +{\n> > > > +\tif (!buf || buf == msg->buf)\n> > > > +\t\treturn;\n> > > > +\n> > > > +\tif (msg->flags & I2C_M_RD)\n> > > > +\t\tmemcpy(msg->buf, buf, msg->len);\n> > > > +\n> > > > +\tkfree(buf);  \n> > \n> > Only free when you actually allocated it.  Seems to me like you need\n> > to check if (!(msg->flags & I2C_M_DMA_SAFE)) before kfree.\n> > \n> > Otherwise the logic to do this will be needed in every driver\n> > which will get irritating fast.  \n> \n> Well, I return early if (buf == msg->buf) which is only true for\n> I2C_M_DMA_SAFE. If not, I allocated the buffer. Am I missing something?\n> It would be very strange to call this function if the caller allocated\n> the buffer manually.\n> \n> Thanks for the review!\n\nDoh missed that check and my comment was bonkers even if it hadn't been there.\nI come back to the claim of insufficient caffeine.\n\nYou are quite correct.  Please ignore previous comment - the code is\nfine as is. \n\nJonathan\n> \n>","headers":{"Return-Path":"<linux-i2c-owner@vger.kernel.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=linux-i2c-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xyfMV3dHvz9sNc\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 00:37:10 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1751938AbdIUOgw (ORCPT <rfc822;incoming@patchwork.ozlabs.org>);\n\tThu, 21 Sep 2017 10:36:52 -0400","from szxga05-in.huawei.com ([45.249.212.191]:6537 \"EHLO\n\tszxga05-in.huawei.com\" rhost-flags-OK-OK-OK-OK) by vger.kernel.org\n\twith ESMTP id S1751929AbdIUOgu (ORCPT\n\t<rfc822; linux-i2c@vger.kernel.org>); Thu, 21 Sep 2017 10:36:50 -0400","from 172.30.72.59 (EHLO DGGEMS410-HUB.china.huawei.com)\n\t([172.30.72.59])\n\tby dggrg05-dlp.huawei.com (MOS 4.4.6-GA FastPath queued)\n\twith ESMTP id DHT69050; Thu, 21 Sep 2017 22:36:42 +0800 (CST)","from localhost (10.206.48.115) by DGGEMS410-HUB.china.huawei.com\n\t(10.3.19.210) with Microsoft SMTP Server (TLS) id 14.3.301.0;\n\tThu, 21 Sep 2017 22:36:38 +0800"],"Date":"Thu, 21 Sep 2017 15:36:29 +0100","From":"Jonathan Cameron <Jonathan.Cameron@huawei.com>","To":"Wolfram Sang <wsa@the-dreams.de>","CC":"Wolfram Sang <wsa+renesas@sang-engineering.com>,\n\t<linux-i2c@vger.kernel.org>, <linux-kernel@vger.kernel.org>,\n\t<linux-renesas-soc@vger.kernel.org>, <linux-iio@vger.kernel.org>,\n\t<linux-input@vger.kernel.org>, <linux-media@vger.kernel.org>,\n\t<dri-devel@lists.freedesktop.org>","Subject":"Re: [RFC PATCH v5 2/6] i2c: add helpers to ease DMA handling","Message-ID":"<20170921153629.00001aae@huawei.com>","In-Reply-To":"<20170921141528.xre53zpxwk355uih@ninjato>","References":"<20170920185956.13874-1-wsa+renesas@sang-engineering.com>\n\t<20170920185956.13874-3-wsa+renesas@sang-engineering.com>\n\t<20170921145922.000017b5@huawei.com>\n\t<20170921150554.0000273b@huawei.com>\n\t<20170921141528.xre53zpxwk355uih@ninjato>","Organization":"Huawei","X-Mailer":"Claws Mail 3.15.0 (GTK+ 2.24.31; x86_64-w64-mingw32)","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"US-ASCII\"","Content-Transfer-Encoding":"7bit","X-Originating-IP":"[10.206.48.115]","X-CFilter-Loop":"Reflected","X-Mirapoint-Virus-RAPID-Raw":"score=unknown(0),\n\trefid=str=0001.0A090203.59C3CE7A.00F0, ss=1, re=0.000, recu=0.000,\n\treip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0,\n\tso=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32","X-Mirapoint-Loop-Id":"0d889656e168e946c9b26c0fee6159d8","Sender":"linux-i2c-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<linux-i2c.vger.kernel.org>","X-Mailing-List":"linux-i2c@vger.kernel.org"}}]