Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp1465660pxt; Sat, 7 Aug 2021 11:42:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgXxtnEgio6iZbgrkYdm3MN6SsWufBNkE62sEhvFyBwZDTz4jnbFT5cd1d19nke515Yjmh X-Received: by 2002:a05:6e02:1093:: with SMTP id r19mr419098ilj.246.1628361728080; Sat, 07 Aug 2021 11:42:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628361728; cv=none; d=google.com; s=arc-20160816; b=KNnBrOpDS8rhvo9OrP32GTNa1TtL43YobBCKYak/itQEXI+9R1cEsU3WuAldXOoY7k lmsdUo/NXIeSn7QA36KaiEKQLMxFuqZ9OZILNMh9+bsE+E/6waZ6MMqKM/9OqJsI5eev ZMqOjhQp+XbBw4Wg3RrLDe29WWFh89aaZpblFLFPcbOpp5fYf6TlSQZKS4q+NqNj1GqU bS49EaU2j+z2EF4X2xZNuNkVzbKGqJ/deScdR1GNGaNu/pWGqvvA+yTZDyXr2K5izmzb uPiMOBeiq/MA5Y2VPoJdUzu4RWTfVZC2wOU+A6cFirnq1r+/7vUatmEp/i0GD7tLFyCX KtVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=oaLmGWuH6WOqpRy/XSVb5VsIb9J40R/P0xYJxYdH3lY=; b=k0UpvAtq6ofvuuDBGBJRpZBTwN1+JrOvlIe3c30w3lav7FIgvCm7xEbiIbPxObYlqk YzEoOoyx/g+I9Z4QB/LNgCBFinfT/8FIjxqbX0kkuYEe5Ff6R31UXKXfQFhxETSIEyOj H8STEMt9Xs4JKyp/UnHoWarnCierWH29lgaTYKMCkLloDvxI8QozMg/gJiyU5IX2qSBT wbCQoS9VTBMpTRQoLU7vSj5BpQIikEM1APJFixHthAzkPTiTGuZzrO/X6Wb0vbQKfhK0 gKtDLOGMZ9pN/JitUNntJpldkXCai9epgozk+CwAPppfv79hv2lP7NKnQujG5jga2Gxp 0UrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=V+N1HZP2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x26si1890223jai.112.2021.08.07.11.41.56; Sat, 07 Aug 2021 11:42:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=V+N1HZP2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229681AbhHGSle (ORCPT + 99 others); Sat, 7 Aug 2021 14:41:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:33022 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229464AbhHGSle (ORCPT ); Sat, 7 Aug 2021 14:41:34 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5DDFD60F25; Sat, 7 Aug 2021 18:41:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1628361675; bh=zvgjE7YTOv76OGelHU+WXD7jLMPH6Bf/LG1/8h+9tOM=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=V+N1HZP2hATamgCS+x0xY8/o2PAoBFXVEcQWz6anC9aVDpHJv+A4HXCdNWsbjC5cS d8fj9BRGrIm6R9d0xeYoUSqYm58PB9FjWsbBVDu9kQ2MuZGE/lnlY4ND5bkvHLWZct hKRozVDKcIf6MvZvVTQYSnmShxfhHhfsfahDgtm0= Date: Sat, 7 Aug 2021 11:41:12 -0700 From: Andrew Morton To: Miaohe Lin Cc: , , , , , Subject: Re: [PATCH 4/5] mm: gup: fix potential pgmap refcnt leak in __gup_device_huge() Message-Id: <20210807114112.6e45b31c65dd62169fee8718@linux-foundation.org> In-Reply-To: <20210807093620.21347-5-linmiaohe@huawei.com> References: <20210807093620.21347-1-linmiaohe@huawei.com> <20210807093620.21347-5-linmiaohe@huawei.com> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 7 Aug 2021 17:36:19 +0800 Miaohe Lin wrote: > When failed to try_grab_page, put_dev_pagemap() is missed. So pgmap > refcnt will leak in this case. Also we remove the check for pgmap > against NULL as it's also checked inside the put_dev_pagemap(). > > ... > > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -2253,14 +2253,14 @@ static int __gup_device_huge(unsigned long pfn, unsigned long addr, > pages[*nr] = page; > if (unlikely(!try_grab_page(page, flags))) { > undo_dev_pagemap(nr, nr_start, flags, pages); > + put_dev_pagemap(pgmap); > return 0; > } > (*nr)++; > pfn++; > } while (addr += PAGE_SIZE, addr != end); > > - if (pgmap) > - put_dev_pagemap(pgmap); > + put_dev_pagemap(pgmap); > return 1; > } We can simplify further, and remove the troublesome multiple return points? --- a/mm/gup.c~mm-gup-fix-potential-pgmap-refcnt-leak-in-__gup_device_huge-fix +++ a/mm/gup.c @@ -2247,14 +2247,13 @@ static int __gup_device_huge(unsigned lo pgmap = get_dev_pagemap(pfn, pgmap); if (unlikely(!pgmap)) { undo_dev_pagemap(nr, nr_start, flags, pages); - return 0; + break; } SetPageReferenced(page); pages[*nr] = page; if (unlikely(!try_grab_page(page, flags))) { undo_dev_pagemap(nr, nr_start, flags, pages); - put_dev_pagemap(pgmap); - return 0; + break; } (*nr)++; pfn++; _