Received: by 10.223.176.46 with SMTP id f43csp2042862wra; Sun, 21 Jan 2018 09:54:48 -0800 (PST) X-Google-Smtp-Source: AH8x224NTKEt2LVNPkHjAxYdSpUaLZEWOp9BOTfeuqg/Udcb3MonDf8jw+dMHw40ENeGWbSKe9p2 X-Received: by 2002:a17:902:3083:: with SMTP id v3-v6mr2146773plb.426.1516557288584; Sun, 21 Jan 2018 09:54:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516557288; cv=none; d=google.com; s=arc-20160816; b=dPmo4YAgALzr7/fCJ63EO9YlBnILFz160upfydR3da8ze7ui2n/Htgv5wu5u4zdRPk rxoQKoWuEZBvpV0rRsvZLbSCwxuA+eiDpJ3vMDyJlcgORGwRqqBUEzle3QTWZjZukQv0 shnv6NOilY2HUXJRBzR+b34CL4VbnIvCI5dSZEAJWuPxy/R5o7uREMq3XkJ2ElnHLPpi PA9hHexRMFwbeRz0QgibLiXjgYgGNEGxfadYM8lrLxD32ghy1e7zyIXGdpRZXhHDdPPW HcuVpBDH/l6ofCyzUyc8dZmYzi2g6si7FJmDXxVgEs9mTQYVhtl4U4JhkupxD/fv/OxL lq/w== 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 :references:in-reply-to:mime-version:arc-authentication-results; bh=3ooVh3l36F16VNo3EYxMaowIz0QH50/xaAw2opR/NAs=; b=WaRT4xyEnaK40vDgIVaakHHTVdk6pHPmeO/Oola3TbD+EIRy47xVSO8ErZmxfCajf7 7mf1JB3yjag2W9XSxcJXe8wShUGlEUOdasmJtdH1wNf1hduTukRag/a/ufN2/gmx62JK ORY/tyA5wQT755f2WCGD/utr85mbycTX6Uj43JXp15icZ9nx0PnAP0twjEEA+o9hrb2u qtPN884whgWx72RZI1ztl/TtV5UvMQOzEExPcotsx1IpPZNh+AANfY2txn72mPhHbT2X 6jdKas9BqQ/Y192x7T90vlqSbOVAJWIkL7aTdCBfhnWi1hSl6md2MVRMIScb7vx6uIWU IOIg== ARC-Authentication-Results: i=1; mx.google.com; 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 m9-v6si2691343plk.486.2018.01.21.09.54.34; Sun, 21 Jan 2018 09:54:48 -0800 (PST) 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; 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 S1751212AbeAURyK (ORCPT + 99 others); Sun, 21 Jan 2018 12:54:10 -0500 Received: from outprodmail01.cc.columbia.edu ([128.59.72.39]:42798 "EHLO outprodmail01.cc.columbia.edu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750885AbeAURyJ (ORCPT ); Sun, 21 Jan 2018 12:54:09 -0500 Received: from hazelnut (hazelnut.cc.columbia.edu [128.59.213.250]) by outprodmail01.cc.columbia.edu (8.14.4/8.14.4) with ESMTP id w0LHo7Gu042659 for ; Sun, 21 Jan 2018 12:54:08 -0500 Received: from hazelnut (localhost.localdomain [127.0.0.1]) by hazelnut (Postfix) with ESMTP id A57ED87 for ; Sun, 21 Jan 2018 12:54:09 -0500 (EST) Received: from sendprodmail01.cc.columbia.edu (sendprodmail01.cc.columbia.edu [128.59.72.13]) by hazelnut (Postfix) with ESMTP id 3CDA48D for ; Sun, 21 Jan 2018 12:54:03 -0500 (EST) Received: from mail-wr0-f200.google.com (mail-wr0-f200.google.com [209.85.128.200]) by sendprodmail01.cc.columbia.edu (8.14.4/8.14.4) with ESMTP id w0LHs1An020446 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NOT) for ; Sun, 21 Jan 2018 12:54:02 -0500 Received: by mail-wr0-f200.google.com with SMTP id k13so5062625wrd.7 for ; Sun, 21 Jan 2018 09:54:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=3ooVh3l36F16VNo3EYxMaowIz0QH50/xaAw2opR/NAs=; b=PU6CWWbiWORhWCBJmBADZqB/iIKkMjv3NSQ9ZWJeG9Z/EE21xrpRNU/6u6PgTkLPY1 AE5teAF8LjBjD2s3Z8Lav4a6ANJYlj4cxS0VBxe3T4vJiGoMS1W422WMWSYTK487vq34 Ofp9h5gte2navovjZBPecnJTH1vBqYcpzaPfCxcSYneWb03W16s92pa3rNhKfYX6RMWr HtUaIZWwNc0uhn0ae7VNU3+su5mAsldTz6+3/ZtsYI5QxcF+UHVS9xO/6r+mabijICw8 PDGQf6CF5IPVnnlY5LKSmurcvBTgVe1QJqUcqW7GUSo3YL6qk0Dk3MHPso/4zNTsGGYC G5qg== X-Gm-Message-State: AKwxyteN46OUOrXx5PwiQ9PrGbbjaIF5SHIzoKIJj1AfKecPh8BQefXH Q0QBHKBdEFl9/6DVM9O+MtUiNwj1Mi4POxZyKWxHn8A/i5MysVthQH6n9XhvDWkgArO9TYgjnay ecEl0VMCv9n69/+RdzIDOxy8jM+cwIrISVpdU/Ee1lVSmxK/z X-Received: by 10.80.212.14 with SMTP id t14mr9568433edh.8.1516557241358; Sun, 21 Jan 2018 09:54:01 -0800 (PST) X-Received: by 10.80.212.14 with SMTP id t14mr9568406edh.8.1516557240823; Sun, 21 Jan 2018 09:54:00 -0800 (PST) MIME-Version: 1.0 Received: by 10.80.241.82 with HTTP; Sun, 21 Jan 2018 09:54:00 -0800 (PST) In-Reply-To: References: From: Shankara Pailoor Date: Sun, 21 Jan 2018 09:54:00 -0800 Message-ID: Subject: Re: Possible Memory Leak in KCOV Linux 4.15-rc1 To: Dmitry Vyukov Cc: LKML , syzkaller , Andrew Zhu Aday Content-Type: text/plain; charset="UTF-8" X-No-Spam-Score: Local X-Scanned-By: MIMEDefang 2.78 on 128.59.72.13 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Below is a reproducer. #define _GNU_SOURCE #include #include #include #include #include #define KCOV_INIT_TRACE _IOR('c', 1, unsigned long) #define KCOV_ENABLE _IO('c', 100) #define KCOV_DISABLE _IO('c', 101) #define COVER_SIZE (16 << 20) void kcov_setup() { unsigned long *cover; int fd; fd = open("/sys/kernel/debug/kcov", O_RDWR); if (fd == -1) perror("open"), exit(1); if (ioctl(fd, KCOV_INIT_TRACE, COVER_SIZE)) perror("ioctl"), exit(1); cover = (unsigned long*)mmap(NULL, COVER_SIZE * sizeof(unsigned long), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if ((void*)cover == MAP_FAILED) perror("mmap"), exit(1); if (ioctl(fd, KCOV_ENABLE, 0)) perror("ioctl"), exit(1); } void main() { int i; for (i = 0; i < 4; i++) kcov_setup(); sleep(10); } On Sun, Jan 21, 2018 at 1:11 AM, Shankara Pailoor wrote: > Hi Dmitry, > > The leaks went away when I disabled and closed the old file > descriptors before opening new ones. > > The patch you sent wouldn't work because t is not initialized at the > line. This seems to work for me > > diff --git a/kernel/kcov.c b/kernel/kcov.c > index 7594c03..1397006 100644 > --- a/kernel/kcov.c > +++ b/kernel/kcov.c > @@ -371,6 +371,8 @@ static int kcov_ioctl_locked(struct kcov *kcov, > unsigned int cmd, > else > return -EINVAL; > t = current; > + if (!t->kcov) > + return -EBUSY; > /* Cache in task struct for performance. */ > t->kcov_size = kcov->size; > t->kcov_area = kcov->area; > > On Sat, Jan 20, 2018 at 7:06 AM, Dmitry Vyukov wrote: >> On Sat, Jan 20, 2018 at 4:01 PM, Shankara Pailoor wrote: >>> Hi Dmitry, >>> >>> I will try and get something to you tomorrow. Just wondering, but what >>> happens to the old struct kcov if a task opens /sys/kernel/debug/kcov >>> twice? I am looking here >>> https://elixir.free-electrons.com/linux/v4.15-rc8/source/kernel/kcov.c#L381 >>> and I don't see where the previous struct would get freed. >> >> Good question. Perhaps we need something like: >> >> diff --git a/kernel/kcov.c b/kernel/kcov.c >> index 7594c033d98a..c76498018500 100644 >> --- a/kernel/kcov.c >> +++ b/kernel/kcov.c >> @@ -358,7 +358,7 @@ static int kcov_ioctl_locked(struct kcov *kcov, >> unsigned int cmd, >> */ >> if (kcov->mode != KCOV_MODE_INIT || !kcov->area) >> return -EINVAL; >> - if (kcov->t != NULL) >> + if (kcov->t != NULL || t->kcov != NULL) >> return -EBUSY; >> if (arg == KCOV_TRACE_PC) >> kcov->mode = KCOV_MODE_TRACE_PC; >> >> >> >>> On Sat, Jan 20, 2018 at 4:38 AM, Dmitry Vyukov wrote: >>>> On Fri, Jan 19, 2018 at 8:29 PM, Shankara Pailoor wrote: >>>>> Hi Dmitry, >>>>> >>>>> I added support for kcov in strace and I have been tracing a fairly >>>>> large program but after a little while, I notice that when I mmap a >>>>> new cover buffer, the call fails with ENOMEM. After killing the >>>>> program, I try and rerun and I notice that there is nearly no memory >>>>> on the system. When I do a kmemleak scan I get the following reports: >>>>> >>>>> I believe the problem occurs when I try and setup the kcov buffer >>>>> again after an exec. Instead of reusing the old file descriptor I open >>>>> kcov again within that process. In that case, I don't know what >>>>> happens to the old kcov struct. >>>>> >>>>> I don't see a maintainers list for kcov so I decided to email you >>>>> directly. Let me know what more information I can provide. >>>> >>>> >>>> Hi Shankara, >>>> >>>> Looks bad. Can you provide a reproducer? >>>> We extensively use kcov with syzkaller, but have not observed such >>>> leaks. Also I don't see anything obvious in the code. >>>> >>>> Thanks >>>