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

soc: qcom: aoss: Format string in qmp_send()

The majority of callers to qmp_send() composes the message dynamically
using some form of sprintf(), resulting in unnecessary complication and
stack usage.

By changing the interface of qmp_send() to take a format string and
arguments, the duplicated composition of the commands can be moved to a
single location.

Reviewed-by: Konrad Dybcio <konrad.dybcio@linaro.org>
Signed-off-by: Bjorn Andersson <quic_bjorande@quicinc.com>
Link: https://lore.kernel.org/r/20230811205839.727373-4-quic_bjorande@quicinc.com
Signed-off-by: Bjorn Andersson <andersson@kernel.org>

authored by

Bjorn Andersson and committed by
Bjorn Andersson
8873d1e2 59e09100

+15 -9
+13 -7
drivers/soc/qcom/qcom_aoss.c
··· 205 205 /** 206 206 * qmp_send() - send a message to the AOSS 207 207 * @qmp: qmp context 208 - * @data: message to be sent 208 + * @fmt: format string for message to be sent 209 + * @...: arguments for the format string 209 210 * 210 - * Transmit @data to AOSS and wait for the AOSS to acknowledge the message. 211 + * Transmit message to AOSS and wait for the AOSS to acknowledge the message. 211 212 * data must not be longer than the mailbox size. Access is synchronized by 212 213 * this implementation. 213 214 * 214 215 * Return: 0 on success, negative errno on failure 215 216 */ 216 - int qmp_send(struct qmp *qmp, const void *data) 217 + int qmp_send(struct qmp *qmp, const char *fmt, ...) 217 218 { 218 219 char buf[QMP_MSG_LEN]; 219 220 long time_left; 221 + va_list args; 222 + int len; 220 223 int ret; 221 224 222 - if (WARN_ON(IS_ERR_OR_NULL(qmp) || !data)) 225 + if (WARN_ON(IS_ERR_OR_NULL(qmp) || !fmt)) 223 226 return -EINVAL; 224 227 225 - if (WARN_ON(strlen(data) >= sizeof(buf))) 226 - return -EINVAL; 228 + memset(buf, 0, sizeof(buf)); 229 + va_start(args, fmt); 230 + len = vsnprintf(buf, sizeof(buf), fmt, args); 231 + va_end(args); 227 232 228 - strscpy_pad(buf, data, sizeof(buf)); 233 + if (WARN_ON(len >= sizeof(buf))) 234 + return -EINVAL; 229 235 230 236 mutex_lock(&qmp->tx_lock); 231 237
+2 -2
include/linux/soc/qcom/qcom_aoss.h
··· 13 13 14 14 #if IS_ENABLED(CONFIG_QCOM_AOSS_QMP) 15 15 16 - int qmp_send(struct qmp *qmp, const void *data); 16 + int qmp_send(struct qmp *qmp, const char *fmt, ...); 17 17 struct qmp *qmp_get(struct device *dev); 18 18 void qmp_put(struct qmp *qmp); 19 19 20 20 #else 21 21 22 - static inline int qmp_send(struct qmp *qmp, const void *data) 22 + static inline int qmp_send(struct qmp *qmp, const char *fmt, ...) 23 23 { 24 24 return -ENODEV; 25 25 }