From patchwork Fri Jul 31 09:18:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 11694519 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CBEEE913 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 8E34522CA1 for ; Fri, 31 Jul 2020 09:19:05 +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 8E34522CA1 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) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 840318D000B; Fri, 31 Jul 2020 05:19:04 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org 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-Original-To: linux-mm@kvack.org 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-Spam-Summary: 1,0,0,,d41d8cd98f00b204,david@redhat.com,,RULES_HIT:30029:30051:30054:30064:30091,0,RBL:207.211.31.81:@redhat.com:.lbl8.mailshell.net-62.18.0.100 66.10.201.10;04yfyp7xwuk3moswy3b5h1zjm5mx3oc88y3t4gkad4x6k1ktiowiioe39kfdcqs.ijfk1mw7bmf7569hibmt8oxtp16oe19fjznqyny8btqo6uenrbe6hr6w89nnqyn.k-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none 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_Pau_Monn=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 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 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é 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 *, resource_size_t, extern void release_mem_region_adjustable(struct resource *, resource_size_t, resource_size_t); #endif +#ifdef CONFIG_MEMORY_HOTPLUG +extern void merge_child_mem_resources(struct resource *res, const char *name); +#endif /* 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 */ +#ifdef CONFIG_MEMORY_HOTPLUG +static bool mem_resources_mergeable(struct resource *r1, struct resource *r2) +{ + return r1->end + 1 == r2->start && + r1->name == r2->name && + r1->flags == r2->flags && + (r1->flags & IORESOURCE_MEM) && + r1->desc == 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 consecutive + * 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. All + * applicable child resources must be immutable during the request. + * + * Note: + * - The caller has to make sure that no pointers to resources that might + * get merged are held anymore. Callers should only trigger merging of child + * resources when they are the only one adding such resources to the parent. + * E.g., if two mechanisms could add "System RAM" immediately below the + * same parent, this function is not safe to use. + * - release_mem_region_adjustable() will split on demand on memory hotunplug + */ +void merge_child_mem_resources(struct resource *parent, const char *name) +{ + struct resource *cur, *next; + + write_lock(&resource_lock); + + cur = parent->child; + while (cur && cur->sibling) { + next = cur->sibling; + if (!strcmp(cur->name, name) && + mem_resources_mergeable(cur, next)) { + cur->end = next->end; + cur->sibling = next->sibling; + free_resource(next); + next = cur->sibling; + } + cur = next; + } + + write_unlock(&resource_lock); +} +EXPORT_SYMBOL(merge_child_mem_resources); +#endif /* CONFIG_MEMORY_HOTPLUG */ + /* * Managed region resource */