From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6DBEC433E0 for ; Mon, 11 Jan 2021 12:42:01 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7F795224B8 for ; Mon, 11 Jan 2021 12:42:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7F795224B8 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A61548D002A; Mon, 11 Jan 2021 07:42:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A131D8D0020; Mon, 11 Jan 2021 07:42:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 901D78D002A; Mon, 11 Jan 2021 07:42:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7B9AE8D0020 for ; Mon, 11 Jan 2021 07:42:00 -0500 (EST) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 38B6F3626 for ; Mon, 11 Jan 2021 12:42:00 +0000 (UTC) X-FDA: 77693456400.13.shade79_440ea002750c Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin13.hostedemail.com (Postfix) with ESMTP id 1424818140B60 for ; Mon, 11 Jan 2021 12:42:00 +0000 (UTC) X-HE-Tag: shade79_440ea002750c X-Filterd-Recvd-Size: 8656 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf23.hostedemail.com (Postfix) with ESMTP for ; Mon, 11 Jan 2021 12:41:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610368918; 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: in-reply-to:in-reply-to:references:references; bh=1jltblhu2Dd6iDOxJ7ANMmEZ7SYdZNOGQ9HGQetpvC0=; b=GMQDkrQDkm425Poi4EBy1MEkDaeCvw8IrGyXlSWLiVTw2bTRJREx66PkxSuw2lAqzwDD1n UHDjvFhpMOOO0adUSxItHQPkZUBo5ppuqvTQHlBX3zPmQ60x9UADczidKzp5nXeUDL081K RASybt+Qf7DuUNc1fqBMrj7APB+xcDw= 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-419-na-pWaWAN-6waX9uPlQz_w-1; Mon, 11 Jan 2021 07:41:55 -0500 X-MC-Unique: na-pWaWAN-6waX9uPlQz_w-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 83A7C190A7A3; Mon, 11 Jan 2021 12:41:52 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-103.ams2.redhat.com [10.36.115.103]) by smtp.corp.redhat.com (Postfix) with ESMTP id 981BF1349A; Mon, 11 Jan 2021 12:41:41 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , "Michael S. Tsirkin" , Jason Wang , Pankaj Gupta , Michal Hocko , Oscar Salvador , Wei Yang , Andrew Morton , catalin.marinas@arm.com, teawater , Anshuman Khandual , Pankaj Gupta , Jonathan Cameron , hca@linux.ibm.com, Vasily Gorbik , Will Deacon , Ard Biesheuvel , Mark Rutland Subject: [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug Date: Mon, 11 Jan 2021 13:41:40 +0100 Message-Id: <20210111124140.11423-1-david@redhat.com> In-Reply-To: <1608218912-28932-1-git-send-email-anshuman.khandual@arm.com> References: <1608218912-28932-1-git-send-email-anshuman.khandual@arm.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Right now, we only check against MAX_PHYSMEM_BITS - but turns out there are more restrictions of which memory we can actually hotplug, especially om arm64 or s390x once we support them: we might receive something like -E2BIG or -ERANGE from add_memory_driver_managed(), stopping device operation. So, check right when initializing the device which memory we can add, warning the user. Try only adding actually pluggable ranges: in the worst case, no memory provided by our device is pluggable. In the usual case, we expect all device memory to be pluggable, and in corner cases only some memory at the end of the device-managed memory region to not be pluggable. Cc: "Michael S. Tsirkin" Cc: Jason Wang Cc: Pankaj Gupta Cc: Michal Hocko Cc: Oscar Salvador Cc: Wei Yang Cc: Andrew Morton Cc: catalin.marinas@arm.com Cc: teawater Cc: Anshuman Khandual Cc: Pankaj Gupta Cc: Jonathan Cameron Cc: hca@linux.ibm.com Cc: Vasily Gorbik Cc: Will Deacon Cc: Ard Biesheuvel Cc: Mark Rutland Cc: Heiko Carstens Cc: Michal Hocko Signed-off-by: David Hildenbrand --- This is an example how virito-mem intends to use an interface like memhp_get_pluggable_range() once around. See: "[PATCH V2 0/3] mm/hotplug: Pre-validate the address range with platform" https://lkml.kernel.org/r/1608218912-28932-1-git-send-email-anshuman.khan= dual@arm.com @Anshuman, feel free to pick up and carry this patch. I'll retest the fin= al result / new versions of you series. --- drivers/virtio/virtio_mem.c | 40 +++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c index 9fc9ec4a25f5..1fe40b2d7b6d 100644 --- a/drivers/virtio/virtio_mem.c +++ b/drivers/virtio/virtio_mem.c @@ -2222,7 +2222,7 @@ static int virtio_mem_unplug_pending_mb(struct virt= io_mem *vm) */ static void virtio_mem_refresh_config(struct virtio_mem *vm) { - const uint64_t phys_limit =3D 1UL << MAX_PHYSMEM_BITS; + const struct range pluggable_range =3D memhp_get_pluggable_range(true); uint64_t new_plugged_size, usable_region_size, end_addr; =20 /* the plugged_size is just a reflection of what _we_ did previously */ @@ -2234,15 +2234,25 @@ static void virtio_mem_refresh_config(struct virt= io_mem *vm) /* calculate the last usable memory block id */ virtio_cread_le(vm->vdev, struct virtio_mem_config, usable_region_size, &usable_region_size); - end_addr =3D vm->addr + usable_region_size; - end_addr =3D min(end_addr, phys_limit); + end_addr =3D min(vm->addr + usable_region_size - 1, + pluggable_range.end); =20 - if (vm->in_sbm) - vm->sbm.last_usable_mb_id =3D - virtio_mem_phys_to_mb_id(end_addr) - 1; - else - vm->bbm.last_usable_bb_id =3D - virtio_mem_phys_to_bb_id(vm, end_addr) - 1; + if (vm->in_sbm) { + vm->sbm.last_usable_mb_id =3D virtio_mem_phys_to_mb_id(end_addr); + if (!IS_ALIGNED(end_addr + 1, memory_block_size_bytes())) + vm->sbm.last_usable_mb_id--; + } else { + vm->bbm.last_usable_bb_id =3D virtio_mem_phys_to_bb_id(vm, + end_addr); + if (!IS_ALIGNED(end_addr + 1, vm->bbm.bb_size)) + vm->bbm.last_usable_bb_id--; + } + /* + * If we cannot plug any of our device memory (e.g., nothing in the + * usable region is addressable), the last usable memory block id will + * be smaller than the first usable memory block id. We'll stop + * attempting to add memory with -ENOSPC from our main loop. + */ =20 /* see if there is a request to change the size */ virtio_cread_le(vm->vdev, struct virtio_mem_config, requested_size, @@ -2364,6 +2374,7 @@ static int virtio_mem_init_vq(struct virtio_mem *vm= ) =20 static int virtio_mem_init(struct virtio_mem *vm) { + const struct range pluggable_range =3D memhp_get_pluggable_range(true); const uint64_t phys_limit =3D 1UL << MAX_PHYSMEM_BITS; uint64_t sb_size, addr; uint16_t node_id; @@ -2405,9 +2416,10 @@ static int virtio_mem_init(struct virtio_mem *vm) if (!IS_ALIGNED(vm->addr + vm->region_size, memory_block_size_bytes())) dev_warn(&vm->vdev->dev, "The alignment of the physical end address can make some memory unus= able.\n"); - if (vm->addr + vm->region_size > phys_limit) + if (vm->addr < pluggable_range.start || + vm->addr + vm->region_size - 1 > pluggable_range.end) dev_warn(&vm->vdev->dev, - "Some memory is not addressable. This can make some memory unusable.= \n"); + "Some device memory is not addressable/pluggable. This can make some= memory unusable.\n"); =20 /* * We want subblocks to span at least MAX_ORDER_NR_PAGES and @@ -2429,7 +2441,8 @@ static int virtio_mem_init(struct virtio_mem *vm) vm->sbm.sb_size; =20 /* Round up to the next full memory block */ - addr =3D vm->addr + memory_block_size_bytes() - 1; + addr =3D max_t(uint64_t, vm->addr, pluggable_range.start) + + memory_block_size_bytes() - 1; vm->sbm.first_mb_id =3D virtio_mem_phys_to_mb_id(addr); vm->sbm.next_mb_id =3D vm->sbm.first_mb_id; } else { @@ -2450,7 +2463,8 @@ static int virtio_mem_init(struct virtio_mem *vm) } =20 /* Round up to the next aligned big block */ - addr =3D vm->addr + vm->bbm.bb_size - 1; + addr =3D max_t(uint64_t, vm->addr, pluggable_range.start) + + vm->bbm.bb_size - 1; vm->bbm.first_bb_id =3D virtio_mem_phys_to_bb_id(vm, addr); vm->bbm.next_bb_id =3D vm->bbm.first_bb_id; } --=20 2.29.2