Commit 9b0dfef4 authored by Rasmus Villemoes's avatar Rasmus Villemoes Committed by Jakub Kicinski

ethernet: ucc_geth: simplify rx/tx allocations

Since kmalloc() is nowadays [1] guaranteed to return naturally
aligned (i.e., aligned to the size itself) memory for power-of-2
sizes, we don't need to over-allocate the align amount, compute an
aligned address within the allocation, and (for later freeing) also
storing the original pointer [2].

Instead, just round up the length we want to allocate to the alignment
requirements, then round that up to the next power of 2. In theory,
this could allocate up to about twice as much memory as we needed.  In
practice, (a) kmalloc() would in most cases anyway return a
power-of-2-sized allocation and (b) with the default values of the
bdRingLen[RT]x fields, the length is already itself a power of 2
greater than the alignment.

So we actually end up saving memory compared to the current
situtation (e.g. for tx, we currently allocate 128+32 bytes, which
kmalloc() likely rounds up to 192 or 256; with this patch, we just
allocate 128 bytes.) Also struct ucc_geth_private becomes a little
smaller.

[1] 59bb4798 ("mm, sl[aou]b: guarantee natural alignment for
kmalloc(power-of-two)")

[2] That storing was anyway done in a u32, which works on 32 bit
machines, but is not very elegant and certainly makes a reader of the
code pause for a while.
Signed-off-by: default avatarRasmus Villemoes <rasmus.villemoes@prevas.dk>
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 53f49d86
...@@ -1835,7 +1835,7 @@ static void ucc_geth_free_rx(struct ucc_geth_private *ugeth) ...@@ -1835,7 +1835,7 @@ static void ucc_geth_free_rx(struct ucc_geth_private *ugeth)
kfree(ugeth->rx_skbuff[i]); kfree(ugeth->rx_skbuff[i]);
kfree((void *)ugeth->rx_bd_ring_offset[i]); kfree(ugeth->p_rx_bd_ring[i]);
ugeth->p_rx_bd_ring[i] = NULL; ugeth->p_rx_bd_ring[i] = NULL;
} }
} }
...@@ -1872,11 +1872,9 @@ static void ucc_geth_free_tx(struct ucc_geth_private *ugeth) ...@@ -1872,11 +1872,9 @@ static void ucc_geth_free_tx(struct ucc_geth_private *ugeth)
kfree(ugeth->tx_skbuff[i]); kfree(ugeth->tx_skbuff[i]);
if (ugeth->p_tx_bd_ring[i]) { kfree(ugeth->p_tx_bd_ring[i]);
kfree((void *)ugeth->tx_bd_ring_offset[i]);
ugeth->p_tx_bd_ring[i] = NULL; ugeth->p_tx_bd_ring[i] = NULL;
} }
}
} }
...@@ -2150,25 +2148,15 @@ static int ucc_geth_alloc_tx(struct ucc_geth_private *ugeth) ...@@ -2150,25 +2148,15 @@ static int ucc_geth_alloc_tx(struct ucc_geth_private *ugeth)
/* Allocate Tx bds */ /* Allocate Tx bds */
for (j = 0; j < ucc_geth_tx_queues(ug_info); j++) { for (j = 0; j < ucc_geth_tx_queues(ug_info); j++) {
u32 align = UCC_GETH_TX_BD_RING_ALIGNMENT; u32 align = max(UCC_GETH_TX_BD_RING_ALIGNMENT,
UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT);
/* Allocate in multiple of u32 alloc;
UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT,
according to spec */ length = ug_info->bdRingLenTx[j] * sizeof(struct qe_bd);
length = ((ug_info->bdRingLenTx[j] * sizeof(struct qe_bd)) alloc = round_up(length, align);
/ UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) alloc = roundup_pow_of_two(alloc);
* UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
if ((ug_info->bdRingLenTx[j] * sizeof(struct qe_bd)) % ugeth->p_tx_bd_ring[j] = kmalloc(alloc, GFP_KERNEL);
UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT)
length += UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
ugeth->tx_bd_ring_offset[j] =
(u32) kmalloc((u32) (length + align), GFP_KERNEL);
if (ugeth->tx_bd_ring_offset[j] != 0)
ugeth->p_tx_bd_ring[j] =
(u8 __iomem *)((ugeth->tx_bd_ring_offset[j] +
align) & ~(align - 1));
if (!ugeth->p_tx_bd_ring[j]) { if (!ugeth->p_tx_bd_ring[j]) {
if (netif_msg_ifup(ugeth)) if (netif_msg_ifup(ugeth))
...@@ -2176,9 +2164,7 @@ static int ucc_geth_alloc_tx(struct ucc_geth_private *ugeth) ...@@ -2176,9 +2164,7 @@ static int ucc_geth_alloc_tx(struct ucc_geth_private *ugeth)
return -ENOMEM; return -ENOMEM;
} }
/* Zero unused end of bd ring, according to spec */ /* Zero unused end of bd ring, according to spec */
memset_io((void __iomem *)(ugeth->p_tx_bd_ring[j] + memset(ugeth->p_tx_bd_ring[j] + length, 0, alloc - length);
ug_info->bdRingLenTx[j] * sizeof(struct qe_bd)), 0,
length - ug_info->bdRingLenTx[j] * sizeof(struct qe_bd));
} }
/* Init Tx bds */ /* Init Tx bds */
...@@ -2225,15 +2211,13 @@ static int ucc_geth_alloc_rx(struct ucc_geth_private *ugeth) ...@@ -2225,15 +2211,13 @@ static int ucc_geth_alloc_rx(struct ucc_geth_private *ugeth)
/* Allocate Rx bds */ /* Allocate Rx bds */
for (j = 0; j < ucc_geth_rx_queues(ug_info); j++) { for (j = 0; j < ucc_geth_rx_queues(ug_info); j++) {
u32 align = UCC_GETH_RX_BD_RING_ALIGNMENT; u32 align = UCC_GETH_RX_BD_RING_ALIGNMENT;
u32 alloc;
length = ug_info->bdRingLenRx[j] * sizeof(struct qe_bd); length = ug_info->bdRingLenRx[j] * sizeof(struct qe_bd);
ugeth->rx_bd_ring_offset[j] = alloc = round_up(length, align);
(u32) kmalloc((u32) (length + align), GFP_KERNEL); alloc = roundup_pow_of_two(alloc);
if (ugeth->rx_bd_ring_offset[j] != 0)
ugeth->p_rx_bd_ring[j] =
(u8 __iomem *)((ugeth->rx_bd_ring_offset[j] +
align) & ~(align - 1));
ugeth->p_rx_bd_ring[j] = kmalloc(alloc, GFP_KERNEL);
if (!ugeth->p_rx_bd_ring[j]) { if (!ugeth->p_rx_bd_ring[j]) {
if (netif_msg_ifup(ugeth)) if (netif_msg_ifup(ugeth))
pr_err("Can not allocate memory for Rx bd rings\n"); pr_err("Can not allocate memory for Rx bd rings\n");
......
...@@ -1181,9 +1181,7 @@ struct ucc_geth_private { ...@@ -1181,9 +1181,7 @@ struct ucc_geth_private {
struct ucc_geth_rx_bd_queues_entry __iomem *p_rx_bd_qs_tbl; struct ucc_geth_rx_bd_queues_entry __iomem *p_rx_bd_qs_tbl;
u32 rx_bd_qs_tbl_offset; u32 rx_bd_qs_tbl_offset;
u8 __iomem *p_tx_bd_ring[NUM_TX_QUEUES]; u8 __iomem *p_tx_bd_ring[NUM_TX_QUEUES];
u32 tx_bd_ring_offset[NUM_TX_QUEUES];
u8 __iomem *p_rx_bd_ring[NUM_RX_QUEUES]; u8 __iomem *p_rx_bd_ring[NUM_RX_QUEUES];
u32 rx_bd_ring_offset[NUM_RX_QUEUES];
u8 __iomem *confBd[NUM_TX_QUEUES]; u8 __iomem *confBd[NUM_TX_QUEUES];
u8 __iomem *txBd[NUM_TX_QUEUES]; u8 __iomem *txBd[NUM_TX_QUEUES];
u8 __iomem *rxBd[NUM_RX_QUEUES]; u8 __iomem *rxBd[NUM_RX_QUEUES];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment