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

firmware: microchip: move buffer allocation into mpfs_auto_update_set_image_address()

This buffer is used exclusively by mpfs_auto_update_set_image_address(),
so move the management of it there, employing the recently added cleanup
infrastructure to avoid littering the function with gotos.

Signed-off-by: Conor Dooley <conor.dooley@microchip.com>

+13 -19
+13 -19
drivers/firmware/microchip/mpfs-auto-update.c
··· 9 9 * 10 10 * Author: Conor Dooley <conor.dooley@microchip.com> 11 11 */ 12 + #include <linux/cleanup.h> 12 13 #include <linux/debugfs.h> 13 14 #include <linux/firmware.h> 14 15 #include <linux/math.h> ··· 234 233 return ret; 235 234 } 236 235 237 - static int mpfs_auto_update_set_image_address(struct mpfs_auto_update_priv *priv, char *buffer, 236 + static int mpfs_auto_update_set_image_address(struct mpfs_auto_update_priv *priv, 238 237 u32 image_address, loff_t directory_address) 239 238 { 240 239 struct erase_info erase; 241 - size_t erase_size = AUTO_UPDATE_DIRECTORY_SIZE; 240 + size_t erase_size = round_up(AUTO_UPDATE_DIRECTORY_SIZE, (u64)priv->flash->erasesize); 242 241 size_t bytes_written = 0, bytes_read = 0; 242 + char *buffer __free(kfree) = kzalloc(erase_size, GFP_KERNEL); 243 243 int ret; 244 244 245 - erase_size = round_up(erase_size, (u64)priv->flash->erasesize); 245 + if (!buffer) 246 + return -ENOMEM; 246 247 247 248 erase.addr = AUTO_UPDATE_DIRECTORY_BASE; 248 249 erase.len = erase_size; ··· 290 287 return ret; 291 288 292 289 if (bytes_written != erase_size) 293 - return ret; 290 + return -EIO; 294 291 295 292 return 0; 296 293 } ··· 300 297 { 301 298 struct mpfs_auto_update_priv *priv = fw_uploader->dd_handle; 302 299 struct erase_info erase; 303 - char *buffer; 304 300 loff_t directory_address = AUTO_UPDATE_UPGRADE_DIRECTORY; 305 301 size_t erase_size = AUTO_UPDATE_DIRECTORY_SIZE; 306 302 size_t bytes_written = 0; ··· 315 313 image_address = AUTO_UPDATE_BITSTREAM_BASE + 316 314 AUTO_UPDATE_UPGRADE_INDEX * priv->size_per_bitstream; 317 315 318 - buffer = devm_kzalloc(priv->dev, erase_size, GFP_KERNEL); 319 - if (!buffer) 320 - return -ENOMEM; 321 - 322 316 /* 323 317 * For bitstream info, the descriptor is written to a fixed offset, 324 318 * so there is no need to set the image address. 325 319 */ 326 320 if (!is_info) { 327 - ret = mpfs_auto_update_set_image_address(priv, buffer, image_address, directory_address); 321 + ret = mpfs_auto_update_set_image_address(priv, image_address, directory_address); 328 322 if (ret) { 329 323 dev_err(priv->dev, "failed to set image address in the SPI directory: %d\n", ret); 330 324 return ret; ··· 343 345 dev_info(priv->dev, "Erasing the flash at address (0x%x)\n", image_address); 344 346 ret = mtd_erase(priv->flash, &erase); 345 347 if (ret) 346 - goto out; 348 + return ret; 347 349 348 350 /* 349 351 * No parsing etc of the bitstream is required. The system controller ··· 353 355 dev_info(priv->dev, "Writing the image to the flash at address (0x%x)\n", image_address); 354 356 ret = mtd_write(priv->flash, (loff_t)image_address, size, &bytes_written, data); 355 357 if (ret) 356 - goto out; 358 + return ret; 357 359 358 - if (bytes_written != size) { 359 - ret = -EIO; 360 - goto out; 361 - } 360 + if (bytes_written != size) 361 + return -EIO; 362 362 363 363 *written = bytes_written; 364 364 dev_info(priv->dev, "Wrote 0x%zx bytes to the flash\n", bytes_written); 365 365 366 - out: 367 - devm_kfree(priv->dev, buffer); 368 - return ret; 366 + return 0; 369 367 } 370 368 371 369 static enum fw_upload_err mpfs_auto_update_write(struct fw_upload *fw_uploader, const u8 *data,