Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'random-6.10-rc1-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/crng/random

Pull random number generator updates from Jason Donenfeld:

- The vmgenid driver can now be bound using device tree, rather than
just ACPI.

The improvement, from Sudan Landge, lets Amazon's Firecracker VMM
make use of the virtual device without having to expose an otherwise
unused ACPI stack in their "micro VM".

* tag 'random-6.10-rc1-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/crng/random:
virt: vmgenid: add support for devicetree bindings
dt-bindings: rng: Add vmgenid support
virt: vmgenid: change implementation to use a platform driver

+166 -36
+49
Documentation/devicetree/bindings/rng/microsoft,vmgenid.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/rng/microsoft,vmgenid.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Virtual Machine Generation ID 8 + 9 + maintainers: 10 + - Jason A. Donenfeld <Jason@zx2c4.com> 11 + 12 + description: 13 + Firmwares or hypervisors can use this devicetree to describe an 14 + interrupt and a shared resource to inject a Virtual Machine Generation ID. 15 + Virtual Machine Generation ID is a globally unique identifier (GUID) and 16 + the devicetree binding follows VMGenID specification defined in 17 + http://go.microsoft.com/fwlink/?LinkId=260709. 18 + 19 + properties: 20 + compatible: 21 + const: microsoft,vmgenid 22 + 23 + reg: 24 + description: 25 + Specifies a 16-byte VMGenID in endianness-agnostic hexadecimal format. 26 + maxItems: 1 27 + 28 + interrupts: 29 + description: 30 + Interrupt used to notify that a new VMGenID is available. 31 + maxItems: 1 32 + 33 + required: 34 + - compatible 35 + - reg 36 + - interrupts 37 + 38 + additionalProperties: false 39 + 40 + examples: 41 + - | 42 + #include <dt-bindings/interrupt-controller/arm-gic.h> 43 + rng@80000000 { 44 + compatible = "microsoft,vmgenid"; 45 + reg = <0x80000000 0x1000>; 46 + interrupts = <GIC_SPI 35 IRQ_TYPE_EDGE_RISING>; 47 + }; 48 + 49 + ...
+1
MAINTAINERS
··· 18669 18669 M: Jason A. Donenfeld <Jason@zx2c4.com> 18670 18670 S: Maintained 18671 18671 T: git https://git.kernel.org/pub/scm/linux/kernel/git/crng/random.git 18672 + F: Documentation/devicetree/bindings/rng/microsoft,vmgenid.yaml 18672 18673 F: drivers/char/random.c 18673 18674 F: drivers/virt/vmgenid.c 18674 18675
-1
drivers/virt/Kconfig
··· 16 16 config VMGENID 17 17 tristate "Virtual Machine Generation ID driver" 18 18 default y 19 - depends on ACPI 20 19 help 21 20 Say Y here to use the hypervisor-provided Virtual Machine Generation ID 22 21 to reseed the RNG when the VM is cloned. This is highly recommended if
+116 -35
drivers/virt/vmgenid.c
··· 2 2 /* 3 3 * Copyright (C) 2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 4 4 * 5 - * The "Virtual Machine Generation ID" is exposed via ACPI and changes when a 5 + * The "Virtual Machine Generation ID" is exposed via ACPI or DT and changes when a 6 6 * virtual machine forks or is cloned. This driver exists for shepherding that 7 7 * information to random.c. 8 8 */ 9 9 10 + #include <linux/acpi.h> 11 + #include <linux/interrupt.h> 10 12 #include <linux/kernel.h> 11 13 #include <linux/module.h> 12 - #include <linux/acpi.h> 14 + #include <linux/platform_device.h> 13 15 #include <linux/random.h> 14 16 15 17 ACPI_MODULE_NAME("vmgenid"); ··· 23 21 u8 this_id[VMGENID_SIZE]; 24 22 }; 25 23 26 - static int vmgenid_add(struct acpi_device *device) 24 + static void vmgenid_notify(struct device *device) 27 25 { 26 + struct vmgenid_state *state = device->driver_data; 27 + u8 old_id[VMGENID_SIZE]; 28 + 29 + memcpy(old_id, state->this_id, sizeof(old_id)); 30 + memcpy(state->this_id, state->next_id, sizeof(state->this_id)); 31 + if (!memcmp(old_id, state->this_id, sizeof(old_id))) 32 + return; 33 + add_vmfork_randomness(state->this_id, sizeof(state->this_id)); 34 + } 35 + 36 + static void setup_vmgenid_state(struct vmgenid_state *state, void *virt_addr) 37 + { 38 + state->next_id = virt_addr; 39 + memcpy(state->this_id, state->next_id, sizeof(state->this_id)); 40 + add_device_randomness(state->this_id, sizeof(state->this_id)); 41 + } 42 + 43 + #ifdef CONFIG_ACPI 44 + static void vmgenid_acpi_handler(acpi_handle __always_unused handle, 45 + u32 __always_unused event, void *dev) 46 + { 47 + vmgenid_notify(dev); 48 + } 49 + 50 + static int vmgenid_add_acpi(struct device *dev, struct vmgenid_state *state) 51 + { 52 + struct acpi_device *device = ACPI_COMPANION(dev); 28 53 struct acpi_buffer parsed = { ACPI_ALLOCATE_BUFFER }; 29 - struct vmgenid_state *state; 30 54 union acpi_object *obj; 31 55 phys_addr_t phys_addr; 32 56 acpi_status status; 57 + void *virt_addr; 33 58 int ret = 0; 34 - 35 - state = devm_kmalloc(&device->dev, sizeof(*state), GFP_KERNEL); 36 - if (!state) 37 - return -ENOMEM; 38 59 39 60 status = acpi_evaluate_object(device->handle, "ADDR", NULL, &parsed); 40 61 if (ACPI_FAILURE(status)) { ··· 74 49 75 50 phys_addr = (obj->package.elements[0].integer.value << 0) | 76 51 (obj->package.elements[1].integer.value << 32); 77 - state->next_id = devm_memremap(&device->dev, phys_addr, VMGENID_SIZE, MEMREMAP_WB); 78 - if (IS_ERR(state->next_id)) { 79 - ret = PTR_ERR(state->next_id); 52 + 53 + virt_addr = devm_memremap(&device->dev, phys_addr, VMGENID_SIZE, MEMREMAP_WB); 54 + if (IS_ERR(virt_addr)) { 55 + ret = PTR_ERR(virt_addr); 56 + goto out; 57 + } 58 + setup_vmgenid_state(state, virt_addr); 59 + 60 + status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY, 61 + vmgenid_acpi_handler, dev); 62 + if (ACPI_FAILURE(status)) { 63 + ret = -ENODEV; 80 64 goto out; 81 65 } 82 66 83 - memcpy(state->this_id, state->next_id, sizeof(state->this_id)); 84 - add_device_randomness(state->this_id, sizeof(state->this_id)); 85 - 86 - device->driver_data = state; 87 - 67 + dev->driver_data = state; 88 68 out: 89 69 ACPI_FREE(parsed.pointer); 90 70 return ret; 91 71 } 92 - 93 - static void vmgenid_notify(struct acpi_device *device, u32 event) 72 + #else 73 + static int vmgenid_add_acpi(struct device *dev, struct vmgenid_state *state) 94 74 { 95 - struct vmgenid_state *state = acpi_driver_data(device); 96 - u8 old_id[VMGENID_SIZE]; 75 + return -EINVAL; 76 + } 77 + #endif 97 78 98 - memcpy(old_id, state->this_id, sizeof(old_id)); 99 - memcpy(state->this_id, state->next_id, sizeof(state->this_id)); 100 - if (!memcmp(old_id, state->this_id, sizeof(old_id))) 101 - return; 102 - add_vmfork_randomness(state->this_id, sizeof(state->this_id)); 79 + static irqreturn_t vmgenid_of_irq_handler(int __always_unused irq, void *dev) 80 + { 81 + vmgenid_notify(dev); 82 + return IRQ_HANDLED; 103 83 } 104 84 105 - static const struct acpi_device_id vmgenid_ids[] = { 85 + static int vmgenid_add_of(struct platform_device *pdev, 86 + struct vmgenid_state *state) 87 + { 88 + void *virt_addr; 89 + int ret; 90 + 91 + virt_addr = devm_platform_get_and_ioremap_resource(pdev, 0, NULL); 92 + if (IS_ERR(virt_addr)) 93 + return PTR_ERR(virt_addr); 94 + 95 + setup_vmgenid_state(state, virt_addr); 96 + 97 + ret = platform_get_irq(pdev, 0); 98 + if (ret < 0) 99 + return ret; 100 + 101 + ret = devm_request_irq(&pdev->dev, ret, vmgenid_of_irq_handler, 102 + IRQF_SHARED, "vmgenid", &pdev->dev); 103 + if (ret < 0) 104 + return ret; 105 + 106 + pdev->dev.driver_data = state; 107 + return 0; 108 + } 109 + 110 + static int vmgenid_add(struct platform_device *pdev) 111 + { 112 + struct device *dev = &pdev->dev; 113 + struct vmgenid_state *state; 114 + int ret; 115 + 116 + state = devm_kmalloc(dev, sizeof(*state), GFP_KERNEL); 117 + if (!state) 118 + return -ENOMEM; 119 + 120 + if (dev->of_node) 121 + ret = vmgenid_add_of(pdev, state); 122 + else 123 + ret = vmgenid_add_acpi(dev, state); 124 + 125 + if (ret < 0) 126 + devm_kfree(dev, state); 127 + return ret; 128 + } 129 + 130 + static const struct of_device_id vmgenid_of_ids[] = { 131 + { .compatible = "microsoft,vmgenid", }, 132 + { }, 133 + }; 134 + MODULE_DEVICE_TABLE(of, vmgenid_of_ids); 135 + 136 + static const struct acpi_device_id vmgenid_acpi_ids[] = { 106 137 { "VMGENCTR", 0 }, 107 138 { "VM_GEN_COUNTER", 0 }, 108 139 { } 109 140 }; 141 + MODULE_DEVICE_TABLE(acpi, vmgenid_acpi_ids); 110 142 111 - static struct acpi_driver vmgenid_driver = { 112 - .name = "vmgenid", 113 - .ids = vmgenid_ids, 114 - .ops = { 115 - .add = vmgenid_add, 116 - .notify = vmgenid_notify 117 - } 143 + static struct platform_driver vmgenid_plaform_driver = { 144 + .probe = vmgenid_add, 145 + .driver = { 146 + .name = "vmgenid", 147 + .acpi_match_table = vmgenid_acpi_ids, 148 + .of_match_table = vmgenid_of_ids, 149 + }, 118 150 }; 119 151 120 - module_acpi_driver(vmgenid_driver); 152 + module_platform_driver(vmgenid_plaform_driver) 121 153 122 - MODULE_DEVICE_TABLE(acpi, vmgenid_ids); 123 154 MODULE_DESCRIPTION("Virtual Machine Generation ID"); 124 155 MODULE_LICENSE("GPL v2"); 125 156 MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");