iio: gts-helper: add __counted_by_ptr to struct iio_gts
Subsystems (2)
PATCH 1/1 iio: gts-helper: add __counted_by_ptr to struct iio_gts
The compiler attribute __counted_by_ptr can be used by KASAN and UBSAN to detect out-of-bounds accesses to pointer fields in structs where a corresponding element count field is available.
In "struct iio_gts", there are multiple pointer fields associated with an element count. This patch annotates these pointer fields with "__counted_by_ptr" to improve runtime safety:
- 'hwgain_table': counted by 'num_hwgain'
- 'itime_table': counted by 'num_itime'
- 'per_time_avail_scale_tables': counted by 'num_itime'
- 'avail_all_scales_table': counted by 'num_avail_all_scales'
- 'avail_time_tables': counted by 'num_avail_time_tables'
To ensure that the count is set correctly before any pointer is accessed or assigned, we update "iio_init_iio_gts()" to set the counts prior to assigning the pointers.
--- drivers/iio/industrialio-gts-helper.c | 4 ++-- include/linux/iio/iio-gts-helper.h | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-)
drivers/iio/industrialio-gts-helper.c +2 -2
@@ -652,10 +652,10 @@ static int iio_init_iio_gts(int max_scale_int, int max_scale_nano,if (ret)return ret; - gts->hwgain_table = gain_tbl; gts->num_hwgain = num_gain; - gts->itime_table = tim_tbl; + gts->hwgain_table = gain_tbl; gts->num_itime = num_times; + gts->itime_table = tim_tbl;return iio_gts_sanity_check (gts); }
include/linux/iio/iio-gts-helper.h +5 -5
@@ -58,14 +58,14 @@ struct iio_itime_sel_mul {struct iio_gts {u64 max_scale; -const struct iio_gain_sel_pair *hwgain_table; + const struct iio_gain_sel_pair *hwgain_table __counted_by_ptr(num_hwgain);int num_hwgain; -const struct iio_itime_sel_mul *itime_table; + const struct iio_itime_sel_mul *itime_table __counted_by_ptr(num_itime);int num_itime; -int **per_time_avail_scale_tables; -int *avail_all_scales_table; + int **per_time_avail_scale_tables __counted_by_ptr(num_itime); + int *avail_all_scales_table __counted_by_ptr(num_avail_all_scales);int num_avail_all_scales; -int *avail_time_tables; + int *avail_time_tables __counted_by_ptr(num_avail_time_tables);int num_avail_time_tables; };
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Quoted 14 lines · Bill Wendling
The compiler attribute
__counted_by_ptrcan be used by KASAN and UBSAN to detect out-of-bounds accesses to pointer fields in structs where a corresponding element count field is available.In "struct
iio_gts", there are multiple pointer fields associated with an element count. This patch annotates these pointer fields with "__counted_by_ptr" to improve runtime safety:- '
hwgain_table': counted by 'num_hwgain'
- 'itime_table': counted by 'num_itime'
- 'per_time_avail_scale_tables': counted by 'num_itime'
- 'avail_all_scales_table': counted by 'num_avail_all_scales'
- 'avail_time_tables': counted by 'num_avail_time_tables'
Quoted 7 lines
To ensure that the count is set correctly before any pointer is accessed or assigned, we update "
iio_init_iio_gts()" to set the counts prior to assigning the pointers.
I like this! Thanks!
Quoted 46 lines
--- drivers/iio/industrialio-gts-helper.c | 4 ++-- include/linux/iio/iio-gts-helper.h | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-)drivers/iio/industrialio-gts-helper.c +2
-2@@ -652,10 +652,10 @@ static int iio_init_iio_gts(int max_scale_int, int max_scale_nano,if (ret)return ret; - gts->hwgain_table = gain_tbl; gts->num_hwgain = num_gain; - gts->itime_table = tim_tbl; + gts->hwgain_table = gain_tbl; gts->num_itime = num_times; + gts->itime_table = tim_tbl;return iio_gts_sanity_check (gts); }include/linux/iio/iio-gts-helper.h +5
-5@@ -58,14 +58,14 @@ struct iio_itime_sel_mul {struct iio_gts {u64 max_scale; -const struct iio_gain_sel_pair *hwgain_table; + const struct iio_gain_sel_pair *hwgain_table __counted_by_ptr(num_hwgain);int num_hwgain; -const struct iio_itime_sel_mul *itime_table; + const struct iio_itime_sel_mul *itime_table __counted_by_ptr(num_itime);int num_itime; -int **per_time_avail_scale_tables; -int *avail_all_scales_table; + int **per_time_avail_scale_tables __counted_by_ptr(num_itime); + int *avail_all_scales_table __counted_by_ptr(num_avail_all_scales);int num_avail_all_scales; -int *avail_time_tables; + int *avail_time_tables __counted_by_ptr(num_avail_time_tables);int num_avail_time_tables; };
Signature · 5 lines
Matti Vaittinen Linux kernel developer at ROHM Semiconductors Oulu Finland ~~ When things go utterly wrong vim users can always type :help! ~~
Quoted 14 lines · Bill Wendling
The compiler attribute
__counted_by_ptrcan be used by KASAN and UBSAN to detect out-of-bounds accesses to pointer fields in structs where a corresponding element count field is available.In "struct
iio_gts", there are multiple pointer fields associated with an element count. This patch annotates these pointer fields with "__counted_by_ptr" to improve runtime safety:- '
hwgain_table': counted by 'num_hwgain'
- 'itime_table': counted by 'num_itime'
- 'per_time_avail_scale_tables': counted by 'num_itime'
- 'avail_all_scales_table': counted by 'num_avail_all_scales'
- 'avail_time_tables': counted by 'num_avail_time_tables'
Quoted 7 lines
To ensure that the count is set correctly before any pointer is accessed or assigned, we update "
iio_init_iio_gts()" to set the counts prior to assigning the pointers.
Thanks
-Gustavo
Quoted 46 lines
--- drivers/iio/industrialio-gts-helper.c | 4 ++-- include/linux/iio/iio-gts-helper.h | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-)drivers/iio/industrialio-gts-helper.c +2
-2@@ -652,10 +652,10 @@ static int iio_init_iio_gts(int max_scale_int, int max_scale_nano,if (ret)return ret; - gts->hwgain_table = gain_tbl; gts->num_hwgain = num_gain; - gts->itime_table = tim_tbl; + gts->hwgain_table = gain_tbl; gts->num_itime = num_times; + gts->itime_table = tim_tbl;return iio_gts_sanity_check (gts); }include/linux/iio/iio-gts-helper.h +5
-5@@ -58,14 +58,14 @@ struct iio_itime_sel_mul {struct iio_gts {u64 max_scale; -const struct iio_gain_sel_pair *hwgain_table; + const struct iio_gain_sel_pair *hwgain_table __counted_by_ptr(num_hwgain);int num_hwgain; -const struct iio_itime_sel_mul *itime_table; + const struct iio_itime_sel_mul *itime_table __counted_by_ptr(num_itime);int num_itime; -int **per_time_avail_scale_tables; -int *avail_all_scales_table; + int **per_time_avail_scale_tables __counted_by_ptr(num_itime); + int *avail_all_scales_table __counted_by_ptr(num_avail_all_scales);int num_avail_all_scales; -int *avail_time_tables; + int *avail_time_tables __counted_by_ptr(num_avail_time_tables);int num_avail_time_tables; };
Quoted 14 lines · Bill Wendling
The compiler attribute
__counted_by_ptrcan be used by KASAN and UBSAN to detect out-of-bounds accesses to pointer fields in structs where a corresponding element count field is available.In "struct
iio_gts", there are multiple pointer fields associated with an element count. This patch annotates these pointer fields with "__counted_by_ptr" to improve runtime safety:- '
hwgain_table': counted by 'num_hwgain'
- 'itime_table': counted by 'num_itime'
- 'per_time_avail_scale_tables': counted by 'num_itime'
- 'avail_all_scales_table': counted by 'num_avail_all_scales'
- 'avail_time_tables': counted by 'num_avail_time_tables'
Quoted 3 lines
To ensure that the count is set correctly before any pointer is accessed or assigned, we update "
iio_init_iio_gts()" to set the counts prior to assigning the pointers.
"Accessed" yes, "assigned" no. :) Let's not churn on assignment ordering unless there is some problem I'm not seeing. The only time this should ever matter is if there is multithread visibility on the object already, but that would require both counter and pointer be set, so that'd be a general locking issue, but ordering during initialization doesn't matter.
Signature · 1 line
Kees Cook
Quoted 19 lines · Kees Cook
Quoted 14 lines · Bill Wendling
The compiler attribute
__counted_by_ptrcan be used by KASAN and UBSAN to detect out-of-bounds accesses to pointer fields in structs where a corresponding element count field is available.In "struct
iio_gts", there are multiple pointer fields associated with an element count. This patch annotates these pointer fields with "__counted_by_ptr" to improve runtime safety:- '
hwgain_table': counted by 'num_hwgain'
- 'itime_table': counted by 'num_itime'
- 'per_time_avail_scale_tables': counted by 'num_itime'
- 'avail_all_scales_table': counted by 'num_avail_all_scales'
- 'avail_time_tables': counted by 'num_avail_time_tables'Quoted 3 lines
To ensure that the count is set correctly before any pointer is accessed or assigned, we update "
iio_init_iio_gts()" to set the counts prior to assigning the pointers.
Quoted 7 lines
"Accessed" yes, "assigned" no. :) Let's not churn on assignment ordering unless there is some problem I'm not seeing. The only time this should ever matter is if there is multithread visibility on the object already, but that would require both counter and pointer be set, so that'd be a general locking issue, but ordering during initialization doesn't matter.
In a separate patch, Sashiko flagged the assignments being "out of order". It's not an error, but I did submit a "v2" that reordered the assignments (that Sashiko is still complaining about, but I think it's a pre-existing issue). I can weed out the needless reordering changes from the patches, however, I like the idea of people thinking about the ordering of such assignments.
It's a small thing I know, but I need something to hold on to. :-D
-bw
PATCH v2 1/1 iio: gts-helper: add __counted_by_ptr to struct iio_gts
In reply to Bill WendlingThe compiler attribute __counted_by_ptr can be used by KASAN and UBSAN to detect out-of-bounds accesses to pointer fields in structs where a corresponding element count field is available.
In "struct iio_gts", there are multiple pointer fields associated with an element count. This patch annotates these pointer fields with "__counted_by_ptr" to improve runtime safety:
- 'hwgain_table': counted by 'num_hwgain'
- 'itime_table': counted by 'num_itime'
- 'per_time_avail_scale_tables': counted by 'num_itime'
- 'avail_all_scales_table': counted by 'num_avail_all_scales'
- 'avail_time_tables': counted by 'num_avail_time_tables'
To ensure that the count is set correctly before any pointer is accessed or assigned, we update "iio_init_iio_gts()" to set the counts prior to assigning the pointers.
---
v2: Remove reordering of assignments. It's not necessary as an assignment of
the pointer isn't an access that's checked by KASAN.
--- include/linux/iio/iio-gts-helper.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
include/linux/iio/iio-gts-helper.h +5 -5
@@ -58,14 +58,14 @@ struct iio_itime_sel_mul {struct iio_gts {u64 max_scale; -const struct iio_gain_sel_pair *hwgain_table; + const struct iio_gain_sel_pair *hwgain_table __counted_by_ptr(num_hwgain);int num_hwgain; -const struct iio_itime_sel_mul *itime_table; + const struct iio_itime_sel_mul *itime_table __counted_by_ptr(num_itime);int num_itime; -int **per_time_avail_scale_tables; -int *avail_all_scales_table; + int **per_time_avail_scale_tables __counted_by_ptr(num_itime); + int *avail_all_scales_table __counted_by_ptr(num_avail_all_scales);int num_avail_all_scales; -int *avail_time_tables; + int *avail_time_tables __counted_by_ptr(num_avail_time_tables);int num_avail_time_tables; };
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
Jonathan Cameron
maintainerQuoted 18 lines · On Wed, 23 Sep 2026 19:24:50 +0000 Bill Wendling <[email protected]> wrote:
The compiler attribute
__counted_by_ptrcan be used by KASAN and UBSAN to detect out-of-bounds accesses to pointer fields in structs where a corresponding element count field is available.In "struct
iio_gts", there are multiple pointer fields associated with an element count. This patch annotates these pointer fields with "__counted_by_ptr" to improve runtime safety:- '
hwgain_table': counted by 'num_hwgain'
- 'itime_table': counted by 'num_itime'
- 'per_time_avail_scale_tables': counted by 'num_itime'
- 'avail_all_scales_table': counted by 'num_avail_all_scales'
- 'avail_time_tables': counted by 'num_avail_time_tables'To ensure that the count is set correctly before any pointer is accessed or assigned, we update "
iio_init_iio_gts()" to set the counts prior to assigning the pointers.
Quoted 5 lines
Assisted-by LLMSigned-off-by Bill Wendling <[email protected]>Acked-by Matti Vaittinen <[email protected]>Reviewed-by Gustavo A. R. Silva <[email protected]>
Applied to the testing branch of iio.git. So effectively queued for 7.4 merge window.
Thanks,
Jonathan