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

soc: qcom: cmd-db: Remove memcpy()ing from cmd_db_get_header()

The cmd_db_get_header() function is a static local function that doesn't
need to copy anything from one place to another. Instead, it can just
point into the region by returning pointers to what we're looking for.
If we do that, we should mark what we're returning as const so that code
can't modify cmd-db without an obvious cast.

Cc: Mahesh Sivasubramanian <msivasub@codeaurora.org>
Cc: Lina Iyer <ilina@codeaurora.org>
Cc: Bjorn Andersson <bjorn.andersson@linaro.org>
Cc: Evan Green <evgreen@chromium.org>
Signed-off-by: Stephen Boyd <swboyd@chromium.org>
Signed-off-by: Andy Gross <andy.gross@linaro.org>

authored by

Stephen Boyd and committed by
Andy Gross
84fa36eb 808e1033

+26 -34
+26 -34
drivers/soc/qcom/cmd-db.c
··· 101 101 102 102 static struct cmd_db_header *cmd_db_header; 103 103 104 - 105 - static inline void *rsc_to_entry_header(struct rsc_hdr *hdr) 104 + static inline const void *rsc_to_entry_header(const struct rsc_hdr *hdr) 106 105 { 107 106 u16 offset = le16_to_cpu(hdr->header_offset); 108 107 ··· 109 110 } 110 111 111 112 static inline void * 112 - rsc_offset(struct rsc_hdr *hdr, struct entry_header *ent) 113 + rsc_offset(const struct rsc_hdr *hdr, const struct entry_header *ent) 113 114 { 114 115 u16 offset = le16_to_cpu(hdr->data_offset); 115 116 u16 loffset = le16_to_cpu(ent->offset); ··· 133 134 } 134 135 EXPORT_SYMBOL(cmd_db_ready); 135 136 136 - static int cmd_db_get_header(const char *id, struct entry_header *eh, 137 - struct rsc_hdr *rh) 137 + static int cmd_db_get_header(const char *id, const struct entry_header **eh, 138 + const struct rsc_hdr **rh) 138 139 { 139 - struct rsc_hdr *rsc_hdr; 140 - struct entry_header *ent; 140 + const struct rsc_hdr *rsc_hdr; 141 + const struct entry_header *ent; 141 142 int ret, i, j; 142 143 u8 query[8]; 143 144 144 145 ret = cmd_db_ready(); 145 146 if (ret) 146 147 return ret; 147 - 148 - if (!eh || !rh) 149 - return -EINVAL; 150 148 151 149 /* Pad out query string to same length as in DB */ 152 150 strncpy(query, id, sizeof(query)); ··· 155 159 156 160 ent = rsc_to_entry_header(rsc_hdr); 157 161 for (j = 0; j < le16_to_cpu(rsc_hdr->cnt); j++, ent++) { 158 - if (memcmp(ent->id, query, sizeof(ent->id)) == 0) 159 - break; 160 - } 161 - 162 - if (j < le16_to_cpu(rsc_hdr->cnt)) { 163 - memcpy(eh, ent, sizeof(*ent)); 164 - memcpy(rh, rsc_hdr, sizeof(*rh)); 165 - return 0; 162 + if (memcmp(ent->id, query, sizeof(ent->id)) == 0) { 163 + if (eh) 164 + *eh = ent; 165 + if (rh) 166 + *rh = rsc_hdr; 167 + return 0; 168 + } 166 169 } 167 170 } 168 171 ··· 181 186 u32 cmd_db_read_addr(const char *id) 182 187 { 183 188 int ret; 184 - struct entry_header ent; 185 - struct rsc_hdr rsc_hdr; 189 + const struct entry_header *ent; 186 190 187 - ret = cmd_db_get_header(id, &ent, &rsc_hdr); 191 + ret = cmd_db_get_header(id, &ent, NULL); 188 192 189 - return ret < 0 ? 0 : le32_to_cpu(ent.addr); 193 + return ret < 0 ? 0 : le32_to_cpu(ent->addr); 190 194 } 191 195 EXPORT_SYMBOL(cmd_db_read_addr); 192 196 ··· 201 207 int cmd_db_read_aux_data(const char *id, u8 *data, size_t len) 202 208 { 203 209 int ret; 204 - struct entry_header ent; 205 - struct rsc_hdr rsc_hdr; 210 + const struct entry_header *ent; 211 + const struct rsc_hdr *rsc_hdr; 206 212 u16 ent_len; 207 213 208 214 if (!data) ··· 212 218 if (ret) 213 219 return ret; 214 220 215 - ent_len = le16_to_cpu(ent.len); 221 + ent_len = le16_to_cpu(ent->len); 216 222 if (len < ent_len) 217 223 return -EINVAL; 218 224 219 225 len = min_t(u16, ent_len, len); 220 - memcpy(data, rsc_offset(&rsc_hdr, &ent), len); 226 + memcpy(data, rsc_offset(rsc_hdr, ent), len); 221 227 222 228 return len; 223 229 } ··· 233 239 size_t cmd_db_read_aux_data_len(const char *id) 234 240 { 235 241 int ret; 236 - struct entry_header ent; 237 - struct rsc_hdr rsc_hdr; 242 + const struct entry_header *ent; 238 243 239 - ret = cmd_db_get_header(id, &ent, &rsc_hdr); 244 + ret = cmd_db_get_header(id, &ent, NULL); 240 245 241 - return ret < 0 ? 0 : le16_to_cpu(ent.len); 246 + return ret < 0 ? 0 : le16_to_cpu(ent->len); 242 247 } 243 248 EXPORT_SYMBOL(cmd_db_read_aux_data_len); 244 249 ··· 251 258 enum cmd_db_hw_type cmd_db_read_slave_id(const char *id) 252 259 { 253 260 int ret; 254 - struct entry_header ent; 255 - struct rsc_hdr rsc_hdr; 261 + const struct entry_header *ent; 256 262 u32 addr; 257 263 258 - ret = cmd_db_get_header(id, &ent, &rsc_hdr); 264 + ret = cmd_db_get_header(id, &ent, NULL); 259 265 if (ret < 0) 260 266 return CMD_DB_HW_INVALID; 261 267 262 - addr = le32_to_cpu(ent.addr); 268 + addr = le32_to_cpu(ent->addr); 263 269 return (addr >> SLAVE_ID_SHIFT) & SLAVE_ID_MASK; 264 270 } 265 271 EXPORT_SYMBOL(cmd_db_read_slave_id);