Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2153424ybv; Fri, 14 Feb 2020 12:31:26 -0800 (PST) X-Google-Smtp-Source: APXvYqxPlV4T5h114K/mI1yTUufDx0s9pyNZKn5Fy1pqA0guxO0NdmJecPB/y7nXNEA6CtCTDRtK X-Received: by 2002:aca:f305:: with SMTP id r5mr3198863oih.174.1581712286729; Fri, 14 Feb 2020 12:31:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581712286; cv=none; d=google.com; s=arc-20160816; b=dXR0Quo/YQAD6+fyiPX2wclgaL6takFfHj9Cons9HIka450DUginIXVMUphCM3co8U BIXv/syUs8z9jcpenZ4sx1R3nJXTJwCsUyg8OvBUNSPetq/QCshrnqN7ZCuJhK8V68Hm /mEZbG0W0OdUpwSCKHFXbLvlqRB5S+pW7tAhIbYVlUlRYYgZzz8cZPOiF4GDaLAfsuSZ v+nuJ2H2U/KRA+Pm0mBllwpf19gWkMTP/3J3ZQSDVg/bJujddIEmvCldZyccx+RfFSNB Ml5jnIRS7LFDfxSf26obXFCYzzqiZ0e4C5/9KH+SV5SOZS9JjiqVtfUndfktlW7P93G9 Ij1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:message-id :subject:cc:to:from:date:dmarc-filter:dkim-signature; bh=h/tkltk20e29nTfkQPxEvZyxDnAN8IM+C5401aPk9gc=; b=XgVKxgHWOk/bK3zp40WlIqxOu6rdJZ1uDyGTDPayn5KfOdZSSDZOffzq6ssMyKWm61 zneLgR3pVBbGyaRDCT5xZYzb2Kl2eMwTtnvu0hPbCcKpKardtbTTDVcvu5fgPx5KPueS /xs5YILMR1IS7LNXK6YCqDrYM9XMg7EiWUm9j4IHGwhYQO9LQcyFK8jWN/a7923mRw3w 7vl1U878QwTeAdREh5iPmFHmC4spAOomr9eUwQmqfiWnlCTWt4huQjQUO9LGGqOq6o/y uGbwNN+MZhHDlq34I5xbAKtInEsDZZ3wC1FI53NbvKLdGukUIip2iIXMbJctzfDucluH nCBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mg.codeaurora.org header.s=smtp header.b=gdBVkjkr; 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 i15si3547699otk.120.2020.02.14.12.31.14; Fri, 14 Feb 2020 12:31:26 -0800 (PST) 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=@mg.codeaurora.org header.s=smtp header.b=gdBVkjkr; 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 S1730008AbgBNUbE (ORCPT + 99 others); Fri, 14 Feb 2020 15:31:04 -0500 Received: from mail27.static.mailgun.info ([104.130.122.27]:11562 "EHLO mail27.static.mailgun.info" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729682AbgBNUbE (ORCPT ); Fri, 14 Feb 2020 15:31:04 -0500 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1581712263; h=Content-Type: MIME-Version: Message-ID: Subject: cc: To: From: Date: Sender; bh=h/tkltk20e29nTfkQPxEvZyxDnAN8IM+C5401aPk9gc=; b=gdBVkjkr5W9bwa0E6l10yLQYfnluulKdjLZqCDW6r9YWM7Zvfiy4fbwgyz1Ki989Y8sPQuE+ 0YlEsgPg5pxJgDkJMOI7k9l2mPe7y4B9ZuSEU96lcXmkvu8fFm0GWBpTRg5d5a/v67yDkvBj J3/fMG1yU276ZMTEzymcLHj/SS0= X-Mailgun-Sending-Ip: 104.130.122.27 X-Mailgun-Sid: WyI0MWYwYSIsICJsaW51eC1rZXJuZWxAdmdlci5rZXJuZWwub3JnIiwgImJlOWU0YSJd Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by mxa.mailgun.org with ESMTP id 5e470381.7fe9bb28a068-smtp-out-n02; Fri, 14 Feb 2020 20:30:57 -0000 (UTC) Received: by smtp.codeaurora.org (Postfix, from userid 1001) id 0435CC4479C; Fri, 14 Feb 2020 20:30:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-caf-mail-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=2.0 tests=ALL_TRUSTED,SPF_NONE, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lmark-linux.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: lmark) by smtp.codeaurora.org (Postfix) with ESMTPSA id 0C8EBC43383; Fri, 14 Feb 2020 20:30:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 0C8EBC43383 Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; spf=none smtp.mailfrom=lmark@codeaurora.org Date: Fri, 14 Feb 2020 12:30:55 -0800 (PST) From: Liam Mark X-X-Sender: lmark@lmark-linux.qualcomm.com To: Joerg Roedel cc: kernel-team@android.com, "Isaac J. Manjarres" , Pratik Patel , linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org Subject: [RFC PATCH] iommu/iova: Support limiting IOVA alignment Message-ID: User-Agent: Alpine 2.10 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When the IOVA framework applies IOVA alignment it aligns all IOVAs to the smallest PAGE_SIZE order which is greater than or equal to the requested IOVA size. We support use cases that requires large buffers (> 64 MB in size) to be allocated and mapped in their stage 1 page tables. However, with this alignment scheme we find ourselves running out of IOVA space for 32 bit devices, so we are proposing this config, along the similar vein as CONFIG_CMA_ALIGNMENT for CMA allocations. Add CONFIG_IOMMU_LIMIT_IOVA_ALIGNMENT to limit the alignment of IOVAs to some desired PAGE_SIZE order, specified by CONFIG_IOMMU_IOVA_ALIGNMENT. This helps reduce the impact of fragmentation caused by the current IOVA alignment scheme, and gives better IOVA space utilization. Signed-off-by: Liam Mark --- drivers/iommu/Kconfig | 31 +++++++++++++++++++++++++++++++ drivers/iommu/iova.c | 20 +++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index d2fade984999..9684a153cc72 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -3,6 +3,37 @@ config IOMMU_IOVA tristate +if IOMMU_IOVA + +config IOMMU_LIMIT_IOVA_ALIGNMENT + bool "Limit IOVA alignment" + help + When the IOVA framework applies IOVA alignment it aligns all + IOVAs to the smallest PAGE_SIZE order which is greater than or + equal to the requested IOVA size. This works fine for sizes up + to several MiB, but for larger sizes it results in address + space wastage and fragmentation. For example drivers with a 4 + GiB IOVA space might run out of IOVA space when allocating + buffers great than 64 MiB. + + Enable this option to impose a limit on the alignment of IOVAs. + + If unsure, say N. + +config IOMMU_IOVA_ALIGNMENT + int "Maximum PAGE_SIZE order of alignment for IOVAs" + depends on IOMMU_LIMIT_IOVA_ALIGNMENT + range 4 9 + default 9 + help + With this parameter you can specify the maximum PAGE_SIZE order for + IOVAs. Larger IOVAs will be aligned only to this specified order. + The order is expressed a power of two multiplied by the PAGE_SIZE. + + If unsure, leave the default value "9". + +endif + # The IOASID library may also be used by non-IOMMU_API users config IOASID tristate diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c index 0e6a9536eca6..259884c8dbd1 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -177,6 +177,24 @@ int init_iova_flush_queue(struct iova_domain *iovad, rb_insert_color(&iova->node, root); } +#ifdef CONFIG_IOMMU_LIMIT_IOVA_ALIGNMENT +static unsigned long limit_align_shift(struct iova_domain *iovad, + unsigned long shift) +{ + unsigned long max_align_shift; + + max_align_shift = CONFIG_IOMMU_IOVA_ALIGNMENT + PAGE_SHIFT + - iova_shift(iovad); + return min_t(unsigned long, max_align_shift, shift); +} +#else +static unsigned long limit_align_shift(struct iova_domain *iovad, + unsigned long shift) +{ + return shift; +} +#endif + static int __alloc_and_insert_iova_range(struct iova_domain *iovad, unsigned long size, unsigned long limit_pfn, struct iova *new, bool size_aligned) @@ -188,7 +206,7 @@ static int __alloc_and_insert_iova_range(struct iova_domain *iovad, unsigned long align_mask = ~0UL; if (size_aligned) - align_mask <<= fls_long(size - 1); + align_mask <<= limit_align_shift(iovad, fls_long(size - 1)); /* Walk the tree backwards */ spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); -- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project