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=-9.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable 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 20AE4C433DF for ; Fri, 31 Jul 2020 09:19:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D73CB22CB3 for ; Fri, 31 Jul 2020 09:19:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gwyJn3jU" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D73CB22CB3 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 8690C8D001C; Fri, 31 Jul 2020 05:19:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 840318D000B; Fri, 31 Jul 2020 05:19:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 755C38D001C; Fri, 31 Jul 2020 05:19:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0181.hostedemail.com [216.40.44.181]) by kanga.kvack.org (Postfix) with ESMTP id 615D38D000B for ; Fri, 31 Jul 2020 05:19:04 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 1F677180AD801 for ; Fri, 31 Jul 2020 09:19:04 +0000 (UTC) X-FDA: 77097821808.23.part16_460838f26f82 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin23.hostedemail.com (Postfix) with ESMTP id E6C2437606 for ; Fri, 31 Jul 2020 09:19:03 +0000 (UTC) X-HE-Tag: part16_460838f26f82 X-Filterd-Recvd-Size: 7628 Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by imf04.hostedemail.com (Postfix) with ESMTP for ; Fri, 31 Jul 2020 09:19:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1596187142; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GX38+opkWxBZfD2far/R2rRkc2X2qlfoUgt4pP5SXDg=; b=gwyJn3jU7N/REiKzRAhgTbpkjsF3/nUITttfsKZRuHB1p23+rMMRAwX0ywfGW4cRJyE5x9 AobB+bvssCqavcJHHZaS6yRmHlBo7MzkztYsCI4ZKmtE8AddPcP19Y7FpfEnil8oK66xtL Wmn+xyRPyWX32GAtaulDphrYkhHmomY= 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-301-Z7A2oVdiNTiak_-Gh5Vfug-1; Fri, 31 Jul 2020 05:18:58 -0400 X-MC-Unique: Z7A2oVdiNTiak_-Gh5Vfug-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4358480382D; Fri, 31 Jul 2020 09:18:55 +0000 (UTC) Received: from t480s.redhat.com (ovpn-113-22.ams2.redhat.com [10.36.113.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5D0681A835; Fri, 31 Jul 2020 09:18:50 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: virtualization@lists.linux-foundation.org, linux-mm@kvack.org, linux-hyperv@vger.kernel.org, xen-devel@lists.xenproject.org, David Hildenbrand , Andrew Morton , Michal Hocko , Dan Williams , Jason Gunthorpe , Kees Cook , Ard Biesheuvel , Thomas Gleixner , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Wei Liu , Boris Ostrovsky , Juergen Gross , Stefano Stabellini , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Julien Grall Subject: [PATCH RFCv1 2/5] kernel/resource: merge_child_mem_resources() to merge memory resources after adding succeeded Date: Fri, 31 Jul 2020 11:18:35 +0200 Message-Id: <20200731091838.7490-3-david@redhat.com> In-Reply-To: <20200731091838.7490-1-david@redhat.com> References: <20200731091838.7490-1-david@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Rspamd-Queue-Id: E6C2437606 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 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: Some add_memory*() users add memory in small, contiguous memory blocks. Examples include virtio-mem, hyper-v balloon, and the XEN balloon. This can quickly result in a lot of memory resources, whereby the actual resource boundaries are not of interest (e.g., it might be relevant for DIMMs, exposed via /proc/iomem to user space). We really want to merge added resources in this scenario where possible. Let's provide an interface to trigger merging of applicable child resources. It will be, for example, used by virtio-mem to trigger merging of memory resources it added (via add_memory_driver() managed) to its resource container. Note: We really want to merge after the whole operation succeeded, not directly when adding a resource to the resource tree (it would break add_memory_resource() and require splitting resources again when the operation failed - e.g., due to -ENOMEM). Cc: Andrew Morton Cc: Michal Hocko Cc: Dan Williams Cc: Jason Gunthorpe Cc: Kees Cook Cc: Ard Biesheuvel Cc: Thomas Gleixner Cc: "K. Y. Srinivasan" Cc: Haiyang Zhang Cc: Stephen Hemminger Cc: Wei Liu Cc: Boris Ostrovsky Cc: Juergen Gross Cc: Stefano Stabellini Cc: Roger Pau Monn=C3=A9 Cc: Julien Grall Signed-off-by: David Hildenbrand --- include/linux/ioport.h | 3 +++ kernel/resource.c | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/include/linux/ioport.h b/include/linux/ioport.h index 52a91f5fa1a36..743b87fe2205b 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -251,6 +251,9 @@ extern void __release_region(struct resource *, resou= rce_size_t, extern void release_mem_region_adjustable(struct resource *, resource_si= ze_t, resource_size_t); #endif +#ifdef CONFIG_MEMORY_HOTPLUG +extern void merge_child_mem_resources(struct resource *res, const char *= name); +#endif =20 /* Wrappers for managed devices */ struct device; diff --git a/kernel/resource.c b/kernel/resource.c index 249c6b54014de..01ecc5b7956f5 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -1360,6 +1360,62 @@ void release_mem_region_adjustable(struct resource= *parent, } #endif /* CONFIG_MEMORY_HOTREMOVE */ =20 +#ifdef CONFIG_MEMORY_HOTPLUG +static bool mem_resources_mergeable(struct resource *r1, struct resource= *r2) +{ + return r1->end + 1 =3D=3D r2->start && + r1->name =3D=3D r2->name && + r1->flags =3D=3D r2->flags && + (r1->flags & IORESOURCE_MEM) && + r1->desc =3D=3D r2->desc && + !r1->child && !r2->child; +} + +/* + * merge_child_mem_resources - try to merge contiguous child IORESOURCE_= MEM + * resources with the given name that match = all + * other properties + * @parent: parent resource descriptor + * @name: name of the child resources to consider for merging + * + * This interface is intended for memory hotplug, whereby lots of consec= utive + * memory resources are added (e.g., via add_memory*()) by a driver, and= the + * actual resource boundaries are not of interest (e.g., it might be + * relevant for DIMMs). Only immediate child resources are considered. A= ll + * applicable child resources must be immutable during the request. + * + * Note: + * - The caller has to make sure that no pointers to resources that migh= t + * get merged are held anymore. Callers should only trigger merging of= child + * resources when they are the only one adding such resources to the p= arent. + * E.g., if two mechanisms could add "System RAM" immediately below th= e + * same parent, this function is not safe to use. + * - release_mem_region_adjustable() will split on demand on memory hotu= nplug + */ +void merge_child_mem_resources(struct resource *parent, const char *name= ) +{ + struct resource *cur, *next; + + write_lock(&resource_lock); + + cur =3D parent->child; + while (cur && cur->sibling) { + next =3D cur->sibling; + if (!strcmp(cur->name, name) && + mem_resources_mergeable(cur, next)) { + cur->end =3D next->end; + cur->sibling =3D next->sibling; + free_resource(next); + next =3D cur->sibling; + } + cur =3D next; + } + + write_unlock(&resource_lock); +} +EXPORT_SYMBOL(merge_child_mem_resources); +#endif /* CONFIG_MEMORY_HOTPLUG */ + /* * Managed region resource */ --=20 2.26.2