From patchwork Tue Mar 21 08:31:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Tesarik X-Patchwork-Id: 72758 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp1661379wrt; Tue, 21 Mar 2023 01:47:26 -0700 (PDT) X-Google-Smtp-Source: AK7set+RT1aP/nP9YP3qoWYkPLAdp7Tu51fK7ynDa/LHfke2JkWHV1Kl4Wf1Xj1A9J+tO9iCuwwb X-Received: by 2002:a17:90a:45:b0:23a:8f25:7fd6 with SMTP id 5-20020a17090a004500b0023a8f257fd6mr1822362pjb.29.1679388445790; Tue, 21 Mar 2023 01:47:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679388445; cv=none; d=google.com; s=arc-20160816; b=iG8QkTkQOidcRTWLKJCl9HaQDew3h/Vf5diUNaLnP4eMqkw7jUtFNs0DXIfTYTXDIR bNNVwaKudRlVfp/NxLi/9e38+ZQxr2aMtUgFcOSuSTGzrDVrdHjYdiZ+0wt/eUDVFseF ejkpNETqEh9Sc/SyEBBr0SucRbONjfgqKbJUU2mT5IjCLU/fxYWSLEfxMjxrHtffqzfh wi5Zm1CJFACMW9F5cDrDDEPGY9LMAP/qyMXDKGc+DsYz0yApKVCWYtqNQTkj1c1q7LMz a3fbOWrd2MdrZ4epmpw8wrla9NX3C5ASHUNdhCSQwMdOjXSU8z03UfDZc3WZWAXSZRjw qnDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=wCHf6Vl/DiOs0YULVS18Pfdiu2CU7RdKGztoG6GXQyw=; b=E3wRCAUie2F/Es+v8AnX6UPfc+6m9kSGjlZ1TdMETaIoMCVAeFFUG+m3+ayQU14Xlj Bi2m+qgYcZ8a6/p9vd8IhxL1RdiWx+nu9dSAfnhWlOyxTfYxzVLIObZPysMQoy+9iNwB rS2k3NrCX5BzfmzqZCQR08NJ9MSZCAxwn8IBeBUwHoEAV0o9idWzFxpXMcl3ilUMFeLU ztfgKSG+gUPKmGr0rc5e4axZF7vb6NAuH8D9m5ToiIprunygfnA1VZdfFOjwPLOl4uHt aLiWjhQPM2+CEPQ1XiNNEMLhA0988neXSFowOwnY3xWobIp3j+lCDh46Pl4y4gjefdP0 8ITw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v20-20020a17090a899400b0023ef9514233si12317085pjn.116.2023.03.21.01.47.10; Tue, 21 Mar 2023 01:47:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230095AbjCUIdn (ORCPT + 99 others); Tue, 21 Mar 2023 04:33:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229992AbjCUIdP (ORCPT ); Tue, 21 Mar 2023 04:33:15 -0400 Received: from frasgout12.his.huawei.com (frasgout12.his.huawei.com [14.137.139.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDD1618A92 for ; Tue, 21 Mar 2023 01:32:28 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.18.147.227]) by frasgout12.his.huawei.com (SkyGuard) with ESMTP id 4Pgl320Ftzz9v7Yh for ; Tue, 21 Mar 2023 16:22:38 +0800 (CST) Received: from A2101119013HW2.china.huawei.com (unknown [10.48.151.252]) by APP2 (Coremail) with SMTP id GxC2BwBHHGNkaxlkK1q2AQ--.49312S3; Tue, 21 Mar 2023 09:31:55 +0100 (CET) From: Petr Tesarik To: Christoph Hellwig , Marek Szyprowski , Robin Murphy , Konrad Rzeszutek Wilk , Jianxiong Gao , David Stevens , Joerg Roedel , iommu@lists.linux.dev (open list:DMA MAPPING HELPERS), linux-kernel@vger.kernel.org (open list) Cc: Roberto Sassu , petr@tesarici.cz Subject: [PATCH v1 1/2] swiotlb: Use wrap_area_index() instead of open-coding it Date: Tue, 21 Mar 2023 09:31:26 +0100 Message-Id: <5c7da4c21a8ba72422567dd157533d062b75024c.1679382779.git.petr.tesarik.ext@huawei.com> X-Mailer: git-send-email 2.21.0.windows.1 In-Reply-To: References: MIME-Version: 1.0 X-CM-TRANSID: GxC2BwBHHGNkaxlkK1q2AQ--.49312S3 X-Coremail-Antispam: 1UD129KBjvdXoWruw1fJw15GF18Ww13AFyxXwb_yoW3KFg_ZF WxXrWv9r4DZr4xZr1xur4ruF4vqw4SgFyfu345XFWUJw1UZr95Z398WF1ktrZxWF1xWF90 vr98Wr4DtF1agjkaLaAFLSUrUUUUjb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUIcSsGvfJTRUUUbHxYFVCjjxCrM7AC8VAFwI0_Wr0E3s1l1xkIjI8I6I8E6xAIw20E Y4v20xvaj40_Wr0E3s1l1IIY67AEw4v_Jr0_Jr4l82xGYIkIc2x26280x7IE14v26r18M2 8IrcIa0xkI8VCY1x0267AKxVW8JVW5JwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK 021l84ACjcxK6xIIjxv20xvE14v26r1j6r1xM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r 4j6F4UM28EF7xvwVC2z280aVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv6xkF7I0E14v26r4j 6r4UJwAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7V C0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j 6r4UM4x0Y48IcxkI7VAKI48JM4IIrI8v6xkF7I0E8cxan2IY04v7MxkF7I0En4kS14v26r 1q6r43MxkF7I0Ew4C26cxK6c8Ij28IcwCF04k20xvY0x0EwIxGrwCF54CYxVCY1x0262kK e7AKxVWUtVW8ZwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI 8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41l IxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIx AIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2 jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07jxsqXUUUUU= X-CM-SenderInfo: hshw23xhvd2x3n6k3tpzhluzxrxghudrp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760966419113271125?= X-GMAIL-MSGID: =?utf-8?q?1760966419113271125?= From: Petr Tesarik No functional change, just use an existing helper. Signed-off-by: Petr Tesarik --- kernel/dma/swiotlb.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 91454b513db0..3856e2b524b4 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -695,10 +695,7 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, /* * Update the indices to avoid searching in the next round. */ - if (index + nslots < mem->area_nslabs) - area->index = index + nslots; - else - area->index = 0; + area->index = wrap_area_index(mem, index + nslots); area->used += nslots; spin_unlock_irqrestore(&area->lock, flags); return slot_index; From patchwork Tue Mar 21 08:31:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Tesarik X-Patchwork-Id: 72757 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp1660403wrt; Tue, 21 Mar 2023 01:44:36 -0700 (PDT) X-Google-Smtp-Source: AK7set/KwB8CMI5Onnsh2mGCqtMCn+4xJDLR6PxxWnbT/MLQHdq/iV2Eu8bFuRJsdLfHOGd0Ysoz X-Received: by 2002:a05:6a20:66b0:b0:d9:63c3:e299 with SMTP id o48-20020a056a2066b000b000d963c3e299mr1389615pzh.8.1679388276337; Tue, 21 Mar 2023 01:44:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679388276; cv=none; d=google.com; s=arc-20160816; b=DtGs9gN4ujj6jy7rHp0nV6I93yPXhNyHcquJ9V69EWpn5EfDSiKf5XMCJ7cVsNUgvI pMkoZAy09vhu5nm4dZyhqJEcalrnAa1/S4TP4T+ew1nilJdJNaPA+npEubmdTjDEykVe D3W5/mZVTmvzqgzbSp6RgHSfA7OKSyfM2xsXSB0mz/bAcJg/+kqv8mOJXkJoLMWypSLJ v54nozR1Bd/3TSv4UtL3xYhSz88R1O1OrfE1pWViKhcQLeWwwj1FL1n37hUqDRvhS8kF /ITzA5aEAIbxt3+eKBqfcUzXHrK713aKhIUoUzIA9pepJSTE7FdzoOq5pGZaaJLnz047 jEdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=qVx6+3/IG8gEGW638m+nF+Ab0z2LauQq3yM6CcDGrcI=; b=BRQ68CPVjjbtDtFx8QwGH2c9q0VgYIGzKAg7LZ/Y7lEDlqrimIetl14X+wmwpi6gOK 4hfiS8XCqAoLs9PDCLq+h4MeGDOfuQaZUc8LE1JNR2MZIrkB1a5v7oKZFpO94LPH8viK 4WV/z1Y1LBXN31N/rtQVIUsV8bf1HzintAQ/4Vl+RCulCEUbzTF8iJT6CK3ZAS6IG/vr v3ebRCDZ5Fs6n1H4YKihqh50pTaW0ru5eLmuZgFQQsBFN/tPF42t/F2GenLZ68BZvWDM V7d55teind5ziQM4lONPJ3JAOKVurFXQkFzeT7KSdJAttR1axsaW6hmAljAvMd4rQ1uD ULjw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x189-20020a6286c6000000b005a5c5914895si11738208pfd.262.2023.03.21.01.44.22; Tue, 21 Mar 2023 01:44:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230347AbjCUIeI (ORCPT + 99 others); Tue, 21 Mar 2023 04:34:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjCUIdc (ORCPT ); Tue, 21 Mar 2023 04:33:32 -0400 Received: from frasgout13.his.huawei.com (frasgout13.his.huawei.com [14.137.139.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 988AC460A3 for ; Tue, 21 Mar 2023 01:32:36 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.18.147.227]) by frasgout13.his.huawei.com (SkyGuard) with ESMTP id 4Pgl3x5Vfcz9v7Yh for ; Tue, 21 Mar 2023 16:23:25 +0800 (CST) Received: from A2101119013HW2.china.huawei.com (unknown [10.48.151.252]) by APP2 (Coremail) with SMTP id GxC2BwBHHGNkaxlkK1q2AQ--.49312S4; Tue, 21 Mar 2023 09:32:02 +0100 (CET) From: Petr Tesarik To: Christoph Hellwig , Marek Szyprowski , Robin Murphy , Konrad Rzeszutek Wilk , Jianxiong Gao , David Stevens , Joerg Roedel , iommu@lists.linux.dev (open list:DMA MAPPING HELPERS), linux-kernel@vger.kernel.org (open list) Cc: Roberto Sassu , petr@tesarici.cz Subject: [PATCH v1 2/2] swiotlb: Fix slot alignment checks Date: Tue, 21 Mar 2023 09:31:27 +0100 Message-Id: X-Mailer: git-send-email 2.21.0.windows.1 In-Reply-To: References: MIME-Version: 1.0 X-CM-TRANSID: GxC2BwBHHGNkaxlkK1q2AQ--.49312S4 X-Coremail-Antispam: 1UD129KBjvJXoWxWryrKw13Wr1fXw1kCryxuFg_yoW5XF4fpF yfWrnYqFWDJF18Aayjka4kWF4F93s7Gay3GF4Yg343ZrykJF9akF9rKF1YqFyFgr4kCFW7 uF1aqa10vw1UZ37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUQCb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUXw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWUJVWUCwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8JVWxJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8 JVW8Jr1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx 0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWU JVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x02628vn2kIc2xKxwCY1x0262kKe7AKxV WUtVW8ZwCY1x0264kExVAvwVAq07x20xyl42xK82IYc2Ij64vIr41l4c8EcI0Ec7CjxVAa w2AFwI0_Jw0_GFyl4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGw C20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48J MIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMI IF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E 87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUc5l1DUUUU X-CM-SenderInfo: hshw23xhvd2x3n6k3tpzhluzxrxghudrp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760966241476704176?= X-GMAIL-MSGID: =?utf-8?q?1760966241476704176?= From: Petr Tesarik Explicit alignment and page alignment are used only to calculate the stride, not when checking actual slot physical address. Originally, only page alignment was implemented, and that worked, because the whole SWIOTLB is allocated on a page boundary, so aligning the start index was sufficient to ensure a page-aligned slot. When Christoph Hellwig added support for min_align_mask, the index could be incremented in the search loop, potentially finding an unaligned slot if minimum device alignment is between IO_TLB_SIZE and PAGE_SIZE. The bug could go unnoticed, because the slot size is 2 KiB, and the most common page size is 4 KiB, so there is no alignment value in between. IIUC the intention has been to find a slot that conforms to all alignment constraints: device minimum alignment, an explicit alignment (given as function parameter) and optionally page alignment (if allocation size is >= PAGE_SIZE). The most restrictive mask can be trivially computed with logical AND. The rest can stay. Fixes: 1f221a0d0dbf ("swiotlb: respect min_align_mask") Fixes: e81e99bacc9f ("swiotlb: Support aligned swiotlb buffers") Signed-off-by: Petr Tesarik Tested-by: Dexuan Cui --- kernel/dma/swiotlb.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 3856e2b524b4..5b919ef832b6 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -634,22 +634,26 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, BUG_ON(!nslots); BUG_ON(area_index >= mem->nareas); + /* + * For allocations of PAGE_SIZE or larger only look for page aligned + * allocations. + */ + if (alloc_size >= PAGE_SIZE) + iotlb_align_mask &= PAGE_MASK; + iotlb_align_mask &= alloc_align_mask; + /* * For mappings with an alignment requirement don't bother looping to - * unaligned slots once we found an aligned one. For allocations of - * PAGE_SIZE or larger only look for page aligned allocations. + * unaligned slots once we found an aligned one. */ stride = (iotlb_align_mask >> IO_TLB_SHIFT) + 1; - if (alloc_size >= PAGE_SIZE) - stride = max(stride, stride << (PAGE_SHIFT - IO_TLB_SHIFT)); - stride = max(stride, (alloc_align_mask >> IO_TLB_SHIFT) + 1); spin_lock_irqsave(&area->lock, flags); if (unlikely(nslots > mem->area_nslabs - area->used)) goto not_found; slot_base = area_index * mem->area_nslabs; - index = wrap_area_index(mem, ALIGN(area->index, stride)); + index = area->index; for (slots_checked = 0; slots_checked < mem->area_nslabs; ) { slot_index = slot_base + index;