block: Make blk-integrity preclude hardware inline encryption
authorSatya Tangirala <satyat@google.com>
Thu, 14 May 2020 00:37:19 +0000 (00:37 +0000)
committerJens Axboe <axboe@kernel.dk>
Thu, 14 May 2020 15:48:03 +0000 (09:48 -0600)
Whenever a device supports blk-integrity, make the kernel pretend that
the device doesn't support inline encryption (essentially by setting the
keyslot manager in the request queue to NULL).

There's no hardware currently that supports both integrity and inline
encryption. However, it seems possible that there will be such hardware
in the near future (like the NVMe key per I/O support that might support
both inline encryption and PI).

But properly integrating both features is not trivial, and without
real hardware that implements both, it is difficult to tell if it will
be done correctly by the majority of hardware that support both.
So it seems best not to support both features together right now, and
to decide what to do at probe time.

Signed-off-by: Satya Tangirala <satyat@google.com>
Reviewed-by: Eric Biggers <ebiggers@google.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/bio-integrity.c
block/blk-integrity.c
block/keyslot-manager.c
include/linux/blkdev.h

index bf62c25cde8f4bbed3e43c2f2dd77cd7daf7fa33..3579ac0f6ec1f95de1c623dbe965c63191b810e4 100644 (file)
@@ -42,6 +42,9 @@ struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
        struct bio_set *bs = bio->bi_pool;
        unsigned inline_vecs;
 
+       if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
+               return ERR_PTR(-EOPNOTSUPP);
+
        if (!bs || !mempool_initialized(&bs->bio_integrity_pool)) {
                bip = kmalloc(struct_size(bip, bip_inline_vecs, nr_vecs), gfp_mask);
                inline_vecs = nr_vecs;
index ff1070edbb400d8c252ba1bdca27fffa15dc945c..c03705cbb9c9f20b9f5f9f2b123fc5883e4287ec 100644 (file)
@@ -409,6 +409,13 @@ void blk_integrity_register(struct gendisk *disk, struct blk_integrity *template
        bi->tag_size = template->tag_size;
 
        disk->queue->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
+
+#ifdef CONFIG_BLK_INLINE_ENCRYPTION
+       if (disk->queue->ksm) {
+               pr_warn("blk-integrity: Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n");
+               blk_ksm_unregister(disk->queue);
+       }
+#endif
 }
 EXPORT_SYMBOL(blk_integrity_register);
 
index fcd3fd469d7c1969b72a2efdd0bc93caa29ef6b6..c2ef41b3147ba95bbcf57bc678286d7464654e98 100644 (file)
@@ -25,6 +25,9 @@
  * Upper layers will call blk_ksm_get_slot_for_key() to program a
  * key into some slot in the inline encryption hardware.
  */
+
+#define pr_fmt(fmt) "blk-crypto: " fmt
+
 #include <linux/keyslot-manager.h>
 #include <linux/atomic.h>
 #include <linux/mutex.h>
@@ -376,3 +379,19 @@ void blk_ksm_destroy(struct blk_keyslot_manager *ksm)
        memzero_explicit(ksm, sizeof(*ksm));
 }
 EXPORT_SYMBOL_GPL(blk_ksm_destroy);
+
+bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q)
+{
+       if (blk_integrity_queue_supports_integrity(q)) {
+               pr_warn("Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n");
+               return false;
+       }
+       q->ksm = ksm;
+       return true;
+}
+EXPORT_SYMBOL_GPL(blk_ksm_register);
+
+void blk_ksm_unregister(struct request_queue *q)
+{
+       q->ksm = NULL;
+}
index 52a9f456cadf965f6742500081ad8e33c1b76480..2b33166b9daf11dcf7414329805dedbe17d63888 100644 (file)
@@ -1583,6 +1583,12 @@ struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
        return blk_get_integrity(bdev->bd_disk);
 }
 
+static inline bool
+blk_integrity_queue_supports_integrity(struct request_queue *q)
+{
+       return q->integrity.profile;
+}
+
 static inline bool blk_integrity_rq(struct request *rq)
 {
        return rq->cmd_flags & REQ_INTEGRITY;
@@ -1663,6 +1669,11 @@ static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
 {
        return NULL;
 }
+static inline bool
+blk_integrity_queue_supports_integrity(struct request_queue *q)
+{
+       return false;
+}
 static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
 {
        return 0;
@@ -1714,6 +1725,25 @@ static inline struct bio_vec *rq_integrity_vec(struct request *rq)
 
 #endif /* CONFIG_BLK_DEV_INTEGRITY */
 
+#ifdef CONFIG_BLK_INLINE_ENCRYPTION
+
+bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q);
+
+void blk_ksm_unregister(struct request_queue *q);
+
+#else /* CONFIG_BLK_INLINE_ENCRYPTION */
+
+static inline bool blk_ksm_register(struct blk_keyslot_manager *ksm,
+                                   struct request_queue *q)
+{
+       return true;
+}
+
+static inline void blk_ksm_unregister(struct request_queue *q) { }
+
+#endif /* CONFIG_BLK_INLINE_ENCRYPTION */
+
+
 struct block_device_operations {
        int (*open) (struct block_device *, fmode_t);
        void (*release) (struct gendisk *, fmode_t);