Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp7290409ybi; Wed, 5 Jun 2019 14:51:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqzVbR7mgECIWg5XUlfhrbvhDPjYk369BYC+HBEOrfC0fCYAyqIwGSVRW8d/pbHnP/uRBA8e X-Received: by 2002:aa7:82cd:: with SMTP id f13mr50713402pfn.203.1559771489093; Wed, 05 Jun 2019 14:51:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559771489; cv=none; d=google.com; s=arc-20160816; b=NrLKGI+9GIwyXQGY9NdNy+0OFhxo3P8xVn0mD1l6aGZJCqlT+bHzh2J69u7OTzhWHy CRPvbC595Hjch7kL32fjFDBmREikfXVQcjlTOIt7NUD/3ViU0R+lhnIJWytJQnhCJkwn 8mdxihcyxme+zirUEeEA793/MOulEzwgYlk7hIwT8ANXBX/yyOf6fzgYtGaOIYJ/3Aim Ekl8go77T7kUem1JvWYrencL3m+mrKZogbUeZEAitX3ToB+pH+BIwARDLpO7EbxHliqG 1gxk967u/bC9LqXvDAq8mcCpJ4+PdgFRMY5MM3EkXpoyuagp4MFPiCYc8hWRuk0BpzZp gqVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=0AHEWITpV6DiHVwFl8kRvKAjquJlUVPVbr6sr1gCdPM=; b=waeMqAr7KLTtVsjXpaBDOGjaN4OgVtJ18JTZEtonHDHtdaZuEcCvHHkBpxXGF8XFlZ JYHN14zBZIRNt/d7HOKt7XsCrp+1PDy0Hki8K6yZT04wD/zRBvQzQdZolsyHVPvwFDSs kIXKZWw3jlhSUFBIJwIFcJc/LGP7ifY5/I/RoS72aDGoOXj0uvHmMXljGBAVpp8hgTF3 S0RCuIGPThLYa7YHVR8Wrjzj1i4mkCwzpXDcAuTyOrEid8K69UXPrQxFpS6lKFK5Vulr Hd5dYx/0jt6SFAxyAUfpz/+z5gSNdTFjm7DEPtHehMU0VOWgQ40MhKUVvKKi2cnpwXdo ToSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=voYT0mE2; 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 91si32126375plh.398.2019.06.05.14.51.12; Wed, 05 Jun 2019 14:51:29 -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; dkim=pass header.i=@kernel.org header.s=default header.b=voYT0mE2; 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 S1726688AbfFEVtO (ORCPT + 99 others); Wed, 5 Jun 2019 17:49:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:58218 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726502AbfFEVtN (ORCPT ); Wed, 5 Jun 2019 17:49:13 -0400 Received: from akpm3.svl.corp.google.com (unknown [104.133.8.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 92F662067C; Wed, 5 Jun 2019 21:49:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1559771352; bh=o9XCwaZGzE63JH3BlHeLxlRN4g5v0bjuEKLTtVdRVy0=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=voYT0mE2QFPaaXfMNidp8XKZzbdetXlW2uiRL+mWOB7xc4nLzric/yQRfNuPzD+hG ltqSZsu4WPGkKl00DUSjIT3Z/2NtvKQLAI0yliqubD6O1B2ARXWZ6grl4zUSxZlkhl ngSgZ0UkH+RQp0JVpDU8lm3O3ftfuUyH8Tk5OVyM= Date: Wed, 5 Jun 2019 14:49:12 -0700 From: Andrew Morton To: Pingfan Liu Cc: linux-mm@kvack.org, Ira Weiny , Mike Rapoport , Dan Williams , Matthew Wilcox , John Hubbard , "Aneesh Kumar K.V" , Keith Busch , Christoph Hellwig , linux-kernel@vger.kernel.org Subject: Re: [PATCHv3 1/2] mm/gup: fix omission of check on FOLL_LONGTERM in get_user_pages_fast() Message-Id: <20190605144912.f0059d4bd13c563ddb37877e@linux-foundation.org> In-Reply-To: <1559725820-26138-1-git-send-email-kernelfans@gmail.com> References: <1559725820-26138-1-git-send-email-kernelfans@gmail.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 5 Jun 2019 17:10:19 +0800 Pingfan Liu wrote: > As for FOLL_LONGTERM, it is checked in the slow path > __gup_longterm_unlocked(). But it is not checked in the fast path, which > means a possible leak of CMA page to longterm pinned requirement through > this crack. > > Place a check in the fast path. I'm not actually seeing a description (in either the existing code or this changelog or patch) an explanation of *why* we wish to exclude CMA pages from longterm pinning. > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -2196,6 +2196,26 @@ static int __gup_longterm_unlocked(unsigned long start, int nr_pages, > return ret; > } > > +#ifdef CONFIG_CMA > +static inline int reject_cma_pages(int nr_pinned, struct page **pages) > +{ > + int i; > + > + for (i = 0; i < nr_pinned; i++) > + if (is_migrate_cma_page(pages[i])) { > + put_user_pages(pages + i, nr_pinned - i); > + return i; > + } > + > + return nr_pinned; > +} There's no point in inlining this. The code seems inefficient. If it encounters a single CMA page it can end up discarding a possibly significant number of non-CMA pages. I guess that doesn't matter much, as get_user_pages(FOLL_LONGTERM) is rare. But could we avoid this (and the second pass across pages[]) by checking for a CMA page within gup_pte_range()? > +#else > +static inline int reject_cma_pages(int nr_pinned, struct page **pages) > +{ > + return nr_pinned; > +} > +#endif > + > /** > * get_user_pages_fast() - pin user pages in memory > * @start: starting user address > @@ -2236,6 +2256,9 @@ int get_user_pages_fast(unsigned long start, int nr_pages, > ret = nr; > } > > + if (unlikely(gup_flags & FOLL_LONGTERM) && nr) > + nr = reject_cma_pages(nr, pages); > + This would be a suitable place to add a comment explaining why we're doing this...