aboutsummaryrefslogtreecommitdiffstats
path: root/fs/xfs
diff options
context:
space:
mode:
authorDarrick J. Wong <darrick.wong@oracle.com>2019-07-05 13:29:56 -0400
committerDarrick J. Wong <darrick.wong@oracle.com>2019-07-05 13:29:56 -0400
commit6d6ccedd76823c28115bd6925342ceb73bab6cd4 (patch)
tree36366452bf5bf8a5dbc9b5b9d2371791444b72c4 /fs/xfs
parent00816759337cd28bc5290a24dcb527fd9d30680e (diff)
xfs: only allocate memory for scrubbing attributes when we need it
In examining a flame graph of time spent running xfs_scrub on various filesystems, I noticed that we spent nearly 7% of the total runtime on allocating a zeroed 65k buffer for every SCRUB_TYPE_XATTR invocation. We do this even if none of the attribute values were anywhere near 64k in size, even if there were no attribute blocks to check space on, and even if it just turns out there are no attributes at all. Therefore, rearrange the xattr buffer setup code to support reallocating with a bigger buffer and redistribute the callers of that function so that we only allocate memory just prior to needing it, and only allocate as much as we need. If we can't get memory with the ILOCK held we'll bail out with EDEADLOCK which will allocate the maximum memory. Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> Reviewed-by: Brian Foster <bfoster@redhat.com>
Diffstat (limited to 'fs/xfs')
-rw-r--r--fs/xfs/scrub/attr.c67
-rw-r--r--fs/xfs/scrub/attr.h6
2 files changed, 63 insertions, 10 deletions
diff --git a/fs/xfs/scrub/attr.c b/fs/xfs/scrub/attr.c
index 7cb412ca473c..266fecbbf98a 100644
--- a/fs/xfs/scrub/attr.c
+++ b/fs/xfs/scrub/attr.c
@@ -20,13 +20,19 @@
20#include "scrub/dabtree.h" 20#include "scrub/dabtree.h"
21#include "scrub/attr.h" 21#include "scrub/attr.h"
22 22
23/* Allocate enough memory to hold an attr value and attr block bitmaps. */ 23/*
24 * Allocate enough memory to hold an attr value and attr block bitmaps,
25 * reallocating the buffer if necessary. Buffer contents are not preserved
26 * across a reallocation.
27 */
24int 28int
25xchk_setup_xattr_buf( 29xchk_setup_xattr_buf(
26 struct xfs_scrub *sc, 30 struct xfs_scrub *sc,
27 size_t value_size) 31 size_t value_size,
32 xfs_km_flags_t flags)
28{ 33{
29 size_t sz; 34 size_t sz;
35 struct xchk_xattr_buf *ab = sc->buf;
30 36
31 /* 37 /*
32 * We need enough space to read an xattr value from the file or enough 38 * We need enough space to read an xattr value from the file or enough
@@ -36,10 +42,23 @@ xchk_setup_xattr_buf(
36 sz = 3 * sizeof(long) * BITS_TO_LONGS(sc->mp->m_attr_geo->blksize); 42 sz = 3 * sizeof(long) * BITS_TO_LONGS(sc->mp->m_attr_geo->blksize);
37 sz = max_t(size_t, sz, value_size); 43 sz = max_t(size_t, sz, value_size);
38 44
39 sc->buf = kmem_zalloc_large(sz, KM_SLEEP); 45 /*
40 if (!sc->buf) 46 * If there's already a buffer, figure out if we need to reallocate it
47 * to accommodate a larger size.
48 */
49 if (ab) {
50 if (sz <= ab->sz)
51 return 0;
52 kmem_free(ab);
53 sc->buf = NULL;
54 }
55
56 ab = kmem_zalloc_large(sizeof(*ab) + sz, flags);
57 if (!ab)
41 return -ENOMEM; 58 return -ENOMEM;
42 59
60 ab->sz = sz;
61 sc->buf = ab;
43 return 0; 62 return 0;
44} 63}
45 64
@@ -51,9 +70,16 @@ xchk_setup_xattr(
51{ 70{
52 int error; 71 int error;
53 72
54 error = xchk_setup_xattr_buf(sc, XATTR_SIZE_MAX); 73 /*
55 if (error) 74 * We failed to get memory while checking attrs, so this time try to
56 return error; 75 * get all the memory we're ever going to need. Allocate the buffer
76 * without the inode lock held, which means we can sleep.
77 */
78 if (sc->flags & XCHK_TRY_HARDER) {
79 error = xchk_setup_xattr_buf(sc, XATTR_SIZE_MAX, KM_SLEEP);
80 if (error)
81 return error;
82 }
57 83
58 return xchk_setup_inode_contents(sc, ip, 0); 84 return xchk_setup_inode_contents(sc, ip, 0);
59} 85}
@@ -104,6 +130,19 @@ xchk_xattr_listent(
104 return; 130 return;
105 } 131 }
106 132
133 /*
134 * Try to allocate enough memory to extrat the attr value. If that
135 * doesn't work, we overload the seen_enough variable to convey
136 * the error message back to the main scrub function.
137 */
138 error = xchk_setup_xattr_buf(sx->sc, valuelen, KM_MAYFAIL);
139 if (error == -ENOMEM)
140 error = -EDEADLOCK;
141 if (error) {
142 context->seen_enough = error;
143 return;
144 }
145
107 args.flags = ATTR_KERNOTIME; 146 args.flags = ATTR_KERNOTIME;
108 if (flags & XFS_ATTR_ROOT) 147 if (flags & XFS_ATTR_ROOT)
109 args.flags |= ATTR_ROOT; 148 args.flags |= ATTR_ROOT;
@@ -117,7 +156,7 @@ xchk_xattr_listent(
117 args.hashval = xfs_da_hashname(args.name, args.namelen); 156 args.hashval = xfs_da_hashname(args.name, args.namelen);
118 args.trans = context->tp; 157 args.trans = context->tp;
119 args.value = xchk_xattr_valuebuf(sx->sc); 158 args.value = xchk_xattr_valuebuf(sx->sc);
120 args.valuelen = XATTR_SIZE_MAX; 159 args.valuelen = valuelen;
121 160
122 error = xfs_attr_get_ilocked(context->dp, &args); 161 error = xfs_attr_get_ilocked(context->dp, &args);
123 if (error == -EEXIST) 162 if (error == -EEXIST)
@@ -270,16 +309,26 @@ xchk_xattr_block(
270 struct xfs_attr_leafblock *leaf = bp->b_addr; 309 struct xfs_attr_leafblock *leaf = bp->b_addr;
271 struct xfs_attr_leaf_entry *ent; 310 struct xfs_attr_leaf_entry *ent;
272 struct xfs_attr_leaf_entry *entries; 311 struct xfs_attr_leaf_entry *entries;
273 unsigned long *usedmap = xchk_xattr_usedmap(ds->sc); 312 unsigned long *usedmap;
274 char *buf_end; 313 char *buf_end;
275 size_t off; 314 size_t off;
276 __u32 last_hashval = 0; 315 __u32 last_hashval = 0;
277 unsigned int usedbytes = 0; 316 unsigned int usedbytes = 0;
278 unsigned int hdrsize; 317 unsigned int hdrsize;
279 int i; 318 int i;
319 int error;
280 320
281 if (*last_checked == blk->blkno) 321 if (*last_checked == blk->blkno)
282 return 0; 322 return 0;
323
324 /* Allocate memory for block usage checking. */
325 error = xchk_setup_xattr_buf(ds->sc, 0, KM_MAYFAIL);
326 if (error == -ENOMEM)
327 return -EDEADLOCK;
328 if (error)
329 return error;
330 usedmap = xchk_xattr_usedmap(ds->sc);
331
283 *last_checked = blk->blkno; 332 *last_checked = blk->blkno;
284 bitmap_zero(usedmap, mp->m_attr_geo->blksize); 333 bitmap_zero(usedmap, mp->m_attr_geo->blksize);
285 334
diff --git a/fs/xfs/scrub/attr.h b/fs/xfs/scrub/attr.h
index 27e879aeaafc..13a1d2e8424d 100644
--- a/fs/xfs/scrub/attr.h
+++ b/fs/xfs/scrub/attr.h
@@ -10,6 +10,9 @@
10 * Temporary storage for online scrub and repair of extended attributes. 10 * Temporary storage for online scrub and repair of extended attributes.
11 */ 11 */
12struct xchk_xattr_buf { 12struct xchk_xattr_buf {
13 /* Size of @buf, in bytes. */
14 size_t sz;
15
13 /* 16 /*
14 * Memory buffer -- either used for extracting attr values while 17 * Memory buffer -- either used for extracting attr values while
15 * walking the attributes; or for computing attr block bitmaps when 18 * walking the attributes; or for computing attr block bitmaps when
@@ -62,6 +65,7 @@ xchk_xattr_dstmap(
62 BITS_TO_LONGS(sc->mp->m_attr_geo->blksize); 65 BITS_TO_LONGS(sc->mp->m_attr_geo->blksize);
63} 66}
64 67
65int xchk_setup_xattr_buf(struct xfs_scrub *sc, size_t value_size); 68int xchk_setup_xattr_buf(struct xfs_scrub *sc, size_t value_size,
69 xfs_km_flags_t flags);
66 70
67#endif /* __XFS_SCRUB_ATTR_H__ */ 71#endif /* __XFS_SCRUB_ATTR_H__ */