From patchwork Fri Jun 3 03:54:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: patrick wang X-Patchwork-Id: 12868486 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 198DDC433EF for ; Fri, 3 Jun 2022 03:54:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A281F6B0075; Thu, 2 Jun 2022 23:54:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D7396B0078; Thu, 2 Jun 2022 23:54:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 89AB26B007B; Thu, 2 Jun 2022 23:54:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7B7076B0075 for ; Thu, 2 Jun 2022 23:54:33 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5A19060A82 for ; Fri, 3 Jun 2022 03:54:33 +0000 (UTC) X-FDA: 79535557626.23.72335A3 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf04.hostedemail.com (Postfix) with ESMTP id 1A0B640070 for ; Fri, 3 Jun 2022 03:54:12 +0000 (UTC) Received: by mail-pj1-f46.google.com with SMTP id gc3-20020a17090b310300b001e33092c737so6349746pjb.3 for ; Thu, 02 Jun 2022 20:54:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NjAQ9U1RyZ6v//Z9T1/sKRQ+oNLTOeoBnmYJ+dO7aSk=; b=IHyysdxE8wrk06+yrVq66k2wvBC4vdESSMS30QlzhI3DHGQKAUXIl7V74dMRlNxQ+9 sTB/p5AMjwxBD4LIvGbBCKV/UxEGlFmPGt0PaYWmW3MJMP432ZhjnyrhSLGK7xfz7fLT W49Mm3467QKIKBD0UBGFN7qc0bvPjze0VzKhBHMfFM/L9Kj/wk4wLCV3+g6hhzSK1q/g powdBclaqBH32B5e4WVQTygGb8QKt6Cwxy9n5UCSVUObNtZGYTFmDT1y4oX+wLyRq99w Onj9IihcmOzyz04nOIZWBykW5oHPFG5gRjJ2rtZEWYRYlMrPDWMUJnAZMz7tF3c3q3x4 wgAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NjAQ9U1RyZ6v//Z9T1/sKRQ+oNLTOeoBnmYJ+dO7aSk=; b=qqDXCrdplxMmR6JuZQszolJZTy36SQ6846HE1sF42hTXgZBu6FVSpTNyekSOyYsT9y fA//7Lc4eLK+T0nl5HeKy7aYiigTK9fOW6ylhPaBQ8HHjyG/lstO4L00jZ2JiP6AeX+y UUOzITL3/P3tgYuykqTAalmk/LlBTZn4e7+jKGviXaH1riPi7pODicqM8ETZ1cJ5tMux HMpsA/NN5oA7FSyrc0rfAtEe3SBPfYhEiDxYrmbVpscUPYlpT4wlMYsKBOCrheMClEtp rEuCYLG3IzQ4HoLG5us3U8fZSxM2yFuZ6xf0y6nQNgmfSJ9S8SHvr3lRV1/e05zul7fi vIYw== X-Gm-Message-State: AOAM5320juvI889N+0ZGP3TUaKWtf6et2Xtw+rKHmkncQ2BmMvuOBj85 maCNErfL+XJQr7JdYyUbQww= X-Google-Smtp-Source: ABdhPJyRoBzhzCK/ZrLQd0WQrH4EgBt6sszDp+oB0AthomXCVEQasTnQQh8J0A+5To1VrAXOfLSVMA== X-Received: by 2002:a17:902:b18e:b0:15f:b2c:6ca with SMTP id s14-20020a170902b18e00b0015f0b2c06camr8275857plr.84.1654228472011; Thu, 02 Jun 2022 20:54:32 -0700 (PDT) Received: from localhost ([101.86.206.159]) by smtp.gmail.com with ESMTPSA id d124-20020a636882000000b003fac21dc6edsm4166400pgc.21.2022.06.02.20.54.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jun 2022 20:54:31 -0700 (PDT) From: Patrick Wang To: catalin.marinas@arm.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, yee.lee@mediatek.com, patrick.wang.shcn@gmail.com Subject: [PATCH v2 3/4] mm: kmemleak: handle address stored in object based on its type Date: Fri, 3 Jun 2022 11:54:14 +0800 Message-Id: <20220603035415.1243913-4-patrick.wang.shcn@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220603035415.1243913-1-patrick.wang.shcn@gmail.com> References: <20220603035415.1243913-1-patrick.wang.shcn@gmail.com> MIME-Version: 1.0 X-Rspam-User: Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=IHyysdxE; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of patrick.wang.shcn@gmail.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=patrick.wang.shcn@gmail.com X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 1A0B640070 X-Stat-Signature: cxi19g36t4yqi83htzjiq6mcdf61cywy X-HE-Tag: 1654228452-191567 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: Treat the address stored in object in different way according to its type: - Only use kasan_reset_tag for virtual address - Only update min_addr and max_addr for virtual address - Convert physical address to virtual address in scan_object Suggested-by: Catalin Marinas Signed-off-by: Patrick Wang --- mm/kmemleak.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 218144392446..246a70b7218f 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -297,7 +297,9 @@ static void hex_dump_object(struct seq_file *seq, warn_or_seq_printf(seq, " hex dump (first %zu bytes):\n", len); kasan_disable_current(); warn_or_seq_hex_dump(seq, DUMP_PREFIX_NONE, HEX_ROW_SIZE, - HEX_GROUP_SIZE, kasan_reset_tag((void *)ptr), len, HEX_ASCII); + HEX_GROUP_SIZE, object->flags & OBJECT_PHYS ? ptr : + kasan_reset_tag((void *)ptr), + len, HEX_ASCII); kasan_enable_current(); } @@ -389,14 +391,15 @@ static struct kmemleak_object *lookup_object(unsigned long ptr, int alias, { struct rb_node *rb = is_phys ? object_phys_tree_root.rb_node : object_tree_root.rb_node; - unsigned long untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr); + unsigned long untagged_ptr = is_phys ? ptr : (unsigned long)kasan_reset_tag((void *)ptr); while (rb) { struct kmemleak_object *object; unsigned long untagged_objp; object = rb_entry(rb, struct kmemleak_object, rb_node); - untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer); + untagged_objp = is_phys ? object->pointer : + (unsigned long)kasan_reset_tag((void *)object->pointer); if (untagged_ptr < untagged_objp) rb = object->rb_node.rb_left; @@ -643,16 +646,19 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size, raw_spin_lock_irqsave(&kmemleak_lock, flags); - untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr); - min_addr = min(min_addr, untagged_ptr); - max_addr = max(max_addr, untagged_ptr + size); + untagged_ptr = is_phys ? ptr : (unsigned long)kasan_reset_tag((void *)ptr); + if (!is_phys) { + min_addr = min(min_addr, untagged_ptr); + max_addr = max(max_addr, untagged_ptr + size); + } link = is_phys ? &object_phys_tree_root.rb_node : &object_tree_root.rb_node; rb_parent = NULL; while (*link) { rb_parent = *link; parent = rb_entry(rb_parent, struct kmemleak_object, rb_node); - untagged_objp = (unsigned long)kasan_reset_tag((void *)parent->pointer); + untagged_objp = is_phys ? parent->pointer : + (unsigned long)kasan_reset_tag((void *)parent->pointer); if (untagged_ptr + size <= untagged_objp) link = &parent->rb_node.rb_left; else if (untagged_objp + parent->size <= untagged_ptr) @@ -1202,7 +1208,9 @@ static bool update_checksum(struct kmemleak_object *object) kasan_disable_current(); kcsan_disable_current(); - object->checksum = crc32(0, kasan_reset_tag((void *)object->pointer), object->size); + object->checksum = crc32(0, object->flags & OBJECT_PHYS ? (void *)object->pointer : + kasan_reset_tag((void *)object->pointer), + object->size); kasan_enable_current(); kcsan_enable_current(); @@ -1353,6 +1361,7 @@ static void scan_object(struct kmemleak_object *object) { struct kmemleak_scan_area *area; unsigned long flags; + void *obj_ptr; /* * Once the object->lock is acquired, the corresponding memory block @@ -1364,10 +1373,15 @@ static void scan_object(struct kmemleak_object *object) if (!(object->flags & OBJECT_ALLOCATED)) /* already freed object */ goto out; + + obj_ptr = object->flags & OBJECT_PHYS ? + __va((void *)object->pointer) : + (void *)object->pointer; + if (hlist_empty(&object->area_list) || object->flags & OBJECT_FULL_SCAN) { - void *start = (void *)object->pointer; - void *end = (void *)(object->pointer + object->size); + void *start = obj_ptr; + void *end = obj_ptr + object->size; void *next; do {