Skip to content

Commit 4bc1dca

Browse files
Artem BityutskiyArtem Bityutskiy
authored andcommitted
UBI: fix mean EC calculation
(a + b) / (c + d) != a / c + b / d. The old code errornously assumed this incorrect formuld. Instead, just sum all erase counters in a 64-bit variable and divide to the number of EBs at the end. Thanks to Adrian Hunter for pointing this out. Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
1 parent cbd8a9d commit 4bc1dca

2 files changed

Lines changed: 9 additions & 34 deletions

File tree

‎drivers/mtd/ubi/scan.c‎

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#include <linux/err.h>
4444
#include <linux/crc32.h>
45+
#include <asm/div64.h>
4546
#include "ubi.h"
4647

4748
#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
@@ -91,27 +92,6 @@ static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
9192
return 0;
9293
}
9394

94-
/**
95-
* commit_to_mean_value - commit intermediate results to the final mean erase
96-
* counter value.
97-
* @si: scanning information
98-
*
99-
* This is a helper function which calculates partial mean erase counter mean
100-
* value and adds it to the resulting mean value. As we can work only in
101-
* integer arithmetic and we want to calculate the mean value of erase counter
102-
* accurately, we first sum erase counter values in @si->ec_sum variable and
103-
* count these components in @si->ec_count. If this temporary @si->ec_sum is
104-
* going to overflow, we calculate the partial mean value
105-
* (@si->ec_sum/@si->ec_count) and add it to @si->mean_ec.
106-
*/
107-
static void commit_to_mean_value(struct ubi_scan_info *si)
108-
{
109-
si->ec_sum /= si->ec_count;
110-
if (si->ec_sum % si->ec_count >= si->ec_count / 2)
111-
si->mean_ec += 1;
112-
si->mean_ec += si->ec_sum;
113-
}
114-
11595
/**
11696
* validate_vid_hdr - check that volume identifier header is correct and
11797
* consistent.
@@ -901,15 +881,8 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum
901881

902882
adjust_mean_ec:
903883
if (!ec_corr) {
904-
if (si->ec_sum + ec < ec) {
905-
commit_to_mean_value(si);
906-
si->ec_sum = 0;
907-
si->ec_count = 0;
908-
} else {
909-
si->ec_sum += ec;
910-
si->ec_count += 1;
911-
}
912-
884+
si->ec_sum += ec;
885+
si->ec_count += 1;
913886
if (ec > si->max_ec)
914887
si->max_ec = ec;
915888
if (ec < si->min_ec)
@@ -965,9 +938,11 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
965938

966939
dbg_msg("scanning is finished");
967940

968-
/* Finish mean erase counter calculations */
969-
if (si->ec_count)
970-
commit_to_mean_value(si);
941+
/* Calculate mean erase counter */
942+
if (si->ec_count) {
943+
do_div(si->ec_sum, si->ec_count);
944+
si->mean_ec = si->ec_sum;
945+
}
971946

972947
if (si->is_empty)
973948
ubi_msg("empty MTD device detected");

‎drivers/mtd/ubi/scan.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ struct ubi_scan_info {
124124
int max_ec;
125125
unsigned long long max_sqnum;
126126
int mean_ec;
127-
int ec_sum;
127+
uint64_t ec_sum;
128128
int ec_count;
129129
};
130130

0 commit comments

Comments
 (0)