Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1364074imm; Wed, 18 Jul 2018 23:44:07 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeQgxUU4VCFIWw95lI1ln7ZrR7V7Y2sGWu72/Z7LCxgejXUjNHepmvHuvVhBoSiXSk58Riu X-Received: by 2002:a17:902:8a94:: with SMTP id p20-v6mr8772411plo.258.1531982647639; Wed, 18 Jul 2018 23:44:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531982647; cv=none; d=google.com; s=arc-20160816; b=tJ/6GM37Q6L/nadu62/gdb0gLN3w134N1TI18dQaRJsClABjYYalAa96Npa2GwNEZJ 5rU/YhZPhdFuYmfEIUj9gNGgYj9wDQJDqQ5Ipj7l0eP6QvDCvVtgF3na5zD/B06xD7uM 7iIL0Fw1UqTK6hbUhRGG3cij63I1dXzqTSLu7JwWHMQ7DgywQLN5toBBBmcJHAgh3Jqw TqZBDWFtGFSRbbENyDH5qBLDselAfclAYbdtjC09b665SwFFAPh4Gw83b248W5VgGG2h VkxfINcM8e/1E/G7Ga+DITAmL2M/D8UGSYNcoM8c9TEqPj9IJ1JSzz0XKMfeUxjYOnvN getg== 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=XbksJd4e8apBGJpMobHnVucBMzB/+qe9I+KB2I13pNU=; b=iYr/qYzheNlSCAOu/WIl1+KuilkwRgnQrplW33tYJZl5khvEi17ODOBU6fVze60EEz EFt3FWDCVFi2N+7Q6Q1Gf2+XBJ+PRLiz/tH7ZEuAWTAC2/84e/qUMfi03jsIA/lYgAlO eNUCS1PQbJOHT9tM0xoq5gES375rC5RY2mTFcSZiZ83xiH9bkHGrdQKopgfB4mcIfHVc ds3vIqJiesD3E19AwHtmHvveq9VwmJTvQH6/tW0PNVv5QEV99dJHuLGrtAZV3AJk/qNm p1aBOHZhpoJug7fRVooURKyNqJRKP7XG9P6KIjOyAiRSMGm5kxcrriZttFSfjFH03cVS 2pHw== 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 g124-v6si4990297pfb.280.2018.07.18.23.43.52; Wed, 18 Jul 2018 23:44:07 -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 S1729215AbeGSHXt (ORCPT + 99 others); Thu, 19 Jul 2018 03:23:49 -0400 Received: from mx2.suse.de ([195.135.220.15]:45358 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726468AbeGSHXt (ORCPT ); Thu, 19 Jul 2018 03:23:49 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 1FA3BACD9; Thu, 19 Jul 2018 06:42:15 +0000 (UTC) Date: Thu, 19 Jul 2018 08:42: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: <88DC34334CA3444C85D647DBFA962C272B0C4C34@SHSMSX104.ccr.corp.intel.com> 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: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. thanks, Takashi --- a/sound/core/memalloc.c +++ b/sound/core/memalloc.c @@ -247,11 +247,10 @@ int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size, return err; if (size <= PAGE_SIZE) return -ENOMEM; + size >>= 1; aligned_size = PAGE_SIZE << get_order(size); if (size != aligned_size) size = aligned_size; - else - size >>= 1; } if (! dmab->area) return -ENOMEM;