From patchwork Sat Mar 25 17:53:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael Kelley (LINUX)" X-Patchwork-Id: 74963 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp543260vqo; Sat, 25 Mar 2023 11:07:48 -0700 (PDT) X-Google-Smtp-Source: AKy350afzDzW/uQXOf3+HIB9HbMQNUFvaC7cwDljgb0SJHGz+7LNp7YPRT7bYOqE9CVC8A2Lics8 X-Received: by 2002:a05:6402:1141:b0:4fc:88d8:a031 with SMTP id g1-20020a056402114100b004fc88d8a031mr7734704edw.28.1679767668175; Sat, 25 Mar 2023 11:07:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679767668; cv=pass; d=google.com; s=arc-20160816; b=jTDZRlgqTjtNt38tpbKhPvr63TLCu79Vx0kv7++AcRbxnNZ6dZlQbOPsGbnDVhxsNO c66/0GVR7DZkMSDLX2fzxyS2AmLGK5S+YtIngQwSQYlqEZXQFSp9YNRvPmFr0SskNLJW SYugdwBdp70L+gqlgaJetIe7NR1CufJ5yXkVvzPKtXJ45mTd98ND234JAT0HW6klktMw QQRJN7xcUIgx4WBiC+kaGvmibVg/Cxn2igbCRHD7FQX4wpX7CailrAjjyEL2PDHPiUN6 QLUzcLu1lKTlBqs55p7/0k/G5S88zStG9S6/MWZEHxccYi5XUiNlcwUoPtP6bSQP3CcI oXEA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from :dkim-signature; bh=UJ4TDw78HgpYLN6SMzdVuCCWaip9zjb3EAurMhRfIow=; b=fgLAPJxOT5CpahN90Su7rBFSOaijKjkW//fRf9fY+wgIoYfxqfEE8u/14Z5Xoun5C2 rjQUaSuigqrcxqEJl/lPx1SvjqiZMlDOKo5OYQy/EyCqrTmISsiXO4bBNUgBynRf6JhV rSmDuGbnzuoRdGG2xsoltwKDsWTf4kf95sjlVBygAFN1rhwDi0wUWAJ9rbVmbg2wC5FS +MF+H7UTxludIao8L4i2gS3RvwrkkT2Z+LY9idF7dpIfBnYJmHVImH8nNvcMThC36y8R DRzPFeSJ8Gv3UCFrUy+DDV4u1t5a+1uz7WCchgBLkU8dUzij+DQ4B1YYd3HOrdebcaLF U64w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector2 header.b=ixyQFAvg; arc=pass (i=1 spf=pass spfdomain=microsoft.com dkim=pass dkdomain=microsoft.com dmarc=pass fromdomain=microsoft.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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d1-20020a056402516100b004fb6e2b5aacsi24061394ede.582.2023.03.25.11.07.22; Sat, 25 Mar 2023 11:07:48 -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; dkim=pass header.i=@microsoft.com header.s=selector2 header.b=ixyQFAvg; arc=pass (i=1 spf=pass spfdomain=microsoft.com dkim=pass dkdomain=microsoft.com dmarc=pass fromdomain=microsoft.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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231510AbjCYRyE (ORCPT + 99 others); Sat, 25 Mar 2023 13:54:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229952AbjCYRyC (ORCPT ); Sat, 25 Mar 2023 13:54:02 -0400 Received: from DM5PR00CU002.outbound.protection.outlook.com (mail-centralusazon11021020.outbound.protection.outlook.com [52.101.62.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B095BE190 for ; Sat, 25 Mar 2023 10:54:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nOoMn/MCxjiap1g9vzszfTBbHKt4TTG2dtrzCEQAdGc8mXjCQ1mTJygaZA/8ktJ5at8n9YHaLUPdRiFwrOEQLwg8KxyjOIxOCIdE6PYikmmNmjXR9ek/MRfuQwKVLuen0BN3l0jVI2NdvpOFiV0JzoZN+imldbhLhMfmLe1uy+gyp6MzedZPt0mt57GNB3OwCqb6IK5c9eVXi9/8PKorLgB68g6HBm7OWeV/4mvHGvceHzk5le9DWD+CgA0jw8Rhv0+CFBKW3n71SN41g30m/Mkk/l/TdcU1I7NFdO1BXHrGxqcfNfkilY6BzU5mCoMXyPvsLZ/fWGpFR5/bPFFc7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=UJ4TDw78HgpYLN6SMzdVuCCWaip9zjb3EAurMhRfIow=; b=LLfH0z1nEofILlZVE3xmVgHKNnD/aAwkMElaiekTLkzPdTRy3pHZ14LibKhC1+TfjZzXQnmFZwhddTxeuNhWxcHu4ZpjExGY+GgxpVjcWk4G3fepHjnoSmSD3cvZU0LyxJgr66V9+nzRFcsuDNZL7vTyDPSiQhUoVRHVs8NBgDQ/vmh2XrEl7COSBXXH7RbEGMdKcURhFUiYny+armyZ56FdLI5YH4b4/dxdjRUu/zaZhSPraCjfubm/Tr8t+RSwvGq5xYKNMcDs9mLCjb5a9BAvG3F8mtJh31pXzS6LpjQ7ROpbNe5Z5WDKNRQtpPvhBo8Q/DQJ7vNQK98MnomYXg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=microsoft.com; dmarc=pass action=none header.from=microsoft.com; dkim=pass header.d=microsoft.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UJ4TDw78HgpYLN6SMzdVuCCWaip9zjb3EAurMhRfIow=; b=ixyQFAvgF5BztXtZ1NZku3LT5lr95pnuTtASBd4hLD2NAiraDdgdHgDCfiUBAFFkJ2ftP+oO19hyWvqBkPrByxVe+XSr4vnR8+1C9YTbIkIoYcRPkXuXu4l7B8lJsThNuJaokEeSN6w2jCVnLyMl0pSGiOS+95AC/SI0d1fQ3y4= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=microsoft.com; Received: from DM6PR21MB1370.namprd21.prod.outlook.com (2603:10b6:5:16b::28) by BYAPR21MB1336.namprd21.prod.outlook.com (2603:10b6:a03:115::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.9; Sat, 25 Mar 2023 17:53:57 +0000 Received: from DM6PR21MB1370.namprd21.prod.outlook.com ([fe80::b7e9:4da1:3c23:35f]) by DM6PR21MB1370.namprd21.prod.outlook.com ([fe80::b7e9:4da1:3c23:35f%3]) with mapi id 15.20.6254.009; Sat, 25 Mar 2023 17:53:57 +0000 From: Michael Kelley To: hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, decui@microsoft.com, tiala@microsoft.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org Cc: mikelley@microsoft.com Subject: [PATCH v2 1/1] swiotlb: Track and report io_tlb_used high water mark in debugfs Date: Sat, 25 Mar 2023 10:53:10 -0700 Message-Id: <1679766790-24629-1-git-send-email-mikelley@microsoft.com> X-Mailer: git-send-email 1.8.3.1 X-ClientProxiedBy: MW4PR03CA0085.namprd03.prod.outlook.com (2603:10b6:303:b6::30) To DM6PR21MB1370.namprd21.prod.outlook.com (2603:10b6:5:16b::28) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR21MB1370:EE_|BYAPR21MB1336:EE_ X-MS-Office365-Filtering-Correlation-Id: 75182aaa-b389-4fb6-c8cb-08db2d59e824 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: n/Vvmqjkv6igfBsYOTG0yUN4OY8KY1rO8qr3MpskAxWPWtFBi3q5HiL4zYWos2vl4scUZP9cB8FgnTgNzqvr7geJ0Q5Abn0LEEbLBQmpqJE4IbZOi8+LMEfjbq6hERKcSl1mvxrnquwmkb694dz2ub/J8hzKDmxlP6GZA4ZiIuG6uLjWbZGK5GjFI2y2prri2Uy8HBgzOz8hBRnee90rfJ1ZNPgZbuxWZ51pjmG6fqAeGMfeDujIBREV6TzT7IHvr640tAVAiTlYfzX8IYZjdEYaYEN/3CoXpxu+3CZ9/IzYGlmjwVlt6rLc3NJgolPp19TrlyJHe+EEvhL7vGCpnJkQiAhOBDTzGZC+twLBrpoifkwltxcmh+XOggXqsEA8UUdpweIOG+bVFJ0haxaF+NuH/FV7MXD9TuOpRyT+s437GipoqONJ2tOcMaBVpvPTOmkHP8qp/kyZjd/lucTOH5UFgcwgmLAwc82wNTKjvmzcIskWtJH9od5QQGeh5NzgJV40DXUVK2KnDKfa1w/UtqURHSA+6FDxjAAZoqCJN8VjTLGsG/bATf8PcxACBcjJSMALHsWBZz05L9YP4PzWz+cKybth5BzswbRFwew9iBQ3Xw4v2K8bSoCdq5/xUsrTn0szjA5J4FeKpTUbI29fSOe97SQ3am3DrlYIwOMDSvs= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR21MB1370.namprd21.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(346002)(376002)(366004)(396003)(136003)(39860400002)(451199021)(82960400001)(38100700002)(38350700002)(86362001)(36756003)(83380400001)(66946007)(6506007)(52116002)(26005)(82950400001)(186003)(41300700001)(2906002)(8676002)(5660300002)(6666004)(8936002)(4326008)(66556008)(107886003)(2616005)(478600001)(66476007)(10290500003)(6486002)(6512007)(316002);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +oE9kEHd9kYy4umdOTif+oKIWLqeILygSxrIV8ldH64XcHKkEqDq9vXSFkPg/0ONWisiSJOUmiNBM/p+RtWUw1R6Ao/lUDykqROOefEtByd8xdcX8GKm/OOvLoqz0fMJtsKUEDjdUD/oDQNBPEBsNSNzNiGcuYNV1ArYgfOB6FnfOfNPB7QDVJWlyCTbSD89Zq77QmBORsTA5TgVvlW3oE1VDaocENnHlzXJEY7GoRdrG7O29JSWDfVNcVCCampidQXGvwYe44b2HakzxZ6/GPoBg0ZuxqRA3c5pOpGrdiKcDNLA4FqcgDuXrBfBhCy9Et1x1eu4amPF3bcq8XFjPTJtSEWlXx29r9KHpf3+O3QxtRu+9HRjnhDqyRIhYYlWSGcaWfrXg+c5EkvS80/oGbVSGCp9zuEdMPtJUFVRvQzoJi89CZOq2RBkFDbHmTzNhBZPJk4T+0soRy0dEGsOg3RtfAp7fqStL9PMBP13ksAaGBSBVAy+zOeFXNnnGpCxe3Vnl7t+gwQ5Qc+pHRVJ9wG+onDgwIItBlo2l9cCv89Fi66G9DgowMakSksgGqS8n3C7S+waJF0S4cKzEZGu31hNfyf+KX8qE92EsePDtQRrD4oEt0D1ZPibUKzBGPoh+QqxGvw6cyb51fTgQzekJqhzsT3tXdlhDAMVqM3eMOR+Faw437WUiBBgQg/ZVz1iD7EQYqh9aLSJOU8h7Md9l3TcdKhqKdMizpcMdmqH+I3HL/N7jYysAcF19KHyeBUd6hfv5Kvn/sGoyRq4mBzyQrF31RgePO1/m1YRwtVyFXRrjjPaHhu+RItIgwP7qLvk5Ny0oM4Mq9crFVQb24PDDb0D+OO+PbsEpwQNffCVKcsRQ7I7FNMcj0Hpw5hdbMrC/jphq1F038WW5W3jli1+aUWXGDn/P6zbXIYF5VzdFyYPCqnIv2fsohighcESMNa7VHvHE9wzEPgREx1ThPOhqM91EjHdeu80V4axCjvn+exZvJ76rRguz65pzNzb7Htn0SYZE5+RvHGCWBXkPDIOlWOqU5PIDABbKirOiwoXWfkQHH/5D7HvY9xZ58eMNxiW5AA8rVMNIumsBItn/ABtNidyP+jIe6Urp1y93FUvB6KTcQaj5VaCxcm+Oa0pt5Z4GJ6Cz90UfX9TxChD1BCO0ecISmOmrZ2kdnriVLDBr5zLhEyaHtaQciIAf6MYdtwmaOs5kftiw4Y/emFsWtkvxRmZ4qYkfRoRrRLe1oN0NwBFiKOgBMMmHmpyj/ERQSK+AnzgC8YWV9aeydYpSKxqRSysOkMwhmFyONu2h2990Qtl9PiXGUBt7yP8NtOewU+VsQ+FzGx4MOUOsYVVct/CpAZj/ArXGy2Py/ZVsnvVIxaczudmZkZGJftM3BOAVpf3xZfHAAQPsuSwtbSV0pJZzCiBW/wm6AKbUZcAGyGUiB7bN6jL9XAb0SYPVA29e64eST+2TG8+YZoIczpXrGHLTG5ovAL7/pqU6aDozsojTxDXXIPDYKgma+gkhfCJnIN0Q7XMHvBEg4w3Bf/QfXNLzFarBOwoHjCoWDYbAdQDjt4cBY1e/kkffUHlKQZmgXSi X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 75182aaa-b389-4fb6-c8cb-08db2d59e824 X-MS-Exchange-CrossTenant-AuthSource: DM6PR21MB1370.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Mar 2023 17:53:57.4507 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: uvP2FsYtncDPnL4lw930SYpTcIqekCOilpZy70JwFbjCpjMkMhC/eQyaSTa3OBIYcuZpiZkKDXdRwu9TW81b9Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR21MB1336 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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?1761364062278980344?= X-GMAIL-MSGID: =?utf-8?q?1761364062278980344?= swiotlb currently reports the total number of slabs and the instantaneous in-use slabs in debugfs. But with increased usage of swiotlb for all I/O in Confidential Computing (coco) VMs, it has become difficult to know how much memory to allocate for swiotlb bounce buffers, either via the automatic algorithm in the kernel or by specifying a value on the kernel boot line. The current automatic algorithm generously allocates swiotlb bounce buffer memory, and may be wasting significant memory in many use cases. To support better understanding swiotlb usage, add tracking of the the high water mark usage of swiotlb bounce buffer memory. Report the high water mark in debugfs along with the other swiotlb metrics. Allow the high water to be reset to zero at runtime by writing to it. Since a global in-use slab count is added alongside the existing per-area in-use count, the mem_used() function that sums across all areas is no longer needed. Remove it and replace with the global in-use count. Signed-off-by: Michael Kelley Changes in v2: * Only reset the high water mark to zero when the specified new value is zero, to prevent confusion about the ability to reset to some other value [Dexuan Cui] Reviewed-by: Dexuan Cui --- kernel/dma/swiotlb.c | 49 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index f9f0279..3e50639 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -76,6 +76,9 @@ struct io_tlb_slot { static unsigned long default_nslabs = IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT; static unsigned long default_nareas; +static atomic_long_t total_used = ATOMIC_LONG_INIT(0); +static atomic_long_t used_hiwater = ATOMIC_LONG_INIT(0); + /** * struct io_tlb_area - IO TLB memory area descriptor * @@ -587,6 +590,7 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, unsigned long flags; unsigned int slot_base; unsigned int slot_index; + unsigned long old_hiwater, new_used; BUG_ON(!nslots); BUG_ON(area_index >= mem->nareas); @@ -659,6 +663,14 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, area->index = wrap_area_index(mem, index + nslots); area->used += nslots; spin_unlock_irqrestore(&area->lock, flags); + + new_used = atomic_long_add_return(nslots, &total_used); + old_hiwater = atomic_long_read(&used_hiwater); + do { + if (new_used <= old_hiwater) + break; + } while (!atomic_long_try_cmpxchg(&used_hiwater, &old_hiwater, new_used)); + return slot_index; } @@ -681,16 +693,6 @@ static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr, return -1; } -static unsigned long mem_used(struct io_tlb_mem *mem) -{ - int i; - unsigned long used = 0; - - for (i = 0; i < mem->nareas; i++) - used += mem->areas[i].used; - return used; -} - phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, size_t mapping_size, size_t alloc_size, unsigned int alloc_align_mask, enum dma_data_direction dir, @@ -723,7 +725,7 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, if (!(attrs & DMA_ATTR_NO_WARN)) dev_warn_ratelimited(dev, "swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n", - alloc_size, mem->nslabs, mem_used(mem)); + alloc_size, mem->nslabs, atomic_long_read(&total_used)); return (phys_addr_t)DMA_MAPPING_ERROR; } @@ -791,6 +793,8 @@ static void swiotlb_release_slots(struct device *dev, phys_addr_t tlb_addr) mem->slots[i].list = ++count; area->used -= nslots; spin_unlock_irqrestore(&area->lock, flags); + + atomic_long_sub(nslots, &total_used); } /* @@ -887,10 +891,29 @@ bool is_swiotlb_active(struct device *dev) static int io_tlb_used_get(void *data, u64 *val) { - *val = mem_used(&io_tlb_default_mem); + *val = (u64)atomic_long_read(&total_used); return 0; } + +static int io_tlb_hiwater_get(void *data, u64 *val) +{ + *val = (u64)atomic_long_read(&used_hiwater); + return 0; +} + +static int io_tlb_hiwater_set(void *data, u64 val) +{ + /* Only allow setting to zero */ + if (val != 0) + return -EINVAL; + + atomic_long_set(&used_hiwater, val); + return 0; +} + DEFINE_DEBUGFS_ATTRIBUTE(fops_io_tlb_used, io_tlb_used_get, NULL, "%llu\n"); +DEFINE_DEBUGFS_ATTRIBUTE(fops_io_tlb_hiwater, io_tlb_hiwater_get, + io_tlb_hiwater_set, "%llu\n"); static void swiotlb_create_debugfs_files(struct io_tlb_mem *mem, const char *dirname) @@ -902,6 +925,8 @@ static void swiotlb_create_debugfs_files(struct io_tlb_mem *mem, debugfs_create_ulong("io_tlb_nslabs", 0400, mem->debugfs, &mem->nslabs); debugfs_create_file("io_tlb_used", 0400, mem->debugfs, NULL, &fops_io_tlb_used); + debugfs_create_file("io_tlb_used_hiwater", 0600, mem->debugfs, NULL, + &fops_io_tlb_hiwater); } static int __init __maybe_unused swiotlb_create_default_debugfs(void)