Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1072990rdb; Mon, 19 Feb 2024 03:31:46 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWU+sxHc0dhiT+lMaLXNAmlYnqX8djGwx4pJGf1n/96w4iUZ1jas4q59caNaka1uuteDVkmdefFoXFejx3La8FdhXOoK7+pwe5n7TUMRw== X-Google-Smtp-Source: AGHT+IFFeBTJEAlakBwqTcNux/Mr3ALbTTGy3E6LuC42IAl5QarReovq9Eg5/FKh2MoXBZO3YjmY X-Received: by 2002:a05:6a21:1798:b0:1a0:88a9:e434 with SMTP id nx24-20020a056a21179800b001a088a9e434mr8451114pzb.6.1708342305945; Mon, 19 Feb 2024 03:31:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708342305; cv=pass; d=google.com; s=arc-20160816; b=QPWDkdYKAyrNnZqM5MqhtIgjbX0ML27aTL34Kfvm0dS1miBtfrMwhwQZJ0d1HSwoOM 5LXU5VR0LJgM4pbkHHcQcdkTS8NBQ3mApeeUVx99MzLpNhT0In07MxemT0KfMeXPS0n/ cn6plRzY5JdBC5BR5VMlFos4+nuxjzL3l9Gm9/xy8+UhLpf0XsqsiimSavGxXq8O/ETW pIoNbS5/gYNIqbwy46wMj0ansCh255KHUkuH1UyPBXwC49B2SBHHbin2hREifytNBuda Vi1xZWNMDqgyzt8NtOxlyAXBhK4qt0ZIcHsKXxF3Ibows5EP+MfvxEwIZznhyUc3eaSu iW4g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=rzcrk3xPXXPW5E4xvwIqBIj0vf/WhGzB03Om/UaEaD4=; fh=tdUSXFEKy4Zu6Z7SbiojbpZa/5Fsrhsyt9zb5gPFCTA=; b=Yc7huX0ShhSwCtJAJho8VQ35QTT3VgrAK1MJokDTMABNzSNC+ChC302F9XDK70kguQ 9TnrZVARRvnGW+za9siyMos6nLc3Dp7eaY9A28MNhDyZatv7NPgBSQVsq/hUz75T+dh7 YfWh5i+U4ysfc0tv1kBVVpFzIrvBoWBp8oEQwhH/UnGqTfKGSOjZtfeyPGVGglewmgbE 5g7Ngg97QiVe3IYzbQBX6uKF5DZBJVb+E2W3CG13VZuxz+qMLHkcZ/Ico09nGWoDT6+O BdeJfX8354Dy4FYzlCpHSqsbg23EGgZdulgJH//oaQ9wKAVNx84sxrvWyeHbl/h6SYFe aWnA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wqBb6Lld; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-71206-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-71206-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id u190-20020a6385c7000000b005dc528be9acsi4180159pgd.475.2024.02.19.03.31.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 03:31:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-71206-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; dkim=pass header.i=@google.com header.s=20230601 header.b=wqBb6Lld; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-71206-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-71206-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 952D5284125 for ; Mon, 19 Feb 2024 11:30:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5DCFC2C1BA; Mon, 19 Feb 2024 11:30:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wqBb6Lld" Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C7A6A2D046 for ; Mon, 19 Feb 2024 11:30:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708342209; cv=none; b=YAu6XEhpASjvNZszkgFtu4ePi2iiHPYtbjPus5BtlGH7b97Mx8wkQUq17+ihPJan78RBGL7HAvYzLPWXSB+iuoega/Q5zjvxBxWsmngGdZSl5pkrfEthj2DgaMixoRu5+wxmu7M2A4OYMvrQhOKKxpNAwFyP4/qRgATvi9n4RIA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708342209; c=relaxed/simple; bh=jA8i3ioQgOQIGjp2Ibf5r1JRi97p+s3VymmYnM5xl2s=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=PKIqCsNe+EpiEPKgSm+K8J2biJE+Xgu3+McKvB/hvLiL2UA2zG3M9wb8iMWffNXEaWN9idU3MFbCO9tO5geboxRrSHKjop83SQ9RS7WQXOceHEaJlSeAU1cBU4cHCFBmoD7pmUcLPMXY4WCv5uKX8OUI6g3UEgcwRZdZI86KObI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wqBb6Lld; arc=none smtp.client-ip=209.85.128.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-60818d338bfso12747437b3.0 for ; Mon, 19 Feb 2024 03:30:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708342206; x=1708947006; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=rzcrk3xPXXPW5E4xvwIqBIj0vf/WhGzB03Om/UaEaD4=; b=wqBb6LldsnqZeMhoa76P2+RPLWCHVZEXdMU9QT30v1k+dndzQ/sbuS+M0r7YkZo5Ri UfjBxAeypMbCzncMoDZkakL8TMLcqfDPaDliSlivQ/FL9jxdzUbg0qQoSpB9JN2OLi0/ fQcXnEOomD4pdhpmolk5RFNzg/x6Sb7fB49OVFJIU6Z4l9hEA1PHQPaH2AI+y4UTVbBT p/B5emNTy8/x4bmm0nDygHA53J1nmAyYPV5KWogIBgmh6gkR+5+ZWs7/LC4MEiiXTfRr wdnWSdJ7qnLR+7OEv+tksWjxfWARuMkNiKUsmMneSQrU65zYoN8EHnp459OydgI3s9gS Vdfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708342206; x=1708947006; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rzcrk3xPXXPW5E4xvwIqBIj0vf/WhGzB03Om/UaEaD4=; b=BN/jUs986SK3ax8PANgGuXcBMY3W+2mJm2tsU/21oVbktiTvFKTckGEnwUGpZcJIVV YT5/U+Movze7fIhlFQHKzWXeLoRnUion59P9WmaF/VonqhQiqqOTznce8qKr76OpE+oP tbM1VL75aXXzjiLiNu8BHcKDLx2ZdhJj6wDAkz0fT/tkHQF6GL2QP9XXu8URBgpNPl16 wipw60GY5ZhKPtF/LzJFke8lDIPvv9HUCOPduCiR8EtaqGWcBLBHWHAZ3ShYSZwo0YAS gSVvleF+FxyguFVKhgl29z1KJ2ZCXiXYv11fzBZqlKbUvGIGN7cqFmC/JAziJ/rRU19u BOFw== X-Forwarded-Encrypted: i=1; AJvYcCXzScTC/MA0hmJ5zrr3QtxS+mJ6avt6Aw8i6jAvdtPvwmxlIhyqbx9JNHNYBc68RwAraknoaiz1WMgocICWIxPrY8AzkItjVBTdFIcO X-Gm-Message-State: AOJu0YwG/p7UfUQBH9i7kBJQghPITINTaAi3kcyw9s4WsDcqtPCXHcxO GU2nAfMdvtA+ghb5a50k5QcBpOg2QqGECLFsrSr+IQymzh5FriJ459o0FIfHQFi3qTrTHfM3+Me NqDXcevn22bOd2D4hamp1wrPTQlLlWvYRHhnv X-Received: by 2002:a0d:cb0e:0:b0:604:7b9e:f62f with SMTP id n14-20020a0dcb0e000000b006047b9ef62fmr13410931ywd.37.1708342206438; Mon, 19 Feb 2024 03:30:06 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240208152808.3712149-1-howardyen@google.com> In-Reply-To: From: Howard Yen Date: Mon, 19 Feb 2024 19:29:55 +0800 Message-ID: Subject: Re: [PATCH v3] dma-coherent: add support for multi coherent rmems per dev To: Andy Shevchenko Cc: hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, gregkh@linuxfoundation.org, rafael@kernel.org, broonie@kernel.org, james@equiv.tech, james.clark@arm.com, masahiroy@kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux.dev Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Feb 9, 2024 at 2:02=E2=80=AFAM Andy Shevchenko wrote: > > On Thu, Feb 08, 2024 at 03:28:05PM +0000, Howard Yen wrote: > > Add support for multiple coherent rmems per device. This patch replaces > > original dma_mem with dma_mems list in device structure to store multip= le > > rmems. > > > > These multiple rmems can be assigned to the device one by one by > > of_reserved_mem_device_init_by_idx() with the memory-region > > declaration in device tree as below and store the rmem to the dma_mems > > list. > > > > device1@0 { > > ... > > memory-region =3D <&reserved_mem0>, <&reserved_mem1>; > > ... > > }; > > > > When driver tries to allocate memory from the rmems, looks for the firs= t > > available rmem and allocates the memory from this rmem. > > > > Then if driver removed, of_reserved_mem_device_release() needs to be > > invoked to release all the rmems assigned to the device. > > ... > > > struct dma_coherent_mem *mem; > > - int ret; > > + int retval; > > > > mem =3D dma_init_coherent_memory(phys_addr, device_addr, size, fa= lse); > > if (IS_ERR(mem)) > > return PTR_ERR(mem); > > > > - ret =3D dma_assign_coherent_memory(dev, mem); > > - if (ret) > > + retval =3D dma_assign_coherent_memory(dev, mem); > > + if (retval) > > _dma_release_coherent_memory(mem); > > - return ret; > > + return retval; > > This is unrelated change. > > But why? Do you have retval in the _existing_ code elsewhere? Rename the 'ret' variable to 'retval' because, in the dma_mmap_from_dev_coherent(), there is an argument named 'ret', and also I add 'retval' for the return va= lue. So I try to rename it here to be consistent. > > > ... > > > int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size, > > dma_addr_t *dma_handle, void **ret) > > { > > - struct dma_coherent_mem *mem =3D dev_get_coherent_memory(dev); > > + struct dma_coherent_mem *mem_tmp; > > > > - if (!mem) > > + if (list_empty(&dev->dma_mems)) > > return 0; > > > > - *ret =3D __dma_alloc_from_coherent(dev, mem, size, dma_handle); > > + list_for_each_entry(mem_tmp, &dev->dma_mems, node) { > > + *ret =3D __dma_alloc_from_coherent(dev, mem_tmp, size, dm= a_handle); > > + if (*ret) > > + break; > > This bails out on the first success. Moreover, if one calls this function > again, it will rewrite the existing allocation. Is this all expected? > > OTOH, if you add multiple entries and bailing out on error condition it s= hould > be clear if the previous allocations have to be released. If it's not able to allocate required memory from the first entry, then tries to allocate from the following entry, and so on. > > > + } > > > return 1; > > > } > > ... > > > int dma_release_from_dev_coherent(struct device *dev, int order, void = *vaddr) > > { > > - struct dma_coherent_mem *mem =3D dev_get_coherent_memory(dev); > > + struct dma_coherent_mem *mem_tmp; > > + int retval =3D 0; > > + > > + list_for_each_entry(mem_tmp, &dev->dma_mems, node) { > > + retval =3D __dma_release_from_coherent(mem_tmp, order, va= ddr); > > + if (retval =3D=3D 1) > > + break; > > Same Q here. > > > + } > > > > - return __dma_release_from_coherent(mem, order, vaddr); > > + return retval; > > } > > ... > > > int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_stru= ct *vma, > > void *vaddr, size_t size, int *ret) > > { > > - struct dma_coherent_mem *mem =3D dev_get_coherent_memory(dev); > > + struct dma_coherent_mem *mem_tmp; > > + int retval =3D 0; > > > > - return __dma_mmap_from_coherent(mem, vma, vaddr, size, ret); > > + list_for_each_entry(mem_tmp, &dev->dma_mems, node) { > > + retval =3D __dma_mmap_from_coherent(mem_tmp, vma, vaddr, = size, ret); > > + if (retval =3D=3D 1) > > + break; > > And here. > > > + } > > + > > + return retval; > > } > > ... > > With the above Q in mind, here is another one: Why can't we allocate all = at once? Not sure if I misunderstand your meaning, It tries to allocate the memory from the first entry that satisfies the allocation requirement, but not separately. > > -- > With Best Regards, > Andy Shevchenko > > --=20 Regards, Howard