History

nfit, libnvdimm: limited/whitelisted dimm command marshaling mechanism

There are currently 4 known similar but incompatible definitions of the command sets that can be sent to an NVDIMM through ACPI. It is also clear that future platform generations (ACPI or not) will continue to revise and extend the DIMM command set as new devices and use cases arrive. It is obviously untenable to continue to proliferate divergence of these command definitions, and to that end a standardization process has begun to provide for a unified specification. However, that leaves a problem about what to do with this first generation where vendors are already shipping divergence. The Linux kernel can support these initial diverged platforms without giving platform-firmware free reign to continue to diverge and compound kernel maintenance overhead. The kernel implementation can encourage standardization in two ways: 1/ Require that any function code that userspace wants to send be explicitly white-listed in the implementation. For ACPI this means function codes marked as supported by acpi_check_dsm() may only be invoked if they appear in the white-list. A function must be publicly documented before it is added to the white-list. 2/ The above restrictions can be trivially bypassed by using the "vendor-specific" payload command. However, since vendor-specific commands are by definition not publicly documented and have the potential to corrupt the kernel's view of the dimm state, we provide a toggle to disable vendor-specific operations. Enabling undefined behavior is a policy decision that can be made by the platform owner and encourages firmware implementations to choose public over private command implementations. Based on an initial patch from Jerry Hoemann Cc: Jerry Hoemann <jerry.hoemann@hpe.com> Cc: Christoph Hellwig <hch@infradead.org> Signed-off-by: Dan Williams <dan.j.williams@intel.com>

Dan Williams committed 10 years ago

Commit 31eca761 parent e3654ec4 files · +179 −17Browse files

Unified · Split

4 files
  1. drivers/acpi/nfit.c +85 −16
  2. drivers/acpi/nfit.h +13 −1
  3. drivers/nvdimm/bus.c +39 −0
  4. include/uapi/linux/ndctl.h +42 −0
drivers/acpi/nfit.c+85−16
@@ -171,33 +171,46 @@ static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, · 170 unmodified lines
171171 unsigned int buf_len, int *cmd_rc)
172172 {
173173 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
174− const struct nd_cmd_desc *desc = NULL;
175174 union acpi_object in_obj, in_buf, *out_obj;
175+ const struct nd_cmd_desc *desc = NULL;
176176 struct device *dev = acpi_desc->dev;
177+ struct nd_cmd_pkg *call_pkg = NULL;
177178 const char *cmd_name, *dimm_name;
178− unsigned long cmd_mask;
179+ unsigned long cmd_mask, dsm_mask;
179180 acpi_handle handle;
181+ unsigned int func;
180182 const u8 *uuid;
181183 u32 offset;
182184 int rc, i;
183185
186+ func = cmd;
187+ if (cmd == ND_CMD_CALL) {
188+ call_pkg = buf;
189+ func = call_pkg->nd_command;
190+ }
191+
184192 if (nvdimm) {
185193 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
186194 struct acpi_device *adev = nfit_mem->adev;
187195
188196 if (!adev)
189197 return -ENOTTY;
198+ if (call_pkg && nfit_mem->family != call_pkg->nd_family)
199+ return -ENOTTY;
200+
190201 dimm_name = nvdimm_name(nvdimm);
191202 cmd_name = nvdimm_cmd_name(cmd);
192203 cmd_mask = nvdimm_cmd_mask(nvdimm);
204+ dsm_mask = nfit_mem->dsm_mask;
193205 desc = nd_cmd_dimm_desc(cmd);
194− uuid = to_nfit_uuid(NFIT_DEV_DIMM);
206+ uuid = to_nfit_uuid(nfit_mem->family);
195207 handle = adev->handle;
196208 } else {
197209 struct acpi_device *adev = to_acpi_dev(acpi_desc);
198210
199211 cmd_name = nvdimm_bus_cmd_name(cmd);
200212 cmd_mask = nd_desc->cmd_mask;
213+ dsm_mask = cmd_mask;
201214 desc = nd_cmd_bus_desc(cmd);
202215 uuid = to_nfit_uuid(NFIT_DEV_BUS);
203216 handle = adev->handle;
@@ -207,7 +220,7 @@ static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, · 3 unmodified lines
207220 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
208221 return -ENOTTY;
209222
210− if (!test_bit(cmd, &cmd_mask))
223+ if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
211224 return -ENOTTY;
212225
213226 in_obj.type = ACPI_TYPE_PACKAGE;
@@ -222,21 +235,44 @@ static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, · 8 unmodified lines
222235 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
223236 i, buf);
224237
238+ if (call_pkg) {
239+ /* skip over package wrapper */
240+ in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
241+ in_buf.buffer.length = call_pkg->nd_size_in;
242+ }
243+
225244 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
226− dev_dbg(dev, "%s:%s cmd: %s input length: %d\n", __func__,
227− dimm_name, cmd_name, in_buf.buffer.length);
228− print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
229− 4, in_buf.buffer.pointer, min_t(u32, 128,
230− in_buf.buffer.length), true);
245+ dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
246+ __func__, dimm_name, cmd, func,
247+ in_buf.buffer.length);
248+ print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
249+ in_buf.buffer.pointer,
250+ min_t(u32, 256, in_buf.buffer.length), true);
231251 }
232252
233− out_obj = acpi_evaluate_dsm(handle, uuid, 1, cmd, &in_obj);
253+ out_obj = acpi_evaluate_dsm(handle, uuid, 1, func, &in_obj);
234254 if (!out_obj) {
235255 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
236256 cmd_name);
237257 return -EINVAL;
238258 }
239259
260+ if (call_pkg) {
261+ call_pkg->nd_fw_size = out_obj->buffer.length;
262+ memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
263+ out_obj->buffer.pointer,
264+ min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
265+
266+ ACPI_FREE(out_obj);
267+ /*
268+ * Need to support FW function w/o known size in advance.
269+ * Caller can determine required size based upon nd_fw_size.
270+ * If we return an error (like elsewhere) then caller wouldn't
271+ * be able to rely upon data returned to make calculation.
272+ */
273+ return 0;
274+ }
275+
240276 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
241277 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
242278 __func__, dimm_name, cmd_name, out_obj->type);
@@ -923,11 +959,13 @@ static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc, · 680 unmodified lines
923959 {
924960 struct acpi_device *adev, *adev_dimm;
925961 struct device *dev = acpi_desc->dev;
926− const u8 *uuid = to_nfit_uuid(NFIT_DEV_DIMM);
962+ unsigned long dsm_mask;
963+ const u8 *uuid;
927964 int i;
928965
929966 /* nfit test assumes 1:1 relationship between commands and dsms */
930967 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
968+ nfit_mem->family = NVDIMM_FAMILY_INTEL;
931969 adev = to_acpi_dev(acpi_desc);
932970 if (!adev)
933971 return 0;
@@ -940,7 +978,31 @@ static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc, · 6 unmodified lines
940978 return force_enable_dimms ? 0 : -ENODEV;
941979 }
942980
943− for (i = ND_CMD_SMART; i <= ND_CMD_VENDOR; i++)
981+ /*
982+ * Until standardization materializes we need to consider up to 3
983+ * different command sets. Note, that checking for function0 (bit0)
984+ * tells us if any commands are reachable through this uuid.
985+ */
986+ for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_HPE2; i++)
987+ if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
988+ break;
989+
990+ /* limit the supported commands to those that are publicly documented */
991+ nfit_mem->family = i;
992+ if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
993+ dsm_mask = 0x3fe;
994+ else if (nfit_mem->family == NVDIMM_FAMILY_HPE1)
995+ dsm_mask = 0x1c3c76;
996+ else if (nfit_mem->family == NVDIMM_FAMILY_HPE2)
997+ dsm_mask = 0x1fe;
998+ else {
999+ dev_err(dev, "unknown dimm command family\n");
1000+ nfit_mem->family = -1;
1001+ return force_enable_dimms ? 0 : -ENODEV;
1002+ }
1003+
1004+ uuid = to_nfit_uuid(nfit_mem->family);
1005+ for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
9441006 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
9451007 set_bit(i, &nfit_mem->dsm_mask);
9461008
@@ -953,8 +1015,8 @@ static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc) · 6 unmodified lines
9531015 int dimm_count = 0;
9541016
9551017 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1018+ unsigned long flags = 0, cmd_mask;
9561019 struct nvdimm *nvdimm;
957− unsigned long flags = 0;
9581020 u32 device_handle;
9591021 u16 mem_flags;
9601022 int rc;
@@ -978,12 +1040,17 @@ static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc) · 17 unmodified lines
9781040 continue;
9791041
9801042 /*
981− * For now there is 1:1 relationship between cmd_mask and
982− * dsm_mask.
1043+ * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1044+ * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1045+ * userspace interface.
9831046 */
1047+ cmd_mask = 1UL << ND_CMD_CALL;
1048+ if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1049+ cmd_mask |= nfit_mem->dsm_mask;
1050+
9841051 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
9851052 acpi_nfit_dimm_attribute_groups,
986− flags, nfit_mem->dsm_mask);
1053+ flags, cmd_mask);
9871054 if (!nvdimm)
9881055 return -ENOMEM;
9891056
@@ -2468,6 +2535,8 @@ static __init int nfit_init(void) · 1478 unmodified lines
24682535 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
24692536 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
24702537 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
2538+ acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
2539+ acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
24712540
24722541 nfit_wq = create_singlethread_workqueue("nfit");
24732542 if (!nfit_wq)
drivers/acpi/nfit.h+13−1
@@ -21,13 +21,25 @@ · 20 unmodified lines
2121 #include <linux/acpi.h>
2222 #include <acpi/acuuid.h>
2323
24+/* ACPI 6.1 */
2425 #define UUID_NFIT_BUS "2f10e7a4-9e91-11e4-89d3-123b93f75cba"
26+
27+/* http://pmem.io/documents/NVDIMM_DSM_Interface_Example.pdf */
2528 #define UUID_NFIT_DIMM "4309ac30-0d11-11e4-9191-0800200c9a66"
29+
30+/* https://github.com/HewlettPackard/hpe-nvm/blob/master/Documentation/ */
31+#define UUID_NFIT_DIMM_N_HPE1 "9002c334-acf3-4c0e-9642-a235f0d53bc6"
32+#define UUID_NFIT_DIMM_N_HPE2 "5008664b-b758-41a0-a03c-27c2f2d04f7e"
33+
2634 #define ACPI_NFIT_MEM_FAILED_MASK (ACPI_NFIT_MEM_SAVE_FAILED \
2735 | ACPI_NFIT_MEM_RESTORE_FAILED | ACPI_NFIT_MEM_FLUSH_FAILED \
2836 | ACPI_NFIT_MEM_NOT_ARMED)
2937
3038 enum nfit_uuids {
39+ /* for simplicity alias the uuid index with the family id */
40+ NFIT_DEV_DIMM = NVDIMM_FAMILY_INTEL,
41+ NFIT_DEV_DIMM_N_HPE1 = NVDIMM_FAMILY_HPE1,
42+ NFIT_DEV_DIMM_N_HPE2 = NVDIMM_FAMILY_HPE2,
3143 NFIT_SPA_VOLATILE,
3244 NFIT_SPA_PM,
3345 NFIT_SPA_DCR,
@@ -37,7 +49,6 @@ enum nfit_uuids { · 3 unmodified lines
3749 NFIT_SPA_PDISK,
3850 NFIT_SPA_PCD,
3951 NFIT_DEV_BUS,
40− NFIT_DEV_DIMM,
4152 NFIT_UUID_MAX,
4253 };
4354
@@ -110,6 +121,7 @@ struct nfit_mem { · 66 unmodified lines
110121 struct list_head list;
111122 struct acpi_device *adev;
112123 unsigned long dsm_mask;
124+ int family;
113125 };
114126
115127 struct acpi_nfit_desc {
drivers/nvdimm/bus.c+39−0
@@ -439,6 +439,12 @@ static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = { · 438 unmodified lines
439439 .out_num = 3,
440440 .out_sizes = { 4, 4, UINT_MAX, },
441441 },
442+ [ND_CMD_CALL] = {
443+ .in_num = 2,
444+ .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
445+ .out_num = 1,
446+ .out_sizes = { UINT_MAX, },
447+ },
442448 };
443449
444450 const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
@@ -473,6 +479,12 @@ static const struct nd_cmd_desc __nd_cmd_bus_descs[] = { · 28 unmodified lines
473479 .out_num = 3,
474480 .out_sizes = { 4, 4, 8, },
475481 },
482+ [ND_CMD_CALL] = {
483+ .in_num = 2,
484+ .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
485+ .out_num = 1,
486+ .out_sizes = { UINT_MAX, },
487+ },
476488 };
477489
478490 const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
@@ -500,6 +512,10 @@ u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd, · 21 unmodified lines
500512 struct nd_cmd_vendor_hdr *hdr = buf;
501513
502514 return hdr->in_length;
515+ } else if (cmd == ND_CMD_CALL) {
516+ struct nd_cmd_pkg *pkg = buf;
517+
518+ return pkg->nd_size_in;
503519 }
504520
505521 return UINT_MAX;
@@ -522,6 +538,12 @@ u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd, · 16 unmodified lines
522538 return out_field[1];
523539 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2)
524540 return out_field[1] - 8;
541+ else if (cmd == ND_CMD_CALL) {
542+ struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
543+
544+ return pkg->nd_size_out;
545+ }
546+
525547
526548 return UINT_MAX;
527549 }
@@ -588,6 +610,7 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · 60 unmodified lines
588610 unsigned int cmd = _IOC_NR(ioctl_cmd);
589611 void __user *p = (void __user *) arg;
590612 struct device *dev = &nvdimm_bus->dev;
613+ struct nd_cmd_pkg pkg;
591614 const char *cmd_name, *dimm_name;
592615 unsigned long cmd_mask;
593616 void *buf;
@@ -605,6 +628,11 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · 11 unmodified lines
605628 dimm_name = "bus";
606629 }
607630
631+ if (cmd == ND_CMD_CALL) {
632+ if (copy_from_user(&pkg, p, sizeof(pkg)))
633+ return -EFAULT;
634+ }
635+
608636 if (!desc || (desc->out_num + desc->in_num == 0) ||
609637 !test_bit(cmd, &cmd_mask))
610638 return -ENOTTY;
@@ -616,6 +644,7 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · 5 unmodified lines
616644 case ND_CMD_SET_CONFIG_DATA:
617645 case ND_CMD_ARS_START:
618646 case ND_CMD_CLEAR_ERROR:
647+ case ND_CMD_CALL:
619648 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
620649 nvdimm ? nvdimm_cmd_name(cmd)
621650 : nvdimm_bus_cmd_name(cmd));
@@ -643,6 +672,16 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · 21 unmodified lines
643672 in_len += in_size;
644673 }
645674
675+ if (cmd == ND_CMD_CALL) {
676+ dev_dbg(dev, "%s:%s, idx: %llu, in: %zu, out: %zu, len %zu\n",
677+ __func__, dimm_name, pkg.nd_command,
678+ in_len, out_len, buf_len);
679+
680+ for (i = 0; i < ARRAY_SIZE(pkg.nd_reserved2); i++)
681+ if (pkg.nd_reserved2[i])
682+ return -EINVAL;
683+ }
684+
646685 /* process an output envelope */
647686 for (i = 0; i < desc->out_num; i++) {
648687 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
include/uapi/linux/ndctl.h+42−0
@@ -125,6 +125,7 @@ enum { · 124 unmodified lines
125125 ND_CMD_VENDOR_EFFECT_LOG_SIZE = 7,
126126 ND_CMD_VENDOR_EFFECT_LOG = 8,
127127 ND_CMD_VENDOR = 9,
128+ ND_CMD_CALL = 10,
128129 };
129130
130131 enum {
@@ -158,6 +159,7 @@ static inline const char *nvdimm_cmd_name(unsigned cmd) · 27 unmodified lines
158159 [ND_CMD_VENDOR_EFFECT_LOG_SIZE] = "effect_size",
159160 [ND_CMD_VENDOR_EFFECT_LOG] = "effect_log",
160161 [ND_CMD_VENDOR] = "vendor",
162+ [ND_CMD_CALL] = "cmd_call",
161163 };
162164
163165 if (cmd < ARRAY_SIZE(names) && names[cmd])
@@ -224,4 +226,44 @@ enum ars_masks { · 60 unmodified lines
224226 ARS_STATUS_MASK = 0x0000FFFF,
225227 ARS_EXT_STATUS_SHIFT = 16,
226228 };
229+
230+/*
231+ * struct nd_cmd_pkg
232+ *
233+ * is a wrapper to a quasi pass thru interface for invoking firmware
234+ * associated with nvdimms.
235+ *
236+ * INPUT PARAMETERS
237+ *
238+ * nd_family corresponds to the firmware (e.g. DSM) interface.
239+ *
240+ * nd_command are the function index advertised by the firmware.
241+ *
242+ * nd_size_in is the size of the input parameters being passed to firmware
243+ *
244+ * OUTPUT PARAMETERS
245+ *
246+ * nd_fw_size is the size of the data firmware wants to return for
247+ * the call. If nd_fw_size is greater than size of nd_size_out, only
248+ * the first nd_size_out bytes are returned.
249+ */
250+
251+struct nd_cmd_pkg {
252+ __u64 nd_family; /* family of commands */
253+ __u64 nd_command;
254+ __u32 nd_size_in; /* INPUT: size of input args */
255+ __u32 nd_size_out; /* INPUT: size of payload */
256+ __u32 nd_reserved2[9]; /* reserved must be zero */
257+ __u32 nd_fw_size; /* OUTPUT: size fw wants to return */
258+ unsigned char nd_payload[]; /* Contents of call */
259+};
260+
261+/* These NVDIMM families represent pre-standardization command sets */
262+#define NVDIMM_FAMILY_INTEL 0
263+#define NVDIMM_FAMILY_HPE1 1
264+#define NVDIMM_FAMILY_HPE2 2
265+
266+#define ND_IOCTL_CALL _IOWR(ND_IOCTL, ND_CMD_CALL,\
267+ struct nd_cmd_pkg)
268+
227269 #endif /* __NDCTL_H__ */