Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp1954816ybg; Thu, 30 Jul 2020 07:02:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwxweztOYqvTtTa88NzFYhEI+iXH31jF2aBc2BBfnupiiwSnJqUkPIURcWEJ/P5gMAjk+dt X-Received: by 2002:a17:906:c34e:: with SMTP id ci14mr2906226ejb.335.1596117776690; Thu, 30 Jul 2020 07:02:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596117776; cv=none; d=google.com; s=arc-20160816; b=hmzDXijRctjAtvSWaBdEqQSX17UYpYvTO3k0A82eAo/zFPQaF3R9GMZMlte6IMKacb itMviH1iXI7HJhjeON9ftiFJ9dwn301P3aHbmvL6d1bh5B+aLRE6N2QhfOt+GwZeeRKI 3Uuw/f/qNb0D/mg+m4nuGfggLoJkr4Oe1ZCiPGbwmUZZSCbjqRjpUK7VQyz6AxhE5B9U JTs6UazybiNHM+YC26+wYl/OzMkB3hoUHgbRxQWsB7kFEg/TvDy2jzpBwn2MFQeite67 0FL67H3DkY2ZF50bcXX5LX+oXJvawGVH6qVL9A6TznZ/wIPt+vCzhMTOV0GJNrOeBWNl aUxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=qEx5et8b6p1HS0hsOSrFx3xfl/v5dgtDHG7BiH9bd2I=; b=wxWMxRUAby3mlKlE7f/kQj21NBL4Up0z6tzQLrEgihH7VWaOccXunvd9f+O9gNtEky xryv1Mt+cepFle/MvNIYCfO9Lxz+Aw9/20D8BW5ZY6VsvnlymYEZx/6A0dIwvZMnTU71 bg2XrEtEZMJ68GhrrbDrR9Sm8WIFW91JGxEan950gt+zwT1VN0qLPhcSbioDlUrWE77K 3FROOiKkdopnEH/MZhnxR8nC1cTVSchJLqe+3VnsiA8IBQZftjUrbq1Ie2AL5Nr42k5R 5yBTvx6uif9EDiu97FjLuFvSIKhfhqSM8D4kv5skJrX5IeBmWZF2LdUWpU+5QRAZMe5w 4NmQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m2si3225708ejr.24.2020.07.30.07.02.31; Thu, 30 Jul 2020 07:02:56 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727966AbgG3OCU (ORCPT + 99 others); Thu, 30 Jul 2020 10:02:20 -0400 Received: from mail-lf1-f65.google.com ([209.85.167.65]:39136 "EHLO mail-lf1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726275AbgG3OCR (ORCPT ); Thu, 30 Jul 2020 10:02:17 -0400 Received: by mail-lf1-f65.google.com with SMTP id v15so10472340lfg.6 for ; Thu, 30 Jul 2020 07:02:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qEx5et8b6p1HS0hsOSrFx3xfl/v5dgtDHG7BiH9bd2I=; b=nO615pugtYFdsIis/K5Bwa6tVpDzd2tEFVe2ObgnKaouK96ca8hCscS5VCjIfGOZQ2 Hq9gYSWvfTKTtH7OQscZvevqD1j3SbLX+PxJiVJvJNBFki9CPGzbp6pfP74k0IlFM+pM 0NXTBfZznNAUCr6dNObMv4rHxVIf9QDwXnKoMGWQ4VItOCmwjooVqmHykz7+zvNj3Xqv 7lbGXCK8ynox3QxmSuv5/N+UiHiod5YfZ4XGXVd0sM5vLpCstM7JywEIfaWE0QFtB9UK sgo4GHl8S5jb1M7EBioYhQOyKFLSPsmYqyTimBpSDziBnYnmJNnVW407M+sNjYf1bz/V aOUQ== X-Gm-Message-State: AOAM533RUJimazdG/28XIBA8SUXBQUZ30p1KkVGezbmvPQEKTUwKA3ek KxbLXVx2fGBsQlUNaD//vjv7ZXFuMoM= X-Received: by 2002:ac2:5991:: with SMTP id w17mr1754567lfn.153.1596117733800; Thu, 30 Jul 2020 07:02:13 -0700 (PDT) Received: from localhost.localdomain (broadband-37-110-38-130.ip.moscow.rt.ru. [37.110.38.130]) by smtp.googlemail.com with ESMTPSA id l12sm1064876ljj.43.2020.07.30.07.02.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jul 2020 07:02:12 -0700 (PDT) From: Denis Efremov To: Julia Lawall Cc: Denis Efremov , cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org Subject: [PATCH v3] coccinelle: api: add kvfree script Date: Thu, 30 Jul 2020 17:01:50 +0300 Message-Id: <20200730140150.8255-1-efremov@linux.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200605204237.85055-1-efremov@linux.com> References: <20200605204237.85055-1-efremov@linux.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Check that alloc and free types of functions match each other. Signed-off-by: Denis Efremov --- Changes in v2: - Lines are limited to 80 characters where possible - Confidence changed from High to Medium because of fs/btrfs/send.c:1119 false-positive - __vmalloc_area_node() explicitly excluded from analysis instead of !(file in "mm/vmalloc.c") condition Changes in v3: - prints style in org && report modes changed for python2 scripts/coccinelle/api/kvfree.cocci | 227 ++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 scripts/coccinelle/api/kvfree.cocci diff --git a/scripts/coccinelle/api/kvfree.cocci b/scripts/coccinelle/api/kvfree.cocci new file mode 100644 index 000000000000..7c396daeacad --- /dev/null +++ b/scripts/coccinelle/api/kvfree.cocci @@ -0,0 +1,227 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// +/// Check that kvmalloc'ed memory is freed by kfree functions, +/// vmalloc'ed by vfree functions and kvmalloc'ed by kvfree +/// functions. +/// +// Confidence: Medium +// Copyright: (C) 2020 Denis Efremov ISPRAS +// Options: --no-includes --include-headers +// + +virtual patch +virtual report +virtual org +virtual context + +@initialize:python@ +@@ +# low-level memory api +filter = frozenset(['__vmalloc_area_node']) + +def relevant(p): + return not (filter & {el.current_element for el in p}) + +@choice@ +expression E, E1; +position kok, vok; +@@ + +( + if (...) { + ... + E = \(kmalloc@kok\|kzalloc@kok\|krealloc@kok\|kcalloc@kok\| + kmalloc_node@kok\|kzalloc_node@kok\|kmalloc_array@kok\| + kmalloc_array_node@kok\|kcalloc_node@kok\)(...) + ... + } else { + ... + E = \(vmalloc@vok\|vzalloc@vok\|vmalloc_user@vok\|vmalloc_node@vok\| + vzalloc_node@vok\|vmalloc_exec@vok\|vmalloc_32@vok\| + vmalloc_32_user@vok\|__vmalloc@vok\|__vmalloc_node_range@vok\| + __vmalloc_node@vok\)(...) + ... + } +| + E = \(kmalloc\|kzalloc\|krealloc\|kcalloc\|kmalloc_node\|kzalloc_node\| + kmalloc_array\|kmalloc_array_node\|kcalloc_node\)(...) + ... when != E = E1 + when any + if (\(!E\|E == NULL\)) { + ... + E = \(vmalloc@vok\|vzalloc@vok\|vmalloc_user@vok\|vmalloc_node@vok\| + vzalloc_node@vok\|vmalloc_exec@vok\|vmalloc_32@vok\| + vmalloc_32_user@vok\|__vmalloc@vok\|__vmalloc_node_range@vok\| + __vmalloc_node@vok\)(...) + ... + } +) + +@opportunity depends on !patch@ +expression E, E1, size; +position p : script:python() { relevant(p) }; +@@ + +( +* if (\(size <= E1\|size < E1\|size = E1\|size > E1\) || ...)@p { + ... + E = \(kmalloc\|kzalloc\|krealloc\|kcalloc\|kmalloc_node\|kzalloc_node\| + kmalloc_array\|kmalloc_array_node\|kcalloc_node\)(..., size, ...) + ... + } else { + ... + E = \(vmalloc\|vzalloc\|vmalloc_user\|vmalloc_node\|vzalloc_node\| + vmalloc_exec\|vmalloc_32\|vmalloc_32_user\|__vmalloc\| + __vmalloc_node_range\|__vmalloc_node\)(..., size, ...) + ... + } +| + E = \(kmalloc\|kzalloc\|krealloc\|kcalloc\|kmalloc_node\|kzalloc_node\| + kmalloc_array\|kmalloc_array_node\|kcalloc_node\)(..., size, ...) + ... when != E = E1 + when != size = E1 + when any +* if (\(!E\|E == NULL\))@p { + ... + E = \(vmalloc\|vzalloc\|vmalloc_user\|vmalloc_node\|vzalloc_node\| + vmalloc_exec\|vmalloc_32\|vmalloc_32_user\|__vmalloc\| + __vmalloc_node_range\|__vmalloc_node\)(..., size, ...) + ... + } +) + +@vfree depends on !patch@ +expression E; +position k != choice.kok; +position p; +@@ + +* E = \(kmalloc@k\|kzalloc@k\|krealloc@k\|kcalloc@k\|kmalloc_node@k\| + kzalloc_node@k\|kmalloc_array@k\|kmalloc_array_node@k\| + kcalloc_node@k\)(...) + ... when != if (...) { ... E = \(vmalloc\|vzalloc\|vmalloc_user\|vmalloc_node\|vzalloc_node\|vmalloc_exec\|vmalloc_32\|vmalloc_32_user\|__vmalloc\|__vmalloc_node_range\|__vmalloc_node\)(...); ... } + when != is_vmalloc_addr(E) + when any +* \(vfree\|vfree_atomic\|kvfree\)(E)@p + +@pvfree depends on patch exists@ +expression E; +position k != choice.kok; +@@ + + E = \(kmalloc@k\|kzalloc@k\|krealloc@k\|kcalloc@k\|kmalloc_node@k\| + kzalloc_node@k\|kmalloc_array@k\|kmalloc_array_node@k\| + kcalloc_node@k\)(...) + ... when != if (...) { ... E = \(vmalloc\|vzalloc\|vmalloc_user\|vmalloc_node\|vzalloc_node\|vmalloc_exec\|vmalloc_32\|vmalloc_32_user\|__vmalloc\|__vmalloc_node_range\|__vmalloc_node\)(...); ... } + when != is_vmalloc_addr(E) + when any +- \(vfree\|vfree_atomic\|kvfree\)(E) ++ kfree(E) + +@kfree depends on !patch@ +expression E; +position v != choice.vok; +position p; +@@ + +* E = \(vmalloc@v\|vzalloc@v\|vmalloc_user@v\|vmalloc_node@v\|vzalloc_node@v\| + vmalloc_exec@v\|vmalloc_32@v\|vmalloc_32_user@v\|__vmalloc@v\| + __vmalloc_node_range@v\|__vmalloc_node@v\)(...) + ... when != !is_vmalloc_addr(E) + when any +* \(kfree\|kzfree\|kvfree\)(E) + +@pkfree depends on patch exists@ +expression E; +position v != choice.vok; +@@ + + E = \(vmalloc@v\|vzalloc@v\|vmalloc_user@v\|vmalloc_node@v\|vzalloc_node@v\| + vmalloc_exec@v\|vmalloc_32@v\|vmalloc_32_user@v\|__vmalloc@v\| + __vmalloc_node_range@v\|__vmalloc_node@v\)(...) + ... when != !is_vmalloc_addr(E) + when any +- \(kfree\|kvfree\)(E) ++ vfree(E) + +@kvfree depends on !patch@ +expression E; +position p, k; +@@ + +* E = \(kvmalloc\|kvzalloc\|kvcalloc\|kvzalloc_node\|kvmalloc_node\| + kvmalloc_array\)(...)@k + ... when != is_vmalloc_addr(E) + when any +* \(kfree\|kzfree\|vfree\|vfree_atomic\)(E)@p + +@pkvfree depends on patch exists@ +expression E; +@@ + + E = \(kvmalloc\|kvzalloc\|kvcalloc\|kvzalloc_node\|kvmalloc_node\| + kvmalloc_array\)(...) + ... when != is_vmalloc_addr(E) + when any +- \(kfree\|vfree\)(E) ++ kvfree(E) + +@script: python depends on report@ +k << vfree.k; +p << vfree.p; +@@ + +msg = "WARNING: kmalloc is used to allocate this memory at line %s" % (k[0].line) +coccilib.report.print_report(p[0], msg) + +@script: python depends on org@ +k << vfree.k; +p << vfree.p; +@@ + +msg = "WARNING: kmalloc is used to allocate this memory at line %s" % (k[0].line) +coccilib.org.print_todo(p[0], msg) + +@script: python depends on report@ +v << kfree.v; +p << kfree.p; +@@ + +msg = "WARNING: vmalloc is used to allocate this memory at line %s" % (v[0].line) +coccilib.report.print_report(p[0], msg) + +@script: python depends on org@ +v << kfree.v; +p << kfree.p; +@@ + +msg = "WARNING: vmalloc is used to allocate this memory at line %s" % (v[0].line) +coccilib.org.print_todo(p[0], + +@script: python depends on report@ +k << kvfree.k; +p << kvfree.p; +@@ + +msg = "WARNING: kvmalloc is used to allocate this memory at line %s" % (k[0].line) +coccilib.report.print_report(p[0], msg) + +@script: python depends on org@ +k << kvfree.k; +p << kvfree.p; +@@ + +msg = "WARNING: kvmalloc is used to allocate this memory at line %s" % (k[0].line) +coccilib.org.print_todo(p[0], msg) + +@script: python depends on report@ +p << opportunity.p; +@@ + +coccilib.report.print_report(p[0], "WARNING: opportunity for kvmalloc") + +@script: python depends on org@ +p << opportunity.p; +@@ + +coccilib.org.print_todo(p[0], "WARNING: opportunity for kvmalloc") -- 2.26.2