verity: short circuit once you hit a verified node

Since we set the VERIFIED bits top-down we can short circuit once
we hit a VERIFIED node.

BUG=9752
TEST=Ran tests in verity.git. Ran platform_DMVerityCorruption on H/W.

Signed-off-by: Mandeep Singh Baines <msb@chromium.org>
kernel.git Review URL: http://codereview.chromium.org/6670008

TBRing because code has already been LGTMed and committed to kernel.git.

Change-Id: Ib8fe833668a62bdc21946e2b274f970b0955cd8c

TBR=wad@chromium.org,taysom@chromium.org,ups@chromium.org

Review URL: http://codereview.chromium.org/6721017
diff --git a/dm-bht.c b/dm-bht.c
index 46b03c0..4c262a5 100644
--- a/dm-bht.c
+++ b/dm-bht.c
@@ -691,12 +691,13 @@
 		int state;
 
 		DMDEBUG("verify_path for b=%u on d=%d", block_index, depth);
+		/* TODO(msb,wad): would be nice to avoid two atomic reads */
 		state = atomic_read(&entry->state);
 		if (state == DM_BHT_ENTRY_VERIFIED) {
-			DMDEBUG("verify_path node %u is verified in parent",
+			DMDEBUG("verify_path node %u is verified to root",
 				block_index);
-			entry = dm_bht_get_entry(bht, --depth, block_index);
-			continue;
+			depth++; /* avoid an extra cmpxchg */
+			break;
 		} else if (state <= DM_BHT_ENTRY_ERROR) {
 			DMCRIT("entry(d=%u,b=%u) is in an error state: %d",
 			       depth, block_index, state);
@@ -726,12 +727,15 @@
 			goto mismatch;
 		}
 
+		entry = parent;
+		depth--;
+	}
+	/* Mark path to leaf as verified. */
+	for (; depth < bht->depth; depth++) {
+		entry = dm_bht_get_entry(bht, depth, block_index);
 		atomic_cmpxchg(&entry->state,
 			       DM_BHT_ENTRY_READY,
 			       DM_BHT_ENTRY_VERIFIED);
-
-		entry = parent;
-		depth--;
 	}
 
 	return 0;