Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp552828imw; Wed, 13 Jul 2022 03:46:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u/3vvgBXGQGj62MB+WhDlYdsgQqGs3vGXV12njY4d/PuqUFU/GShn3QdPG+o6oj5LbBkH1 X-Received: by 2002:a05:6a00:1412:b0:528:47a6:8569 with SMTP id l18-20020a056a00141200b0052847a68569mr2684304pfu.39.1657709201240; Wed, 13 Jul 2022 03:46:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657709201; cv=none; d=google.com; s=arc-20160816; b=kJ7Wpedn4K6F4Et0wycq0o/2IJ/vLiYn9jbUXnypVAzYwlowE2LVtGYn83msSVHUbI +PZZfoVEZRksuR6QwUcD2LBoqR1SKG6wzpowqKQ2NCzZ7jsUOP4V92IPF7jkVe5nXKuU KUQzBVgy2WBDzoFWpxwUC+m7RW6v20ky8ohRCkOmnDpS17ptrG29AYyUyStSIiPAhLsC Qpcfsgrs8kms6nS7fDfTiADOxJglmQsBW43l3FFwkPRs5PXhscDEvMBrCrlnwuHJOORj G1GkZ0ogc/ixT6XGroenVI4gxlv1HI9mYqVHdY3RFNRY9+wWtQIbOFk4JTMrEe23wTbv n0eg== 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=PD3ohJ/EmhNYKgVuLh+1SqFmgLy6OdvncR7q74oGJgI=; b=inTXI8qTVVU86w6DGMbOZDXLOOZHFjimM47dIT2sQ3hPemQ5sKiQJVqQohjETtHd2K xx9QOpNoPdSWPcObIVQs2cAbo6UKmIZpqjSwfVZMhPC3fvYSJCaK29tkZkyRxZYkVQTR B/rKlgI8oiBPVp/UH3dv/bJK9c031N693QZnpBHMAVU+wStxEHxyQXAPockf+VvLRxu7 /CEl+gKD9rEO92Er8YRFttCfz4IAKlNY+AfwLMQpxbNa97jKOZabRyKEkVZ10jpHRlWI JPLKeYaH1UKsHqSqZylYhcsuQnpxKZbl3ftkUiAEc2k4B7WVWcablN3PA54nATR38YYl PwCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=SFkRptQv; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t8-20020a6549c8000000b0040986ef2410si2025829pgs.493.2022.07.13.03.46.28; Wed, 13 Jul 2022 03:46:41 -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=@google.com header.s=20210112 header.b=SFkRptQv; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235991AbiGMKeO (ORCPT + 99 others); Wed, 13 Jul 2022 06:34:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235740AbiGMKeJ (ORCPT ); Wed, 13 Jul 2022 06:34:09 -0400 Received: from mail-yb1-xb33.google.com (mail-yb1-xb33.google.com [IPv6:2607:f8b0:4864:20::b33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4A0DE1920 for ; Wed, 13 Jul 2022 03:34:08 -0700 (PDT) Received: by mail-yb1-xb33.google.com with SMTP id n74so18532971yba.3 for ; Wed, 13 Jul 2022 03:34:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=PD3ohJ/EmhNYKgVuLh+1SqFmgLy6OdvncR7q74oGJgI=; b=SFkRptQv/fARrJEXyjgdl3GI/SNI8s22WPYtvUrVhQI7uqGJl1eG7kDJG9WCzpi/oS vbOs8s7fLxxgZipbGnE+yg2GJbqhEOAof8pKtlWJO3Nv40SHvj+Ok+9BDj4FJRxH7+p1 FNdVIuTsM+3E0KR+CwyzKwWY3lbJAsKTDJP9YJ17aKY0UajdUFkZGa5EkGCYtUUfm+jj UjFKgWFRFTer4Hkq1JP2TkX4prE8+mpUkTbipb78wRzGnvCjsX995BzgRk/BswHHsiS2 X5bDvWUo/+QLB8d2JenM7ZAiK6VfHNsNnB01WX5kJluNt0tuqVJCjSFBQlEplHVJk62w jBEg== 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=PD3ohJ/EmhNYKgVuLh+1SqFmgLy6OdvncR7q74oGJgI=; b=d9vsJxdcc2pgiYL0mzgjLO2VeOuJ3w7UvYMZT+AMmpjxyelVaCpoaYN4r+FqVBt/i/ I1lZTIpaNuFuw8nkn53+FZwCQiwm5XykBzRl4TpciiiZKZ9a0nq5qgetw14hx0+MlZ+T Zx6bdVH7aEan8PDhNt+5iObYRFC0P0FgiZyrlZdbFW/Tpb+mshik7LwrEpEnlBTtNoCm /gBXgKgqlxzsqbg4Aw6SDLit0lhtwHBbkU2ea2j5zrJCDYhjp54E6FScVf9r5hCePxPc PvIqzBGvk1nUg9yVUF6cIKhw58sAFUW+0lmIb2b483cxgoqTlOCwsBJ/TYKpOhPkWMmu b3Cg== X-Gm-Message-State: AJIora+pE6jqtZ0p0LVtZ30Tm2KfLD877wDVhozmo9NRj9ueXEOTNLB7 8tf4xpJCyKHKTd4PIXThTgs6cXCtR1JSNgp5z7NvRw== X-Received: by 2002:a25:abc5:0:b0:66e:3983:3ca7 with SMTP id v63-20020a25abc5000000b0066e39833ca7mr2939658ybi.168.1657708447835; Wed, 13 Jul 2022 03:34:07 -0700 (PDT) MIME-Version: 1.0 References: <20220712133946.307181-1-42.hyeyoo@gmail.com> <20220712133946.307181-17-42.hyeyoo@gmail.com> In-Reply-To: From: Marco Elver Date: Wed, 13 Jul 2022 12:33:31 +0200 Message-ID: Subject: Re: [PATCH 16/16] mm/sl[au]b: check if large object is valid in __ksize() To: Christoph Lameter Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>, Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Joe Perches , Vasily Averin , Matthew WilCox , linux-kernel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 Wed, 13 Jul 2022 at 12:07, Christoph Lameter wrote: > > On Wed, 13 Jul 2022, Hyeonggon Yoo wrote: > > > > Why return 0 if there is an error and why bother the callers with these > > > checks. BUG()? > > > > I thought BUG should be used when there is no other solution. > > Spurios returns of 0 that the caller has to check for is a solution? It's never really been about the caller checking for 0, see below. > > > I guess this is an error since the order-0 page cannot come from slab > > > allocations. > > > > comment in ksize() says: > > "The caller must guarantee that objp points to a valid object > > previously allocated with either kmalloc() or kmem_cache_alloc()." > > > > It should not be used on order-0 page that is not allocated from slab. No? > > I guess we would need to check. Code could exist that does this. > > Getting a 0 size would be surprising too here. BUG()? Or WARN() and return > PAGE_SIZE. We shouldn't crash, so it should be WARN(), but also returning PAGE_SIZE is bad. The intuition behind returning 0 is to try and make the buggy code cause less harm to the rest of the kernel. From [1]: > Similarly, if you are able to tell if the passed pointer is not a > valid object some other way, you can do something better - namely, > return 0. The intuition here is that the caller has a pointer to an > invalid object, and wants to use ksize() to determine its size, and > most likely access all those bytes. Arguably, at that point the kernel > is already in a degrading state. But we can try to not let things get > worse by having ksize() return 0, in the hopes that it will stop > corrupting more memory. It won't work in all cases, but should avoid > things like "s = ksize(obj); touch_all_bytes(obj, s)" where the size > bounds the memory accessed corrupting random memory. [1] https://lore.kernel.org/all/CANpmjNNYt9AG8RrGF0pq2dPbFc=vw2kaOnL2k5+8kfJeEMGuwg@mail.gmail.com/ Thanks, -- Marco