Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

pwm: Ensure a struct pwm has the same lifetime as its pwm_chip

It's required to not free the memory underlying a requested PWM
while a consumer still has a reference to it. While currently a pwm_chip
doesn't live long enough in all cases, linking the struct pwm to the
pwm_chip results in the right lifetime as soon as the pwmchip is living
long enough. This happens with the following commits.

Note this is a breaking change for all pwm drivers that don't use
pwmchip_alloc().

Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org> # for struct_size() and __counted_by()
Link: https://lore.kernel.org/r/7e9e958841f049026c0023b309cc9deecf0ab61d.1710670958.git.u.kleine-koenig@pengutronix.de
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

+11 -18
+10 -17
drivers/pwm/core.c
··· 987 987 988 988 static void *pwmchip_priv(struct pwm_chip *chip) 989 989 { 990 - return (void *)chip + ALIGN(sizeof(*chip), PWMCHIP_ALIGN); 990 + return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN); 991 991 } 992 992 993 993 /* This is the counterpart to pwmchip_alloc() */ ··· 1001 1001 { 1002 1002 struct pwm_chip *chip; 1003 1003 size_t alloc_size; 1004 + unsigned int i; 1004 1005 1005 - alloc_size = size_add(ALIGN(sizeof(*chip), PWMCHIP_ALIGN), sizeof_priv); 1006 + alloc_size = size_add(ALIGN(struct_size(chip, pwms, npwm), PWMCHIP_ALIGN), 1007 + sizeof_priv); 1006 1008 1007 1009 chip = kzalloc(alloc_size, GFP_KERNEL); 1008 1010 if (!chip) ··· 1015 1013 chip->uses_pwmchip_alloc = true; 1016 1014 1017 1015 pwmchip_set_drvdata(chip, pwmchip_priv(chip)); 1016 + 1017 + for (i = 0; i < chip->npwm; i++) { 1018 + struct pwm_device *pwm = &chip->pwms[i]; 1019 + pwm->chip = chip; 1020 + pwm->hwpwm = i; 1021 + } 1018 1022 1019 1023 return chip; 1020 1024 } ··· 1093 1085 */ 1094 1086 int __pwmchip_add(struct pwm_chip *chip, struct module *owner) 1095 1087 { 1096 - unsigned int i; 1097 1088 int ret; 1098 1089 1099 1090 if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm) ··· 1112 1105 1113 1106 chip->owner = owner; 1114 1107 1115 - chip->pwms = kcalloc(chip->npwm, sizeof(*chip->pwms), GFP_KERNEL); 1116 - if (!chip->pwms) 1117 - return -ENOMEM; 1118 - 1119 1108 mutex_lock(&pwm_lock); 1120 1109 1121 1110 ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL); 1122 1111 if (ret < 0) { 1123 1112 mutex_unlock(&pwm_lock); 1124 - kfree(chip->pwms); 1125 1113 return ret; 1126 1114 } 1127 1115 1128 1116 chip->id = ret; 1129 - 1130 - for (i = 0; i < chip->npwm; i++) { 1131 - struct pwm_device *pwm = &chip->pwms[i]; 1132 - 1133 - pwm->chip = chip; 1134 - pwm->hwpwm = i; 1135 - } 1136 1117 1137 1118 mutex_unlock(&pwm_lock); 1138 1119 ··· 1151 1156 idr_remove(&pwm_chips, chip->id); 1152 1157 1153 1158 mutex_unlock(&pwm_lock); 1154 - 1155 - kfree(chip->pwms); 1156 1159 } 1157 1160 EXPORT_SYMBOL_GPL(pwmchip_remove); 1158 1161
+1 -1
include/linux/pwm.h
··· 290 290 /* only used internally by the PWM framework */ 291 291 bool uses_pwmchip_alloc; 292 292 void *driver_data; 293 - struct pwm_device *pwms; 293 + struct pwm_device pwms[] __counted_by(npwm); 294 294 }; 295 295 296 296 static inline struct device *pwmchip_parent(const struct pwm_chip *chip)