NFSv4.2: improve page handling for GETXATTR
Subsystems (2)
PATCH 1/1 NFSv4.2: improve page handling for GETXATTR
XDRBUF_SPARSE_PAGES can cause problems for the RDMA transport, and it's easy enough to allocate enough pages for the request up front, so do that.
Also, since we've allocated the pages anyway, use the full page aligned length for the receive buffer. This will allow caching of valid replies that are too large for the caller, but that still fit in the allocated pages.
--- fs/nfs/nfs42proc.c | 39 ++++++++++++++++++++++++++++++--------- fs/nfs/nfs42xdr.c | 22 +++++++++++++++++----- 2 files changed, 47 insertions(+), 14 deletions(-)
fs/nfs/nfs42proc.c +30 -9
@@ -1176,11 +1176,9 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, void *buf, size_t buflen) {struct nfs_server *server =NFS_SERVER (inode); -struct page *pages[NFS4XATTR_MAXPAGES ] = {}; +struct page **pages;struct nfs42_getxattrargs arg = { .fh =NFS_FH (inode), - .xattr_pages = pages, - .xattr_len = buflen, .xattr_name = name, };struct nfs42_getxattrres res;
@@ -1189,12 +1187,31 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, .rpc_argp = &arg, .rpc_resp = &res, }; -int ret, np; +ssize_t ret, np, i; + + arg.xattr_len = buflen ?:XATTR_SIZE_MAX ; + + ret = -ENOMEM ; + np =DIV_ROUND_UP (arg.xattr_len,PAGE_SIZE ); + + pages =kcalloc (np,sizeof (struct page *),GFP_KERNEL ); +if (pages ==NULL ) +return ret; + +for (i =0 ; i < np; i++) { + pages[i] =alloc_page (GFP_KERNEL ); +if (!pages[i]) + goto out_free; + } + + arg.xattr_pages = pages; ret =nfs4_call_sync (server->client, server, &msg, &arg.seq_args, &res.seq_res,0 );if (ret <0 ) -return ret; + goto out_free; + + ret = res.xattr_len;/* * Normally, the caching is done one layer up, but for successful
@@ -1209,16 +1226,20 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name,nfs4_xattr_cache_add (inode, name,NULL , pages, res.xattr_len);if (buflen) { -if (res.xattr_len > buflen) -return -ERANGE ; +if (res.xattr_len > buflen) { + ret = -ERANGE ; + goto out_free; + }_copy_from_pages (buf, pages,0 , res.xattr_len); } - np =DIV_ROUND_UP (res.xattr_len,PAGE_SIZE ); +out_free:while (--np >=0 )__free_page (pages[np]); -return res.xattr_len; +kfree (pages); + +return ret; } static ssize_t _nfs42_proc_listxattrs(struct inode *inode, void *buf,
fs/nfs/nfs42xdr.c +17 -5
@@ -489,6 +489,12 @@ static int decode_getxattr(struct xdr_stream *xdr,return -EIO ; len =be32_to_cpup (p); + +/* +* Only check against the page length here. The actual +* requested length may be smaller, but that is only +* checked against after possibly caching a valid reply. +*/ if (len > req->rq_rcv_buf.page_len)return -ERANGE ;
@@ -1483,11 +1489,17 @@ static void nfs4_xdr_enc_getxattr(struct rpc_rqst *req, struct xdr_stream *xdr,encode_putfh (xdr, args->fh, &hdr);encode_getxattr (xdr, args->xattr_name, &hdr); - plen = args->xattr_len ? args->xattr_len :XATTR_SIZE_MAX ; - -rpc_prepare_reply_pages (req, args->xattr_pages,0 , plen, - hdr.replen); - req->rq_rcv_buf.flags |=XDRBUF_SPARSE_PAGES ; +/* +* The GETXATTR op has no length field in the call, and the +* xattr data is at the end of the reply. +* +* Since the pages have already been allocated, there is no +* downside in using the page-aligned length. It will allow +* receiving and caching xattrs that are too large for the +* caller but still fit in the page-rounded value. +*/ + plen =round_up (args->xattr_len,PAGE_SIZE ); +rpc_prepare_reply_pages (req, args->xattr_pages,0 , plen, hdr.replen);encode_nops (&hdr); }
Signature · 1 line
2.23.3
Quoted 24 lines · Frank van der Linden
XDRBUF_SPARSE_PAGEScan cause problems for the RDMA transport, and it's easy enough to allocate enough pages for the request up front, so do that.Also, since we've allocated the pages anyway, use the full page aligned length for the receive buffer. This will allow caching of valid replies that are too large for the caller, but that still fit in the allocated pages.
Signed-off-by Frank van der Linden <[email protected]>---
fs/nfs/nfs42proc.c| 39 ++++++++++++++++++++++++++++++---------fs/nfs/nfs42xdr.c| 22 +++++++++++++++++-----2 files changed, 47 insertions(+), 14 deletions(-)fs/nfs/nfs42proc.c +0
-0
fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, · line 1179
Quoted 5 lines
fs/nfs/nfs42proc.c +1
-1@@ -1176,11 +1176,9 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, void *buf, size_t buflen) {struct nfs_server *server =NFS_SERVER (inode); -struct page *pages[NFS4XATTR_MAXPAGES ] = {}; +struct page **pages;
Taking this array of pointers off the stack is Goodness(tm).
Thanks for doing this update!
Quoted 109 lines
fs/nfs/nfs42proc.c +29
-8struct nfs42_getxattrargs arg = { .fh =NFS_FH (inode), - .xattr_pages = pages, - .xattr_len = buflen, .xattr_name = name, };struct nfs42_getxattrres res;@@ -1189,12 +1187,31 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, .rpc_argp = &arg, .rpc_resp = &res, }; -int ret, np; +ssize_t ret, np, i; + + arg.xattr_len = buflen ?:XATTR_SIZE_MAX ; + + ret = -ENOMEM ; + np =DIV_ROUND_UP (arg.xattr_len,PAGE_SIZE ); + + pages =kcalloc (np,sizeof (struct page *),GFP_KERNEL ); +if (pages ==NULL ) +return ret; + +for (i =0 ; i < np; i++) { + pages[i] =alloc_page (GFP_KERNEL ); +if (!pages[i]) + goto out_free; + } + + arg.xattr_pages = pages; ret =nfs4_call_sync (server->client, server, &msg, &arg.seq_args, &res.seq_res,0 );if (ret <0 ) -return ret; + goto out_free; + + ret = res.xattr_len;/* * Normally, the caching is done one layer up, but for successful @@ -1209,16 +1226,20 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name,nfs4_xattr_cache_add (inode, name,NULL , pages, res.xattr_len);if (buflen) { -if (res.xattr_len > buflen) -return -ERANGE ; +if (res.xattr_len > buflen) { + ret = -ERANGE ; + goto out_free; + }_copy_from_pages (buf, pages,0 , res.xattr_len); } - np =DIV_ROUND_UP (res.xattr_len,PAGE_SIZE ); +out_free:while (--np >=0 )__free_page (pages[np]); -return res.xattr_len; +kfree (pages); + +return ret; } static ssize_t _nfs42_proc_listxattrs(struct inode *inode, void *buf,fs/nfs/nfs42xdr.c +17
-5@@ -489,6 +489,12 @@ static int decode_getxattr(struct xdr_stream *xdr,return -EIO ; len =be32_to_cpup (p); + +/* +* Only check against the page length here. The actual +* requested length may be smaller, but that is only +* checked against after possibly caching a valid reply. +*/ if (len > req->rq_rcv_buf.page_len)return -ERANGE ;@@ -1483,11 +1489,17 @@ static void nfs4_xdr_enc_getxattr(struct rpc_rqst *req, struct xdr_stream *xdr,encode_putfh (xdr, args->fh, &hdr);encode_getxattr (xdr, args->xattr_name, &hdr); - plen = args->xattr_len ? args->xattr_len :XATTR_SIZE_MAX ; - -rpc_prepare_reply_pages (req, args->xattr_pages,0 , plen, - hdr.replen); - req->rq_rcv_buf.flags |=XDRBUF_SPARSE_PAGES ; +/* +* The GETXATTR op has no length field in the call, and the +* xattr data is at the end of the reply. +* +* Since the pages have already been allocated, there is no +* downside in using the page-aligned length. It will allow +* receiving and caching xattrs that are too large for the +* caller but still fit in the page-rounded value. +*/ + plen =round_up (args->xattr_len,PAGE_SIZE ); +rpc_prepare_reply_pages (req, args->xattr_pages,0 , plen, hdr.replen);encode_nops (&hdr); }Signature · 1 line
2.23.3
--
Chuck Lever
Quoted 74 lines · Frank van der Linden
XDRBUF_SPARSE_PAGEScan cause problems for the RDMA transport, and it's easy enough to allocate enough pages for the request up front, so do that.Also, since we've allocated the pages anyway, use the full page aligned length for the receive buffer. This will allow caching of valid replies that are too large for the caller, but that still fit in the allocated pages.
Signed-off-by Frank van der Linden <[email protected]>--- fs/nfs/nfs42proc.c | 39 ++++++++++++++++++++++++++++++--------- fs/nfs/nfs42xdr.c | 22 +++++++++++++++++----- 2 files changed, 47 insertions(+), 14 deletions(-)fs/nfs/nfs42proc.c +23
-7@@ -1176,11 +1176,9 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, void *buf, size_t buflen) {struct nfs_server *server =NFS_SERVER (inode); -struct page *pages[NFS4XATTR_MAXPAGES ] = {}; +struct page **pages;struct nfs42_getxattrargs arg = { .fh =NFS_FH (inode), - .xattr_pages = pages, - .xattr_len = buflen, .xattr_name = name, };struct nfs42_getxattrres res;@@ -1189,12 +1187,31 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, .rpc_argp = &arg, .rpc_resp = &res, }; -int ret, np; +ssize_t ret, np, i; + + arg.xattr_len = buflen ?:XATTR_SIZE_MAX ; + + ret = -ENOMEM ; + np =DIV_ROUND_UP (arg.xattr_len,PAGE_SIZE ); + + pages =kcalloc (np,sizeof (struct page *),GFP_KERNEL ); +if (pages ==NULL ) +return ret; + +for (i =0 ; i < np; i++) { + pages[i] =alloc_page (GFP_KERNEL ); +if (!pages[i]) + goto out_free; + } + + arg.xattr_pages = pages; ret =nfs4_call_sync (server->client, server, &msg, &arg.seq_args, &res.seq_res,0 );if (ret <0 ) -return ret; + goto out_free; + + ret = res.xattr_len;/* * Normally, the caching is done one layer up, but for successful @@ -1209,16 +1226,20 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name,nfs4_xattr_cache_add (inode, name,NULL , pages, res.xattr_len);if (buflen) { -if (res.xattr_len > buflen) -return -ERANGE ; +if (res.xattr_len > buflen) {
fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, · line 1238
Quoted 10 lines
fs/nfs/nfs42proc.c +4
-1+ ret = -ERANGE ; + goto out_free; + }_copy_from_pages (buf, pages,0 , res.xattr_len); } - np =DIV_ROUND_UP (res.xattr_len,PAGE_SIZE ); +out_free:while (--np >=0 )__free_page (pages[np]);
Looking at Chuck's page for listxattr, I think we need to check if (pages[np) before freeing?
Otherwise, looks fine to me. Reviewed-by.
Quoted 50 lines
fs/nfs/nfs42proc.c +3
-1-return res.xattr_len; +kfree (pages); + +return ret; } static ssize_t _nfs42_proc_listxattrs(struct inode *inode, void *buf,fs/nfs/nfs42xdr.c +17
-6@@ -489,6 +489,12 @@ static int decode_getxattr(struct xdr_stream *xdr,return -EIO ; len =be32_to_cpup (p); + +/* +* Only check against the page length here. The actual +* requested length may be smaller, but that is only +* checked against after possibly caching a valid reply. +*/ if (len > req->rq_rcv_buf.page_len)return -ERANGE ;@@ -1483,11 +1489,17 @@ static void nfs4_xdr_enc_getxattr(struct rpc_rqst *req, struct xdr_stream *xdr,encode_putfh (xdr, args->fh, &hdr);encode_getxattr (xdr, args->xattr_name, &hdr); - plen = args->xattr_len ? args->xattr_len :XATTR_SIZE_MAX ; - -rpc_prepare_reply_pages (req, args->xattr_pages,0 , plen, - hdr.replen); - req->rq_rcv_buf.flags |=XDRBUF_SPARSE_PAGES ; +/* +* The GETXATTR op has no length field in the call, and the +* xattr data is at the end of the reply. +* +* Since the pages have already been allocated, there is no +* downside in using the page-aligned length. It will allow +* receiving and caching xattrs that are too large for the +* caller but still fit in the page-rounded value. +*/ + plen =round_up (args->xattr_len,PAGE_SIZE ); +rpc_prepare_reply_pages (req, args->xattr_pages,0 , plen, hdr.replen);encode_nops (&hdr); } --2.23.3
Quoted 90 lines · Olga Kornievskaia
Quoted 74 lines · Frank van der Linden
XDRBUF_SPARSE_PAGEScan cause problems for the RDMA transport, and it's easy enough to allocate enough pages for the request up front, so do that.Also, since we've allocated the pages anyway, use the full page aligned length for the receive buffer. This will allow caching of valid replies that are too large for the caller, but that still fit in the allocated pages.
Signed-off-by Frank van der Linden <[email protected]>--- fs/nfs/nfs42proc.c | 39 ++++++++++++++++++++++++++++++--------- fs/nfs/nfs42xdr.c | 22 +++++++++++++++++----- 2 files changed, 47 insertions(+), 14 deletions(-)fs/nfs/nfs42proc.c +23
-7@@ -1176,11 +1176,9 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, void *buf, size_t buflen) {struct nfs_server *server =NFS_SERVER (inode); -struct page *pages[NFS4XATTR_MAXPAGES ] = {}; +struct page **pages;struct nfs42_getxattrargs arg = { .fh =NFS_FH (inode), - .xattr_pages = pages, - .xattr_len = buflen, .xattr_name = name, };struct nfs42_getxattrres res;@@ -1189,12 +1187,31 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, .rpc_argp = &arg, .rpc_resp = &res, }; -int ret, np; +ssize_t ret, np, i; + + arg.xattr_len = buflen ?:XATTR_SIZE_MAX ; + + ret = -ENOMEM ; + np =DIV_ROUND_UP (arg.xattr_len,PAGE_SIZE ); + + pages =kcalloc (np,sizeof (struct page *),GFP_KERNEL ); +if (pages ==NULL ) +return ret; + +for (i =0 ; i < np; i++) { + pages[i] =alloc_page (GFP_KERNEL ); +if (!pages[i]) + goto out_free; + } + + arg.xattr_pages = pages; ret =nfs4_call_sync (server->client, server, &msg, &arg.seq_args, &res.seq_res,0 );if (ret <0 ) -return ret; + goto out_free; + + ret = res.xattr_len;/* * Normally, the caching is done one layer up, but for successful @@ -1209,16 +1226,20 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name,nfs4_xattr_cache_add (inode, name,NULL , pages, res.xattr_len);if (buflen) { -if (res.xattr_len > buflen) -return -ERANGE ; +if (res.xattr_len > buflen) {fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, · line 1238
Quoted 10 lines
fs/nfs/nfs42proc.c +4
-1+ ret = -ERANGE ; + goto out_free; + }_copy_from_pages (buf, pages,0 , res.xattr_len); } - np =DIV_ROUND_UP (res.xattr_len,PAGE_SIZE ); +out_free:while (--np >=0 )__free_page (pages[np]);Looking at Chuck's page for listxattr, I think we need to check if (pages[np) before freeing?
Quoted 1 line
Otherwise, looks fine to me. Reviewed-by.
Hm, originally, yes, because of SPARSE_PAGES use. So that was actually a bug in the original code, which is now fixed. In other words, we need a Cc: -stable here (which I assume we want anyway, since this fixes RDMA).
It's not needed anymore, and it's not needed for listxattr anymore either, although there the code was originally correct.
- Frank
Quoted 91 lines · Frank van der Linden
Quoted 90 lines · Olga Kornievskaia
Quoted 74 lines · Frank van der Linden
XDRBUF_SPARSE_PAGEScan cause problems for the RDMA transport, and it's easy enough to allocate enough pages for the request up front, so do that.Also, since we've allocated the pages anyway, use the full page aligned length for the receive buffer. This will allow caching of valid replies that are too large for the caller, but that still fit in the allocated pages.
Signed-off-by Frank van der Linden <[email protected]>--- fs/nfs/nfs42proc.c | 39 ++++++++++++++++++++++++++++++--------- fs/nfs/nfs42xdr.c | 22 +++++++++++++++++----- 2 files changed, 47 insertions(+), 14 deletions(-)fs/nfs/nfs42proc.c +23
-7@@ -1176,11 +1176,9 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, void *buf, size_t buflen) {struct nfs_server *server =NFS_SERVER (inode); -struct page *pages[NFS4XATTR_MAXPAGES ] = {}; +struct page **pages;struct nfs42_getxattrargs arg = { .fh =NFS_FH (inode), - .xattr_pages = pages, - .xattr_len = buflen, .xattr_name = name, };struct nfs42_getxattrres res;@@ -1189,12 +1187,31 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, .rpc_argp = &arg, .rpc_resp = &res, }; -int ret, np; +ssize_t ret, np, i; + + arg.xattr_len = buflen ?:XATTR_SIZE_MAX ; + + ret = -ENOMEM ; + np =DIV_ROUND_UP (arg.xattr_len,PAGE_SIZE ); + + pages =kcalloc (np,sizeof (struct page *),GFP_KERNEL ); +if (pages ==NULL ) +return ret; + +for (i =0 ; i < np; i++) { + pages[i] =alloc_page (GFP_KERNEL ); +if (!pages[i]) + goto out_free; + } + + arg.xattr_pages = pages; ret =nfs4_call_sync (server->client, server, &msg, &arg.seq_args, &res.seq_res,0 );if (ret <0 ) -return ret; + goto out_free; + + ret = res.xattr_len;/* * Normally, the caching is done one layer up, but for successful @@ -1209,16 +1226,20 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name,nfs4_xattr_cache_add (inode, name,NULL , pages, res.xattr_len);if (buflen) { -if (res.xattr_len > buflen) -return -ERANGE ; +if (res.xattr_len > buflen) {fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, · line 1238
Quoted 10 lines
fs/nfs/nfs42proc.c +4
-1+ ret = -ERANGE ; + goto out_free; + }_copy_from_pages (buf, pages,0 , res.xattr_len); } - np =DIV_ROUND_UP (res.xattr_len,PAGE_SIZE ); +out_free:while (--np >=0 )__free_page (pages[np]);Looking at Chuck's page for listxattr, I think we need to check if (pages[np) before freeing?
Quoted 6 lines
Quoted 1 line
Otherwise, looks fine to me. Reviewed-by.
Hm, originally, yes, because of
SPARSE_PAGESuse. So that was actually a bug in the original code, which is now fixed. In other words, we need a Cc: -stable here (which I assume we want anyway, since this fixes RDMA).
Oh, no, actually the original code was fine - it only freed the pages based on the actual returned result - so no issue there.
Let me address Trond's comments, though, I'll send a v2.
Frank
fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct
Quoted 34 lines · Frank van der Linden
XDRBUF_SPARSE_PAGEScan cause problems for the RDMA transport, and it's easy enough to allocate enough pages for the request up front, so do that.Also, since we've allocated the pages anyway, use the full page aligned length for the receive buffer. This will allow caching of valid replies that are too large for the caller, but that still fit in the allocated pages.
Signed-off-by Frank van der Linden <[email protected]>---
fs/nfs/nfs42proc.c | 39 ++++++++++++++++++++++++++++++--------- fs/nfs/nfs42xdr.c | 22 +++++++++++++++++-----2 files changed, 47 insertions(+), 14 deletions(-)fs/nfs/nfs42proc.c +1
-3@@ -1176,11 +1176,9 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, void *buf, size_t buflen) { struct nfs_server *server = NFS_SERVER(inode); - struct page *pages[NFS4XATTR_MAXPAGES] = {}; + struct page **pages; struct nfs42_getxattrargs arg = { .fh = NFS_FH(inode), - .xattr_pages = pages, - .xattr_len = buflen, .xattr_name = name, };struct
nfs42_getxattrresres;fs/nfs/nfs42proc.c +0
-0@@ -1189,12 +1187,31 @@ static ssize_t _nfs42_proc_getxattr(struct
Why can't we set up the page array in nfs42_proc_getxattr()? This means that if we get a retryable error from the server, then we perform multiple allocations of the same buffer.
fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, · line 1195
Quoted 11 lines
fs/nfs/nfs42proc.c +6
-1inode *inode, const char *name, .rpc_argp = &arg, .rpc_resp = &res, }; - int ret, np; + ssize_t ret, np, i; + + arg.xattr_len = buflen ?: XATTR_SIZE_MAX; + + ret = -ENOMEM; + np = DIV_ROUND_UP(arg.xattr_len, PAGE_SIZE);
Please just use nfs_page_array_len(). It should be more efficient than the above.
fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct · line 1198
Quoted 2 lines
fs/nfs/nfs42proc.c +2
-0+ + pages = kcalloc(np, sizeof(struct page *), GFP_KERNEL);
Perhaps do this as kmalloc_array() so we don't have to zero out the array? If the alloc_page() fails below, you can always just truncate the value of 'np' before jumping to 'out_free'.
Also note that we prefer to use 'sizeof(*pages)' rather than sizeof(struct page *).
Quoted 81 lines
fs/nfs/nfs42proc.c +10
-0+ if (pages == NULL) + return ret; + + for (i = 0; i < np; i++) { + pages[i] = alloc_page(GFP_KERNEL); + if (!pages[i]) + goto out_free; + } + + arg.xattr_pages = pages;
ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
&res.seq_res, 0);
if (ret < 0)
- return ret;
+ gotoout_free;
+
+ ret = res.xattr_len;
/*
* Normally, the caching is done one layer up, but for successfulfs/nfs/nfs42proc.c +0
-0@@ -1209,16 +1226,20 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, nfs4_xattr_cache_add(inode, name, NULL, pages, res.xattr_len);
if (buflen) {
- if (res.xattr_len > buflen)
- return-ERANGE;
+ if (res.xattr_len > buflen) {
+ ret =-ERANGE;
+ gotoout_free;
+ }
_copy_from_pages(buf, pages, 0, res.xattr_len); }
- np =DIV_ROUND_UP(res.xattr_len, PAGE_SIZE); +out_free:
while (--np >= 0)
__free_page(pages[np]);
- return res.xattr_len;
+ kfree(pages);
+
+ return ret;
}
staticssize_t_nfs42_proc_listxattrs(struct inode *inode, void *buf,fs/nfs/nfs42xdr.c +0
-0@@ -489,6 +489,12 @@ static int decode_getxattr(struct xdr_stream *xdr, return -EIO;
len =be32_to_cpup(p);
+
+ /*
+ * Only check against the page length here. The actual + * requested length may be smaller, but that is only + * checked against after possibly caching a valid reply. + */
if (len >req->rq_rcv_buf.page_len)
return-ERANGE;fs/nfs/nfs42xdr.c +0
-0@@ -1483,11 +1489,17 @@ static void nfs4_xdr_enc_getxattr(struct rpc_rqst *req, struct xdr_stream *xdr, encode_putfh(xdr, args->fh, &hdr); encode_getxattr(xdr, args->xattr_name, &hdr);
- plen =args->xattr_len?args->xattr_len:XATTR_SIZE_MAX; -
- rpc_prepare_reply_pages(req,args->xattr_pages, 0, plen, - hdr.replen);
- req->rq_rcv_buf.flags |=XDRBUF_SPARSE_PAGES;
+ /*
+ * The GETXATTR op has no length field in the call, and the
Quoted 8 lines
+ * xattr data is at the end of the reply.
+ *
+ * Since the pages have already been allocated, there is no + * downside in using the page-aligned length. It will allow + * receiving and caching xattrs that are too large for the + * caller but still fit in the page-rounded value. + */
+ plen =round_up(args->xattr_len, PAGE_SIZE);
This is the wrong place to be recalculating page buffer sizes. This function should have no idea what was allocated or by whom. Please just set the correct value for args->xattr_len in nfs42_proc_getxattr().
Quoted 5 lines
+ rpc_prepare_reply_pages(req,
args->xattr_pages, 0, plen, hdr.replen);
encode_nops(&hdr);
}
Signature · 3 lines
Trond Myklebust Linux NFS client maintainer, Hammerspace [email protected]
Quoted 32 lines · Trond Myklebust
Quoted 30 lines · Frank van der Linden
XDRBUF_SPARSE_PAGEScan cause problems for the RDMA transport, and it's easy enough to allocate enough pages for the request up front, so do that.Also, since we've allocated the pages anyway, use the full page aligned length for the receive buffer. This will allow caching of valid replies that are too large for the caller, but that still fit in the allocated pages.
Signed-off-by Frank van der Linden <[email protected]>--- fs/nfs/nfs42proc.c | 39 ++++++++++++++++++++++++++++++--------- fs/nfs/nfs42xdr.c | 22 +++++++++++++++++----- 2 files changed, 47 insertions(+), 14 deletions(-)fs/nfs/nfs42proc.c +1
-3@@ -1176,11 +1176,9 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, void *buf, size_t buflen) { struct nfs_server *server = NFS_SERVER(inode); -struct page *pages[NFS4XATTR_MAXPAGES ] = {}; +struct page **pages; struct nfs42_getxattrargs arg = { .fh = NFS_FH(inode), - .xattr_pages = pages, - .xattr_len = buflen,
Quoted 8 lines
fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct
Quoted 4 lines
fs/nfs/nfs42proc.c +0
-0.xattr_name = name, };struct nfs42_getxattrres res;fs/nfs/nfs42proc.c +0
-0@@ -1189,12 +1187,31 @@ static ssize_t _nfs42_proc_getxattr(structWhy can't we set up the page array in
nfs42_proc_getxattr()? This means that if we get a retryable error from the server, then we perform multiple allocations of the same buffer.
I wonder if the same then is needed for the LISTXATTR as we allocated pages in _nfs42_proc_listxattr() and instead should do it in nfs42_proc_listxattr()....
Quoted 131 lines
fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, · line 1195
Quoted 11 lines
fs/nfs/nfs42proc.c +6
-1inode *inode, const char *name, .rpc_argp = &arg, .rpc_resp = &res, }; -int ret, np; +ssize_t ret, np, i; + + arg.xattr_len = buflen ?:XATTR_SIZE_MAX ; + + ret = -ENOMEM ; + np =DIV_ROUND_UP (arg.xattr_len,PAGE_SIZE );Please just use
nfs_page_array_len(). It should be more efficient than the above.fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct · line 1198
Quoted 2 lines
fs/nfs/nfs42proc.c +2
-0+ + pages =kcalloc (np,sizeof (struct page *),GFP_KERNEL );Perhaps do this as
kmalloc_array()so we don't have to zero out the array? If thealloc_page()fails below, you can always just truncate the value of 'np' before jumping to 'out_free'.Also note that we prefer to use '
sizeof(*pages)' rather thansizeof(struct page *).Quoted 79 lines
fs/nfs/nfs42proc.c +13
-1+if (pages ==NULL ) +return ret; + +for (i =0 ; i < np; i++) { + pages[i] =alloc_page (GFP_KERNEL ); +if (!pages[i]) + goto out_free; + } + + arg.xattr_pages = pages; ret =nfs4_call_sync (server->client, server, &msg, &arg.seq_args, &res.seq_res,0 );if (ret <0 ) -return ret; + goto out_free; + + ret = res.xattr_len;/*
* Normally, the caching is done one layer up, but for
successfulfs/nfs/nfs42proc.c +8
-4@@ -1209,16 +1226,20 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name,nfs4_xattr_cache_add (inode ,name ,NULL ,pages , res.xattr_len);if (buflen) { -if (res.xattr_len > buflen) -return -ERANGE ; +if (res.xattr_len > buflen) { + ret = -ERANGE ; + goto out_free; + }_copy_from_pages (buf, pages,0 , res.xattr_len); } - np =DIV_ROUND_UP (res.xattr_len,PAGE_SIZE ); +out_free:while (--np >=0 )__free_page (pages[np]); -return res.xattr_len; +kfree (pages); + +return ret; }static
ssize_t_nfs42_proc_listxattrs(struct inode *inode, void
*buf,fs/nfs/nfs42xdr.c +6
-5@@ -489,6 +489,12 @@ static int decode_getxattr(struct xdr_stream *xdr, return -EIO ; len =be32_to_cpup (p); + +/* +* Only check against the page length here. The actual +* requested length may be smaller, but that is only +* checked against after possibly caching a valid reply. +*/ if (len > req->rq_rcv_buf.page_len)return -ERANGE ;@@ -1483,11 +1489,17 @@ static void nfs4_xdr_enc_getxattr(struct rpc_rqst *req, struct xdr_stream *xdr, encode_putfh(xdr, args->fh, &hdr);encode_getxattr (xdr, args->xattr_name, &hdr); - plen = args->xattr_len ? args->xattr_len :XATTR_SIZE_MAX ; - -rpc_prepare_reply_pages (req, args->xattr_pages,0 , plen, - hdr.replen); - req->rq_rcv_buf.flags |=XDRBUF_SPARSE_PAGES ;fs/nfs/nfs42xdr.c static void nfs4_xdr_enc_getxattr(struct · line 1502
Quoted 10 lines
fs/nfs/nfs42xdr.c +10
-0+/* +* The GETXATTR op has no length field in the call, and the +* xattr data is at the end of the reply. +* +* Since the pages have already been allocated, there is no +* downside in using the page-aligned length. It will allow +* receiving and caching xattrs that are too large for the +* caller but still fit in the page-rounded value. +*/ + plen =round_up (args->xattr_len,PAGE_SIZE );This is the wrong place to be recalculating page buffer sizes. This function should have no idea what was allocated or by whom. Please just set the correct value for
args->xattr_leninnfs42_proc_getxattr().Quoted 5 lines
fs/nfs/nfs42xdr.c +1
-0+rpc_prepare_reply_pages (req, args->xattr_pages,0 , plen, hdr.replen);encode_nops(&hdr);}
--
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]
Quoted 42 lines · Olga Kornievskaia
Quoted 32 lines · Trond Myklebust
Quoted 30 lines · Frank van der Linden
XDRBUF_SPARSE_PAGEScan cause problems for the RDMA transport, and it's easy enough to allocate enough pages for the request up front, so do that.Also, since we've allocated the pages anyway, use the full page aligned length for the receive buffer. This will allow caching of valid replies that are too large for the caller, but that still fit in the allocated pages.
Signed-off-by Frank van der Linden <[email protected]>--- fs/nfs/nfs42proc.c | 39 ++++++++++++++++++++++++++++++--------- fs/nfs/nfs42xdr.c | 22 +++++++++++++++++----- 2 files changed, 47 insertions(+), 14 deletions(-)fs/nfs/nfs42proc.c +1
-3@@ -1176,11 +1176,9 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, void *buf, size_t buflen) { struct nfs_server *server = NFS_SERVER(inode); -struct page *pages[NFS4XATTR_MAXPAGES ] = {}; +struct page **pages; struct nfs42_getxattrargs arg = { .fh = NFS_FH(inode), - .xattr_pages = pages, - .xattr_len = buflen,Quoted 8 lines
fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct
Quoted 4 lines
fs/nfs/nfs42proc.c +0
-0.xattr_name = name, };struct nfs42_getxattrres res;fs/nfs/nfs42proc.c +0
-0@@ -1189,12 +1187,31 @@ static ssize_t _nfs42_proc_getxattr(structWhy can't we set up the page array in
nfs42_proc_getxattr()? This means that if we get a retryable error from the server, then we perform multiple allocations of the same buffer.
Quoted 3 lines
I wonder if the same then is needed for the LISTXATTR as we allocated pages in
_nfs42_proc_listxattr()and instead should do it innfs42_proc_listxattr()....
There are a few more places of which this is true, yep. getactl is the other one.
In any case, fixed for the getxattr case in v2.
- Frank
Quoted 162 lines · Trond Myklebust
fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct
Quoted 34 lines · Frank van der Linden
XDRBUF_SPARSE_PAGEScan cause problems for the RDMA transport, and it's easy enough to allocate enough pages for the request up front, so do that.Also, since we've allocated the pages anyway, use the full page aligned length for the receive buffer. This will allow caching of valid replies that are too large for the caller, but that still fit in the allocated pages.
Signed-off-by Frank van der Linden <[email protected]>--- fs/nfs/nfs42proc.c | 39 ++++++++++++++++++++++++++++++--------- fs/nfs/nfs42xdr.c | 22 +++++++++++++++++----- 2 files changed, 47 insertions(+), 14 deletions(-)fs/nfs/nfs42proc.c +1
-3@@ -1176,11 +1176,9 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, void *buf, size_t buflen) { struct nfs_server *server = NFS_SERVER(inode); -struct page *pages[NFS4XATTR_MAXPAGES ] = {}; +struct page **pages;struct nfs42_getxattrargs arg = { .fh =NFS_FH (inode), - .xattr_pages = pages, - .xattr_len = buflen, .xattr_name = name, };struct nfs42_getxattrres res;fs/nfs/nfs42proc.c +0
-0@@ -1189,12 +1187,31 @@ static ssize_t _nfs42_proc_getxattr(structWhy can't we set up the page array in
nfs42_proc_getxattr()? This means that if we get a retryable error from the server, then we perform multiple allocations of the same buffer.fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, · line 1195
Quoted 11 lines
fs/nfs/nfs42proc.c +6
-1inode *inode, const char *name, .rpc_argp = &arg, .rpc_resp = &res, }; -int ret, np; +ssize_t ret, np, i; + + arg.xattr_len = buflen ?:XATTR_SIZE_MAX ; + + ret = -ENOMEM ; + np =DIV_ROUND_UP (arg.xattr_len,PAGE_SIZE );Please just use
nfs_page_array_len(). It should be more efficient than the above.fs/nfs/nfs42proc.c static ssize_t _nfs42_proc_getxattr(struct · line 1198
Quoted 2 lines
fs/nfs/nfs42proc.c +2
-0+ + pages =kcalloc (np,sizeof (struct page *),GFP_KERNEL );Perhaps do this as
kmalloc_array()so we don't have to zero out the array? If thealloc_page()fails below, you can always just truncate the value of 'np' before jumping to 'out_free'.Also note that we prefer to use '
sizeof(*pages)' rather thansizeof(struct page *).Quoted 79 lines
fs/nfs/nfs42proc.c +13
-1+if (pages ==NULL ) +return ret; + +for (i =0 ; i < np; i++) { + pages[i] =alloc_page (GFP_KERNEL ); +if (!pages[i]) + goto out_free; + } + + arg.xattr_pages = pages; ret =nfs4_call_sync (server->client, server, &msg, &arg.seq_args, &res.seq_res,0 );if (ret <0 ) -return ret; + goto out_free; + + ret = res.xattr_len;/*
* Normally, the caching is done one layer up, but for
successfulfs/nfs/nfs42proc.c +8
-4@@ -1209,16 +1226,20 @@ static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name,nfs4_xattr_cache_add (inode ,name ,NULL ,pages , res.xattr_len);if (buflen) { -if (res.xattr_len > buflen) -return -ERANGE ; +if (res.xattr_len > buflen) { + ret = -ERANGE ; + goto out_free; + }_copy_from_pages (buf, pages,0 , res.xattr_len); } - np =DIV_ROUND_UP (res.xattr_len,PAGE_SIZE ); +out_free:while (--np >=0 )__free_page (pages[np]); -return res.xattr_len; +kfree (pages); + +return ret; }static
ssize_t_nfs42_proc_listxattrs(struct inode *inode, void
*buf,fs/nfs/nfs42xdr.c +6
-5@@ -489,6 +489,12 @@ static int decode_getxattr(struct xdr_stream *xdr, return -EIO ; len =be32_to_cpup (p); + +/* +* Only check against the page length here. The actual +* requested length may be smaller, but that is only +* checked against after possibly caching a valid reply. +*/ if (len > req->rq_rcv_buf.page_len)return -ERANGE ;@@ -1483,11 +1489,17 @@ static void nfs4_xdr_enc_getxattr(struct rpc_rqst *req, struct xdr_stream *xdr, encode_putfh(xdr, args->fh, &hdr);encode_getxattr (xdr, args->xattr_name, &hdr); - plen = args->xattr_len ? args->xattr_len :XATTR_SIZE_MAX ; - -rpc_prepare_reply_pages (req, args->xattr_pages,0 , plen, - hdr.replen); - req->rq_rcv_buf.flags |=XDRBUF_SPARSE_PAGES ;fs/nfs/nfs42xdr.c static void nfs4_xdr_enc_getxattr(struct · line 1502
Quoted 10 lines
fs/nfs/nfs42xdr.c +10
-0+/* +* The GETXATTR op has no length field in the call, and the +* xattr data is at the end of the reply. +* +* Since the pages have already been allocated, there is no +* downside in using the page-aligned length. It will allow +* receiving and caching xattrs that are too large for the +* caller but still fit in the page-rounded value. +*/ + plen =round_up (args->xattr_len,PAGE_SIZE );This is the wrong place to be recalculating page buffer sizes. This function should have no idea what was allocated or by whom. Please just set the correct value for
args->xattr_leninnfs42_proc_getxattr().Quoted 3 lines
fs/nfs/nfs42xdr.c +1
-0+rpc_prepare_reply_pages (req, args->xattr_pages,0 , plen, hdr.replen);
Quoted 7 lines
Quoted 2 lines
encode_nops(&hdr);}
--
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]
Thanks for the comments - v2 sent.
- Frank