Skip to content

Commit 9b6ee3c

Browse files
solbjorndavem330
authored andcommitted
qed: sanitize PBL chains allocation
PBL chain elements are actually DMA addresses stored in __le64, but currently their size is hardcoded to 8, and DMA addresses are assigned via cast to variable-sized dma_addr_t without any bitwise conversions. Change the type of pbl_virt array to match the actual one, add a new field to store the size of allocated DMA memory and sanitize elements assignment. Misc: give more logic names to the members of qed_chain::pbl_sp embedded struct. Signed-off-by: Alexander Lobakin <alobakin@marvell.com> Signed-off-by: Igor Russkikh <irusskikh@marvell.com> Signed-off-by: Michal Kalderon <michal.kalderon@marvell.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 96ca4c5 commit 9b6ee3c

3 files changed

Lines changed: 20 additions & 21 deletions

File tree

‎drivers/net/ethernet/qlogic/qed/qed_chain.c‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static void qed_chain_free_pbl(struct qed_dev *cdev, struct qed_chain *chain)
4949
{
5050
struct device *dev = &cdev->pdev->dev;
5151
struct addr_tbl_entry *entry;
52-
u32 pbl_size, i;
52+
u32 i;
5353

5454
if (!chain->pbl.pp_addr_tbl)
5555
return;
@@ -63,11 +63,10 @@ static void qed_chain_free_pbl(struct qed_dev *cdev, struct qed_chain *chain)
6363
entry->dma_map);
6464
}
6565

66-
pbl_size = chain->page_cnt * QED_CHAIN_PBL_ENTRY_SIZE;
67-
6866
if (!chain->b_external_pbl)
69-
dma_free_coherent(dev, pbl_size, chain->pbl_sp.p_virt_table,
70-
chain->pbl_sp.p_phys_table);
67+
dma_free_coherent(dev, chain->pbl_sp.table_size,
68+
chain->pbl_sp.table_virt,
69+
chain->pbl_sp.table_phys);
7170

7271
vfree(chain->pbl.pp_addr_tbl);
7372
chain->pbl.pp_addr_tbl = NULL;
@@ -190,7 +189,7 @@ static int qed_chain_alloc_pbl(struct qed_dev *cdev, struct qed_chain *chain,
190189
struct device *dev = &cdev->pdev->dev;
191190
struct addr_tbl_entry *addr_tbl;
192191
dma_addr_t phys, pbl_phys;
193-
void *pbl_virt;
192+
__le64 *pbl_virt;
194193
u32 page_cnt, i;
195194
size_t size;
196195
void *virt;
@@ -214,7 +213,7 @@ static int qed_chain_alloc_pbl(struct qed_dev *cdev, struct qed_chain *chain,
214213

215214
chain->b_external_pbl = true;
216215
} else {
217-
size = array_size(page_cnt, QED_CHAIN_PBL_ENTRY_SIZE);
216+
size = array_size(page_cnt, sizeof(*pbl_virt));
218217
if (unlikely(size == SIZE_MAX))
219218
return -EOVERFLOW;
220219

@@ -225,8 +224,9 @@ static int qed_chain_alloc_pbl(struct qed_dev *cdev, struct qed_chain *chain,
225224
if (!pbl_virt)
226225
return -ENOMEM;
227226

228-
chain->pbl_sp.p_virt_table = pbl_virt;
229-
chain->pbl_sp.p_phys_table = pbl_phys;
227+
chain->pbl_sp.table_virt = pbl_virt;
228+
chain->pbl_sp.table_phys = pbl_phys;
229+
chain->pbl_sp.table_size = size;
230230

231231
for (i = 0; i < page_cnt; i++) {
232232
virt = dma_alloc_coherent(dev, QED_CHAIN_PAGE_SIZE, &phys,
@@ -240,8 +240,7 @@ static int qed_chain_alloc_pbl(struct qed_dev *cdev, struct qed_chain *chain,
240240
}
241241

242242
/* Fill the PBL table with the physical address of the page */
243-
*(dma_addr_t *)pbl_virt = phys;
244-
pbl_virt += QED_CHAIN_PBL_ENTRY_SIZE;
243+
pbl_virt[i] = cpu_to_le64(phys);
245244

246245
/* Keep the virtual address of the page */
247246
addr_tbl[i].virt_addr = virt;

‎drivers/net/ethernet/qlogic/qed/qed_sp_commands.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,11 @@ int qed_sp_pf_start(struct qed_hwfn *p_hwfn,
366366

367367
/* Place EQ address in RAMROD */
368368
DMA_REGPAIR_LE(p_ramrod->event_ring_pbl_addr,
369-
p_hwfn->p_eq->chain.pbl_sp.p_phys_table);
369+
qed_chain_get_pbl_phys(&p_hwfn->p_eq->chain));
370370
page_cnt = (u8)qed_chain_get_page_cnt(&p_hwfn->p_eq->chain);
371371
p_ramrod->event_ring_num_pages = page_cnt;
372372
DMA_REGPAIR_LE(p_ramrod->consolid_q_pbl_addr,
373-
p_hwfn->p_consq->chain.pbl_sp.p_phys_table);
373+
qed_chain_get_pbl_phys(&p_hwfn->p_consq->chain));
374374

375375
qed_tunn_set_pf_start_params(p_hwfn, p_tunn, &p_ramrod->tunnel_config);
376376

‎include/linux/qed/qed_chain.h‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ struct qed_chain {
127127

128128
/* Base address of a pre-allocated buffer for pbl */
129129
struct {
130-
dma_addr_t p_phys_table;
131-
void *p_virt_table;
130+
__le64 *table_virt;
131+
dma_addr_t table_phys;
132+
size_t table_size;
132133
} pbl_sp;
133134

134135
/* Address of first page of the chain - the address is required
@@ -146,7 +147,6 @@ struct qed_chain {
146147
bool b_external_pbl;
147148
};
148149

149-
#define QED_CHAIN_PBL_ENTRY_SIZE 8
150150
#define QED_CHAIN_PAGE_SIZE 0x1000
151151

152152
#define ELEMS_PER_PAGE(elem_size) \
@@ -236,7 +236,7 @@ static inline u32 qed_chain_get_page_cnt(struct qed_chain *p_chain)
236236

237237
static inline dma_addr_t qed_chain_get_pbl_phys(struct qed_chain *p_chain)
238238
{
239-
return p_chain->pbl_sp.p_phys_table;
239+
return p_chain->pbl_sp.table_phys;
240240
}
241241

242242
/**
@@ -527,8 +527,8 @@ static inline void qed_chain_init_params(struct qed_chain *p_chain,
527527
p_chain->capacity = p_chain->usable_per_page * page_cnt;
528528
p_chain->size = p_chain->elem_per_page * page_cnt;
529529

530-
p_chain->pbl_sp.p_phys_table = 0;
531-
p_chain->pbl_sp.p_virt_table = NULL;
530+
p_chain->pbl_sp.table_phys = 0;
531+
p_chain->pbl_sp.table_virt = NULL;
532532
p_chain->pbl.pp_addr_tbl = NULL;
533533
}
534534

@@ -569,8 +569,8 @@ static inline void qed_chain_init_pbl_mem(struct qed_chain *p_chain,
569569
dma_addr_t p_phys_pbl,
570570
struct addr_tbl_entry *pp_addr_tbl)
571571
{
572-
p_chain->pbl_sp.p_phys_table = p_phys_pbl;
573-
p_chain->pbl_sp.p_virt_table = p_virt_pbl;
572+
p_chain->pbl_sp.table_phys = p_phys_pbl;
573+
p_chain->pbl_sp.table_virt = p_virt_pbl;
574574
p_chain->pbl.pp_addr_tbl = pp_addr_tbl;
575575
}
576576

0 commit comments

Comments
 (0)