Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1163513ybg; Thu, 11 Jun 2020 02:38:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7o46F3S8pdnl/9r3D/QQGlXmdJ6YLcB3zNd0QkE9NNtCTuTjZvGpQy2Z+eJiwZyn4j9+0 X-Received: by 2002:a17:906:d93c:: with SMTP id rn28mr7938266ejb.190.1591868293685; Thu, 11 Jun 2020 02:38:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591868293; cv=none; d=google.com; s=arc-20160816; b=L3/Mtc9ptev5C96aprlVQCXtbvAGQEgCZp6lbbT0+VCl+QhlgL2BUnTmc9BsUnupKW Eo9ziuR/Gf5ivAezB2kniHOeypeVVjbkrvH40wpcuTiIaQ4Olv1i38TSXoiauikw7emd pzLaMUSi+g76lO5L1oyS4Z8PlucwmMvRHyvavt4AqFxhX2g2we7WavQOp595BGG53iT2 7uND0FD9ITnku6HKnHPDVrds74aPsLzdvwuXS0L2v6zMSpbINy1Pu0mqu3BpYIeH2hLZ fWZXgxftqPN4zFd5buRHwnDuf5zRzx7V0OW0oc3rDTdeygheLG+TVTDOlmj7mqYD3XWI e2NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=skekyVLdwFwBTm754pc77fwSJmldMeXVb8k3yiao8Zo=; b=0FRE0syM90KSFQKsrkfkckhRQEiwodZZVgS5JcHPIeflWSQaeCKDaZfqNj2zNNC8+e Qf5c/4bN0CZ/i7XQk2pSCaor4wcixiM0yr1Ds/HRSvpacjHGDPAVUUgrCwwbTKu3lrkI UI9H1T3gBa9YcDcHcCnsFlzLuXxEClSbjAxtDU4QcEvhHjwb5oghKKVuSeNfLx19ShGc 8U3407TXE3baHBB1OgcwHuRcJ9HBPd8gIioRo96ivx7CntL5YiCeWZQc7E+t+09m58v/ Glt084daB4S1XneDlBte+m/2nN2gwY/EBontERHyDYmpqNuvWUwpoa/T/7pQWJ9/qslR u3cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CiYkZgks; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u5si1602416ejz.120.2020.06.11.02.37.51; Thu, 11 Jun 2020 02:38:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CiYkZgks; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726945AbgFKJfj (ORCPT + 99 others); Thu, 11 Jun 2020 05:35:39 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:48956 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726907AbgFKJfj (ORCPT ); Thu, 11 Jun 2020 05:35:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591868137; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=skekyVLdwFwBTm754pc77fwSJmldMeXVb8k3yiao8Zo=; b=CiYkZgksT71QAo9GQc/TM2XXvtrXM5TycKVm3BzS9JHAF2ZibGak5Fr0oL6MAyf1e1HDMM CheY6/Cqqu13PESHqjQIiJrAl3gnT/qroz5sBQHdKdK0gUCkPpeI+e5N2O5ryRgkuk9Oam c8oGE03bL2w2c+7fQl+N7mEVJzXdbGI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-46-79N41bhHNyOSrHDxFwNDUg-1; Thu, 11 Jun 2020 05:35:35 -0400 X-MC-Unique: 79N41bhHNyOSrHDxFwNDUg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 17C5E1883605; Thu, 11 Jun 2020 09:35:34 +0000 (UTC) Received: from t480s.redhat.com (ovpn-114-160.ams2.redhat.com [10.36.114.160]) by smtp.corp.redhat.com (Postfix) with ESMTP id B303110013C1; Thu, 11 Jun 2020 09:35:25 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, virtio-dev@lists.oasis-open.org, virtualization@lists.linux-foundation.org, kvm@vger.kernel.org, "Michael S . Tsirkin" , Jason Wang , David Hildenbrand , Pankaj Gupta , teawater Subject: [PATCH v1] virtio-mem: add memory via add_memory_driver_managed() Date: Thu, 11 Jun 2020 11:35:18 +0200 Message-Id: <20200611093518.5737-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Virtio-mem managed memory is always detected and added by the virtio-mem driver, never using something like the firmware-provided memory map. This is the case after an ordinary system reboot, and has to be guaranteed after kexec. Especially, virtio-mem added memory resources can contain inaccessible parts ("unblocked memory blocks"), blindly forwarding them to a kexec kernel is dangerous, as unplugged memory will get accessed (esp. written). Let's use the new way of adding special driver-managed memory introduced in commit 75ac4c58bc0d ("mm/memory_hotplug: introduce add_memory_driver_managed()"). This will result in no entries in /sys/firmware/memmap ("raw firmware- provided memory map"), the memory resource will be flagged IORESOURCE_MEM_DRIVER_MANAGED (esp., kexec_file_load() will not place kexec images on this memory), and it is exposed as "System RAM (virtio_mem)" in /proc/iomem, so esp. kexec-tools can properly handle it. Example /proc/iomem before this change: [...] 140000000-333ffffff : virtio0 140000000-147ffffff : System RAM 334000000-533ffffff : virtio1 338000000-33fffffff : System RAM 340000000-347ffffff : System RAM 348000000-34fffffff : System RAM [...] Example /proc/iomem after this change: [...] 140000000-333ffffff : virtio0 140000000-147ffffff : System RAM (virtio_mem) 334000000-533ffffff : virtio1 338000000-33fffffff : System RAM (virtio_mem) 340000000-347ffffff : System RAM (virtio_mem) 348000000-34fffffff : System RAM (virtio_mem) [...] Cc: "Michael S. Tsirkin" Cc: Pankaj Gupta Cc: teawater Signed-off-by: David Hildenbrand --- Based on latest Linus' tree (and not a tag) because - virtio-mem has just been merged via the vhost tree - add_memory_driver_managed() has been merged a week ago via the -mm tree I'd like to have this patch in 5.8, with the initial merge of virtio-mem if possible (so the user space representation of virtio-mem added memory resources won't change anymore). --- drivers/virtio/virtio_mem.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c index 50c689f250450..d2eab3558a9e1 100644 --- a/drivers/virtio/virtio_mem.c +++ b/drivers/virtio/virtio_mem.c @@ -101,6 +101,11 @@ struct virtio_mem { /* The parent resource for all memory added via this device. */ struct resource *parent_resource; + /* + * Copy of "System RAM (virtio_mem)" to be used for + * add_memory_driver_managed(). + */ + const char *resource_name; /* Summary of all memory block states. */ unsigned long nb_mb_state[VIRTIO_MEM_MB_STATE_COUNT]; @@ -414,8 +419,20 @@ static int virtio_mem_mb_add(struct virtio_mem *vm, unsigned long mb_id) if (nid == NUMA_NO_NODE) nid = memory_add_physaddr_to_nid(addr); + /* + * When force-unloading the driver and we still have memory added to + * Linux, the resource name has to stay. + */ + if (!vm->resource_name) { + vm->resource_name = kstrdup_const("System RAM (virtio_mem)", + GFP_KERNEL); + if (!vm->resource_name) + return -ENOMEM; + } + dev_dbg(&vm->vdev->dev, "adding memory block: %lu\n", mb_id); - return add_memory(nid, addr, memory_block_size_bytes()); + return add_memory_driver_managed(nid, addr, memory_block_size_bytes(), + vm->resource_name); } /* @@ -1890,10 +1907,12 @@ static void virtio_mem_remove(struct virtio_device *vdev) vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL] || vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE] || vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL] || - vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE]) + vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE]) { dev_warn(&vdev->dev, "device still has system memory added\n"); - else + } else { virtio_mem_delete_resource(vm); + kfree_const(vm->resource_name); + } /* remove all tracking data - no locking needed */ vfree(vm->mb_state); -- 2.26.2