diff options
author | Sami Tolvanen <samitolvanen@google.com> | 2015-10-29 15:45:17 +0000 |
---|---|---|
committer | Sami Tolvanen <samitolvanen@google.com> | 2015-10-29 15:54:27 +0000 |
commit | dadd5e33ac00df9a57114487f8441a59fd08bd89 (patch) | |
tree | 29acb8d24f4a66a8ba09c0a1763ce741bca4fde4 /libfec | |
parent | 4ea2a41464237deabff29bd29a9ffb621ac9f7af (diff) | |
download | extras-dadd5e33ac00df9a57114487f8441a59fd08bd89.tar.gz |
libfec: remove verity validation cache
Caching block validation results increases libfec memory usage and
does not provide comparable benefits in normal use cases, which do
not involve re-reading the same blocks.
Change-Id: I71ac47a7059b350b16e32207932004f3f147abcb
Diffstat (limited to 'libfec')
-rw-r--r-- | libfec/fec_private.h | 8 | ||||
-rw-r--r-- | libfec/fec_read.cpp | 8 | ||||
-rw-r--r-- | libfec/fec_verity.cpp | 53 |
3 files changed, 14 insertions, 55 deletions
diff --git a/libfec/fec_private.h b/libfec/fec_private.h index 21758990..bde30bd8 100644 --- a/libfec/fec_private.h +++ b/libfec/fec_private.h @@ -19,7 +19,6 @@ #include <errno.h> #include <fcntl.h> -#include <list> #include <memory> #include <new> #include <pthread.h> @@ -28,7 +27,6 @@ #include <string.h> #include <sys/syscall.h> #include <unistd.h> -#include <unordered_map> #include <vector> #include <utils/Compat.h> @@ -103,8 +101,6 @@ struct fec_handle { int flags; /* additional flags passed to fec_open */ int mode; /* mode for open(2) */ pthread_mutex_t mutex; - std::list<verity_block_info> lru; - std::unordered_map<uint64_t, std::list<verity_block_info>::iterator> cache; uint64_t errors; uint64_t data_size; uint64_t pos; @@ -131,8 +127,8 @@ extern uint64_t verity_get_size(uint64_t file_size, uint32_t *verity_levels, extern int verity_parse_header(fec_handle *f, uint64_t offset); -extern bool verity_check_block(fec_handle *f, uint64_t index, - const uint8_t *expected, const uint8_t *block); +extern bool verity_check_block(fec_handle *f, const uint8_t *expected, + const uint8_t *block); /* helper macros */ #ifndef unlikely diff --git a/libfec/fec_read.cpp b/libfec/fec_read.cpp index 68ea410e..3736e337 100644 --- a/libfec/fec_read.cpp +++ b/libfec/fec_read.cpp @@ -77,7 +77,7 @@ static inline bool is_erasure(fec_handle *f, uint64_t offset, uint64_t n = offset / FEC_BLOCKSIZE; - return !verity_check_block(f, n, &f->verity.hash[n * SHA256_DIGEST_LENGTH], + return !verity_check_block(f, &f->verity.hash[n * SHA256_DIGEST_LENGTH], data); } @@ -327,7 +327,7 @@ static ssize_t verity_read(fec_handle *f, uint8_t *dest, size_t count, return -1; } - if (likely(verity_check_block(f, curr, hash, data))) { + if (likely(verity_check_block(f, hash, data))) { goto valid; } @@ -352,14 +352,14 @@ static ssize_t verity_read(fec_handle *f, uint8_t *dest, size_t count, erasure locations is slower */ if (__ecc_read(f, rs.get(), data, curr_offset, false, ecc_data.get(), errors) == FEC_BLOCKSIZE && - verity_check_block(f, VERITY_NO_CACHE, hash, data)) { + verity_check_block(f, hash, data)) { goto corrected; } /* try to correct with erasures */ if (__ecc_read(f, rs.get(), data, curr_offset, true, ecc_data.get(), errors) == FEC_BLOCKSIZE && - verity_check_block(f, VERITY_NO_CACHE, hash, data)) { + verity_check_block(f, hash, data)) { goto corrected; } diff --git a/libfec/fec_verity.cpp b/libfec/fec_verity.cpp index 7537530b..8de25efc 100644 --- a/libfec/fec_verity.cpp +++ b/libfec/fec_verity.cpp @@ -136,30 +136,12 @@ static inline int verity_hash(fec_handle *f, const uint8_t *block, } /* computes a verity hash for FEC_BLOCKSIZE bytes from buffer `block' and - compres it to the expected value in `expected'; if `index' has a value - different from `VERITY_NO_CACHE', uses `f->cache' to cache the results */ -bool verity_check_block(fec_handle *f, uint64_t index, const uint8_t *expected, + compares it to the expected value in `expected' */ +bool verity_check_block(fec_handle *f, const uint8_t *expected, const uint8_t *block) { check(f); - - if (index != VERITY_NO_CACHE) { - pthread_mutex_lock(&f->mutex); - auto cached = f->cache.find(index); - - if (cached != f->cache.end()) { - verity_block_info vbi = *(cached->second); - - f->lru.erase(cached->second); - f->lru.push_front(vbi); - f->cache[index] = f->lru.begin(); - - pthread_mutex_unlock(&f->mutex); - return vbi.valid; - } - - pthread_mutex_unlock(&f->mutex); - } + check(block); uint8_t hash[SHA256_DIGEST_LENGTH]; @@ -169,26 +151,7 @@ bool verity_check_block(fec_handle *f, uint64_t index, const uint8_t *expected, } check(expected); - bool valid = !memcmp(expected, hash, SHA256_DIGEST_LENGTH); - - if (index != VERITY_NO_CACHE) { - pthread_mutex_lock(&f->mutex); - - verity_block_info vbi; - vbi.index = index; - vbi.valid = valid; - - if (f->lru.size() >= VERITY_CACHE_BLOCKS) { - f->cache.erase(f->lru.rbegin()->index); - f->lru.pop_back(); - } - - f->lru.push_front(vbi); - f->cache[index] = f->lru.begin(); - pthread_mutex_unlock(&f->mutex); - } - - return valid; + return !memcmp(expected, hash, SHA256_DIGEST_LENGTH); } /* reads a verity hash and the corresponding data block using error correction, @@ -244,10 +207,10 @@ static int verify_tree(fec_handle *f, const uint8_t *root) /* validate the root hash */ if (!raw_pread(f, data, FEC_BLOCKSIZE, hash_offset) || - !verity_check_block(f, VERITY_NO_CACHE, root, data)) { + !verity_check_block(f, root, data)) { /* try to correct */ if (!ecc_read_hashes(f, 0, NULL, hash_offset, data) || - !verity_check_block(f, VERITY_NO_CACHE, root, data)) { + !verity_check_block(f, root, data)) { error("root hash invalid"); return -1; } else if (f->mode & O_RDWR && @@ -312,12 +275,12 @@ static int verify_tree(fec_handle *f, const uint8_t *root) return -1; } - if (!verity_check_block(f, VERITY_NO_CACHE, hash, data)) { + if (!verity_check_block(f, hash, data)) { /* try to correct */ if (!ecc_read_hashes(f, hash_offset + j * SHA256_DIGEST_LENGTH, hash, data_offset + j * FEC_BLOCKSIZE, data) || - !verity_check_block(f, VERITY_NO_CACHE, hash, data)) { + !verity_check_block(f, hash, data)) { error("invalid hash tree: hash_offset %" PRIu64 ", " "data_offset %" PRIu64 ", block %u", hash_offset, data_offset, j); |