From patchwork Thu Feb 15 00:07:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karthikeyan Ramasubramanian X-Patchwork-Id: 201207 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp67875dyb; Wed, 14 Feb 2024 16:08:19 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWqAGeags9QAd6nNMbvNBbePYDlo7KfleCREf2pECrR2FZRpK+OLfCsDElPZYHr79ETf0KLF5F5CL8kM767pBMNMCsmbQ== X-Google-Smtp-Source: AGHT+IEP9X/SmmcLlyRXPk3YJ/G1tKsGNDEm3NqxxEYFWILCQYkrtO1idoudDlDjnpxAYMD6q4bt X-Received: by 2002:a1f:6682:0:b0:4bf:d147:63c1 with SMTP id a124-20020a1f6682000000b004bfd14763c1mr49757vkc.6.1707955699599; Wed, 14 Feb 2024 16:08:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955699; cv=pass; d=google.com; s=arc-20160816; b=CfvqxBOxSNdWTQAFyqCaBt2Hd/KXlJ8Bq8/BMUSCiZViIpv0o2wRwUi2rbnwVMbtIB Iv5A1npsOJK6QLVFPXANaCcZgVHHxfIfdVZJ1aJJN7oQk2J7BsxYuEMHXUO7EDYNYRjD UHD2i9qY+AwN18RZ6/95/8SLBHsfn2URVlMW4uELbfCbRbL6wSTQ4bUHSwdJGP7qzZ+x SPyw4T5edIRjX3oL8XtiITdQDD02L5aS0jdMKi8r3vGPLpdRJWhtwyxWDrE08HUUIykW 3JdMQ1D06qD3owK8nWjTHZkqkKXgHVLnVF572qrUR2Gkwlv13XE+bXmUwXpwZ8AoO6vd 6dHw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=xk1V2GeLZIB0eK1Atb00UHsPHWh3K6myT0Efv9rtx7M=; fh=hs55QPYfHWFkn2cPPDyizh/jG9Y84hFeEmoZ++cRCUA=; b=pTIBLJss9dDGfC4iK9Gm8D+qqn/TlfpvJ5Hv8O9Fc0il1jP2Py+XcRPSdpQ7ax24Ne YwbeO+oF43TKe+fsgijEEYTJMD11BEoQqDVe1LRzEj8NPRsOpcW0qcyetr1X/sX26hUh 1JEj9dOYMVLAVa0+MVcYl2x65KWESBHSebRWSJMY7ODs93L3mky3J8nMvJkT6OE88S5l 3+HOjRV2qAdlQEMxXHVXZm/5qh+AS32hFynkxQy0VdX5IvNUczY3D50zyhbXwA3v4LdQ FHdSQFGxRh7sbxavNf3l1AbM9K5J5OzluiIsRTviiCDkm/6hxWMpzMnctAfvQ/ckFPYD /aQA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="iI5dPF/H"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-66175-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66175-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id bq8-20020a056122230800b004c02cd5c94esi46827vkb.226.2024.02.14.16.08.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:08:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66175-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="iI5dPF/H"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-66175-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66175-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 36E9E1C221C5 for ; Thu, 15 Feb 2024 00:08:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A11F1378; Thu, 15 Feb 2024 00:08:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="iI5dPF/H" Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E2CCC391 for ; Thu, 15 Feb 2024 00:08:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707955687; cv=none; b=L2fgyrN8gdV0Yk1yUoRm0lNOHefGAc03Pgyokh1XON18HboCV59kRM4MbtipE7HkZq/9MMUrbJYXcupYSneu3d7LkuYtsP4FCTh/L/pNfDTl+lJjzrNjlZfKBT4E3H1byro1anVuqGBMyCZJSfHasphZF1YUxe0vn2m3TMGEGaM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707955687; c=relaxed/simple; bh=MXm7+NJ1npnE1n0F4jgnhA9YQZg7N1sx7Rye2amVgf0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=k7G2TTmBBm9w8sYYydT8wOVmB8gwj9uIT5I0wBwcwmdqsQXpY/NlAdm+jQj3Jn9m75ygylPgm4xMGQsUmKiq/u3G1v66j/SBM3rhTpeanTpPYEKXpSqqvFcyz2ZU4HJIDtNJdJUIlfPeRbJoQxQC5apAnWQrTx0uFETrY0GK5j0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=iI5dPF/H; arc=none smtp.client-ip=209.85.167.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3c0471954faso247679b6e.2 for ; Wed, 14 Feb 2024 16:08:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707955684; x=1708560484; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=xk1V2GeLZIB0eK1Atb00UHsPHWh3K6myT0Efv9rtx7M=; b=iI5dPF/HSjGS+h0IkgezGQjrujzUJ+FIXkwRZY+4trK7l2UqAWEnkNdgjSnKQivrah AYA/1BIiysMZhUzX4uLmc2WL9VOSJiA/1qpFFFZ40YStdnYq7gb3sYfw1VlQJrq6E/Vo Bpmwb9TiW6gYDCgSZ/wwowYHEAbooHr1ArGVA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707955684; x=1708560484; 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=xk1V2GeLZIB0eK1Atb00UHsPHWh3K6myT0Efv9rtx7M=; b=E38ci8QY7BH722OFdhEuDa50rUcuPfV9RC/Y6WZQY4wvg3rAc0GZSxfZ6U8vUn87ne 6YA2g/8j7xfAbaftmPGiyP3E6AcTR0IUvftOAUNoVL3SL2XuYfwIBUq0POwhMg60chCQ Jpy70gLtT0KoQQF+3nebzOMWbWBct7UToLM0VSApIiPPpNom72W2vpB0w5/AZ43lihAk sUdZVzM3vhQeNH6gmPkuJgg/TLP+mUcil+fs9/N9f+11XG6xoaYJVT3HCMyhXJhEvxro QipzyCv86py9praG0OgHvn4F/EETKsrIWo1ZMyF8emoRcqxDCAgUSaWPnhmNrfi5fj3A F+QQ== X-Gm-Message-State: AOJu0Yz+dgZqmm3aLajpw1Buwu8IPLkfITvlTgFs3QrZ6JUzZ4TAcy4C TUmHZ6G50+5wNEOe/gZPuV9NCKdnoo0IM+xtYrAeBT0FTSIbjOSxuod01S5EHvnvs90CAD7x78P Vyw== X-Received: by 2002:a05:6808:2182:b0:3c1:3661:d37 with SMTP id be2-20020a056808218200b003c136610d37mr399215oib.38.1707955684095; Wed, 14 Feb 2024 16:08:04 -0800 (PST) Received: from kramasub2.cros.corp.google.com ([100.107.108.189]) by smtp.gmail.com with ESMTPSA id r6-20020ac85206000000b0042c71ca69b4sm54981qtn.11.2024.02.14.16.08.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:08:03 -0800 (PST) From: Karthikeyan Ramasubramanian To: LKML , Takashi Iwai Cc: Karthikeyan Ramasubramanian , Sven van Ashbrook , Brian Geffon , stable@vger.kernel.org, Curtis Malainey , Jaroslav Kysela , Takashi Iwai , linux-sound@vger.kernel.org Subject: [PATCH v1] ALSA: memalloc: Fix indefinite hang in non-iommu case Date: Wed, 14 Feb 2024 17:07:25 -0700 Message-ID: <20240214170720.v1.1.Ic3de2566a7fd3de8501b2f18afa9f94eadb2df0a@changeid> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790921355594101831 X-GMAIL-MSGID: 1790921355594101831 Before 9d8e536 ("ALSA: memalloc: Try dma_alloc_noncontiguous() at first") the alsa non-contiguous allocator always called the alsa fallback allocator in the non-iommu case. This allocated non-contig memory consisting of progressively smaller contiguous chunks. Allocation was fast due to the OR-ing in of __GFP_NORETRY. After 9d8e536 ("ALSA: memalloc: Try dma_alloc_noncontiguous() at first") the code tries the dma non-contig allocator first, then falls back to the alsa fallback allocator. In the non-iommu case, the former supports only a single contiguous chunk. We have observed experimentally that under heavy memory fragmentation, allocating a large-ish contiguous chunk with __GFP_RETRY_MAYFAIL triggers an indefinite hang in the dma non-contig allocator. This has high-impact, as an occurrence will trigger a device reboot, resulting in loss of user state. Fix the non-iommu path by letting dma_alloc_noncontiguous() fail quickly so it does not get stuck looking for that elusive large contiguous chunk, in which case we will fall back to the alsa fallback allocator. Note that the iommu dma non-contiguous allocator is not affected. While assembling an array of pages, it tries consecutively smaller contiguous allocations, and lets higher-order chunk allocations fail quickly. Suggested-by: Sven van Ashbrook Suggested-by: Brian Geffon Fixes: 9d8e536d36e7 ("ALSA: memalloc: Try dma_alloc_noncontiguous() at first") Cc: stable@vger.kernel.org Cc: Sven van Ashbrook Cc: Brian Geffon Cc: Curtis Malainey Signed-off-by: Karthikeyan Ramasubramanian --- sound/core/memalloc.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sound/core/memalloc.c b/sound/core/memalloc.c index f901504b5afc1..5f6526a0d731c 100644 --- a/sound/core/memalloc.c +++ b/sound/core/memalloc.c @@ -540,13 +540,18 @@ static void *snd_dma_noncontig_alloc(struct snd_dma_buffer *dmab, size_t size) { struct sg_table *sgt; void *p; + gfp_t gfp_flags = DEFAULT_GFP; #ifdef CONFIG_SND_DMA_SGBUF if (cpu_feature_enabled(X86_FEATURE_XENPV)) return snd_dma_sg_fallback_alloc(dmab, size); + + /* Non-IOMMU case: prevent allocator from searching forever */ + if (!get_dma_ops(dmab->dev.dev)) + gfp_flags |= __GFP_NORETRY; #endif sgt = dma_alloc_noncontiguous(dmab->dev.dev, size, dmab->dev.dir, - DEFAULT_GFP, 0); + gfp_flags, 0); #ifdef CONFIG_SND_DMA_SGBUF if (!sgt && !get_dma_ops(dmab->dev.dev)) return snd_dma_sg_fallback_alloc(dmab, size);