diff mbox series

[1/2] fs: avoid fdput() after failed fdget() in ksys_sync_file_range()

Message ID 31be6e0896eba59c06eb9d3d137b214f7220cc53.1588894359.git.skhan@linuxfoundation.org (mailing list archive)
State New, archived
Headers show
Series fs: avoid fdput() after failed fdget() | expand

Commit Message

Shuah Khan May 7, 2020, 11:57 p.m. UTC
Fix ksys_sync_file_range() to avoid fdput() after a failed fdget().
fdput() doesn't do fput() on this file since FDPUT_FPUT isn't set
in fd.flags. Fix it anyway since failed fdget() doesn't require
a fdput().

This was introdcued in a commit to add sync_file_range() helper.

Fixes: 22f96b3808c1 ("fs: add sync_file_range() helper")
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
---
 fs/sync.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Al Viro May 8, 2020, 12:05 a.m. UTC | #1
On Thu, May 07, 2020 at 05:57:09PM -0600, Shuah Khan wrote:
> Fix ksys_sync_file_range() to avoid fdput() after a failed fdget().
> fdput() doesn't do fput() on this file since FDPUT_FPUT isn't set
> in fd.flags. Fix it anyway since failed fdget() doesn't require
> a fdput().
> 
> This was introdcued in a commit to add sync_file_range() helper.

Er...  What's the point microoptimizing the slow path here?
Al Viro May 8, 2020, 12:24 a.m. UTC | #2
On Fri, May 08, 2020 at 01:05:09AM +0100, Al Viro wrote:
> On Thu, May 07, 2020 at 05:57:09PM -0600, Shuah Khan wrote:
> > Fix ksys_sync_file_range() to avoid fdput() after a failed fdget().
> > fdput() doesn't do fput() on this file since FDPUT_FPUT isn't set
> > in fd.flags. Fix it anyway since failed fdget() doesn't require
> > a fdput().
> > 
> > This was introdcued in a commit to add sync_file_range() helper.
> 
> Er...  What's the point microoptimizing the slow path here?

PS: I'm not saying the patch is incorrect, but Fixes: is IMO over the
top.  And looking at that thing,
{
        struct fd f = fdget(fd);
        int ret;

	if (unlikely(!f.file))
		return -EBADF;

	ret = sync_file_range(f.file, offset, nbytes, flags);
        fdput(f);
        return ret;
}

might be cleaner, but that's a matter of taste...
Luis Chamberlain May 8, 2020, 2:21 a.m. UTC | #3
On Fri, May 08, 2020 at 01:24:22AM +0100, Al Viro wrote:
> On Fri, May 08, 2020 at 01:05:09AM +0100, Al Viro wrote:
> > On Thu, May 07, 2020 at 05:57:09PM -0600, Shuah Khan wrote:
> > > Fix ksys_sync_file_range() to avoid fdput() after a failed fdget().
> > > fdput() doesn't do fput() on this file since FDPUT_FPUT isn't set
> > > in fd.flags. Fix it anyway since failed fdget() doesn't require
> > > a fdput().
> > > 
> > > This was introdcued in a commit to add sync_file_range() helper.
> > 
> > Er...  What's the point microoptimizing the slow path here?
> 
> PS: I'm not saying the patch is incorrect, but Fixes: is IMO over the
> top.  And looking at that thing,
> {
>         struct fd f = fdget(fd);
>         int ret;
> 
> 	if (unlikely(!f.file))
> 		return -EBADF;
> 
> 	ret = sync_file_range(f.file, offset, nbytes, flags);
>         fdput(f);
>         return ret;
> }
> 
> might be cleaner, but that's a matter of taste...

This makes it easier to read.

  Luis
Shuah Khan May 8, 2020, 3:07 p.m. UTC | #4
On 5/7/20 8:21 PM, Luis Chamberlain wrote:
> On Fri, May 08, 2020 at 01:24:22AM +0100, Al Viro wrote:
>> On Fri, May 08, 2020 at 01:05:09AM +0100, Al Viro wrote:
>>> On Thu, May 07, 2020 at 05:57:09PM -0600, Shuah Khan wrote:
>>>> Fix ksys_sync_file_range() to avoid fdput() after a failed fdget().
>>>> fdput() doesn't do fput() on this file since FDPUT_FPUT isn't set
>>>> in fd.flags. Fix it anyway since failed fdget() doesn't require
>>>> a fdput().
>>>>
>>>> This was introdcued in a commit to add sync_file_range() helper.
>>>
>>> Er...  What's the point microoptimizing the slow path here?
>>
>> PS: I'm not saying the patch is incorrect, but Fixes: is IMO over the
>> top.  And looking at that thing,
>> {
>>          struct fd f = fdget(fd);
>>          int ret;
>>
>> 	if (unlikely(!f.file))
>> 		return -EBADF;
>>
>> 	ret = sync_file_range(f.file, offset, nbytes, flags);
>>          fdput(f);
>>          return ret;
>> }
>>
>> might be cleaner, but that's a matter of taste...
> 
> This makes it easier to read.
> 

Yes it does. I will make the changes and send v2.

thanks,
-- Shuah
diff mbox series

Patch

diff --git a/fs/sync.c b/fs/sync.c
index 4d1ff010bc5a..faaff835ef12 100644
--- a/fs/sync.c
+++ b/fs/sync.c
@@ -369,10 +369,11 @@  int ksys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
 
 	ret = -EBADF;
 	f = fdget(fd);
-	if (f.file)
+	if (f.file) {
 		ret = sync_file_range(f.file, offset, nbytes, flags);
+		fdput(f);
+	}
 
-	fdput(f);
 	return ret;
 }