Merge tag 'for-6.7/dm-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/devic...
[linux-2.6-microblaze.git] / drivers / md / dm-crypt.c
index be32a29..6de107a 100644 (file)
@@ -652,13 +652,7 @@ static int crypt_iv_tcw_whitening(struct crypt_config *cc,
        /* calculate crc32 for every 32bit part and xor it */
        desc->tfm = tcw->crc32_tfm;
        for (i = 0; i < 4; i++) {
-               r = crypto_shash_init(desc);
-               if (r)
-                       goto out;
-               r = crypto_shash_update(desc, &buf[i * 4], 4);
-               if (r)
-                       goto out;
-               r = crypto_shash_final(desc, &buf[i * 4]);
+               r = crypto_shash_digest(desc, &buf[i * 4], 4, &buf[i * 4]);
                if (r)
                        goto out;
        }
@@ -1699,11 +1693,17 @@ retry:
                order = min(order, remaining_order);
 
                while (order > 0) {
+                       if (unlikely(percpu_counter_read_positive(&cc->n_allocated_pages) +
+                                       (1 << order) > dm_crypt_pages_per_client))
+                               goto decrease_order;
                        pages = alloc_pages(gfp_mask
                                | __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN | __GFP_COMP,
                                order);
-                       if (likely(pages != NULL))
+                       if (likely(pages != NULL)) {
+                               percpu_counter_add(&cc->n_allocated_pages, 1 << order);
                                goto have_pages;
+                       }
+decrease_order:
                        order--;
                }
 
@@ -1741,10 +1741,13 @@ static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
 
        if (clone->bi_vcnt > 0) { /* bio_for_each_folio_all crashes with an empty bio */
                bio_for_each_folio_all(fi, clone) {
-                       if (folio_test_large(fi.folio))
+                       if (folio_test_large(fi.folio)) {
+                               percpu_counter_sub(&cc->n_allocated_pages,
+                                               1 << folio_order(fi.folio));
                                folio_put(fi.folio);
-                       else
+                       } else {
                                mempool_free(&fi.folio->page, &cc->page_pool);
+                       }
                }
        }
 }
@@ -2859,10 +2862,9 @@ static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
        if (!start || !end || ++start > end)
                return -EINVAL;
 
-       mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
+       mac_alg = kmemdup_nul(start, end - start, GFP_KERNEL);
        if (!mac_alg)
                return -ENOMEM;
-       strncpy(mac_alg, start, end - start);
 
        mac = crypto_alloc_ahash(mac_alg, 0, CRYPTO_ALG_ALLOCATES_MEMORY);
        kfree(mac_alg);