Commit 85c6201a80ce4464a52c58a5f5ea8de15a557a6f
Committed by
Herbert Xu
1 parent
3385329a0a
Exists in
master
and in
4 other branches
crypto: scatterwalk - Fix scatterwalk_done() test
We are done with the scattergather entry when the walk offset goes past sg->offset + sg->length, not when it crosses a page boundary. There is a similarly queer test in the second half of scatterwalk_pagedone() that probably needs some scrutiny. Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
crypto/scatterwalk.c
1 | /* | 1 | /* |
2 | * Cryptographic API. | 2 | * Cryptographic API. |
3 | * | 3 | * |
4 | * Cipher operations. | 4 | * Cipher operations. |
5 | * | 5 | * |
6 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> | 6 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
7 | * 2002 Adam J. Richter <adam@yggdrasil.com> | 7 | * 2002 Adam J. Richter <adam@yggdrasil.com> |
8 | * 2004 Jean-Luc Cooke <jlcooke@certainkey.com> | 8 | * 2004 Jean-Luc Cooke <jlcooke@certainkey.com> |
9 | * | 9 | * |
10 | * This program is free software; you can redistribute it and/or modify it | 10 | * This program is free software; you can redistribute it and/or modify it |
11 | * under the terms of the GNU General Public License as published by the Free | 11 | * under the terms of the GNU General Public License as published by the Free |
12 | * Software Foundation; either version 2 of the License, or (at your option) | 12 | * Software Foundation; either version 2 of the License, or (at your option) |
13 | * any later version. | 13 | * any later version. |
14 | * | 14 | * |
15 | */ | 15 | */ |
16 | 16 | ||
17 | #include <crypto/scatterwalk.h> | 17 | #include <crypto/scatterwalk.h> |
18 | #include <linux/kernel.h> | 18 | #include <linux/kernel.h> |
19 | #include <linux/mm.h> | 19 | #include <linux/mm.h> |
20 | #include <linux/module.h> | 20 | #include <linux/module.h> |
21 | #include <linux/pagemap.h> | 21 | #include <linux/pagemap.h> |
22 | #include <linux/highmem.h> | 22 | #include <linux/highmem.h> |
23 | #include <linux/scatterlist.h> | 23 | #include <linux/scatterlist.h> |
24 | 24 | ||
25 | static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out) | 25 | static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out) |
26 | { | 26 | { |
27 | void *src = out ? buf : sgdata; | 27 | void *src = out ? buf : sgdata; |
28 | void *dst = out ? sgdata : buf; | 28 | void *dst = out ? sgdata : buf; |
29 | 29 | ||
30 | memcpy(dst, src, nbytes); | 30 | memcpy(dst, src, nbytes); |
31 | } | 31 | } |
32 | 32 | ||
33 | void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg) | 33 | void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg) |
34 | { | 34 | { |
35 | walk->sg = sg; | 35 | walk->sg = sg; |
36 | 36 | ||
37 | BUG_ON(!sg->length); | 37 | BUG_ON(!sg->length); |
38 | 38 | ||
39 | walk->offset = sg->offset; | 39 | walk->offset = sg->offset; |
40 | } | 40 | } |
41 | EXPORT_SYMBOL_GPL(scatterwalk_start); | 41 | EXPORT_SYMBOL_GPL(scatterwalk_start); |
42 | 42 | ||
43 | void *scatterwalk_map(struct scatter_walk *walk, int out) | 43 | void *scatterwalk_map(struct scatter_walk *walk, int out) |
44 | { | 44 | { |
45 | return crypto_kmap(scatterwalk_page(walk), out) + | 45 | return crypto_kmap(scatterwalk_page(walk), out) + |
46 | offset_in_page(walk->offset); | 46 | offset_in_page(walk->offset); |
47 | } | 47 | } |
48 | EXPORT_SYMBOL_GPL(scatterwalk_map); | 48 | EXPORT_SYMBOL_GPL(scatterwalk_map); |
49 | 49 | ||
50 | static void scatterwalk_pagedone(struct scatter_walk *walk, int out, | 50 | static void scatterwalk_pagedone(struct scatter_walk *walk, int out, |
51 | unsigned int more) | 51 | unsigned int more) |
52 | { | 52 | { |
53 | if (out) { | 53 | if (out) { |
54 | struct page *page; | 54 | struct page *page; |
55 | 55 | ||
56 | page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT); | 56 | page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT); |
57 | if (!PageSlab(page)) | 57 | if (!PageSlab(page)) |
58 | flush_dcache_page(page); | 58 | flush_dcache_page(page); |
59 | } | 59 | } |
60 | 60 | ||
61 | if (more) { | 61 | if (more) { |
62 | walk->offset += PAGE_SIZE - 1; | 62 | walk->offset += PAGE_SIZE - 1; |
63 | walk->offset &= PAGE_MASK; | 63 | walk->offset &= PAGE_MASK; |
64 | if (walk->offset >= walk->sg->offset + walk->sg->length) | 64 | if (walk->offset >= walk->sg->offset + walk->sg->length) |
65 | scatterwalk_start(walk, scatterwalk_sg_next(walk->sg)); | 65 | scatterwalk_start(walk, scatterwalk_sg_next(walk->sg)); |
66 | } | 66 | } |
67 | } | 67 | } |
68 | 68 | ||
69 | void scatterwalk_done(struct scatter_walk *walk, int out, int more) | 69 | void scatterwalk_done(struct scatter_walk *walk, int out, int more) |
70 | { | 70 | { |
71 | if (!offset_in_page(walk->offset) || !more) | 71 | if (!(scatterwalk_pagelen(walk) & (PAGE_SIZE - 1)) || !more) |
72 | scatterwalk_pagedone(walk, out, more); | 72 | scatterwalk_pagedone(walk, out, more); |
73 | } | 73 | } |
74 | EXPORT_SYMBOL_GPL(scatterwalk_done); | 74 | EXPORT_SYMBOL_GPL(scatterwalk_done); |
75 | 75 | ||
76 | void scatterwalk_copychunks(void *buf, struct scatter_walk *walk, | 76 | void scatterwalk_copychunks(void *buf, struct scatter_walk *walk, |
77 | size_t nbytes, int out) | 77 | size_t nbytes, int out) |
78 | { | 78 | { |
79 | for (;;) { | 79 | for (;;) { |
80 | unsigned int len_this_page = scatterwalk_pagelen(walk); | 80 | unsigned int len_this_page = scatterwalk_pagelen(walk); |
81 | u8 *vaddr; | 81 | u8 *vaddr; |
82 | 82 | ||
83 | if (len_this_page > nbytes) | 83 | if (len_this_page > nbytes) |
84 | len_this_page = nbytes; | 84 | len_this_page = nbytes; |
85 | 85 | ||
86 | vaddr = scatterwalk_map(walk, out); | 86 | vaddr = scatterwalk_map(walk, out); |
87 | memcpy_dir(buf, vaddr, len_this_page, out); | 87 | memcpy_dir(buf, vaddr, len_this_page, out); |
88 | scatterwalk_unmap(vaddr, out); | 88 | scatterwalk_unmap(vaddr, out); |
89 | 89 | ||
90 | scatterwalk_advance(walk, len_this_page); | 90 | scatterwalk_advance(walk, len_this_page); |
91 | 91 | ||
92 | if (nbytes == len_this_page) | 92 | if (nbytes == len_this_page) |
93 | break; | 93 | break; |
94 | 94 | ||
95 | buf += len_this_page; | 95 | buf += len_this_page; |
96 | nbytes -= len_this_page; | 96 | nbytes -= len_this_page; |
97 | 97 | ||
98 | scatterwalk_pagedone(walk, out, 1); | 98 | scatterwalk_pagedone(walk, out, 1); |
99 | } | 99 | } |
100 | } | 100 | } |
101 | EXPORT_SYMBOL_GPL(scatterwalk_copychunks); | 101 | EXPORT_SYMBOL_GPL(scatterwalk_copychunks); |
102 | 102 | ||
103 | void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg, | 103 | void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg, |
104 | unsigned int start, unsigned int nbytes, int out) | 104 | unsigned int start, unsigned int nbytes, int out) |
105 | { | 105 | { |
106 | struct scatter_walk walk; | 106 | struct scatter_walk walk; |
107 | unsigned int offset = 0; | 107 | unsigned int offset = 0; |
108 | 108 | ||
109 | if (!nbytes) | 109 | if (!nbytes) |
110 | return; | 110 | return; |
111 | 111 | ||
112 | for (;;) { | 112 | for (;;) { |
113 | scatterwalk_start(&walk, sg); | 113 | scatterwalk_start(&walk, sg); |
114 | 114 | ||
115 | if (start < offset + sg->length) | 115 | if (start < offset + sg->length) |
116 | break; | 116 | break; |
117 | 117 | ||
118 | offset += sg->length; | 118 | offset += sg->length; |
119 | sg = scatterwalk_sg_next(sg); | 119 | sg = scatterwalk_sg_next(sg); |
120 | } | 120 | } |
121 | 121 | ||
122 | scatterwalk_advance(&walk, start - offset); | 122 | scatterwalk_advance(&walk, start - offset); |
123 | scatterwalk_copychunks(buf, &walk, nbytes, out); | 123 | scatterwalk_copychunks(buf, &walk, nbytes, out); |
124 | scatterwalk_done(&walk, out, 0); | 124 | scatterwalk_done(&walk, out, 0); |
125 | } | 125 | } |
126 | EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy); | 126 | EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy); |
127 | 127 |