Received: by 2002:a05:6500:1b41:b0:1fb:d597:ff75 with SMTP id cz1csp74695lqb; Tue, 4 Jun 2024 05:41:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWxr731jeb8lC1XL7g6ql0XkMQsIXK2ZUcCmbGh04JcKmhgLhOVqxY0lRt/sFKTESoi0uXGmvD30sLm6OjQ3vrDUfI+NmsX+3iY71dsyA== X-Google-Smtp-Source: AGHT+IFeoa2sS4zfyqAGnC1fpgWbvHwP9zcmbrK4pjb0r02Vtos+iI9hv3ZpjsEdOtMdBRBSMRJT X-Received: by 2002:a05:6a21:185:b0:1ac:e379:52fd with SMTP id adf61e73a8af0-1b26f121841mr14134053637.14.1717504916005; Tue, 04 Jun 2024 05:41:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717504915; cv=pass; d=google.com; s=arc-20160816; b=BcwIjpXUwYNMCnpGydqlS2EC5avcmVsjee6+FsQyM1W98+hwdVoHhk2FkO/L65wp9W uGUXa1UQYoshNeInaQdzia1KIHmAcvcmce0iSFBI4mdI0Cl8LLtNKOdZ4GwsjLVZ84EC xuSYG5rLbjCrQd3fJW2Xn33QQEiQTyPNLsvMGn9WTn2djOyZeDXTSB59o0/O8qth4XkN nTQlPcUf76vMmL1WK9eN6qwIQxHITr7LSf1Xdnc4uGbCgbCFWH31E+2M/2WPuOGb/kLK 9fGT06qKkAGkMQ++CPw/hjkvErOD9y74Z5IsZWZD5JJ2s5VvusP1DH31Ukazw0rBv+Kq OCMg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:content-language:from :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=8MOZ4TZVQKaMJfRLbwwyY7WdA7s70emFTaq1FTko3pc=; fh=3kc6irD9u0esHwKMtpPVXXg7+d5azOE+mYo7JVUkB8w=; b=CM7sBZYP5WBAfVaRh/xTbo2WqXjAPSQXp/5LVnrCsg3HU48MgbEV+cQj8jJbfePN42 +c6hRIUuf1auJuwZ8aLNUJm2VAFyTDVFYtXzdtu0ch4bCrdRNICoGPMoGe4kQy/PBqtf Jw6+Cz4hV8HldZf1E4qNlWWU9Z95SEpgMwIJLAfQ60Bp9I6chBAr5A/+XtLE1buBtXAI oAER3e4utdHOuPyjracIxLnz7l9jSgbzrDahsM69JzVMMjEkXIFWCT+SEWjRsD/96dbH P0LRsTfgtAYwtuZmmT/OakCU1pJXWlLheqmTjfQbSeX2DoovIU40n3ShyvCdqfLMkG2q WvLA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-200619-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200619-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d2e1a72fcca58-702423dc692si8062087b3a.6.2024.06.04.05.41.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 05:41:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-200619-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-200619-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200619-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id EA22F282DC8 for ; Tue, 4 Jun 2024 12:41:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9FC6A146A72; Tue, 4 Jun 2024 12:39:47 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CC7661422B4 for ; Tue, 4 Jun 2024 12:39:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717504787; cv=none; b=eS4klpZpKYUZ4cbw0iytdv6Drb9lernMhOg2e/GzuxQFHF7qF+ZZ/899yK3fzVHX1ls0dRkIWH4HtjtiECz1ml0m+e7JkOmUSJb/mboVMl2Y1EkeyKK4tN2HkCrqAQVW+Tc1jsr3duiKoOk536P/Z+ukN1JDxZQjCHcR+QaTauc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717504787; c=relaxed/simple; bh=uTsztnqbT4iOcXiermoEhzWU5FdfFGzOa8AsZ4gZ2Q8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=ajTnmxJ7V2MbaV0F5B4lXh+FRo71JbCRxn+X0v4iKeGLJfRAwwnu+q/qnppb0JcWA+TsMIB7XRVka5I8OXmWGoQNxj/Zwc33nZER2eHY3yhfnSsm0evUgE2tr1DaSk8oQjB4ZR5mLAcG55diWVA3gGOJcwVD3paYmqiLJsy9pc0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A57451063; Tue, 4 Jun 2024 05:40:09 -0700 (PDT) Received: from [10.1.196.40] (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5E75D3F64C; Tue, 4 Jun 2024 05:39:41 -0700 (PDT) Message-ID: Date: Tue, 4 Jun 2024 13:39:36 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] dma: direct: Optimize the code for the dma_direct_free function To: kunyu , hch@lst.de, m.szyprowski@samsung.com Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org References: <20240604084100.51464-1-kunyu@nfschina.com> From: Robin Murphy Content-Language: en-GB In-Reply-To: <20240604084100.51464-1-kunyu@nfschina.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 04/06/2024 9:41 am, kunyu wrote: > The 'is_swiotlb-for-alloc' and 'dev_isdma_coherent' judgment functions > need to be called multiple times, so they are adjusted to variable > judgment, which can improve code conciseness. > > Signed-off-by: kunyu > --- > kernel/dma/direct.c | 9 +++++---- > 1 file changed, 5 insertions(+), 4 deletions(-) It's hardly concise to add *more* code than was there before... :/ Personally I don't think shaving a handful of characters off each invocation has any positive impact on readability in this case, while the extra visual indirection, and breaking consistency with the rest of this file, definitely has a negative one. Also note that these "functions" are already just inline wrappers around a single variable dereference - for my arm64 build at least, this patch has no effect at all on the generated object code, since the compiler can still optimise out the locals (so at least it doesn't make things *worse* by forcing it to allocate a larger stack frame). Thanks, Robin. > diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c > index 4d543b1e9d57..041e316ad4c0 100644 > --- a/kernel/dma/direct.c > +++ b/kernel/dma/direct.c > @@ -315,23 +315,24 @@ void dma_direct_free(struct device *dev, size_t size, > void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs) > { > unsigned int page_order = get_order(size); > + bool swiotlb_for_alloc = is_swiotlb_for_alloc(dev); > + bool is_dma_coherent = dev_is_dma_coherent(dev); > > if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) && > - !force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev)) { > + !force_dma_unencrypted(dev) && !swiotlb_for_alloc) { > /* cpu_addr is a struct page cookie, not a kernel address */ > dma_free_contiguous(dev, cpu_addr, size); > return; > } > > if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_ALLOC) && > - !dev_is_dma_coherent(dev) && > - !is_swiotlb_for_alloc(dev)) { > + !is_dma_coherent && !swiotlb_for_alloc) { > arch_dma_free(dev, size, cpu_addr, dma_addr, attrs); > return; > } > > if (IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) && > - !dev_is_dma_coherent(dev)) { > + !is_dma_coherent) { > if (!dma_release_from_global_coherent(page_order, cpu_addr)) > WARN_ON_ONCE(1); > return;