Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1136003pxj; Thu, 17 Jun 2021 23:49:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxeWnqgd1An+Hk7j069jtpglOZNDn1Wdn2MS0wJp8euhxDBxdVW+cawLWRInUJCvtm/lkJ8 X-Received: by 2002:a05:6e02:1e06:: with SMTP id g6mr6222167ila.192.1623998971678; Thu, 17 Jun 2021 23:49:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623998971; cv=none; d=google.com; s=arc-20160816; b=ypisY3pk20ygSQrGp5VBq9gBK/vf8uJeTiWxR9olSyhHkZaofDFtZpv2p/qW0/DKLm y+YSRWsWV+y6VLd1tgxaGKD68bEu5GYQTBrhu5AZ08yqIPcaWiJn8tYwIzLqy3QBHbEY A7GZJb21Lhf1T0BKco6j+ZPH830sTt0MvsimC+pq/uAqeDLi/dkaJT/KLboBvd2elIIj X0/uAxVmOLTrCk3FPC+FB6MBOOCmr03Sk0DPqkNMbFIcdAfnXJuc9KtzN5wZ64br3V7D zDWW8AyZOagk7MntTHRi3wZovnTXNgv30gLJyLs2+xXklsHcKC1uTqx0421Ww4+z50m7 qmUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=K4gmwVrd4ipCgh7Pt4U3qK6zM5RZFqVsfT+5m/JaTNY=; b=kp+M6e6pInE1tckAuErvLZNtELShBPXHASHU6ky+VK8PIdk9cMmZ/fR+MDeZXUJg38 DOi39pTKue7njeP6piO81SLkXZALFYAzo14cZrS2HmKy+oejVvG/hcAnnoJOIlLjfBio glrp5WwzOZ+XAvgQIgqgk5PwE7+oFxaGGhKU6uLfhSm/LTsahyACimUv1LEQ6S9mGtk5 xHADQhbSwnFW4YOe7XUxy3H2U/9ehzJVh6mnx0EHb6fOPSUDRMl8KyyPybvsj15uQ3+x p1tKb7GVEU2nhDuQFdPK+1yUCps7VJ0BuXG9rH2h6reuIuHtOodDZlC8pXP//1ZZ3klS 1Lzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=b3TOMvQ1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b19si8800525iow.46.2021.06.17.23.49.19; Thu, 17 Jun 2021 23:49:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=b3TOMvQ1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231783AbhFRGfv (ORCPT + 99 others); Fri, 18 Jun 2021 02:35:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231168AbhFRGfv (ORCPT ); Fri, 18 Jun 2021 02:35:51 -0400 Received: from mail-qt1-x830.google.com (mail-qt1-x830.google.com [IPv6:2607:f8b0:4864:20::830]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34DD8C061574 for ; Thu, 17 Jun 2021 23:33:42 -0700 (PDT) Received: by mail-qt1-x830.google.com with SMTP id w26so1486236qto.13 for ; Thu, 17 Jun 2021 23:33:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=K4gmwVrd4ipCgh7Pt4U3qK6zM5RZFqVsfT+5m/JaTNY=; b=b3TOMvQ1kXF7QuAFyHdOkvmT8LXOEbUEDAbnT1VD77ESf6dWqJqkel2pij6H8qazUH qeKiHzooTElNu69H2pMv9jI9D4woaiXlDH7E4nc1glsurOz9EYewwC9YHW+cnkiHTax7 tbypLVBnZif7y60KH97lEwI+rEiH+ILoO0+Go= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=K4gmwVrd4ipCgh7Pt4U3qK6zM5RZFqVsfT+5m/JaTNY=; b=S63msIeXv7OBtToLhI9s3RUltBlfUvscZEiBgWH5LPsSGyFGfS7phRfbz9CdKZJ9Ze gV9acQzoEpxOfX/Ns34f9NmdAWK7stv6lN1lRr824FFrkDdaoskTebhB5Lc/jpUGvAco ALbabRHE0HZFpZniYI6vRZvl5fmkCUIJNx5SvzcYV76Z3RrrdmR290Ch0VWZNof10YDm B7lnAvdmhlRXgi18wRfIWpFoPV7Hs75qHPOOq64q1qgR6eOd/7dd0biUJKIYqdjZLyPY CWUTcHud2IPCtXOAHuEYl2iWZsWZ3n/KSOw1gc13Xd7CzeuGf9qE4pDv7MXDjsJDU64L 4F6w== X-Gm-Message-State: AOAM533ro3m5VfMtCNVBFJe/Kq+RaDz6HvJ2thOicLujiMcAML9WFbPX YrG5duCEmT5Q6SvCHl/fFRra6UYkFD5Vhg== X-Received: by 2002:a05:622a:391:: with SMTP id j17mr8899935qtx.217.1623998021209; Thu, 17 Jun 2021 23:33:41 -0700 (PDT) Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com. [209.85.222.178]) by smtp.gmail.com with ESMTPSA id 85sm2434653qkl.46.2021.06.17.23.33.40 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Jun 2021 23:33:40 -0700 (PDT) Received: by mail-qk1-f178.google.com with SMTP id d196so8371013qkg.12 for ; Thu, 17 Jun 2021 23:33:40 -0700 (PDT) X-Received: by 2002:a02:384b:: with SMTP id v11mr1842282jae.90.1623997548077; Thu, 17 Jun 2021 23:25:48 -0700 (PDT) MIME-Version: 1.0 References: <20210617062635.1660944-1-tientzu@chromium.org> <20210617062635.1660944-2-tientzu@chromium.org> In-Reply-To: From: Claire Chang Date: Fri, 18 Jun 2021 14:25:37 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v13 01/12] swiotlb: Refactor swiotlb init functions To: Stefano Stabellini Cc: Rob Herring , mpe@ellerman.id.au, Joerg Roedel , Will Deacon , Frank Rowand , Konrad Rzeszutek Wilk , boris.ostrovsky@oracle.com, jgross@suse.com, Christoph Hellwig , Marek Szyprowski , benh@kernel.crashing.org, paulus@samba.org, "list@263.net:IOMMU DRIVERS" , Robin Murphy , grant.likely@arm.com, xypron.glpk@gmx.de, Thierry Reding , mingo@kernel.org, bauerman@linux.ibm.com, peterz@infradead.org, Greg KH , Saravana Kannan , "Rafael J . Wysocki" , heikki.krogerus@linux.intel.com, Andy Shevchenko , Randy Dunlap , Dan Williams , Bartosz Golaszewski , linux-devicetree , lkml , linuxppc-dev@lists.ozlabs.org, xen-devel@lists.xenproject.org, Nicolas Boichat , Jim Quinlan , Tomasz Figa , bskeggs@redhat.com, Bjorn Helgaas , chris@chris-wilson.co.uk, Daniel Vetter , airlied@linux.ie, dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, jani.nikula@linux.intel.com, Jianxiong Gao , joonas.lahtinen@linux.intel.com, linux-pci@vger.kernel.org, maarten.lankhorst@linux.intel.com, matthew.auld@intel.com, rodrigo.vivi@intel.com, thomas.hellstrom@linux.intel.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 18, 2021 at 7:30 AM Stefano Stabellini wrote: > > On Thu, 17 Jun 2021, Claire Chang wrote: > > Add a new function, swiotlb_init_io_tlb_mem, for the io_tlb_mem struct > > initialization to make the code reusable. > > > > Signed-off-by: Claire Chang > > Reviewed-by: Christoph Hellwig > > Tested-by: Stefano Stabellini > > Tested-by: Will Deacon > > --- > > kernel/dma/swiotlb.c | 50 ++++++++++++++++++++++---------------------- > > 1 file changed, 25 insertions(+), 25 deletions(-) > > > > diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c > > index 52e2ac526757..47bb2a766798 100644 > > --- a/kernel/dma/swiotlb.c > > +++ b/kernel/dma/swiotlb.c > > @@ -168,9 +168,28 @@ void __init swiotlb_update_mem_attributes(void) > > memset(vaddr, 0, bytes); > > } > > > > -int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose) > > +static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start, > > + unsigned long nslabs, bool late_alloc) > > { > > + void *vaddr = phys_to_virt(start); > > unsigned long bytes = nslabs << IO_TLB_SHIFT, i; > > + > > + mem->nslabs = nslabs; > > + mem->start = start; > > + mem->end = mem->start + bytes; > > + mem->index = 0; > > + mem->late_alloc = late_alloc; > > + spin_lock_init(&mem->lock); > > + for (i = 0; i < mem->nslabs; i++) { > > + mem->slots[i].list = IO_TLB_SEGSIZE - io_tlb_offset(i); > > + mem->slots[i].orig_addr = INVALID_PHYS_ADDR; > > + mem->slots[i].alloc_size = 0; > > + } > > + memset(vaddr, 0, bytes); > > +} > > + > > +int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose) > > +{ > > struct io_tlb_mem *mem; > > size_t alloc_size; > > > > @@ -186,16 +205,8 @@ int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose) > > if (!mem) > > panic("%s: Failed to allocate %zu bytes align=0x%lx\n", > > __func__, alloc_size, PAGE_SIZE); > > - mem->nslabs = nslabs; > > - mem->start = __pa(tlb); > > - mem->end = mem->start + bytes; > > - mem->index = 0; > > - spin_lock_init(&mem->lock); > > - for (i = 0; i < mem->nslabs; i++) { > > - mem->slots[i].list = IO_TLB_SEGSIZE - io_tlb_offset(i); > > - mem->slots[i].orig_addr = INVALID_PHYS_ADDR; > > - mem->slots[i].alloc_size = 0; > > - } > > + > > + swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false); > > > > io_tlb_default_mem = mem; > > if (verbose) > > @@ -282,8 +293,8 @@ swiotlb_late_init_with_default_size(size_t default_size) > > int > > swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs) > > { > > - unsigned long bytes = nslabs << IO_TLB_SHIFT, i; > > struct io_tlb_mem *mem; > > + unsigned long bytes = nslabs << IO_TLB_SHIFT; > > > > if (swiotlb_force == SWIOTLB_NO_FORCE) > > return 0; > > @@ -297,20 +308,9 @@ swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs) > > if (!mem) > > return -ENOMEM; > > > > - mem->nslabs = nslabs; > > - mem->start = virt_to_phys(tlb); > > - mem->end = mem->start + bytes; > > - mem->index = 0; > > - mem->late_alloc = 1; > > - spin_lock_init(&mem->lock); > > - for (i = 0; i < mem->nslabs; i++) { > > - mem->slots[i].list = IO_TLB_SEGSIZE - io_tlb_offset(i); > > - mem->slots[i].orig_addr = INVALID_PHYS_ADDR; > > - mem->slots[i].alloc_size = 0; > > - } > > - > > + memset(mem, 0, sizeof(*mem)); > > + swiotlb_init_io_tlb_mem(mem, virt_to_phys(tlb), nslabs, true); > > set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT); > > - memset(tlb, 0, bytes); > > This is good for swiotlb_late_init_with_tbl. However I have just noticed > that mem could also be allocated from swiotlb_init_with_tbl, in which > case the zeroing is missing. I think we need another memset in > swiotlb_init_with_tbl as well. Or maybe it could be better to have a > single memset at the beginning of swiotlb_init_io_tlb_mem instead. Up to > you. swiotlb_init_with_tbl uses memblock_alloc to allocate the io_tlb_mem and memblock_alloc[1] will do memset in memblock_alloc_try_nid[2], so swiotlb_init_with_tbl is also good. I'm happy to add the memset in swiotlb_init_io_tlb_mem if you think it's clearer and safer. [1] https://elixir.bootlin.com/linux/v5.13-rc6/source/include/linux/memblock.h#L407 [2] https://elixir.bootlin.com/linux/v5.13-rc6/source/mm/memblock.c#L1555