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=-15.1 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 1EA38C4338F for ; Thu, 12 Aug 2021 07:34:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8349D6101E for ; Thu, 12 Aug 2021 07:34:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8349D6101E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 8C5B58D0001; Thu, 12 Aug 2021 03:34:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 874096B0071; Thu, 12 Aug 2021 03:34:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 73C368D0001; Thu, 12 Aug 2021 03:34:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0157.hostedemail.com [216.40.44.157]) by kanga.kvack.org (Postfix) with ESMTP id 56FDA6B006C for ; Thu, 12 Aug 2021 03:34:05 -0400 (EDT) Received: from smtpin40.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id E56D1180397D6 for ; Thu, 12 Aug 2021 07:34:04 +0000 (UTC) X-FDA: 78465614808.40.50D948E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 90DEE30013BE for ; Thu, 12 Aug 2021 07:34:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628753644; 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=mYH9IB8kLiUJPK2eGWH1AkGjFzTYCt4aUi+sk0Dhj6E=; b=Vz3D6WBB28s8Ccqu1K+2Q9h84fW4iw0i8I9bIxbMfBJcQ7cmcawVzFdzmwAeWNTiyWdUyu RN4Lz7jTMTaHNH++mkMB4meXikBNyEXdMu7H71VATqB3sdMjPFtGZ27rzgs4iw11daOyjj 6RR8wiqPSZoyfIhtzjs7nOPwGXHcvVc= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-374-WeaYbIVqP4KsIiDW1B9MoQ-1; Thu, 12 Aug 2021 03:34:03 -0400 X-MC-Unique: WeaYbIVqP4KsIiDW1B9MoQ-1 Received: by mail-wm1-f71.google.com with SMTP id b196-20020a1c80cd0000b02902e677003785so3008974wmd.7 for ; Thu, 12 Aug 2021 00:34:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:cc:references:from:organization:subject :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=mYH9IB8kLiUJPK2eGWH1AkGjFzTYCt4aUi+sk0Dhj6E=; b=EWK0yRCGvlZ0bvfS4x6NKnhaPq/8yWz9TUMuHVMWrWR+sqBWVVEoFNqHBEVoj79zYC RGpsBCe0G9aea6Uqx2w4aswrFvpUlBtTRsMpVg6tMaTx58BSuny4jGkeQExgdpgt5goA 1HVBHu3hD9cM7wWE5RvktFFcAVedwDbr2aTbOYmXryE0fDQUYWtpmZxDf7hg/1DG8+2P uhi2PMc7JAOqyZaTzon3m39J61r4xewSB/yfvY4FJrDW95H802sijzhjtiu3KrADokP7 5W3va0yH06h0OI7jEaaA06I5oPe9HWn3/1laAbwzrmnmpe3+cVF58FAoigGEj/boTVlM RzaA== X-Gm-Message-State: AOAM53083AotX/jWokcfTy+dN5wSXEpcK30LMToX+++IXb7WY1exfLw9 lJbzhFrEtzdGw1cyeAJSNuS9vPDDzwRhM6cP2VxwfHUBkEFL0WV/RQ8BSvma9rcxMvU677ygZ/H qrz2OxZKaneHzfpzo5FybCIEAdABOBoOIclRPt8j+iQdAHuBgMGCK1zV5mo0= X-Received: by 2002:a7b:c7d7:: with SMTP id z23mr2456028wmk.136.1628753641788; Thu, 12 Aug 2021 00:34:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwXDY4HnbdNw6PM31nv/QfI9ZkXgq7IZiHchMbUa+C8ozEvyCRTCvh51tc4Bi0fHK++AFiliw== X-Received: by 2002:a7b:c7d7:: with SMTP id z23mr2455999wmk.136.1628753641495; Thu, 12 Aug 2021 00:34:01 -0700 (PDT) Received: from [192.168.3.132] (p4ff23d8b.dip0.t-ipconnect.de. [79.242.61.139]) by smtp.gmail.com with ESMTPSA id n186sm9388381wme.40.2021.08.12.00.34.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 12 Aug 2021 00:34:01 -0700 (PDT) To: Andy Shevchenko Cc: "linux-kernel@vger.kernel.org" , Arnd Bergmann , Greg Kroah-Hartman , "Michael S. Tsirkin" , Jason Wang , "Rafael J. Wysocki" , Andrew Morton , Dan Williams , Hanjun Guo , Andy Shevchenko , "virtualization@lists.linux-foundation.org" , "linux-mm@kvack.org" References: <20210811203612.138506-1-david@redhat.com> <20210811203612.138506-4-david@redhat.com> <37179df3-13d7-9b98-4cd8-13bb7f735129@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v1 3/3] kernel/resource: cleanup and optimize iomem_is_exclusive() Message-ID: Date: Thu, 12 Aug 2021 09:34:00 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US X-Rspamd-Queue-Id: 90DEE30013BE Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Vz3D6WBB; spf=none (imf09.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam01 X-Stat-Signature: 93gqduc1etezaw86y7rro4h1tzwx9z7b X-HE-Tag: 1628753644-979264 Content-Transfer-Encoding: quoted-printable 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 12.08.21 09:14, Andy Shevchenko wrote: >=20 >=20 > On Thursday, August 12, 2021, David Hildenbrand > wrote: >=20 > On 11.08.21 22:47, Andy Shevchenko wrote: >=20 >=20 >=20 > On Wednesday, August 11, 2021, David Hildenbrand > > >> wrote: >=20 > =C2=A0 =C2=A0 Let's clean it up a bit, removing the unnecessar= y usage of > r_next() by > =C2=A0 =C2=A0 next_resource(), and use next_range_resource() i= n case we > are not > =C2=A0 =C2=A0 interested in a certain subtree. >=20 > =C2=A0 =C2=A0 Signed-off-by: David Hildenbrand > =C2=A0 =C2=A0 >> > =C2=A0 =C2=A0 --- > =C2=A0 =C2=A0 =C2=A0=C2=A0kernel/resource.c | 19 +++++++++++--= ------ > =C2=A0 =C2=A0 =C2=A0=C2=A01 file changed, 11 insertions(+), 8 = deletions(-) >=20 > =C2=A0 =C2=A0 diff --git a/kernel/resource.c b/kernel/resource= .c > =C2=A0 =C2=A0 index 2938cf520ca3..ea853a075a83 100644 > =C2=A0 =C2=A0 --- a/kernel/resource.c > =C2=A0 =C2=A0 +++ b/kernel/resource.c > =C2=A0 =C2=A0 @@ -1754,9 +1754,8 @@ static int strict_iomem_ch= ecks; > =C2=A0 =C2=A0 =C2=A0=C2=A0 */ > =C2=A0 =C2=A0 =C2=A0=C2=A0bool iomem_is_exclusive(u64 addr) > =C2=A0 =C2=A0 =C2=A0=C2=A0{ > =C2=A0 =C2=A0 -=C2=A0 =C2=A0 =C2=A0 =C2=A0struct resource *p =3D= &iomem_resource; > =C2=A0 =C2=A0 +=C2=A0 =C2=A0 =C2=A0 =C2=A0struct resource *p; > =C2=A0 =C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 bool err =3D f= alse; > =C2=A0 =C2=A0 -=C2=A0 =C2=A0 =C2=A0 =C2=A0loff_t l; > =C2=A0 =C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 int size =3D P= AGE_SIZE; >=20 > =C2=A0 =C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (!strict_io= mem_checks) > =C2=A0 =C2=A0 @@ -1765,27 +1764,31 @@ bool iomem_is_exclusive(= u64 addr) > =C2=A0 =C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 addr =3D addr = & PAGE_MASK; >=20 > =C2=A0 =C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 read_lock(&res= ource_lock); > =C2=A0 =C2=A0 -=C2=A0 =C2=A0 =C2=A0 =C2=A0for (p =3D p->child;= p ; p =3D r_next(NULL, p, &l)) { > =C2=A0 =C2=A0 +=C2=A0 =C2=A0 =C2=A0 =C2=A0for (p =3D iomem_res= ource.child; p ;) { >=20 >=20 > Hi Andy, >=20 >=20 > I consider the ordinal part of p initialization is slightly > better and done outside of read lock. >=20 > Something like > p=3D &iomem_res...; > read lock > for (p =3D p->child; ...) { >=20 >=20 > Why should we care about doing that outside of the lock? That smell= s > like a micro-optimization the compiler will most probably overwrite > either way as the address of iomem_resource is just constant? >=20 > Also, for me it's much more readable and compact if we perform a > single initialization instead of two separate ones in this case. >=20 > We're using the pattern I use in, find_next_iomem_res() and > __region_intersects(), while we use the old pattern in > iomem_map_sanity_check(), where we also use the same unnecessary > r_next() call. >=20 > I might just cleanup iomem_map_sanity_check() in a similar way. >=20 >=20 >=20 > Yes, it=E2=80=99s like micro optimization. If you want your way I sugge= st then=20 > to add a macro >=20 > #define for_each_iomem_resource_child() \ > =C2=A0for (iomem_resource...) I think the only thing that really makes sense would be something like th= is on top (not compiled yet): diff --git a/kernel/resource.c b/kernel/resource.c index ea853a075a83..35aaa72df0ce 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -80,6 +80,11 @@ static struct resource *next_resource_skip_children(st= ruct resource *p) return p->sibling; } =20 +#define for_each_resource(_root, _p, _skip_children) \ + for ((_p) =3D (_root)->child; (_p); \ + (_p) =3D (_skip_children) ? next_resource_skip_children(_p) = : \ + next_resource(_p)) + static void *r_next(struct seq_file *m, void *v, loff_t *pos) { struct resource *p =3D v; @@ -1714,16 +1719,16 @@ int iomem_map_sanity_check(resource_size_t addr, = unsigned long size) bool iomem_range_contains_excluded(u64 addr, u64 size) { const unsigned int flags =3D IORESOURCE_SYSTEM_RAM | IORESOURCE_= EXCLUSIVE; - bool excluded =3D false; + bool skip_children, excluded =3D false; struct resource *p; =20 read_lock(&resource_lock); - for (p =3D iomem_resource.child; p ;) { + for_each_resource(&iomem_resource, p, skip_children) { if (p->start >=3D addr + size) break; if (p->end < addr) { /* No need to consider children */ - p =3D next_resource_skip_children(p); + skip_children =3D true; continue; } /* @@ -1735,7 +1740,7 @@ bool iomem_range_contains_excluded(u64 addr, u64 si= ze) excluded =3D true; break; } - p =3D next_resource(p); + skip_children =3D false; } read_unlock(&resource_lock); =20 @@ -1755,7 +1760,7 @@ static int strict_iomem_checks; bool iomem_is_exclusive(u64 addr) { struct resource *p; - bool err =3D false; + bool skip_children, err =3D false; int size =3D PAGE_SIZE; =20 if (!strict_iomem_checks) @@ -1764,7 +1769,7 @@ bool iomem_is_exclusive(u64 addr) addr =3D addr & PAGE_MASK; =20 read_lock(&resource_lock); - for (p =3D iomem_resource.child; p ;) { + for_each_resource(&iomem_resource, p, skip_children) { /* * We can probably skip the resources without * IORESOURCE_IO attribute? @@ -1773,7 +1778,7 @@ bool iomem_is_exclusive(u64 addr) break; if (p->end < addr) { /* No need to consider children */ - p =3D next_resource_skip_children(p); + skip_children =3D true; continue; } =20 @@ -1788,7 +1793,7 @@ bool iomem_is_exclusive(u64 addr) err =3D true; break; } - p =3D next_resource(p); + skip_children =3D false; } read_unlock(&resource_lock); =20 Thoughts? --=20 Thanks, David / dhildenb