Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2624094pxk; Sun, 20 Sep 2020 10:42:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzWw+F6oj0ChFb12Ryh0fAGZPJ3B03f8DM9wzURv/LFsdF4wbDI2/Nsh9CJkO1gapxrVkeQ X-Received: by 2002:a50:8062:: with SMTP id 89mr47479051eda.241.1600623729444; Sun, 20 Sep 2020 10:42:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600623729; cv=none; d=google.com; s=arc-20160816; b=sAKmTENPeqxTOw2CPslQmbMa7fgYx9410NWvLZKW7ir6G1kh1HbMHI+J40QbFlZ2g+ GQMBbb2MGi+l0KfXoOc7NELMuBx1u1ftoRILVv3lojDeW9fMrQf2NcgRu/YeSW/XmoVo wMs9B/Mb2I/8PRK8J1CxebHqq71J20eGv+TI6kEpEz+CrHiMIBO3JpsQVrfd4u/umNns TtB+l/BnQUnqNWsbDIJht23Fe2O41xwxaJBT/HsDLS4ZmcNp+5xwJ41/q8emeJ3Tr1kc KhlFQWXttbTktal8va1ncY5kIK27HdLZqyG2kpVEIFFpV3DCHFNcFE0Qc+7rSr5zsy16 jgCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=9ka0n+28OJXKPQLIVVR5Idhk3egkf6ni8LHSNLSNto8=; b=bNbT4hwJsORxw9slJg0waCdpQIn9uEhRImSrP9kiHbla0Q4N+v7pVnRwLDB7dLBv02 OyJwoak6EHi6bMyWTLr2sW7Kh+HF7hXRcYD4pQDJ3LTgTA0SWJq4UjDyiXRKDiZPMbB0 fZyxyZGvzmiHsV7k5cqn9+L01ImhPLBKWQ/WXipzvkwc0m8f1p4SGTZE9ZeOJD/fAthp uYpF3ner/YiRANta83vLZIGyMo+fuTZOkJWyD5lhkH1QD2mwX+mINQomeY4+OJq3Ywc0 K1WrEiZKx36N2IzkTHIT4I6TblPaJd7Cf7SdwlqTe89UinevdlRx8qemy1UPN6Hrndsd abDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="QNCArf/v"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m11si3906306edp.265.2020.09.20.10.41.45; Sun, 20 Sep 2020 10:42:09 -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=@linutronix.de header.s=2020 header.b="QNCArf/v"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726267AbgITRkq (ORCPT + 99 others); Sun, 20 Sep 2020 13:40:46 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:46884 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726037AbgITRkp (ORCPT ); Sun, 20 Sep 2020 13:40:45 -0400 From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1600623642; 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=9ka0n+28OJXKPQLIVVR5Idhk3egkf6ni8LHSNLSNto8=; b=QNCArf/vGifXO0hggd/oZT3co789XMX68FBpCZc9ELpUzjSb976p4qnrJXsUs4Wtn54Oe5 cREp7rnAnzQ2lkpB3S51Szog5eqcRviWtdYmN6anOoL9fmTtcQqDtDSWpVqORSo+xe9eNI ov8P0XhtU/ulbV28ywMlU0xr6kjRfIcVtmCVZ2FKw+DGK6aL/ezJuFiklF8NhXvAMiIh49 yrsZFHHw6uzr03zNYpu3ftPmTuZ7RyM2m1FWCRDFflQbJ/wuC7iq3YbPgWy/7QFdvivYSQ /Lqxr0wjpHyMFoiBEk4JJ16r2HL2cAu9GKsxBmw9WWw39VkpbDCGdel1jZCIGw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1600623642; 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=9ka0n+28OJXKPQLIVVR5Idhk3egkf6ni8LHSNLSNto8=; b=pomJJyR0iiUjZA6jjfbK7rYJBQcHpFL04sHLhdkrrobwFUG9h3ONNeKC4FHSzSIeasL6F6 5LIRCzwKaerEOICg== To: Linus Torvalds Cc: LKML , linux-arch , Paul McKenney , the arch/x86 maintainers , Sebastian Andrzej Siewior , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Will Deacon , Andrew Morton , Linux-MM , Russell King , Linux ARM , Chris Zankel , Max Filippov , linux-xtensa@linux-xtensa.org, Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , David Airlie , Daniel Vetter , intel-gfx , dri-devel , Ard Biesheuvel , Herbert Xu , Vineet Gupta , "open list\:SYNOPSYS ARC ARCHITECTURE" , Arnd Bergmann , Guo Ren , linux-csky@vger.kernel.org, Michal Simek , Thomas Bogendoerfer , linux-mips@vger.kernel.org, Nick Hu , Greentime Hu , Vincent Chen , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , linuxppc-dev , "David S. Miller" , linux-sparc Subject: Re: [patch RFC 00/15] mm/highmem: Provide a preemptible variant of kmap_atomic & friends In-Reply-To: References: <20200919091751.011116649@linutronix.de> <87mu1lc5mp.fsf@nanos.tec.linutronix.de> <87k0wode9a.fsf@nanos.tec.linutronix.de> Date: Sun, 20 Sep 2020 19:40:41 +0200 Message-ID: <87eemwcpnq.fsf@nanos.tec.linutronix.de> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Sep 20 2020 at 09:57, Linus Torvalds wrote: > On Sun, Sep 20, 2020 at 1:49 AM Thomas Gleixner wrote: > Btw, looking at the stack code, Ithink your new implementation of it > is a bit scary: > > static inline int kmap_atomic_idx_push(void) > { > - int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1; > + int idx = current->kmap_ctrl.idx++; > > and now that 'current->kmap_ctrl.idx' is not atomic wrt > > (a) NMI's (this may be ok, maybe we never do kmaps in NMIs, and with > nesting I think it's fine anyway - the NMI will undo whatever it did) Right. Nesting should be a non issue, but I don't think we have kmap_atomic() in NMI context. > (b) the prev/next switch > > And that (b) part worries me. You do the kmap_switch_temporary() to > switch the entries, but you do that *separately* from actually > switching 'current' to the new value. > > So kmap_switch_temporary() looks safe, but I don't think it actually > is. Because while it first unmaps the old entries and then remaps the > new ones, an interrupt can come in, and at that point it matters what > is *CURRENT*. > > And regardless of whether 'current' is 'prev' or 'next', that > kmap_switch_temporary() loop may be doing the wrong thing, depending > on which one had the deeper stack. The interrupt will be using > whatever "current->kmap_ctrl.idx" is, but that might overwrite entries > that are in the process of being restored (if current is still 'prev', > but kmap_switch_temporary() is in the "restore @next's kmaps" pgase), > or it might stomp on entries that have been pte_clear()'ed by the > 'prev' thing. Duh yes. Never thought about that. > Alternatively, that process counter would need about a hundred lines > of commentary about exactly why it's safe. Because I don't think it > is. I think the more obvious solution is to split the whole exercise: schedule() prepare_switch() unmap() switch_to() finish_switch() map() That's safe because neither the unmap() nor the map() code changes kmap_ctrl.idx. So if there is an interrupt coming in between unmap() and switch_to() then a kmap_local() there will use the next entry. So we could even do the unmap() with interrupts enabled (preemption disabled). Same for the map() part. To explain that we need only a few lines of commentry methinks. Thanks, tglx