* [PATCH 0/2] of: address: Fix empty resource handling in __of_address_resource_bounds()
@ 2025-01-20 14:09 Thomas Weißschuh
2025-01-20 14:09 ` [PATCH 1/2] " Thomas Weißschuh
2025-01-20 14:09 ` [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() Thomas Weißschuh
0 siblings, 2 replies; 7+ messages in thread
From: Thomas Weißschuh @ 2025-01-20 14:09 UTC (permalink / raw)
To: Rob Herring, Saravana Kannan
Cc: Basharath Hussain Khaja, devicetree, linux-kernel,
Thomas Weißschuh, stable
Also add a kunit testcase to make sure the function works correctly now
and doesn't regress in the future.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
Thomas Weißschuh (2):
of: address: Fix empty resource handling in __of_address_resource_bounds()
of: address: Add kunit test for __of_address_resource_bounds()
drivers/of/address.c | 17 +++----
drivers/of/of_private.h | 4 ++
drivers/of/of_test.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 132 insertions(+), 9 deletions(-)
---
base-commit: ffd294d346d185b70e28b1a28abe367bbfe53c04
change-id: 20250120-of-address-overflow-a59476362885
Best regards,
--
Thomas Weißschuh <thomas.weissschuh@linutronix.de>
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 1/2] of: address: Fix empty resource handling in __of_address_resource_bounds() 2025-01-20 14:09 [PATCH 0/2] of: address: Fix empty resource handling in __of_address_resource_bounds() Thomas Weißschuh @ 2025-01-20 14:09 ` Thomas Weißschuh 2025-01-24 22:00 ` Rob Herring (Arm) 2025-01-20 14:09 ` [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() Thomas Weißschuh 1 sibling, 1 reply; 7+ messages in thread From: Thomas Weißschuh @ 2025-01-20 14:09 UTC (permalink / raw) To: Rob Herring, Saravana Kannan Cc: Basharath Hussain Khaja, devicetree, linux-kernel, Thomas Weißschuh, stable "resource->end" needs to always be equal to "resource->start + size - 1". The previous version of the function did not perform the "- 1" in case of an empty resource. Also make sure to allow an empty resource at address 0. Reported-by: Basharath Hussain Khaja <basharath@couthit.com> Closes: https://lore.kernel.org/lkml/20250108140414.13530-1-basharath@couthit.com/ Fixes: 1a52a094c2f0 ("of: address: Unify resource bounds overflow checking") Cc: stable@vger.kernel.org Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de> --- drivers/of/address.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/drivers/of/address.c b/drivers/of/address.c index 8770004d9b085f1ff40f693d695d284a6ef3dfde..5c0663066a7f3816a05077f99124ca25e3c152d7 100644 --- a/drivers/of/address.c +++ b/drivers/of/address.c @@ -200,17 +200,15 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size) { - u64 end = start; - if (overflows_type(start, r->start)) return -EOVERFLOW; - if (size && check_add_overflow(end, size - 1, &end)) - return -EOVERFLOW; - if (overflows_type(end, r->end)) - return -EOVERFLOW; r->start = start; - r->end = end; + + if (!size) + r->end = wrapping_sub(typeof(r->end), r->start, 1); + else if (size && check_add_overflow(r->start, size - 1, &r->end)) + return -EOVERFLOW; return 0; } -- 2.48.1 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] of: address: Fix empty resource handling in __of_address_resource_bounds() 2025-01-20 14:09 ` [PATCH 1/2] " Thomas Weißschuh @ 2025-01-24 22:00 ` Rob Herring (Arm) 0 siblings, 0 replies; 7+ messages in thread From: Rob Herring (Arm) @ 2025-01-24 22:00 UTC (permalink / raw) To: Thomas Weißschuh Cc: devicetree, linux-kernel, Basharath Hussain Khaja, Saravana Kannan, stable On Mon, 20 Jan 2025 15:09:40 +0100, Thomas Weißschuh wrote: > "resource->end" needs to always be equal to "resource->start + size - 1". > The previous version of the function did not perform the "- 1" in case > of an empty resource. > > Also make sure to allow an empty resource at address 0. > > Reported-by: Basharath Hussain Khaja <basharath@couthit.com> > Closes: https://lore.kernel.org/lkml/20250108140414.13530-1-basharath@couthit.com/ > Fixes: 1a52a094c2f0 ("of: address: Unify resource bounds overflow checking") > Cc: stable@vger.kernel.org > Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de> > --- > drivers/of/address.c | 12 +++++------- > 1 file changed, 5 insertions(+), 7 deletions(-) > Applied, thanks! Please resend the kunit test with 0-day issues fixed. Rob ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() 2025-01-20 14:09 [PATCH 0/2] of: address: Fix empty resource handling in __of_address_resource_bounds() Thomas Weißschuh 2025-01-20 14:09 ` [PATCH 1/2] " Thomas Weißschuh @ 2025-01-20 14:09 ` Thomas Weißschuh 2025-01-20 19:31 ` kernel test robot ` (2 more replies) 1 sibling, 3 replies; 7+ messages in thread From: Thomas Weißschuh @ 2025-01-20 14:09 UTC (permalink / raw) To: Rob Herring, Saravana Kannan Cc: Basharath Hussain Khaja, devicetree, linux-kernel, Thomas Weißschuh The overflow checking has to deal with different datatypes and edgecases. Add a new kunit testcase to make sure it works correctly. Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de> --- Technically it's possible to run this unittest with !CONFIG_OF_ADDRESS, so there is an explicit check inside the test. It would also be possible to add a dedicated source file, but that seemed like a lot of churn to me. --- drivers/of/address.c | 5 +- drivers/of/of_private.h | 4 ++ drivers/of/of_test.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 127 insertions(+), 2 deletions(-) diff --git a/drivers/of/address.c b/drivers/of/address.c index 5c0663066a7f3816a05077f99124ca25e3c152d7..84b8d16919dc4aae966a09f7fe0606b2e51c3dde 100644 --- a/drivers/of/address.c +++ b/drivers/of/address.c @@ -16,6 +16,8 @@ #include <linux/string.h> #include <linux/dma-direct.h> /* for bus_dma_region */ +#include <kunit/visibility.h> + #include "of_private.h" /* Max address size we deal with */ @@ -198,7 +200,7 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, #endif /* CONFIG_PCI */ -static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size) +VISIBLE_IF_KUNIT int __of_address_resource_bounds(struct resource *r, u64 start, u64 size) { if (overflows_type(start, r->start)) return -EOVERFLOW; @@ -212,6 +214,7 @@ static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size) return 0; } +EXPORT_SYMBOL_IF_KUNIT(__of_address_resource_bounds); /* * of_pci_range_to_resource - Create a resource from an of_pci_range diff --git a/drivers/of/of_private.h b/drivers/of/of_private.h index ea5a0951ec5e107bab265ab5f6c043e2bfb15ecc..0d29d71ac7c62ae91bfac0c8d1518622729575cd 100644 --- a/drivers/of/of_private.h +++ b/drivers/of/of_private.h @@ -188,4 +188,8 @@ void __init fdt_scan_reserved_mem_reg_nodes(void); bool of_fdt_device_is_available(const void *blob, unsigned long node); +#if IS_ENABLED(CONFIG_KUNIT) +int __of_address_resource_bounds(struct resource *r, u64 start, u64 size); +#endif + #endif /* _LINUX_OF_PRIVATE_H */ diff --git a/drivers/of/of_test.c b/drivers/of/of_test.c index b0557ded838fdf70f0b679c31ead38f501371304..b8eb1d4b76fdf3a16fc5dd24f8e6ed0c3fca5e9c 100644 --- a/drivers/of/of_test.c +++ b/drivers/of/of_test.c @@ -2,6 +2,7 @@ /* * KUnit tests for OF APIs */ +#include <linux/ioport.h> #include <linux/module.h> #include <linux/of.h> @@ -54,8 +55,125 @@ static struct kunit_suite of_dtb_suite = { .init = of_dtb_test_init, }; +struct of_address_resource_bounds_case { + u64 start; + u64 size; + int ret; + + resource_size_t res_start; + resource_size_t res_end; +}; + +static void of_address_resource_bounds_case_desc(const struct of_address_resource_bounds_case *p, + char *name) +{ + snprintf(name, KUNIT_PARAM_DESC_SIZE, "start=0x%016llx,size=0x%016llx", p->start, p->size); +} + +static const struct of_address_resource_bounds_case of_address_resource_bounds_cases[] = { + { + .start = 0, + .size = 0, + .ret = 0, + .res_start = 0, + .res_end = -1, + }, + { + .start = 0, + .size = 0x1000, + .ret = 0, + .res_start = 0, + .res_end = 0xfff, + }, + { + .start = 0x1000, + .size = 0, + .ret = 0, + .res_start = 0x1000, + .res_end = 0xfff, + }, + { + .start = 0x1000, + .size = 0x1000, + .ret = 0, + .res_start = 0x1000, + .res_end = 0x1fff, + }, + { + .start = 1, + .size = RESOURCE_SIZE_MAX, + .ret = 0, + .res_start = 1, + .res_end = RESOURCE_SIZE_MAX, + }, + { + .start = RESOURCE_SIZE_MAX, + .size = 1, + .ret = 0, + .res_start = RESOURCE_SIZE_MAX, + .res_end = RESOURCE_SIZE_MAX, + }, + { + .start = 2, + .size = RESOURCE_SIZE_MAX, + .ret = -EOVERFLOW, + }, + { + .start = RESOURCE_SIZE_MAX, + .size = 2, + .ret = -EOVERFLOW, + }, + { + .start = 0x100000000ULL, + .size = 1, + .ret = sizeof(resource_size_t) > sizeof(u32) ? 0 : -EOVERFLOW, + .res_start = (resource_size_t)0x100000000, + .res_end = (resource_size_t)0x100000000, + }, + { + .start = 0x1000, + .size = 0xffffffff, + .ret = sizeof(resource_size_t) > sizeof(u32) ? 0 : -EOVERFLOW, + .res_start = (resource_size_t)0x1000, + .res_end = (resource_size_t)0x100000ffe, + }, +}; + +KUNIT_ARRAY_PARAM(of_address_resource_bounds, + of_address_resource_bounds_cases, of_address_resource_bounds_case_desc); + +static void of_address_resource_bounds(struct kunit *test) +{ + const struct of_address_resource_bounds_case *param; + struct resource r; /* Intentionally uninitialized */ + int ret; + + if (!IS_ENABLED(CONFIG_OF_ADDRESS)) + kunit_skip(test, "CONFIG_OF_ADDRESS not enabled\n"); + + param = test->param_value; + + ret = __of_address_resource_bounds(&r, param->start, param->size); + KUNIT_EXPECT_EQ(test, param->ret, ret); + if (ret == 0) { + KUNIT_EXPECT_EQ(test, param->res_start, r.start); + KUNIT_EXPECT_EQ(test, param->res_end, r.end); + KUNIT_EXPECT_EQ(test, param->size, resource_size(&r)); + } +} + +static struct kunit_case of_address_test_cases[] = { + KUNIT_CASE_PARAM(of_address_resource_bounds, of_address_resource_bounds_gen_params), + {} +}; + +static struct kunit_suite of_address_suite = { + .name = "of_address", + .test_cases = of_address_test_cases, +}; + kunit_test_suites( - &of_dtb_suite, + &of_dtb_suite, &of_address_suite, ); MODULE_DESCRIPTION("KUnit tests for OF APIs"); MODULE_LICENSE("GPL"); -- 2.48.1 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() 2025-01-20 14:09 ` [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() Thomas Weißschuh @ 2025-01-20 19:31 ` kernel test robot 2025-01-21 1:57 ` kernel test robot 2025-01-26 9:59 ` kernel test robot 2 siblings, 0 replies; 7+ messages in thread From: kernel test robot @ 2025-01-20 19:31 UTC (permalink / raw) To: Thomas Weißschuh, Rob Herring, Saravana Kannan Cc: oe-kbuild-all, Basharath Hussain Khaja, devicetree, linux-kernel, Thomas Weißschuh Hi Thomas, kernel test robot noticed the following build errors: [auto build test ERROR on ffd294d346d185b70e28b1a28abe367bbfe53c04] url: https://github.com/intel-lab-lkp/linux/commits/Thomas-Wei-schuh/of-address-Fix-empty-resource-handling-in-__of_address_resource_bounds/20250120-221141 base: ffd294d346d185b70e28b1a28abe367bbfe53c04 patch link: https://lore.kernel.org/r/20250120-of-address-overflow-v1-2-dd68dbf47bce%40linutronix.de patch subject: [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() config: riscv-randconfig-002-20250121 (https://download.01.org/0day-ci/archive/20250121/202501210330.aqouOniZ-lkp@intel.com/config) compiler: riscv64-linux-gcc (GCC) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250121/202501210330.aqouOniZ-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202501210330.aqouOniZ-lkp@intel.com/ All errors (new ones prefixed by >>, old ones prefixed by <<): >> ERROR: modpost: module of_test uses symbol __of_address_resource_bounds from namespace EXPORTED_FOR_KUNIT_TESTING, but does not import it. -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() 2025-01-20 14:09 ` [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() Thomas Weißschuh 2025-01-20 19:31 ` kernel test robot @ 2025-01-21 1:57 ` kernel test robot 2025-01-26 9:59 ` kernel test robot 2 siblings, 0 replies; 7+ messages in thread From: kernel test robot @ 2025-01-21 1:57 UTC (permalink / raw) To: Thomas Weißschuh, Rob Herring, Saravana Kannan Cc: llvm, oe-kbuild-all, Basharath Hussain Khaja, devicetree, linux-kernel, Thomas Weißschuh Hi Thomas, kernel test robot noticed the following build warnings: [auto build test WARNING on ffd294d346d185b70e28b1a28abe367bbfe53c04] url: https://github.com/intel-lab-lkp/linux/commits/Thomas-Wei-schuh/of-address-Fix-empty-resource-handling-in-__of_address_resource_bounds/20250120-221141 base: ffd294d346d185b70e28b1a28abe367bbfe53c04 patch link: https://lore.kernel.org/r/20250120-of-address-overflow-v1-2-dd68dbf47bce%40linutronix.de patch subject: [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() config: powerpc-randconfig-001-20250121 (https://download.01.org/0day-ci/archive/20250121/202501210916.KXb12BAV-lkp@intel.com/config) compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project c23f2417dc5f6dc371afb07af5627ec2a9d373a0) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250121/202501210916.KXb12BAV-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202501210916.KXb12BAV-lkp@intel.com/ All warnings (new ones prefixed by >>, old ones prefixed by <<): WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/fpga/tests/fpga-mgr-test.o WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/fpga/tests/fpga-bridge-test.o WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/fpga/tests/fpga-region-test.o >> WARNING: modpost: module of_test uses symbol __of_address_resource_bounds from namespace EXPORTED_FOR_KUNIT_TESTING, but does not import it. -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() 2025-01-20 14:09 ` [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() Thomas Weißschuh 2025-01-20 19:31 ` kernel test robot 2025-01-21 1:57 ` kernel test robot @ 2025-01-26 9:59 ` kernel test robot 2 siblings, 0 replies; 7+ messages in thread From: kernel test robot @ 2025-01-26 9:59 UTC (permalink / raw) To: Thomas Weißschuh, Rob Herring, Saravana Kannan Cc: oe-kbuild-all, Basharath Hussain Khaja, devicetree, linux-kernel, Thomas Weißschuh Hi Thomas, kernel test robot noticed the following build warnings: [auto build test WARNING on ffd294d346d185b70e28b1a28abe367bbfe53c04] url: https://github.com/intel-lab-lkp/linux/commits/Thomas-Wei-schuh/of-address-Fix-empty-resource-handling-in-__of_address_resource_bounds/20250120-221141 base: ffd294d346d185b70e28b1a28abe367bbfe53c04 patch link: https://lore.kernel.org/r/20250120-of-address-overflow-v1-2-dd68dbf47bce%40linutronix.de patch subject: [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() config: arc-randconfig-r121-20250126 (https://download.01.org/0day-ci/archive/20250126/202501261727.x0aztept-lkp@intel.com/config) compiler: arceb-elf-gcc (GCC) 13.2.0 reproduce: (https://download.01.org/0day-ci/archive/20250126/202501261727.x0aztept-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202501261727.x0aztept-lkp@intel.com/ sparse warnings: (new ones prefixed by >>) >> drivers/of/of_test.c:130:47: sparse: sparse: cast truncates bits from constant value (100000000 becomes 0) drivers/of/of_test.c:131:45: sparse: sparse: cast truncates bits from constant value (100000000 becomes 0) >> drivers/of/of_test.c:138:45: sparse: sparse: cast truncates bits from constant value (100000ffe becomes ffe) vim +130 drivers/of/of_test.c 72 73 static const struct of_address_resource_bounds_case of_address_resource_bounds_cases[] = { 74 { 75 .start = 0, 76 .size = 0, 77 .ret = 0, 78 .res_start = 0, 79 .res_end = -1, 80 }, 81 { 82 .start = 0, 83 .size = 0x1000, 84 .ret = 0, 85 .res_start = 0, 86 .res_end = 0xfff, 87 }, 88 { 89 .start = 0x1000, 90 .size = 0, 91 .ret = 0, 92 .res_start = 0x1000, 93 .res_end = 0xfff, 94 }, 95 { 96 .start = 0x1000, 97 .size = 0x1000, 98 .ret = 0, 99 .res_start = 0x1000, 100 .res_end = 0x1fff, 101 }, 102 { 103 .start = 1, 104 .size = RESOURCE_SIZE_MAX, 105 .ret = 0, 106 .res_start = 1, 107 .res_end = RESOURCE_SIZE_MAX, 108 }, 109 { 110 .start = RESOURCE_SIZE_MAX, 111 .size = 1, 112 .ret = 0, 113 .res_start = RESOURCE_SIZE_MAX, 114 .res_end = RESOURCE_SIZE_MAX, 115 }, 116 { 117 .start = 2, 118 .size = RESOURCE_SIZE_MAX, 119 .ret = -EOVERFLOW, 120 }, 121 { 122 .start = RESOURCE_SIZE_MAX, 123 .size = 2, 124 .ret = -EOVERFLOW, 125 }, 126 { 127 .start = 0x100000000ULL, 128 .size = 1, 129 .ret = sizeof(resource_size_t) > sizeof(u32) ? 0 : -EOVERFLOW, > 130 .res_start = (resource_size_t)0x100000000, 131 .res_end = (resource_size_t)0x100000000, 132 }, 133 { 134 .start = 0x1000, 135 .size = 0xffffffff, 136 .ret = sizeof(resource_size_t) > sizeof(u32) ? 0 : -EOVERFLOW, 137 .res_start = (resource_size_t)0x1000, > 138 .res_end = (resource_size_t)0x100000ffe, 139 }, 140 }; 141 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-01-26 10:00 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-01-20 14:09 [PATCH 0/2] of: address: Fix empty resource handling in __of_address_resource_bounds() Thomas Weißschuh 2025-01-20 14:09 ` [PATCH 1/2] " Thomas Weißschuh 2025-01-24 22:00 ` Rob Herring (Arm) 2025-01-20 14:09 ` [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() Thomas Weißschuh 2025-01-20 19:31 ` kernel test robot 2025-01-21 1:57 ` kernel test robot 2025-01-26 9:59 ` kernel test robot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).