Skip to contentSkip to conversation

atm: nicstar: simplify allocation

PATCH In review Rosen Penev 2 participants 2 messages Mar 27 – Mar 31

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 atm: nicstar: simplify allocation

Use a flexible array member with kzalloc_flex to combine allocations into one.

Add __counted_by for extra runtime analysis. Move counting variable assignment to right after allocation as required by __counted_by.

---
v3: reduce to 80 columns
v2: use kzalloc_flex

 drivers/atm/nicstar.c | 18 ++++++------------
 drivers/atm/nicstar.h |  5 +++--
 2 files changed, 9 insertions(+), 14 deletions(-)
drivers/atm/nicstar.c +6 -12
index 24e51343df15..c0bed85daf96 100644
@@ -867,23 +867,18 @@ static scq_info *get_scq(ns_dev *card, int size, u32 scd)
 	if (size != VBR_SCQSIZE && size != CBR_SCQSIZE)
 		return NULL;
 
-	scq = kmalloc_obj(*scq);
+	scq = kzalloc_flex(*scq, skb, size / NS_SCQE_SIZE);
 	if (!scq)
 		return NULL;
-        scq->org = dma_alloc_coherent(&card->pcidev->dev,
-				      2 * size,  &scq->dma, GFP_KERNEL);
+
+	scq->num_entries = size / NS_SCQE_SIZE;
+
+	scq->org = dma_alloc_coherent(&card->pcidev->dev, 2 * size, &scq->dma,
+				      GFP_KERNEL);
 	if (!scq->org) {
 		kfree(scq);
 		return NULL;
 	}
-	scq->skb = kzalloc_objs(*scq->skb, size / NS_SCQE_SIZE);
-	if (!scq->skb) {
-		dma_free_coherent(&card->pcidev->dev,
-				  2 * size, scq->org, scq->dma);
-		kfree(scq);
-		return NULL;
-	}
-	scq->num_entries = size / NS_SCQE_SIZE;
 	scq->base = PTR_ALIGN(scq->org, size);
 	scq->next = scq->base;
 	scq->last = scq->base + (scq->num_entries - 1);
@@ -928,7 +923,6 @@ static void free_scq(ns_dev *card, scq_info *scq, struct atm_vcc *vcc)
 				}
 			}
 	}
-	kfree(scq->skb);
 	dma_free_coherent(&card->pcidev->dev,
 			  2 * (scq->num_entries == VBR_SCQ_NUM_ENTRIES ?
 			       VBR_SCQSIZE : CBR_SCQSIZE),
drivers/atm/nicstar.h +3 -2
index 1b7f1dfc1735..76635d46016e 100644
@@ -667,14 +667,15 @@ typedef struct scq_info {
 	ns_scqe *next;
 	volatile ns_scqe *tail;	/* Not related to the nicstar register */
 	unsigned num_entries;
-	struct sk_buff **skb;	/* Pointer to an array of pointers
-				   to the sk_buffs used for tx */
 	u32 scd;		/* SRAM address of the corresponding
 				   SCD */
 	int tbd_count;		/* Only meaningful on variable rate */
 	wait_queue_head_t scqfull_waitq;
 	volatile char full;	/* SCQ full indicator */
 	spinlock_t lock;	/* SCQ spinlock */
+	struct sk_buff *skb[] __counted_by(num_entries);
+	/* Pointer to an array of pointers
+	   to the sk_buffs used for tx */
 } scq_info;
 
 typedef struct rsq_info {

--
2.53.0

#1 lore raw
Quoted 4 lines · Rosen Penev

Use a flexible array member with kzalloc_flex to combine allocations into one.

Add __counted_by for extra runtime analysis.

Move counting variable

Quoted 1 line

assignment to right after allocation as required by __counted_by.

This is misinformation and should be phrased differently[1]

-Gustavo

[1] https://lore.kernel.org/linux-hardening/[email protected]/

Quoted 77 lines

---
v3: reduce to 80 columns
v2: use kzalloc_flex

  drivers/atm/nicstar.c | 18 ++++++------------
  drivers/atm/nicstar.h |  5 +++--
  2 files changed, 9 insertions(+), 14 deletions(-)
drivers/atm/nicstar.c +6 -12
index 24e51343df15..c0bed85daf96 100644
@@ -867,23 +867,18 @@ static scq_info *get_scq(ns_dev *card, int size, u32 scd)
  	if (size != VBR_SCQSIZE && size != CBR_SCQSIZE)
  		return NULL;
 
-	scq = kmalloc_obj(*scq);
+	scq = kzalloc_flex(*scq, skb, size / NS_SCQE_SIZE);
  	if (!scq)
  		return NULL;
-        scq->org = dma_alloc_coherent(&card->pcidev->dev,
-				      2 * size,  &scq->dma, GFP_KERNEL);
+
+	scq->num_entries = size / NS_SCQE_SIZE;
+
+	scq->org = dma_alloc_coherent(&card->pcidev->dev, 2 * size, &scq->dma,
+				      GFP_KERNEL);
  	if (!scq->org) {
  		kfree(scq);
  		return NULL;
  	}
-	scq->skb = kzalloc_objs(*scq->skb, size / NS_SCQE_SIZE);
-	if (!scq->skb) {
-		dma_free_coherent(&card->pcidev->dev,
-				  2 * size, scq->org, scq->dma);
-		kfree(scq);
-		return NULL;
-	}
-	scq->num_entries = size / NS_SCQE_SIZE;
  	scq->base = PTR_ALIGN(scq->org, size);
  	scq->next = scq->base;
  	scq->last = scq->base + (scq->num_entries - 1);
@@ -928,7 +923,6 @@ static void free_scq(ns_dev *card, scq_info *scq, struct atm_vcc *vcc)
  				}
  			}
  	}
-	kfree(scq->skb);
  	dma_free_coherent(&card->pcidev->dev,
  			  2 * (scq->num_entries == VBR_SCQ_NUM_ENTRIES ?
  			       VBR_SCQSIZE : CBR_SCQSIZE),
drivers/atm/nicstar.h +3 -3
index 1b7f1dfc1735..76635d46016e 100644
@@ -667,14 +667,15 @@ typedef struct scq_info {
  	ns_scqe *next;
  	volatile ns_scqe *tail;	/* Not related to the nicstar register */
  	unsigned num_entries;
-	struct sk_buff **skb;	/* Pointer to an array of pointers
-				   to the sk_buffs used for tx */
  	u32 scd;		/* SRAM address of the corresponding
  				   SCD */
  	int tbd_count;		/* Only meaningful on variable rate */
  	wait_queue_head_t scqfull_waitq;
  	volatile char full;	/* SCQ full indicator */
  	spinlock_t lock;	/* SCQ spinlock */
+	struct sk_buff *skb[] __counted_by(num_entries);
+	/* Pointer to an array of pointers
+	   to the sk_buffs used for tx */
  } scq_info;
 
  typedef struct rsq_info {
--

2.53.0