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_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, 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 90158C433E6 for ; Mon, 31 Aug 2020 09:35:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 43BF32068F for ; Mon, 31 Aug 2020 09:35:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BkWe+B6D" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 43BF32068F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C3D4A6B0002; Mon, 31 Aug 2020 05:35:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BEE7C6B0003; Mon, 31 Aug 2020 05:35:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ADCE46B0005; Mon, 31 Aug 2020 05:35:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0167.hostedemail.com [216.40.44.167]) by kanga.kvack.org (Postfix) with ESMTP id 97B3C6B0002 for ; Mon, 31 Aug 2020 05:35:49 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 4B3D9181AEF1A for ; Mon, 31 Aug 2020 09:35:49 +0000 (UTC) X-FDA: 77210356818.22.meat72_111833c2708e Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id 1A0DD18038E67 for ; Mon, 31 Aug 2020 09:35:49 +0000 (UTC) X-HE-Tag: meat72_111833c2708e X-Filterd-Recvd-Size: 8912 Received: from mail-io1-f65.google.com (mail-io1-f65.google.com [209.85.166.65]) by imf40.hostedemail.com (Postfix) with ESMTP for ; Mon, 31 Aug 2020 09:35:48 +0000 (UTC) Received: by mail-io1-f65.google.com with SMTP id l8so5179781ios.2 for ; Mon, 31 Aug 2020 02:35:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=HsNtRpi0XGja6Sj9lSJWoznoixc15RWCQVmmU4+oCGQ=; b=BkWe+B6D3lN045gds9IoFYUQ+o2juq6u/5mtP4qMWEZgN4V72MQv52x13zt+0Vagcw PiplKpJDZIg1gJ/K+PqleXk0+ms/PIwH4m04bUTXOP6LZe4mc4lUMCnaFMAZ1Nf7OH5R +TO1BAV2uJm4VdeFkABkZgYtRWdSxBF7MObhl58VcQjVfHSqlwYCoTT8LWmc7Hg5H7zn rFVALNyy0QNyyM9EWLw6uOxE4Uiqo6MEltvqGfVltYm+Lsi1MNLu6dooosPYw/1NDhKj 0s2nGoPPTS/ePGpaVVkCc9/P0ug7/BLTlsgu0GElkFNNztt3JlZyxdTPXMRhtYmxlaLF /55A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=HsNtRpi0XGja6Sj9lSJWoznoixc15RWCQVmmU4+oCGQ=; b=PXvj5c6w20jXRe2w9e4xUxyZCUM1kh8VsN0GxFg12Z+ZKRddU8CC1RHI8ZOQN4TjWl +v48tcX/OBvpcENa+h9QCPyCxvzLHizX0vD8IPAggGSJJhtfpZqR8W+t44wQDca44PRy jw6TPUducFPN2hdqNmmbWTCdJeCFUJcwGXmOQlcTbj4u3MdIanALuZ0eqa7mSQKNZOCd JOBaRhzWema+4a3K1h6iEya3IGkFVLmgrEcgLQef0j0cIM3Ca7qDTYpMfG6z487DS2Up 0f2D2pFVcxA76x3Rzm8Xmok9aW3m7xyVeS/ZKEyMEHMtfxQwqDvMd1qlEC0cWZgdKrGw tBBg== X-Gm-Message-State: AOAM531z8VqPS1ZwFGXONz6SeT6cxqIZKwhbsCYNmRwR99rBOSoNcT9I fzyNrWYTAGPVe7KJwOk9vyJj0me0lI9KVW8zHnI= X-Google-Smtp-Source: ABdhPJww8t42sUm+jGq9r4vVjstNF9Iiu7OItpZmQeVdogqBS81K4nUxuzik3G0XU545fyNmeFJRfcVCeSn7YUqh2/o= X-Received: by 2002:a6b:b513:: with SMTP id e19mr547960iof.167.1598866548037; Mon, 31 Aug 2020 02:35:48 -0700 (PDT) MIME-Version: 1.0 References: <20200821103431.13481-1-david@redhat.com> <20200821103431.13481-3-david@redhat.com> In-Reply-To: <20200821103431.13481-3-david@redhat.com> From: Pankaj Gupta Date: Mon, 31 Aug 2020 11:35:36 +0200 Message-ID: Subject: Re: [PATCH v1 2/5] kernel/resource: merge_system_ram_resources() to merge resources after hotplug To: David Hildenbrand Cc: LKML , virtualization@lists.linux-foundation.org, Linux MM , linux-hyperv@vger.kernel.org, xen-devel@lists.xenproject.org, 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 , Baoquan He , Wei Yang Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 1A0DD18038E67 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam05 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 system ram resources it added to its resource container, but > also by XEN and Hyper-V to trigger merging of system ram resources in > iomem_resource. > > 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 > Cc: Pankaj Gupta > Cc: Baoquan He > Cc: Wei Yang > Signed-off-by: David Hildenbrand > --- > include/linux/ioport.h | 3 +++ > kernel/resource.c | 52 ++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 55 insertions(+) > > diff --git a/include/linux/ioport.h b/include/linux/ioport.h > index 52a91f5fa1a36..3bb0020cd6ddc 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_system_ram_resources(struct resource *res); > +#endif > > /* Wrappers for managed devices */ > struct device; > diff --git a/kernel/resource.c b/kernel/resource.c > index 1dcef5d53d76e..b4e0963edadd2 100644 > --- a/kernel/resource.c > +++ b/kernel/resource.c > @@ -1360,6 +1360,58 @@ void release_mem_region_adjustable(struct resource= *parent, > } > #endif /* CONFIG_MEMORY_HOTREMOVE */ > > +#ifdef CONFIG_MEMORY_HOTPLUG > +static bool system_ram_resources_mergeable(struct resource *r1, > + struct resource *r2) > +{ > + return r1->flags =3D=3D r2->flags && r1->end + 1 =3D=3D r2->start= && > + r1->name =3D=3D r2->name && r1->desc =3D=3D r2->desc && > + !r1->child && !r2->child; > +} > + > +/* > + * merge_system_ram_resources - try to merge contiguous system ram resou= rces > + * @parent: parent resource descriptor > + * > + * This interface is intended for memory hotplug, whereby lots of contig= uous > + * system ram 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 that are busy and > + * don't have any children are considered. All applicable child resource= s > + * 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 system ram resources to= the > + * parent (besides during boot). > + * - release_mem_region_adjustable() will split on demand on memory hotu= nplug > + */ > +void merge_system_ram_resources(struct resource *parent) > +{ > + const unsigned long flags =3D IORESOURCE_SYSTEM_RAM | IORESOURCE_= BUSY; > + struct resource *cur, *next; > + > + write_lock(&resource_lock); > + > + cur =3D parent->child; > + while (cur && cur->sibling) { > + next =3D cur->sibling; > + if ((cur->flags & flags) =3D=3D flags && Maybe this can be changed to: !(cur->flags & ~flags) > + system_ram_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_system_ram_resources); > +#endif /* CONFIG_MEMORY_HOTPLUG */ > + > /* > * Managed region resource > */ > -- > 2.26.2 >