diff mbox series

[1/1] btrfs: Annotate the reservation space wait event with lockdep

Message ID 20220812181754.1535281-2-iangelak@fb.com (mailing list archive)
State New, archived
Headers show
Series btrfs: Add a lockdep annotation for the reservation space wait event | expand

Commit Message

Ioannis Angelakopoulos Aug. 12, 2022, 6:17 p.m. UTC
Add a lockdep annotation for the reservation space wait event.

This wait event is special in the sense that its condition modification and
waitqueue signaling are protected by the space_info->lock spinlock.

However, that is not the case with flush_space() in fs/btrfs/space-info.c
which is usually part of the execution context that leads to the signaling
of the reservation space waitqueue. This function is not protected by the
space_info->lock thus we place our annotation in flush_space().

Since, flush_space() operates differently depending on the flushing state,
we use a multilevel lockdep map where each subclass/level corresponds to
each flushing state.

Signed-off-by: Ioannis Angelakopoulos <iangelak@fb.com>
---
 fs/btrfs/ctree.h      |  9 +++++++++
 fs/btrfs/disk-io.c    |  1 +
 fs/btrfs/space-info.c | 25 +++++++++++++++++++++++++
 3 files changed, 35 insertions(+)

Comments

kernel test robot Aug. 13, 2022, 6:15 a.m. UTC | #1
Hi Ioannis,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on kdave/for-next]
[also build test ERROR on next-20220812]
[cannot apply to linus/master v5.19]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Ioannis-Angelakopoulos/btrfs-Add-a-lockdep-annotation-for-the-reservation-space-wait-event/20220813-022105
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux.git for-next
config: x86_64-randconfig-a012 (https://download.01.org/0day-ci/archive/20220813/202208131436.QpHxeBKQ-lkp@intel.com/config)
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 3329cec2f79185bafd678f310fafadba2a8c76d2)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/70919e3504790a947de789ed2298275d25770f83
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Ioannis-Angelakopoulos/btrfs-Add-a-lockdep-annotation-for-the-reservation-space-wait-event/20220813-022105
        git checkout 70919e3504790a947de789ed2298275d25770f83
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash fs/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

>> fs/btrfs/space-info.c:701:3: error: call to undeclared function 'btrfs_lockdep_acquire_nested'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
                   btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
                   ^
>> fs/btrfs/space-info.c:701:41: error: use of undeclared identifier 'btrfs_reservation_space'
                   btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
                                                         ^
   fs/btrfs/space-info.c:712:41: error: use of undeclared identifier 'btrfs_reservation_space'
                   btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
                                                         ^
   fs/btrfs/space-info.c:725:41: error: use of undeclared identifier 'btrfs_reservation_space'
                   btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
                                                         ^
   fs/btrfs/space-info.c:754:41: error: use of undeclared identifier 'btrfs_reservation_space'
                   btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
                                                         ^
   fs/btrfs/space-info.c:788:41: error: use of undeclared identifier 'btrfs_reservation_space'
                   btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
                                                         ^
>> fs/btrfs/space-info.c:1462:3: error: call to undeclared function 'btrfs_might_wait_for_event_nested'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
                   btrfs_might_wait_for_event_nested(fs_info, btrfs_reservation_space,
                   ^
   fs/btrfs/space-info.c:1462:46: error: use of undeclared identifier 'btrfs_reservation_space'
                   btrfs_might_wait_for_event_nested(fs_info, btrfs_reservation_space,
                                                              ^
   8 errors generated.


vim +/btrfs_lockdep_acquire_nested +701 fs/btrfs/space-info.c

   673	
   674	/*
   675	 * Try to flush some data based on policy set by @state. This is only advisory
   676	 * and may fail for various reasons. The caller is supposed to examine the
   677	 * state of @space_info to detect the outcome.
   678	 */
   679	static void flush_space(struct btrfs_fs_info *fs_info,
   680			       struct btrfs_space_info *space_info, u64 num_bytes,
   681			       enum btrfs_flush_state state, bool for_preempt)
   682	{
   683		struct btrfs_root *root = fs_info->tree_root;
   684		struct btrfs_trans_handle *trans;
   685		int nr;
   686		int ret = 0;
   687	
   688		switch (state) {
   689		case FLUSH_DELAYED_ITEMS_NR:
   690		case FLUSH_DELAYED_ITEMS:
   691			if (state == FLUSH_DELAYED_ITEMS_NR)
   692				nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
   693			else
   694				nr = -1;
   695	
   696			trans = btrfs_join_transaction(root);
   697			if (IS_ERR(trans)) {
   698				ret = PTR_ERR(trans);
   699				break;
   700			}
 > 701			btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
   702						     BTRFS_LOCKDEP_FLUSH_DELAYED_ITEMS);
   703			ret = btrfs_run_delayed_items_nr(trans, nr);
   704			btrfs_lockdep_release(fs_info, btrfs_reservation_space);
   705			btrfs_end_transaction(trans);
   706			break;
   707		case FLUSH_DELALLOC:
   708		case FLUSH_DELALLOC_WAIT:
   709		case FLUSH_DELALLOC_FULL:
   710			if (state == FLUSH_DELALLOC_FULL)
   711				num_bytes = U64_MAX;
   712			btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
   713						     BTRFS_LOCKDEP_FLUSH_DELALLOC_FULL);
   714			shrink_delalloc(fs_info, space_info, num_bytes,
   715					state != FLUSH_DELALLOC, for_preempt);
   716			btrfs_lockdep_release(fs_info, btrfs_reservation_space);
   717			break;
   718		case FLUSH_DELAYED_REFS_NR:
   719		case FLUSH_DELAYED_REFS:
   720			trans = btrfs_join_transaction(root);
   721			if (IS_ERR(trans)) {
   722				ret = PTR_ERR(trans);
   723				break;
   724			}
 > 725			btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
   726						     BTRFS_LOCKDEP_FLUSH_DELAYED_REFS);
   727			if (state == FLUSH_DELAYED_REFS_NR)
   728				nr = calc_reclaim_items_nr(fs_info, num_bytes);
   729			else
   730				nr = 0;
   731			btrfs_run_delayed_refs(trans, nr);
   732			btrfs_lockdep_release(fs_info, btrfs_reservation_space);
   733			btrfs_end_transaction(trans);
   734			break;
   735		case ALLOC_CHUNK:
   736		case ALLOC_CHUNK_FORCE:
   737			/*
   738			 * For metadata space on zoned filesystem, reaching here means we
   739			 * don't have enough space left in active_total_bytes. Try to
   740			 * activate a block group first, because we may have inactive
   741			 * block group already allocated.
   742			 */
   743			ret = btrfs_zoned_activate_one_bg(fs_info, space_info, false);
   744			if (ret < 0)
   745				break;
   746			else if (ret == 1)
   747				break;
   748	
   749			trans = btrfs_join_transaction(root);
   750			if (IS_ERR(trans)) {
   751				ret = PTR_ERR(trans);
   752				break;
   753			}
   754			btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
   755						     BTRFS_LOCKDEP_ALLOC_CHUNK_FORCE);
   756			ret = btrfs_chunk_alloc(trans,
   757					btrfs_get_alloc_profile(fs_info, space_info->flags),
   758					(state == ALLOC_CHUNK) ? CHUNK_ALLOC_NO_FORCE :
   759						CHUNK_ALLOC_FORCE);
   760			btrfs_lockdep_release(fs_info, btrfs_reservation_space);
   761			btrfs_end_transaction(trans);
   762	
   763			/*
   764			 * For metadata space on zoned filesystem, allocating a new chunk
   765			 * is not enough. We still need to activate the block * group.
   766			 * Active the newly allocated block group by (maybe) finishing
   767			 * a block group.
   768			 */
   769			if (ret == 1) {
   770				ret = btrfs_zoned_activate_one_bg(fs_info, space_info, true);
   771				/*
   772				 * Revert to the original ret regardless we could finish
   773				 * one block group or not.
   774				 */
   775				if (ret >= 0)
   776					ret = 1;
   777			}
   778	
   779			if (ret > 0 || ret == -ENOSPC)
   780				ret = 0;
   781			break;
   782		case RUN_DELAYED_IPUTS:
   783			/*
   784			 * If we have pending delayed iputs then we could free up a
   785			 * bunch of pinned space, so make sure we run the iputs before
   786			 * we do our pinned bytes check below.
   787			 */
   788			btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
   789						     BTRFS_LOCKDEP_RUN_DELAYED_IPUTS);
   790			btrfs_run_delayed_iputs(fs_info);
   791			btrfs_wait_on_delayed_iputs(fs_info);
   792			btrfs_lockdep_release(fs_info, btrfs_reservation_space);
   793			break;
   794		case COMMIT_TRANS:
   795			ASSERT(current->journal_info == NULL);
   796			trans = btrfs_join_transaction(root);
   797			if (IS_ERR(trans)) {
   798				ret = PTR_ERR(trans);
   799				break;
   800			}
   801			ret = btrfs_commit_transaction(trans);
   802			break;
   803		default:
   804			ret = -ENOSPC;
   805			break;
   806		}
   807	
   808		trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, state,
   809					ret, for_preempt);
   810		return;
   811	}
   812
kernel test robot Aug. 13, 2022, 6:46 a.m. UTC | #2
Hi Ioannis,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on kdave/for-next]
[also build test ERROR on next-20220812]
[cannot apply to linus/master v5.19]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Ioannis-Angelakopoulos/btrfs-Add-a-lockdep-annotation-for-the-reservation-space-wait-event/20220813-022105
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux.git for-next
config: x86_64-randconfig-a011 (https://download.01.org/0day-ci/archive/20220813/202208131410.ZZhMqctz-lkp@intel.com/config)
compiler: gcc-11 (Debian 11.3.0-3) 11.3.0
reproduce (this is a W=1 build):
        # https://github.com/intel-lab-lkp/linux/commit/70919e3504790a947de789ed2298275d25770f83
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Ioannis-Angelakopoulos/btrfs-Add-a-lockdep-annotation-for-the-reservation-space-wait-event/20220813-022105
        git checkout 70919e3504790a947de789ed2298275d25770f83
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash fs/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   fs/btrfs/space-info.c: In function 'flush_space':
>> fs/btrfs/space-info.c:701:17: error: implicit declaration of function 'btrfs_lockdep_acquire_nested'; did you mean 'btrfs_lockdep_acquire'? [-Werror=implicit-function-declaration]
     701 |                 btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                 btrfs_lockdep_acquire
>> fs/btrfs/space-info.c:701:55: error: 'btrfs_reservation_space' undeclared (first use in this function); did you mean 'btrfs_reservation_space_states'?
     701 |                 btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
         |                                                       ^~~~~~~~~~~~~~~~~~~~~~~
         |                                                       btrfs_reservation_space_states
   fs/btrfs/space-info.c:701:55: note: each undeclared identifier is reported only once for each function it appears in
   fs/btrfs/space-info.c: In function 'wait_reserve_ticket':
>> fs/btrfs/space-info.c:1462:17: error: implicit declaration of function 'btrfs_might_wait_for_event_nested'; did you mean 'btrfs_might_wait_for_event'? [-Werror=implicit-function-declaration]
    1462 |                 btrfs_might_wait_for_event_nested(fs_info, btrfs_reservation_space,
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                 btrfs_might_wait_for_event
   fs/btrfs/space-info.c:1462:60: error: 'btrfs_reservation_space' undeclared (first use in this function); did you mean 'btrfs_reservation_space_states'?
    1462 |                 btrfs_might_wait_for_event_nested(fs_info, btrfs_reservation_space,
         |                                                            ^~~~~~~~~~~~~~~~~~~~~~~
         |                                                            btrfs_reservation_space_states
   cc1: some warnings being treated as errors


vim +701 fs/btrfs/space-info.c

   673	
   674	/*
   675	 * Try to flush some data based on policy set by @state. This is only advisory
   676	 * and may fail for various reasons. The caller is supposed to examine the
   677	 * state of @space_info to detect the outcome.
   678	 */
   679	static void flush_space(struct btrfs_fs_info *fs_info,
   680			       struct btrfs_space_info *space_info, u64 num_bytes,
   681			       enum btrfs_flush_state state, bool for_preempt)
   682	{
   683		struct btrfs_root *root = fs_info->tree_root;
   684		struct btrfs_trans_handle *trans;
   685		int nr;
   686		int ret = 0;
   687	
   688		switch (state) {
   689		case FLUSH_DELAYED_ITEMS_NR:
   690		case FLUSH_DELAYED_ITEMS:
   691			if (state == FLUSH_DELAYED_ITEMS_NR)
   692				nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
   693			else
   694				nr = -1;
   695	
   696			trans = btrfs_join_transaction(root);
   697			if (IS_ERR(trans)) {
   698				ret = PTR_ERR(trans);
   699				break;
   700			}
 > 701			btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
   702						     BTRFS_LOCKDEP_FLUSH_DELAYED_ITEMS);
   703			ret = btrfs_run_delayed_items_nr(trans, nr);
   704			btrfs_lockdep_release(fs_info, btrfs_reservation_space);
   705			btrfs_end_transaction(trans);
   706			break;
   707		case FLUSH_DELALLOC:
   708		case FLUSH_DELALLOC_WAIT:
   709		case FLUSH_DELALLOC_FULL:
   710			if (state == FLUSH_DELALLOC_FULL)
   711				num_bytes = U64_MAX;
   712			btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
   713						     BTRFS_LOCKDEP_FLUSH_DELALLOC_FULL);
   714			shrink_delalloc(fs_info, space_info, num_bytes,
   715					state != FLUSH_DELALLOC, for_preempt);
   716			btrfs_lockdep_release(fs_info, btrfs_reservation_space);
   717			break;
   718		case FLUSH_DELAYED_REFS_NR:
   719		case FLUSH_DELAYED_REFS:
   720			trans = btrfs_join_transaction(root);
   721			if (IS_ERR(trans)) {
   722				ret = PTR_ERR(trans);
   723				break;
   724			}
   725			btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
   726						     BTRFS_LOCKDEP_FLUSH_DELAYED_REFS);
   727			if (state == FLUSH_DELAYED_REFS_NR)
   728				nr = calc_reclaim_items_nr(fs_info, num_bytes);
   729			else
   730				nr = 0;
   731			btrfs_run_delayed_refs(trans, nr);
   732			btrfs_lockdep_release(fs_info, btrfs_reservation_space);
   733			btrfs_end_transaction(trans);
   734			break;
   735		case ALLOC_CHUNK:
   736		case ALLOC_CHUNK_FORCE:
   737			/*
   738			 * For metadata space on zoned filesystem, reaching here means we
   739			 * don't have enough space left in active_total_bytes. Try to
   740			 * activate a block group first, because we may have inactive
   741			 * block group already allocated.
   742			 */
   743			ret = btrfs_zoned_activate_one_bg(fs_info, space_info, false);
   744			if (ret < 0)
   745				break;
   746			else if (ret == 1)
   747				break;
   748	
   749			trans = btrfs_join_transaction(root);
   750			if (IS_ERR(trans)) {
   751				ret = PTR_ERR(trans);
   752				break;
   753			}
   754			btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
   755						     BTRFS_LOCKDEP_ALLOC_CHUNK_FORCE);
   756			ret = btrfs_chunk_alloc(trans,
   757					btrfs_get_alloc_profile(fs_info, space_info->flags),
   758					(state == ALLOC_CHUNK) ? CHUNK_ALLOC_NO_FORCE :
   759						CHUNK_ALLOC_FORCE);
   760			btrfs_lockdep_release(fs_info, btrfs_reservation_space);
   761			btrfs_end_transaction(trans);
   762	
   763			/*
   764			 * For metadata space on zoned filesystem, allocating a new chunk
   765			 * is not enough. We still need to activate the block * group.
   766			 * Active the newly allocated block group by (maybe) finishing
   767			 * a block group.
   768			 */
   769			if (ret == 1) {
   770				ret = btrfs_zoned_activate_one_bg(fs_info, space_info, true);
   771				/*
   772				 * Revert to the original ret regardless we could finish
   773				 * one block group or not.
   774				 */
   775				if (ret >= 0)
   776					ret = 1;
   777			}
   778	
   779			if (ret > 0 || ret == -ENOSPC)
   780				ret = 0;
   781			break;
   782		case RUN_DELAYED_IPUTS:
   783			/*
   784			 * If we have pending delayed iputs then we could free up a
   785			 * bunch of pinned space, so make sure we run the iputs before
   786			 * we do our pinned bytes check below.
   787			 */
   788			btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
   789						     BTRFS_LOCKDEP_RUN_DELAYED_IPUTS);
   790			btrfs_run_delayed_iputs(fs_info);
   791			btrfs_wait_on_delayed_iputs(fs_info);
   792			btrfs_lockdep_release(fs_info, btrfs_reservation_space);
   793			break;
   794		case COMMIT_TRANS:
   795			ASSERT(current->journal_info == NULL);
   796			trans = btrfs_join_transaction(root);
   797			if (IS_ERR(trans)) {
   798				ret = PTR_ERR(trans);
   799				break;
   800			}
   801			ret = btrfs_commit_transaction(trans);
   802			break;
   803		default:
   804			ret = -ENOSPC;
   805			break;
   806		}
   807	
   808		trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, state,
   809					ret, for_preempt);
   810		return;
   811	}
   812
diff mbox series

Patch

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 44837545eef8..9925b79cebf1 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1104,6 +1104,7 @@  struct btrfs_fs_info {
 	struct lockdep_map btrfs_state_change_map[4];
 	struct lockdep_map btrfs_trans_pending_ordered_map;
 	struct lockdep_map btrfs_ordered_extent_map;
+	struct lockdep_map btrfs_reservation_space_map;
 
 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
 	spinlock_t ref_verify_lock;
@@ -1194,6 +1195,14 @@  enum btrfs_lockdep_trans_states {
 	BTRFS_LOCKDEP_TRANS_COMPLETED,
 };
 
+enum btrfs_reservation_space_states {
+	BTRFS_LOCKDEP_FLUSH_DELAYED_ITEMS,
+	BTRFS_LOCKDEP_FLUSH_DELALLOC_FULL,
+	BTRFS_LOCKDEP_FLUSH_DELAYED_REFS,
+	BTRFS_LOCKDEP_ALLOC_CHUNK_FORCE,
+	BTRFS_LOCKDEP_RUN_DELAYED_IPUTS,
+};
+
 /*
  * Lockdep annotation for wait events.
  *
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 6268dafeeb2d..a107b6954a8b 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -2996,6 +2996,7 @@  void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
 	btrfs_lockdep_init_map(fs_info, btrfs_trans_num_extwriters);
 	btrfs_lockdep_init_map(fs_info, btrfs_trans_pending_ordered);
 	btrfs_lockdep_init_map(fs_info, btrfs_ordered_extent);
+	btrfs_lockdep_init_map(fs_info, btrfs_reservation_space);
 	btrfs_state_lockdep_init_map(fs_info, btrfs_trans_commit_start,
 				     BTRFS_LOCKDEP_TRANS_COMMIT_START);
 	btrfs_state_lockdep_init_map(fs_info, btrfs_trans_unblocked,
diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c
index 477e57ace48d..96631d1a9d82 100644
--- a/fs/btrfs/space-info.c
+++ b/fs/btrfs/space-info.c
@@ -702,7 +702,10 @@  static void flush_space(struct btrfs_fs_info *fs_info,
 			ret = PTR_ERR(trans);
 			break;
 		}
+		btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
+					     BTRFS_LOCKDEP_FLUSH_DELAYED_ITEMS);
 		ret = btrfs_run_delayed_items_nr(trans, nr);
+		btrfs_lockdep_release(fs_info, btrfs_reservation_space);
 		btrfs_end_transaction(trans);
 		break;
 	case FLUSH_DELALLOC:
@@ -710,8 +713,11 @@  static void flush_space(struct btrfs_fs_info *fs_info,
 	case FLUSH_DELALLOC_FULL:
 		if (state == FLUSH_DELALLOC_FULL)
 			num_bytes = U64_MAX;
+		btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
+					     BTRFS_LOCKDEP_FLUSH_DELALLOC_FULL);
 		shrink_delalloc(fs_info, space_info, num_bytes,
 				state != FLUSH_DELALLOC, for_preempt);
+		btrfs_lockdep_release(fs_info, btrfs_reservation_space);
 		break;
 	case FLUSH_DELAYED_REFS_NR:
 	case FLUSH_DELAYED_REFS:
@@ -720,11 +726,14 @@  static void flush_space(struct btrfs_fs_info *fs_info,
 			ret = PTR_ERR(trans);
 			break;
 		}
+		btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
+					     BTRFS_LOCKDEP_FLUSH_DELAYED_REFS);
 		if (state == FLUSH_DELAYED_REFS_NR)
 			nr = calc_reclaim_items_nr(fs_info, num_bytes);
 		else
 			nr = 0;
 		btrfs_run_delayed_refs(trans, nr);
+		btrfs_lockdep_release(fs_info, btrfs_reservation_space);
 		btrfs_end_transaction(trans);
 		break;
 	case ALLOC_CHUNK:
@@ -746,10 +755,13 @@  static void flush_space(struct btrfs_fs_info *fs_info,
 			ret = PTR_ERR(trans);
 			break;
 		}
+		btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
+					     BTRFS_LOCKDEP_ALLOC_CHUNK_FORCE);
 		ret = btrfs_chunk_alloc(trans,
 				btrfs_get_alloc_profile(fs_info, space_info->flags),
 				(state == ALLOC_CHUNK) ? CHUNK_ALLOC_NO_FORCE :
 					CHUNK_ALLOC_FORCE);
+		btrfs_lockdep_release(fs_info, btrfs_reservation_space);
 		btrfs_end_transaction(trans);
 
 		/*
@@ -777,8 +789,11 @@  static void flush_space(struct btrfs_fs_info *fs_info,
 		 * bunch of pinned space, so make sure we run the iputs before
 		 * we do our pinned bytes check below.
 		 */
+		btrfs_lockdep_acquire_nested(fs_info, btrfs_reservation_space,
+					     BTRFS_LOCKDEP_RUN_DELAYED_IPUTS);
 		btrfs_run_delayed_iputs(fs_info);
 		btrfs_wait_on_delayed_iputs(fs_info);
+		btrfs_lockdep_release(fs_info, btrfs_reservation_space);
 		break;
 	case COMMIT_TRANS:
 		ASSERT(current->journal_info == NULL);
@@ -1444,6 +1459,16 @@  static void wait_reserve_ticket(struct btrfs_fs_info *fs_info,
 	DEFINE_WAIT(wait);
 	int ret = 0;
 
+#ifdef CONFIG_LOCKDEP
+	int state = BTRFS_LOCKDEP_FLUSH_DELAYED_ITEMS;
+
+	while (state <= BTRFS_LOCKDEP_RUN_DELAYED_IPUTS) {
+		btrfs_might_wait_for_event_nested(fs_info, btrfs_reservation_space,
+						  state);
+		state++;
+	}
+#endif
+
 	spin_lock(&space_info->lock);
 	while (ticket->bytes > 0 && ticket->error == 0) {
 		ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);