From nobody Thu Sep 07 21:36:02 2023 X-Original-To: bugs@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RhXc30NHQz4sP08 for ; Thu, 7 Sep 2023 21:36:03 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RhXc22sz3z3RyJ for ; Thu, 7 Sep 2023 21:36:02 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1694122562; a=rsa-sha256; cv=none; b=rBqVLVWV0qwODDbK6SBjGi1z9yqfddd+NrcPxYZOIdUlgHwXt/oqNdif3hKfFnE67y+4gs gxWvB2GfRS/mQCf9naWgIniAW5m0mtmMYPQ76th0RidPuaXp6LX8A7SveX43eEE/nuB0eT 7C0uuj6KvVwh6TAJshcthH0gIbf4mGENDd7H8InSObCvFHBdqWrMhlhqs2OyyoMUGQvl2A S9k+qxc0nhoTjTubhpfF/eZdyZFvlyQ135MzdUVl1QXB8pzY70z1AuSArtRqKTAAqQJrZf uIDD6E5cv1z4zmnqTXOdAyDwH1mFum48VtBXl+3357CdbJQPsBbajKfeD0ymmw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694122562; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=i1zgbNxSKajeTlK5pjOY93xyhTRaoUEGfLDfyoF1UTQ=; b=JZFAfL60/dDYa6nDGLJstJ1zpK+EDRRabfOtR0A1qp510Zslc/mEO7MMuOAvM9nICy4c+s 9zQ/Y10GAFsyBEVaDNirs0+BykLcDHiA0ayhg3FiwzBTv/NYl2YtI7m9MRyILXhan7PSam Hu1begFcxlXVtpg2XPaHRiXWTbqncarCV4XmCtSDSceMo+bg5ahXE0ovCuX2y/9RWlpmqZ vbF1mG3kWY8dnX943ZN5zy+p2ANSIL+pMoMh40CE1DxY/cWFC6Mhtu9IkaQQ5qDoof0ZHK CivMDSF77718cYdLdbr6TsARfwnAn74nHKNkPquHi1dw6Whwx/zYKk806cHQjQ== Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2610:1c1:1:606c::50:1d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RhXc21y39z1ss for ; Thu, 7 Sep 2023 21:36:02 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org ([127.0.1.5]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id 387La27A000979 for ; Thu, 7 Sep 2023 21:36:02 GMT (envelope-from bugzilla-noreply@freebsd.org) Received: (from www@localhost) by kenobi.freebsd.org (8.15.2/8.15.2/Submit) id 387La2wK000978 for bugs@FreeBSD.org; Thu, 7 Sep 2023 21:36:02 GMT (envelope-from bugzilla-noreply@freebsd.org) X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f From: bugzilla-noreply@freebsd.org To: bugs@FreeBSD.org Subject: [Bug 273626] Memory leak in ioctl nvme passthrough commands Date: Thu, 07 Sep 2023 21:36:02 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: kern X-Bugzilla-Version: 13.2-STABLE X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Some People X-Bugzilla-Who: david.sloan@eideticom.com X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: bugs@FreeBSD.org X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated List-Id: Bug reports List-Archive: https://lists.freebsd.org/archives/freebsd-bugs List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-bugs@freebsd.org MIME-Version: 1.0 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D273626 Bug ID: 273626 Summary: Memory leak in ioctl nvme passthrough commands Product: Base System Version: 13.2-STABLE Hardware: Any OS: Any Status: New Severity: Affects Some People Priority: --- Component: kern Assignee: bugs@FreeBSD.org Reporter: david.sloan@eideticom.com When running nvme passthrough commands through the ioctl interface buffers = are never released from the kernel and increase the wired memory count permanen= tly. This will eventually lead to system lockup if enough buffers are sent throu= gh the passthrough command interface. This can be replicated with C program at= the end of this bug report, which will leak 512 KB of memory per execution. We = have tested this on 13.2 and 15-current with the same result. On inspection of sys/dev/nvme/nvme_ctrlr.c it appears that nvme_ctrlr_passthrough_cmd() is missing a call to vunmapbuf() on exit when = user buffers are mapped. Applying the diff: diff --git a/sys/dev/nvme/nvme_ctrlr.c b/sys/dev/nvme/nvme_ctrlr.c index c4a75090174..608b738a1ff 100644 --- a/sys/dev/nvme/nvme_ctrlr.c +++ b/sys/dev/nvme/nvme_ctrlr.c @@ -1361,8 +1361,9 @@ nvme_ctrlr_passthrough_cmd(struct nvme_controller *ct= rlr, mtx_sleep(pt, mtx, PRIBIO, "nvme_pt", 0); mtx_unlock(mtx); -err: if (buf !=3D NULL) { + vunmapbuf(buf); +err: uma_zfree(pbuf_zone, buf); PRELE(curproc); } Resolves the issue. Example program to reproduce the issue: #include #include #include #include #include #include #include #define SZ_512K (512 * 1024) int nvme_read(int fd, void *data, size_t len, uint64_t pos) { // Assumes device with 512 byte lbas struct nvme_pt_command pt =3D { .cmd =3D { .opc =3D NVME_OPC_READ, // LBA .cdw10 =3D (pos / 512) & 0xffffffff, .cdw11 =3D (pos / 512) >> 32, // nblocks .cdw12 =3D len / 512 - 1, }, .buf =3D data, .len =3D len, .is_read =3D 1, }; return ioctl(fd, NVME_PASSTHROUGH_CMD, &pt); } int main(int argc, const char **argv) { void *buf; int rc; int fd; if (argc !=3D 2) { fprintf(stderr, "Expected single nvme namespace argument\n"= ); exit(1); } fd =3D open(argv[1], O_RDWR); if (fd =3D=3D -1) { fprintf(stderr, "error opening device %s: %m\n", argv[1]); exit(1); } rc =3D posix_memalign(&buf, 4096, SZ_512K); if (rc) { errno =3D rc; fprintf(stderr, "error allocating buffer %m\n"); exit(1); } rc =3D nvme_read(fd, buf, SZ_512K, 0); if (rc) { fprintf(stderr, "error reading from nvme device: %m\n"); exit(1); } free(buf); close(fd); return 0; } --=20 You are receiving this mail because: You are the assignee for the bug.=