udmabuf: Ensure to perform cache synchronisation in begin_cpu_udmabuf()
PATCH 1/1 udmabuf: Ensure to perform cache synchronisation in begin_cpu_udmabuf()
The message of commit 504e2b4ab97a ("dma-buf/udmabuf: skip redundant cpu sync to fix cacheline EEXIST warning") says:
Quoted 4 lines
The CPU sync at map/unmap time is also redundant for udmabuf:
begin_cpu_udmabuf()andend_cpu_udmabuf()already perform explicit cache synchronization via dma_sync_sgtable_for_cpu/device() when CPU access is requested through the dma-buf interface.
This, however, does not apply to the first time begin_cpu_udmabuf() is called on an udmabuf, in which case the implementation previously relied on get_sg_table() to perform the cache synchronisation.
Ensure to call dma_sync_sgtable_for_cpu() in that case as well.
--- drivers/dma-buf/udmabuf.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/dma-buf/udmabuf.c +5 -4
@@ -224,21 +224,22 @@ static int begin_cpu_udmabuf(struct dma_buf *buf, {struct udmabuf *ubuf = buf->priv;struct device *dev = ubuf->device->this_device; -int ret =0 ;if (!ubuf->sg) { ubuf->sg =get_sg_table (dev, buf, direction);if (IS_ERR (ubuf->sg)) { +int ret; + ret =PTR_ERR (ubuf->sg); ubuf->sg =NULL ; +return ret; }else { ubuf->sg_dir = direction; } - }else { -dma_sync_sgtable_for_cpu (dev, ubuf->sg, direction); } -return ret; +dma_sync_sgtable_for_cpu (dev, ubuf->sg, direction); +return 0 ; } static int end_cpu_udmabuf(struct dma_buf *buf,
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Unprotected concurrent accesses to ubuf->sg allow memory and DMA mapping leaks, and expose a TOCTOU race condition that can trigger a kernel panic via an invalid pointer dereference.
- [High] Mismatch between the initial DMA mapping direction and the cache synchronization direction for subsequent CPU accesses violates the DMA API contract.
--
commit aa38d7b2c22ba17dc9edaf6a59e4f9a1d76a592b
Author: Robert Mader <[email protected]>
udmabuf: Ensure to perform cache synchronisation in begin_cpu_udmabuf()
The message of commit 504e2b4ab97a ("dma-buf/udmabuf: skip redundant cpu sync to fix cacheline EEXIST warning") says:
Quoted 4 lines
The CPU sync at map/unmap time is also redundant for udmabuf:
begin_cpu_udmabuf()andend_cpu_udmabuf()already perform explicit cache synchronization via dma_sync_sgtable_for_cpu/device() when CPU access is requested through the dma-buf interface.
This, however, does not apply to the first time begin_cpu_udmabuf() is called on an udmabuf, in which case the implementation previously relied on get_sg_table() to perform the cache synchronisation.
Ensure to call dma_sync_sgtable_for_cpu() in that case as well.
Quoted 14 lines
drivers/dma-buf/udmabuf.c +1
-1@@ -224,21 +224,22 @@ static int begin_cpu_udmabuf(struct dma_buf *buf, {struct udmabuf *ubuf = buf->priv;struct device *dev = ubuf->device->this_device; -int ret =0 ;if (!ubuf->sg) { ubuf->sg =get_sg_table (dev, buf, direction);if (IS_ERR (ubuf->sg)) { +int ret;
drivers/dma-buf/udmabuf.c static int begin_cpu_udmabuf(struct dma_buf *buf, · line 239
Quoted 10 lines
drivers/dma-buf/udmabuf.c +2
-2+ ret =PTR_ERR (ubuf->sg); ubuf->sg =NULL ; +return ret; }else { ubuf->sg_dir = direction; } - }else { -dma_sync_sgtable_for_cpu (dev, ubuf->sg, direction); }
[Severity: Critical]
This is a pre-existing issue, but does this lack of synchronization allow concurrent threads to see a transient error pointer?
If multiple threads invoke the sync ioctl concurrently, they can race on the !ubuf->sg check. If the first thread fails to allocate the sg_table, it temporarily assigns an error pointer to ubuf->sg before setting it back to null:
Thread 1:
if (!ubuf->sg) {
ubuf->sg = get_sg_table(dev, buf, direction);Thread 2 could observe ubuf->sg as an error pointer, bypass the null check, and pass the error pointer to dma_sync_sgtable_for_cpu() further down, triggering a kernel panic when dereferenced.
Alternatively, if both threads see !ubuf->sg as true, they could both allocate sg_table structures, leaking one and its dma mapping.
drivers/dma-buf/udmabuf.c static int begin_cpu_udmabuf(struct dma_buf *buf, · line 241
Quoted 3 lines
drivers/dma-buf/udmabuf.c +1
-1-return ret; +dma_sync_sgtable_for_cpu (dev, ubuf->sg, direction);
[Severity: High]
This is a pre-existing issue, but does this allow syncing with a different direction than the original mapping?
If a user issues a sync ioctl with one direction to establish the sg_table mapping, and then issues a subsequent sync with a different direction, dma_sync_sgtable_for_cpu() and dma_sync_sgtable_for_device() will be called using the new direction.
Since the dma mapping rules require sync and map directions to exactly match unless the mapping is bidirectional, could this lead to incorrect cache maintenance operations?
Quoted 2 lines
drivers/dma-buf/udmabuf.c +1
-0+return 0 ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 53 lines · Robert Mader
The message of commit
504e2b4ab97a("dma-buf/udmabuf: skip redundant cpu sync to fix cacheline EEXIST warning") says:Quoted 4 lines
The CPU sync at map/unmap time is also redundant for udmabuf:
begin_cpu_udmabuf()andend_cpu_udmabuf()already perform explicit cache synchronization via dma_sync_sgtable_for_cpu/device() when CPU access is requested through the dma-buf interface.This, however, does not apply to the first time
begin_cpu_udmabuf()is called on an udmabuf, in which case the implementation previously relied onget_sg_table()to perform the cache synchronisation.Ensure to call
dma_sync_sgtable_for_cpu()in that case as well.Fixes 504e2b4ab97a ("dma-buf/udmabuf: skip redundant cpu sync to fix cacheline EEXIST warning")Signed-off-by Robert Mader <[email protected]>--- drivers/dma-buf/udmabuf.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)drivers/dma-buf/udmabuf.c +5
-5@@ -224,21 +224,22 @@ static int begin_cpu_udmabuf(struct dma_buf *buf, {struct udmabuf *ubuf = buf->priv;struct device *dev = ubuf->device->this_device; -int ret =0 ;if (!ubuf->sg) { ubuf->sg =get_sg_table (dev, buf, direction);if (IS_ERR (ubuf->sg)) { +int ret; + ret =PTR_ERR (ubuf->sg); ubuf->sg =NULL ; +return ret; }else { ubuf->sg_dir = direction; } - }else { -dma_sync_sgtable_for_cpu (dev, ubuf->sg, direction); } -return ret; +dma_sync_sgtable_for_cpu (dev, ubuf->sg, direction); +return 0 ; } static int end_cpu_udmabuf(struct dma_buf *buf, --
Quoted 2 lines
2.54.0
Hi Robert,
Thanks for catching this. You're right -- with DMA_ATTR_SKIP_CPU_SYNC in get_sg_table(), the first begin_cpu_udmabuf() call no longer gets the implicit CPU sync that the dma_map path used to provide, and the explicit sync in the old else-branch only ran when ubuf->sg already existed.
The fix correctly moves dma_sync_sgtable_for_cpu() so it runs in both cases. The logic looks right to me.
This wouldn't reproduce on x86 since DMA is cache-coherent there (dma_sync_* is a no-op), which is why I missed it -- it only matters on non-coherent architectures. My apologies for the regression.
Signature · 2 lines
Thanks, Mikhail
Hi Mikhail,
Quoted 8 lines · Mikhail Gavrilov
Hi Robert,
Thanks for catching this. You're right -- with
DMA_ATTR_SKIP_CPU_SYNCinget_sg_table(), the firstbegin_cpu_udmabuf()call no longer gets the implicit CPU sync that thedma_mappath used to provide, and the explicit sync in the old else-branch only ran whenubuf->sgalready existed.
Quoted 8 lines
The fix correctly moves
dma_sync_sgtable_for_cpu()so it runs in both cases. The logic looks right to me.This wouldn't reproduce on x86 since DMA is cache-coherent there (dma_sync_* is a no-op), which is why I missed it -- it only matters on non-coherent architectures. My apologies for the regression.
Reviewed-by Mikhail Gavrilov <[email protected]>
thanks for the quick review! And no worries, I was very happy about your patch, which is why I looked deeper into it in the first place.
Given the patch fixes a newly introduced regression (and is very simple), do you think there's a chance to still get it into 7.1 - or would it need to go via the usual 7.2/7.3 release schedule + backport?
Best regards
Signature · 6 lines
Robert Mader Consultant Software Developer Collabora Ltd. Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, UK Registered in England & Wales, no. 5513718
Quoted 9 lines · Robert Mader
Hi Mikhail,
thanks for the quick review! And no worries, I was very happy about your patch, which is why I looked deeper into it in the first place.
Given the patch fixes a newly introduced regression (and is very simple), do you think there's a chance to still get it into 7.1 - or would it need to go via the usual 7.2/7.3 release schedule + backport?
Hi Robert,
The timing depends on Vivek and the drm-misc maintainers, not me, but here's the situation as I understand it:
The original commit 504e2b4ab97a only landed in 7.2 (it went into drm-misc-next, not drm-misc-fixes). So 7.1 was never affected by the regression -- there's nothing to fix there.
For 7.2: since your patch fixes a regression introduced in the same cycle, it would ideally go through drm-misc-fixes to land in 7.2 before release, avoiding a broken commit in the final tree. That's the cleanest outcome.
Adding Vivek -- he pushed the original patch and would know whether drm-misc-fixes is the right route at this point in the cycle.
Signature · 2 lines
Thanks, Mikhail
Quoted 18 lines · Mikhail Gavrilov
Quoted 8 lines · Robert Mader
Hi Mikhail,
thanks for the quick review! And no worries, I was very happy about your patch, which is why I looked deeper into it in the first place.
Given the patch fixes a newly introduced regression (and is very simple), do you think there's a chance to still get it into 7.1 - or would it need to go via the usual 7.2/7.3 release schedule + backport?
Hi Robert,
The timing depends on Vivek and the drm-misc maintainers, not me, but here's the situation as I understand it:
The original commit
504e2b4ab97aonly landed in 7.2 (it went into drm-misc-next, not drm-misc-fixes). So 7.1 was never affected by the regression -- there's nothing to fix there.
Quoted 7 lines
For 7.2: since your patch fixes a regression introduced in the same cycle, it would ideally go through drm-misc-fixes to land in 7.2 before release, avoiding a broken commit in the final tree. That's the cleanest outcome.
Adding Vivek -- he pushed the original patch and would know whether drm-misc-fixes is the right route at this point in the cycle.
Hi Mikhail, I of course meant 7.2, not 7.1, sorry! And agreed, that would be ideal / perfect.
Signature · 6 lines
Robert Mader Consultant Software Developer Collabora Ltd. Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, UK Registered in England & Wales, no. 5513718
Hi, sorry for the noise - adding more udmabuf maintainers in case Vivek doesn't have time to look into it atm, just to ensure people are aware.
Gerd, Sumit, Christian: this is a simple fix for a minor regression in the upcoming 7.2 release - it would therefor be great if anyone could pick it to the fixes branch :)
Thanks and best regards,
Robert
Quoted 28 lines · Robert Mader
Quoted 18 lines · Mikhail Gavrilov
Quoted 8 lines · Robert Mader
Hi Mikhail,
thanks for the quick review! And no worries, I was very happy about your patch, which is why I looked deeper into it in the first place.
Given the patch fixes a newly introduced regression (and is very simple), do you think there's a chance to still get it into 7.1 - or would it need to go via the usual 7.2/7.3 release schedule + backport?
Hi Robert,
The timing depends on Vivek and the drm-misc maintainers, not me, but here's the situation as I understand it:
The original commit
504e2b4ab97aonly landed in 7.2 (it went into drm-misc-next, not drm-misc-fixes). So 7.1 was never affected by the regression -- there's nothing to fix there.Quoted 7 lines
For 7.2: since your patch fixes a regression introduced in the same cycle, it would ideally go through drm-misc-fixes to land in 7.2 before release, avoiding a broken commit in the final tree. That's the cleanest outcome.
Adding Vivek -- he pushed the original patch and would know whether drm-misc-fixes is the right route at this point in the cycle.
Hi Mikhail, I of course meant 7.2, not 7.1, sorry! And agreed, that would be ideal / perfect.
Signature · 6 lines
Robert Mader Consultant Software Developer Collabora Ltd. Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, UK Registered in England & Wales, no. 5513718
Kasireddy, Vivek
maintainerQuoted 9 lines
Subject: Re: [PATCH v1] udmabuf: Ensure to perform cache synchronisation in
begin_cpu_udmabuf()Hi, sorry for the noise - adding more udmabuf maintainers in case Vivek doesn't have time to look into it atm, just to ensure people are aware.
Gerd, Sumit, Christian: this is a simple fix for a minor regression in the upcoming 7.2 release - it would therefor be great if anyone could pick it to the fixes branch :)
Hi Robert,
Your patch LGTM. If no one else picks it up soon, I'll push it to drm-misc-fixes in the next few days.
Thanks,
Vivek
Quoted 46 lines
Thanks and best regards,
Robert
Quoted 5 lines · Robert Mader
Quoted 4 lines · Mikhail Gavrilov
Quoted 2 lines · Robert Mader
Hi Mikhail,
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
thanks for the quick review! And no worries, I was very happy about your
patch, which is why I looked deeper into it in the first place.Given the patch fixes a newly introduced regression (and is very simple), do you think there's a chance to still get it into 7.1 - or would it need to go via the usual 7.2/7.3 release schedule +
backport?
Quoted 9 lines
Quoted 9 lines
Hi Robert,
The timing depends on Vivek and the drm-misc maintainers, not me, but here's the situation as I understand it:
The original commit
504e2b4ab97aonly landed in 7.2 (it went into drm-misc-next, not drm-misc-fixes). So 7.1 was never affected by the regression -- there's nothing to fix there.Quoted 6 lines
Quoted 6 lines
For 7.2: since your patch fixes a regression introduced in the same cycle, it would ideally go through drm-misc-fixes to land in 7.2 before release, avoiding a broken commit in the final tree. That's the cleanest outcome.
Adding Vivek -- he pushed the original patch and would know
whether
Quoted 4 lines
Quoted 1 line
drm-misc-fixes is the right route at this point in the cycle.
Hi Mikhail, I of course meant 7.2, not 7.1, sorry! And agreed, that would be ideal / perfect.
--
Robert Mader
Consultant Software DeveloperCollabora Ltd.
Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, UK Registered in England & Wales, no. 5513718
Hi,
Quoted 6 lines · Vivek
Quoted 6 lines
Subject: Re: [PATCH v1] udmabuf: Ensure to perform cache synchronisation in
begin_cpu_udmabuf()Hi, sorry for the noise - adding more udmabuf maintainers in case Vivek doesn't have time to look into it atm, just to ensure people are aware.
Quoted 7 lines
Quoted 3 lines
Gerd, Sumit, Christian: this is a simple fix for a minor regression in the upcoming 7.2 release - it would therefor be great if anyone could pick it to the fixes branch :)
Hi Robert,
Your patch LGTM. If no one else picks it up soon, I'll push it to drm-misc-fixes in the next few days.
gentle ping ;) Would be great if this could go into this weeks fixes push.
Thanks and best regards,
Robert
Quoted 49 lines
Thanks,
VivekQuoted 45 lines
Thanks and best regards,
Robert
Quoted 5 lines · Robert Mader
Quoted 4 lines · Mikhail Gavrilov
Quoted 2 lines · Robert Mader
Hi Mikhail,
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
thanks for the quick review! And no worries, I was very happy about your
patch, which is why I looked deeper into it in the first place.Given the patch fixes a newly introduced regression (and is very simple), do you think there's a chance to still get it into 7.1 - or would it need to go via the usual 7.2/7.3 release schedule +
backport?
Quoted 9 lines
Quoted 9 lines
Hi Robert,
The timing depends on Vivek and the drm-misc maintainers, not me, but here's the situation as I understand it:
The original commit
504e2b4ab97aonly landed in 7.2 (it went into drm-misc-next, not drm-misc-fixes). So 7.1 was never affected by the regression -- there's nothing to fix there.Quoted 6 lines
Quoted 6 lines
For 7.2: since your patch fixes a regression introduced in the same cycle, it would ideally go through drm-misc-fixes to land in 7.2 before release, avoiding a broken commit in the final tree. That's the cleanest outcome.
Adding Vivek -- he pushed the original patch and would know
whether
Quoted 4 lines
Quoted 1 line
drm-misc-fixes is the right route at this point in the cycle.
Hi Mikhail, I of course meant 7.2, not 7.1, sorry! And agreed, that would be ideal / perfect.
--
Robert Mader
Consultant Software DeveloperCollabora Ltd.
Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, UK Registered in England & Wales, no. 5513718
Kasireddy, Vivek
maintainerQuoted 17 lines
Subject: Re: [PATCH v1] udmabuf: Ensure to perform cache synchronisation in
begin_cpu_udmabuf()Hi,
Quoted 4 lines · Vivek
Quoted 4 lines
Subject: Re: [PATCH v1] udmabuf: Ensure to perform cache synchronisation in
begin_cpu_udmabuf()Hi, sorry for the noise - adding more udmabuf maintainers in case
Vivek
Quoted 1 line
Quoted 1 line
doesn't have time to look into it atm, just to ensure people are
aware.
Quoted 4 lines
Quoted 4 lines
Gerd, Sumit, Christian: this is a simple fix for a minor regression in the upcoming 7.2 release - it would therefor be great if anyone could pick it to the fixes branch :)
Quoted 7 lines
Quoted 3 lines
Hi Robert,
Your patch LGTM. If no one else picks it up soon, I'll push it to drm-
misc-fixes in the
Quoted 1 line
next few days.
gentle ping ;) Would be great if this could go into this weeks fixes push.
Hi Robert,
I pushed your patch to drm-misc-fixes last week, so it should be able to make it to this week's fixes.
Thanks,
Vivek
Quoted 56 lines
Thanks and best regards,
Robert
Quoted 15 lines
Thanks,
VivekQuoted 11 lines
Thanks and best regards,
Robert
Quoted 6 lines · Robert Mader
Quoted 5 lines · Mikhail Gavrilov
Quoted 3 lines · Robert Mader
Hi Mikhail,
thanks for the quick review! And no worries, I was very happy
about
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
your
patch, which is why I looked deeper into it in the first place.Given the patch fixes a newly introduced regression (and is very simple), do you think there's a chance to still get it into 7.1 - or would it need to go via the usual 7.2/7.3 release schedule +
backport?
Quoted 3 lines
Quoted 3 lines
Hi Robert,
The timing depends on Vivek and the drm-misc maintainers, not
me,
Quoted 24 lines
Quoted 24 lines
Quoted 6 lines
Quoted 6 lines
but here's the situation as I understand it:
The original commit
504e2b4ab97aonly landed in 7.2 (it went into drm-misc-next, not drm-misc-fixes). So 7.1 was never affected by the regression -- there's nothing to fix there.Quoted 6 lines
Quoted 6 lines
For 7.2: since your patch fixes a regression introduced in the same cycle, it would ideally go through drm-misc-fixes to land in 7.2 before release, avoiding a broken commit in the final tree. That's the cleanest outcome.
Adding Vivek -- he pushed the original patch and would know
whether
Quoted 4 lines
Quoted 1 line
drm-misc-fixes is the right route at this point in the cycle.
Hi Mikhail, I of course meant 7.2, not 7.1, sorry! And agreed, that would be ideal / perfect.
--
Robert Mader
Consultant Software DeveloperCollabora Ltd.
Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, UK Registered in England & Wales, no. 5513718
Hi Vivek,
Quoted 18 lines · Vivek
Quoted 18 lines
Subject: Re: [PATCH v1] udmabuf: Ensure to perform cache synchronisation in
begin_cpu_udmabuf()Hi,
Quoted 4 lines · Vivek
Quoted 4 lines
Subject: Re: [PATCH v1] udmabuf: Ensure to perform cache synchronisation in
begin_cpu_udmabuf()Hi, sorry for the noise - adding more udmabuf maintainers in case
Vivek
Quoted 1 line
Quoted 1 line
doesn't have time to look into it atm, just to ensure people are
aware.
Quoted 5 lines
Quoted 3 lines
Gerd, Sumit, Christian: this is a simple fix for a minor regression in the upcoming 7.2 release - it would therefor be great if anyone could pick it to the fixes branch :)
Hi Robert,
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Your patch LGTM. If no one else picks it up soon, I'll push it to drm-
misc-fixes in the
Quoted 1 line
next few days.
gentle ping ;) Would be great if this could go into this weeks fixes push.
Hi Robert,
I pushed your patch to drm-misc-fixes last week, so it should be able to make it to this week's fixes.
oh, I totally missed that, sorry. And I see it's now queued for upstream - thanks!
Quoted 58 lines
Thanks,
VivekQuoted 54 lines
Thanks and best regards,
Robert
Quoted 14 lines
Thanks,
VivekQuoted 11 lines
Thanks and best regards,
Robert
Quoted 6 lines · Robert Mader
Quoted 5 lines · Mikhail Gavrilov
Quoted 3 lines · Robert Mader
Hi Mikhail,
thanks for the quick review! And no worries, I was very happy
about
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
your
patch, which is why I looked deeper into it in the first place.Given the patch fixes a newly introduced regression (and is very simple), do you think there's a chance to still get it into 7.1 - or would it need to go via the usual 7.2/7.3 release schedule +
backport?
Quoted 3 lines
Quoted 3 lines
Hi Robert,
The timing depends on Vivek and the drm-misc maintainers, not
me,
Quoted 24 lines
Quoted 24 lines
Quoted 6 lines
Quoted 6 lines
but here's the situation as I understand it:
The original commit
504e2b4ab97aonly landed in 7.2 (it went into drm-misc-next, not drm-misc-fixes). So 7.1 was never affected by the regression -- there's nothing to fix there.Quoted 6 lines
Quoted 6 lines
For 7.2: since your patch fixes a regression introduced in the same cycle, it would ideally go through drm-misc-fixes to land in 7.2 before release, avoiding a broken commit in the final tree. That's the cleanest outcome.
Adding Vivek -- he pushed the original patch and would know
whether
Quoted 4 lines
Quoted 1 line
drm-misc-fixes is the right route at this point in the cycle.
Hi Mikhail, I of course meant 7.2, not 7.1, sorry! And agreed, that would be ideal / perfect.
--
Robert Mader
Consultant Software DeveloperCollabora Ltd.
Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, UK Registered in England & Wales, no. 5513718
Signature · 6 lines
Robert Mader Consultant Software Developer Collabora Ltd. Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, UK Registered in England & Wales, no. 5513718