# Re: \[RFC PATCH v5 2/6\] i2c: add helpers to ease DMA handling

- From: Wolfram Sang \<hidden\>
- Date: 2017-09-21 14:15:32 
- List: linux-input
- Page: https://ratatoskr.run/linux-input/2017/09/5092571
- Thread: https://ratatoskr.run/linux-i2c/2017/09/5091922/t.md

```text

> > > +/**
> > > + * i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
> > > + * @msg: the message to be synced with
> > > + * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
> > > + */
> > > +void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)
> > > +{
> > > +	if (!buf || buf == msg->buf)
> > > +		return;
> > > +
> > > +	if (msg->flags & I2C_M_RD)
> > > +		memcpy(msg->buf, buf, msg->len);
> > > +
> > > +	kfree(buf);
> 
> Only free when you actually allocated it.  Seems to me like you need
> to check if (!(msg->flags & I2C_M_DMA_SAFE)) before kfree.
> 
> Otherwise the logic to do this will be needed in every driver
> which will get irritating fast.

Well, I return early if (buf == msg->buf) which is only true for
I2C_M_DMA_SAFE. If not, I allocated the buffer. Am I missing something?
It would be very strange to call this function if the caller allocated
the buffer manually.

Thanks for the review!

```

## Attachments

- [signature.asc](https://ratatoskr.run/linux-input/2017/09/5092571/attachment/0) (application/pgp-signature, 833 bytes)
