Skip to contentSkip to conversation

async_pq: Remove VLA usage

PATCH Superseded Kyle Spiers 3 participants 3 messages May 3, 2018 – May 4, 2018

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 async_pq: Remove VLA usage

In the quest to remove VLAs from the kernel[1], this moves the allocation of coefs and blocks from the stack to being kmalloc()ed.

[1] https://lkml.org/lkml/2018/3/7/621

---
 crypto/async_tx/async_pq.c  | 18 ++++++++++++++----
 crypto/async_tx/raid6test.c |  8 +++++++-
 2 files changed, 21 insertions(+), 5 deletions(-)
crypto/async_tx/async_pq.c +14 -4
index 56bd612927ab..af1912313a23 100644
@@ -194,9 +194,9 @@ async_gen_syndrome(struct page **blocks, unsigned int offset, int disks,
 	    (src_cnt <= dma_maxpq(device, 0) ||
 	     dma_maxpq(device, DMA_PREP_CONTINUE) > 0) &&
 	    is_dma_pq_aligned(device, offset, 0, len)) {
-		struct dma_async_tx_descriptor *tx;
+		struct dma_async_tx_descriptor *tx = NULL;
 		enum dma_ctrl_flags dma_flags = 0;
-		unsigned char coefs[src_cnt];
+		unsigned char *coefs;
 		int i, j;
 
 		/* run the p+q asynchronously */
@@ -207,6 +207,9 @@ async_gen_syndrome(struct page **blocks, unsigned int offset, int disks,
 		 * sources and update the coefficients accordingly
 		 */
 		unmap->len = len;
+		coefs = kmalloc_array(src_cnt, sizeof(*coefs), GFP_KERNEL);
+		if (!coefs)
+			goto out;
 		for (i = 0, j = 0; i < src_cnt; i++) {
 			if (blocks[i] == NULL)
 				continue;
@@ -240,7 +243,9 @@ async_gen_syndrome(struct page **blocks, unsigned int offset, int disks,
 		}
 
 		tx = do_async_gen_syndrome(chan, coefs, j, unmap, dma_flags, submit);
+out:
 		dmaengine_unmap_put(unmap);
+		kfree(coefs);
 		return tx;
 	}
 
@@ -298,8 +303,8 @@ async_syndrome_val(struct page **blocks, unsigned int offset, int disks,
 {
 	struct dma_chan *chan = pq_val_chan(submit, blocks, disks, len);
 	struct dma_device *device = chan ? chan->device : NULL;
-	struct dma_async_tx_descriptor *tx;
-	unsigned char coefs[disks-2];
+	struct dma_async_tx_descriptor *tx = NULL;
+	unsigned char *coefs = NULL;
 	enum dma_ctrl_flags dma_flags = submit->cb_fn ? DMA_PREP_INTERRUPT : 0;
 	struct dmaengine_unmap_data *unmap = NULL;
 
@@ -318,6 +323,9 @@ async_syndrome_val(struct page **blocks, unsigned int offset, int disks,
 			 __func__, disks, len);
 
 		unmap->len = len;
+		coefs = kmalloc_array(disks - 2, sizeof(*coefs), GFP_KERNEL);
+		if (!coefs)
+			goto out;
 		for (i = 0; i < disks-2; i++)
 			if (likely(blocks[i])) {
 				unmap->addr[j] = dma_map_page(dev, blocks[i],
@@ -423,6 +431,8 @@ async_syndrome_val(struct page **blocks, unsigned int offset, int disks,
 		async_tx_sync_epilog(submit);
 		tx = NULL;
 	}
+out:
+	kfree(coefs);
 	dmaengine_unmap_put(unmap);
 
 	return tx;
crypto/async_tx/raid6test.c +7 -1
index dad95f45b88f..ea036b531ef2 100644
@@ -81,11 +81,16 @@ static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, stru
 			init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
 			tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
 		} else {
-			struct page *blocks[disks];
+			struct page **blocks;
 			struct page *dest;
 			int count = 0;
 			int i;
 
+			blocks = kmalloc_array(disks, sizeof(*blocks),
+							GFP_KERNEL);
+			if (!blocks)
+				return;
+
 			/* data+Q failure.  Reconstruct data from P,
 			 * then rebuild syndrome
 			 */
@@ -101,6 +106,7 @@ static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, stru
 
 			init_async_submit(&submit, 0, tx, NULL, NULL, addr_conv);
 			tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
+			kfree(blocks);
 		}
 	} else {
 		if (failb == disks-2) {
Signature · 1 line
2.17.0.441.gb46fe60e1d-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Kyle Spiers

In the quest to remove VLAs from the kernel[1], this moves the allocation of coefs and blocks from the stack to being kmalloc()ed.

[1] https://lkml.org/lkml/2018/3/7/621

Thanks for working on this one!

-Kees

Quoted 104 lines
---
 crypto/async_tx/async_pq.c  | 18 ++++++++++++++----
 crypto/async_tx/raid6test.c |  8 +++++++-
 2 files changed, 21 insertions(+), 5 deletions(-)
crypto/async_tx/async_pq.c +14 -4
index 56bd612927ab..af1912313a23 100644
@@ -194,9 +194,9 @@ async_gen_syndrome(struct page **blocks, unsigned int offset, int disks,
            (src_cnt <= dma_maxpq(device, 0) ||
             dma_maxpq(device, DMA_PREP_CONTINUE) > 0) &&
            is_dma_pq_aligned(device, offset, 0, len)) {
-               struct dma_async_tx_descriptor *tx;
+               struct dma_async_tx_descriptor *tx = NULL;
                enum dma_ctrl_flags dma_flags = 0;
-               unsigned char coefs[src_cnt];
+               unsigned char *coefs;
                int i, j;
 
                /* run the p+q asynchronously */
@@ -207,6 +207,9 @@ async_gen_syndrome(struct page **blocks, unsigned int offset, int disks,
                 * sources and update the coefficients accordingly
                 */
                unmap->len = len;
+               coefs = kmalloc_array(src_cnt, sizeof(*coefs), GFP_KERNEL);
+               if (!coefs)
+                       goto out;
                for (i = 0, j = 0; i < src_cnt; i++) {
                        if (blocks[i] == NULL)
                                continue;
@@ -240,7 +243,9 @@ async_gen_syndrome(struct page **blocks, unsigned int offset, int disks,
                }
 
                tx = do_async_gen_syndrome(chan, coefs, j, unmap, dma_flags, submit);
+out:
                dmaengine_unmap_put(unmap);
+               kfree(coefs);
                return tx;
        }
 
@@ -298,8 +303,8 @@ async_syndrome_val(struct page **blocks, unsigned int offset, int disks,
 {
        struct dma_chan *chan = pq_val_chan(submit, blocks, disks, len);
        struct dma_device *device = chan ? chan->device : NULL;
-       struct dma_async_tx_descriptor *tx;
-       unsigned char coefs[disks-2];
+       struct dma_async_tx_descriptor *tx = NULL;
+       unsigned char *coefs = NULL;
        enum dma_ctrl_flags dma_flags = submit->cb_fn ? DMA_PREP_INTERRUPT : 0;
        struct dmaengine_unmap_data *unmap = NULL;
 
@@ -318,6 +323,9 @@ async_syndrome_val(struct page **blocks, unsigned int offset, int disks,
                         __func__, disks, len);
 
                unmap->len = len;
+               coefs = kmalloc_array(disks - 2, sizeof(*coefs), GFP_KERNEL);
+               if (!coefs)
+                       goto out;
                for (i = 0; i < disks-2; i++)
                        if (likely(blocks[i])) {
                                unmap->addr[j] = dma_map_page(dev, blocks[i],
@@ -423,6 +431,8 @@ async_syndrome_val(struct page **blocks, unsigned int offset, int disks,
                async_tx_sync_epilog(submit);
                tx = NULL;
        }
+out:
+       kfree(coefs);
        dmaengine_unmap_put(unmap);
 
        return tx;
crypto/async_tx/raid6test.c +7 -2
index dad95f45b88f..ea036b531ef2 100644
@@ -81,11 +81,16 @@ static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, stru
                        init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
                        tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
                } else {
-                       struct page *blocks[disks];
+                       struct page **blocks;
                        struct page *dest;
                        int count = 0;
                        int i;
 
+                       blocks = kmalloc_array(disks, sizeof(*blocks),
+                                                       GFP_KERNEL);
+                       if (!blocks)
+                               return;
+
                        /* data+Q failure.  Reconstruct data from P,
                         * then rebuild syndrome
                         */
@@ -101,6 +106,7 @@ static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, stru
 
                        init_async_submit(&submit, 0, tx, NULL, NULL, addr_conv);
                        tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
+                       kfree(blocks);
                }
        } else {
                if (failb == disks-2) {
--

2.17.0.441.gb46fe60e1d-goog

Signature · 2 lines
Kees Cook
Pixel Security
#2 lore raw
In reply to Kyle Spiers
Thank you for the patch! Yet something to improve:

Hi Kyle,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on cryptodev/master]
[also build test ERROR on v4.17-rc3 next-20180504]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Kyle-Spiers/async_pq-Remove-VLA-usage/20180505-012638
base:   https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master
config: sparc64-allmodconfig (attached as .config)
compiler: sparc64-linux-gnu-gcc (Debian 7.2.0-11) 7.2.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=sparc64

All error/warnings (new ones prefixed by >>):

crypto/async_tx/raid6test.c: In function 'raid6_dual_recov':

>> crypto/async_tx/raid6test.c:89:13: error: implicit declaration of function 'kmalloc_array'; did you mean 'kvmalloc_array'? [-Werror=implicit-function-declaration]
       blocks = kmalloc_array(disks, sizeof(*blocks),
                ^~~~~~~~~~~~~
                kvmalloc_array
>> crypto/async_tx/raid6test.c:89:11: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
       blocks = kmalloc_array(disks, sizeof(*blocks),
              ^
>> crypto/async_tx/raid6test.c:109:4: error: implicit declaration of function 'kfree'; did you mean 'kvfree'? [-Werror=implicit-function-declaration]
       kfree(blocks);
       ^~~~~
       kvfree

cc1: some warnings being treated as errors

vim +89 crypto/async_tx/raid6test.c

    66	
    67	/* Recover two failed blocks. */
    68	static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, struct page **ptrs)
    69	{
    70		struct async_submit_ctl submit;
    71		struct completion cmp;
    72		struct dma_async_tx_descriptor *tx = NULL;
    73		enum sum_check_flags result = ~0;
    74	
    75		if (faila > failb)
    76			swap(faila, failb);
    77	
    78		if (failb == disks-1) {
    79			if (faila == disks-2) {
    80				/* P+Q failure.  Just rebuild the syndrome. */
    81				init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
    82				tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
    83			} else {
    84				struct page **blocks;
    85				struct page *dest;
    86				int count = 0;
    87				int i;
    88	
  > 89				blocks = kmalloc_array(disks, sizeof(*blocks),
    90								GFP_KERNEL);
    91				if (!blocks)
    92					return;
    93	
    94				/* data+Q failure.  Reconstruct data from P,
    95				 * then rebuild syndrome
    96				 */
    97				for (i = disks; i-- ; ) {
    98					if (i == faila || i == failb)
    99						continue;
   100					blocks[count++] = ptrs[i];
   101				}
   102				dest = ptrs[faila];
   103				init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
   104						  NULL, NULL, addr_conv);
   105				tx = async_xor(dest, blocks, 0, count, bytes, &submit);
   106	
   107				init_async_submit(&submit, 0, tx, NULL, NULL, addr_conv);
   108				tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
 > 109				kfree(blocks);

110 }
111 } else {
112 if (failb == disks-2) {
113 /* data+P failure. */
114 init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
115 tx = async_raid6_datap_recov(disks, bytes, faila, ptrs, &submit);
116 } else {
117 /* data+data failure. */
118 init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
119 tx = async_raid6_2data_recov(disks, bytes, faila, failb, ptrs, &submit);
120 }
121 }
122 init_completion(&cmp);
123 init_async_submit(&submit, ASYNC_TX_ACK, tx, callback, &cmp, addr_conv);
124 tx = async_syndrome_val(ptrs, 0, disks, bytes, &result, spare, &submit);
125 async_tx_issue_pending(tx);
126
127 if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0)
128 pr("%s: timeout! (faila: %d failb: %d disks: %d)\n",
129 __func__, faila, failb, disks);
130
131 if (result != 0)
132 pr("%s: validation failure! faila: %d failb: %d sum_check_flags: %x\n",
133 __func__, faila, failb, result);
134 }
135

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation