i2c: refactor releasing a DMA safe buffer
Subsystems (3)
Wolfram Sang
maintainercover i2c: refactor releasing a DMA safe buffer
Jun Gao correctly identified a problem when freeing a DMA safe buffer [1], many thanks for that! However, I'd like to not introduce yet another function. So, I thought about changing the existing API to support the case of freeing a buffer in case no sync back to the message is needed. Here is what I came up with and I like the new way better.
Once this is set in place, the only existing user so far, has been fixed. It was leaking the bounce buffer on error cases before. Tested on a Renesas Lager board (R-Car H2).
Please let me know your thoughts. Jun Gao, does it work for you as well?
If all goes well, I would like to apply it for -rc2. But I am still open for comments, of course.
Thanks and happy hacking,
Wolfram
[1] http://patchwork.ozlabs.org/patch/940768/
Wolfram Sang (3):
i2c: refactor function to release a DMA safe buffer
i2c: sh_mobile: define start_ch() void as it only returns 0 anyhow
i2c: sh_mobile: fix leak when using DMA bounce buffer
Documentation/i2c/DMA-considerations | 10 +++++++--- drivers/i2c/busses/i2c-sh_mobile.c | 15 +++++++-------- drivers/i2c/i2c-core-base.c | 11 ++++++----- include/linux/i2c.h | 2 +- 4 files changed, 21 insertions(+), 17 deletions(-)
Signature · 1 line
2.11.0
Wolfram Sang
maintainerPATCH 1/3 i2c: refactor function to release a DMA safe buffer
a) rename to 'put' instead of 'release' to match 'get' when obtaining the buffer
b) change the argument order to have the buffer as first argument
c) add a new argument telling the function if the message was transferred. This allows the function to be used also in cases where setting up DMA failed, so the buffer needs to be freed without syncing to the message buffer.
Also convert the only user.
--- Documentation/i2c/DMA-considerations | 10 +++++++--- drivers/i2c/busses/i2c-sh_mobile.c | 2 +- drivers/i2c/i2c-core-base.c | 11 ++++++----- include/linux/i2c.h | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-)
Documentation/i2c/DMA-considerations +7 -3
@@ -50,10 +50,14 @@ bounce buffer. But you don't need to care about that detail, just use the returned buffer. If NULL is returned, the threshold was not met or a bounce buffer could not be allocated. Fall back to PIO in that case. -In any case, a buffer obtained from above needs to be released. It ensures data -is copied back to the message and a potentially used bounce buffer is freed:: +In any case, a buffer obtained from above needs to be released. Another helper +function ensures a potentially used bounce buffer is freed:: - i2c_release_dma_safe_msg_buf(msg, dma_buf); + i2c_put_dma_safe_msg_buf(dma_buf, msg, xferred); + +The last argument 'xferred' controls if the buffer is synced back to the +message or not. No syncing is needed in cases setting up DMA had an error and +there was no data transferred. The bounce buffer handling from the core is generic and simple. It will always allocate a new bounce buffer. If you want a more sophisticated handling (e.g.
drivers/i2c/busses/i2c-sh_mobile.c +1 -1
@@ -515,7 +515,7 @@ static void sh_mobile_i2c_dma_callback(void *data) pd->pos = pd->msg->len; pd->stop_after_dma = true; -i2c_release_dma_safe_msg_buf (pd->msg, pd->dma_buf); +i2c_put_dma_safe_msg_buf (pd->dma_buf, pd->msg, true);iic_set_clr (pd,ICIC ,0 ,ICIC_TDMAE |ICIC_RDMAE ); }
drivers/i2c/i2c-core-base.c +6 -5
@@ -2302,21 +2302,22 @@ u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)EXPORT_SYMBOL_GPL (i2c_get_dma_safe_msg_buf);/** -* i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg -* @msg: the message to be synced with +* i2c_put_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL. +* @msg: the message which the buffer corresponds to +* @xferred: bool saying if the message was transferred */ -void i2c_release_dma_safe_msg_buf (struct i2c_msg *msg,u8 *buf) +void i2c_put_dma_safe_msg_buf (u8 *buf,struct i2c_msg *msg,bool xferred) {if (!buf || buf == msg->buf)return ; -if (msg->flags &I2C_M_RD ) +if (xferred && msg->flags &I2C_M_RD )memcpy (msg->buf, buf, msg->len);kfree (buf); } -EXPORT_SYMBOL_GPL (i2c_release_dma_safe_msg_buf); +EXPORT_SYMBOL_GPL (i2c_put_dma_safe_msg_buf);MODULE_AUTHOR ("Simon G. Vogl <[email protected]>" );MODULE_DESCRIPTION ("I2C-Bus main module" );
include/linux/i2c.h +1 -1
@@ -861,7 +861,7 @@ static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg) }u8 *i2c_get_dma_safe_msg_buf (struct i2c_msg *msg,unsigned int threshold); -void i2c_release_dma_safe_msg_buf (struct i2c_msg *msg,u8 *buf); +void i2c_put_dma_safe_msg_buf (u8 *buf,struct i2c_msg *msg,bool xferred);int i2c_handle_smbus_host_notify (struct i2c_adapter *adap,unsigned short addr);/**
Signature · 1 line
2.11.0
Re: [PATCH 1/3] i2c: refactor function to release a DMA safe buffer
Hi Wolfram,
Thanks for your patch.
Quoted 11 lines · Wolfram Sang
a) rename to 'put' instead of 'release' to match 'get' when obtaining the buffer
b) change the argument order to have the buffer as first argument
c) add a new argument telling the function if the message was transferred. This allows the function to be used also in cases where setting up DMA failed, so the buffer needs to be freed without syncing to the message buffer.Also convert the only user.
Signed-off-by Wolfram Sang <[email protected]>
Quoted 90 lines
--- Documentation/i2c/DMA-considerations | 10 +++++++--- drivers/i2c/busses/i2c-sh_mobile.c | 2 +- drivers/i2c/i2c-core-base.c | 11 ++++++----- include/linux/i2c.h | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-)Documentation/i2c/DMA-considerations +7
-3@@ -50,10 +50,14 @@ bounce buffer. But you don't need to care about that detail, just use the returned buffer. If NULL is returned, the threshold was not met or a bounce buffer could not be allocated. Fall back to PIO in that case. -In any case, a buffer obtained from above needs to be released. It ensures data -is copied back to the message and a potentially used bounce buffer is freed:: +In any case, a buffer obtained from above needs to be released. Another helper +function ensures a potentially used bounce buffer is freed:: - i2c_release_dma_safe_msg_buf(msg, dma_buf); + i2c_put_dma_safe_msg_buf(dma_buf, msg, xferred); + +The last argument 'xferred' controls if the buffer is synced back to the +message or not. No syncing is needed in cases setting up DMA had an error and +there was no data transferred. The bounce buffer handling from the core is generic and simple. It will always allocate a new bounce buffer. If you want a more sophisticated handling (e.g.drivers/i2c/busses/i2c-sh_mobile.c +1
-1@@ -515,7 +515,7 @@ static void sh_mobile_i2c_dma_callback(void *data) pd->pos = pd->msg->len; pd->stop_after_dma = true; -i2c_release_dma_safe_msg_buf (pd->msg, pd->dma_buf); +i2c_put_dma_safe_msg_buf (pd->dma_buf, pd->msg, true);iic_set_clr (pd,ICIC ,0 ,ICIC_TDMAE |ICIC_RDMAE ); }drivers/i2c/i2c-core-base.c +6
-5@@ -2302,21 +2302,22 @@ u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)EXPORT_SYMBOL_GPL (i2c_get_dma_safe_msg_buf);/** -* i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg -* @msg: the message to be synced with +* i2c_put_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL. +* @msg: the message which the buffer corresponds to +* @xferred: bool saying if the message was transferred */ -void i2c_release_dma_safe_msg_buf (struct i2c_msg *msg,u8 *buf) +void i2c_put_dma_safe_msg_buf (u8 *buf,struct i2c_msg *msg,bool xferred) {if (!buf || buf == msg->buf)return ; -if (msg->flags &I2C_M_RD ) +if (xferred && msg->flags &I2C_M_RD )memcpy (msg->buf, buf, msg->len);kfree (buf); } -EXPORT_SYMBOL_GPL (i2c_release_dma_safe_msg_buf); +EXPORT_SYMBOL_GPL (i2c_put_dma_safe_msg_buf);MODULE_AUTHOR ("Simon G. Vogl <[email protected]>" );MODULE_DESCRIPTION ("I2C-Bus main module" );include/linux/i2c.h +1
-1@@ -861,7 +861,7 @@ static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg) }u8 *i2c_get_dma_safe_msg_buf (struct i2c_msg *msg,unsigned int threshold); -void i2c_release_dma_safe_msg_buf (struct i2c_msg *msg,u8 *buf); +void i2c_put_dma_safe_msg_buf (u8 *buf,struct i2c_msg *msg,bool xferred);int i2c_handle_smbus_host_notify (struct i2c_adapter *adap,unsigned short addr);/** Signature · 1 line
2.11.0
Signature · 2 lines
Regards, Niklas S�derlund
Wolfram Sang
maintainerPATCH 2/3 i2c: sh_mobile: define start_ch() void as it only returns 0 anyhow
In reply to Wolfram SangAfter various refactoring over the years, start_ch() doesn't return errno anymore, so make the function return void. This saves the error handling when calling it which in turn eases cleanup of resources of a future patch.
--- drivers/i2c/busses/i2c-sh_mobile.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/i2c/busses/i2c-sh_mobile.c +3 -6
@@ -610,8 +610,8 @@ static void sh_mobile_i2c_xfer_dma(struct sh_mobile_i2c_data *pd)dma_async_issue_pending (chan); } -static int start_ch (struct sh_mobile_i2c_data *pd,struct i2c_msg *usr_msg, -bool do_init) +static void start_ch (struct sh_mobile_i2c_data *pd,struct i2c_msg *usr_msg, +bool do_init) {if (do_init) {/* Initialize channel registers */
@@ -635,7 +635,6 @@ static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg,/* Enable all interrupts to begin with */ iic_wr (pd,ICIC ,ICIC_DTEE |ICIC_WAITE |ICIC_ALE |ICIC_TACKE ); -return 0 ; }static int poll_dte (struct sh_mobile_i2c_data *pd)
@@ -706,9 +705,7 @@ static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter, pd->send_stop = i == num -1 || msg->flags &I2C_M_STOP ; pd->stop_after_dma = false; - err =start_ch (pd, msg, do_start); -if (err) -break ; +start_ch (pd, msg, do_start);if (do_start)i2c_op (pd,OP_START ,0 );
Signature · 1 line
2.11.0
Re: [PATCH 2/3] i2c: sh_mobile: define start_ch() void as it only returns 0 anyhow
Hi Wolfram,
Thanks for your work.
Quoted 6 lines · Wolfram Sang
After various refactoring over the years,
start_ch()doesn't return errno anymore, so make the function return void. This saves the error handling when calling it which in turn eases cleanup of resources of a future patch.Signed-off-by Wolfram Sang <[email protected]>
Quoted 41 lines
--- drivers/i2c/busses/i2c-sh_mobile.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)drivers/i2c/busses/i2c-sh_mobile.c +3
-6@@ -610,8 +610,8 @@ static void sh_mobile_i2c_xfer_dma(struct sh_mobile_i2c_data *pd)dma_async_issue_pending (chan); } -static int start_ch (struct sh_mobile_i2c_data *pd,struct i2c_msg *usr_msg, -bool do_init) +static void start_ch (struct sh_mobile_i2c_data *pd,struct i2c_msg *usr_msg, +bool do_init) {if (do_init) {/* Initialize channel registers */ @@ -635,7 +635,6 @@ static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg,/* Enable all interrupts to begin with */ iic_wr (pd,ICIC ,ICIC_DTEE |ICIC_WAITE |ICIC_ALE |ICIC_TACKE ); -return 0 ; }static int poll_dte (struct sh_mobile_i2c_data *pd)@@ -706,9 +705,7 @@ static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter, pd->send_stop = i == num -1 || msg->flags &I2C_M_STOP ; pd->stop_after_dma = false; - err =start_ch (pd, msg, do_start); -if (err) -break ; +start_ch (pd, msg, do_start);if (do_start)i2c_op (pd,OP_START ,0 );Signature · 1 line
2.11.0
Signature · 2 lines
Regards, Niklas S�derlund
Wolfram Sang
maintainerPATCH 3/3 i2c: sh_mobile: fix leak when using DMA bounce buffer
In reply to Wolfram SangWe only freed the bounce buffer after successful DMA, missing the cases where DMA setup may have gone wrong. Use a better location which always gets called after each message and use 'stop_after_dma' as a flag for a successful transfer.
--- drivers/i2c/busses/i2c-sh_mobile.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-sh_mobile.c +4 -2
@@ -515,8 +515,6 @@ static void sh_mobile_i2c_dma_callback(void *data) pd->pos = pd->msg->len; pd->stop_after_dma = true; -i2c_put_dma_safe_msg_buf (pd->dma_buf, pd->msg, true); -iic_set_clr (pd,ICIC ,0 ,ICIC_TDMAE |ICIC_RDMAE ); }
@@ -714,6 +712,10 @@ static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter, timeout =wait_event_timeout (pd->wait, pd->sr & (ICSR_TACK |SW_DONE ), adapter->timeout); + +/* 'stop_after_dma' tells if DMA transfer was complete */ +i2c_put_dma_safe_msg_buf (pd->dma_buf, pd->msg, pd->stop_after_dma); +if (!timeout) {dev_err (pd->dev,"Transfer request timed out\n" );if (pd->dma_direction !=DMA_NONE )
Signature · 1 line
2.11.0
Re: [PATCH 3/3] i2c: sh_mobile: fix leak when using DMA bounce buffer
Hi Wolfram,
Thanks for your work.
Quoted 6 lines · Wolfram Sang
We only freed the bounce buffer after successful DMA, missing the cases where DMA setup may have gone wrong. Use a better location which always gets called after each message and use '
stop_after_dma' as a flag for a successful transfer.Signed-off-by Wolfram Sang <[email protected]>
Quoted 31 lines
--- drivers/i2c/busses/i2c-sh_mobile.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/i2c/busses/i2c-sh_mobile.c +4
-2@@ -515,8 +515,6 @@ static void sh_mobile_i2c_dma_callback(void *data) pd->pos = pd->msg->len; pd->stop_after_dma = true; -i2c_put_dma_safe_msg_buf (pd->dma_buf, pd->msg, true); -iic_set_clr (pd,ICIC ,0 ,ICIC_TDMAE |ICIC_RDMAE ); }@@ -714,6 +712,10 @@ static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter, timeout =wait_event_timeout (pd->wait, pd->sr & (ICSR_TACK |SW_DONE ), adapter->timeout); + +/* 'stop_after_dma' tells if DMA transfer was complete */ +i2c_put_dma_safe_msg_buf (pd->dma_buf, pd->msg, pd->stop_after_dma); +if (!timeout) {dev_err (pd->dev,"Transfer request timed out\n" );if (pd->dma_direction !=DMA_NONE )Signature · 1 line
2.11.0
Signature · 2 lines
Regards, Niklas S�derlund
Quoted 11 lines · Wolfram Sang
Jun Gao correctly identified a problem when freeing a DMA safe buffer [1], many thanks for that! However, I'd like to not introduce yet another function. So, I thought about changing the existing API to support the case of freeing a buffer in case no sync back to the message is needed. Here is what I came up with and I like the new way better.
Once this is set in place, the only existing user so far, has been fixed. It was leaking the bounce buffer on error cases before. Tested on a Renesas Lager board (R-Car H2).
Please let me know your thoughts. Jun Gao, does it work for you as well?
Dear Wolfram,
Thanks for your work.
It can work for us. I will modify my patch base on it.
Thanks!
Jun
Quoted 22 lines
If all goes well, I would like to apply it for -rc2. But I am still open for comments, of course.
Thanks and happy hacking,
Wolfram
[1] http://patchwork.ozlabs.org/patch/940768/
Wolfram Sang (3):
i2c: refactor function to release a DMA safe buffer
i2c:sh_mobile: definestart_ch()void as it only returns 0 anyhow
i2c:sh_mobile: fix leak when using DMA bounce bufferDocumentation/i2c/DMA-considerations | 10 +++++++--- drivers/i2c/busses/i2c-sh_mobile.c | 15 +++++++-------- drivers/i2c/i2c-core-base.c | 11 ++++++----- include/linux/i2c.h | 2 +- 4 files changed, 21 insertions(+), 17 deletions(-)
Wolfram Sang
AppliedQuoted 10 lines · Wolfram Sang
Jun Gao correctly identified a problem when freeing a DMA safe buffer [1], many thanks for that! However, I'd like to not introduce yet another function. So, I thought about changing the existing API to support the case of freeing a buffer in case no sync back to the message is needed. Here is what I came up with and I like the new way better.
Once this is set in place, the only existing user so far, has been fixed. It was leaking the bounce buffer on error cases before. Tested on a Renesas Lager board (R-Car H2).
Quoted 8 lines
Please let me know your thoughts. Jun Gao, does it work for you as well?
If all goes well, I would like to apply it for -rc2. But I am still open for comments, of course.
Thanks and happy hacking,
Wolfram
Applied all to for-current.