Skip to content

Commit 6a21d3c

Browse files
Shrikanth Hegdemaddy-kerneldev
authored andcommitted
powerpc: rtas: use lock guard for mutex
use guard(mutex) for scope based resource management of mutex. This would make the code simpler and easier to maintain. More details on lock guards can be found at https://lore.kernel.org/all/[email protected]/T/#u Reviewed-by: Srikar Dronamraju <[email protected]> Signed-off-by: Shrikanth Hegde <[email protected]> Tested-by: Venkat Rao Bagalkote <[email protected]> Signed-off-by: Madhavan Srinivasan <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 77ad7ef commit 6a21d3c

File tree

1 file changed

+20
-44
lines changed

1 file changed

+20
-44
lines changed

arch/powerpc/kernel/rtas_flash.c

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,13 @@ static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
312312
{
313313
struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
314314
char *p;
315-
int next_free, rc;
315+
int next_free;
316316
struct flash_block_list *fl;
317317

318-
mutex_lock(&rtas_update_flash_mutex);
318+
guard(mutex)(&rtas_update_flash_mutex);
319319

320320
if (uf->status == FLASH_AUTH || count == 0)
321-
goto out; /* discard data */
321+
return count; /* discard data */
322322

323323
/* In the case that the image is not ready for flashing, the memory
324324
* allocated for the block list will be freed upon the release of the
@@ -327,7 +327,7 @@ static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
327327
if (uf->flist == NULL) {
328328
uf->flist = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
329329
if (!uf->flist)
330-
goto nomem;
330+
return -ENOMEM;
331331
}
332332

333333
fl = uf->flist;
@@ -338,7 +338,7 @@ static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
338338
/* Need to allocate another block_list */
339339
fl->next = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
340340
if (!fl->next)
341-
goto nomem;
341+
return -ENOMEM;
342342
fl = fl->next;
343343
next_free = 0;
344344
}
@@ -347,25 +347,17 @@ static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
347347
count = RTAS_BLK_SIZE;
348348
p = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
349349
if (!p)
350-
goto nomem;
350+
return -ENOMEM;
351351

352352
if(copy_from_user(p, buffer, count)) {
353353
kmem_cache_free(flash_block_cache, p);
354-
rc = -EFAULT;
355-
goto error;
354+
return -EFAULT;
356355
}
357356
fl->blocks[next_free].data = p;
358357
fl->blocks[next_free].length = count;
359358
fl->num_blocks++;
360-
out:
361-
mutex_unlock(&rtas_update_flash_mutex);
362-
return count;
363359

364-
nomem:
365-
rc = -ENOMEM;
366-
error:
367-
mutex_unlock(&rtas_update_flash_mutex);
368-
return rc;
360+
return count;
369361
}
370362

371363
/*
@@ -405,38 +397,30 @@ static ssize_t manage_flash_write(struct file *file, const char __user *buf,
405397
static const char reject_str[] = "0";
406398
static const char commit_str[] = "1";
407399
char stkbuf[10];
408-
int op, rc;
400+
int op;
409401

410-
mutex_lock(&rtas_manage_flash_mutex);
402+
guard(mutex)(&rtas_manage_flash_mutex);
411403

412404
if ((args_buf->status == MANAGE_AUTH) || (count == 0))
413-
goto out;
405+
return count;
414406

415407
op = -1;
416408
if (buf) {
417409
if (count > 9) count = 9;
418-
rc = -EFAULT;
419410
if (copy_from_user (stkbuf, buf, count))
420-
goto error;
411+
return -EFAULT;
421412
if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0)
422413
op = RTAS_REJECT_TMP_IMG;
423414
else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0)
424415
op = RTAS_COMMIT_TMP_IMG;
425416
}
426417

427418
if (op == -1) { /* buf is empty, or contains invalid string */
428-
rc = -EINVAL;
429-
goto error;
419+
return -EINVAL;
430420
}
431421

432422
manage_flash(args_buf, op);
433-
out:
434-
mutex_unlock(&rtas_manage_flash_mutex);
435423
return count;
436-
437-
error:
438-
mutex_unlock(&rtas_manage_flash_mutex);
439-
return rc;
440424
}
441425

442426
/*
@@ -499,16 +483,14 @@ static ssize_t validate_flash_write(struct file *file, const char __user *buf,
499483
{
500484
struct rtas_validate_flash_t *const args_buf =
501485
&rtas_validate_flash_data;
502-
int rc;
503486

504-
mutex_lock(&rtas_validate_flash_mutex);
487+
guard(mutex)(&rtas_validate_flash_mutex);
505488

506489
/* We are only interested in the first 4K of the
507490
* candidate image */
508491
if ((*off >= VALIDATE_BUF_SIZE) ||
509492
(args_buf->status == VALIDATE_AUTH)) {
510493
*off += count;
511-
mutex_unlock(&rtas_validate_flash_mutex);
512494
return count;
513495
}
514496

@@ -519,20 +501,14 @@ static ssize_t validate_flash_write(struct file *file, const char __user *buf,
519501
args_buf->status = VALIDATE_INCOMPLETE;
520502
}
521503

522-
if (!access_ok(buf, count)) {
523-
rc = -EFAULT;
524-
goto done;
525-
}
526-
if (copy_from_user(args_buf->buf + *off, buf, count)) {
527-
rc = -EFAULT;
528-
goto done;
529-
}
504+
if (!access_ok(buf, count))
505+
return -EFAULT;
506+
507+
if (copy_from_user(args_buf->buf + *off, buf, count))
508+
return -EFAULT;
530509

531510
*off += count;
532-
rc = count;
533-
done:
534-
mutex_unlock(&rtas_validate_flash_mutex);
535-
return rc;
511+
return count;
536512
}
537513

538514
static int validate_flash_release(struct inode *inode, struct file *file)

0 commit comments

Comments
 (0)