Skip to content

Commit 9793c26

Browse files
Mikulas PatockaMike Snitzer
authored andcommitted
dm crypt: account large pages in cc->n_allocated_pages
The commit 5054e77 ("dm crypt: allocate compound pages if possible") changed dm-crypt to use compound pages to improve performance. Unfortunately, there was an oversight: the allocation of compound pages was not accounted at all. Normal pages are accounted in a percpu counter cc->n_allocated_pages and dm-crypt is limited to allocate at most 2% of memory. Because compound pages were not accounted at all, dm-crypt could allocate memory over the 2% limit. Fix this by adding the accounting of compound pages, so that memory consumption of dm-crypt is properly limited. Signed-off-by: Mikulas Patocka <[email protected]> Fixes: 5054e77 ("dm crypt: allocate compound pages if possible") Cc: [email protected] # v6.5+ Signed-off-by: Mike Snitzer <[email protected]>
1 parent 070bb43 commit 9793c26

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

drivers/md/dm-crypt.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,11 +1692,17 @@ static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned int size)
16921692
order = min(order, remaining_order);
16931693

16941694
while (order > 0) {
1695+
if (unlikely(percpu_counter_read_positive(&cc->n_allocated_pages) +
1696+
(1 << order) > dm_crypt_pages_per_client))
1697+
goto decrease_order;
16951698
pages = alloc_pages(gfp_mask
16961699
| __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN | __GFP_COMP,
16971700
order);
1698-
if (likely(pages != NULL))
1701+
if (likely(pages != NULL)) {
1702+
percpu_counter_add(&cc->n_allocated_pages, 1 << order);
16991703
goto have_pages;
1704+
}
1705+
decrease_order:
17001706
order--;
17011707
}
17021708

@@ -1734,10 +1740,13 @@ static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
17341740

17351741
if (clone->bi_vcnt > 0) { /* bio_for_each_folio_all crashes with an empty bio */
17361742
bio_for_each_folio_all(fi, clone) {
1737-
if (folio_test_large(fi.folio))
1743+
if (folio_test_large(fi.folio)) {
1744+
percpu_counter_sub(&cc->n_allocated_pages,
1745+
1 << folio_order(fi.folio));
17381746
folio_put(fi.folio);
1739-
else
1747+
} else {
17401748
mempool_free(&fi.folio->page, &cc->page_pool);
1749+
}
17411750
}
17421751
}
17431752
}

0 commit comments

Comments
 (0)