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 AD1E4C77B7F for ; Tue, 16 May 2023 13:10:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 488F0280003; Tue, 16 May 2023 09:10:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43A68900002; Tue, 16 May 2023 09:10:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2B3C2280003; Tue, 16 May 2023 09:10:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1A60C900002 for ; Tue, 16 May 2023 09:10:51 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id AFFF440290 for ; Tue, 16 May 2023 13:10:50 +0000 (UTC) X-FDA: 80796153060.13.1BAFD81 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by imf23.hostedemail.com (Postfix) with ESMTP id 0BE091401BF for ; Tue, 16 May 2023 13:09:29 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=nW7C0Bg4; spf=pass (imf23.hostedemail.com: domain of tsahu@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=tsahu@linux.ibm.com; dmarc=pass (policy=none) header.from=ibm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684242570; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=nxzlSFyM1UfLWBpMcq3UIm35aq+DbuE26Lnn/akAZ+I=; b=7J51/t8xYToxSoR94WLqsfAaqW9ruE1WV2ExDvT28ce8rMda6zYXPZm3J+CCaNHU05WLAt /ANPahDax81jBCCqW6EOjAi4uA5k96V8ZKksVXQrdEpkEzoA/5dbhDkbo6jeQfaN9Pjpyx WSZo3wc/bL8lWdtB0u5Qgr9jcXulyGo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684242570; a=rsa-sha256; cv=none; b=wnnfJtYcyDV6q1F8ewwQuJoawkZZG70ph62BCKv38b+Bma7pH5tt+93Em+00tuj5Zah6S/ M4+34iRD4vVul0NQwDEQLXkX+XFBSqgyamKLMwWP22LWRp+uHAT233fVdqpZAFAtg9hfCx As2FKBJ0WJkFeoblZq4KdtrRt/IJJzE= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=nW7C0Bg4; spf=pass (imf23.hostedemail.com: domain of tsahu@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=tsahu@linux.ibm.com; dmarc=pass (policy=none) header.from=ibm.com Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 34GD8soS007201; Tue, 16 May 2023 13:09:24 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : in-reply-to : references : date : message-id : mime-version : content-type; s=pp1; bh=nxzlSFyM1UfLWBpMcq3UIm35aq+DbuE26Lnn/akAZ+I=; b=nW7C0Bg4E1f/rxu6QWAzs8Zyyf+NOTjzz6ZVecIAT439ZQoCQIGytyYXmqVu+xn6bSqQ jTuUeF18T8dwd8m1ACLpLT8H5wDP5wIxA3uKNGdpdZD4dCDQohQ4tNSv2XGsPAsKJqzY LBrp5WUYLJHLmX0J0wcUKEMJg0fk+NAnD2YKPqNr8VwPS8AlgaeHCb0c9MmWmO4czlfl o/oTX6Tdq9cmluqUYxs5WhxebvZ/mhCVjlHATtfiy41L3ehI6YWmC0ykQUlufmdIdMMw Tf5IuyGHjNcW7NMW5zLnvUwssu9tU4GVIInxElVL/tfZajB+IDPHkOTEMKmvihzbiRC6 nw== Received: from ppma03ams.nl.ibm.com (62.31.33a9.ip4.static.sl-reverse.com [169.51.49.98]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3qm9akafr6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 May 2023 13:09:23 +0000 Received: from pps.filterd (ppma03ams.nl.ibm.com [127.0.0.1]) by ppma03ams.nl.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 34G1c0nk027917; Tue, 16 May 2023 13:09:19 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma03ams.nl.ibm.com (PPS) with ESMTPS id 3qj264sn5f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 May 2023 13:09:19 +0000 Received: from smtpav05.fra02v.mail.ibm.com (smtpav05.fra02v.mail.ibm.com [10.20.54.104]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 34GD9FAk9437940 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 16 May 2023 13:09:15 GMT Received: from smtpav05.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6B7BC20049; Tue, 16 May 2023 13:09:15 +0000 (GMT) Received: from smtpav05.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 45F2D20043; Tue, 16 May 2023 13:09:13 +0000 (GMT) Received: from tarunpc (unknown [9.199.157.25]) by smtpav05.fra02v.mail.ibm.com (Postfix) with ESMTPS; Tue, 16 May 2023 13:09:12 +0000 (GMT) From: Tarun Sahu To: Matthew Wilcox Cc: linux-mm@kvack.org, akpm@linux-foundation.org, muchun.song@linux.dev, mike.kravetz@oracle.com, aneesh.kumar@linux.ibm.com, sidhartha.kumar@oracle.com, gerald.schaefer@linux.ibm.com, linux-kernel@vger.kernel.org, jaypatel@linux.ibm.com Subject: Re: [PATCH v2] mm/folio: Avoid special handling for order value 0 in folio_set_order In-Reply-To: References: <20230515170809.284680-1-tsahu@linux.ibm.com> Date: Tue, 16 May 2023 18:39:11 +0530 Message-ID: <87fs7wqv7s.fsf@linux.ibm.com> MIME-Version: 1.0 Content-Type: text/plain X-TM-AS-GCONF: 00 X-Proofpoint-GUID: aBal4JNQ1_zjIOHk7A1PNZgOgWx31HNL X-Proofpoint-ORIG-GUID: aBal4JNQ1_zjIOHk7A1PNZgOgWx31HNL X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-05-16_06,2023-05-16_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 mlxlogscore=999 clxscore=1015 malwarescore=0 mlxscore=0 suspectscore=0 phishscore=0 spamscore=0 priorityscore=1501 adultscore=0 lowpriorityscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305160110 X-Rspamd-Queue-Id: 0BE091401BF X-Rspam-User: X-Rspamd-Server: rspam06 X-Stat-Signature: 3gd7r7skqtab4nrgoua53wb67mje15rx X-HE-Tag: 1684242569-522929 X-HE-Meta: U2FsdGVkX18bud3tpTIO+TsX+RmjazDiLkiw1xgZ8/KFnE8ZdW2u5jfwAK9y+Ycx1H7BfgSwqNPcUDxeWp3bjZhvwf6bzM7xqcO+sMts6h/+3RXsngV504nFh63zkZ+3KNi1mnXZBidjtYCA3dSICaY3WAv4NmlSthwfaD1Ac/EVWTCgiI9WqOBDD48mHRfioqb/MABp8o+91MWQhGtcCEz9OWh/tRf54TARt4vGIyrYeU+jouzHhKZ9U57YPlmhVR8XEkyZsR+9LilZqSV35Mo0PWZJPXddOaV8gnTeqyc3W4xmUvc9SdT9DoixXAneqDoaHKuAyDladQznnkK6nR0S8+TmC3/ELdrP890XOPmwArjyMAU5ecAzFa2jPqOUWqEFIjAyxjnzrmD0XvSJ99Zo3Lw3JxrAncCP8uLddsHqA8FJ+FHyb/f2rrVBspdHV9PqXXdUwla4TtVMq7xIitpULqtjx5KANzVMvS7eIaXe55eunDFV1pCCT6QeIcXifDVr3XyMCXCX4Mpd1UN3mF4uxi5ivimIGq/agxA0LhHsIIrUCCEHQ05WUvJOFAKMMhKGBuRdjIEkvKTkALgjEo8IkecyxrnZTdAFDol/lydRU2krr2UqLb8tkeiv61OD9ZlJXKlyc5++cyuNimepP77mTzwNHdGWdtw/UVserUxLMW8ytNSXHfG+CPaTdrQbBitQQPQFYq6Mf8m4qb1RybE7KgYUyQE+1xT1X7TCkX+T2wREyqfxzz8rN3Mz1mpq3afrN0pJSwon710BKujhkr7cMNIR8Oxf4Bi7mjztus+NyEECcbib9BStc6G2ITq1laXHCvUJ2ODM9VUG5XUTDtbS2CBlUV7gxlZKY9EoNpRN62aCkhq0vxae26pKsXpu6AA6L9Kdq+is4Dhh0ZT9JutKyGwaQhO9jgNCnd6bp8WD+bykAYXjfnI6zbnJaM43ttX/AKXnYbcM2nn+lH4 m0AV43pN jrzZbkZhdqlUtf4irFf8wTktO60DEEUp1sQJxOt2DJf+HfyuKAI1KMJh+h5TbMgy+31iaA6xN/OfMcm+MZlF7XDPMUaN5VsCD5v5zuKZMqq8EAAbg3DX69yiCMAiHmcfHTQ9bnOoo40F8RpLLbNjgn+e78Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.001789, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Hi Mathew, Matthew Wilcox writes: > On Mon, May 15, 2023 at 10:38:09PM +0530, Tarun Sahu wrote: >> @@ -1951,9 +1950,6 @@ static bool __prep_compound_gigantic_folio(struct folio *folio, >> struct page *p; >> >> __folio_clear_reserved(folio); >> - __folio_set_head(folio); >> - /* we rely on prep_new_hugetlb_folio to set the destructor */ >> - folio_set_order(folio, order); >> for (i = 0; i < nr_pages; i++) { >> p = folio_page(folio, i); >> >> @@ -1999,6 +1995,9 @@ static bool __prep_compound_gigantic_folio(struct folio *folio, >> if (i != 0) >> set_compound_head(p, &folio->page); >> } >> + __folio_set_head(folio); >> + /* we rely on prep_new_hugetlb_folio to set the destructor */ >> + folio_set_order(folio, order); > > This makes me nervous, as I said before. This means that > compound_head(tail) can temporarily point to a page which is not marked > as a head page. That's different from prep_compound_page(). You need to > come up with some good argumentation for why this is safe, and no amount > of testing you do can replace it -- any race in this area will be subtle. IIUC, I am certain that it is safe to move these calls and agree with what Mike said. Here is my reasoning: When we get pages from CMA allocator for gigantic folio, page refcount for each pages is 1. page_cache_get_speculative (now folio_try_get_rcu) can take reference to any of these pages before prep_compound_gigantic_folio explicitly freeze refcount of these pages. With this race condition there are 2 possible situation. ... if (!demote) { if (!page_ref_freeze(p, 1)) { pr_warn("HugeTLB page can not be used due to unexpected inflated ref count\n"); goto out_error; } } else { VM_BUG_ON_PAGE(page_count(p), p); } if (i != 0) set_compound_head(p, &folio->page); } ... 1. In the current code, before freezing refcount of nth (hence, n+th) tail page, folio_try_get_rcu might try to take nth tail page reference, so refcount will be increased of the nth tail page not the head page (as compound head is not yet set for nth tail page). and once this happens, nth iteration of loop will cause error and prep_compound_gigantic_folio will fail. So, setting the PG_head at the starting of for-loop or at the end won't have any difference to this flow. 2. If reference for the head page is taken by folio_try_get_rcu before freezing it, prep_compound_gigantic_page will fail, but before PG_head and folio_order of head page is cleared in error path, the caller of folio_try_get_rcu path will find that this page is head page and might try to operate on its tail pages while these tail pages are invalid. Hence, It will be safer if we call __folio_set_head and folio_set_order after freezing the tail page refcount. ~Tarun