Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1473706imm; Thu, 19 Jul 2018 02:13:31 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcFEcBN5pGh2aOAXIM+7wDnFnoxZmsFD4rloO6IT9MUoldyz6iw73IQU23DwwowTWk1CZyS X-Received: by 2002:a65:5c02:: with SMTP id u2-v6mr9150896pgr.304.1531991610939; Thu, 19 Jul 2018 02:13:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531991610; cv=none; d=google.com; s=arc-20160816; b=P9PlPZKdZYYffirfagFwI9XiHNWd+wYRb0oBJ6t1BXk38Wgbol8EVB9hxJcaiKjNaX yLPG01s+GWClWK1+UvwWmLkXQJOcBVNzSTpSXvaZicDET7u7VSlZs8yazovlmRtCc3fd Jy4tbwzgCxEYOKHwR0EgD5Et+6RmS8R8j0YdBdV5R56mpPA7In9OJtf+KFUF1sVmWIWZ Lxb7DpEZZHRn7BkmfoRTvc8G52oqdjD8gRuB4mumL5XgSTXR+h+1VmIcb0ZxJps0FoSy mT0ZRC7Pekzh8dyzwX5FmZPZFFL9NxL5pQYApFSkg/iRF2QzAAEnqTf9dCtDqudMWEd3 hzYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:subject:cc:to:from:message-id:date :arc-authentication-results; bh=0KHmqgJJYNXVNmP/P7Wl8lmnqwroPquNtFTevbTJCoI=; b=0QqbbmF9P/z4wLoZpVVXJPEqH9BoeOzMvnayuTnRrwWkvN2wW9DFInNPPkx8UHHRQ4 +rp9q83WQgrSvuYC0lwJDUmQWfsSBPBkPbhyJ/h9sYTe9PFag0YnoyZ0wKxrHgeUvGrp 3X2zRi0wH9jzTK1gPPOf0eXJ/Up/QLICx2S/L4KZUNLAJVPZ9HTrOuOZTxSwH4jHiIEq LB6388FcaZIgKmPrVC2sm1ZoEeUG33g0sL0849QkWUsS9cmHhMQWvXE9+Bib1DPHfSBW gmHXr+LNRG52XQFZzfGmBw87/HHdi58fuBbA3mFo9kRdGUYcfu90UW3gAQzuNStAOOyB xUfg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i10-v6si6100380pfe.119.2018.07.19.02.12.53; Thu, 19 Jul 2018 02:13:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730880AbeGSJx3 (ORCPT + 99 others); Thu, 19 Jul 2018 05:53:29 -0400 Received: from mx2.suse.de ([195.135.220.15]:38474 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726975AbeGSJx2 (ORCPT ); Thu, 19 Jul 2018 05:53:28 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 910ECADB3; Thu, 19 Jul 2018 09:11:14 +0000 (UTC) Date: Thu, 19 Jul 2018 11:11:14 +0200 Message-ID: From: Takashi Iwai To: "Zhang, Jun" Cc: "He, Bo" , "alsa-devel@alsa-project.org" , "perex@perex.cz" , "linux-kernel@vger.kernel.org" , "Zhang, Yanmin" Subject: Re: [PATCH] ALSA: core: fix unsigned int pages overflow when comapred In-Reply-To: References: <88DC34334CA3444C85D647DBFA962C272B0C4C34@SHSMSX104.ccr.corp.intel.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/26 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 19 Jul 2018 08:42:14 +0200, Takashi Iwai wrote: > > On Thu, 19 Jul 2018 08:08:06 +0200, > Zhang, Jun wrote: > > > > Hello, Takashi > > > > I think use our patch, it's NOT possible that the returned size is over sgbuf->tblsize. > > > > In function snd_malloc_sgbuf_pages, > > > > Pages is align page, > > sgbuf->tblsize is align 32*page, > > chunk is align 2^n*page, > > > > in our panic case, pages = 123, tlbsize = 128, > > 1st loop trunk = 32 > > 2nd loop trunk = 32 > > 3rd loop trunk = 32 > > 4th loop trunk = 16 > > 5th loop trunk = 16 > > So in 5th loop pages-trunk = -5, which make dead loop. > > Looking at the code again, yeah, you are right, that won't happen. > > And now it becomes clear: the fundamental problem is that > snd_dma_alloc_pages_fallback() returns a larger size than requested. > It would be acceptable if the internal allocator aligns a larger size, > but it shouldn't appear in the returned size outside. I believe this > was just a misunderstanding of get_order() usage there. > (BTW, it's interesting that the allocation with a larger block worked > while allocation with a smaller chunk failed; it must be a rare case > and that's one of reasons this bug didn't hit frequently.) > > That being said, what we should fix is rather the function > snd_dma_alloc_pages_fallback() to behave as expected, and it'll be > like the patch below. And we can reduce even more lines. A proper patch is below. thanks, Takashi -- 8< -- From: Takashi Iwai Subject: [PATCH] ALSA: memalloc: Don't exceed over the requested size snd_dma_alloc_pages_fallback() tries to allocate pages again when the allocation fails with reduced size. But the first try actually *increases* the size to power-of-two, which may give back a larger chunk than the requested size. This confuses the callers, e.g. sgbuf assumes that the size is equal or less, and it may result in a bad loop due to the underflow and eventually lead to Oops. The code of this function seems incorrectly assuming the usage of get_order(). We need to decrease at first, then align to power-of-two. Reported-by: he, bo Reported-by: zhang jun Cc: Signed-off-by: Takashi Iwai --- sound/core/memalloc.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/sound/core/memalloc.c b/sound/core/memalloc.c index 7f89d3c79a4b..753d5fc4b284 100644 --- a/sound/core/memalloc.c +++ b/sound/core/memalloc.c @@ -242,16 +242,12 @@ int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size, int err; while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) { - size_t aligned_size; if (err != -ENOMEM) return err; if (size <= PAGE_SIZE) return -ENOMEM; - aligned_size = PAGE_SIZE << get_order(size); - if (size != aligned_size) - size = aligned_size; - else - size >>= 1; + size >>= 1; + size = PAGE_SIZE << get_order(size); } if (! dmab->area) return -ENOMEM; -- 2.18.0