Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp3743080ybh; Tue, 6 Aug 2019 00:33:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqy4XN7aYI3qch2cgYea0XKtuw5RSra8hEJK09yLqNG+6tGVme57l93g8rBGjmvoiDcNsm1/ X-Received: by 2002:a17:90a:d593:: with SMTP id v19mr1758653pju.1.1565076787949; Tue, 06 Aug 2019 00:33:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565076787; cv=none; d=google.com; s=arc-20160816; b=AcILf5nZ4KwTFgDLHt7fMa/+bUxgh/VG+52g54FSXRzi8IRhaoWV3Se2hSqcekpRy7 Fe8EupsSqTpL65wDR6cg8WXlUfeVUwLWr4f4JoE4pnZ9RZSbLscrIAGXOnwHUicHYT0Y KctGi0W2YeMuWPDz4KpoOOlsAAvAIbwgnnuxXuSPlDesPs8y2w9g822uobJ2ANTGdpRG nLayHsly8mxfeShr9r6zXnBasEG4Y0KlVH5V0cBR6sX1mUS3Jcla2nZKsh1WhINjVS5G 87jA6GURvYvCa3Vw9fbla20HMUaYioQhZY8Z/eLzhmlGyWfDcDwNkwXX7DgNrQ8xKWvS JS3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=oTs2cY0AP7Gt6Axj3jnR38Lz4Y8u331s1JOYlARdadg=; b=pN73zhYI+AFTgjIc9cyzP+yRz44JjTXmOROZdf4++9loST9lRzsOIBe+sdEw2SQ4yQ fnvwUDFsiCNwFeBpPn/RotS/VqnESulkAlxxBSxYTwH5ESm/6fDjCoMm7a19KyL6o1P2 AncuSqRQWNG0PpPRVGJEeAov+GPx94tByTSQbShqPIx0uwTzSPGwznADwkmx5MznOHOm ZOMVuhyuH2V/8FKkALJwUk/FTEe+VVcn2MCUrQRKCbu/mzrQ/2+iKdbLtCRN4vpuSiqS klXLl+5qACVoozOYOEagu6mx8tm9JmP2WPEgebBMBelU1FxckJuFTr85ji3bNtmasgh8 3avA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Eu966wVx; 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 a13si46347706pgw.81.2019.08.06.00.32.52; Tue, 06 Aug 2019 00:33:07 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Eu966wVx; 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 S1732034AbfHFHcI (ORCPT + 99 others); Tue, 6 Aug 2019 03:32:08 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:50466 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731576AbfHFHcH (ORCPT ); Tue, 6 Aug 2019 03:32:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=oTs2cY0AP7Gt6Axj3jnR38Lz4Y8u331s1JOYlARdadg=; b=Eu966wVxtPDyAfxY4T5b9Mvp/ HDR0erbGAZXz2AqGAeOdEK/K2XQOlQyLFVaKfbHmskzMRogWsRVFf0LnYCUZq9AyhET+H+vM1RZHr vCIiWCAJvnK6MTZkjamjsUOj585w67g30jU0Fwujx4PC8+7jHBrOW8snLwJTpHWqf6Cz2oB2pmaSV DX1j6EYOG6XABnCRU/Kfrtx9XGQW6pUOuOCOGY/1B7Aih8on8ATF7J5BeSOzis45yUKB+8NzOAT7r PS2vzt0MgWhrIfmutdWvIPkCihF4U8wHpmdjG+LLRa31zeTy96L3iN6r75nZ7ec6uQzrCErOoz8KS v3FL8ONsQ==; Received: from hch by bombadil.infradead.org with local (Exim 4.92 #3 (Red Hat Linux)) id 1hutww-0006vs-OK; Tue, 06 Aug 2019 07:32:02 +0000 Date: Tue, 6 Aug 2019 00:32:02 -0700 From: Christoph Hellwig To: Eric Auger Cc: eric.auger.pro@gmail.com, joro@8bytes.org, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, dwmw2@infradead.org, shameerali.kolothum.thodi@huawei.com, alex.williamson@redhat.com, robin.murphy@arm.com, hch@infradead.org Subject: Re: [PATCH v2] iommu: revisit iommu_insert_resv_region() implementation Message-ID: <20190806073202.GA26575@infradead.org> References: <20190801155946.20645-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190801155946.20645-1-eric.auger@redhat.com> User-Agent: Mutt/1.11.4 (2019-03-13) X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A couple nitpicks below: On Thu, Aug 01, 2019 at 05:59:46PM +0200, Eric Auger wrote: > - * The new element is sorted by address with respect to the other > - * regions of the same type. In case it overlaps with another > - * region of the same type, regions are merged. In case it > - * overlaps with another region of different type, regions are > - * not merged. > + * Elements are sorted by start address and overlapping segments > + * of the same type are merged. > */ > +int iommu_insert_resv_region(struct iommu_resv_region *new, > + struct list_head *regions) > { > + struct iommu_resv_region *iter, *tmp, *nr, *top; > + struct list_head stack; > + bool added = false; > > + INIT_LIST_HEAD(&stack); Nit: you could just use LIST_HEAD(&stack); to declare and initialize the variable in a single line. > + nr = iommu_alloc_resv_region(new->start, new->length, > + new->prot, new->type); > + if (!nr) > return -ENOMEM; > > + /* First add the new elt based on start address sorting */ /elt/element/ ? > + list_for_each_entry(iter, regions, list) { > + if (nr->start < iter->start) { > + list_add_tail(&nr->list, &iter->list); > + added = true; > + break; > + } else if (nr->start == iter->start && nr->type <= iter->type) { > + list_add_tail(&nr->list, &iter->list); > + added = true; > + break; > + } Nit: no need for an else after a a break. But then again both branches look identical, so why don't you just merge them: if (nr->start < iter->start || (nr->start == iter->start && nr->type <= iter->type)) { list_add_tail(&nr->list, &iter->list); added = true; break; } > + if (!added) > + list_add_tail(&nr->list, regions); Probably down to preference, but I'd just use a goto to jump past the list_add and save the added variable. > + /* Merge overlapping segments of type nr->type, if any */ > + list_for_each_entry_safe(iter, tmp, regions, list) { > + phys_addr_t top_end, iter_end = iter->start + iter->length - 1; > + bool found = false; > + > + /* no merge needed on elements of different types than @nr */ > + if (iter->type != nr->type) { > + list_move_tail(&iter->list, &stack); > + continue; > + } > + > + /* look for the last stack element of same type as @iter */ > + list_for_each_entry_reverse(top, &stack, list) > + if (top->type == iter->type) { > + found = true; > + break; > + } > + if (!found) { Same here. > + list_move_tail(&iter->list, &stack); > + continue; > + } > + > + top_end = top->start + top->length - 1; > + > + if (iter->start > top_end + 1) { > + list_move_tail(&iter->list, &stack); > + } else { > + top->length = max(top_end, iter_end) - top->start + 1; > + list_del(&iter->list); > + kfree(iter); > + } I wonder if the body of the outer list_for_each_entry_safe loop would be a bit nicer in a helper, but again that is probably just down to personal preference.