Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp4029240ybd; Tue, 25 Jun 2019 12:45:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqxFPtkdtD9GlS/JKxxlnuUdB4EwOpelQPYQ3489Ed1Yvx+gcgtJhddDnQbjW/sa2CSZ/59f X-Received: by 2002:a63:88c1:: with SMTP id l184mr28653588pgd.376.1561491907688; Tue, 25 Jun 2019 12:45:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561491907; cv=none; d=google.com; s=arc-20160816; b=UgFERPSiqsliAL2rfSpg5z0Lq7iccsBe3V/u5QlkfgFph1t5y1iVwyb9Yr1w4KH99t a2YXKW0kX51VRvthlEDqoi/tVOAmsXhY+SFSm8o7KEvBKg0SidGAL1qHU1ZURI4KtSwc 2sIin9KDQCuyp2VoUPU8uHZwrj8pqxPcvWmiFGlbU5kmUOkrIreUnU66M37jQ9wjJFKz EQOdjHVjatwlN/yxGK6WH1V/Jy6fClrY7n2fqDRDhcwQXal575nQPNG2tztUijHFUKPg A41jlPK7FDVZSInYzjPcfG+v3c5aky8n8UP7z5x7388lo4h2YKi7dUoMg0alWsUVVqK4 afLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=EzzLn0GswMFcyMLNaQ7ggxi30GgEafznXlfGyBwbniM=; b=IRJcQhh50ViTPj7EKADxlTnmKvO8SjJ4O0x4XNuZ94cujKJGp8Y0vF+x9F++ie8Mky MlsFDpeXgcFvKDsxcBULUC82rNWoCs08NTMukWKZYoRI2Yocv/smLd6xLZ0+mNQnwsBX T48Q4zASC+a39H4bQMbcCSLhA7taTO7ID1HTsKMn6TdwPgkFxaAG/3BZ15EgyStuL9w9 mNvi51PzJg6cO6vCHhMPjPiAhKE+g/RuNRTo+E4fnV2IEiLvs/0oUy2NLG+coIOyX7nt UbAQBf3A0j0LOfzIvaZ3lDnx33su3cbtJUse6IG2/C7iQ0DhLKNKpOL9kDIJ26B9NoWY wM/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=GXRap7s5; 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 v10si1104148plg.320.2019.06.25.12.44.51; Tue, 25 Jun 2019 12:45:07 -0700 (PDT) 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=pass header.i=@sifive.com header.s=google header.b=GXRap7s5; 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 S1731111AbfFYR1V (ORCPT + 99 others); Tue, 25 Jun 2019 13:27:21 -0400 Received: from mail-lf1-f65.google.com ([209.85.167.65]:43502 "EHLO mail-lf1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727033AbfFYR1V (ORCPT ); Tue, 25 Jun 2019 13:27:21 -0400 Received: by mail-lf1-f65.google.com with SMTP id j29so13209914lfk.10 for ; Tue, 25 Jun 2019 10:27:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=EzzLn0GswMFcyMLNaQ7ggxi30GgEafznXlfGyBwbniM=; b=GXRap7s5jEiN/yj8znTw5VoaIeXeTRZCA56SzbEgqrRSfr1R32FibRI3wBA7cG9NLs q1G4QUquAYQe0O39WbBEa8WsKHH7bGeEZl6cjOxEc2L1ZIWrJOURVZ8TJu0dDJ//w7ck bNCIgX2ypyFZT5lsTK/jWhcdiSTTu28EMnpJMrm6anadT+0OVbTZ7SBYAoSxCqtk71h+ 8jOvlf4DiKJZCGH6tASAOzF9RxNndGi2c1qhz88dnEFInYMU9hP6or0aCD24oremwdGY 0PV/CrEP60RgzkVRW//JzKvIUs+kvFj/5hjqT/ST60egaNav6AKYXwUK0IaPvUBkfLFm MKBg== 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=EzzLn0GswMFcyMLNaQ7ggxi30GgEafznXlfGyBwbniM=; b=BpsVlslZGYdsTDuxoToq7p3uLt3PyGF/FyNVZGMGl0YH1leoNQiGhnUoKyJjCok1eF Mbxh5m9uLvG3xMzFP8pY9xbPSXlecbWzid/o1wnEgRf+YsStHENG1Gy2hb/uqeN6FCEh aMlpasEcEkDQyuk/LCBjsXicMF4EXfmcXPNysav+nM2x/hhE56TOYH+BYFOLj143F7CF oXUOLvCDnkJgBYQhYjKlVCf1gMYHs1zc47cwoTm7WNMf0kggFYSVYGU/SlaogL92+Nhb IUVNRUd5xyIJkZotWI3r259AH4BziH3kzlRUsxJJM272YYjEwjvHb/VeDOjFkWyaiJkP 1Plw== X-Gm-Message-State: APjAAAU1q0G59fRCcmrlnfyNILV2xFmV6ILuIhWMnVsoh9iqGySQO83T Jc8HBBLuGdLano/HgGHtoBTCWYUO6bxneHwJFxDBwq/RUqE= X-Received: by 2002:a19:f00a:: with SMTP id p10mr43511314lfc.68.1561483639083; Tue, 25 Jun 2019 10:27:19 -0700 (PDT) MIME-Version: 1.0 References: <1561420642-21186-1-git-send-email-alan.mikhak@sifive.com> <20190625070835.GC30123@lst.de> In-Reply-To: <20190625070835.GC30123@lst.de> From: Alan Mikhak Date: Tue, 25 Jun 2019 10:27:08 -0700 Message-ID: Subject: Re: [PATCH] nvme-pci: Avoid leak if pci_p2pmem_virt_to_bus() returns null To: Christoph Hellwig Cc: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, keith.busch@intel.com, axboe@fb.com, sagi@grimberg.me, Palmer Dabbelt , Paul Walmsley Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 25, 2019 at 12:09 AM Christoph Hellwig wrote: > > On Mon, Jun 24, 2019 at 04:57:22PM -0700, Alan Mikhak wrote: > > Modify nvme_alloc_sq_cmds() to call pci_free_p2pmem() > > to free the memory it allocated using pci_alloc_p2pmem() > > in case pci_p2pmem_virt_to_bus() returns null. > > > > Make sure not to call pci_free_p2pmem() if pci_alloc_p2pmem() > > returned null which can happen if CONFIG_PCI_P2PDMA is not > > configured. > > Can you > I mean this patch makes sure not to call pci_free_p2pmem() if nothing was allocated by pci_alloc_p2pmem(). > > > > Signed-off-by: Alan Mikhak > > --- > > drivers/nvme/host/pci.c | 14 +++++++++----- > > 1 file changed, 9 insertions(+), 5 deletions(-) > > > > diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c > > index 524d6bd6d095..5dfa067f6506 100644 > > --- a/drivers/nvme/host/pci.c > > +++ b/drivers/nvme/host/pci.c > > @@ -1456,11 +1456,15 @@ static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq, > > > > if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) { > > nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(depth)); > > - nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev, > > - nvmeq->sq_cmds); > > - if (nvmeq->sq_dma_addr) { > > - set_bit(NVMEQ_SQ_CMB, &nvmeq->flags); > > - return 0; > > + if (nvmeq->sq_cmds) { > > + nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev, > > + nvmeq->sq_cmds); > > + if (nvmeq->sq_dma_addr) { > > + set_bit(NVMEQ_SQ_CMB, &nvmeq->flags); > > + return 0; > > + } > > + > > + pci_free_p2pmem(pdev, nvmeq->sq_cmds, SQ_SIZE(depth)); > > pci_p2pmem_virt_to_bus should only fail when > pci_p2pmem_virt_to_bus failed. That being said I think doing the > error check on pci_alloc_p2pmem instead of relying on > pci_p2pmem_virt_to_bus "passing through" the error seems odd, I'd > rather check the pci_alloc_p2pmem return value directly. Thanks Christoph. I could see the existing code should not leak but only after inspecting pci_p2pmem_virt_to_bus() and gen_pool_virt_to_phys(). I wondered what if these functions changed and broke the relation but that seems unlikely. Checking the return value directly may require less from the reader, if that's a good outcome. Alan