Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp9940534ybi; Wed, 24 Jul 2019 12:43:03 -0700 (PDT) X-Google-Smtp-Source: APXvYqyMe06oswcBZZvLC3pcEAeiQsA2EvTQFjYuuhU6XIbzM2PgEykbnvyiD83GZ11n1Hla+eNj X-Received: by 2002:a62:ce8e:: with SMTP id y136mr13132141pfg.29.1563997383121; Wed, 24 Jul 2019 12:43:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563997383; cv=none; d=google.com; s=arc-20160816; b=wgfi/BO2doEQOWCzk2slv8w0pkgiQzsv8LvpW2Mmi3xnCuGVf6ccEbDeOTPv7obLoi fXIUy3chxyGUhuMLh+EXmivP5I83UDqvkR22FMVbSNaIekkjmQc5zlINQsur4diQQvJG K6lj8I+ZlHFpiAhEkYuBKHt3v+0eu/qtlE2uojstMFUe96ooVCL9WsOWwbjdlq5gLjl0 zNkylcF2qArdbkyVHgZicAOXOnMKnOfodG/RPP0n0X+F3xYWBPS1qeexvy2JS8rEwECZ 82MzaXthhgYzEkwe5kFBz//Y9cEkagXpBZ18sHs/SVKEzhg7893cw4F0qrf/YQVzl/7d fNJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=0CxDyRjl2r1RA4AssopKQa5EFdzIQrd9tEPopiUUTwo=; b=EXJP9fD69MUtnrfCVmuXUmQc1VYH7y0huK5xwHFHGife9G0YGSlF3G1fgXmjPU/JA4 +4K06SmA3A/Q8+3ByRz1s9vyikCBBhquGobxG7kZJxLGCVxC2IG+Acbrq5fyCRMPmnIr +oR+1jPX6OzxC1oYqJi/N/4Ho8Q9lOEpzRwap6N1eNJdHXJQrm8lLeI3rBIgLgwDN3Hi 8KWqvDHriDudBhGl0pTwTCEB8aeNDGZnSm9d2+4Ke78rLVDlkKd2Hxm/OGtdpTTxXD+x iSsLhlxjSuKKBlJkAbuf/pR02hE+ePKBzUNtdx5OEl5qTHzL4o9AB6JeiCRB54Suhy+6 n1Gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=LqRmqCap; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d7si16842169pfd.185.2019.07.24.12.42.48; Wed, 24 Jul 2019 12:43:03 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=LqRmqCap; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390214AbfGXTlf (ORCPT + 99 others); Wed, 24 Jul 2019 15:41:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:42846 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389610AbfGXTl2 (ORCPT ); Wed, 24 Jul 2019 15:41:28 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DC35520665; Wed, 24 Jul 2019 19:41:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563997287; bh=B7m4baX6UanuRsGECnWTZXi5zT2+G5/6xl7S3Vw6AJ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LqRmqCapzS4HsAp3G6cwdu5bDQLbQe/AkJTVrV/XZs/tj5TBlfWQUFT6XrDStlZ3Z uGUNOy8b3R0zRXVtiGX2u65Mhp8ApP+Mu4Ok2Yqi6NbHHqoSfgAcn2G0pNh4jTbjFU PIbNDwIKQM3JxOVfmF1BIVUyXflY3kQznmGgQi0Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nadav Amit , Andrew Morton , Dan Williams , Borislav Petkov , Toshi Kani , Peter Zijlstra , Dave Hansen , Bjorn Helgaas , Ingo Molnar , Linus Torvalds Subject: [PATCH 5.2 382/413] resource: fix locking in find_next_iomem_res() Date: Wed, 24 Jul 2019 21:21:13 +0200 Message-Id: <20190724191802.405370162@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190724191735.096702571@linuxfoundation.org> References: <20190724191735.096702571@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nadav Amit commit 49f17c26c123b60fd1c74629eef077740d16ffc2 upstream. Since resources can be removed, locking should ensure that the resource is not removed while accessing it. However, find_next_iomem_res() does not hold the lock while copying the data of the resource. Keep holding the lock while the data is copied. While at it, change the return value to a more informative value. It is disregarded by the callers. [akpm@linux-foundation.org: fix find_next_iomem_res() documentation] Link: http://lkml.kernel.org/r/20190613045903.4922-2-namit@vmware.com Fixes: ff3cc952d3f00 ("resource: Add remove_resource interface") Signed-off-by: Nadav Amit Reviewed-by: Andrew Morton Reviewed-by: Dan Williams Cc: Borislav Petkov Cc: Toshi Kani Cc: Peter Zijlstra Cc: Dave Hansen Cc: Bjorn Helgaas Cc: Ingo Molnar Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- kernel/resource.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) --- a/kernel/resource.c +++ b/kernel/resource.c @@ -326,7 +326,7 @@ EXPORT_SYMBOL(release_resource); * * If a resource is found, returns 0 and @*res is overwritten with the part * of the resource that's within [@start..@end]; if none is found, returns - * -1 or -EINVAL for other invalid parameters. + * -ENODEV. Returns -EINVAL for invalid parameters. * * This function walks the whole tree and not just first level children * unless @first_lvl is true. @@ -365,16 +365,16 @@ static int find_next_iomem_res(resource_ break; } - read_unlock(&resource_lock); - if (!p) - return -1; + if (p) { + /* copy data */ + res->start = max(start, p->start); + res->end = min(end, p->end); + res->flags = p->flags; + res->desc = p->desc; + } - /* copy data */ - res->start = max(start, p->start); - res->end = min(end, p->end); - res->flags = p->flags; - res->desc = p->desc; - return 0; + read_unlock(&resource_lock); + return p ? 0 : -ENODEV; } static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,