From: John Hubbard Subject: Re: [PATCH v5 02/24] mm/gup: factor out duplicate code from four routines Date: Mon, 18 Nov 2019 23:00:33 -0800 Message-ID: <152e2ea9-edd9-f868-7731-ff467d692f5f@nvidia.com> References: <20191115055340.1825745-1-jhubbard@nvidia.com> <20191115055340.1825745-3-jhubbard@nvidia.com> <20191118094604.GC17319@quack2.suse.cz> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: <20191118094604.GC17319@quack2.suse.cz> Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org To: Jan Kara Cc: Andrew Morton , Al Viro , Alex Williamson , Benjamin Herrenschmidt , =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Christoph Hellwig , Dan Williams , Daniel Vetter , Dave Chinner , David Airlie , "David S . Miller" , Ira Weiny , Jason Gunthorpe , Jens Axboe , Jonathan Corbet , =?UTF-8?B?SsOpcsO0bWUgR2xpc3Nl?= , Magnus Karlsson , Mauro Carvalho Chehab , Michael Ellerman List-Id: dri-devel@lists.freedesktop.org On 11/18/19 1:46 AM, Jan Kara wrote: > On Thu 14-11-19 21:53:18, John Hubbard wrote: >> There are four locations in gup.c that have a fair amount of code >> duplication. This means that changing one requires making the same >> changes in four places, not to mention reading the same code four >> times, and wondering if there are subtle differences. >> >> Factor out the common code into static functions, thus reducing the >> overall line count and the code's complexity. >> >> Also, take the opportunity to slightly improve the efficiency of the >> error cases, by doing a mass subtraction of the refcount, surrounded >> by get_page()/put_page(). >> >> Also, further simplify (slightly), by waiting until the the successful >> end of each routine, to increment *nr. >> >> Reviewed-by: J=C3=A9r=C3=B4me Glisse >> Cc: Jan Kara >> Cc: Ira Weiny >> Cc: Christoph Hellwig >> Cc: Aneesh Kumar K.V >> Signed-off-by: John Hubbard >> --- >> mm/gup.c | 95 ++++++++++++++++++++++++-------------------------------- >> 1 file changed, 40 insertions(+), 55 deletions(-) >> >> diff --git a/mm/gup.c b/mm/gup.c >> index 85caf76b3012..858541ea30ce 100644 >> --- a/mm/gup.c >> +++ b/mm/gup.c >> @@ -1969,6 +1969,29 @@ static int __gup_device_huge_pud(pud_t pud, pud_t= *pudp, unsigned long addr, >> } >> #endif >> =20 >> +static int __record_subpages(struct page *page, unsigned long addr, >> + unsigned long end, struct page **pages) >> +{ >> + int nr =3D 0; >> + int nr_recorded_pages =3D 0; >> + >> + do { >> + pages[nr] =3D page; >> + nr++; >> + page++; >> + nr_recorded_pages++; >> + } while (addr +=3D PAGE_SIZE, addr !=3D end); >> + return nr_recorded_pages; >=20 > nr =3D=3D nr_recorded_pages so no need for both... BTW, structuring this = as a > for loop would be probably more logical and shorter now: >=20 > for (nr =3D 0; addr !=3D end; addr +=3D PAGE_SIZE) > pages[nr++] =3D page++; > return nr; >=20 Nice touch, I've applied it. thanks, --=20 John Hubbard NVIDIA > The rest of the patch looks good to me. >=20 > Honza >=20