Skip to contentSkip to conversation

SED OPAL Library

PATCH Superseded Scott Bauer 8 participants 35 messages Dec 19, 2016 – Dec 28, 2016

Subsystems (5)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover SED OPAL Library

Changes from v2->v3:

1) Removed the necessity of passing around block devices into the opal code. We now pass around a sed_context structure which contains a previously allocated opal_dev structure, sec_ops fn pointers, and opaque data for the send/recv functions to use.
2) Removed the allocation of the opal_dev structure from the opal_code to the driver wishing to use opal. The driver will use a sed_context, structure and allocate an opal_dev structure for each device. In the case of NVMe we store the sed_context structure in the control struct. When someone wishes to issue opal commands down to the controller they open the char dev. In the NVMe open implementation we assign our sed_context into the file structure.

Pushing the burden of allocating and storing the opal_dev into the driver alleviates a bunch of look up code we had in v1/v2. Now by the time we get into the sed-opal code the driver has assigned us a sed_context and we operate directly on that.

This should help with in-kernel use cases as well.
3) Since Opal will operate on the entire device, not per-namespace, we moved from block/ to fs/ and will operate on the nvme character driver. Because of that the sed "guts" have moved from block/ back to lib/.
4) Numerous code clean ups in sed-opal.c to shorten the file. ~700 lines reduced
5) Removed the variadic test_and_add_va for a hopefully easier to understand and maintain ADD_TOKEN macro that assigns values into the flat buffer and does error checking.

--------------------------------------------------------------------

This Patch series implements a large portion of the Opal protocol for self encrypting devices. The driver has the capability of storing a locking range's password. The password can then be replayed during a resume from previous suspend-to-RAM.

The driver also supports logic to bring the device out of a factory default-inactive state into a functional Opal state.

The following logic is supported in order to bring the tper into a working state:

1) Taking Ownership of the drive (Setting the Admin CPIN).
2) Activating the Locking SP (In Single User Mode or Normal Mode).
3) Setting up Locking Ranges (Single User or Normal Mode).
4) Adding users to Locking Ranges (Normal Mode Only).
5) Locking or Unlocking Locking Ranges (Single User Mode or Normal Mode).
6) Reverting the TPer (Restore to factory default).
7) Setting LR/User passwords (Single User Mode or Normal Mode).
8) Enabling/disabling Shadow MBR.
9) Enabling Users in the LockingSP (Normal Mode Only).
10) Saving Password for resume from suspend.
11) Erase and Secure erasing locking ranges.

All commands are exported through the Fs ioctl.

Scott Bauer (5):
include: Add definitions for sed
lib: Add Sed-opal library
fs: Wire up SED/Opal to ioctl
nvme: Implement resume_from_suspend and SED Allocation code.
Maintainers: Add Information for SED Opal library

 MAINTAINERS                   |   10 +
 drivers/nvme/host/core.c      |   67 ++
 drivers/nvme/host/nvme.h      |    8 +-
 drivers/nvme/host/pci.c       |   10 +-
 fs/ioctl.c                    |    3 +
 include/linux/fs.h            |    2 +
 include/linux/sed-opal.h      |   38 +
 include/linux/sed.h           |   76 ++
 include/uapi/linux/sed-opal.h |   94 ++
 include/uapi/linux/sed.h      |   64 ++
 lib/Makefile                  |    2 +-
 lib/sed-opal.c                | 2376 +++++++++++++++++++++++++++++++++++++++++
 lib/sed-opal_internal.h       |  601 +++++++++++
 lib/sed.c                     |  197 ++++
 14 files changed, 3545 insertions(+), 3 deletions(-)
 create mode 100644 include/linux/sed-opal.h
 create mode 100644 include/linux/sed.h
 create mode 100644 include/uapi/linux/sed-opal.h
 create mode 100644 include/uapi/linux/sed.h
 create mode 100644 lib/sed-opal.c
 create mode 100644 lib/sed-opal_internal.h
 create mode 100644 lib/sed.c
#1 lore raw

PATCH v3 1/5 include: Add definitions for sed

This patch adds the definitions and structures for the SED Opal code.

Signed-off-by Scott Bauer <scott.bauer at intel.com>
Signed-off-by Rafael Antognolli <Rafael.Antognolli at intel.com>
---
 include/linux/sed-opal.h      | 38 +++++++++++++++++
 include/linux/sed.h           | 76 ++++++++++++++++++++++++++++++++++
 include/uapi/linux/sed-opal.h | 94 +++++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/sed.h      | 64 +++++++++++++++++++++++++++++
 4 files changed, 272 insertions(+)
 create mode 100644 include/linux/sed-opal.h
 create mode 100644 include/linux/sed.h
 create mode 100644 include/uapi/linux/sed-opal.h
 create mode 100644 include/uapi/linux/sed.h
include/linux/sed-opal.h new file +38 -0
new file mode 100644
index 0000000..668401c
@@ -0,0 +1,38 @@
+/*
+ * Copyright ? 2016 Intel Corporation
+ *
+ * Authors:
+ *    Rafael Antognolli <rafael.antognolli at intel.com>
+ *    Scott  Bauer      <scott.bauer at intel.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#ifndef LINUX_OPAL_H
+#define LINUX_OPAL_H
+
+#include <linux/sed.h>
+#include <linux/kernel.h>
+
+int opal_save(struct sed_context *sedc, struct sed_key *key);
+int opal_lock_unlock(struct sed_context *sedc, struct sed_key *key);
+int opal_take_ownership(struct sed_context *sedc, struct sed_key *key);
+int opal_activate_lsp(struct sed_context *sedc, struct sed_key *key);
+int opal_set_new_pw(struct sed_context *sedc, struct sed_key *key);
+int opal_activate_user(struct sed_context *sedc, struct sed_key *key);
+int opal_reverttper(struct sed_context *sedc, struct sed_key *key);
+int opal_setup_locking_range(struct sed_context *sedc, struct sed_key *key);
+int opal_add_user_to_lr(struct sed_context *sedc, struct sed_key *key);
+int opal_enable_disable_shadow_mbr(struct sed_context *sedc, struct sed_key *key);
+int opal_erase_locking_range(struct sed_context *sedc, struct sed_key *key);
+int opal_secure_erase_locking_range(struct sed_context *sedc, struct sed_key *key);
+int opal_unlock_from_suspend(struct sed_context *sedc);
+struct opal_dev *alloc_opal_dev(struct request_queue *q);
+#endif /* LINUX_OPAL_H */
include/linux/sed.h new file +76 -0

76 lines not on this page · raw message

include/uapi/linux/sed-opal.h new file +94 -0

94 lines not on this page · raw message

include/uapi/linux/sed.h new file +64 -0

64 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH v3 1/5] include: Add definitions for sed

Quoted 2 lines · Scott Bauer

This patch adds the definitions and structures for the SED Opal code.

This seems to contain a few things: userspace ABIs, on the wire defintions, and prototypes for the code added in patch 2.

The userspace ABIs should be a header on it's own. For new code we also usually try to split the protocol definitions from the in-kernel APIs, although that's not even reflected in the headers here. If you think it's reasonable to split those out those should be a separate new patch, and the actual in-kernel data structures and protoypes should just go with the actual code in your current patch 2.

Quoted 9 lines

+/**
+ * struct sed_context - SED Security context for a device
+ * @ops:The Trusted Send/Recv functions.
+ * @sec_data:Opaque pointer that will be passed to the send/recv fn.
+ *Drivers can use this to pass necessary data required for
+ *Their implementation of send/recv.
+ * @dev:Currently an Opal Dev structure. In the future can be other types
+ *Of security structures.
+ */

This is missing a couple spaces for the common kerneldoc format.

include/linux/sed.h line 53

Quoted 5 lines
include/linux/sed.h new file +5 -0
+struct sed_context {
+	const struct sec_ops *ops;
+	void *sec_data;
+	void *dev;
+};

What is the difference between sec_data and dev? And do your really need either one when using the container_of trick I pointed out in response to the other patch.

Quoted 15 lines
include/uapi/linux/sed.h new file +15 -0
+struct sed_key {
+	__u32 sed_type;
+	union {
+		struct opal_key            opal;
+		struct opal_new_pw         opal_pw;
+		struct opal_session_info   opal_session;
+		struct opal_user_lr_setup  opal_lrs;
+		struct opal_lock_unlock    opal_lk_unlk;
+		struct opal_mbr_data       opal_mbr;
+		/* additional command set key types */
+	};
+};
+
+#define IOC_SED_SAVE		   _IOW('p', 220, struct sed_key)
+#define IOC_SED_LOCK_UNLOCK	   _IOW('p', 221, struct sed_key)

include/uapi/linux/sed.h line 58

Quoted 10 lines
include/uapi/linux/sed.h new file +10 -0
+#define IOC_SED_TAKE_OWNERSHIP	   _IOW('p', 222, struct sed_key)
+#define IOC_SED_ACTIVATE_LSP       _IOW('p', 223, struct sed_key)
+#define IOC_SED_SET_PW             _IOW('p', 224, struct sed_key)
+#define IOC_SED_ACTIVATE_USR       _IOW('p', 225, struct sed_key)
+#define IOC_SED_REVERT_TPR         _IOW('p', 226, struct sed_key)
+#define IOC_SED_LR_SETUP           _IOW('p', 227, struct sed_key)
+#define IOC_SED_ADD_USR_TO_LR      _IOW('p', 228, struct sed_key)
+#define IOC_SED_ENABLE_DISABLE_MBR _IOW('p', 229, struct sed_key)
+#define IOC_SED_ERASE_LR           _IOW('p', 230, struct sed_key)
+#define IOC_SED_SECURE_ERASE_LR    _IOW('p', 231, struct sed_key)

I'll need to look at the details again, but it seems like each ioctl uses exactly one of the structures in the union above, so there is no real need for that union.

#3 lore raw

Re: [PATCH v3 1/5] include: Add definitions for sed

In reply to Scott Bauer

include/linux/sed.h line 36

Quoted 9 lines · Scott Bauer
include/linux/sed.h +7 -0
 
+/*
+ * These constant values come from:
+ * TCG Storage Architecture Core Spec v2.01 r1
+ * Section: 3.3 Interface Communications
+ */
+enum {
+	TCG_SECP_00 = 0,

Protocol 0 is not defined by TCG. Perhaps you should reference SPC-4 section 6.30 SECURITY PROTOCOL IN command / table 265.

Quoted 26 lines
include/uapi/linux/sed.h +24 -0
 
+enum sed_key_type {
+	OPAL,
+	OPAL_PW,
+	OPAL_ACT_USR,
+	OPAL_LR_SETUP,
+	OPAL_LOCK_UNLOCK,
+	OPAL_MBR_DATA,
+};
+
+struct sed_key {
+	__u32 sed_type;
+	union {
+		struct opal_key            opal;
+		struct opal_new_pw         opal_pw;
+		struct opal_session_info   opal_session;
+		struct opal_user_lr_setup  opal_lrs;
+		struct opal_lock_unlock    opal_lk_unlk;
+		struct opal_mbr_data       opal_mbr;
+		/* additional command set key types */
+	};
+};
+
+#define IOC_SED_SAVE		   _IOW('p', 220, struct sed_key)
+#define IOC_SED_LOCK_UNLOCK	   _IOW('p', 221, struct sed_key)

include/uapi/linux/sed.h line 58

Quoted 10 lines
include/uapi/linux/sed.h +10 -0
+#define IOC_SED_TAKE_OWNERSHIP	   _IOW('p', 222, struct sed_key)
+#define IOC_SED_ACTIVATE_LSP       _IOW('p', 223, struct sed_key)
+#define IOC_SED_SET_PW             _IOW('p', 224, struct sed_key)
+#define IOC_SED_ACTIVATE_USR       _IOW('p', 225, struct sed_key)
+#define IOC_SED_REVERT_TPR         _IOW('p', 226, struct sed_key)
+#define IOC_SED_LR_SETUP           _IOW('p', 227, struct sed_key)
+#define IOC_SED_ADD_USR_TO_LR      _IOW('p', 228, struct sed_key)
+#define IOC_SED_ENABLE_DISABLE_MBR _IOW('p', 229, struct sed_key)
+#define IOC_SED_ERASE_LR           _IOW('p', 230, struct sed_key)
+#define IOC_SED_SECURE_ERASE_LR    _IOW('p', 231, struct sed_key)

I'm slightly confused by the split between SED-generic and OPAL-specific files here. Maybe I'm misunderstanding the intent of these ioctls. I think SED means "possible any drive supporting the security command set". Therefore these definitions (quoted) should have OPAL names and live in an OPAL header.

Jethro

#4 lore raw

Re: [PATCH v3 1/5] include: Add definitions for sed

Quoted 13 lines · Jethro Beekman

include/linux/sed.h line 36

Quoted 9 lines · Scott Bauer
include/linux/sed.h +7 -0
 
+/*
+ * These constant values come from:
+ * TCG Storage Architecture Core Spec v2.01 r1
+ * Section: 3.3 Interface Communications
+ */
+enum {
+	TCG_SECP_00 = 0,

Protocol 0 is not defined by TCG. Perhaps you should reference SPC-4 section 6.30 SECURITY PROTOCOL IN command / table 265.

Thanks, I've added this.

Quoted 34 lines
Quoted 34 lines
include/uapi/linux/sed.h +31 -0
 
+enum sed_key_type {
+	OPAL,
+	OPAL_PW,
+	OPAL_ACT_USR,
+	OPAL_LR_SETUP,
+	OPAL_LOCK_UNLOCK,
+	OPAL_MBR_DATA,
+};
+
+struct sed_key {
+	__u32 sed_type;
+	union {
+		struct opal_key            opal;
+		struct opal_new_pw         opal_pw;
+		struct opal_session_info   opal_session;
+		struct opal_user_lr_setup  opal_lrs;
+		struct opal_lock_unlock    opal_lk_unlk;
+		struct opal_mbr_data       opal_mbr;
+		/* additional command set key types */
+	};
+};
+
+#define IOC_SED_SAVE		   _IOW('p', 220, struct sed_key)
+#define IOC_SED_LOCK_UNLOCK	   _IOW('p', 221, struct sed_key)
+#define IOC_SED_TAKE_OWNERSHIP	   _IOW('p', 222, struct sed_key)
+#define IOC_SED_ACTIVATE_LSP       _IOW('p', 223, struct sed_key)
+#define IOC_SED_SET_PW             _IOW('p', 224, struct sed_key)
+#define IOC_SED_ACTIVATE_USR       _IOW('p', 225, struct sed_key)
+#define IOC_SED_REVERT_TPR         _IOW('p', 226, struct sed_key)
+#define IOC_SED_LR_SETUP           _IOW('p', 227, struct sed_key)
+#define IOC_SED_ADD_USR_TO_LR      _IOW('p', 228, struct sed_key)
Quoted 8 lines

include/uapi/linux/sed.h line 58

Quoted 3 lines
include/uapi/linux/sed.h +3 -0
+#define IOC_SED_ENABLE_DISABLE_MBR _IOW('p', 229, struct sed_key)
+#define IOC_SED_ERASE_LR           _IOW('p', 230, struct sed_key)
+#define IOC_SED_SECURE_ERASE_LR    _IOW('p', 231, struct sed_key)

I'm slightly confused by the split between SED-generic and OPAL-specific files here. Maybe I'm misunderstanding the intent of these ioctls. I think SED means "possible any drive supporting the security command set". Therefore these definitions (quoted) should have OPAL names and live in an OPAL header.

You're right. These are opal specific methods. I've ripped them from sed.h and put them in the opal headers, also changed from IOC_SED to IOC_OPAL. Thanks

#5 lore raw

PATCH v3 2/5 lib: Add Sed-opal library

In reply to Scott Bauer

This patch implements the necessary logic to bring an Opal enabled drive out of a factory-enabled into a working Opal state.

This patch set also enables logic to save a password to be replayed during a resume from suspend.

Signed-off-by Scott Bauer <scott.bauer at intel.com>
Signed-off-by Rafael Antognolli <Rafael.Antognolli at intel.com>
---
 lib/Makefile            |    2 +-
 lib/sed-opal.c          | 2376 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/sed-opal_internal.h |  601 ++++++++++++
 lib/sed.c               |  197 ++++
 4 files changed, 3175 insertions(+), 1 deletion(-)
 create mode 100644 lib/sed-opal.c
 create mode 100644 lib/sed-opal_internal.h
 create mode 100644 lib/sed.c
lib/Makefile +1 -1
index 50144a3..acb5d82 100644
@@ -36,7 +36,7 @@ obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
 	 gcd.o lcm.o list_sort.o uuid.o flex_array.o iov_iter.o clz_ctz.o \
 	 bsearch.o find_bit.o llist.o memweight.o kfifo.o \
 	 percpu-refcount.o percpu_ida.o rhashtable.o reciprocal_div.o \
-	 once.o
+	 once.o sed.o sed-opal.o
 obj-y += string_helpers.o
 obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
 obj-y += hexdump.o
lib/sed-opal.c new file +2376 -0

2,376 lines not on this page · raw message

lib/sed-opal_internal.h new file +601 -0

601 lines not on this page · raw message

lib/sed.c new file +197 -0

15 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.7.4

Part of the diff is not shown here. View the raw message.

#6 lore raw

Re: [PATCH v3 2/5] lib: Add Sed-opal library

Quoted 24 lines · Scott Bauer

This patch implements the necessary logic to bring an Opal enabled drive out of a factory-enabled into a working Opal state.

This patch set also enables logic to save a password to be replayed during a resume from suspend.

---
 lib/Makefile            |    2 +-
 lib/sed-opal.c          | 2376 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/sed-opal_internal.h |  601 ++++++++++++
 lib/sed.c               |  197 ++++
 4 files changed, 3175 insertions(+), 1 deletion(-)
 create mode 100644 lib/sed-opal.c
 create mode 100644 lib/sed-opal_internal.h
 create mode 100644 lib/sed.c
lib/Makefile +0 -0
index 50144a3..acb5d82 100644

lib/Makefile obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ · line 39

Quoted 5 lines
lib/Makefile +1 -1
@@ -36,7 +36,7 @@ obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
 	 gcd.o lcm.o list_sort.o uuid.o flex_array.o iov_iter.o clz_ctz.o \
 	 bsearch.o find_bit.o llist.o memweight.o kfifo.o \
 	 percpu-refcount.o percpu_ida.o rhashtable.o reciprocal_div.o \
-	 once.o
+	 once.o sed.o sed-opal.o

This seems like an optional library that some environments may wish to opt-out of building into the kernel. Any reason not to add an entry into the Kconfig to turn this on/off?

#7 lore raw

Re: [PATCH v3 2/5] lib: Add Sed-opal library

Quoted 3 lines · Keith Busch

This seems like an optional library that some environments may wish to opt-out of building into the kernel. Any reason not to add an entry into the Kconfig to turn this on/off?

This needs to be a CONFIG_BLOCK_SED / CONFIG_BLOCK_SED_OPAL and should move to block/.

#8 lore raw

Re: [PATCH v3 2/5] lib: Add Sed-opal library

In reply to Scott Bauer
url: https://github.com/0day-ci/linux/commits/Scott-Bauer/include-Add-definitions-for-sed/20161220-110214 config: i386-tinyconfig (attached as .config) compiler: gcc-6 (Debian 6.2.0-3) 6.2.0…

Hi Scott,

[auto build test ERROR on linus/master]
[also build test ERROR on v4.9 next-20161219]
[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/Scott-Bauer/include-Add-definitions-for-sed/20161220-110214
config: i386-tinyconfig (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

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

In file included from lib/sed.c:20:0:

>> include/linux/sed-opal.h:37:40: warning: 'struct request_queue' declared inside parameter list will not be visible outside of this definition or declaration
    struct opal_dev *alloc_opal_dev(struct request_queue *q);
                                           ^~~~~~~~~~~~~
   lib/sed.c: In function 'fdev_sed_ioctl':
>> lib/sed.c:161:12: error: dereferencing pointer to incomplete type 'struct file'
     if (!filep->f_sedctx || !filep->f_sedctx->ops || !filep->f_sedctx->dev)
               ^~
--
   In file included from lib/sed-opal.c:29:0:
>> include/linux/sed-opal.h:37:40: warning: 'struct request_queue' declared inside parameter list will not be visible outside of this definition or declaration
    struct opal_dev *alloc_opal_dev(struct request_queue *q);
                                           ^~~~~~~~~~~~~
   lib/sed-opal.c: In function 'opal_discovery0_end':
   lib/sed-opal.c:276:6: warning: unused variable 'error' [-Wunused-variable]
     int error = 0;
         ^~~~~
   lib/sed-opal.c: In function 'response_parse':
>> lib/sed-opal.c:793:15: error: invalid storage class for function 'response_get_string'
    static size_t response_get_string(const struct parsed_resp *resp, int n,
                  ^~~~~~~~~~~~~~~~~~~
>> lib/sed-opal.c:793:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    static size_t response_get_string(const struct parsed_resp *resp, int n,
    ^~~~~~
>> lib/sed-opal.c:817:12: error: invalid storage class for function 'response_get_u64'
    static u64 response_get_u64(const struct parsed_resp *resp, int n)
               ^~~~~~~~~~~~~~~~
>> lib/sed-opal.c:846:11: error: invalid storage class for function 'response_status'
    static u8 response_status(const struct parsed_resp *resp)
              ^~~~~~~~~~~~~~~
>> lib/sed-opal.c:866:12: error: invalid storage class for function 'parse_and_check_status'
    static int parse_and_check_status(struct opal_dev *dev)
               ^~~~~~~~~~~~~~~~~~~~~~
>> lib/sed-opal.c:883:13: error: invalid storage class for function 'clear_opal_cmd'
    static void clear_opal_cmd(struct opal_cmd *cmd)
                ^~~~~~~~~~~~~~
>> lib/sed-opal.c:891:12: error: invalid storage class for function 'start_opal_session_cont'
    static int start_opal_session_cont(struct opal_dev *dev)
               ^~~~~~~~~~~~~~~~~~~~~~~
>> lib/sed-opal.c:913:20: error: invalid storage class for function 'opal_dev_get'
    static inline void opal_dev_get(struct opal_dev *dev)
                       ^~~~~~~~~~~~
>> lib/sed-opal.c:918:20: error: invalid storage class for function 'opal_dev_put'
    static inline void opal_dev_put(struct opal_dev *dev)
                       ^~~~~~~~~~~~
>> lib/sed-opal.c:923:12: error: invalid storage class for function 'add_suspend_info'
    static int add_suspend_info(struct opal_dev *dev, struct opal_suspend_data *sus)
               ^~~~~~~~~~~~~~~~
>> lib/sed-opal.c:949:12: error: invalid storage class for function 'end_session_cont'
    static int end_session_cont(struct opal_dev *dev)
               ^~~~~~~~~~~~~~~~
>> lib/sed-opal.c:956:12: error: invalid storage class for function 'finalize_and_send'
    static int finalize_and_send(struct opal_dev *dev, struct opal_cmd *cmd,
               ^~~~~~~~~~~~~~~~~
>> lib/sed-opal.c:972:12: error: invalid storage class for function 'gen_key'
    static int gen_key(struct opal_dev *dev)
               ^~~~~~~
>> lib/sed-opal.c:1002:12: error: invalid storage class for function 'get_active_key_cont'
    static int get_active_key_cont(struct opal_dev *dev)
               ^~~~~~~~~~~~~~~~~~~
>> lib/sed-opal.c:1027:12: error: invalid storage class for function 'get_active_key'
    static int get_active_key(struct opal_dev *dev)
               ^~~~~~~~~~~~~~
>> lib/sed-opal.c:1067:12: error: invalid storage class for function 'generic_lr_enable_disable'
    static int generic_lr_enable_disable(struct opal_cmd *cmd,
               ^~~~~~~~~~~~~~~~~~~~~~~~~
>> lib/sed-opal.c:1107:19: error: invalid storage class for function 'enable_global_lr'
    static inline int enable_global_lr(struct opal_cmd *cmd, u8 *uid,
                      ^~~~~~~~~~~~~~~~
>> lib/sed-opal.c:1118:12: error: invalid storage class for function 'setup_locking_range'
    static int setup_locking_range(struct opal_dev *dev)
               ^~~~~~~~~~~~~~~~~~~
>> lib/sed-opal.c:1183:12: error: invalid storage class for function 'start_generic_opal_session'

static int start_generic_opal_session(struct opal_dev *dev,
^~~~~~~~~~~~~~~~~~~~~~~~~~

vim +161 lib/sed.c

    14	 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
    15	 * more details.
    16	 */
    17	
    18	#include <linux/blkdev.h>
    19	#include <linux/sed.h>
  > 20	#include <linux/sed-opal.h>
    21	#include <asm/uaccess.h>
    22	
    23	int sed_save(struct sed_context *sed_ctx, struct sed_key *key)
    24	{
    25		switch (key->sed_type) {
    26		case OPAL_LOCK_UNLOCK:
    27			return opal_save(sed_ctx, key);
    28		}
    29	
    30		return -EOPNOTSUPP;
    31	}
    32	
    33	int sed_lock_unlock(struct sed_context *sed_ctx, struct sed_key *key)
    34	{
    35		switch (key->sed_type) {
    36		case OPAL_LOCK_UNLOCK:
    37			return opal_lock_unlock(sed_ctx, key);
    38		}
    39	
    40		return -EOPNOTSUPP;
    41	}
    42	
    43	int sed_take_ownership(struct sed_context *sed_ctx, struct sed_key *key)
    44	{
    45	
    46		switch (key->sed_type) {
    47		case OPAL:
    48			return opal_take_ownership(sed_ctx, key);
    49		}
    50	
    51		return -EOPNOTSUPP;
    52	}
    53	
    54	int sed_activate_lsp(struct sed_context *sed_ctx, struct sed_key *key)
    55	{
    56	
    57		switch (key->sed_type) {
    58		case OPAL:
    59			return opal_activate_lsp(sed_ctx, key);
    60		}
    61	
    62		return -EOPNOTSUPP;
    63	}
    64	
    65	int sed_set_pw(struct sed_context *sed_ctx, struct sed_key *key)
    66	{
    67	
    68		switch (key->sed_type) {
    69		case OPAL_PW:
    70			return opal_set_new_pw(sed_ctx, key);
    71		}
    72	
    73		return -EOPNOTSUPP;
    74	}
    75	
    76	int sed_activate_user(struct sed_context *sed_ctx, struct sed_key *key)
    77	{
    78	
    79		switch (key->sed_type) {
    80		case OPAL_ACT_USR:
    81			return opal_activate_user(sed_ctx, key);
    82		}
    83	
    84		return -EOPNOTSUPP;
    85	}
    86	
    87	int sed_reverttper(struct sed_context *sed_ctx, struct sed_key *key)
    88	{
    89	
    90		switch (key->sed_type) {
    91		case OPAL:
    92			return opal_reverttper(sed_ctx, key);
    93		}
    94	
    95		return -EOPNOTSUPP;
    96	}
    97	
    98	int sed_setup_locking_range(struct sed_context *sed_ctx, struct sed_key *key)
    99	{
   100	
   101		switch (key->sed_type) {
   102		case OPAL_LR_SETUP:
   103			return opal_setup_locking_range(sed_ctx, key);
   104		}
   105	
   106		return -EOPNOTSUPP;
   107	}
   108	
   109	int sed_adduser_to_lr(struct sed_context *sed_ctx, struct sed_key *key)
   110	{
   111	
   112		switch (key->sed_type) {
   113		case OPAL_LOCK_UNLOCK:
   114			return opal_add_user_to_lr(sed_ctx, key);
   115		}
   116	
   117		return -EOPNOTSUPP;
   118	}
   119	
   120	int sed_do_mbr(struct sed_context *sed_ctx, struct sed_key *key)
   121	{
   122	
   123		switch (key->sed_type) {
   124		case OPAL_MBR_DATA:
   125			return opal_enable_disable_shadow_mbr(sed_ctx, key);
   126		}
   127	
   128		return -EOPNOTSUPP;
   129	}
   130	
   131	int sed_erase_lr(struct sed_context *sed_ctx, struct sed_key *key)
   132	{
   133	
   134		switch (key->sed_type) {
   135		case OPAL:
   136			return opal_erase_locking_range(sed_ctx, key);
   137		}
   138	
   139		return -EOPNOTSUPP;
   140	}
   141	
   142	int sed_secure_erase_lr(struct sed_context *sed_ctx, struct sed_key *key)
   143	{
   144		switch (key->sed_type) {
   145		case OPAL_ACT_USR:
   146			return opal_secure_erase_locking_range(sed_ctx, key);
   147	
   148		}
   149		return -EOPNOTSUPP;
   150	}
   151	
   152	int fdev_sed_ioctl(struct file *filep, unsigned int cmd,
   153			   unsigned long arg)
   154	{
   155		struct sed_key key;
   156		struct sed_context *sed_ctx;
   157	
   158		if (!capable(CAP_SYS_ADMIN))
   159			return -EACCES;
   160	
 > 161		if (!filep->f_sedctx || !filep->f_sedctx->ops || !filep->f_sedctx->dev)

162 return -ENODEV;
163
164 sed_ctx = filep->f_sedctx;

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

#9 lore raw

Re: [PATCH v3 2/5] lib: Add Sed-opal library

In reply to Scott Bauer
url: https://github.com/0day-ci/linux/commits/Scott-Bauer/include-Add-definitions-for-sed/20161220-110214 config: i386-randconfig-r0-201651 (attached as .config) compiler: gcc-5 (Debian 5.4.1-2)…

Hi Scott,

[auto build test WARNING on linus/master]
[also build test WARNING on v4.9 next-20161219]
[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/Scott-Bauer/include-Add-definitions-for-sed/20161220-110214
config: i386-randconfig-r0-201651 (attached as .config)
compiler: gcc-5 (Debian 5.4.1-2) 5.4.1 20160904 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All warnings (new ones prefixed by >>):

In file included from lib/sed.c:20:0:

>> include/linux/sed-opal.h:37:40: warning: 'struct request_queue' declared inside parameter list
    struct opal_dev *alloc_opal_dev(struct request_queue *q);
                                           ^
>> include/linux/sed-opal.h:37:40: warning: its scope is only this definition or declaration, which is probably not what you want
   lib/sed.c: In function 'fdev_sed_ioctl':
   lib/sed.c:161:12: error: dereferencing pointer to incomplete type 'struct file'
     if (!filep->f_sedctx || !filep->f_sedctx->ops || !filep->f_sedctx->dev)
               ^
--
   In file included from lib/sed-opal.c:29:0:
>> include/linux/sed-opal.h:37:40: warning: 'struct request_queue' declared inside parameter list
    struct opal_dev *alloc_opal_dev(struct request_queue *q);
                                           ^
>> include/linux/sed-opal.h:37:40: warning: its scope is only this definition or declaration, which is probably not what you want
   lib/sed-opal.c: In function 'opal_discovery0_end':
   lib/sed-opal.c:276:6: warning: unused variable 'error' [-Wunused-variable]
     int error = 0;
         ^
   lib/sed-opal.c: In function 'response_parse':
   lib/sed-opal.c:793:15: error: invalid storage class for function 'response_get_string'
    static size_t response_get_string(const struct parsed_resp *resp, int n,
                  ^
   lib/sed-opal.c:793:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    static size_t response_get_string(const struct parsed_resp *resp, int n,
    ^
   lib/sed-opal.c:817:12: error: invalid storage class for function 'response_get_u64'
    static u64 response_get_u64(const struct parsed_resp *resp, int n)
               ^
   lib/sed-opal.c:846:11: error: invalid storage class for function 'response_status'
    static u8 response_status(const struct parsed_resp *resp)
              ^
   lib/sed-opal.c:866:12: error: invalid storage class for function 'parse_and_check_status'
    static int parse_and_check_status(struct opal_dev *dev)
               ^
   lib/sed-opal.c:883:13: error: invalid storage class for function 'clear_opal_cmd'
    static void clear_opal_cmd(struct opal_cmd *cmd)
                ^
   lib/sed-opal.c:891:12: error: invalid storage class for function 'start_opal_session_cont'
    static int start_opal_session_cont(struct opal_dev *dev)
               ^
   lib/sed-opal.c:913:20: error: invalid storage class for function 'opal_dev_get'
    static inline void opal_dev_get(struct opal_dev *dev)
                       ^
   lib/sed-opal.c:918:20: error: invalid storage class for function 'opal_dev_put'
    static inline void opal_dev_put(struct opal_dev *dev)
                       ^
   lib/sed-opal.c:923:12: error: invalid storage class for function 'add_suspend_info'
    static int add_suspend_info(struct opal_dev *dev, struct opal_suspend_data *sus)
               ^
   lib/sed-opal.c:949:12: error: invalid storage class for function 'end_session_cont'
    static int end_session_cont(struct opal_dev *dev)
               ^
   lib/sed-opal.c:956:12: error: invalid storage class for function 'finalize_and_send'
    static int finalize_and_send(struct opal_dev *dev, struct opal_cmd *cmd,
               ^
   lib/sed-opal.c:972:12: error: invalid storage class for function 'gen_key'
    static int gen_key(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1002:12: error: invalid storage class for function 'get_active_key_cont'
    static int get_active_key_cont(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1027:12: error: invalid storage class for function 'get_active_key'
    static int get_active_key(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1067:12: error: invalid storage class for function 'generic_lr_enable_disable'
    static int generic_lr_enable_disable(struct opal_cmd *cmd,
               ^
   lib/sed-opal.c:1107:19: error: invalid storage class for function 'enable_global_lr'
    static inline int enable_global_lr(struct opal_cmd *cmd, u8 *uid,
                      ^
   lib/sed-opal.c:1118:12: error: invalid storage class for function 'setup_locking_range'
    static int setup_locking_range(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1183:12: error: invalid storage class for function 'start_generic_opal_session'
    static int start_generic_opal_session(struct opal_dev *dev,
               ^
   lib/sed-opal.c:1242:12: error: invalid storage class for function 'start_anybodyASP_opal_session'
    static int start_anybodyASP_opal_session(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1248:12: error: invalid storage class for function 'start_SIDASP_opal_session'
    static int start_SIDASP_opal_session(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1271:19: error: invalid storage class for function 'start_admin1LSP_opal_session'
    static inline int start_admin1LSP_opal_session(struct opal_dev *dev)
                      ^
   lib/sed-opal.c:1279:12: error: invalid storage class for function 'start_auth_opal_session'
    static int start_auth_opal_session(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1332:12: error: invalid storage class for function 'revert_tper'
    static int revert_tper(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1354:12: error: invalid storage class for function 'internal_activate_user'
    static int internal_activate_user(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1391:12: error: invalid storage class for function 'erase_locking_range'
    static int erase_locking_range(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1419:12: error: invalid storage class for function 'set_mbr_done'
    static int set_mbr_done(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1451:12: error: invalid storage class for function 'set_mbr_enable_disable'
    static int set_mbr_enable_disable(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1483:12: error: invalid storage class for function 'generic_pw_cmd'
    static int generic_pw_cmd(u8 *key, size_t key_len, u8 *cpin_uid,
               ^
   lib/sed-opal.c:1510:12: error: invalid storage class for function 'set_new_pw'
    static int set_new_pw(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1536:12: error: invalid storage class for function 'set_sid_cpin_pin'
    static int set_sid_cpin_pin(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1551:12: error: invalid storage class for function 'add_user_to_lr'
    static int add_user_to_lr(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1621:12: error: invalid storage class for function 'lock_unlock_locking_range'
    static int lock_unlock_locking_range(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1687:12: error: invalid storage class for function 'lock_unlock_locking_range_SUM'
    static int lock_unlock_locking_range_SUM(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1782:12: error: invalid storage class for function 'get_lsp_lifecycle_cont'
    static int get_lsp_lifecycle_cont(struct opal_dev *dev)
               ^
   lib/sed-opal.c: In function 'get_lsp_lifecycle_cont':
   lib/sed-opal.c:1799:1: warning: label 'err_return' defined but not used [-Wunused-label]
    err_return:
    ^
   lib/sed-opal.c: In function 'response_parse':
   lib/sed-opal.c:1842:12: error: invalid storage class for function 'get_msid_cpin_pin_cont'
    static int get_msid_cpin_pin_cont(struct opal_dev *dev)
               ^
   lib/sed-opal.c: In function 'get_msid_cpin_pin_cont':
   lib/sed-opal.c:1864:2: warning: label 'err_return' defined but not used [-Wunused-label]
     err_return:
     ^
   lib/sed-opal.c: In function 'response_parse':
   lib/sed-opal.c:1868:12: error: invalid storage class for function 'get_msid_cpin_pin'
    static int get_msid_cpin_pin(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1906:12: error: invalid storage class for function 'build_end_opal_session'
    static int build_end_opal_session(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1917:12: error: invalid storage class for function 'end_opal_session'
    static int end_opal_session(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1931:12: error: invalid storage class for function 'end_opal_session_error'
    static int end_opal_session_error(struct opal_dev *dev)
               ^
>> lib/sed-opal.c:1939:40: warning: 'struct request_queue' declared inside parameter list
    struct opal_dev *alloc_opal_dev(struct request_queue *q)
                                           ^
   lib/sed-opal.c: In function 'alloc_opal_dev':
   lib/sed-opal.c:1953:15: error: implicit declaration of function 'queue_dma_alignment' [-Werror=implicit-function-declaration]
     dma_align = (queue_dma_alignment(q) | q->dma_pad_mask) + 1;
                  ^
   lib/sed-opal.c:1953:41: error: dereferencing pointer to incomplete type 'struct request_queue'
     dma_align = (queue_dma_alignment(q) | q->dma_pad_mask) + 1;
                                            ^
   lib/sed-opal.c: In function 'response_parse':
   lib/sed-opal.c:1968:12: error: invalid storage class for function 'do_cmds'
    static int do_cmds(struct opal_dev *dev)
               ^
   lib/sed-opal.c:1968:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    static int do_cmds(struct opal_dev *dev)
    ^
   lib/sed-opal.c:1976:25: error: invalid storage class for function 'get_opal_dev'
    static struct opal_dev *get_opal_dev(struct sed_context *sedc,
                            ^
   lib/sed-opal.c:2019:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    int opal_erase_locking_range(struct sed_context *sedc, struct sed_key *key)
    ^
   lib/sed-opal.c:2043:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    int opal_enable_disable_shadow_mbr(struct sed_context *sedc,
    ^
   lib/sed-opal.c:2077:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    int opal_save(struct sed_context *sedc, struct sed_key *key)
    ^
   lib/sed-opal.c:2098:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    int opal_add_user_to_lr(struct sed_context *sedc, struct sed_key *key)
    ^
   lib/sed-opal.c:2139:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    int opal_reverttper(struct sed_context *sedc, struct sed_key *key)
    ^
   lib/sed-opal.c:2162:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    const opal_step ulk_funcs_SUM[] = {
    ^
   lib/sed-opal.c:2202:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    int opal_take_ownership(struct sed_context *sedc, struct sed_key *key)
    ^
   lib/sed-opal.c:2227:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    int opal_activate_lsp(struct sed_context *sedc, struct sed_key *key)
    ^
   lib/sed-opal.c:2250:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    int opal_setup_locking_range(struct sed_context *sedc, struct sed_key *pw)
    ^
   lib/sed-opal.c:2273:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    int opal_set_new_pw(struct sed_context *sedc, struct sed_key *pw)
    ^
   lib/sed-opal.c:2306:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    int opal_activate_user(struct sed_context *sedc, struct sed_key *pw)
    ^
   lib/sed-opal.c:2342:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    int opal_unlock_from_suspend(struct sed_context *sedc)
    ^
   lib/sed-opal.c:2376:1: error: expected declaration or statement at end of input
    EXPORT_SYMBOL(opal_unlock_from_suspend);
    ^
   lib/sed-opal.c:727:6: warning: unused variable 'ret' [-Wunused-variable]
     int ret, num_entries = 0;
         ^

cc1: some warnings being treated as errors

vim +37 include/linux/sed-opal.h

0d963c73 Scott Bauer 2016-12-19  21  #include <linux/sed.h>
0d963c73 Scott Bauer 2016-12-19  22  #include <linux/kernel.h>
0d963c73 Scott Bauer 2016-12-19  23  
0d963c73 Scott Bauer 2016-12-19  24  int opal_save(struct sed_context *sedc, struct sed_key *key);
0d963c73 Scott Bauer 2016-12-19  25  int opal_lock_unlock(struct sed_context *sedc, struct sed_key *key);
0d963c73 Scott Bauer 2016-12-19  26  int opal_take_ownership(struct sed_context *sedc, struct sed_key *key);
0d963c73 Scott Bauer 2016-12-19  27  int opal_activate_lsp(struct sed_context *sedc, struct sed_key *key);
0d963c73 Scott Bauer 2016-12-19  28  int opal_set_new_pw(struct sed_context *sedc, struct sed_key *key);
0d963c73 Scott Bauer 2016-12-19  29  int opal_activate_user(struct sed_context *sedc, struct sed_key *key);
0d963c73 Scott Bauer 2016-12-19  30  int opal_reverttper(struct sed_context *sedc, struct sed_key *key);
0d963c73 Scott Bauer 2016-12-19  31  int opal_setup_locking_range(struct sed_context *sedc, struct sed_key *key);
0d963c73 Scott Bauer 2016-12-19  32  int opal_add_user_to_lr(struct sed_context *sedc, struct sed_key *key);
0d963c73 Scott Bauer 2016-12-19  33  int opal_enable_disable_shadow_mbr(struct sed_context *sedc, struct sed_key *key);
0d963c73 Scott Bauer 2016-12-19  34  int opal_erase_locking_range(struct sed_context *sedc, struct sed_key *key);
0d963c73 Scott Bauer 2016-12-19  35  int opal_secure_erase_locking_range(struct sed_context *sedc, struct sed_key *key);
0d963c73 Scott Bauer 2016-12-19  36  int opal_unlock_from_suspend(struct sed_context *sedc);

0d963c73 Scott Bauer 2016-12-19 @37 struct opal_dev *alloc_opal_dev(struct request_queue *q); 0d963c73 Scott Bauer 2016-12-19 38 #endif /* LINUX_OPAL_H */

:::::: The code at line 37 was first introduced by commit
:::::: 0d963c733cceca032531cf3f500125bc28d01178 include: Add definitions for sed

:::::: TO: Scott Bauer <scott.bauer at intel.com>
:::::: CC: 0day robot <fengguang.wu at intel.com>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
-------------- next part --------------
A non-text attachment was scrubbed...
Name: .config.gz
Type: application/gzip
Size: 22850 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-nvme/attachments/20161220/b5427c66/attachment-0001.gz>

maintainer
#10 lore raw

Re: [PATCH v3 2/5] lib: Add Sed-opal library

In reply to Scott Bauer

lib/sed.c line 164

Quoted 13 lines · Scott Bauer
lib/sed.c new file +13 -0
+int fdev_sed_ioctl(struct file *filep, unsigned int cmd,
+		   unsigned long arg)
+{
+	struct sed_key key;
+	struct sed_context *sed_ctx;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EACCES;
+
+	if (!filep->f_sedctx || !filep->f_sedctx->ops || !filep->f_sedctx->dev)
+		return -ENODEV;
+
+	sed_ctx = filep->f_sedctx;

First of all, that's a bisect hazard. What's more, looking through the rest of patchset, WTF does it
* need to be called that early in ioctl(2) handling, instead of
having ->ioctl() instance for that sucker calling it?
* _not_ get your ->f_sedctx as an explicit argument, passed by
the caller in ->ioctl(), seeing that it's possible to calculate by file->private_data?
* store that thing in struct file itself, bloating it for everything
all for the sake of few drivers that might want to use that helper?

#11 lore raw

Re: [PATCH v3 2/5] lib: Add Sed-opal library

In reply to Scott Bauer

lib/sed-opal.c line 91

Quoted 3 lines
lib/sed-opal.c new file +3 -0
+	u8 lr;
+	size_t key_name_len;
+	char key_name[36];

Who is going to use the key_name? I can't find another reference to
it anywhere else in the code. The reason why this tripped me off was
the hardcoded length so I was going to check on how access to it is bounds checked.

Quoted 4 lines

+/**
+ * struct opal_dev - The structure representing a OPAL enabled SED.
+ * @sed_ctx:The SED context, contains fn pointers to sec_send/recv.
+ * @opal_step:A series of opal methods that are necessary to complete a comannd.

Quoted 8 lines

+ * @func_data:An array of parameters for the opal methods above.
+ * @state:Describes the current opal_step we're working on.
+ * @dev_lock:Locks the entire opal_dev structure.
+ * @parsed:Parsed response from controller.
+ * @prev_data:Data returned from a method to the controller
+ * @error_cb:Error function that handles closing sessions after a failed method.
+ * @unlk_lst:A list of Locking ranges to unlock on this device during a resume.
+ */

Needs spaces after the colons.

lib/sed-opal.c line 115

Quoted 3 lines
lib/sed-opal.c new file +3 -0
+	u16 comID;
+	u32 HSN;
+	u32 TSN;

Please use lower case variable names in Linux code, and separate words by underscores if needed.

Quoted 1 line

+DEFINE_SPINLOCK(list_spinlock);

Should be marked static.

Quoted 1 line

+#define TPER_SYNC_SUPPORTED BIT(0)

This sounds like a protocol constant and should go into the header with the rest of the protocol.

lib/sed-opal.c line 147

Quoted 8 lines
lib/sed-opal.c new file +8 -0
+static bool check_tper(const void *data)
+{
+	const struct d0_tper_features *tper = data;
+	u8 flags = tper->supported_features;
+
+	if (!(flags & TPER_SYNC_SUPPORTED)) {
+		pr_err("TPer sync not supported. flags = %d\n",
+		       tper->supported_features);

It would be great to use dev_err / dev_warn / etc in the opal code, although for that you'll need to pass a struct device from the driver into the code.

Quoted 1 line

+static bool check_SUM(const void *data)

The comment on member naming above also applies to variables and function names.

lib/sed-opal.c line 280

Quoted 9 lines
lib/sed-opal.c new file +9 -0
+	bool foundComID = false, supported = true, single_user = false;
+	const struct d0_header *hdr;
+	const u8 *epos, *cpos;
+	u16 comID = 0;
+	int error = 0;
+
+	epos = dev->cmd.resp;
+	cpos = dev->cmd.resp;
+	hdr = (struct d0_header *)dev->cmd.resp;

You probably want to structure your command buffers to use void pointers and avoid ths kind of casting.

lib/sed-opal.c line 383

Quoted 7 lines
lib/sed-opal.c new file +7 -0
+#define TINY_ATOM_DATA_MASK GENMASK(5, 0)
+#define TINY_ATOM_SIGNED BIT(6)
+
+#define SHORT_ATOM_ID BIT(7)
+#define SHORT_ATOM_BYTESTRING BIT(5)
+#define SHORT_ATOM_SIGNED BIT(4)
+#define SHORT_ATOM_LEN_MASK GENMASK(3, 0)

Protocol constants for the header again?

lib/sed-opal.c line 533

Quoted 7 lines
lib/sed-opal.c new file +7 -0
+#define ADD_TOKEN_STRING(cmd, key, keylen)		        \
+	if (!err)					        \
+		err = test_and_add_string(cmd, key, keylen);
+
+#define ADD_TOKEN(type, cmd, tok)				\
+	if (!err)						\
+		err = test_and_add_token_##type(cmd, tok);

Please remove these macros and just open code the calls. If you want to avoid writing the if err lines again and again just pass err by reference to the functions and move the err check into the add_token* helpers.

lib/sed-opal.c line 749

Quoted 3 lines
lib/sed-opal.c new file +3 -0
+	if ((hdr->cp.length == 0)
+	    || (hdr->pkt.length == 0)
+	    || (hdr->subpkt.length == 0)) {

No need for the inner braces, also please place the operators at the end of the previous line instead of the beginning of the new line.

lib/sed-opal.c line 772

Quoted 9 lines
lib/sed-opal.c new file +9 -0
+	while (cpos < total) {
+		if (!(pos[0] & 0x80)) /* tiny atom */
+			token_length = response_parse_tiny(iter, pos);
+		else if (!(pos[0] & 0x40)) /* short atom */
+			token_length = response_parse_short(iter, pos);
+		else if (!(pos[0] & 0x20)) /* medium atom */
+			token_length = response_parse_medium(iter, pos);
+		else if (!(pos[0] & 0x10)) /* long atom */
+			token_length = response_parse_long(iter, pos);

Please add symbolic names for these constants to the protocol header.

lib/sed-opal.c line 788

Quoted 4 lines
lib/sed-opal.c new file +4 -0
+	if (num_entries == 0) {
+		pr_err("Couldn't parse response.\n");
+		return -EINVAL;
+	resp->num = num_entries;

Where is the closing brace for that if?

lib/sed-opal.c line 837

Quoted 2 lines
lib/sed-opal.c new file +2 -0
+	if (!((resp->toks[n].width == OPAL_WIDTH_TINY) ||
+	      (resp->toks[n].width == OPAL_WIDTH_SHORT))) {

No need for the inner braces.

lib/sed-opal.c line 849

Quoted 12 lines
lib/sed-opal.c new file +12 -0
+		pr_err("Atom is not short or tiny: %d\n",
+		       resp->toks[n].width);
+		return 0;
+	}
+
+	return resp->toks[n].stored.u;
+}
+
+static u8 response_status(const struct parsed_resp *resp)
+{
+	if ((token_type(resp, 0) == OPAL_DTA_TOKENID_TOKEN)
+	    && (response_get_token(resp, 0) == OPAL_ENDOFSESSION)) {

Same here, also please fix the operator placement.

lib/sed-opal.c line 859

Quoted 4 lines
lib/sed-opal.c new file +4 -0
+	if ((token_type(resp, resp->num - 1) != OPAL_DTA_TOKENID_TOKEN) ||
+	    (token_type(resp, resp->num - 5) != OPAL_DTA_TOKENID_TOKEN) ||
+	    (response_get_token(resp, resp->num - 1) != OPAL_ENDLIST) ||
+	    (response_get_token(resp, resp->num - 5) != OPAL_STARTLIST))

More brace removal here please (and probably a lot more later on, I'm going to skip them from here)

lib/sed-opal.c line 921

Quoted 9 lines
lib/sed-opal.c new file +9 -0
+static inline void opal_dev_get(struct opal_dev *dev)
+{
+	mutex_lock(&dev->dev_lock);
+}
+
+static inline void opal_dev_put(struct opal_dev *dev)
+{
+	mutex_unlock(&dev->dev_lock);
+}

No trivial wrappers around locking primitives, please.

lib/sed-opal.c line 931

Quoted 9 lines
lib/sed-opal.c new file +9 -0
+static int add_suspend_info(struct opal_dev *dev, struct opal_suspend_data *sus)
+{
+	struct opal_suspend_data *iter;
+	bool found = false;
+
+	if (list_empty(&dev->unlk_lst))
+		goto add_out;
+
+	list_for_each_entry(iter, &dev->unlk_lst, node) {

list_for_each_entry will do the right thing for an empty list, you can remove the above check,

lib/sed-opal.c line 942

Quoted 11 lines
lib/sed-opal.c new file +11 -0
+		if (iter->lr == sus->lr) {
+			found = true;
+			break;
+		}
+	}
+
+	if (found) {
+		/* Replace the old with the new */
+		list_del(&iter->node);
+		kfree(iter);
+	}

Just move the list_del and kfree inside the if above and you can remove the found variable.

lib/sed-opal.c line 1732

Quoted 2 lines
lib/sed-opal.c new file +2 -0
+
+int activate_lsp(struct opal_dev *dev)

static?

Quoted 4 lines
lib/sed-opal.c new file +4 -0
+static int get_msid_cpin_pin_cont(struct opal_dev *dev)
+{
+	const char *msid_pin;
+	size_t strlen;

lib/sed-opal.c line 1855

Quoted 10 lines
lib/sed-opal.c new file +10 -0
+	int error = 0;
+
+	error = parse_and_check_status(dev);
+	if (error)
+		return error;
+
+	strlen = response_get_string(&dev->parsed, 4, &msid_pin);
+	if (!msid_pin) {
+		pr_err("%s: Couldn't extract PIN from response\n", __func__);
+		return 11;

please add constant for your magic return values.

lib/sed-opal.c line 1865

Quoted 10 lines
lib/sed-opal.c new file +10 -0
+	}
+
+	dev->prev_data = kmemdup(msid_pin, strlen, GFP_KERNEL);
+	if (!dev->prev_data)
+		return -ENOMEM;
+
+	dev->prev_d_len = strlen;
+
+ err_return:
+	return 0;

I can't find anything actually jumping to this label. And if it did it could just return directly.

Quoted 7 lines
lib/sed-opal.c new file +7 -0
+
+static struct opal_dev *get_opal_dev(struct sed_context *sedc,
+				     const opal_step *funcs)
+{
+	struct opal_dev *dev = sedc->dev;
+	if (dev) {
+		dev->state = 0;

lib/sed-opal.c line 1991

Quoted 10 lines
lib/sed-opal.c new file +10 -0
+		dev->funcs = funcs;
+		dev->TSN = 0;
+		dev->HSN = 0;
+		dev->error_cb = end_opal_session_error;
+		dev->error_cb_data = dev;
+		dev->func_data = NULL;
+		dev->sed_ctx = sedc;
+		opal_dev_get(dev);
+	}
+	return dev;

Who serialized access to sedv->dev?

lib/sed-opal.c line 2005

Quoted 10 lines
lib/sed-opal.c new file +10 -0
+	struct opal_dev *dev;
+	void *data[3] = { NULL };
+	const opal_step erase_funcs[] = {
+		opal_discovery0,
+		start_auth_opal_session,
+		get_active_key,
+		gen_key,
+		end_opal_session,
+		NULL,
+	};

static?

Quoted 1 line

+EXPORT_SYMBOL(opal_enable_disable_shadow_mbr);

As far as I can tell nothing but alloc_opal_dev and opal_unlock_from_suspend is every called from another module, so all these exports could be dropped.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	struct opal_dev *dev;
+		const opal_step funcs[] = {

wrong indentation. also all these arrays of functions should be marked static.

lib/sed-opal.c line 2358

Quoted 2 lines
lib/sed-opal.c new file +2 -0
+	if (!list_empty(&dev->unlk_lst)) {
+		list_for_each_entry(suspend, &dev->unlk_lst, node) {

No need for the list_empty check.

lib/sed-opal_internal.h

Quoted 2 lines
lib/sed-opal_internal.h new file +0 -0

This pretty much seem to contain the OPAL protocol defintions, so why not opal_proto.h?

lib/sed-opal_internal.h line 19

Quoted 2 lines
lib/sed-opal_internal.h new file +2 -0
+#ifndef _NVME_OPAL_INTERNAL_H
+#define _NVME_OPAL_INTERNAL_H

And this doesn't seem to match the file name.

lib/sed-opal_internal.h line 22

Quoted 2 lines
lib/sed-opal_internal.h new file +2 -0
+#include <linux/key-type.h>
+#include <keys/user-type.h>

These don't seem to be needed.

lib/sed-opal_internal.h line 34

Quoted 6 lines
lib/sed-opal_internal.h new file +6 -0
+/*
+ * Derived from:
+ * TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
+ * Section: 5.1.5 Method Status Codes
+ */
+static const char *opal_errors[] = {

static consta char * const opal_errors[] = {

also please move this into a .c file so that it's not duplicated in every file that includes the header.

lib/sed-opal_internal.h line 65

Quoted 10 lines
lib/sed-opal_internal.h new file +10 -0
+static const char *opal_error_to_human(int error)
+{
+	if (error == 0x3f)
+		return "Failed";
+
+	if (error >= ARRAY_SIZE(opal_errors) || error < 0)
+		return "Unknown Error";
+
+	return opal_errors[error];
+}

Same for this one.

lib/sed-opal_internal.h line 72

Quoted 6 lines
lib/sed-opal_internal.h new file +6 -0
+/*
+ * User IDs used in the TCG storage SSCs
+ * Derived from: TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
+ * Section: 6.3 Assigned UIDs
+ */
+static const u8 OPALUID[][8] = {

And all the other following variable delcarations.

Also please use lower case names with underscores as separators for your variable and type names. Constants can remain in all caps.

lib/sed-opal_internal.h line 146

Quoted 3 lines
lib/sed-opal_internal.h new file +3 -0
+static const size_t OPAL_UID_LENGTH = 8;
+static const size_t OPAL_MSID_KEYLEN = 15;
+static const size_t OPAL_UID_LENGTH_HALF = 4;

Use a #define or enum fo these constants.

lib/sed-opal_internal.h line 599

Quoted 2 lines
lib/sed-opal_internal.h new file +2 -0
+struct key *request_user_key(const char *master_desc, const u8 **master_key,
+			     size_t *master_keylen);

This one doesn't actually seem to be used.

lib/sed-opal_internal.h

Quoted 8 lines
lib/sed-opal_internal.h new file +8 -0
+int sed_save(struct sed_context *sed_ctx, struct sed_key *key)
+{
+	switch (key->sed_type) {
+	case OPAL_LOCK_UNLOCK:
+		return opal_save(sed_ctx, key);
+	}
+
+	return -EOPNOTSUPP;

It seems to me that we should skip this whole sed_type indirections and just specify the ioctls directly for OPAL (which would include opalite and pyrite as subsets). The only other protocols of interest for Linux would be the ATA "security" plain text passwords, which can be handled differently, or enterprise SSC which we can hopefully avoid to implement entirely.

lib/sed-opal_internal.h

Quoted 12 lines
lib/sed-opal_internal.h new file +12 -0
+
+int fdev_sed_ioctl(struct file *filep, unsigned int cmd,
+		   unsigned long arg)
+{
+	struct sed_key key;
+	struct sed_context *sed_ctx;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EACCES;
+
+	if (!filep->f_sedctx || !filep->f_sedctx->ops || !filep->f_sedctx->dev)
+		return -ENODEV;

In the previous version this was called from the block driver which could pass in the context (and ops). Why was this changed?

#12 lore raw

Re: [PATCH v3 2/5] lib: Add Sed-opal library

Quoted 7 lines
Quoted 7 lines
lib/sed.c new file +7 -0
+
+int fdev_sed_ioctl(struct file *filep, unsigned int cmd,
+		   unsigned long arg)
+{
+	struct sed_key key;
+	struct sed_context *sed_ctx;
+
Quoted 9 lines

lib/sed.c line 162

Quoted 5 lines
lib/sed.c new file +5 -0
+	if (!capable(CAP_SYS_ADMIN))
+		return -EACCES;
+
+	if (!filep->f_sedctx || !filep->f_sedctx->ops || !filep->f_sedctx->dev)
+		return -ENODEV;

In the previous version this was called from the block driver which could pass in the context (and ops). Why was this changed?

Do you mean from nvme_ioctl? or from blk_ioctl? It was removed from blk_ioctl because I must have misinterpreted your comments here:
http://lists.infradead.org/pipermail/linux-nvme/2016-December/007364.html

Copied from the link:

Quoted 8 lines
Quoted 7 lines

and directly use
block_device. Then if we add the security send/receive operations to the block_device_operations, that will simplify chaining the security request to the driver without needing to thread the driver's requested callback and data the way you have to here since all the necessary information is encapsulated in the block_device.

Quoted 8 lines

Maybe. I need to look at the TCG spec again (oh my good, what a fucking mess), but if I remember the context if it is the whole nvme controller and not just a namespace, so a block_device might be the wrong context. Then again we can always go from the block_device to the controller fairly easily. So instead of adding the security operation to the block_device_operations which we don't really need for now maybe we should add a security_conext to the block device so that we can avoid all the lookup code?

I took your hesitation about the block_device to mean try something new, that combined with my concern about having namespaces have the ability to lock the global range which can span ourside their LBA ranges lead me to remove it from block and put it in the char dev world.

On the same note there is a public review of
TCG Storage Opal SSC Feature Set: Configurable Namespace Locking:
Which Jon Derrick found for us:
https://www.trustedcomputinggroup.org/wp-content/uploads/TCG_Storage_Feature_Set_Namespaces_phase_1b_v1_00_r1_19_public-review.pdf

Where they are thinking about doing a complete 180 from:
https://www.trustedcomputinggroup.org/wp-content/uploads/TCG_SWG_SIIS_Version_1_05_Revision_1_00.pdf

And now Namespaces can have their own global locking range as well as locking objects within them.

#13 lore raw

Re: [PATCH v3 2/5] lib: Add Sed-opal library

Quoted 3 lines · Scott Bauer

Do you mean from nvme_ioctl? or from blk_ioctl? It was removed from blk_ioctl because I must have misinterpreted your comments here:
http://lists.infradead.org/pipermail/linux-nvme/2016-December/007364.html

nvme_ioctl, aka the ioctl method of the block driver. In the future I expect another caller in scsi_ioctl or sd_ioctl.

Quoted 4 lines

I took your hesitation about the block_device to mean try something new, that combined with my concern about having namespaces have the ability to lock the global range which can span ourside their LBA ranges lead me to remove it from block and put it in the char dev world.

In the link I literally meant the struct block_device, not nessecarily the block device node.

Quoted 11 lines

On the same note there is a public review of
TCG Storage Opal SSC Feature Set: Configurable Namespace Locking:
Which Jon Derrick found for us:
https://www.trustedcomputinggroup.org/wp-content/uploads/TCG_Storage_Feature_Set_Namespaces_phase_1b_v1_00_r1_19_public-review.pdf

Where they are thinking about doing a complete 180 from:
https://www.trustedcomputinggroup.org/wp-content/uploads/TCG_SWG_SIIS_Version_1_05_Revision_1_00.pdf

And now Namespaces can have their own global locking range as well as locking objects within them.

Interesting. From a quick look this is how it should have been done from the start. Now the big question is when devices are going to implement it, and how we can support both from the same driver, as this means we'll have to deal with two different ways to allocate the security context based on the device. And do the horrible mess that the TCG SIISs are there is absolute no way to discover this at the NVMe level, but we actually need to start doing TCG method calls to even figure out what's going on.

#14 lore raw

Re: [PATCH v3 2/5] lib: Add Sed-opal library

In reply to Christoph Hellwig

On Mon, Dec 19, 2016@11:28:47PM -0800, Christoph Hellwig wrote:
[snip]

Quoted 12 lines

lib/sed-opal.c line 772

Quoted 9 lines
lib/sed-opal.c new file +9 -0
+	while (cpos < total) {
+		if (!(pos[0] & 0x80)) /* tiny atom */
+			token_length = response_parse_tiny(iter, pos);
+		else if (!(pos[0] & 0x40)) /* short atom */
+			token_length = response_parse_short(iter, pos);
+		else if (!(pos[0] & 0x20)) /* medium atom */
+			token_length = response_parse_medium(iter, pos);
+		else if (!(pos[0] & 0x10)) /* long atom */
+			token_length = response_parse_long(iter, pos);

Please add symbolic names for these constants to the protocol header.

I get tripped up by this logic every time I look at it. I'd almost rather see something like the following, which more closely follows the TCG core spec and the optimizing compiler should turn it into something similar to above anyways:

	if (pos[0] <= 0x7F)
		token_length = response_parse_tiny..
	else if (pos[0] <= 0xBF)
		token_length = response_parse_short..
	else if (pos[0] <= 0xDF)
		token_length = response_parse_medium..
	else if (pos[0] <= 0xE3)
		token_length = response_parse_long..

Then just add those 0x7F, 0xBF, 0xDF, and 0xE3 constants to proto header. We could even add in a TCG reserved error for 0xE4-0xEF

Also instead of tracking cpos, we could subtract from total until negative (if you make it signed). It's similar to how we do length in nvme_setup_prps.

[snip]

Quoted 5 lines

lib/sed-opal_internal.h

Quoted 2 lines
lib/sed-opal_internal.h new file +0 -0

This pretty much seem to contain the OPAL protocol defintions, so why not opal_proto.h?

Since there might eventually be a whole class of opal-like sed protocols, why does it make more sense to have opal_proto.h instead of sed-opal.h or some variation? This is similar to how leds-*.h look to me. Although I agree that sed-ATA.h would be dishonest since ATA security doesn't imply a self-encrypting-disk.

[snip]

Quoted 7 lines
Quoted 7 lines
lib/sed-opal_internal.h new file +7 -0
+int sed_save(struct sed_context *sed_ctx, struct sed_key *key)
+{
+	switch (key->sed_type) {
+	case OPAL_LOCK_UNLOCK:
+		return opal_save(sed_ctx, key);
+	}
+
Quoted 8 lines

lib/sed-opal_internal.h

Quoted 1 line
lib/sed-opal_internal.h new file +1 -0
+	return -EOPNOTSUPP;

It seems to me that we should skip this whole sed_type indirections and just specify the ioctls directly for OPAL (which would include opalite and pyrite as subsets). The only other protocols of interest for Linux would be the ATA "security" plain text passwords, which can be handled differently, or enterprise SSC which we can hopefully avoid to implement entirely.

I'm on board with this if you think we won't have enough different, but similar, SED protocols to justify the indirection. In that case you can ignore the above comment as well.

Quoted 17 lines

lib/sed-opal_internal.h

Quoted 13 lines
lib/sed-opal_internal.h new file +12 -0
+
+int fdev_sed_ioctl(struct file *filep, unsigned int cmd,
+		   unsigned long arg)
+{
+	struct sed_key key;
+	struct sed_context *sed_ctx;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EACCES;
+
+	if (!filep->f_sedctx || !filep->f_sedctx->ops || !filep->f_sedctx->dev)
+		return -ENODEV;

In the previous version this was called from the block driver which could pass in the context (and ops). Why was this changed?

#15 lore raw

Re: [PATCH v3 2/5] lib: Add Sed-opal library

Quoted 7 lines · Jon Derrick
Quoted 2 lines

This pretty much seem to contain the OPAL protocol defintions, so why not opal_proto.h?

Since there might eventually be a whole class of opal-like sed protocols, why does it make more sense to have opal_proto.h instead of sed-opal.h or some variation? This is similar to how leds-*.h look to me. Although I agree that sed-ATA.h would be dishonest since ATA security doesn't imply a self-encrypting-disk.

As far as I can tell the NVMe / SCSI / ATA security landscape looks like:

- ATA security - specified in ATA, kinda implicitly referenced by SPC and then even more implitly in NVMe. Actually implemented in various NVMe consumer devices because OEMs insist on it. Basically just a trivial plain text password lock/unlock.

- TCG OPAL including the subsets OPALite and Pyrite, whereas the latter is just the above lock/unlock in a TCG way.

- TCG Enterprise SSC

I think it's pretty obvious that ATA security should be something on it's own. OPALite and Pyrite are strict subsets of OPAL, so having them in something named opal shouldn't be surprising. The big question is what to do about Enterprise SSC. Which has some overlaps with OPAL but also major differences, so keeping it separate if we ever have to implement it (I hope we don't) would be best.

Quoted 3 lines

I'm on board with this if you think we won't have enough different, but similar, SED protocols to justify the indirection. In that case you can ignore the above comment as well.

This goes back to the above. The only thing that is not a strict subset of OPAL but kinda sorta similar is TCG Enterprise SSC, but my preference would be to ignore it, and the second best preference would be to keep it separate.

#16 lore raw

PATCH v3 3/5 fs: Wire up SED/Opal to ioctl

In reply to Scott Bauer

Adds a new sed_context pointer to file struct, for char devs who wish to suppor SED.
Adds ioctl handling code.

Signed-off-by Scott Bauer <scott.bauer at intel.com>
Signed-off-by Rafael Antognolli <Rafael.Antognolli at intel.com>
---
 fs/ioctl.c         | 3 +++
 include/linux/fs.h | 2 ++
 2 files changed, 5 insertions(+)
fs/ioctl.c +3 -0
index cb9b029..77def42 100644
@@ -15,6 +15,7 @@
 #include <linux/writeback.h>
 #include <linux/buffer_head.h>
 #include <linux/falloc.h>
+#include <linux/sed.h>
 #include "internal.h"
 
 #include <asm/ioctls.h>
@@ -679,6 +680,8 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
 	default:
 		if (S_ISREG(inode->i_mode))
 			error = file_ioctl(filp, cmd, arg);
+		else if (is_sed_ioctl(cmd))
+			error = fdev_sed_ioctl(filp, cmd, arg);
 		else
 			error = vfs_ioctl(filp, cmd, arg);
 		break;
include/linux/fs.h +2 -0
index 2ba0743..75a99e5 100644
@@ -55,6 +55,7 @@ struct workqueue_struct;
 struct iov_iter;
 struct fscrypt_info;
 struct fscrypt_operations;
+struct sed_context;
 
 extern void __init inode_init(void);
 extern void __init inode_init_early(void);
@@ -853,6 +854,7 @@ struct file {
 #ifdef CONFIG_SECURITY
 	void			*f_security;
 #endif
+	struct sed_context      *f_sedctx;
 	/* needed for tty driver, and maybe others */
 	void			*private_data;
 
Signature · 1 line
2.7.4
#17 lore raw

Re: [PATCH v3 3/5] fs: Wire up SED/Opal to ioctl

include/linux/fs.h struct file { · line 857

Quoted 5 lines
include/linux/fs.h +1 -0
@@ -853,6 +854,7 @@ struct file {
 #ifdef CONFIG_SECURITY
 	void			*f_security;
 #endif
+	struct sed_context      *f_sedctx;

Adding a new field to the global struct file for a block driver feature is not acceptable. And I don't really see why it would be nessecary anyway.

#18 lore raw

PATCH v3 4/5 nvme: Implement resume_from_suspend and SED Allocation code.

In reply to Scott Bauer

This patch implements the necessary logic to unlock a SED enabled device coming back from an S3.

The patch also implements the necessary logic to allocate the appropriate opal_dev structures to support the OPAL protocol.

Signed-off-by Scott Bauer <scott.bauer at intel.com>
Signed-off-by Rafael Antognolli <Rafael.Antognolli at intel.com>
---
 drivers/nvme/host/core.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/nvme/host/nvme.h |  8 +++++-
 drivers/nvme/host/pci.c  | 10 +++++++-
 3 files changed, 83 insertions(+), 2 deletions(-)
drivers/nvme/host/core.c +67 -0
index b40cfb0..f9731ce 100644
@@ -28,6 +28,8 @@
 #include <linux/t10-pi.h>
 #include <scsi/sg.h>
 #include <asm/unaligned.h>
+#include <linux/sed.h>
+#include <linux/sed-opal.h>
 
 #include "nvme.h"
 #include "fabrics.h"
@@ -762,6 +764,48 @@ static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
 	return status;
 }
 
+static int nvme_sec_submit(struct nvme_ctrl *ctrl, u16 spsp, u8 secp,
+			   void *buffer, size_t len, u8 opcode)
+{
+	struct nvme_command cmd = { 0 };
+	struct nvme_ns *ns = NULL;
+
+	mutex_lock(&ctrl->namespaces_mutex);
+	if (!list_empty(&ctrl->namespaces))
+		ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
+
+	mutex_unlock(&ctrl->namespaces_mutex);
+	if (!ns)
+		return -ENODEV;
+
+	cmd.common.opcode = opcode;
+	cmd.common.nsid = ns->ns_id;
+	cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
+	cmd.common.cdw10[1] = cpu_to_le32(len);
+
+	return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
+				      ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
+}
+
+static int nvme_sec_send(void *ctrl_data, u16 spsp, u8 secp,
+			 void *buf, size_t len)
+{
+	return nvme_sec_submit(ctrl_data, spsp, secp, buf, len,
+			       nvme_admin_security_send);
+}
+
+static int nvme_sec_recv(void *ctrl_data, u16 spsp, u8 secp,
+			 void *buf, size_t len)
+{
+	return nvme_sec_submit(ctrl_data, spsp, secp, buf, len,
+			       nvme_admin_security_recv);
+}
+
+static const struct sec_ops nvme_sec_ops = {
+	.sec_send = nvme_sec_send,
+	.sec_recv = nvme_sec_recv,
+};
+
 static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
 		unsigned int cmd, unsigned long arg)
 {
@@ -1051,6 +1095,28 @@ static const struct pr_ops nvme_pr_ops = {
 	.pr_clear	= nvme_pr_clear,
 };
 
+int nvme_opal_initialize(struct nvme_ctrl *ctrl)
+{
+	/* Opal dev has already been allocated for this controller */
+	if (ctrl->sed_ctx.dev)
+		return 0;
+
+	ctrl->sed_ctx.dev = alloc_opal_dev(ctrl->admin_q);
+	if (!ctrl->sed_ctx.dev)
+		return -ENOMEM;
+	ctrl->sed_ctx.ops = &nvme_sec_ops;
+	ctrl->sed_ctx.sec_data = ctrl;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nvme_opal_initialize);
+
+void nvme_unlock_from_suspend(struct nvme_ctrl *ctrl)
+{
+	if (opal_unlock_from_suspend(&ctrl->sed_ctx))
+		pr_warn("Failed to unlock one or more locking ranges!\n");
+}
+EXPORT_SYMBOL_GPL(nvme_unlock_from_suspend);
+
 static const struct block_device_operations nvme_fops = {
 	.owner		= THIS_MODULE,
 	.ioctl		= nvme_ioctl,
@@ -1312,6 +1378,7 @@ static int nvme_dev_open(struct inode *inode, struct file *file)
 		if (!kref_get_unless_zero(&ctrl->kref))
 			break;
 		file->private_data = ctrl;
+		file->f_sedctx = &ctrl->sed_ctx;
 		ret = 0;
 		break;
 	}
drivers/nvme/host/nvme.h +7 -1
index bd53214..851830b 100644
@@ -19,6 +19,7 @@
 #include <linux/kref.h>
 #include <linux/blk-mq.h>
 #include <linux/lightnvm.h>
+#include <linux/sed.h>
 
 enum {
 	/*
@@ -151,6 +152,8 @@ struct nvme_ctrl {
 	struct work_struct async_event_work;
 	struct delayed_work ka_work;
 
+	struct sed_context sed_ctx;
+
 	/* Fabrics only */
 	u16 sqsize;
 	u32 ioccsz;
@@ -256,7 +259,8 @@ static inline int nvme_error_status(u16 status)
 
 static inline bool nvme_req_needs_retry(struct request *req, u16 status)
 {
-	return !(status & NVME_SC_DNR || blk_noretry_request(req)) &&
+	return !(status & NVME_SC_DNR || status & NVME_SC_ACCESS_DENIED ||
+		 blk_noretry_request(req)) &&
 		(jiffies - req->start_time) < req->timeout &&
 		req->retries < nvme_max_retries;
 }
@@ -275,6 +279,8 @@ int nvme_init_identify(struct nvme_ctrl *ctrl);
 
 void nvme_queue_scan(struct nvme_ctrl *ctrl);
 void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
+void nvme_unlock_from_suspend(struct nvme_ctrl *ctrl);
+int nvme_opal_initialize(struct nvme_ctrl *ctrl);
 
 #define NVME_NR_AERS	1
 void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
drivers/nvme/host/pci.c +9 -1
index 2fd7dc2..d298c15 100644
@@ -43,6 +43,7 @@
 #include <linux/types.h>
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <asm/unaligned.h>
+#include <linux/sed-opal.h>
 
 #include "nvme.h"
 
@@ -1765,7 +1766,7 @@ static void nvme_reset_work(struct work_struct *work)
 {
 	struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
 	int result = -ENODEV;
-
+	bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
 	if (WARN_ON(dev->ctrl.state == NVME_CTRL_RESETTING))
 		goto out;
 
@@ -1796,6 +1797,13 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
+	result = nvme_opal_initialize(&dev->ctrl);
+	if (result)
+		goto out;
+
+	if (was_suspend)
+		nvme_unlock_from_suspend(&dev->ctrl);
+
 	result = nvme_setup_io_queues(dev);
 	if (result)
 		goto out;
Signature · 1 line
2.7.4
#19 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

Quoted 6 lines · Scott Bauer
drivers/nvme/host/core.c +6 -0
+static int nvme_sec_submit(struct nvme_ctrl *ctrl, u16 spsp, u8 secp,
+			   void *buffer, size_t len, u8 opcode)
+{
+	struct nvme_command cmd = { 0 };
+	struct nvme_ns *ns = NULL;
+

drivers/nvme/host/core.c static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns, · line 782

Quoted 10 lines
drivers/nvme/host/core.c +10 -0
+	mutex_lock(&ctrl->namespaces_mutex);
+	if (!list_empty(&ctrl->namespaces))
+		ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
+
+	mutex_unlock(&ctrl->namespaces_mutex);
+	if (!ns)
+		return -ENODEV;
+
+	cmd.common.opcode = opcode;
+	cmd.common.nsid = ns->ns_id;

Should be:

	cmd.common.nsid = cpu_to_le32(ns->ns_id);

But now wondering how you can send a security command to different namespaces. That's why I thought it'd make more sense to threa this through block_device, but maybe Christoph had some idea on how to get the same functionality without that?

#20 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

Quoted 20 lines · Keith Busch
Quoted 6 lines · Scott Bauer
drivers/nvme/host/core.c +6 -0
+static int nvme_sec_submit(struct nvme_ctrl *ctrl, u16 spsp, u8 secp,
+			   void *buffer, size_t len, u8 opcode)
+{
+	struct nvme_command cmd = { 0 };
+	struct nvme_ns *ns = NULL;
+

drivers/nvme/host/core.c static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns, · line 782

Quoted 10 lines
drivers/nvme/host/core.c +10 -0
+	mutex_lock(&ctrl->namespaces_mutex);
+	if (!list_empty(&ctrl->namespaces))
+		ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
+
+	mutex_unlock(&ctrl->namespaces_mutex);
+	if (!ns)
+		return -ENODEV;
+
+	cmd.common.opcode = opcode;
+	cmd.common.nsid = ns->ns_id;

Should be:

Quoted 6 lines
	cmd.common.nsid = cpu_to_le32(ns->ns_id);

But now wondering how you can send a security command to different namespaces. That's why I thought it'd make more sense to threa this through block_device, but maybe Christoph had some idea on how to get the same functionality without that?

I went back and reviewed the spec 1.2.1:

http://www.nvmexpress.org/wp-content/uploads/NVM_Express_1_2_1_Gold_20160603.pdf
Section 5.18 (page 140->141)

Describes the security send command type and it doesn't have any reference of a namespace ID. Anecdotally, I just removed the ns->ns_id line from the code and everything still works as intended. Is there another portion of the spec or errata that requires ns_id? (I can't access 1.2.1 errta the link doesn't work).

#21 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

Quoted 9 lines · Scott Bauer

I went back and reviewed the spec 1.2.1:

http://www.nvmexpress.org/wp-content/uploads/NVM_Express_1_2_1_Gold_20160603.pdf
Section 5.18 (page 140->141)

Describes the security send command type and it doesn't have any reference of a namespace ID. Anecdotally, I just removed the ns->ns_id line from the code and everything still works as intended. Is there another portion of the spec or errata that requires ns_id? (I can't access 1.2.1 errta the link doesn't work).

As far as I can tell Security Send / Receive has always been intended to apply to the whole controller, even if that's something I would not personally think is a good idea.

#22 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

Quoted 3 lines · Christoph Hellwig

As far as I can tell Security Send / Receive has always been intended to apply to the whole controller, even if that's something I would not personally think is a good idea.

NVMe security commands required the namespace ID since the very beginning. It's currently documented in figure 42 of section 5, "Namespace Identifier Used" column.

#23 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

Quoted 8 lines · Keith Busch
Quoted 3 lines · Christoph Hellwig

As far as I can tell Security Send / Receive has always been intended to apply to the whole controller, even if that's something I would not personally think is a good idea.

NVMe security commands required the namespace ID since the very beginning. It's currently documented in figure 42 of section 5, "Namespace Identifier Used" column.

Oh, for some reason I read a no there when looking it up. Good to know, although TCG spec still seem to ignore it.

#24 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Keith Busch
Quoted 3 lines · Christoph Hellwig

As far as I can tell Security Send / Receive has always been intended to apply to the whole controller, even if that's something I would not personally think is a good idea.

Quoted 6 lines
Quoted 3 lines

NVMe security commands required the namespace ID since the very beginning. It's currently documented in figure 42 of section 5, "Namespace Identifier Used" column.

Oh, for some reason I read a no there when looking it up. Good to know, although TCG spec still seem to ignore it.

Thanks Keith. Although TCG Spec currently ignores it in the future it may not. In that case we should probably attempt to future proof it a bit. Since the namespace ID is accessible via the block device structure I'll find a way to include that in some opaque pointer that we can deliver through the core into NVMe.

But this also brings up another question (and part of the reason I moved from the block ioctl to fs ioctl): For drives with multiple namsepaces is it acceptable to allow a namespace, who has a segregated chunk of space, the ability to perform actions outside of its range? Since the multiple namespaces portion of the spec says there will be one Global LR a namespace that doesn't encompass the entire LBA range can end up locking other LBAs via locking the global.

That's why I wanted to go to char dev because it seemed like a better fit for this scenario. Any thoughts on the above?

Quoted 6 lines

_______________________________________________
Linux-nvme mailing list
Linux-nvme at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

#25 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

Quoted 5 lines · Scott Bauer

Thanks Keith. Although TCG Spec currently ignores it in the future it may not. In that case we should probably attempt to future proof it a bit. Since the namespace ID is accessible via the block device structure I'll find a way to include that in some opaque pointer that we can deliver through the core into NVMe.

Honestly I think this nsid issue is a major, major mess. Without a nsid only the global range makes sense, and in that case it does not make sense to set a nsid (so it should be 0xFFFFFFFF). This one more of the major major issues because the NVMe group decided to not care about security and outsourced it to that giant heap of crap called TCG.

I think we'll need to start a little conversation on the nvme list on how to handle this, including potential errata.

Quoted 9 lines

But this also brings up another question (and part of the reason I moved from the block ioctl to fs ioctl): For drives with multiple namsepaces is it acceptable to allow a namespace, who has a segregated chunk of space, the ability to perform actions outside of its range? Since the multiple namespaces portion of the spec says there will be one Global LR a namespace that doesn't encompass the entire LBA range can end up locking other LBAs via locking the global.

That's why I wanted to go to char dev because it seemed like a better fit for this scenario. Any thoughts on the above?

In SCSI we allow all kinds of ioctls affecting target-wide behavior on the device nodes. These needs to be protected using CAP_SYS_ADMIN, otherwise we're going to get security issues, though (and in SCSI land we had such issues before).

#26 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Keith Busch
Quoted 3 lines · Christoph Hellwig

As far as I can tell Security Send / Receive has always been intended to apply to the whole controller, even if that's something I would not personally think is a good idea.

Quoted 6 lines
Quoted 3 lines

NVMe security commands required the namespace ID since the very beginning. It's currently documented in figure 42 of section 5, "Namespace Identifier Used" column.

Oh, for some reason I read a no there when looking it up. Good to know, although TCG spec still seem to ignore it.

Before I submit another version I want to address a few design issues we seem to be walking around a bit:

The other reviews you gave for the series are fine and will be implemented, thank you for that.

The main development issue seems to be how the drivers/block layer interact with the core sed.

1) We will move the core from lib/ back to block/ and add CONFIGS in kconfig.

2) Do we want to continue passing around a sed_context to the core? Instead of a block_device struct like we did in previous versions.

2a) If we do wish to do wish to continue passng sed_contexts to the core I have to add a new variable to the block_device structure for our sed_context. Will this be acceptable? It wasn't acceptable for the file struct. The reason I need a new variable in the struct is:
On the ioctl path, if we intercept the SED call in the block layer ioctl and have the call chain be:
uland -> blk_ioctl -> sed_ioctl() -> sedcore -> sec_send/recv -> nvme then I need to be able to pass a sed_ctx struct in blk_ioctl to sed-ioctl and the only way is to have it sitting in our block_device structure.

The other way which was sorta nack'd last time is the following call chain:
uland -> blk_ioctl -> nvme_ioctl -> sed_ioctl -> sedcore -> send/rcv -> nvme

In this call chain in nvme_ioctl we have access to our block device struct and from there we can do blkdev->bd_disk->private_data to get our ns and then eventually our sed_ctx to pass to sed_ioctl. I could add the ns to the sec_data pointer in sed_context. This would give us access to ns without having to pass around a block device or store it anywhere.

In the first scenario I can't work at all with opaque pointers like we can in the drivers itself (private_data). I don't know what they are, the drivers have the domain knowledge of what type they actually stored in private_data. That's why I need an explicit member in the block_device for the first scenario.

3) For NVMe we need access to our ns ID. It's in the block_device behind a few pointers. What I can do is if we want to continue with the first ioctl path described above is something like:

sed_ioctl(struct block_device *bdev, ...)
{
sed_context *ctx = bdev->sed_ctx;
ctx->sed_data = bdev->bd_disk->private_data;
switch(cmd) {
...
...
return some_opal_cmd(ctx);
}
}

While this works for NVMe I don't know if this is acceptible for *all* users. Since this is in a generic ioctl that is supposed to work with all drivers, who knows what the hell they're putting in private_data and whether its useful for their implementation of sec_send/recv.

I think that's all I have for now. If I think of anything throughout the day I'll reply to to this email.

#27 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

Quoted 2 lines · Scott Bauer

2) Do we want to continue passing around a sed_context to the core? Instead of a block_device struct like we did in previous versions.

My personal preference would be the block_device, but in this case my preference collides with the (sad) reality, and that reality is that TCG did a major, major fuckup in specifyiong the NVMe interaction in the SIIG and does not treat TPer as per-Namespaces, as it does in SCSI for LUNs. So in NVMe all the interactions are on a per-controller level, not per namespaces. And the block_device maps to the namespacespace in NVMe. So I fear we'll have to keep the sed_context.

Quoted 3 lines

2a) If we do wish to do wish to continue passng sed_contexts to the core I have to add a new variable to the block_device structure for our sed_context. Will this be acceptable?

We have a lot less block_device structures, and they are limited to block devices, so I think it should be acceptable.

Quoted 5 lines

It wasn't acceptable for the file struct. The reason
I need a new variable in the struct is:
On the ioctl path, if we intercept the SED call in the block layer ioctl and have the call chain be:
uland -> blk_ioctl -> sed_ioctl() -> sedcore -> sec_send/recv -> nvme

But I really don't think we strictly need it for this reason. The above callchain should be:

	userland -> blk_ioctl -> nvme_ioctl -> sed_opal_iocl

This allows passing the sec context including the submission method to sed_opal_ioctl from the driver and should not require anything in the block device.

Quoted 5 lines

then I need to be able to pass a sed_ctx struct in blk_ioctl to sed-ioctl and the only way is to have it sitting in our block_device structure.

The other way which was sorta nack'd last time is the following call chain:
uland -> blk_ioctl -> nvme_ioctl -> sed_ioctl -> sedcore -> send/rcv -> nvme

Why was this nacked? This is still my preference, except that it could still be simplified a bit per the other comments, e.g. I don't think we really need a sec_core.

Quoted 9 lines

In this call chain in nvme_ioctl we have access to our block device struct and from there we can do blkdev->bd_disk->private_data to get our ns and then eventually our sed_ctx to pass to sed_ioctl. I could add the ns to the sec_data pointer in sed_context. This would give us access to ns without having to pass around a block device or store it anywhere.

3) For NVMe we need access to our ns ID. It's in the block_device behind a few pointers. What I can do is if we want to continue with the first ioctl path described above is something like:

The think is the ns does not matter for the OPAL device. I suspect the right answer is to pass 0xffffffff as the nsid. I need to verify this with some devices I should have access to, and you should verity it with Intel. I've also kicked a mail to some people involved with TCG to see if we can get some agreement on this behavior. If we can't get agreement on that we'll just need to have a variable in the nvme_ctrl that stores some valid nsid just for security send/receive.

Quoted 1 line

While this works for NVMe I don't know if this is acceptible for *all* users.

The other users are SCSI, ATA and eMMC.

For SCSI the scsi_device is per-lun, and the SIIS specifies that all TPers are per-lun, so we'll simply have a context per scsi_device, otherwise it should work the same as NVMe. ATA doesn't support LUNs (except for ATAPI, which is SCSI over ATA), so it's even simpler. Additionally Linux hides ATA behind the SCSI layer, so the only thing we'll need to implement is the translation between the two. I don't really know enough about eMMC to comment on it.

#28 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

In reply to Scott Bauer
url: https://github.com/0day-ci/linux/commits/Scott-Bauer/include-Add-definitions-for-sed/20161220-110214 config: x86_64-randconfig-i0-201651 (attached as .config) compiler: gcc-4.9 (Debian 4.9.4-2)…

Hi Scott,

[auto build test WARNING on linus/master]
[also build test WARNING on v4.9 next-20161219]
[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/Scott-Bauer/include-Add-definitions-for-sed/20161220-110214
config: x86_64-randconfig-i0-201651 (attached as .config)
compiler: gcc-4.9 (Debian 4.9.4-2) 4.9.4 reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

drivers/nvme/host/core.c: In function 'nvme_sec_submit':

>> drivers/nvme/host/core.c:770:9: warning: missing braces around initializer [-Wmissing-braces]
     struct nvme_command cmd = { 0 };
            ^

drivers/nvme/host/core.c:770:9: warning: (near initialization for 'cmd.<anonymous>') [-Wmissing-braces] Cyclomatic Complexity 5 include/linux/compiler.h:__read_once_size
Cyclomatic Complexity 5 include/linux/compiler.h:__write_once_size
Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:__set_bit
Cyclomatic Complexity 2 arch/x86/include/asm/bitops.h:test_and_set_bit
Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:constant_test_bit
Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:variable_test_bit
Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:fls64
Cyclomatic Complexity 1 include/uapi/linux/byteorder/little_endian.h:__le64_to_cpup Cyclomatic Complexity 1 include/uapi/linux/byteorder/little_endian.h:__le16_to_cpup Cyclomatic Complexity 1 include/linux/log2.h:__ilog2_u64
Cyclomatic Complexity 1 include/linux/list.h:INIT_LIST_HEAD
Cyclomatic Complexity 1 include/linux/list.h:__list_del
Cyclomatic Complexity 1 include/linux/list.h:list_empty
Cyclomatic Complexity 1 arch/x86/include/asm/current.h:get_current
Cyclomatic Complexity 2 arch/x86/include/asm/page_64.h:__phys_addr_nodebug
Cyclomatic Complexity 1 include/asm-generic/getorder.h:__get_order
Cyclomatic Complexity 1 arch/x86/include/asm/atomic.h:atomic_read
Cyclomatic Complexity 1 arch/x86/include/asm/atomic.h:atomic_set
Cyclomatic Complexity 2 arch/x86/include/asm/atomic.h:atomic_sub_and_test
Cyclomatic Complexity 1 arch/x86/include/asm/atomic.h:atomic_add_return
Cyclomatic Complexity 1 arch/x86/include/asm/atomic.h:atomic_cmpxchg
Cyclomatic Complexity 5 arch/x86/include/asm/atomic.h:__atomic_add_unless
Cyclomatic Complexity 1 include/linux/atomic.h:atomic_add_unless
Cyclomatic Complexity 1 include/linux/err.h:PTR_ERR
Cyclomatic Complexity 1 include/linux/thread_info.h:check_object_size
Cyclomatic Complexity 1 include/linux/spinlock.h:spinlock_check
Cyclomatic Complexity 1 include/linux/spinlock.h:spin_lock
Cyclomatic Complexity 1 include/linux/spinlock.h:spin_lock_irq
Cyclomatic Complexity 1 include/linux/spinlock.h:spin_unlock
Cyclomatic Complexity 1 include/linux/spinlock.h:spin_unlock_irq
Cyclomatic Complexity 1 include/linux/jiffies.h:_msecs_to_jiffies
Cyclomatic Complexity 5 include/linux/jiffies.h:msecs_to_jiffies
Cyclomatic Complexity 1 include/linux/workqueue.h:to_delayed_work
Cyclomatic Complexity 1 include/linux/signal.h:sigismember
Cyclomatic Complexity 1 include/linux/sched.h:task_thread_info
Cyclomatic Complexity 1 include/linux/slab.h:__kmalloc_node
Cyclomatic Complexity 68 include/linux/slab.h:kmalloc_large
Cyclomatic Complexity 5 include/linux/slab.h:kmalloc
Cyclomatic Complexity 1 include/linux/slab.h:kmalloc_node
Cyclomatic Complexity 1 include/linux/slab.h:kzalloc
Cyclomatic Complexity 1 include/linux/slab.h:kzalloc_node
Cyclomatic Complexity 1 include/linux/kref.h:kref_init
Cyclomatic Complexity 1 include/linux/kref.h:kref_get_unless_zero
Cyclomatic Complexity 1 include/linux/device.h:dev_to_node
Cyclomatic Complexity 1 include/linux/device.h:dev_get_drvdata
Cyclomatic Complexity 1 include/linux/fs.h:iminor
Cyclomatic Complexity 1 include/linux/genhd.h:get_capacity
Cyclomatic Complexity 1 include/linux/genhd.h:set_capacity
Cyclomatic Complexity 1 include/linux/kasan-checks.h:kasan_check_read
Cyclomatic Complexity 1 include/linux/kasan-checks.h:kasan_check_write
Cyclomatic Complexity 11 arch/x86/include/asm/uaccess.h:copy_from_user
Cyclomatic Complexity 11 arch/x86/include/asm/uaccess.h:copy_to_user
Cyclomatic Complexity 1 include/linux/blk_types.h:op_is_write
Cyclomatic Complexity 1 include/linux/blkdev.h:queue_flag_set_unlocked
Cyclomatic Complexity 1 include/linux/blkdev.h:blk_rq_pos
Cyclomatic Complexity 1 include/linux/blkdev.h:blk_rq_bytes
Cyclomatic Complexity 1 include/linux/blkdev.h:blk_integrity_rq
Cyclomatic Complexity 1 include/linux/blkdev.h:blk_queue_max_integrity_segments Cyclomatic Complexity 1 include/linux/blk-mq.h:blk_mq_rq_to_pdu
Cyclomatic Complexity 1 include/linux/module.h:try_module_get
Cyclomatic Complexity 1 include/linux/module.h:module_put
Cyclomatic Complexity 1 include/linux/unaligned/access_ok.h:put_unaligned_le64 Cyclomatic Complexity 1 drivers/nvme/host/nvme.h:nvme_req
Cyclomatic Complexity 1 drivers/nvme/host/nvme.h:nvme_block_nr
Cyclomatic Complexity 1 drivers/nvme/host/nvme.h:nvme_nvm_register
Cyclomatic Complexity 1 drivers/nvme/host/nvme.h:nvme_nvm_unregister
Cyclomatic Complexity 1 drivers/nvme/host/nvme.h:nvme_nvm_register_sysfs
Cyclomatic Complexity 1 drivers/nvme/host/nvme.h:nvme_nvm_unregister_sysfs
Cyclomatic Complexity 1 drivers/nvme/host/nvme.h:nvme_nvm_ns_supported
Cyclomatic Complexity 1 drivers/nvme/host/nvme.h:nvme_get_ns_from_dev
Cyclomatic Complexity 1 drivers/nvme/host/core.c:nvme_getgeo
Cyclomatic Complexity 7 drivers/nvme/host/core.c:nvme_pr_type
Cyclomatic Complexity 1 drivers/nvme/host/core.c:nvme_sysfs_show_address
Cyclomatic Complexity 1 drivers/nvme/host/core.c:ns_cmp
Cyclomatic Complexity 2 drivers/nvme/host/core.c:nvme_async_event_work
Cyclomatic Complexity 2 include/linux/thread_info.h:test_ti_thread_flag
Cyclomatic Complexity 1 include/linux/sched.h:test_tsk_thread_flag
Cyclomatic Complexity 3 include/linux/blkdev.h:blk_get_integrity
Cyclomatic Complexity 3 drivers/nvme/host/core.c:nvme_sysfs_reset
Cyclomatic Complexity 21 drivers/nvme/host/core.c:nvme_dev_attrs_are_visible Cyclomatic Complexity 9 include/linux/blkdev.h:queue_logical_block_size
Cyclomatic Complexity 7 drivers/nvme/host/core.c:nvme_dev_open
Cyclomatic Complexity 3 drivers/nvme/host/nvme.h:nvme_reset_subsystem
Cyclomatic Complexity 3 include/linux/nvme.h:nvme_is_write
Cyclomatic Complexity 2 include/linux/err.h:IS_ERR
Cyclomatic Complexity 1 include/linux/sched.h:signal_pending
Cyclomatic Complexity 1 include/linux/sched.h:__fatal_signal_pending
Cyclomatic Complexity 3 include/linux/sched.h:fatal_signal_pending
Cyclomatic Complexity 1 drivers/nvme/host/core.c:nvme_setup_flush
Cyclomatic Complexity 3 drivers/nvme/host/core.c:nvme_setup_discard
Cyclomatic Complexity 13 drivers/nvme/host/core.c:nvme_setup_rw
Cyclomatic Complexity 4 arch/x86/include/asm/uaccess.h:copy_user_overflow
Cyclomatic Complexity 1 include/linux/workqueue.h:queue_delayed_work
Cyclomatic Complexity 1 include/linux/workqueue.h:schedule_delayed_work
Cyclomatic Complexity 3 drivers/nvme/host/core.c:nvme_keep_alive_end_io
Cyclomatic Complexity 12 drivers/nvme/host/core.c:nvme_wait_ready
Cyclomatic Complexity 7 drivers/nvme/host/core.c:nvme_set_queue_limits

vim +770 drivers/nvme/host/core.c

754 timeout = msecs_to_jiffies(cmd.timeout_ms);
755
756 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
757 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
758 &cmd.result, timeout);
759 if (status >= 0) {
760 if (put_user(cmd.result, &ucmd->result))
761 return -EFAULT;
762 }
763
764 return status;
765 }
766
767 static int nvme_sec_submit(struct nvme_ctrl *ctrl, u16 spsp, u8 secp,
768 void *buffer, size_t len, u8 opcode)
769 {

 > 770		struct nvme_command cmd = { 0 };

771 struct nvme_ns *ns = NULL;
772
773 mutex_lock(&ctrl->namespaces_mutex);
774 if (!list_empty(&ctrl->namespaces))
775 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
776
777 mutex_unlock(&ctrl->namespaces_mutex);
778 if (!ns)

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
-------------- next part --------------
A non-text attachment was scrubbed...
Name: .config.gz
Type: application/gzip
Size: 20893 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-nvme/attachments/20161220/b2ab85ac/attachment-0001.gz>

#29 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

In reply to Scott Bauer
Quoted 8 lines
drivers/nvme/host/core.c +8 -0
+static int nvme_sec_send(void *ctrl_data, u16 spsp, u8 secp,
+			 void *buf, size_t len)
+{
+	return nvme_sec_submit(ctrl_data, spsp, secp, buf, len,
+			       nvme_admin_security_send);
+}
+
+static int nvme_sec_recv(void *ctrl_data, u16 spsp, u8 secp,

drivers/nvme/host/core.c static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns, · line 807

Quoted 10 lines
drivers/nvme/host/core.c +10 -0
+			 void *buf, size_t len)
+{
+	return nvme_sec_submit(ctrl_data, spsp, secp, buf, len,
+			       nvme_admin_security_recv);
+}
+
+static const struct sec_ops nvme_sec_ops = {
+	.sec_send = nvme_sec_send,
+	.sec_recv = nvme_sec_recv,
+};

Just make sec_submit the callback passed to the core and avoid this boiler-plate code.

drivers/nvme/host/core.c static const struct pr_ops nvme_pr_ops = { · line 1108

Quoted 12 lines
drivers/nvme/host/core.c +11 -0
+int nvme_opal_initialize(struct nvme_ctrl *ctrl)
+{
+	/* Opal dev has already been allocated for this controller */
+	if (ctrl->sed_ctx.dev)
+		return 0;
+
+	ctrl->sed_ctx.dev = alloc_opal_dev(ctrl->admin_q);
+	if (!ctrl->sed_ctx.dev)
+		return -ENOMEM;
+	ctrl->sed_ctx.ops = &nvme_sec_ops;
+	ctrl->sed_ctx.sec_data = ctrl;

No need for sec_data callback, just pass the sed_ctx to the driver and use container_of to get at the containing structure.

#30 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

In reply to Scott Bauer

drivers/nvme/host/core.c static const struct pr_ops nvme_pr_ops = { · line 1118

Quoted 6 lines
drivers/nvme/host/core.c +6 -0
+void nvme_unlock_from_suspend(struct nvme_ctrl *ctrl)
+{
+	if (opal_unlock_from_suspend(&ctrl->sed_ctx))
+		pr_warn("Failed to unlock one or more locking ranges!\n");
+}
+EXPORT_SYMBOL_GPL(nvme_unlock_from_suspend);

I don't think we even need this wrapper. Also for the warning please use dev_warn so that the user knows which controller failed.

drivers/nvme/host/pci.c static void nvme_reset_work(struct work_struct *work) · line 1770

Quoted 7 lines
drivers/nvme/host/pci.c +1 -1
@@ -1765,7 +1766,7 @@ static void nvme_reset_work(struct work_struct *work)
 {
 	struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
 	int result = -ENODEV;
-
+	bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
 	if (WARN_ON(dev->ctrl.state == NVME_CTRL_RESETTING))

Please don't remove the empty line after the variable declarations. Also I would place your new line before the result line, as that makes it a tad easier to read.

#31 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

In reply to Scott Bauer

drivers/nvme/host/pci.c static void nvme_reset_work(struct work_struct *work) · line 1802

Quoted 7 lines · Scott Bauer
drivers/nvme/host/pci.c +3 -0
@@ -1796,6 +1797,13 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
+	result = nvme_opal_initialize(&dev->ctrl);
+	if (result)
+		goto out;

It seems you always try to intialize OPAL even if the drive doesn't support it. I think you should check if the device supports security commands and then see if it supports OPAL before calling this. See e.g.
https://lkml.org/lkml/2016/6/19/139 . Ideally, this code would check all supported protocols and initialize the appropriate security device based on that.

Jethro

#32 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

Quoted 5 lines · Jethro Beekman
Quoted 4 lines · Scott Bauer
drivers/nvme/host/pci.c +0 -0
@@ -1796,6 +1797,13 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
Quoted 9 lines

drivers/nvme/host/pci.c static void nvme_reset_work(struct work_struct *work) · line 1802

Quoted 3 lines
drivers/nvme/host/pci.c +3 -0
+	result = nvme_opal_initialize(&dev->ctrl);
+	if (result)
+		goto out;

It seems you always try to intialize OPAL even if the drive doesn't support it. I think you should check if the device supports security commands and then see if it supports OPAL before calling this. See e.g.
https://lkml.org/lkml/2016/6/19/139 . Ideally, this code would check all supported protocols and initialize the appropriate security device based on that.

The nvme_opal_initalize should probably be changed to nvme_opal_allocate or something. It's not really initalizing anything other than allocting the necessary structures for OPAL. In order to determine if the controller supports opal we need to allocate the previously mentioned structures anyway. I want to stay away from making payloads (specifically discovery0 ) payload in the nvme driver and allow the opal core to do a all the grunt work. In the future we'll probably have to refactor the core a bit to do just packet generation. It looks like at least for NVMe we're going to have to do a discovery to figure out whether we've got mutiple locking ranges per NS or just one global lr during initialization.

#33 lore raw

Re: [PATCH v3 4/5] nvme: Implement resume_from_suspend and SED Allocation code.

Quoted 7 lines

for OPAL. In order to determine if the controller supports opal we need to allocate the previously mentioned structures anyway. I want to stay away from making payloads (specifically discovery0 ) payload in the nvme driver and allow the opal core to do a all the grunt work. In the future we'll probably have to refactor the core a bit to do just packet generation. It looks like at least for NVMe we're going to have to do a discovery to figure out whether we've got mutiple locking ranges per NS or just one global lr during initialization.

We might have to do discovery in the nvme driver - there also is the ATA security feature supported by various consumer drives. Jethro has been doing some work in that direction, although driven by userspace for now.

#34 lore raw

PATCH v3 5/5 Maintainers: Add Information for SED Opal library

In reply to Scott Bauer
Signed-off-by Scott Bauer <scott.bauer at intel.com>
Signed-off-by Rafael Antognolli <Rafael.Antognolli at intel.com>
---
 MAINTAINERS | 10 ++++++++++
 1 file changed, 10 insertions(+)
MAINTAINERS +10 -0
index f6eb97b..76d542c 100644
@@ -11051,6 +11051,16 @@ L:	linux-mmc at vger.kernel.org
 S:	Maintained
 F:	drivers/mmc/host/sdhci-spear.c
 
+SECURE ENCRYPTING DEVICE (SED) OPAL DRIVER
+M:	Scott Bauer <scott.bauer at intel.com>
+M:	Jonathan Derrick <jonathan.derrick at intel.com>
+M:	Rafael Antognolli <rafael.antognolli at intel.com>
+L:	linux-nvme at lists.infradead.org
+S:	Supported
+F:	lib/sed*
+F:	include/linux/sed*
+F:	include/uapi/linux/sed*
+
 SECURITY SUBSYSTEM
 M:	James Morris <james.l.morris at oracle.com>
 M:	"Serge E. Hallyn" <serge at hallyn.com>
Signature · 1 line
2.7.4