From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 98E98C433E3 for ; Thu, 16 Jul 2020 15:46:15 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 57BB720787 for ; Thu, 16 Jul 2020 15:46:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="aPfB56bi" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 57BB720787 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id DD93F8D0026; Thu, 16 Jul 2020 11:46:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D89BE8D0016; Thu, 16 Jul 2020 11:46:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C79508D0026; Thu, 16 Jul 2020 11:46:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0042.hostedemail.com [216.40.44.42]) by kanga.kvack.org (Postfix) with ESMTP id B4B2C8D0016 for ; Thu, 16 Jul 2020 11:46:14 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 5AAD0180AD815 for ; Thu, 16 Jul 2020 15:46:14 +0000 (UTC) X-FDA: 77044365468.15.stone65_5c15e3526f03 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id 249821814B0C8 for ; Thu, 16 Jul 2020 15:46:14 +0000 (UTC) X-HE-Tag: stone65_5c15e3526f03 X-Filterd-Recvd-Size: 5370 Received: from mail-ed1-f66.google.com (mail-ed1-f66.google.com [209.85.208.66]) by imf35.hostedemail.com (Postfix) with ESMTP for ; Thu, 16 Jul 2020 15:46:13 +0000 (UTC) Received: by mail-ed1-f66.google.com with SMTP id h28so5106048edz.0 for ; Thu, 16 Jul 2020 08:46:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=6ljVtEAns8z/jn7DCignaoAnzmnNFDfpuHqi7UerqxU=; b=aPfB56bi8qbQ9Bnwzyzn1R7YAPhRGG2a7TuNSTdn1VG+EFU2a3GK0Os96HBqBxOA4Q crd5Uc/b5vofjrBBPsO1A1M1u9mJ8VlxCnRA3zghwvh+MzJO18eT+hOHd3EY1SY7BLTm yXhBPQr9E6IN6tHfotQyQ8cWkV+jODeykTCbVZVR6MfzIvqJy/PiX1x19JHDutuE9nan d//Grc923sXLl6RS3HS503/R80t9jP8jYvTth/s5OGZUNqw/HgDHl/08+hIEx5VKGemC wKzoutyUUbjWCgX8MnAhynaEkuG0UbKh7rrHT4Fo0p+uhXu48OFPLlynHiobSNh3EFZx rwQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=6ljVtEAns8z/jn7DCignaoAnzmnNFDfpuHqi7UerqxU=; b=WMu0SE7rlUKgqpIQ/H0HQiAffBusQw3B/NnzoazZeiCTgdhRHFWxt1JGvXHbKgKytJ l5DxRSIVjYlxX+l/19LvTH1L6gohvKDix5WVP/rpvk4TBjDlv8WUmz2TbyQzUPvOQhAU u6p+tf9GQJciCmeX21d9Kaa7WnLygVJ2Of3yzn7i2rLgizqBgZ2bfmHPzKG8MneiLAD6 oScqIsr8leDkAoRe2SLlXg/1JBj+qe2z+oem/blW65XTMFYf3hUHDPsvScjCPAhnA86e nWFqaPz0AiTUfYdxs0O7D+62OXUu9sopnTM0w0oeK/ly3nXImAwM4N+dzBgggOEOdcYI LL8Q== X-Gm-Message-State: AOAM532sriJOwHzyeuCMXMc/PYY1oxPwZLoTzcPASnQTtrFc3QTpp170 2ntPJ7BZWZiL5K21DlEa9s2DFA== X-Google-Smtp-Source: ABdhPJwbdkI3Q+F63rYDOBFMMWI1aDZf4JEWa5oyS7j5a2G8ovxWK10g6L0x8GCJgTIZg2aQM1EIpg== X-Received: by 2002:a05:6402:31ba:: with SMTP id dj26mr5026934edb.181.1594914372307; Thu, 16 Jul 2020 08:46:12 -0700 (PDT) Received: from myrica ([2001:1715:4e26:a7e0:116c:c27a:3e7f:5eaf]) by smtp.gmail.com with ESMTPSA id f16sm5367024ejr.0.2020.07.16.08.46.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jul 2020 08:46:11 -0700 (PDT) Date: Thu, 16 Jul 2020 17:45:58 +0200 From: Jean-Philippe Brucker To: Will Deacon Cc: iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, joro@8bytes.org, catalin.marinas@arm.com, robin.murphy@arm.com, baolu.lu@linux.intel.com, Jonathan.Cameron@huawei.com, jacob.jun.pan@linux.intel.com, zhangfei.gao@linaro.org, xuzaibo@huawei.com, zhengxiang9@huawei.com, fenghua.yu@intel.com, hch@infradead.org, Suzuki K Poulose Subject: Re: [PATCH v8 07/12] iommu/arm-smmu-v3: Share process page tables Message-ID: <20200716154558.GC447208@myrica> References: <20200618155125.1548969-1-jean-philippe@linaro.org> <20200618155125.1548969-8-jean-philippe@linaro.org> <20200713202236.GA3575@willie-the-truck> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200713202236.GA3575@willie-the-truck> X-Rspamd-Queue-Id: 249821814B0C8 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Jul 13, 2020 at 09:22:37PM +0100, Will Deacon wrote: > > +static struct arm_smmu_ctx_desc *arm_smmu_share_asid(u16 asid) > > +{ > > + struct arm_smmu_ctx_desc *cd; > > > > - xa_erase(&asid_xa, cd->asid); > > + cd = xa_load(&asid_xa, asid); > > + if (!cd) > > + return NULL; > > + > > + if (cd->mm) { > > + /* All devices bound to this mm use the same cd struct. */ > > + refcount_inc(&cd->refs); > > + return cd; > > + } > > How do you handle racing against a concurrent arm_smmu_free_asid() here? Patch 8 adds an asid_lock to deal with this, but it should be introduced in this patch. There is a potential use-after-free here, if arm_smmu_domain_free() runs concurrently. > > > +__maybe_unused > > +static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm) > > +{ > > + u16 asid; > > + int ret = 0; > > + u64 tcr, par, reg; > > + struct arm_smmu_ctx_desc *cd; > > + struct arm_smmu_ctx_desc *old_cd = NULL; > > + > > + lockdep_assert_held(&sva_lock); > > Please don't bother with these for static functions (but I can see the > value in having them for functions with external callers). > > > + > > + asid = mm_context_get(mm); > > + if (!asid) > > + return ERR_PTR(-ESRCH); > > + > > + cd = kzalloc(sizeof(*cd), GFP_KERNEL); > > + if (!cd) { > > + ret = -ENOMEM; > > + goto err_put_context; > > + } > > + > > + arm_smmu_init_cd(cd); > > + > > + old_cd = arm_smmu_share_asid(asid); > > + if (IS_ERR(old_cd)) { > > + ret = PTR_ERR(old_cd); > > + goto err_free_cd; > > + } else if (old_cd) { > > Don't need the 'else' > > > + if (WARN_ON(old_cd->mm != mm)) { > > + ret = -EINVAL; > > + goto err_free_cd; > > + } > > + kfree(cd); > > + mm_context_put(mm); > > + return old_cd; > > This is a bit messy. Can you consolidate the return path so that ret is a > pointer and you have an 'int err', e.g.: > > return err < 0 ? ERR_PTR(err) : ret; Sure, I think it looks a little nicer this way Thanks, Jean