Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp910748pxy; Wed, 28 Apr 2021 17:10:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzgg9w//SumnlH1IKD+nMwLI9d2fQsJalIz/GTMhiZbY8+UoQx5spxDr0E+bk6/Hxb3LXw5 X-Received: by 2002:a63:4a44:: with SMTP id j4mr29133527pgl.283.1619655048941; Wed, 28 Apr 2021 17:10:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619655048; cv=none; d=google.com; s=arc-20160816; b=KgDCgG9jpH2S8ZcRv8dHDZqp39uWLfvdwYYpP4Ub4LRMpyDZDyomJtUenLp4NQgxre h4yM2GsdhozB+htQeS1SdDOKs7BZWCSCiFigBDDA55i3wRgaEYs5CE4DEXHzgUe2vTo8 teZ6rAc9lPk76krm3Jsf/4Lj2O8j1FdCftM9iXSkrcTM9f/xGEa8zGpdowIiAqF4GZjr alzjAzZfhH9TUYN17Aiq01eQCN38n037doH71gBoZHtJFDXgMCjIa8ZVffoOdg9V7zLx +pope5mbT+Ha1uzKpTwa6Rzl5nhbyFsutEeerqBtLc6x74RoKql476Z+pp/ADDF1tn1P bT3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=r7GVrafhqBt/NzEHPShnR5kpF2aomX40MHGlLdQ6KOU=; b=L2r9DEySFkhEtvWGb1TNpfExmquyqnEsBOlkim2VG/ZSYmLSu9rFTaOSRETOA+Sqxy QO/otjOEn4Q6FPUmvWa33IigQvALfIzBJHGNV92X2pbmZwqS+MVvBabCm/51MAlGDTRu FmgaqkZGv33qC2ZzO0KYCKABOmVxPUytv+hU56wu0P2XJFY7KI+vr7bt+3drAEbJq1e0 VBFVP1CLWE5kwZH5lOMB0A0SHGRMRMmfoeEwVsoWa6ykfYcMnHIIbA1EKQKb8y1LOHFH 5SfCOnblUa3ag+Fh/pFj/CZYhu9MFskF4JRdXl76i6odQ3g2/SwiFKTxDT7g5RDvRUzd GU9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=InhhHjuE; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c17si1270523pgv.34.2021.04.28.17.10.35; Wed, 28 Apr 2021 17:10:48 -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=@redhat.com header.s=mimecast20190719 header.b=InhhHjuE; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231898AbhD2AJf (ORCPT + 99 others); Wed, 28 Apr 2021 20:09:35 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:34669 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231627AbhD2AJf (ORCPT ); Wed, 28 Apr 2021 20:09:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619654929; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=r7GVrafhqBt/NzEHPShnR5kpF2aomX40MHGlLdQ6KOU=; b=InhhHjuE7QNDE2xwEwSZW8DUThLmnPvaaSs0kU1Eyw3NEjLXn3NLWzZE8xahzwVMkTcGxi kDqifjMo6uBgA7ewdKGpxr4qL23ZVYUl/gX30xMHl4truBdktYXCMiXEZtjHx1HdzpYGvE mW4C7zWX1uEcNrI97XX9uB/fu32sTiE= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-414-f9WpmmTaNrqsKr82kTU0xg-1; Wed, 28 Apr 2021 20:08:47 -0400 X-MC-Unique: f9WpmmTaNrqsKr82kTU0xg-1 Received: by mail-qk1-f197.google.com with SMTP id m4-20020a37a3040000b02902e6776757c3so1878131qke.13 for ; Wed, 28 Apr 2021 17:08:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=r7GVrafhqBt/NzEHPShnR5kpF2aomX40MHGlLdQ6KOU=; b=Rn5FhgqmYAFgJOIrF/zt9eh5K5AiGK6bI0htpWxRCVtONeofeF/Qx/Rk2C5+WPfWhy VA60S7bALrGKDVFABxuxSoik5+FqCFZE7yGxwEPUKVwrBdvRli8oqcMPOONuOmPOTxxZ +AN7aWSPKFFlCIUBKP/DOaQC6OpVJJoZal7bc96d6kG/sW+InFNsxnDzHPyR4a2G6wqB 3ilRpQlbXYahZ558swPgH6VppJrd6j7ujqJMxU7cpSUwfZjexNARfHobbq/JUuwfu4tZ CfpLXwbqhuSh84X4c0HHAX27aSU/tbG/P6MxHVKyDal3msyh2nrIo+FNTRodtRJMcvOE Huyg== X-Gm-Message-State: AOAM530jEvp3YeGHs5pJa1utBCSavR9McLaeTZm6ra0d+wg0sgBLTrO0 Zeb4BSOewI/pBMPhq2ANgEsPfo/a2LGcVhzMB4q6OsC3Ix6cpI8JhISiU58ifnjNW2v1HIJMPgF 9d+BZufbhJerkknDb1f/VSEo+ X-Received: by 2002:ac8:7947:: with SMTP id r7mr28544012qtt.104.1619654927363; Wed, 28 Apr 2021 17:08:47 -0700 (PDT) X-Received: by 2002:ac8:7947:: with SMTP id r7mr28543999qtt.104.1619654927126; Wed, 28 Apr 2021 17:08:47 -0700 (PDT) Received: from xz-x1 (bras-base-toroon474qw-grc-77-184-145-104-227.dsl.bell.ca. [184.145.104.227]) by smtp.gmail.com with ESMTPSA id a22sm1020526qtp.80.2021.04.28.17.08.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Apr 2021 17:08:46 -0700 (PDT) Date: Wed, 28 Apr 2021 20:08:45 -0400 From: Peter Xu To: Axel Rasmussen Cc: Andrea Arcangeli , Andrew Morton , Hugh Dickins , Lokesh Gidra , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] userfaultfd: release page in error path to avoid BUG_ON Message-ID: <20210429000845.GA8339@xz-x1> References: <20210428180109.293606-1-axelrasmussen@google.com> <20210428230858.348400-1-axelrasmussen@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210428230858.348400-1-axelrasmussen@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 28, 2021 at 04:08:58PM -0700, Axel Rasmussen wrote: > Consider the following sequence of events: > > 1. Userspace issues a UFFD ioctl, which ends up calling into > shmem_mfill_atomic_pte(). We successfully account the blocks, we > shmem_alloc_page(), but then the copy_from_user() fails. We return > -ENOENT. We don't release the page we allocated. > 2. Our caller detects this error code, tries the copy_from_user() after > dropping the mmap_lock, and retries, calling back into > shmem_mfill_atomic_pte(). > 3. Meanwhile, let's say another process filled up the tmpfs being used. > 4. So shmem_mfill_atomic_pte() fails to account blocks this time, and > immediately returns - without releasing the page. > > This triggers a BUG_ON in our caller, which asserts that the page > should always be consumed, unless -ENOENT is returned. > > To fix this, detect if we have such a "dangling" page when accounting > fails, and if so, release it before returning. > > Fixes: cb658a453b93 ("userfaultfd: shmem: avoid leaking blocks and used blocks in UFFDIO_COPY") > Reported-by: Hugh Dickins > Signed-off-by: Axel Rasmussen Reviewed-by: Peter Xu Thanks, -- Peter Xu