Skip to content

Commit 466c525

Browse files
author
Darrick J. Wong
committed
xfs: minimize overhead of drain wakeups by using jump labels
To reduce the runtime overhead even further when online fsck isn't running, use a static branch key to decide if we call wake_up on the drain. For compilers that support jump labels, the call to wake_up is replaced by a nop sled when nobody is waiting for intents to drain. From my initial microbenchmarking, every transition of the static key between the on and off states takes about 22000ns to complete; this is paid entirely by the xfs_scrub process. When the static key is off (which it should be when fsck isn't running), the nop sled adds an overhead of approximately 0.36ns to runtime code. The post-atomic lockless waiter check adds about 0.03ns, which is basically free. For the few compilers that don't support jump labels, runtime code pays the cost of calling wake_up on an empty waitqueue, which was observed to be about 30ns. However, most architectures that have sufficient memory and CPU capacity to run XFS also support jump labels, so this is not much of a worry. Signed-off-by: Darrick J. Wong <djwong@kernel.org> Reviewed-by: Dave Chinner <dchinner@redhat.com>
1 parent 3f64c71 commit 466c525

17 files changed

Lines changed: 168 additions & 5 deletions

fs/xfs/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ config XFS_RT
9595

9696
config XFS_DRAIN_INTENTS
9797
bool
98+
select JUMP_LABEL if HAVE_ARCH_JUMP_LABEL
9899

99100
config XFS_ONLINE_SCRUB
100101
bool "XFS online metadata check support"

fs/xfs/scrub/agheader.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
#include "scrub/scrub.h"
1919
#include "scrub/common.h"
2020

21+
int
22+
xchk_setup_agheader(
23+
struct xfs_scrub *sc)
24+
{
25+
if (xchk_need_intent_drain(sc))
26+
xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
27+
return xchk_setup_fs(sc);
28+
}
29+
2130
/* Superblock */
2231

2332
/* Cross-reference with the other btrees. */

fs/xfs/scrub/alloc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ int
2424
xchk_setup_ag_allocbt(
2525
struct xfs_scrub *sc)
2626
{
27+
if (xchk_need_intent_drain(sc))
28+
xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
29+
2730
return xchk_setup_ag_btree(sc, false);
2831
}
2932

fs/xfs/scrub/bmap.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ xchk_setup_inode_bmap(
3131
{
3232
int error;
3333

34+
if (xchk_need_intent_drain(sc))
35+
xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
36+
3437
error = xchk_get_inode(sc);
3538
if (error)
3639
goto out;

fs/xfs/scrub/common.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ xchk_perag_drain_and_lock(
487487
sa->agi_bp = NULL;
488488
}
489489

490+
if (!(sc->flags & XCHK_FSGATES_DRAIN))
491+
return -EDEADLOCK;
490492
error = xfs_perag_intent_drain(sa->pag);
491493
if (error == -ERESTARTSYS)
492494
error = -EINTR;
@@ -1005,3 +1007,25 @@ xchk_start_reaping(
10051007
}
10061008
sc->flags &= ~XCHK_REAPING_DISABLED;
10071009
}
1010+
1011+
/*
1012+
* Enable filesystem hooks (i.e. runtime code patching) before starting a scrub
1013+
* operation. Callers must not hold any locks that intersect with the CPU
1014+
* hotplug lock (e.g. writeback locks) because code patching must halt the CPUs
1015+
* to change kernel code.
1016+
*/
1017+
void
1018+
xchk_fsgates_enable(
1019+
struct xfs_scrub *sc,
1020+
unsigned int scrub_fsgates)
1021+
{
1022+
ASSERT(!(scrub_fsgates & ~XCHK_FSGATES_ALL));
1023+
ASSERT(!(sc->flags & scrub_fsgates));
1024+
1025+
trace_xchk_fsgates_enable(sc, scrub_fsgates);
1026+
1027+
if (scrub_fsgates & XCHK_FSGATES_DRAIN)
1028+
xfs_drain_wait_enable();
1029+
1030+
sc->flags |= scrub_fsgates;
1031+
}

fs/xfs/scrub/common.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ bool xchk_should_check_xref(struct xfs_scrub *sc, int *error,
7272
struct xfs_btree_cur **curpp);
7373

7474
/* Setup functions */
75+
int xchk_setup_agheader(struct xfs_scrub *sc);
7576
int xchk_setup_fs(struct xfs_scrub *sc);
7677
int xchk_setup_ag_allocbt(struct xfs_scrub *sc);
7778
int xchk_setup_ag_iallocbt(struct xfs_scrub *sc);
@@ -151,4 +152,18 @@ int xchk_ilock_inverted(struct xfs_inode *ip, uint lock_mode);
151152
void xchk_stop_reaping(struct xfs_scrub *sc);
152153
void xchk_start_reaping(struct xfs_scrub *sc);
153154

155+
/*
156+
* Setting up a hook to wait for intents to drain is costly -- we have to take
157+
* the CPU hotplug lock and force an i-cache flush on all CPUs once to set it
158+
* up, and again to tear it down. These costs add up quickly, so we only want
159+
* to enable the drain waiter if the drain actually detected a conflict with
160+
* running intent chains.
161+
*/
162+
static inline bool xchk_need_intent_drain(struct xfs_scrub *sc)
163+
{
164+
return sc->flags & XCHK_TRY_HARDER;
165+
}
166+
167+
void xchk_fsgates_enable(struct xfs_scrub *sc, unsigned int scrub_fshooks);
168+
154169
#endif /* __XFS_SCRUB_COMMON_H__ */

fs/xfs/scrub/fscounters.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ xchk_setup_fscounters(
130130
struct xchk_fscounters *fsc;
131131
int error;
132132

133+
/*
134+
* If the AGF doesn't track btreeblks, we have to lock the AGF to count
135+
* btree block usage by walking the actual btrees.
136+
*/
137+
if (!xfs_has_lazysbcount(sc->mp))
138+
xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
139+
133140
sc->buf = kzalloc(sizeof(struct xchk_fscounters), XCHK_GFP_FLAGS);
134141
if (!sc->buf)
135142
return -ENOMEM;

fs/xfs/scrub/ialloc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ int
3232
xchk_setup_ag_iallocbt(
3333
struct xfs_scrub *sc)
3434
{
35+
if (xchk_need_intent_drain(sc))
36+
xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
3537
return xchk_setup_ag_btree(sc, sc->flags & XCHK_TRY_HARDER);
3638
}
3739

fs/xfs/scrub/inode.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ xchk_setup_inode(
3232
{
3333
int error;
3434

35+
if (xchk_need_intent_drain(sc))
36+
xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
37+
3538
/*
3639
* Try to get the inode. If the verifiers fail, we try again
3740
* in raw mode.

fs/xfs/scrub/quota.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ xchk_setup_quota(
5353
if (!xfs_this_quota_on(sc->mp, dqtype))
5454
return -ENOENT;
5555

56+
if (xchk_need_intent_drain(sc))
57+
xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
58+
5659
error = xchk_setup_fs(sc);
5760
if (error)
5861
return error;

0 commit comments

Comments
 (0)