From patchwork Sun Aug 29 01:19:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12463661 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59FE3C432BE for ; Sun, 29 Aug 2021 01:20:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C082B60295 for ; Sun, 29 Aug 2021 01:20:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C082B60295 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id CA90A6B006C; Sat, 28 Aug 2021 21:20:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C578C6B0071; Sat, 28 Aug 2021 21:20:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B46FF8D0001; Sat, 28 Aug 2021 21:20:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0229.hostedemail.com [216.40.44.229]) by kanga.kvack.org (Postfix) with ESMTP id A5CD76B006C for ; Sat, 28 Aug 2021 21:20:10 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 45028180ACEFB for ; Sun, 29 Aug 2021 01:20:10 +0000 (UTC) X-FDA: 78526362180.05.CCCA4B6 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf20.hostedemail.com (Postfix) with ESMTP id ECD56D0000A9 for ; Sun, 29 Aug 2021 01:20:09 +0000 (UTC) Received: by mail-pf1-f175.google.com with SMTP id v123so9162344pfb.11 for ; Sat, 28 Aug 2021 18:20:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=0O6GrKm57SYbqv9M7au82BgC1oQhBuwVa1aqBn+1mlQ=; b=fzoSwxZnAQy+WIMycbrwKfr0v4l0w6qXgtmrOpfi8My3hcJ+71//5F2GoAz61fTf6s eErC9CAMLAumhuYSaTOgpa6aEgXLiSvy6V0QeZYISRi+h+x+MJmWlQE7ZHBs11LbtPIw 6abGkl+4PC6gsflLsWRihXG+oHL0dUI8Q/2Hb5ZGI8ndhnOEAMfMQN2Xyu3jwX1w9wxJ eGn9Xi97L++WKgXnTzOo2/yjFQxP9vAd74l2mVCwEZV86Y5YJQsnsE3ZvcHnuro3ga+s RNYr0S0Vodr7Y/3ChvkyvrleiaUsCnbbd5APj38kIFJoPWkQdrD6ysNbPDS8WgIfesEM 2Hpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=0O6GrKm57SYbqv9M7au82BgC1oQhBuwVa1aqBn+1mlQ=; b=uQqNPDSDcrBl1YyECbabwxZoANHRsZB856aL8WbXi4VfL7IoYu1MPun5bWzqEu2JyP DsHgUltYKFHwqjY+o1yyE3SaAWwns1PUzisY9YtWZ9Iv2iHB4RYw09dRSNVarX/s6oJ3 ejizuQYwkDSjFUHpLP56Q1UVqwXmQhrfnB3kT7yCWhI1Q3GF0eKjf35LFgeQeLxagQ0e ElwCKco+OsOPdMWpdTWPFyI0eEVmw5DpXgqIEK58IFBBAEo6XgRf8qKk9bByIEhFVpSn REuiSnZMoSnmaA6yI9HhMipWk8QMhQozhfIspcHthAcbGIQnZ66383g075qOQXHdT1tK YB/w== X-Gm-Message-State: AOAM531LtxBZrlE7D08mS2c4tSB2yLTPFu1vsG3GuRClbLo2hLJ+Xq/f gP7AiBVt8PA/dp50avZ+lWM= X-Google-Smtp-Source: ABdhPJw2Y4MrmhLOEHHcciSydUx1DEYA4oyiUde0A4kGZrm8YUgtHJOXeUmIA8na/s7+Nz7miA4jog== X-Received: by 2002:a62:a513:0:b029:3e0:719d:397f with SMTP id v19-20020a62a5130000b02903e0719d397fmr16108941pfm.55.1630200008700; Sat, 28 Aug 2021 18:20:08 -0700 (PDT) Received: from localhost.localdomain ([141.164.38.246]) by smtp.gmail.com with ESMTPSA id y23sm9983122pfe.129.2021.08.28.18.20.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Aug 2021 18:20:08 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, Yafang Shao , "Kirill A . Shutemov" Subject: [PATCH] mm, gpu: fix error when FOLL_MLOCK an unpresent page Date: Sun, 29 Aug 2021 01:19:53 +0000 Message-Id: <20210829011953.9051-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=fzoSwxZn; spf=pass (imf20.hostedemail.com: domain of laoarshao@gmail.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=laoarshao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Stat-Signature: s35676ep39hoo93yr9gdgm7jehhti6ca X-Rspamd-Queue-Id: ECD56D0000A9 X-Rspamd-Server: rspam04 X-HE-Tag: 1630200009-598138 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: There was an error occurred in my server as follows, [137830.506483] general protection fault: 0000 [#2] SMP NOPTI [137830.506488] CPU: 5 PID: 8500 Comm: user Tainted: P D OE 4.18.0-22-generic #23~18.04.1-Ubuntu [137830.506491] Hardware name: Dell Inc. Precision 5820 Tower/06JWJY, BIOS 2.8.0 01/15/2021 [137830.506498] RIP: 0010:tlp_write+0x16a/0x1d0 [tlp] [137830.506500] Code: 25 00 00 00 4c 89 e2 4c 89 e8 48 2b 05 5f ad f1 cb 48 c1 f8 06 48 c1 e0 0c 48 03 05 60 ad f1 cb 48 89 05 81 22 00 00 48 89 c7 a4 48 c7 c7 50 81 8a c0 48 8b 35 6e 22 00 00 e8 e4 02 e5 ca 48 [137830.506545] RSP: 0018:ffffa79708cfbe10 EFLAGS: 00010207 [137830.506549] RAX: 0004098280000000 RBX: 000000000000000e RCX: 0000000000000025 [137830.506552] RDX: 00007f3fdcf77000 RSI: ffffffffc08a8128 RDI: 0004098280000000 [137830.506554] RBP: ffffa79708cfbe88 R08: 000000000000046b R09: 000000000000000f [137830.506557] R10: ffff9c4280000738 R11: ffffffff8cf8a80e R12: 00007f3fdcf77000 [137830.506559] R13: 0000000000000000 R14: ffff9c52a2e13b80 R15: ffff9c5299e29800 [137830.506562] FS: 00007f3fdcf414c0(0000) GS:ffff9c52df740000(0000) knlGS:0000000000000000 [137830.506564] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [137830.506567] CR2: 00007f3fdcf78000 CR3: 0000001020b66002 CR4: 00000000003606e0 [137830.506570] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [137830.506572] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [137830.506574] Call Trace: [137830.506584] __vfs_write+0x1b/0x40 [137830.506587] vfs_write+0xb1/0x1a0 [137830.506590] ksys_write+0x55/0xc0 [137830.506594] __x64_sys_write+0x1a/0x20 [137830.506600] do_syscall_64+0x5a/0x120 [137830.506608] entry_SYSCALL_64_after_hwframe+0x44/0xa9 [137830.506611] RIP: 0033:0x7f3fdca71154 [137830.506612] Code: 89 02 48 c7 c0 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 48 8d 05 b1 07 2e 00 8b 00 85 c0 75 13 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 54 f3 c3 66 90 41 54 55 49 89 d4 53 48 89 f5 [137830.506656] RSP: 002b:00007ffe7efbb5f8 EFLAGS: 00000246 ORIG_RAX: 0000000000000001 [137830.506659] RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007f3fdca71154 [137830.506661] RDX: 000000000000000e RSI: 00007f3fdcf78000 RDI: 0000000000000003 [137830.506663] RBP: 00007ffe7efbb640 R08: 0000000000000000 R09: 0000000000000000 [137830.506665] R10: 0000000000000000 R11: 0000000000000246 R12: 0000562b09bbd810 [137830.506667] R13: 00007ffe7efbb720 R14: 0000000000000000 R15: 0000000000000000 [137830.506670] Modules linked in: tlp(OE) snd_hda_codec_hdmi binfmt_misc nls_iso8859_1 nvidia_uvm(OE) nvidia_drm(POE) nvidia_modeset(POE) intel_rapl skx_edac nfit nvidia(POE) x86_pkg_temp_thermal intel_powerclamp coretemp snd_hda_codec_realtek snd_hda_codec_generic kvm_intel kvm joydev irqbypass crct10dif_pclmul snd_hda_intel crc32_pclmul ghash_clmulni_intel pcbc snd_hda_codec snd_hda_core snd_hwdep snd_pcm dell_wmi sparse_keymap aesni_intel wmi_bmof dell_smbios video aes_x86_64 crypto_simd snd_seq_midi cryptd snd_seq_midi_event intel_wmi_thunderbolt glue_helper dell_wmi_descriptor snd_rawmidi dcdbas intel_cstate drm_kms_helper snd_seq input_leds dell_smm_hwmon intel_rapl_perf drm serio_raw snd_seq_device fb_sys_fops snd_timer syscopyarea sysfillrect sysimgblt snd mei_me soundcore mei ioatdma wmi dca [137830.506738] acpi_tad mac_hid sch_fq_codel parport_pc ppdev lp parport ip_tables x_tables autofs4 hid_generic usbhid hid uas usb_storage nvme nvme_core vmd ahci e1000e libahci [137830.506761] ---[ end trace a751f7202b6306cc ]--- [137830.507839] RIP: 0010:tlp_write+0x16a/0x1d0 [tlp] [137830.507842] Code: 25 00 00 00 4c 89 e2 4c 89 e8 48 2b 05 5f ad f1 cb 48 c1 f8 06 48 c1 e0 0c 48 03 05 60 ad f1 cb 48 89 05 81 22 00 00 48 89 c7 a4 48 c7 c7 50 81 8a c0 48 8b 35 6e 22 00 00 e8 e4 02 e5 ca 48 [137830.507886] RSP: 0018:ffffa79709cf7e10 EFLAGS: 00010207 [137830.507889] RAX: 0004098280000000 RBX: 000000000000000e RCX: 0000000000000025 [137830.507891] RDX: 00007f1b78242000 RSI: ffffffffc08a8128 RDI: 0004098280000000 [137830.507893] RBP: ffffa79709cf7e88 R08: 0000000000000437 R09: 0000000000000004 [137830.507896] R10: ffff9c4280000e08 R11: ffffffff8cf8a80d R12: 00007f1b78242000 [137830.507898] R13: 0000000000000000 R14: ffff9c5289665940 R15: ffff9c52d52f4400 [137830.507900] FS: 00007f3fdcf414c0(0000) GS:ffff9c52df740000(0000) knlGS:0000000000000000 [137830.507903] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [137830.507905] CR2: 00007f3fdcf78000 CR3: 0000001020b66002 CR4: 00000000003606e0 [137830.507907] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [137830.507909] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 As we can see, the error was caused by writing an unpresent page in my kernel module 'tlp'. After some analyzation, I found it was caused by a bug in GUP. When the kernel module calls get_user_pages() with FOLL_MLOCK being set but FOLL_POPULATE being unset, if the page of the user addr isn't present, the kernel won't fault in the user page. But get_user_pages() still count this unpresent page as a present page and then returns a wrong number of pinned pages. Then the kernel module will think it has already pinned the page successfully and do the DMA in the unpresent page. At the last the above error will occur. After this change, the above error disappears. Signed-off-by: Yafang Shao Cc: Kirill A. Shutemov --- mm/gup.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mm/gup.c b/mm/gup.c index b94717977d17..bb0edfd77ca4 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1098,6 +1098,7 @@ static long __get_user_pages(struct mm_struct *mm, long ret = 0, i = 0; struct vm_area_struct *vma = NULL; struct follow_page_context ctx = { NULL }; + unsigned long unpresent = 0; if (!nr_pages) return 0; @@ -1182,6 +1183,8 @@ static long __get_user_pages(struct mm_struct *mm, case -EHWPOISON: goto out; case -ENOENT: + /* ctx.page_mask is 0 */ + unpresent += 1; goto next_page; } BUG(); @@ -1216,7 +1219,7 @@ static long __get_user_pages(struct mm_struct *mm, out: if (ctx.pgmap) put_dev_pagemap(ctx.pgmap); - return i ? i : ret; + return (i - unpresent) ? (i - unpresent) : ret; } static bool vma_permits_fault(struct vm_area_struct *vma,