* [PATCH] ext4: do the quotafile name safe check before allocating new string
@ 2020-10-26 14:43 xiakaixu1987
2020-12-03 14:28 ` Theodore Y. Ts'o
0 siblings, 1 reply; 2+ messages in thread
From: xiakaixu1987 @ 2020-10-26 14:43 UTC (permalink / raw)
To: linux-ext4; +Cc: tytso, adilger.kernel, Kaixu Xia
From: Kaixu Xia <kaixuxia@tencent.com>
Now we do the quotafile name safe check after allocating the new string
by using kmalloc(), and have to release the string with kfree() if check
fails. Maybe we can check them before allocating memory and directly
return error if check fails to avoid the unnecessary kmalloc()/kfree()
operations.
Signed-off-by: Kaixu Xia <kaixuxia@tencent.com>
---
fs/ext4/super.c | 29 +++++++++++++----------------
1 file changed, 13 insertions(+), 16 deletions(-)
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 5308f0d5fb5a..83fdde498414 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1861,7 +1861,6 @@ static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
{
struct ext4_sb_info *sbi = EXT4_SB(sb);
char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
- int ret = -1;
if (sb_any_quota_loaded(sb) && !old_qname) {
ext4_msg(sb, KERN_ERR,
@@ -1874,32 +1873,30 @@ static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
"ignored when QUOTA feature is enabled");
return 1;
}
- qname = match_strdup(args);
- if (!qname) {
- ext4_msg(sb, KERN_ERR,
- "Not enough memory for storing quotafile name");
- return -1;
- }
if (old_qname) {
- if (strcmp(old_qname, qname) == 0)
- ret = 1;
- else
+ if (strlen(old_qname) != args->to - args->from ||
+ strncmp(old_qname, args->from, args->to - args->from)) {
ext4_msg(sb, KERN_ERR,
"%s quota file already specified",
QTYPE2NAME(qtype));
- goto errout;
+ return -1;
+ }
+ return 1;
}
- if (strchr(qname, '/')) {
+ if (strnchr(args->from, args->to - args->from, '/')) {
ext4_msg(sb, KERN_ERR,
"quotafile must be on filesystem root");
- goto errout;
+ return -1;
+ }
+ qname = match_strdup(args);
+ if (!qname) {
+ ext4_msg(sb, KERN_ERR,
+ "Not enough memory for storing quotafile name");
+ return -1;
}
rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
set_opt(sb, QUOTA);
return 1;
-errout:
- kfree(qname);
- return ret;
}
static int clear_qf_name(struct super_block *sb, int qtype)
--
2.20.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] ext4: do the quotafile name safe check before allocating new string
2020-10-26 14:43 [PATCH] ext4: do the quotafile name safe check before allocating new string xiakaixu1987
@ 2020-12-03 14:28 ` Theodore Y. Ts'o
0 siblings, 0 replies; 2+ messages in thread
From: Theodore Y. Ts'o @ 2020-12-03 14:28 UTC (permalink / raw)
To: xiakaixu1987; +Cc: linux-ext4, adilger.kernel, Kaixu Xia
On Mon, Oct 26, 2020 at 10:43:17PM +0800, xiakaixu1987@gmail.com wrote:
> From: Kaixu Xia <kaixuxia@tencent.com>
>
> Now we do the quotafile name safe check after allocating the new string
> by using kmalloc(), and have to release the string with kfree() if check
> fails. Maybe we can check them before allocating memory and directly
> return error if check fails to avoid the unnecessary kmalloc()/kfree()
> operations.
>
> Signed-off-by: Kaixu Xia <kaixuxia@tencent.com>
> ---
> fs/ext4/super.c | 29 +++++++++++++----------------
> 1 file changed, 13 insertions(+), 16 deletions(-)
This patch reduces the line count by 3, which is good, but...
(a) It makes the code more complex, harder to read, and harder to be
sure things are correct:
compare:
> - if (strcmp(old_qname, qname) == 0)
with
> + if (strlen(old_qname) != args->to - args->from ||
> + strncmp(old_qname, args->from, args->to - args->from)) {
(b) This is optimizing the error path, which is uncommon, and saving
the allocation and free is not really worth trading off making the code
slightly harder to read and maintain.
So I don't think taking this patch is worthwhile.
Cheers,
- Ted
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-12-03 14:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-26 14:43 [PATCH] ext4: do the quotafile name safe check before allocating new string xiakaixu1987
2020-12-03 14:28 ` Theodore Y. Ts'o
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).