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=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 0B69AC433B4 for ; Thu, 1 Apr 2021 04:56:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6CE9D6108F for ; Thu, 1 Apr 2021 04:56:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6CE9D6108F 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 F0E8F6B0080; Thu, 1 Apr 2021 00:56:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EBEC76B0081; Thu, 1 Apr 2021 00:56:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D38906B0082; Thu, 1 Apr 2021 00:56:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0099.hostedemail.com [216.40.44.99]) by kanga.kvack.org (Postfix) with ESMTP id B367D6B0080 for ; Thu, 1 Apr 2021 00:56:43 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 76AEF62C4 for ; Thu, 1 Apr 2021 04:56:43 +0000 (UTC) X-FDA: 77982587886.04.3ABD59C Received: from mail-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) by imf19.hostedemail.com (Postfix) with ESMTP id 8FB7D90009EA for ; Thu, 1 Apr 2021 04:56:41 +0000 (UTC) Received: by mail-il1-f182.google.com with SMTP id u2so1062264ilk.1 for ; Wed, 31 Mar 2021 21:56:42 -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; bh=i7SLQO3Ok64tMY0jVST2p67zAs5hiN1eckYi4HSFlxw=; b=IY4ucmt2w0wCGubldflas8o0drp6KCF5+tHz8qix+Y7GhdYp+Iwt641JmcieUXRL9Y Q2YI964uX1n67MbNcJ9yz0amTiM9jVTEPEgNEcIkimBrsn0d/jJtQVyFCCEaCUdxpzrg MM/iMCLZY4gWaKoIt1bB/54Rb2I0BJ3cnxsltETP3Ex3p1UeNo6l7T9RI/l+H7ewTGTV /3eLRYcLSaC+d7sOKMWYenWDMx5DICOyOOFoDKR1nUCNUIUN2vlhF4/wqhT7V40yzW/i yc0KgTriQyO+CM0Uie/4pRdfSbnCDSB6VfWssoVSDKpS6B/Z+b/apnPphvOddl1JIezq C1nQ== 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; bh=i7SLQO3Ok64tMY0jVST2p67zAs5hiN1eckYi4HSFlxw=; b=YNvvmEJpSb3y3Z5nDxoSNiKg+HsRk7nHcI519Kg95xsyXryIaImN5ehgQcQThuWc9b Hv495U9MVmWvKpKHWbkeQEdGgWrPWQRHHl2mdUVxUp1tEoH69WZ/KkTBdfwG3EwWWr+E ww+pdL79acnBAFkZofWlt2Sv1ppwdq9nS7rMp13Fxqb1VfE0gFz63ds36LMJY2wv4uvb ApOVHdQcE0wTIZzrpLEJ8UkW68CATwgU5WX0niLG12Hnnh9S65OziJXXQc5wHEUYiXws jeaGrFqRyb+nA6YaMxt688CElnzk6tvi/fgpqKs8m4Z37HOjmCIC4H4lHZ0C2pryHU4R oMaw== X-Gm-Message-State: AOAM530OPYwndO1lx/gNkmXtJSvYz9WHVEqiO+AQnuHlqzeOjH4Dx/GU XRuq5nJEXai6nlAqIGfJdcgH1iWidgTqG1Df3IY= X-Google-Smtp-Source: ABdhPJyjUvoCdhEDl1VlMFjSR7LXfTYc+Ew8zg5IxALbQCAbEcF5rOKFwZblg8ZVnu4Lxe8kxnHv5LMZDTzmilsBqiw= X-Received: by 2002:a92:ccd0:: with SMTP id u16mr5343881ilq.6.1617253002405; Wed, 31 Mar 2021 21:56:42 -0700 (PDT) MIME-Version: 1.0 References: <20210326012035.3853-1-apopple@nvidia.com> In-Reply-To: <20210326012035.3853-1-apopple@nvidia.com> From: Muchun Song Date: Thu, 1 Apr 2021 12:56:05 +0800 Message-ID: Subject: Re: [PATCH v2] kernel/resource: Fix locking in request_free_mem_region To: Alistair Popple Cc: linux-kernel , Andrew Morton , david@redhat.com, daniel.vetter@ffwll.ch, dan.j.williams@intel.com, Greg Kroah-Hartman , jhubbard@nvidia.com, jglisse@redhat.com, linux-mm@kvack.org, songmuchun@bytedance.com Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 8FB7D90009EA X-Stat-Signature: 7aa18h9t1kmih1yiqoioa5rey5kb3aop X-Rspamd-Server: rspam02 Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf19; identity=mailfrom; envelope-from=""; helo=mail-il1-f182.google.com; client-ip=209.85.166.182 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1617253001-631915 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: On Fri, Mar 26, 2021 at 9:22 AM Alistair Popple wrote: > > request_free_mem_region() is used to find an empty range of physical > addresses for hotplugging ZONE_DEVICE memory. It does this by iterating > over the range of possible addresses using region_intersects() to see if > the range is free. > > region_intersects() obtains a read lock before walking the resource tree > to protect against concurrent changes. However it drops the lock prior > to returning. This means by the time request_mem_region() is called in > request_free_mem_region() another thread may have already reserved the > requested region resulting in unexpected failures and a message in the > kernel log from hitting this condition: > > /* > * mm/hmm.c reserves physical addresses which then > * become unavailable to other users. Conflicts are > * not expected. Warn to aid debugging if encountered. > */ > if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) { > pr_warn("Unaddressable device %s %pR conflicts with %pR", > conflict->name, conflict, res); > > To fix this create versions of region_intersects() and > request_mem_region() that allow the caller to take the appropriate lock > such that it may be held over the required calls. > > Instead of creating another version of devm_request_mem_region() that > doesn't take the lock open-code it to allow the caller to pre-allocate > the required memory prior to taking the lock. > > Fixes: 0c385190392d8 ("resource: add a not device managed request_free_mem_region variant") > Fixes: 0092908d16c60 ("mm: factor out a devm_request_free_mem_region helper") > Fixes: 4ef589dc9b10c ("mm/hmm/devmem: device memory hotplug using ZONE_DEVICE") > Signed-off-by: Alistair Popple +cc my email (songmuchun@bytedance.com). Hi Alistair, Thanks for your patch. But there is a deadlock that should be fixed. Please see the following scenario. __request_region write_lock(&resource_lock) request_region_locked revoke_iomem devmem_is_allowed (arch/x86/mm/init.c) region_intersects read_lock(&resource_lock) // deadlock > > --- > > v2: > - Added Fixes tag > > --- > kernel/resource.c | 146 +++++++++++++++++++++++++++++----------------- > 1 file changed, 94 insertions(+), 52 deletions(-) > > diff --git a/kernel/resource.c b/kernel/resource.c > index 627e61b0c124..2d4652383dd2 100644 > --- a/kernel/resource.c > +++ b/kernel/resource.c > @@ -523,6 +523,34 @@ int __weak page_is_ram(unsigned long pfn) > } > EXPORT_SYMBOL_GPL(page_is_ram); > > +static int __region_intersects(resource_size_t start, size_t size, > + unsigned long flags, unsigned long desc) > +{ > + struct resource res; > + int type = 0; int other = 0; > + struct resource *p; > + > + res.start = start; > + res.end = start + size - 1; > + > + for (p = iomem_resource.child; p ; p = p->sibling) { > + bool is_type = (((p->flags & flags) == flags) && > + ((desc == IORES_DESC_NONE) || > + (desc == p->desc))); > + > + if (resource_overlaps(p, &res)) > + is_type ? type++ : other++; > + } > + > + if (type == 0) > + return REGION_DISJOINT; > + > + if (other == 0) > + return REGION_INTERSECTS; > + > + return REGION_MIXED; > +} > + > /** > * region_intersects() - determine intersection of region with known resources > * @start: region start address > @@ -546,31 +574,12 @@ EXPORT_SYMBOL_GPL(page_is_ram); > int region_intersects(resource_size_t start, size_t size, unsigned long flags, > unsigned long desc) > { > - struct resource res; > - int type = 0; int other = 0; > - struct resource *p; > - > - res.start = start; > - res.end = start + size - 1; > + int rc; > > read_lock(&resource_lock); > - for (p = iomem_resource.child; p ; p = p->sibling) { > - bool is_type = (((p->flags & flags) == flags) && > - ((desc == IORES_DESC_NONE) || > - (desc == p->desc))); > - > - if (resource_overlaps(p, &res)) > - is_type ? type++ : other++; > - } > + rc = __region_intersects(start, size, flags, desc); > read_unlock(&resource_lock); > - > - if (type == 0) > - return REGION_DISJOINT; > - > - if (other == 0) > - return REGION_INTERSECTS; > - > - return REGION_MIXED; > + return rc; > } > EXPORT_SYMBOL_GPL(region_intersects); > > @@ -1171,31 +1180,17 @@ struct address_space *iomem_get_mapping(void) > return smp_load_acquire(&iomem_inode)->i_mapping; > } > > -/** > - * __request_region - create a new busy resource region > - * @parent: parent resource descriptor > - * @start: resource start address > - * @n: resource region size > - * @name: reserving caller's ID string > - * @flags: IO resource flags > - */ > -struct resource * __request_region(struct resource *parent, > - resource_size_t start, resource_size_t n, > - const char *name, int flags) > +static bool request_region_locked(struct resource *parent, > + struct resource *res, resource_size_t start, > + resource_size_t n, const char *name, int flags) > { > - DECLARE_WAITQUEUE(wait, current); > - struct resource *res = alloc_resource(GFP_KERNEL); > struct resource *orig_parent = parent; > - > - if (!res) > - return NULL; > + DECLARE_WAITQUEUE(wait, current); > > res->name = name; > res->start = start; > res->end = start + n - 1; > > - write_lock(&resource_lock); > - > for (;;) { > struct resource *conflict; > > @@ -1230,16 +1225,39 @@ struct resource * __request_region(struct resource *parent, > write_lock(&resource_lock); > continue; > } > - /* Uhhuh, that didn't work out.. */ > - free_resource(res); > - res = NULL; > - break; > + return false; > } > - write_unlock(&resource_lock); > > if (res && orig_parent == &iomem_resource) > revoke_iomem(res); What do you think of the fix below? + if (res && orig_parent == &iomem_resource) { + write_unlock(&resource_lock); + revoke_iomem(res); + write_lock(&resource_lock); + } > > + return true; > +} > + > +/** > + * __request_region - create a new busy resource region > + * @parent: parent resource descriptor > + * @start: resource start address > + * @n: resource region size > + * @name: reserving caller's ID string > + * @flags: IO resource flags > + */ > +struct resource *__request_region(struct resource *parent, > + resource_size_t start, resource_size_t n, > + const char *name, int flags) > +{ > + struct resource *res = alloc_resource(GFP_KERNEL); > + > + if (!res) > + return NULL; > + > + write_lock(&resource_lock); > + if (!request_region_locked(parent, res, start, n, name, flags)) { > + /* Uhhuh, that didn't work out.. */ > + free_resource(res); > + res = NULL; > + } > + write_unlock(&resource_lock); > return res; > } > EXPORT_SYMBOL(__request_region); > @@ -1779,26 +1797,50 @@ static struct resource *__request_free_mem_region(struct device *dev, > { > resource_size_t end, addr; > struct resource *res; > + struct region_devres *dr = NULL; > + > + res = alloc_resource(GFP_KERNEL); > + if (!res) > + return ERR_PTR(-ENOMEM); > + > + if (dev) { > + dr = devres_alloc(devm_region_release, sizeof(struct region_devres), > + GFP_KERNEL); > + if (!dr) { > + free_resource(res); > + return ERR_PTR(-ENOMEM); > + } > + } > > size = ALIGN(size, 1UL << PA_SECTION_SHIFT); > end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1); > addr = end - size + 1UL; > > + write_lock(&resource_lock); > for (; addr > size && addr >= base->start; addr -= size) { > - if (region_intersects(addr, size, 0, IORES_DESC_NONE) != > + if (__region_intersects(addr, size, 0, IORES_DESC_NONE) != > REGION_DISJOINT) > continue; > > - if (dev) > - res = devm_request_mem_region(dev, addr, size, name); > - else > - res = request_mem_region(addr, size, name); > - if (!res) > - return ERR_PTR(-ENOMEM); > + if (!request_region_locked(&iomem_resource, res, addr, > + size, name, 0)) > + break; > + > res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY; > + if (dev) { > + dr->parent = &iomem_resource; > + dr->start = addr; > + dr->n = size; > + devres_add(dev, dr); > + } > + > + write_unlock(&resource_lock); > return res; > } > > + write_unlock(&resource_lock); > + free_resource(res); > + > return ERR_PTR(-ERANGE); > } > > -- > 2.20.1 >