2024-04-22 08:52:51

by stsp

[permalink] [raw]
Subject: [PATCH 1/2] fs: reorganize path_openat()

This patch moves the call to alloc_empty_file() below the call to
path_init(). That changes is needed for the next patch, which adds
a cred override for alloc_empty_file(). The needed cred info is only
available after the call to path_init().

No functional changes are intended by that patch.

Signed-off-by: Stas Sergeev <[email protected]>

CC: Eric Biederman <[email protected]>
CC: Alexander Viro <[email protected]>
CC: Christian Brauner <[email protected]>
CC: Jan Kara <[email protected]>
CC: Andy Lutomirski <[email protected]>
CC: [email protected]
CC: [email protected]
---
fs/namei.c | 26 +++++++++++++++++---------
1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/fs/namei.c b/fs/namei.c
index c5b2a25be7d0..2fde2c320ae9 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -3782,22 +3782,30 @@ static struct file *path_openat(struct nameidata *nd,
struct file *file;
int error;

- file = alloc_empty_file(op->open_flag, current_cred());
- if (IS_ERR(file))
- return file;
-
- if (unlikely(file->f_flags & __O_TMPFILE)) {
+ if (unlikely(op->open_flag & __O_TMPFILE)) {
+ file = alloc_empty_file(op->open_flag, current_cred());
+ if (IS_ERR(file))
+ return file;
error = do_tmpfile(nd, flags, op, file);
- } else if (unlikely(file->f_flags & O_PATH)) {
+ } else if (unlikely(op->open_flag & O_PATH)) {
+ file = alloc_empty_file(op->open_flag, current_cred());
+ if (IS_ERR(file))
+ return file;
error = do_o_path(nd, flags, file);
} else {
const char *s = path_init(nd, flags);
- while (!(error = link_path_walk(s, nd)) &&
- (s = open_last_lookups(nd, file, op)) != NULL)
- ;
+ file = alloc_empty_file(op->open_flag, current_cred());
+ error = PTR_ERR_OR_ZERO(file);
+ if (!error) {
+ while (!(error = link_path_walk(s, nd)) &&
+ (s = open_last_lookups(nd, file, op)) != NULL)
+ ;
+ }
if (!error)
error = do_open(nd, file, op);
terminate_walk(nd);
+ if (IS_ERR(file))
+ return file;
}
if (likely(!error)) {
if (likely(file->f_mode & FMODE_OPENED))
--
2.44.0



2024-04-24 09:33:20

by David Laight

[permalink] [raw]
Subject: RE: [PATCH 1/2] fs: reorganize path_openat()

From: Stas Sergeev
> Sent: 22 April 2024 09:45

I seem to have 5 copies of this patch.....

> This patch moves the call to alloc_empty_file() below the call to
> path_init(). That changes is needed for the next patch, which adds
> a cred override for alloc_empty_file(). The needed cred info is only
> available after the call to path_init().
>
> No functional changes are intended by that patch.
..
> ---
> fs/namei.c | 26 +++++++++++++++++---------
> 1 file changed, 17 insertions(+), 9 deletions(-)
>
> diff --git a/fs/namei.c b/fs/namei.c
> index c5b2a25be7d0..2fde2c320ae9 100644
> --- a/fs/namei.c
> +++ b/fs/namei.c
> @@ -3782,22 +3782,30 @@ static struct file *path_openat(struct nameidata *nd,
> struct file *file;
> int error;
>
> - file = alloc_empty_file(op->open_flag, current_cred());
> - if (IS_ERR(file))
> - return file;
> -
> - if (unlikely(file->f_flags & __O_TMPFILE)) {
> + if (unlikely(op->open_flag & __O_TMPFILE)) {
> + file = alloc_empty_file(op->open_flag, current_cred());
> + if (IS_ERR(file))
> + return file;
> error = do_tmpfile(nd, flags, op, file);
> - } else if (unlikely(file->f_flags & O_PATH)) {
> + } else if (unlikely(op->open_flag & O_PATH)) {
> + file = alloc_empty_file(op->open_flag, current_cred());
> + if (IS_ERR(file))
> + return file;
> error = do_o_path(nd, flags, file);

You probably ought to merge the two 'unlikely' tests.
Otherwise there'll be two conditionals in the 'hot path'.
(There probably always were.)
So something like:
if (unlikely(op->open_flag & (__O_TMPFILE | O_PATH))) {
file = alloc_empty_file(op->open_flag, current_cred());
if (IS_ERR(file))
return file;
if (op->open_flag & __O_TMFILE)
error = do_tmpfile(nd, flags, op, file);
else
error = do_o_path(nd, flags, file);
} else {
Copying op->open_flag to a local may also generate better code.

David

> } else {
> const char *s = path_init(nd, flags);
> - while (!(error = link_path_walk(s, nd)) &&
> - (s = open_last_lookups(nd, file, op)) != NULL)
> - ;
> + file = alloc_empty_file(op->open_flag, current_cred());
> + error = PTR_ERR_OR_ZERO(file);
> + if (!error) {
> + while (!(error = link_path_walk(s, nd)) &&
> + (s = open_last_lookups(nd, file, op)) != NULL)
> + ;
> + }
> if (!error)
> error = do_open(nd, file, op);
> terminate_walk(nd);
> + if (IS_ERR(file))
> + return file;
> }
> if (likely(!error)) {
> if (likely(file->f_mode & FMODE_OPENED))
> --
> 2.44.0
>

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


2024-04-24 10:59:01

by stsp

[permalink] [raw]
Subject: Re: [PATCH 1/2] fs: reorganize path_openat()

24.04.2024 12:17, David Laight пишет:
> You probably ought to merge the two 'unlikely' tests.
> Otherwise there'll be two conditionals in the 'hot path'.
> (There probably always were.)
> So something like:
> if (unlikely(op->open_flag & (__O_TMPFILE | O_PATH))) {
> file = alloc_empty_file(op->open_flag, current_cred());
> if (IS_ERR(file))
> return file;
> if (op->open_flag & __O_TMFILE)
> error = do_tmpfile(nd, flags, op, file);
> else
> error = do_o_path(nd, flags, file);
> } else {

Posted v4 with this code verbatim.

> Copying op->open_flag to a local may also generate better code.
Done this as well.

Thank you.

2024-04-24 15:39:58

by David Laight

[permalink] [raw]
Subject: RE: [PATCH 1/2] fs: reorganize path_openat()

From: stsp
> Sent: 24 April 2024 11:59
...
> Posted v4 with this code verbatim.

Nope - you've just posted a different version of the 'v1' patch.
Should be [PATCH v4 1/2]

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

2024-04-24 17:41:24

by stsp

[permalink] [raw]
Subject: Re: [PATCH 1/2] fs: reorganize path_openat()

24.04.2024 18:07, David Laight пишет:
> From: stsp
>> Sent: 24 April 2024 11:59
> ...
>> Posted v4 with this code verbatim.
> Nope - you've just posted a different version of the 'v1' patch.
> Should be [PATCH v4 1/2]
I simply use `git format-patch`
and `git send-email`.
Are all those customizations needs
to be done by hands?