Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp6488676ioo; Thu, 2 Jun 2022 07:27:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwQH+xNC4GN2PH8/D75s6FBgdYi68otquFkw8V11gm/DG4MsgJ4S1AZ5SSnMPnmpvP8eY84 X-Received: by 2002:a17:907:7f26:b0:6ff:ab8:e85 with SMTP id qf38-20020a1709077f2600b006ff0ab80e85mr4446473ejc.281.1654180050448; Thu, 02 Jun 2022 07:27:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654180050; cv=none; d=google.com; s=arc-20160816; b=la2Clsm5nFAmEnIPGdZHVZvKVCD2tcTw+W6iN92rfL39QhE+svMzjE1GxwIbo0QxXc wmW10HVvxcNGWWPT9NpdT0jvvXT1nH78RoELkeSeOOBRZC/o/oVnHV8lrxzNZ6J2hDCj 8O0LEd7XSH6A1ZBNdtmpumO4NaJv3Dnj7BvFZhOJKLop/4sE6IQnWRx7fkZsNvxqvlT1 90gHtRp12Sk0/y1SCAEi7WDiLD0ijc2uXUPToA7KgoLUReCQqZHPwZKy2pKkCkx5ROIh xKboiiBMBUppQLQuQlQIptJ5BzNh1Lb8dAAURXfYO9/o6dcU4U6jFRe1vQ+6rerFsyAT 7s4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=RUd3SPNTBG11Pw4ONwPRjvgnGE2d3p2dtoAJcfU9H9o=; b=vUcfXHzkVYGL9vdqLRHSqtCvgR+x+j2/LMmS7j2Ru02Q/cddmmJrVD0ttbGyeetgV/ 2qFAZkdBQNn52OAA1rfNT4v2cglEK6V6+a4oNW5CJFOvX4huqG/F0eaeZ7MO2P03q72T /jpPofYfRUaVIybAmVsS1oTUORZ2Nrv45Fqr3Emu73+Y9YX6+9GTaZy+ApHJZjZ414sL AnPA9lI4JHvz0P+ppjgXYtJz5C5224omIEARgXKvioNWeWUogxLx6icpZALR3uzXqWDn RuhREljMZciisDx2DsHUFv/VHwCneRTUFSkiEni51WrPm/3SEJyR87T6iP2FJ+7Pv0Zm 8+SA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=nlFi7M3t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a14-20020a170906274e00b00705234e632asi4425198ejd.170.2022.06.02.07.27.02; Thu, 02 Jun 2022 07:27:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=nlFi7M3t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230003AbiFBKWZ (ORCPT + 99 others); Thu, 2 Jun 2022 06:22:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232285AbiFBKWX (ORCPT ); Thu, 2 Jun 2022 06:22:23 -0400 Received: from mail-yw1-x112e.google.com (mail-yw1-x112e.google.com [IPv6:2607:f8b0:4864:20::112e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AE0F2ADF49 for ; Thu, 2 Jun 2022 03:22:22 -0700 (PDT) Received: by mail-yw1-x112e.google.com with SMTP id 00721157ae682-30fdbe7467cso11267277b3.1 for ; Thu, 02 Jun 2022 03:22:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=RUd3SPNTBG11Pw4ONwPRjvgnGE2d3p2dtoAJcfU9H9o=; b=nlFi7M3tVp4pU0E+EsVFl71MIOQgnjG/VIgFEYoBqd5wL5HW85p7qwOMrc7kSxxMSD tKziZmGmQZNze4UEDfqNLwkQAl6+Vpn9cR0R2CPRBmg9OHKSUDbyaKag6NVI/sOg5XRL FmQqV6ZOW1JLooFruZDqNqFZSeLVlcO2KGhyt6h5Iy+sjbY0rEd8cYIK6RaOYM+W5PYJ xmv3UMY/zCWm2gO5ZjXB9ZkuvOCOQJPcZLW8Ya75ElBU9OsmLO5NfeZcoNcXWrtbfIX/ hWi6KD2Yf2SivFGS8G2PIKWuEVRjUaoA+VuPStNaEEShkULE92qVBimRO2q7wdRHgcge LHVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=RUd3SPNTBG11Pw4ONwPRjvgnGE2d3p2dtoAJcfU9H9o=; b=MQu4nsCBrNN0mwUR0PM1ZNFFaa9GKI/z3elQbIPcTasIjFZqWhXabLoWSb5WAZA67Z 0OtYtxtOyLYqefdnCNdT3bWkLRUKJnyNNwyh1YT0mj4vaXEAnWVsNSmm44XiPZ52ddM8 Td037jAfOI7TIhr6WMyW8sjMEVWhO7zHGPKEGAwbx9eWEv0SMxnTTjn+LA7v8CVWre4p NZHhfrvYwpxwfAZcXnhBL5uBaLBbH84HsNxcXRMfKSP8e/5l4I9UfP17iHun9fy2Zml/ 8NoqNwD6wYqAyoB76tMr9xPyBqILClXb6IneAwLfc+VlrG5gLirN2LuxykobP81xFV14 uxzg== X-Gm-Message-State: AOAM531vnIxSVO6cg4OO8k8IscMTs2B76tg7cyMuLBOg3H/h1VsR79Zb QIaIKDUSx5s+F8Wch5NBqBYsoGycXDPmPrr0Hw0= X-Received: by 2002:a0d:f502:0:b0:2ff:3e75:b4ea with SMTP id e2-20020a0df502000000b002ff3e75b4eamr4499151ywf.171.1654165341322; Thu, 02 Jun 2022 03:22:21 -0700 (PDT) MIME-Version: 1.0 References: <20220531150823.1004101-1-patrick.wang.shcn@gmail.com> <99faf6b0-30bf-f87c-2620-1eafb4eac1ac@gmail.com> In-Reply-To: From: patrick wang Date: Thu, 2 Jun 2022 18:22:10 +0800 Message-ID: Subject: Re: [PATCH] mm: kmemleak: check boundary of objects allocated with physical address when scan To: Catalin Marinas Cc: Andrew Morton , "open list:MEMORY MANAGEMENT" , linux-kernel@vger.kernel.org, Yee Lee Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 2, 2022 at 12:13 AM Catalin Marinas wrote: > > On Wed, Jun 01, 2022 at 06:24:34PM +0800, Patrick Wang wrote: > > On 2022/6/1 00:29, Catalin Marinas wrote: > > > On Tue, May 31, 2022 at 11:08:23PM +0800, Patrick Wang wrote: > > > > + if (kmemleak_enabled && (unsigned long)__va(phys) >= PAGE_OFFSET && > > > > + !IS_ERR(__va(phys))) > > > > + /* create object with OBJECT_PHYS flag */ > > > > + create_object((unsigned long)__va(phys), size, min_count, > > > > + gfp, true); > > > > > > Do we still need to check for __va(phys) >= PAGE_OFFSET? Also I don't > > > think IS_ERR(__va(phys)) makes sense, we can't store an error in a > > > physical address. The kmemleak_alloc_phys() function is only called on > > > successful allocation, so shouldn't bother with error codes. > > > > In this commit: > > 972fa3a7c17c(mm: kmemleak: alloc gray object for reserved > > region with direct map) > > > > The kmemleak_alloc_phys() function is called directly by passing > > physical address from devicetree. So I'm concerned that could > > __va() => __pa() convert always get the phys back? I thought > > check for __va(phys) might help, but it probably dosen't work > > and using IS_ERR is indeed inappropriate. > > > > We might have to store phys in object and convert it via __va() > > for normal use like: > > > > #define object_pointer(obj) \ > > (obj->flags & OBJECT_PHYS ? (unsigned long)__va((void *)obj->pointer) \ > > : obj->pointer) > > In the commit you mentioned, the kmemleak callback is skipped if the > memory is marked no-map. > > But you have a point with the va->pa conversion. On 32-bit > architectures, the __va() is no longer valid if the pfn is above > max_low_pfn. So whatever we add to the rbtree may be entirely bogus, > and we can't guarantee that the va->pa conversion back is correct. > > Storing the phys address in object->pointer only solves the conversion > but it doesn't solve the rbtree problem (VA and PA values may overlap, > we can't just store the physical address either). And we use the rbtree > for searching objects on freeing as well. > > Given that all the kmemleak_alloc_phys() calls always pass min_count=0 > (we should probably get rid of the extra arguments), we don't expect > them to leak, so there's no point in adding them to the rbtree. We can > instead add a new object_phys_tree_root to store these objects by the > physical address for when we need to search (kmemleak_free_part_phys()). > This would probably look simpler than recording the callbacks and > replaying them. > > Wherever we use object_tree_root we should check for OBJECT_PHYS and use > object_phys_tree_root instead. There aren't many places. Considering the usage of objects with OBJECT_PHYS, storing the phys address and giving their own rbtree should solve the phys problem. I will post a v2 ASAP. Thanks, Patrick