From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B08CC25B75 for ; Sat, 1 Jun 2024 00:59:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F29416B00A2; Fri, 31 May 2024 20:59:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ED9306B00A3; Fri, 31 May 2024 20:59:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA0836B00A4; Fri, 31 May 2024 20:59:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id BEB796B00A2 for ; Fri, 31 May 2024 20:59:38 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 4F97A16019D for ; Sat, 1 Jun 2024 00:59:38 +0000 (UTC) X-FDA: 82180512036.01.EB21F79 Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) by imf19.hostedemail.com (Postfix) with ESMTP id 7DB8D1A0002 for ; Sat, 1 Jun 2024 00:59:36 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fjSJXQF5; spf=pass (imf19.hostedemail.com: domain of shy828301@gmail.com designates 209.85.208.174 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717203576; a=rsa-sha256; cv=none; b=ykhqcM+Gcfhsn7hQT/Uqw5ZvuQuD3ABGOEBnvHs2CtcRwXTutEMj0XfSWcrf7J97e/qoDw +JBuSmZCi5s/MkIOSqlE1IpCSzU2CVJ5w/eWitcJ1nWKr4tdVojMvq3uRLh2nb/v4X+mdW xv+RlBkDCbzp8gj++ZWBsJSpKsyZ2cc= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fjSJXQF5; spf=pass (imf19.hostedemail.com: domain of shy828301@gmail.com designates 209.85.208.174 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717203576; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4c1H1czvW5wu5dZLp5czTd3lmrqNcfjJq6zE2Ej+bK8=; b=nYeZfZpzCd7mEY/5tgb06ipwu9HEsuE+Do/1aRhVS209NporDmtdgboNSv33JrFeKT5azP Ii+0GYZjqOSiDmpjcHMlA5tckq4rbs2maFm1VmHR/Kb1FWjcbynr/z0AQcKiLTicVYeUjR 4xChZ1U+rRQ75YZsEI9XCV1GdxPhe5s= Received: by mail-lj1-f174.google.com with SMTP id 38308e7fff4ca-2e719bab882so27586401fa.3 for ; Fri, 31 May 2024 17:59:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717203575; x=1717808375; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=4c1H1czvW5wu5dZLp5czTd3lmrqNcfjJq6zE2Ej+bK8=; b=fjSJXQF5hPHdxEpiBMLK9mqyTAfHIoIqSntMtBL1i0Q41rd+szqJFBRNhTqE/5ksEL VQv25HdLLPDSsroI/e8RM/oGvbDL9j07Jy4Iy2koCCdAMybRgQu9FP/lxSX4Fi/GZkPV KCC/1/3rNcy8EpBo8HfhcWkiSPeZEoVP+79eoS7A63EXQKxEDHLmzGYF17x20M6B+WFU NyL4sQofT1fHHWV1KoY2OGwExI26HVA4l1X4TIlhPAz1pmt5I1qU/DswO2D3xMDWtslh +GWy2+R8pnTimN0w/lX4Y6Ngy/pLaaiwsf3rKkMRPNuBvTHVeNaz2W3CwtgtQ5+oCHQx Ohgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717203575; x=1717808375; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4c1H1czvW5wu5dZLp5czTd3lmrqNcfjJq6zE2Ej+bK8=; b=k9x6sx8H1MI6UzA0666APwesWy+d1TKVtnHbsZmhYNaR2rUimyGoHpVCVx+w2/EbA0 2UJyO7252pjKzMeyVT4FoIL/rQU3HxjryJi9T0lbb0xf9n0ngPPoxTU4p3vUZpBHKeVy IdFYAqBCfnZDYOP0Z1t1ckJYdp87uwhqqan2uKgCeFGZMeach3Ty2r9mx2uAE7mS/fCg If97+5/UWawlD/C24ghkuXfz3iuNa+QwPxImNKY97GlAQbkWC/orBsQZI/GGzDCzMuOz KGZq+/0LwWANH4gg3I2XceNNsSt27LYiOm7TmwuiGomFG2n1r4cd0EMTuc8wwvKXp7xw +IcQ== X-Forwarded-Encrypted: i=1; AJvYcCX/enUWEx8QV9WEstyI+ePpGWbH2I749PGnsUsOCLd0urrIA4ylUovXd+Qvs6QB784nHCgBA1VAQcjbm2xUsx8z4ro= X-Gm-Message-State: AOJu0Yzy4lWMT6V6mFWptdUGSDv2lizWHr5zE08PfKNDELh4KsSHSGMq asMearWaveOfyuGNGLB61yDHb1zwZiwN5yovcH0QLTeJ8XVosxQUZROheH5rFT164to9pSE6aZg XabqzFwcQpNQVx4VJudqAtS1qzVM= X-Google-Smtp-Source: AGHT+IHeoalXtZ3m7YrazfHsP5LzCh2JGOqNV7G4XfGNDHFGYgwWm2xv+wPuIMhJJDh5SJTmRIOAomfVKcyaXijLOF0= X-Received: by 2002:a2e:9054:0:b0:2d6:c43e:f0b3 with SMTP id 38308e7fff4ca-2ea951d21f8mr19580481fa.50.1717203574325; Fri, 31 May 2024 17:59:34 -0700 (PDT) MIME-Version: 1.0 References: <202405311534.86cd4043-lkp@intel.com> <890e5a79-8574-4a24-90ab-b9888968d5e5@redhat.com> In-Reply-To: From: Yang Shi Date: Fri, 31 May 2024 17:59:22 -0700 Message-ID: Subject: Re: [linus:master] [mm] efa7df3e3b: kernel_BUG_at_include/linux/page_ref.h To: Peter Xu Cc: David Hildenbrand , kernel test robot , Jason Gunthorpe , Vivek Kasireddy , Rik van Riel , oe-lkp@lists.linux.dev, lkp@intel.com, linux-kernel@vger.kernel.org, Andrew Morton , Matthew Wilcox , Christopher Lameter , linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 7DB8D1A0002 X-Stat-Signature: j53p9y39r7sqjto43ahskasppijg4b8n X-HE-Tag: 1717203576-523900 X-HE-Meta: U2FsdGVkX1/z5AhavHms22UfQt2K3+ekSKQF4nwIlMGUamlcpR44qW+QTkCOIW5PZiq/R9idW5qmh5vhCAuy2RIJ52TvYsbJM04NQVbwlyLlPQI8akIxBT749O0ncXIin5aOjGjcBWVFTYLknEg4kNOlRq7VFGDT/KZ1iGT+CtHdesFiyJkUkYwSV4Z4GO+MRHGGHyeNpSpaRq13TIBOu1udV3SNWC/6V6g0CqHMcscYpySsCpc1yzt3JRZz0qduOYhjgZOEugDZPl6SWqaS1ie8pVhxFi22KlU/n2onW+fR4y/iKSRroWbX13f0V1UIeYL+DMzgt8tr5FIq+PUzt9O7no2nwPDbcX40+uggs84/EeLfH3v51SyRM4bJH+8gZqW4+S2+DTNJ7KZymT9z5Cw3DwYRmhjSG+HzOjRSoYah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edence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Fri, May 31, 2024 at 5:01=E2=80=AFPM Yang Shi wrot= e: > > On Fri, May 31, 2024 at 4:25=E2=80=AFPM Peter Xu wrot= e: > > > > On Fri, May 31, 2024 at 07:46:41PM +0200, David Hildenbrand wrote: > > > try_grab_folio()->try_get_folio()->folio_ref_try_add_rcu() > > > > > > Is called (mm-unstable) from: > > > > > > (1) gup_fast function, here IRQs are disable > > > (2) gup_hugepte(), possibly problematic > > > (3) memfd_pin_folios(), possibly problematic > > > (4) __get_user_pages(), likely problematic > > > > > > (1) should be fine. > > > > > > (2) is possibly problematic on the !fast path. If so, due to commit > > > a12083d721d7 ("mm/gup: handle hugepd for follow_page()") ? CCing = Peter. > > > > > > (3) is possibly wrong. CCing Vivek. > > > > > > (4) is what we hit here > > > > I guess it was overlooked because try_grab_folio() didn't have any comm= ent > > or implication on RCU or IRQ internal helpers being used, hence a bit > > confusing. E.g. it has different context requirement on try_grab_page(= ), > > even though they look like sister functions. It might be helpful to ha= ve a > > better name, something like try_grab_folio_rcu() in this case. > > > > Btw, none of above cases (2-4) have real bug, but we're looking at some= way > > to avoid triggering the sanity check, am I right? I hope besides the h= ost > > splash I didn't overlook any other side effect this issue would cause, = and > > the splash IIUC should so far be benign, as either gup slow (2,4) or th= e > > newly added memfd_pin_folios() (3) look like to have the refcount stabl= ized > > anyway. > > > > Yang's patch in the other email looks sane to me, just that then we'll = add > > quite some code just to avoid this sanity check in paths 2-4 which seem= s > > like an slight overkill. > > > > One thing I'm thinking is whether folio_ref_try_add_rcu() can get rid o= f > > its RCU limitation. It boils down to whether we can use atomic_add_unle= ss() > > on TINY_RCU / UP setup too? I mean, we do plenty of similar things > > (get_page_unless_zero, etc.) in generic code and I don't understand why > > here we need to treat folio_ref_try_add_rcu() specially. > > > > IOW, the assertions here we added: > > > > VM_BUG_ON(!in_atomic() && !irqs_disabled()); > > > > Is because we need atomicity of below sequences: > > > > VM_BUG_ON_FOLIO(folio_ref_count(folio) =3D=3D 0, folio); > > folio_ref_add(folio, count); > > > > But atomic ops avoids it. > > Yeah, I didn't think of why atomic can't do it either. But is it > written in this way because we want to catch the refcount =3D=3D 0 case > since it means a severe bug? Did we miss something? Thought more about it and disassembled the code. IIUC, this is an optimization for non-SMP kernel. When in rcu critical section or irq is disabled, we just need an atomic add instruction. folio_ref_add_unless() would yield more instructions, including branch instruction. But I'm wondering how useful it would be nowadays. Is it really worth the complexity? AFAIK, for example, ARM64 has not supported non-SMP kernel for years. My patch actually replaced all folio_ref_add_unless() to folio_ref_add() for slow paths, so it is supposed to run faster, but we are already in slow path, it may be not measurable at all. So having more simple and readable code may outweigh the potential slight performance gain in this case? > > > > > This chunk of code was (mostly) originally added in 2008 in commit > > e286781d5f2e ("mm: speculative page references"). > > > > In short, I'm wondering whether something like below would make sense a= nd > > easier: > > > > =3D=3D=3D8<=3D=3D=3D > > diff --git a/include/linux/page_ref.h b/include/linux/page_ref.h > > index 1acf5bac7f50..c89a67d239d1 100644 > > --- a/include/linux/page_ref.h > > +++ b/include/linux/page_ref.h > > @@ -258,26 +258,9 @@ static inline bool folio_try_get(struct folio *fol= io) > > return folio_ref_add_unless(folio, 1, 0); > > } > > > > -static inline bool folio_ref_try_add_rcu(struct folio *folio, int coun= t) > > -{ > > -#ifdef CONFIG_TINY_RCU > > - /* > > - * The caller guarantees the folio will not be freed from inter= rupt > > - * context, so (on !SMP) we only need preemption to be disabled > > - * and TINY_RCU does that for us. > > - */ > > -# ifdef CONFIG_PREEMPT_COUNT > > - VM_BUG_ON(!in_atomic() && !irqs_disabled()); > > -# endif > > - VM_BUG_ON_FOLIO(folio_ref_count(folio) =3D=3D 0, folio); > > - folio_ref_add(folio, count); > > -#else > > - if (unlikely(!folio_ref_add_unless(folio, count, 0))) { > > - /* Either the folio has been freed, or will be freed. *= / > > - return false; > > - } > > -#endif > > - return true; > > +static inline bool folio_ref_try_add(struct folio *folio, int count) > > +{ > > + return folio_ref_add_unless(folio, count, 0); > > } > > > > /** > > @@ -305,7 +288,7 @@ static inline bool folio_ref_try_add_rcu(struct fol= io *folio, int count) > > */ > > static inline bool folio_try_get_rcu(struct folio *folio) > > { > > - return folio_ref_try_add_rcu(folio, 1); > > + return folio_ref_try_add(folio, 1); > > } > > > > static inline int page_ref_freeze(struct page *page, int count) > > diff --git a/mm/gup.c b/mm/gup.c > > index e17466fd62bb..17f89e8d31f1 100644 > > --- a/mm/gup.c > > +++ b/mm/gup.c > > @@ -78,7 +78,7 @@ static inline struct folio *try_get_folio(struct page= *page, int refs) > > folio =3D page_folio(page); > > if (WARN_ON_ONCE(folio_ref_count(folio) < 0)) > > return NULL; > > - if (unlikely(!folio_ref_try_add_rcu(folio, refs))) > > + if (unlikely(!folio_ref_try_add(folio, refs))) > > return NULL; > > > > /* > > =3D=3D=3D8<=3D=3D=3D > > > > So instead of adding new code, we fix it by removing some. There might= be > > some implication on TINY_RCU / UP config on using the atomic_add_unless= () > > to replace one atomic_add(), but I'm not sure whether that's a major is= sue. > > > > The benefit is try_get_folio() then don't need a renaming then, because= the > > rcu implication just went away. > > > > Thanks, > > > > -- > > Peter Xu > >