Skip to content

Commit 642c28a

Browse files
yopestorulf
authored andcommitted
mmc: core: Optimize case for exactly one erase-group budget
In the (not so unlikely) case that the mmc controller timeout budget is enough for exactly one erase-group, the simplification of allowing one sector has an enormous performance penalty. We optimize this special case by introducing a flag that prohibits erase-group boundary crossing, so that we can allow trimming more than one sector at a time. Signed-off-by: David Jander <david@protonic.nl> Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
1 parent 2c6625c commit 642c28a

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

drivers/mmc/core/core.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,7 @@ int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
21682168
unsigned int arg)
21692169
{
21702170
unsigned int rem, to = from + nr;
2171+
int err;
21712172

21722173
if (!(card->host->caps & MMC_CAP_ERASE) ||
21732174
!(card->csd.cmdclass & CCC_ERASE))
@@ -2218,6 +2219,23 @@ int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
22182219
/* 'from' and 'to' are inclusive */
22192220
to -= 1;
22202221

2222+
/*
2223+
* Special case where only one erase-group fits in the timeout budget:
2224+
* If the region crosses an erase-group boundary on this particular
2225+
* case, we will be trimming more than one erase-group which, does not
2226+
* fit in the timeout budget of the controller, so we need to split it
2227+
* and call mmc_do_erase() twice if necessary. This special case is
2228+
* identified by the card->eg_boundary flag.
2229+
*/
2230+
if ((arg & MMC_TRIM_ARGS) && (card->eg_boundary) &&
2231+
(from % card->erase_size)) {
2232+
rem = card->erase_size - (from % card->erase_size);
2233+
err = mmc_do_erase(card, from, from + rem - 1, arg);
2234+
from += rem;
2235+
if ((err) || (to <= from))
2236+
return err;
2237+
}
2238+
22212239
return mmc_do_erase(card, from, to, arg);
22222240
}
22232241
EXPORT_SYMBOL(mmc_erase);
@@ -2313,16 +2331,28 @@ static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
23132331
if (!qty)
23142332
return 0;
23152333

2334+
/*
2335+
* When specifying a sector range to trim, chances are we might cross
2336+
* an erase-group boundary even if the amount of sectors is less than
2337+
* one erase-group.
2338+
* If we can only fit one erase-group in the controller timeout budget,
2339+
* we have to care that erase-group boundaries are not crossed by a
2340+
* single trim operation. We flag that special case with "eg_boundary".
2341+
* In all other cases we can just decrement qty and pretend that we
2342+
* always touch (qty + 1) erase-groups as a simple optimization.
2343+
*/
23162344
if (qty == 1)
2317-
return 1;
2345+
card->eg_boundary = 1;
2346+
else
2347+
qty--;
23182348

23192349
/* Convert qty to sectors */
23202350
if (card->erase_shift)
2321-
max_discard = --qty << card->erase_shift;
2351+
max_discard = qty << card->erase_shift;
23222352
else if (mmc_card_sd(card))
2323-
max_discard = qty;
2353+
max_discard = qty + 1;
23242354
else
2325-
max_discard = --qty * card->erase_size;
2355+
max_discard = qty * card->erase_size;
23262356

23272357
return max_discard;
23282358
}

include/linux/mmc/card.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ struct mmc_card {
283283
unsigned int erase_size; /* erase size in sectors */
284284
unsigned int erase_shift; /* if erase unit is power 2 */
285285
unsigned int pref_erase; /* in sectors */
286+
unsigned int eg_boundary; /* don't cross erase-group boundaries */
286287
u8 erased_byte; /* value of erased bytes */
287288

288289
u32 raw_cid[4]; /* raw card CID */

0 commit comments

Comments
 (0)