Skip to contentSkip to conversation

rcu: Annotate struct kvfree_rcu_bulk_data with __counted_by()

PATCH In review Thorsten Blum 4 participants 4 messages Aug 7, 2024 – Aug 8, 2024 RCU

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rcu: Annotate struct kvfree_rcu_bulk_data with __counted_by()

Add the __counted_by compiler attribute to the flexible array member records to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.

Increment nr_records before adding a new pointer to the records array.

---
 kernel/rcu/tree.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/rcu/tree.c +3 -2
index e641cc681901..76d8d75dd8b3 100644
@@ -3227,7 +3227,7 @@ struct kvfree_rcu_bulk_data {
 	struct list_head list;
 	struct rcu_gp_oldstate gp_snap;
 	unsigned long nr_records;
-	void *records[];
+	void *records[] __counted_by(nr_records);
 };
 
 /*
@@ -3767,7 +3767,8 @@ add_ptr_to_bulk_krc_lock(struct kfree_rcu_cpu **krcp,
 	}
 
 	// Finally insert and update the GP for this page.
-	bnode->records[bnode->nr_records++] = ptr;
+	bnode->nr_records++;
+	bnode->records[bnode->nr_records - 1] = ptr;
 	get_state_synchronize_rcu_full(&bnode->gp_snap);
 	atomic_inc(&(*krcp)->bulk_count[idx]);
 
Signature · 1 line
2.45.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Thorsten Blum

Add the __counted_by compiler attribute to the flexible array member records to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.

Increment nr_records before adding a new pointer to the records array.

Looks good.

Quoted 1 line

Thanks
--
Gustavo

Quoted 27 lines
---
  kernel/rcu/tree.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
kernel/rcu/tree.c +3 -2
index e641cc681901..76d8d75dd8b3 100644
@@ -3227,7 +3227,7 @@ struct kvfree_rcu_bulk_data {
  	struct list_head list;
  	struct rcu_gp_oldstate gp_snap;
  	unsigned long nr_records;
-	void *records[];
+	void *records[] __counted_by(nr_records);
  };
 
  /*
@@ -3767,7 +3767,8 @@ add_ptr_to_bulk_krc_lock(struct kfree_rcu_cpu **krcp,
  	}
 
  	// Finally insert and update the GP for this page.
-	bnode->records[bnode->nr_records++] = ptr;
+	bnode->nr_records++;
+	bnode->records[bnode->nr_records - 1] = ptr;
  	get_state_synchronize_rcu_full(&bnode->gp_snap);
  	atomic_inc(&(*krcp)->bulk_count[idx]);
 
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Thorsten Blum
Quoted 37 lines · Thorsten Blum

Add the __counted_by compiler attribute to the flexible array member records to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.

Increment nr_records before adding a new pointer to the records array.

---
 kernel/rcu/tree.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/rcu/tree.c +3 -2
index e641cc681901..76d8d75dd8b3 100644
@@ -3227,7 +3227,7 @@ struct kvfree_rcu_bulk_data {
 	struct list_head list;
 	struct rcu_gp_oldstate gp_snap;
 	unsigned long nr_records;
-	void *records[];
+	void *records[] __counted_by(nr_records);
 };
 
 /*
@@ -3767,7 +3767,8 @@ add_ptr_to_bulk_krc_lock(struct kfree_rcu_cpu **krcp,
 	}
 
 	// Finally insert and update the GP for this page.
-	bnode->records[bnode->nr_records++] = ptr;
+	bnode->nr_records++;
+	bnode->records[bnode->nr_records - 1] = ptr;
 	get_state_synchronize_rcu_full(&bnode->gp_snap);
 	atomic_inc(&(*krcp)->bulk_count[idx]);
 
Signature · 1 line
2.45.2

--
Uladzislau Rezki

maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Thorsten Blum
Quoted 7 lines · Thorsten Blum

Add the __counted_by compiler attribute to the flexible array member records to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.

Increment nr_records before adding a new pointer to the records array.

Quoted 30 lines
---
 kernel/rcu/tree.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/rcu/tree.c +3 -2
index e641cc681901..76d8d75dd8b3 100644
@@ -3227,7 +3227,7 @@ struct kvfree_rcu_bulk_data {
 	struct list_head list;
 	struct rcu_gp_oldstate gp_snap;
 	unsigned long nr_records;
-	void *records[];
+	void *records[] __counted_by(nr_records);
 };
 
 /*
@@ -3767,7 +3767,8 @@ add_ptr_to_bulk_krc_lock(struct kfree_rcu_cpu **krcp,
 	}
 
 	// Finally insert and update the GP for this page.
-	bnode->records[bnode->nr_records++] = ptr;
+	bnode->nr_records++;
+	bnode->records[bnode->nr_records - 1] = ptr;
 	get_state_synchronize_rcu_full(&bnode->gp_snap);
 	atomic_inc(&(*krcp)->bulk_count[idx]);
 
Signature · 1 line
2.45.2