~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/block/blk-crypto.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /block/blk-crypto.c (Version linux-6.12-rc7) and /block/blk-crypto.c (Version linux-4.9.337)


  1 // SPDX-License-Identifier: GPL-2.0                 1 
  2 /*                                                
  3  * Copyright 2019 Google LLC                      
  4  */                                               
  5                                                   
  6 /*                                                
  7  * Refer to Documentation/block/inline-encrypt    
  8  */                                               
  9                                                   
 10 #define pr_fmt(fmt) "blk-crypto: " fmt            
 11                                                   
 12 #include <linux/bio.h>                            
 13 #include <linux/blkdev.h>                         
 14 #include <linux/blk-crypto-profile.h>             
 15 #include <linux/module.h>                         
 16 #include <linux/ratelimit.h>                      
 17 #include <linux/slab.h>                           
 18                                                   
 19 #include "blk-crypto-internal.h"                  
 20                                                   
 21 const struct blk_crypto_mode blk_crypto_modes[    
 22         [BLK_ENCRYPTION_MODE_AES_256_XTS] = {     
 23                 .name = "AES-256-XTS",            
 24                 .cipher_str = "xts(aes)",         
 25                 .keysize = 64,                    
 26                 .ivsize = 16,                     
 27         },                                        
 28         [BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV    
 29                 .name = "AES-128-CBC-ESSIV",      
 30                 .cipher_str = "essiv(cbc(aes),    
 31                 .keysize = 16,                    
 32                 .ivsize = 16,                     
 33         },                                        
 34         [BLK_ENCRYPTION_MODE_ADIANTUM] = {        
 35                 .name = "Adiantum",               
 36                 .cipher_str = "adiantum(xchach    
 37                 .keysize = 32,                    
 38                 .ivsize = 32,                     
 39         },                                        
 40         [BLK_ENCRYPTION_MODE_SM4_XTS] = {         
 41                 .name = "SM4-XTS",                
 42                 .cipher_str = "xts(sm4)",         
 43                 .keysize = 32,                    
 44                 .ivsize = 16,                     
 45         },                                        
 46 };                                                
 47                                                   
 48 /*                                                
 49  * This number needs to be at least (the numbe    
 50  * concurrently) * (maximum recursive depth of    
 51  * deadlock on crypt_ctx allocations. The defa    
 52  * as the default number of post read contexts    
 53  */                                               
 54 static int num_prealloc_crypt_ctxs = 128;         
 55                                                   
 56 module_param(num_prealloc_crypt_ctxs, int, 044    
 57 MODULE_PARM_DESC(num_prealloc_crypt_ctxs,         
 58                 "Number of bio crypto contexts    
 59                                                   
 60 static struct kmem_cache *bio_crypt_ctx_cache;    
 61 static mempool_t *bio_crypt_ctx_pool;             
 62                                                   
 63 static int __init bio_crypt_ctx_init(void)        
 64 {                                                 
 65         size_t i;                                 
 66                                                   
 67         bio_crypt_ctx_cache = KMEM_CACHE(bio_c    
 68         if (!bio_crypt_ctx_cache)                 
 69                 goto out_no_mem;                  
 70                                                   
 71         bio_crypt_ctx_pool = mempool_create_sl    
 72                                                   
 73         if (!bio_crypt_ctx_pool)                  
 74                 goto out_no_mem;                  
 75                                                   
 76         /* This is assumed in various places.     
 77         BUILD_BUG_ON(BLK_ENCRYPTION_MODE_INVAL    
 78                                                   
 79         /* Sanity check that no algorithm exce    
 80         for (i = 0; i < BLK_ENCRYPTION_MODE_MA    
 81                 BUG_ON(blk_crypto_modes[i].key    
 82                 BUG_ON(blk_crypto_modes[i].ivs    
 83         }                                         
 84                                                   
 85         return 0;                                 
 86 out_no_mem:                                       
 87         panic("Failed to allocate mem for bio     
 88 }                                                 
 89 subsys_initcall(bio_crypt_ctx_init);              
 90                                                   
 91 void bio_crypt_set_ctx(struct bio *bio, const     
 92                        const u64 dun[BLK_CRYPT    
 93 {                                                 
 94         struct bio_crypt_ctx *bc;                 
 95                                                   
 96         /*                                        
 97          * The caller must use a gfp_mask that    
 98          * that the mempool_alloc() can't fail    
 99          */                                       
100         WARN_ON_ONCE(!(gfp_mask & __GFP_DIRECT    
101                                                   
102         bc = mempool_alloc(bio_crypt_ctx_pool,    
103                                                   
104         bc->bc_key = key;                         
105         memcpy(bc->bc_dun, dun, sizeof(bc->bc_    
106                                                   
107         bio->bi_crypt_context = bc;               
108 }                                                 
109                                                   
110 void __bio_crypt_free_ctx(struct bio *bio)        
111 {                                                 
112         mempool_free(bio->bi_crypt_context, bi    
113         bio->bi_crypt_context = NULL;             
114 }                                                 
115                                                   
116 int __bio_crypt_clone(struct bio *dst, struct     
117 {                                                 
118         dst->bi_crypt_context = mempool_alloc(    
119         if (!dst->bi_crypt_context)               
120                 return -ENOMEM;                   
121         *dst->bi_crypt_context = *src->bi_cryp    
122         return 0;                                 
123 }                                                 
124                                                   
125 /* Increments @dun by @inc, treating @dun as a    
126 void bio_crypt_dun_increment(u64 dun[BLK_CRYPT    
127                              unsigned int inc)    
128 {                                                 
129         int i;                                    
130                                                   
131         for (i = 0; inc && i < BLK_CRYPTO_DUN_    
132                 dun[i] += inc;                    
133                 /*                                
134                  * If the addition in this lim    
135                  * carry 1 into the next limb.    
136                  */                               
137                 if (dun[i] < inc)                 
138                         inc = 1;                  
139                 else                              
140                         inc = 0;                  
141         }                                         
142 }                                                 
143                                                   
144 void __bio_crypt_advance(struct bio *bio, unsi    
145 {                                                 
146         struct bio_crypt_ctx *bc = bio->bi_cry    
147                                                   
148         bio_crypt_dun_increment(bc->bc_dun,       
149                                 bytes >> bc->b    
150 }                                                 
151                                                   
152 /*                                                
153  * Returns true if @bc->bc_dun plus @bytes con    
154  * @next_dun, treating the DUNs as multi-limb     
155  */                                               
156 bool bio_crypt_dun_is_contiguous(const struct     
157                                  unsigned int     
158                                  const u64 nex    
159 {                                                 
160         int i;                                    
161         unsigned int carry = bytes >> bc->bc_k    
162                                                   
163         for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_S    
164                 if (bc->bc_dun[i] + carry != n    
165                         return false;             
166                 /*                                
167                  * If the addition in this lim    
168                  * carry 1 into the next limb.    
169                  */                               
170                 if ((bc->bc_dun[i] + carry) <     
171                         carry = 1;                
172                 else                              
173                         carry = 0;                
174         }                                         
175                                                   
176         /* If the DUN wrapped through 0, don't    
177         return carry == 0;                        
178 }                                                 
179                                                   
180 /*                                                
181  * Checks that two bio crypt contexts are comp    
182  * they are mergeable except for data_unit_num    
183  */                                               
184 static bool bio_crypt_ctx_compatible(struct bi    
185                                      struct bi    
186 {                                                 
187         if (!bc1)                                 
188                 return !bc2;                      
189                                                   
190         return bc2 && bc1->bc_key == bc2->bc_k    
191 }                                                 
192                                                   
193 bool bio_crypt_rq_ctx_compatible(struct reques    
194 {                                                 
195         return bio_crypt_ctx_compatible(rq->cr    
196 }                                                 
197                                                   
198 /*                                                
199  * Checks that two bio crypt contexts are comp    
200  * that their data_unit_nums are continuous (a    
201  * in the order @bc1 followed by @bc2.            
202  */                                               
203 bool bio_crypt_ctx_mergeable(struct bio_crypt_    
204                              struct bio_crypt_    
205 {                                                 
206         if (!bio_crypt_ctx_compatible(bc1, bc2    
207                 return false;                     
208                                                   
209         return !bc1 || bio_crypt_dun_is_contig    
210 }                                                 
211                                                   
212 /* Check that all I/O segments are data unit a    
213 static bool bio_crypt_check_alignment(struct b    
214 {                                                 
215         const unsigned int data_unit_size =       
216                 bio->bi_crypt_context->bc_key-    
217         struct bvec_iter iter;                    
218         struct bio_vec bv;                        
219                                                   
220         bio_for_each_segment(bv, bio, iter) {     
221                 if (!IS_ALIGNED(bv.bv_len | bv    
222                         return false;             
223         }                                         
224                                                   
225         return true;                              
226 }                                                 
227                                                   
228 blk_status_t __blk_crypto_rq_get_keyslot(struc    
229 {                                                 
230         return blk_crypto_get_keyslot(rq->q->c    
231                                       rq->cryp    
232                                       &rq->cry    
233 }                                                 
234                                                   
235 void __blk_crypto_rq_put_keyslot(struct reques    
236 {                                                 
237         blk_crypto_put_keyslot(rq->crypt_keysl    
238         rq->crypt_keyslot = NULL;                 
239 }                                                 
240                                                   
241 void __blk_crypto_free_request(struct request     
242 {                                                 
243         /* The keyslot, if one was needed, sho    
244         if (WARN_ON_ONCE(rq->crypt_keyslot))      
245                 __blk_crypto_rq_put_keyslot(rq    
246                                                   
247         mempool_free(rq->crypt_ctx, bio_crypt_    
248         rq->crypt_ctx = NULL;                     
249 }                                                 
250                                                   
251 /**                                               
252  * __blk_crypto_bio_prep - Prepare bio for inl    
253  *                                                
254  * @bio_ptr: pointer to original bio pointer      
255  *                                                
256  * If the bio crypt context provided for the b    
257  * device's inline encryption hardware, do not    
258  *                                                
259  * Otherwise, try to perform en/decryption for    
260  * kernel crypto API. When the crypto API fall    
261  * blk-crypto may choose to split the bio into    
262  * continue to be processed and the second one    
263  * submit_bio_noacct. A bounce bio will be all    
264  * of the aforementioned "first one", and *bio    
265  * bounce bio.                                    
266  *                                                
267  * Caller must ensure bio has bio_crypt_ctx.      
268  *                                                
269  * Return: true on success; false on error (an    
270  *         appropriately, and bio_endio() will    
271  *         submission should abort).              
272  */                                               
273 bool __blk_crypto_bio_prep(struct bio **bio_pt    
274 {                                                 
275         struct bio *bio = *bio_ptr;               
276         const struct blk_crypto_key *bc_key =     
277                                                   
278         /* Error if bio has no data. */           
279         if (WARN_ON_ONCE(!bio_has_data(bio)))     
280                 bio->bi_status = BLK_STS_IOERR    
281                 goto fail;                        
282         }                                         
283                                                   
284         if (!bio_crypt_check_alignment(bio)) {    
285                 bio->bi_status = BLK_STS_IOERR    
286                 goto fail;                        
287         }                                         
288                                                   
289         /*                                        
290          * Success if device supports the encr    
291          * in falling back to the crypto API.     
292          */                                       
293         if (blk_crypto_config_supported_native    
294                                                   
295                 return true;                      
296         if (blk_crypto_fallback_bio_prep(bio_p    
297                 return true;                      
298 fail:                                             
299         bio_endio(*bio_ptr);                      
300         return false;                             
301 }                                                 
302                                                   
303 int __blk_crypto_rq_bio_prep(struct request *r    
304                              gfp_t gfp_mask)      
305 {                                                 
306         if (!rq->crypt_ctx) {                     
307                 rq->crypt_ctx = mempool_alloc(    
308                 if (!rq->crypt_ctx)               
309                         return -ENOMEM;           
310         }                                         
311         *rq->crypt_ctx = *bio->bi_crypt_contex    
312         return 0;                                 
313 }                                                 
314                                                   
315 /**                                               
316  * blk_crypto_init_key() - Prepare a key for u    
317  * @blk_key: Pointer to the blk_crypto_key to     
318  * @raw_key: Pointer to the raw key. Must be t    
319  *           @crypto_mode; see blk_crypto_mode    
320  * @crypto_mode: identifier for the encryption    
321  * @dun_bytes: number of bytes that will be us    
322  *             key is used                        
323  * @data_unit_size: the data unit size to use     
324  *                                                
325  * Return: 0 on success, -errno on failure.  T    
326  *         zeroizing both blk_key and raw_key     
327  */                                               
328 int blk_crypto_init_key(struct blk_crypto_key     
329                         enum blk_crypto_mode_n    
330                         unsigned int dun_bytes    
331                         unsigned int data_unit    
332 {                                                 
333         const struct blk_crypto_mode *mode;       
334                                                   
335         memset(blk_key, 0, sizeof(*blk_key));     
336                                                   
337         if (crypto_mode >= ARRAY_SIZE(blk_cryp    
338                 return -EINVAL;                   
339                                                   
340         mode = &blk_crypto_modes[crypto_mode];    
341         if (mode->keysize == 0)                   
342                 return -EINVAL;                   
343                                                   
344         if (dun_bytes == 0 || dun_bytes > mode    
345                 return -EINVAL;                   
346                                                   
347         if (!is_power_of_2(data_unit_size))       
348                 return -EINVAL;                   
349                                                   
350         blk_key->crypto_cfg.crypto_mode = cryp    
351         blk_key->crypto_cfg.dun_bytes = dun_by    
352         blk_key->crypto_cfg.data_unit_size = d    
353         blk_key->data_unit_size_bits = ilog2(d    
354         blk_key->size = mode->keysize;            
355         memcpy(blk_key->raw, raw_key, mode->ke    
356                                                   
357         return 0;                                 
358 }                                                 
359                                                   
360 bool blk_crypto_config_supported_natively(stru    
361                                           cons    
362 {                                                 
363         return __blk_crypto_cfg_supported(bdev    
364                                           cfg)    
365 }                                                 
366                                                   
367 /*                                                
368  * Check if bios with @cfg can be en/decrypted    
369  * block_device it's submitted to supports inl    
370  * blk-crypto-fallback is enabled and supports    
371  */                                               
372 bool blk_crypto_config_supported(struct block_    
373                                  const struct     
374 {                                                 
375         return IS_ENABLED(CONFIG_BLK_INLINE_EN    
376                blk_crypto_config_supported_nat    
377 }                                                 
378                                                   
379 /**                                               
380  * blk_crypto_start_using_key() - Start using     
381  * @bdev: block device to operate on              
382  * @key: A key to use on the device               
383  *                                                
384  * Upper layers must call this function to ens    
385  * supports the key's crypto settings, or the     
386  * for the needed mode allocated and ready to     
387  * an skcipher, and *should not* be called fro    
388  * cause a deadlock                               
389  *                                                
390  * Return: 0 on success; -ENOPKG if the hardwa    
391  *         blk-crypto-fallback is either disab    
392  *         is disabled in the crypto API; or a    
393  */                                               
394 int blk_crypto_start_using_key(struct block_de    
395                                const struct bl    
396 {                                                 
397         if (blk_crypto_config_supported_native    
398                 return 0;                         
399         return blk_crypto_fallback_start_using    
400 }                                                 
401                                                   
402 /**                                               
403  * blk_crypto_evict_key() - Evict a blk_crypto    
404  * @bdev: a block_device on which I/O using th    
405  * @key: the key to evict                         
406  *                                                
407  * For a given block_device, this function rem    
408  * the keyslot management structures and evict    
409  * keyslot(s) or blk-crypto-fallback keyslot i    
410  *                                                
411  * Upper layers must call this before freeing     
412  * called for every block_device the key may h    
413  * longer be in use by any I/O when this funct    
414  *                                                
415  * Context: May sleep.                            
416  */                                               
417 void blk_crypto_evict_key(struct block_device     
418                           const struct blk_cry    
419 {                                                 
420         struct request_queue *q = bdev_get_que    
421         int err;                                  
422                                                   
423         if (blk_crypto_config_supported_native    
424                 err = __blk_crypto_evict_key(q    
425         else                                      
426                 err = blk_crypto_fallback_evic    
427         /*                                        
428          * An error can only occur here if the    
429          * keyslot (due to a hardware or drive    
430          * use by I/O (due to a kernel bug).      
431          * still unlinked from the keyslot man    
432          * is allowed and expected to free it     
433          * callers can do to handle errors, so    
434          */                                       
435         if (err)                                  
436                 pr_warn_ratelimited("%pg: erro    
437 }                                                 
438 EXPORT_SYMBOL_GPL(blk_crypto_evict_key);          
439                                                   

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php