Skip to content

Commit 0481291

Browse files
tititiou36vinodkoul
authored andcommitted
dmaengine: pxa_dma: Annotate struct pxad_desc_sw with __counted_by
Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). To do so, the code needs a little shuffling related to how hw_desc is used and nb_desc incremented. The one by one increment is needed for the error handling path, calling pxad_free_desc(), to work correctly. So, add a new intermediate variable, desc, to store the result of the dma_pool_alloc() call. Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> Reviewed-by: Kees Cook <keescook@chromium.org> Link: https://lore.kernel.org/r/1c9ef22826f449a3756bb13a83494e9fe3e0be8b.1696676782.git.christophe.jaillet@wanadoo.fr Signed-off-by: Vinod Koul <vkoul@kernel.org>
1 parent 83c761f commit 0481291

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

drivers/dma/pxa_dma.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ struct pxad_desc_sw {
9191
bool cyclic;
9292
struct dma_pool *desc_pool; /* Channel's used allocator */
9393

94-
struct pxad_desc_hw *hw_desc[]; /* DMA coherent descriptors */
94+
struct pxad_desc_hw *hw_desc[] __counted_by(nb_desc);
95+
/* DMA coherent descriptors */
9596
};
9697

9798
struct pxad_phy {
@@ -739,6 +740,7 @@ pxad_alloc_desc(struct pxad_chan *chan, unsigned int nb_hw_desc)
739740
{
740741
struct pxad_desc_sw *sw_desc;
741742
dma_addr_t dma;
743+
void *desc;
742744
int i;
743745

744746
sw_desc = kzalloc(struct_size(sw_desc, hw_desc, nb_hw_desc),
@@ -748,20 +750,21 @@ pxad_alloc_desc(struct pxad_chan *chan, unsigned int nb_hw_desc)
748750
sw_desc->desc_pool = chan->desc_pool;
749751

750752
for (i = 0; i < nb_hw_desc; i++) {
751-
sw_desc->hw_desc[i] = dma_pool_alloc(sw_desc->desc_pool,
752-
GFP_NOWAIT, &dma);
753-
if (!sw_desc->hw_desc[i]) {
753+
desc = dma_pool_alloc(sw_desc->desc_pool, GFP_NOWAIT, &dma);
754+
if (!desc) {
754755
dev_err(&chan->vc.chan.dev->device,
755756
"%s(): Couldn't allocate the %dth hw_desc from dma_pool %p\n",
756757
__func__, i, sw_desc->desc_pool);
757758
goto err;
758759
}
759760

761+
sw_desc->nb_desc++;
762+
sw_desc->hw_desc[i] = desc;
763+
760764
if (i == 0)
761765
sw_desc->first = dma;
762766
else
763767
sw_desc->hw_desc[i - 1]->ddadr = dma;
764-
sw_desc->nb_desc++;
765768
}
766769

767770
return sw_desc;

0 commit comments

Comments
 (0)