From patchwork Mon Feb 5 17:26:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 13545908 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 52B59C4828D for ; Mon, 5 Feb 2024 17:27:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=khICFYcZfsg/eCtYilHXmAIw+GQmYcGipUDri0AbP1Y=; b=Kwv49WoEcyicF+ MBJoDemSC+pBaUl/psWg1+rM5XvgUvUbfdWDxHVxZQdE+FyaRQg6kngNkUjbgTFr3WomA5HM0uyv3 kmZRAgo012vxArWSGmaEgpMAYNm58xvO6oxe9shw6Lxw7XW+PUBPt22nta3cliBz6VhwKIKZS2Kv9 BCl38FyTeUjenx+9qMFHtzABsCzkM5bWfa19KDrO6GRV9qVS2wBG/AgDFpgIKgueUy3x0uLJAudJB vdsOYZOlK5QOizl15/UHLFloSv6907bpXn2NbSoh1/1j3BUAj5vX1ELF/I/Ti54TRDMUVmjoos5eH 7P6eqwi541U+J43UA2/g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rX2ko-000000049B8-0cfI; Mon, 05 Feb 2024 17:27:34 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rX2kk-0000000498H-1kil for linux-arm-kernel@lists.infradead.org; Mon, 05 Feb 2024 17:27:32 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1d91397bd22so35670785ad.0 for ; Mon, 05 Feb 2024 09:27:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707154048; x=1707758848; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=4U+N71xHm4gg+B0aHt1NbcRFqwuGDCVmnH0NzcNhfq4=; b=mxihqO6EBzoEP8ZllaW3ZkLtIM+TPbfaY3tzPJ4RKzDtNmzVLcBGjWKeeNezu5l2pj trGpCoblZFnWrdBWGSOPYNQHnqJIFzrk5pEb1uaOe4zCZzFeq/vsI3u06gdDKqqtua16 Bjt0xJDZ8FjbGzFKrnCplIH3/gLT034iYD3co= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707154048; x=1707758848; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4U+N71xHm4gg+B0aHt1NbcRFqwuGDCVmnH0NzcNhfq4=; b=vv7n+vJsmRrdV11xxaWsoCXCleNve0y+kHjZtotdXHgPGNMqlhL5si+ZepZtcjhL/P DZYDuJXGFOmSApHc4OS/e70hdX6HRcfh1rqf4mV+hOLLLZ/Om7JA7iEqnK7o0UG5XvJa IHGONSQxUUwJ+TkogVHQUcRO8Twt6ZIGhESqaiK8JPoCG7gfnzYF0MPPBrUwv4HAcoIB uE0GVi+nLreuPV3yPksomRNfnvUnAJFCnmn7InOjpu1Co/vVwwHSVho3uGJAE6HRPy2d mCK+11uuGxPwaMoTFAKPhxyUKDcbaND+ela5k8QZtN20Y4MdM7ym3w+HpIlb6nqHIJ9b Dybg== X-Gm-Message-State: AOJu0Yz+Q/0/HnLWzjVyvusAonPo8TNFd/Wip5LJEAR7ZUrLY6Rzezkp Mx9IRF13l0ZAjB88Um9YoZg2NK1OoEC+FE53svZdbRa4pT9px4rHqYOly0x7t58Wqdi0Y2Flguw fJQ== X-Google-Smtp-Source: AGHT+IFUJYXHGIr2LCGI8FKWXE3xOLGI2mYhp7DYexE/TWjuF+OeqNnn4MkzN96+o/X73V7ZYaQWDA== X-Received: by 2002:a17:903:181:b0:1d9:3938:40bb with SMTP id z1-20020a170903018100b001d9393840bbmr164878plg.20.1707154048553; Mon, 05 Feb 2024 09:27:28 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVkRLaBMi1cNLgwcJ4gr0SdqkEdQyijwctiuf5LdVVfpVmK0cY3vbrQqxELm4VsUf5Iviie567CjnCp6QpZv2cJZ9qTb8bfFOzwsPSAlJh48jISJ5aEd1JYomsvY9PR9cZx2/SH/22aLo2LrtRfXvQSb2Bh1iuSAKT6xYetCUbxpQ4DFFVfEk7UJMJ1DqEHXhF2UQ+ReydK0bsF1WRihOmbhvch2i16kfzSuBnbaT8FTOs/yaujTj8mQ7QfpkBkp6Pbv7f/gEovrwRk1fsN+HmauyY2TRN4DA0XDpWrv23N7KxjeaOVBouvbOiux/BPvBL/ENUNLXoA3iqUrfDtfX7ZH924Fou+JlW18gHs1XX4HkDX8Bw5WvPK3NZTGnMWnpblMUi56HW33N7QE5JjPqf30PAXfjhzwnnGvCPTjkg0V7+BrDv9nwbIgg0TgeH2EJZ6lj47Nwmd/T1HsnXwnzPsUI46BtJckryogPjZAVY9WQM= Received: from dianders.sjc.corp.google.com ([2620:15c:9d:2:95f9:acd:a17b:b759]) by smtp.gmail.com with ESMTPSA id v11-20020a170902d08b00b001d942f744f6sm115259plv.157.2024.02.05.09.27.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 09:27:28 -0800 (PST) From: Douglas Anderson To: Alexander Viro , Christian Brauner Cc: Mark Brown , Catalin Marinas , Will Deacon , Dave Martin , Oleg Nesterov , linux-arm-kernel@lists.infradead.org, Matthew Wilcox , Douglas Anderson , Eric Biederman , Jan Kara , Kees Cook , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2] regset: use kvzalloc() for regset_get_alloc() Date: Mon, 5 Feb 2024 09:26:30 -0800 Message-ID: <20240205092626.v2.1.Id9ad163b60d21c9e56c2d686b0cc9083a8ba7924@changeid> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240205_092730_589385_74496C1B X-CRM114-Status: GOOD ( 21.89 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org While browsing through ChromeOS crash reports, I found one with an allocation failure that looked like this: chrome: page allocation failure: order:7, mode:0x40dc0(GFP_KERNEL|__GFP_COMP|__GFP_ZERO), nodemask=(null),cpuset=urgent,mems_allowed=0 CPU: 7 PID: 3295 Comm: chrome Not tainted 5.15.133-20574-g8044615ac35c #1 (HASH:1162 1) Hardware name: Google Lazor (rev3 - 8) with KB Backlight (DT) Call trace: ... warn_alloc+0x104/0x174 __alloc_pages+0x5f0/0x6e4 kmalloc_order+0x44/0x98 kmalloc_order_trace+0x34/0x124 __kmalloc+0x228/0x36c __regset_get+0x68/0xcc regset_get_alloc+0x1c/0x28 elf_core_dump+0x3d8/0xd8c do_coredump+0xeb8/0x1378 get_signal+0x14c/0x804 ... An order 7 allocation is (1 << 7) contiguous pages, or 512K. It's not a surprise that this allocation failed on a system that's been running for a while. More digging showed that it was fairly easy to see the order 7 allocation by just sending a SIGQUIT to chrome (or other processes) to generate a core dump. The actual amount being allocated was 279,584 bytes and it was for "core_note_type" NT_ARM_SVE. There was quite a bit of discussion [1] on the mailing lists in response to my v1 patch attempting to switch to vmalloc. The overall conclusion was that we could likely reduce the 279,584 byte allocation by quite a bit and Mark Brown has sent a patch to that effect [2]. However even with the 279,584 byte allocation gone there are still 65,552 byte allocations. These are just barely more than the 65,536 bytes and thus would require an order 5 allocation. An order 5 allocation is still something to avoid unless necessary and nothing needs the memory here to be contiguous. Change the allocation to kvzalloc() which should still be efficient for small allocations but doesn't force the memory subsystem to work hard (and maybe fail) at getting a large contiguous chunk. [1] https://lore.kernel.org/r/20240201171159.1.Id9ad163b60d21c9e56c2d686b0cc9083a8ba7924@changeid [2] https://lore.kernel.org/r/20240203-arm64-sve-ptrace-regset-size-v1-1-2c3ba1386b9e@kernel.org Signed-off-by: Douglas Anderson Reviewed-by: Catalin Marinas --- Changes in v2: - Use kvzalloc() instead of vmalloc(). - Update description based on v1 discussion. fs/binfmt_elf.c | 2 +- kernel/regset.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 5397b552fbeb..ac178ad38823 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -1928,7 +1928,7 @@ static void free_note_info(struct elf_note_info *info) threads = t->next; WARN_ON(t->notes[0].data && t->notes[0].data != &t->prstatus); for (i = 1; i < info->thread_notes; ++i) - kfree(t->notes[i].data); + kvfree(t->notes[i].data); kfree(t); } kfree(info->psinfo.data); diff --git a/kernel/regset.c b/kernel/regset.c index 586823786f39..b2871fa68b2a 100644 --- a/kernel/regset.c +++ b/kernel/regset.c @@ -16,14 +16,14 @@ static int __regset_get(struct task_struct *target, if (size > regset->n * regset->size) size = regset->n * regset->size; if (!p) { - to_free = p = kzalloc(size, GFP_KERNEL); + to_free = p = kvzalloc(size, GFP_KERNEL); if (!p) return -ENOMEM; } res = regset->regset_get(target, regset, (struct membuf){.p = p, .left = size}); if (res < 0) { - kfree(to_free); + kvfree(to_free); return res; } *data = p; @@ -71,6 +71,6 @@ int copy_regset_to_user(struct task_struct *target, ret = regset_get_alloc(target, regset, size, &buf); if (ret > 0) ret = copy_to_user(data, buf, ret) ? -EFAULT : 0; - kfree(buf); + kvfree(buf); return ret; }