From patchwork Thu Feb 9 13:56:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Tyshchenko X-Patchwork-Id: 9564621 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7B149601C3 for ; Thu, 9 Feb 2017 13:58:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6672C28499 for ; Thu, 9 Feb 2017 13:58:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5B2E2284EB; Thu, 9 Feb 2017 13:58:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=no version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E60C828499 for ; Thu, 9 Feb 2017 13:58:43 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cbpFC-0005Y2-19; Thu, 09 Feb 2017 13:58:42 +0000 Received: from mail-lf0-f65.google.com ([209.85.215.65]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cbpF8-0005V4-9v for linux-arm-kernel@lists.infradead.org; Thu, 09 Feb 2017 13:58:40 +0000 Received: by mail-lf0-f65.google.com with SMTP id x1so433732lff.0 for ; Thu, 09 Feb 2017 05:58:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=v4Y6oatdAFOw3Hh+ITSvRKONUMaWGxQrz9aQ2JNxcSU=; b=bAFcxXa5Pu58fgwnGii4sQ8fYrChrysbfQucvn/9bQkCW8R05/D5YqxIH3MPHgeP9E f1EEtvpqlVNAX+eLvA1ZPEyjRrfz0QRg0hYnOoUTKgiTRP7HCnqGU379Z4lnRFW8FyKd Z7CE433ibvs8R1Hu+W8GXtWs8JD/M/jcyqES3aGbmJPNoYYOfS4Ho+lRdjEYEpIo5kSu c7+trrve5KjDdD84Ik1u6qDii4PZgxiZ93w+QQAH/P5LBCbXBToMbey+txtLZotFrskD waorqH89W6HMsCmIqhR+ZNGwPIPsPYH+JsCvzXV4MVxNczskK8nd6dxFqopsZDZQHwri 0Rrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=v4Y6oatdAFOw3Hh+ITSvRKONUMaWGxQrz9aQ2JNxcSU=; b=nuemyKXnvgIazvAPvEu78rbXykbdEmK/6h6lGcsXjyPxqfDQWPi5E/4xTEujvOpoqc /YNEe3YaDVOpFeFLcyGGkc40PzJo/vmijY3FvsX3BT5rCFQHEZ6jCGPHwUuq+hRgz6pH DIdqbM2+0XYSAqM4rZ4jVowV75gZOc7k9ZvYvvcUWzRJk2aTiQDcz9W8HoZPwNG7MwmK 72/HNg2+Gr0iFbfw0IFCtLspb/PiapgcdGB8KwuaZfkpKViVho8xI2Yx3jrPiboIh9pQ izXz2Mj0LTELT0lM75wrVan195wOey1Kkxm/BSMWKSxvspgQ3Mtw+NUayIFiQW5HLcyR BnDw== X-Gm-Message-State: AMke39k4eXdUVJhnxtoeMD2RQl3pOa8dJiF4HZnlVO/t3msDfKK1QvGnwhW3nb+7d5guYA== X-Received: by 10.25.203.3 with SMTP id b3mr1199304lfg.130.1486648635566; Thu, 09 Feb 2017 05:57:15 -0800 (PST) Received: from otyshchenko.kyiv.epam.com (ll-74.141.223.85.sovam.net.ua. [85.223.141.74]) by smtp.gmail.com with ESMTPSA id m18sm3504519lfe.45.2017.02.09.05.57.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 09 Feb 2017 05:57:14 -0800 (PST) From: Oleksandr Tyshchenko To: linux-arm-kernel@lists.infradead.org, iommu@lists.linux-foundation.org Subject: [RFC PATCH v1] iommu/io-pgtable-arm: Check for leaf entry right after finding it Date: Thu, 9 Feb 2017 15:56:40 +0200 Message-Id: <1486648600-27457-1-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170209_055838_563023_2E49EA70 X-CRM114-Status: GOOD ( 14.06 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Oleksandr Tyshchenko , vlad.babchuk@gmail.com, joro@8bytes.org, will.deacon@arm.com, andrii.anisov@gmail.com, andr2000@gmail.com, al1img@gmail.com, robin.murphy@arm.com, joculator@gmail.com MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Oleksandr Tyshchenko Do a check for already installed leaf entry at the current level before performing any actions when trying to map. This check is already present in arm_lpae_init_pte(), i.e. before installing new leaf entry at the current level if conditions to do so are met (size == block_size). But, this might be insufficient in case when we have already installed block mapping at this level and it is not time to install new leaf entry (size != block_size). In that case we continue walking the page table down with wrong pointer to the next level. So, move check from arm_lpae_init_pte() to __arm_lpae_map() in order to avoid all cases. Signed-off-by: Oleksandr Tyshchenko --- I hope that following actions can help to catch it: 1. Call iommu_map for a block mapping (e.g. 2M) at some address (e.g. iova 0x80000000 pa 0x80000000). 2. Call iommu_map for a page mapping (4k) at some address from the previous mapped region (e.g. iova 0x80008000 pa 0x90000000). I understand that after iommu_map should be iommu_unmap, but different scenarios may occur). --- --- drivers/iommu/io-pgtable-arm.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c index f5c90e1..ebdb82f 100644 --- a/drivers/iommu/io-pgtable-arm.c +++ b/drivers/iommu/io-pgtable-arm.c @@ -272,11 +272,7 @@ static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, arm_lpae_iopte pte = prot; struct io_pgtable_cfg *cfg = &data->iop.cfg; - if (iopte_leaf(*ptep, lvl)) { - /* We require an unmap first */ - WARN_ON(!selftest_running); - return -EEXIST; - } else if (iopte_type(*ptep, lvl) == ARM_LPAE_PTE_TYPE_TABLE) { + if (iopte_type(*ptep, lvl) == ARM_LPAE_PTE_TYPE_TABLE) { /* * We need to unmap and free the old table before * overwriting it with a block entry. @@ -315,6 +311,13 @@ static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, /* Find our entry at the current level */ ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); + /* Check for already installed leaf entry */ + if (iopte_leaf(*ptep, lvl)) { + /* We require an unmap first */ + WARN_ON(!selftest_running); + return -EEXIST; + } + /* If we can install a leaf entry at this level, then do so */ if (size == block_size && (size & cfg->pgsize_bitmap)) return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);