Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp188042rwl; Thu, 6 Apr 2023 17:14:10 -0700 (PDT) X-Google-Smtp-Source: AKy350ZyRw3nlwPj1RhiWa8qWTN3yfS06/1R4jPeEAoM94/sLcVsCiv9nIwSkFcG9CMJZhtHdRd3 X-Received: by 2002:aa7:ca41:0:b0:502:67b6:9734 with SMTP id j1-20020aa7ca41000000b0050267b69734mr160806edt.6.1680826450685; Thu, 06 Apr 2023 17:14:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680826450; cv=none; d=google.com; s=arc-20160816; b=nGUQVL/yaFcCrvvnJy8Ytit3/+tJGuoDa5la+e3d9vxX4PiaLvUqFCsQ6LDYKrWKTt 2tLFqbZXq2U9sZT6rNzJX27nFTf9nV4/qsNlu8ALa5TvI006+sxbTyEsNBbcH09ebfVd lROFom2rZlEbrqMK2RST74HmfGMfW2L45s9E8cORrUIsb9uNdE7NHQQkoAXGIY607szR XWPJI0IMK8tEWbTObmMuxxwnASWSunezBXuxmoB5zKn0IRwBGri6QRxO8Q/muzWTGr8J Wz0CSvVNFubmCAyoDxU6uj6XOhBv/JPi0DbrAVKP5Ypc0+Kuu5FbG4pJveZo5zA5FjPn 7v/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=EUe0Zeu+77um4muZNyM98wqgNCUSH9t8M+sk1P4Rs2c=; b=ooRUOZUUZr/DpNQYrUh3KLGPm72ryrXiqCxHRXhvVE9RE7exi0tbHc1DkHDlfui6zF q23E0wxnZK8CS1AajCnyLZRQCxIrxM+tsLv3fiIwzeSVFMuJdSLCct5qmNPgCz3sPR68 ohgZj4AtYwoXM825D7znIRXZbrmgpjaCUOVeGSB5Jf3tRlCUUov75cAg2QYitlEoczHi WoFZo6FFvEj+FFnuFuJnnGk9vPsshz0qzLo71zKLGJ5agVtZCRd6xJiK6af29D/PjBvq MLxzX2FlVr9rlML3KjCw9prxBRZ5azSsEzRoeA9A/RJmEoDa4j/OyMWHp+me0J+d/ugm Er8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=GAEu0hx2; 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 r9-20020aa7cfc9000000b004acdef7bab2si1973454edy.220.2023.04.06.17.13.44; Thu, 06 Apr 2023 17:14:10 -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=GAEu0hx2; 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 S236739AbjDGAAv (ORCPT + 99 others); Thu, 6 Apr 2023 20:00:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230335AbjDGAAu (ORCPT ); Thu, 6 Apr 2023 20:00:50 -0400 Received: from mail-yw1-x112c.google.com (mail-yw1-x112c.google.com [IPv6:2607:f8b0:4864:20::112c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC48483FF for ; Thu, 6 Apr 2023 17:00:46 -0700 (PDT) Received: by mail-yw1-x112c.google.com with SMTP id 00721157ae682-54c12009c30so56428637b3.9 for ; Thu, 06 Apr 2023 17:00:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680825646; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=EUe0Zeu+77um4muZNyM98wqgNCUSH9t8M+sk1P4Rs2c=; b=GAEu0hx2ollv+g0TFazo9MnuzGvE0WDNcW64TTrX0NKQ9CqQU1OY6BjBHOgZgeeLCL CGniCLuxUFAfE9r0tZhfZs5/oFl6eiA/uOSitNgdka5p7+CVUwEOwdZd8MWbi0qxj9SJ 8iwTgtLb3G1q2pQgNWyBjxWuOui/eKrOTJkOMuFUYtAQ8lPoM8hK/+fpd5zKfwNRPYsq JtVcrH8ZSI9sZ+a6X9dXiXZXZC66l6g0lJEmUOzY9CBGZV1NWKkPGee3AAo8MEKzhTWP DNsYUwHbhlG5x87B60Wco+S0q0utb+UVgVdGIwUTHjt34Hbohp7KBaXTH+/v3CtC2LLy 6MYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680825646; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EUe0Zeu+77um4muZNyM98wqgNCUSH9t8M+sk1P4Rs2c=; b=inaawvjLovxav1hCOk49m1lRuARhXq9zxoDL/rWbrNHqybtYBe1g6WIsorGv2FrKsH DcCf/dMQFJkNMhc06r5gTDZj6iVsjnM8zdtxloOHb4E/5LYBEARYoXzyRSXMWru/gdmv ibVWshHuTWzvSRV6OiijFiahOvSsEYso760vGtAv4gFMkwcu2qYhAs29pJswjabcvIes cOVX7NvQ6/IjeqkzhoWfH9gv2UN/f+K5JYuquk4plNpxliflajtvJ6haBLUi3PYomZMX wWDZUKMCn/XpqNbD9yT3OfAyN6wGyqoYrjsrf228lVl55QKSOjk9gWNqvy+VkFoeOLkp cVDg== X-Gm-Message-State: AAQBX9c+8wYvcEZU65CQ6/rN+Sr0fRiRYYcY9vDjT1+QTt/gANKXVfxr N7pcBFRjQtW9vneMDkH4jQS2Zo3sWnWDyNG7uunKBUFJMDFwqBYfMq51hw== X-Received: by 2002:a81:ac64:0:b0:541:7237:6e6b with SMTP id z36-20020a81ac64000000b0054172376e6bmr84906ywj.0.1680825645755; Thu, 06 Apr 2023 17:00:45 -0700 (PDT) MIME-Version: 1.0 References: <20230405185650.239f9721f066aa480e83d543@linux-foundation.org> <20230405172524.e25b62e1c548a95564b1d324@linux-foundation.org> <20230406000854.25764-1-jaewon31.kim@samsung.com> <20230406014419epcms1p3f285b6e3fdbb1457db1bcbaab9e863be@epcms1p3> <20230406021712epcms1p216f274040d25d18380668ffbfa809c48@epcms1p2> <20230405200923.9b0dca2165ef3335a0f6b112@linux-foundation.org> <20230406163822.faae6a90b3aa4942df6e7442@linux-foundation.org> In-Reply-To: <20230406163822.faae6a90b3aa4942df6e7442@linux-foundation.org> From: "T.J. Mercier" Date: Thu, 6 Apr 2023 17:00:34 -0700 Message-ID: Subject: Re: [PATCH v2] dma-buf/heaps: system_heap: Avoid DoS by limiting single allocations to half of all memory To: Andrew Morton Cc: John Stultz , jaewon31.kim@samsung.com, "sumit.semwal@linaro.org" , "daniel.vetter@ffwll.ch" , "hannes@cmpxchg.org" , "mhocko@kernel.org" , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" , "jaewon31.kim@gmail.com" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-15.7 required=5.0 tests=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,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=unavailable 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, Apr 6, 2023 at 4:38=E2=80=AFPM Andrew Morton wrote: > > On Thu, 6 Apr 2023 16:27:28 -0700 "T.J. Mercier" w= rote: > > > > When you say "decide what's the largest reasonable size", I think it > > > is difficult as with the variety of RAM sizes and buffer sizes I don'= t > > > think there's a fixed limit. Systems with more ram will use larger > > > buffers for image/video capture buffers. And yes, you're right that > > > ram/2-1 in a single allocation is just as broken, but I'm not sure ho= w > > > to establish a better guard rail. > > > > > > thanks > > > -john > > > > I like ENOMEM with the len / PAGE_SIZE > totalram_pages() check and > > WARN_ON. We know for sure that's an invalid request, and it's pretty > > cheap to check as opposed to trying a bunch of reclaim before failing. > > Well, if some buggy caller has gone and requested eleventy bigabytes of :) > memory, doing a lot of reclaiming before failing isn't really a problem > - we don't want to optimize for this case! > The issue I see is that it could delay other non-buggy callers, or cause reclaim that wouldn't have happened if we just outright rejected a known-bad allocation request from the beginning. > > For buffers smaller than that I agree with John in that I'm not sure > > there's a definitive threshold. > > Well... why do we want to do _anything_ here? Why cater for buggy > callers? I think it's because "dma-buf behaves really badly with very > large allocation requests". Again, can we fix that instead? > There are a variety of different allocation strategies used by different exporters so I don't think there's one dma-buf thing we could fix for slow, large allocations in general. For the system_heap in this patch it's really just alloc_pages. I'm saying I don't think the kernel should ever ask alloc_pages for more memory than exists on the system, which seems like a pretty reasonable sanity check to me. Given that, I don't think we should do anything for buffers smaller than totalram_pages() (except maybe to prevent OOM panics via __GFP_RETRY_MAYFAIL when we attempt to exhaust system memory on any request - valid or otherwise).