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 46315C433C1 for ; Fri, 26 Mar 2021 05:15:44 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 97DED61A4A for ; Fri, 26 Mar 2021 05:15:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 97DED61A4A 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 C347C6B0036; Fri, 26 Mar 2021 01:15:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C0C0B6B006E; Fri, 26 Mar 2021 01:15:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AAD196B0070; Fri, 26 Mar 2021 01:15:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0186.hostedemail.com [216.40.44.186]) by kanga.kvack.org (Postfix) with ESMTP id 922CF6B0036 for ; Fri, 26 Mar 2021 01:15:42 -0400 (EDT) Received: from smtpin37.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 55016180ACEEB for ; Fri, 26 Mar 2021 05:15:42 +0000 (UTC) X-FDA: 77960862924.37.1800F5B Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf24.hostedemail.com (Postfix) with ESMTP id A6C92A0000FC for ; Fri, 26 Mar 2021 05:15:39 +0000 (UTC) Received: by mail-pl1-f174.google.com with SMTP id f17so346386plr.0 for ; Thu, 25 Mar 2021 22:15:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=oN13yPbaAsOv4qWLYqKdeUMjaQgF1YjFDRa8G7F1Wns=; b=MHKrmGYdWvxfU6nP53gVJ5o3QYXhXONq42CG74OvvDcxJUI3jQYYfy4vK9US1P8Lv6 kJkfd34cGxYIhJfmNIsS/UviGwuTWtZKsU+emSCCZsTnrMJgrIYhNuvU+zEWcWZOUVsS PNzkiXMI8Ya/gImUrrYzUUOJ7PJInAiaN2A/zOa8Knl4S8wctKALuEZLFPP0qBdhFRQO WLRp+YF5qccYcOcPm6x39J6ddFUqFIky3DyD6HxCkhBUXroQtpoNTKVXWfO+2hbkhGKX HVGShowUaHz/Pe9JqJ+F5c3cR+rV78rh5nCkmB+A/MY9dpONMpZ+tClZQxijUe/OiQzN bq3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=oN13yPbaAsOv4qWLYqKdeUMjaQgF1YjFDRa8G7F1Wns=; b=OsIKq68A7fbnSJwEX5AvoaDkPq2UwOc54IGZsG3C1NdcgftZWPAmU52HyWRtR9uOnQ z9n0NwIYYKeEjDzvgzTGSvqXKDfKr0rPixMj8LEWsw7VNql6CCRCvAk4Zpao+tu+2s/W 6M/+TQBwwszoU3Mu7d+R4Iw0wC9m03KwKqC5JVKCL0hKz1NrOesAb0TM2xwGcQj/hmvG T0LTHAMoCczdxrM8kQ4WTFysmi9YPkYPbYc2h6kcGr9dlM3rn00gwt03diBtuWSYUzyw dG168R37ZmUis9Ev29yHZM29Zu59gajCv4aC/ChorF/4EUhMADC8WrMgfnCDZwWC6wE6 +mjw== X-Gm-Message-State: AOAM532mdRftB0KFzPofWIP35memoPtuFQxLValB1bB1mMSNs7iTxle6 St2Vy7rSiZA1v4CBHcGBdjs= X-Google-Smtp-Source: ABdhPJyxPxOLe+skuF9DoXO9L/gWbycGcV6blk57NdpBvnx/vG95h9BukJ3WmcKzR14TbUYzECUHmA== X-Received: by 2002:a17:902:b088:b029:e6:e1d8:20cc with SMTP id p8-20020a170902b088b02900e6e1d820ccmr13455564plr.27.1616735740442; Thu, 25 Mar 2021 22:15:40 -0700 (PDT) Received: from localhost (121-45-173-48.tpgi.com.au. [121.45.173.48]) by smtp.gmail.com with ESMTPSA id t22sm7162931pjw.54.2021.03.25.22.15.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 22:15:39 -0700 (PDT) Date: Fri, 26 Mar 2021 16:15:36 +1100 From: Balbir Singh To: Alistair Popple Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org, david@redhat.com, daniel.vetter@ffwll.ch, dan.j.williams@intel.com, gregkh@linuxfoundation.org, jhubbard@nvidia.com, jglisse@redhat.com, linux-mm@kvack.org Subject: Re: [PATCH v2] kernel/resource: Fix locking in request_free_mem_region Message-ID: <20210326051536.GN77072@balbir-desktop> References: <20210326012035.3853-1-apopple@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210326012035.3853-1-apopple@nvidia.com> X-Stat-Signature: kbdsbdqzijhzy6j5k7cpjcxzx4xcqde9 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: A6C92A0000FC Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf24; identity=mailfrom; envelope-from=""; helo=mail-pl1-f174.google.com; client-ip=209.85.214.174 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616735739-34587 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 12:20:35PM +1100, 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 > > --- > > 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))); is_type is a bad name, are we saying "is" as in boolean question? Or is it short for something like intersection_type? I know you've just moved the code over :) > + > + 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); This part of the diff looks confusing, do we have a waitqueue and we call schedule() within a function called with the lock held? > > 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); > > + 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); Should the function be called __request_region_locked? > 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); > } > Balbir Singh.