From patchwork Tue Jan 9 02:45:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "zhangpeng (AS)" X-Patchwork-Id: 186196 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:37c1:b0:101:2151:f287 with SMTP id y1csp1407913dyq; Mon, 8 Jan 2024 18:46:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IGXqogA4bGHwVc0x27hlSnvPlYotOiGzVAWtdwR9QLXdMMrdW4mm99qPviqwNcrj9t0cb9u X-Received: by 2002:ac8:5889:0:b0:429:7fbc:5b95 with SMTP id t9-20020ac85889000000b004297fbc5b95mr125313qta.1.1704768373302; Mon, 08 Jan 2024 18:46:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704768373; cv=none; d=google.com; s=arc-20160816; b=wtAviFyxl5ye7TyjvP2Hum9m9E03aG2/D4b3lsrbrYd8JCbFo6hTCB23Llrc4zOn5n S7sGSaPl/kCRa4qawIpELbVO/hKZVWAvNbrUlAGhCZvFKwJOOqw0uwrwLgVeAeDTlUmO 2W1gdnVX4hhCX8XhqN5+iExGhcEPZySc+XYPAwT7PBn11Gje/qSaCaAWAtez3eLQFt1J 1WJ9px1fR08zDtp+Uj2zCf2KA6P8HBJZ08VvNTNBXzIbB/8pKnS2mZkkWZWQAcI4Q2rX rmrx5XVPXeaRMJOP9bvVXPla8xo3VLxk/d+FqVH6SMhMKieWVJcxyca9Ue4eLIkOnHV5 ilYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=1UfHonsI4VEuC3NubZaURhnQkvTv1O/puhLkP9wcq3A=; fh=YV9sm+238rh61pEFohXPzAfeYIwssiQmuyuLZ7d61Ek=; b=GQ1zFN6id/kihGPPYjBPId5/nWjy2NCTyfTUN9mdPZWAzX04A5Do/QzV6bugvpNk8O RfC8plLvYt0hzGTqvSJJNBpCuplo6wFaLqM64BWQakQhLIM8ZSgL+Ytk1unSbY8I2TSP vpthYoGHQ+Hg/oqjQrT+nIawL8o9ZYp7sF2T0dEZf/Gcsj+oKGa6uWJS5T7EZSxmHXJy uVXjzRNeNFiGBhAIkFjNbYx3XwVJWAJn3HFBlppLafLGBO+rPMB3/m5X2jsD93+pulZ/ iqSoXNGeLjCFf4akzg69k1TyvSbqqIjBI6w3Yfmq8Fi4y1oFrSoa1onToqqhJHCnjcXg C22Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-20295-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20295-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id t29-20020a05620a005d00b007832a1c1451si1141702qkt.25.2024.01.08.18.46.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 18:46:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-20295-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-20295-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20295-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 1E4421C233F1 for ; Tue, 9 Jan 2024 02:46:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1B3F1443D; Tue, 9 Jan 2024 02:46:01 +0000 (UTC) Received: from szxga07-in.huawei.com (szxga07-in.huawei.com [45.249.212.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA22B257B for ; Tue, 9 Jan 2024 02:45:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.234]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4T8Fd31BrYz1Q7Pj; Tue, 9 Jan 2024 10:44:23 +0800 (CST) Received: from kwepemm600020.china.huawei.com (unknown [7.193.23.147]) by mail.maildlp.com (Postfix) with ESMTPS id 488B51402CB; Tue, 9 Jan 2024 10:45:54 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by kwepemm600020.china.huawei.com (7.193.23.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Tue, 9 Jan 2024 10:45:53 +0800 From: Peng Zhang To: , , , , , CC: , , Subject: [PATCH v2] swiotlb: check alloc_size before the allocation of a new memory pool Date: Tue, 9 Jan 2024 10:45:47 +0800 Message-ID: <20240109024547.3541529-1-zhangpeng362@huawei.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemm600020.china.huawei.com (7.193.23.147) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787531051470974839 X-GMAIL-MSGID: 1787579202235111212 From: ZhangPeng The allocation request for swiotlb contiguous memory greater than 128*2KB cannot be fulfilled because it exceeds the maximum contiguous memory limit. If the swiotlb memory we allocate is larger than 128*2KB, swiotlb_find_slots() will still schedule the allocation of a new memory pool, which will increase memory overhead. Fix it by adding a check with alloc_size no more than 128*2KB before scheduling the allocation of a new memory pool in swiotlb_find_slots(). Signed-off-by: ZhangPeng Reviewed-by: Petr Tesarik --- v1->v2: - Rebased on for-next branch per Christoph Hellwig - Added RB from Petr Tesarik kernel/dma/swiotlb.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 97c298b210bc..b079a9a8e087 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -1136,6 +1136,9 @@ static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr, int cpu, i; int index; + if (alloc_size > IO_TLB_SEGSIZE * IO_TLB_SIZE) + return -1; + cpu = raw_smp_processor_id(); for (i = 0; i < default_nareas; ++i) { index = swiotlb_search_area(dev, cpu, i, orig_addr, alloc_size,