Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp2059750ybp; Sat, 12 Oct 2019 03:21:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqx6MxxjqQfr1z3Nlh5JLSbaqBBptBeTLOZcpWsgjHTcqvLmUDoyMkbdRu67Rcd2z8sikF8Y X-Received: by 2002:aa7:c24d:: with SMTP id y13mr18217281edo.186.1570875712543; Sat, 12 Oct 2019 03:21:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570875712; cv=none; d=google.com; s=arc-20160816; b=AtOrAc/joSrXdmTkg1XFV0oPRTWQDWFaaiNuVj1Vutk6opSrMvG6cY8SbYdyVUqPmZ pyhTunzdi8/EFh1UqjTC9TVT1ASymSByVnCngwrQIrg4JMGbj4h5hMm3GCUBiUTiEfwy stb/zCCwmOjGBJDP1sR+aKpujqN5XZ00X8kJR/xhOpFygaB271sRB6pFbdKFd/Wzg4Qw C159djipU62schf6LFHdGTR0ekmoOb4zjAwHRKybMslfTAl0h5goV/kRh+IWuaa8FQXT f4dauNT4FMV1YcrqYw97AZb/in1uHcqUad8tt7foCFmVEuZOx2vZyhQXVL54ZOD8Zkq+ fFPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=1Z3Bf9f7A6JvxU1KAyWaqdtwVI1lJbNO4oaY3xv7VhQ=; b=EzjLRlcXkO++V7GyQnAseQX4+Q40yWKzwMqKkYOrKHbqApIhEX0WOf+r2zNI/CnxIV ajv6/S4RsEhD1BER5rdeZSMkkh7VL9It7mTAFHFw5qhnzjLsYyuZKvDXlKVawzLh7A9Z JcKxPGR32dU/Oa/9fTPaN6nbaSF6MEgmDBaduqVYIkKvfulmzjSzxhqyfgJfosad+S1y OrEiexOGXYswUwQbUj3rRHzIgJeZM9B9qdQLzZso0Kc3NcANZAvyb1Efm1MSM+BE/Ede +N633yVdiwIcs165+Tlg/QWq0Jz2w+aRbLgeFAWUr3L8hE1oFkNC36j/TdORMKNXnPCi incQ== 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 pj21si7247854ejb.64.2019.10.12.03.21.16; Sat, 12 Oct 2019 03:21:52 -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 S1727642AbfJLKMY (ORCPT + 99 others); Sat, 12 Oct 2019 06:12:24 -0400 Received: from mx2a.mailbox.org ([80.241.60.219]:48631 "EHLO mx2a.mailbox.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726839AbfJLKMY (ORCPT ); Sat, 12 Oct 2019 06:12:24 -0400 Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mx2a.mailbox.org (Postfix) with ESMTPS id 53317A3733; Sat, 12 Oct 2019 12:12:20 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter06.heinlein-hosting.de (spamfilter06.heinlein-hosting.de [80.241.56.125]) (amavisd-new, port 10030) with ESMTP id YvIH0-myk1cl; Sat, 12 Oct 2019 12:12:16 +0200 (CEST) Date: Sat, 12 Oct 2019 21:12:05 +1100 From: Aleksa Sarai To: Michael Ellerman Cc: mingo@redhat.com, peterz@infradead.org, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org, christian@brauner.io, keescook@chromium.org, linux@rasmusvillemoes.dk, viro@zeniv.linux.org.uk, torvalds@linux-foundation.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] usercopy: Avoid soft lockups in test_check_nonzero_user() Message-ID: <20191012101205.px6ocxg4lkuc3gia@yavin.dot.cyphar.com> References: <20191010114007.o3bygjf4jlfk242e@yavin.dot.cyphar.com> <20191011022447.24249-1-mpe@ellerman.id.au> <20191011034810.xkmz3e4l5ezxvq57@yavin.dot.cyphar.com> <87tv8euw44.fsf@mpe.ellerman.id.au> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="6w5sfp6n6zzwxgln" Content-Disposition: inline In-Reply-To: <87tv8euw44.fsf@mpe.ellerman.id.au> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --6w5sfp6n6zzwxgln Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2019-10-12, Michael Ellerman wrote: > Aleksa Sarai writes: > > On 2019-10-11, Michael Ellerman wrote: > >> On a machine with a 64K PAGE_SIZE, the nested for loops in > >> test_check_nonzero_user() can lead to soft lockups, eg: > ... > >> diff --git a/lib/test_user_copy.c b/lib/test_user_copy.c > >> index 950ee88cd6ac..9fb6bc609d4c 100644 > >> --- a/lib/test_user_copy.c > >> +++ b/lib/test_user_copy.c > >> @@ -47,9 +47,26 @@ static bool is_zeroed(void *from, size_t size) > >> static int test_check_nonzero_user(char *kmem, char __user *umem, siz= e_t size) > >> { > >> int ret =3D 0; > >> - size_t start, end, i; > >> - size_t zero_start =3D size / 4; > >> - size_t zero_end =3D size - zero_start; > >> + size_t start, end, i, zero_start, zero_end; > >> + > >> + if (test(size < 1024, "buffer too small")) > >> + return -EINVAL; > >> + > >> + /* > >> + * We want to cross a page boundary to exercise the code more > >> + * effectively. We assume the buffer we're passed has a page boundar= y at > >> + * size / 2. We also don't want to make the size we scan too large, > >> + * otherwise the test can take a long time and cause soft lockups. So > >> + * scan a 1024 byte region across the page boundary. > >> + */ > >> + start =3D size / 2 - 512; > >> + size =3D 1024; > > > > I don't think it's necessary to do "size / 2" here -- you can just use > > PAGE_SIZE directly and check above that "size =3D=3D 2*PAGE_SIZE" (not = that > > this check is exceptionally necessary -- since there's only one caller > > of this function and it's in the same file). >=20 > OK, like this? Yup -- that looks good. I'll give it a Reviewed-by once you resend it. > diff --git a/lib/test_user_copy.c b/lib/test_user_copy.c > index 950ee88cd6ac..48bc669b2549 100644 > --- a/lib/test_user_copy.c > +++ b/lib/test_user_copy.c > @@ -47,9 +47,25 @@ static bool is_zeroed(void *from, size_t size) > static int test_check_nonzero_user(char *kmem, char __user *umem, size_t= size) > { > int ret =3D 0; > - size_t start, end, i; > - size_t zero_start =3D size / 4; > - size_t zero_end =3D size - zero_start; > + size_t start, end, i, zero_start, zero_end; > + > + if (test(size < 2 * PAGE_SIZE, "buffer too small")) > + return -EINVAL; > + > + /* > + * We want to cross a page boundary to exercise the code more > + * effectively. We also don't want to make the size we scan too large, > + * otherwise the test can take a long time and cause soft lockups. So > + * scan a 1024 byte region across the page boundary. > + */ > + size =3D 1024; > + start =3D PAGE_SIZE - (size / 2); > + > + kmem +=3D start; > + umem +=3D start; > + > + zero_start =3D size / 4; > + zero_end =3D size - zero_start; > =20 > /* > * We conduct a series of check_nonzero_user() tests on a block of memo= ry --=20 Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH --6w5sfp6n6zzwxgln Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQSxZm6dtfE8gxLLfYqdlLljIbnQEgUCXaGm8gAKCRCdlLljIbnQ Emn4AP41igjRH0F5DHCfmErfNAUW5/gBhcs02P7qsfK/94ZEVQEAtLiYGeydbgdt XejHbb0dVmtijotI9qFDCyYPp0ix4Ag= =Wb1Y -----END PGP SIGNATURE----- --6w5sfp6n6zzwxgln--